2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
43 #include "transaction.h"
44 #include "btrfs_inode.h"
46 #include "print-tree.h"
48 #include "ordered-data.h"
51 #include "compression.h"
53 #include "free-space-cache.h"
54 #include "inode-map.h"
56 struct btrfs_iget_args {
58 struct btrfs_root *root;
61 static const struct inode_operations btrfs_dir_inode_operations;
62 static const struct inode_operations btrfs_symlink_inode_operations;
63 static const struct inode_operations btrfs_dir_ro_inode_operations;
64 static const struct inode_operations btrfs_special_inode_operations;
65 static const struct inode_operations btrfs_file_inode_operations;
66 static const struct address_space_operations btrfs_aops;
67 static const struct address_space_operations btrfs_symlink_aops;
68 static const struct file_operations btrfs_dir_file_operations;
69 static struct extent_io_ops btrfs_extent_io_ops;
71 static struct kmem_cache *btrfs_inode_cachep;
72 struct kmem_cache *btrfs_trans_handle_cachep;
73 struct kmem_cache *btrfs_transaction_cachep;
74 struct kmem_cache *btrfs_path_cachep;
75 struct kmem_cache *btrfs_free_space_cachep;
78 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
79 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
80 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
81 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
82 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
83 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
84 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
85 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88 static int btrfs_setsize(struct inode *inode, loff_t newsize);
89 static int btrfs_truncate(struct inode *inode);
90 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
91 static noinline int cow_file_range(struct inode *inode,
92 struct page *locked_page,
93 u64 start, u64 end, int *page_started,
94 unsigned long *nr_written, int unlock);
96 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
97 struct inode *inode, struct inode *dir,
98 const struct qstr *qstr)
102 err = btrfs_init_acl(trans, inode, dir);
104 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
109 * this does all the hard work for inserting an inline extent into
110 * the btree. The caller should have done a btrfs_drop_extents so that
111 * no overlapping inline items exist in the btree
113 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
114 struct btrfs_root *root, struct inode *inode,
115 u64 start, size_t size, size_t compressed_size,
117 struct page **compressed_pages)
119 struct btrfs_key key;
120 struct btrfs_path *path;
121 struct extent_buffer *leaf;
122 struct page *page = NULL;
125 struct btrfs_file_extent_item *ei;
128 size_t cur_size = size;
130 unsigned long offset;
132 if (compressed_size && compressed_pages)
133 cur_size = compressed_size;
135 path = btrfs_alloc_path();
139 path->leave_spinning = 1;
140 btrfs_set_trans_block_group(trans, inode);
142 key.objectid = btrfs_ino(inode);
144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
145 datasize = btrfs_file_extent_calc_inline_size(cur_size);
147 inode_add_bytes(inode, size);
148 ret = btrfs_insert_empty_item(trans, root, path, &key,
155 leaf = path->nodes[0];
156 ei = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_file_extent_item);
158 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
159 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
160 btrfs_set_file_extent_encryption(leaf, ei, 0);
161 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163 ptr = btrfs_file_extent_inline_start(ei);
165 if (compress_type != BTRFS_COMPRESS_NONE) {
168 while (compressed_size > 0) {
169 cpage = compressed_pages[i];
170 cur_size = min_t(unsigned long, compressed_size,
173 kaddr = kmap_atomic(cpage, KM_USER0);
174 write_extent_buffer(leaf, kaddr, ptr, cur_size);
175 kunmap_atomic(kaddr, KM_USER0);
179 compressed_size -= cur_size;
181 btrfs_set_file_extent_compression(leaf, ei,
184 page = find_get_page(inode->i_mapping,
185 start >> PAGE_CACHE_SHIFT);
186 btrfs_set_file_extent_compression(leaf, ei, 0);
187 kaddr = kmap_atomic(page, KM_USER0);
188 offset = start & (PAGE_CACHE_SIZE - 1);
189 write_extent_buffer(leaf, kaddr + offset, ptr, size);
190 kunmap_atomic(kaddr, KM_USER0);
191 page_cache_release(page);
193 btrfs_mark_buffer_dirty(leaf);
194 btrfs_free_path(path);
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
205 BTRFS_I(inode)->disk_i_size = inode->i_size;
206 btrfs_update_inode(trans, root, inode);
210 btrfs_free_path(path);
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
220 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
221 struct btrfs_root *root,
222 struct inode *inode, u64 start, u64 end,
223 size_t compressed_size, int compress_type,
224 struct page **compressed_pages)
226 u64 isize = i_size_read(inode);
227 u64 actual_end = min(end + 1, isize);
228 u64 inline_len = actual_end - start;
229 u64 aligned_end = (end + root->sectorsize - 1) &
230 ~((u64)root->sectorsize - 1);
232 u64 data_len = inline_len;
236 data_len = compressed_size;
239 actual_end >= PAGE_CACHE_SIZE ||
240 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
242 (actual_end & (root->sectorsize - 1)) == 0) ||
244 data_len > root->fs_info->max_inline) {
248 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
252 if (isize > actual_end)
253 inline_len = min_t(u64, isize, actual_end);
254 ret = insert_inline_extent(trans, root, inode, start,
255 inline_len, compressed_size,
256 compress_type, compressed_pages);
258 btrfs_delalloc_release_metadata(inode, end + 1 - start);
259 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
263 struct async_extent {
268 unsigned long nr_pages;
270 struct list_head list;
275 struct btrfs_root *root;
276 struct page *locked_page;
279 struct list_head extents;
280 struct btrfs_work work;
283 static noinline int add_async_extent(struct async_cow *cow,
284 u64 start, u64 ram_size,
287 unsigned long nr_pages,
290 struct async_extent *async_extent;
292 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
293 BUG_ON(!async_extent);
294 async_extent->start = start;
295 async_extent->ram_size = ram_size;
296 async_extent->compressed_size = compressed_size;
297 async_extent->pages = pages;
298 async_extent->nr_pages = nr_pages;
299 async_extent->compress_type = compress_type;
300 list_add_tail(&async_extent->list, &cow->extents);
305 * we create compressed extents in two phases. The first
306 * phase compresses a range of pages that have already been
307 * locked (both pages and state bits are locked).
309 * This is done inside an ordered work queue, and the compression
310 * is spread across many cpus. The actual IO submission is step
311 * two, and the ordered work queue takes care of making sure that
312 * happens in the same order things were put onto the queue by
313 * writepages and friends.
315 * If this code finds it can't get good compression, it puts an
316 * entry onto the work queue to write the uncompressed bytes. This
317 * makes sure that both compressed inodes and uncompressed inodes
318 * are written in the same order that pdflush sent them down.
320 static noinline int compress_file_range(struct inode *inode,
321 struct page *locked_page,
323 struct async_cow *async_cow,
326 struct btrfs_root *root = BTRFS_I(inode)->root;
327 struct btrfs_trans_handle *trans;
329 u64 blocksize = root->sectorsize;
331 u64 isize = i_size_read(inode);
333 struct page **pages = NULL;
334 unsigned long nr_pages;
335 unsigned long nr_pages_ret = 0;
336 unsigned long total_compressed = 0;
337 unsigned long total_in = 0;
338 unsigned long max_compressed = 128 * 1024;
339 unsigned long max_uncompressed = 128 * 1024;
342 int compress_type = root->fs_info->compress_type;
344 actual_end = min_t(u64, isize, end + 1);
347 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
348 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
351 * we don't want to send crud past the end of i_size through
352 * compression, that's just a waste of CPU time. So, if the
353 * end of the file is before the start of our current
354 * requested range of bytes, we bail out to the uncompressed
355 * cleanup code that can deal with all of this.
357 * It isn't really the fastest way to fix things, but this is a
358 * very uncommon corner.
360 if (actual_end <= start)
361 goto cleanup_and_bail_uncompressed;
363 total_compressed = actual_end - start;
365 /* we want to make sure that amount of ram required to uncompress
366 * an extent is reasonable, so we limit the total size in ram
367 * of a compressed extent to 128k. This is a crucial number
368 * because it also controls how easily we can spread reads across
369 * cpus for decompression.
371 * We also want to make sure the amount of IO required to do
372 * a random read is reasonably small, so we limit the size of
373 * a compressed extent to 128k.
375 total_compressed = min(total_compressed, max_uncompressed);
376 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
377 num_bytes = max(blocksize, num_bytes);
382 * we do compression for mount -o compress and when the
383 * inode has not been flagged as nocompress. This flag can
384 * change at any time if we discover bad compression ratios.
386 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
387 (btrfs_test_opt(root, COMPRESS) ||
388 (BTRFS_I(inode)->force_compress) ||
389 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
391 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
394 if (BTRFS_I(inode)->force_compress)
395 compress_type = BTRFS_I(inode)->force_compress;
397 ret = btrfs_compress_pages(compress_type,
398 inode->i_mapping, start,
399 total_compressed, pages,
400 nr_pages, &nr_pages_ret,
406 unsigned long offset = total_compressed &
407 (PAGE_CACHE_SIZE - 1);
408 struct page *page = pages[nr_pages_ret - 1];
411 /* zero the tail end of the last page, we might be
412 * sending it down to disk
415 kaddr = kmap_atomic(page, KM_USER0);
416 memset(kaddr + offset, 0,
417 PAGE_CACHE_SIZE - offset);
418 kunmap_atomic(kaddr, KM_USER0);
424 trans = btrfs_join_transaction(root, 1);
425 BUG_ON(IS_ERR(trans));
426 btrfs_set_trans_block_group(trans, inode);
427 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
429 /* lets try to make an inline extent */
430 if (ret || total_in < (actual_end - start)) {
431 /* we didn't compress the entire range, try
432 * to make an uncompressed inline extent.
434 ret = cow_file_range_inline(trans, root, inode,
435 start, end, 0, 0, NULL);
437 /* try making a compressed inline extent */
438 ret = cow_file_range_inline(trans, root, inode,
441 compress_type, pages);
445 * inline extent creation worked, we don't need
446 * to create any more async work items. Unlock
447 * and free up our temp pages.
449 extent_clear_unlock_delalloc(inode,
450 &BTRFS_I(inode)->io_tree,
452 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
453 EXTENT_CLEAR_DELALLOC |
454 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
456 btrfs_end_transaction(trans, root);
459 btrfs_end_transaction(trans, root);
464 * we aren't doing an inline extent round the compressed size
465 * up to a block size boundary so the allocator does sane
468 total_compressed = (total_compressed + blocksize - 1) &
472 * one last check to make sure the compression is really a
473 * win, compare the page count read with the blocks on disk
475 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
476 ~(PAGE_CACHE_SIZE - 1);
477 if (total_compressed >= total_in) {
480 num_bytes = total_in;
483 if (!will_compress && pages) {
485 * the compression code ran but failed to make things smaller,
486 * free any pages it allocated and our page pointer array
488 for (i = 0; i < nr_pages_ret; i++) {
489 WARN_ON(pages[i]->mapping);
490 page_cache_release(pages[i]);
494 total_compressed = 0;
497 /* flag the file so we don't compress in the future */
498 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
499 !(BTRFS_I(inode)->force_compress)) {
500 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
506 /* the async work queues will take care of doing actual
507 * allocation on disk for these compressed pages,
508 * and will submit them to the elevator.
510 add_async_extent(async_cow, start, num_bytes,
511 total_compressed, pages, nr_pages_ret,
514 if (start + num_bytes < end) {
521 cleanup_and_bail_uncompressed:
523 * No compression, but we still need to write the pages in
524 * the file we've been given so far. redirty the locked
525 * page if it corresponds to our extent and set things up
526 * for the async work queue to run cow_file_range to do
527 * the normal delalloc dance
529 if (page_offset(locked_page) >= start &&
530 page_offset(locked_page) <= end) {
531 __set_page_dirty_nobuffers(locked_page);
532 /* unlocked later on in the async handlers */
534 add_async_extent(async_cow, start, end - start + 1,
535 0, NULL, 0, BTRFS_COMPRESS_NONE);
543 for (i = 0; i < nr_pages_ret; i++) {
544 WARN_ON(pages[i]->mapping);
545 page_cache_release(pages[i]);
553 * phase two of compressed writeback. This is the ordered portion
554 * of the code, which only gets called in the order the work was
555 * queued. We walk all the async extents created by compress_file_range
556 * and send them down to the disk.
558 static noinline int submit_compressed_extents(struct inode *inode,
559 struct async_cow *async_cow)
561 struct async_extent *async_extent;
563 struct btrfs_trans_handle *trans;
564 struct btrfs_key ins;
565 struct extent_map *em;
566 struct btrfs_root *root = BTRFS_I(inode)->root;
567 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
568 struct extent_io_tree *io_tree;
571 if (list_empty(&async_cow->extents))
575 while (!list_empty(&async_cow->extents)) {
576 async_extent = list_entry(async_cow->extents.next,
577 struct async_extent, list);
578 list_del(&async_extent->list);
580 io_tree = &BTRFS_I(inode)->io_tree;
583 /* did the compression code fall back to uncompressed IO? */
584 if (!async_extent->pages) {
585 int page_started = 0;
586 unsigned long nr_written = 0;
588 lock_extent(io_tree, async_extent->start,
589 async_extent->start +
590 async_extent->ram_size - 1, GFP_NOFS);
592 /* allocate blocks */
593 ret = cow_file_range(inode, async_cow->locked_page,
595 async_extent->start +
596 async_extent->ram_size - 1,
597 &page_started, &nr_written, 0);
600 * if page_started, cow_file_range inserted an
601 * inline extent and took care of all the unlocking
602 * and IO for us. Otherwise, we need to submit
603 * all those pages down to the drive.
605 if (!page_started && !ret)
606 extent_write_locked_range(io_tree,
607 inode, async_extent->start,
608 async_extent->start +
609 async_extent->ram_size - 1,
617 lock_extent(io_tree, async_extent->start,
618 async_extent->start + async_extent->ram_size - 1,
621 trans = btrfs_join_transaction(root, 1);
622 BUG_ON(IS_ERR(trans));
623 ret = btrfs_reserve_extent(trans, root,
624 async_extent->compressed_size,
625 async_extent->compressed_size,
628 btrfs_end_transaction(trans, root);
632 for (i = 0; i < async_extent->nr_pages; i++) {
633 WARN_ON(async_extent->pages[i]->mapping);
634 page_cache_release(async_extent->pages[i]);
636 kfree(async_extent->pages);
637 async_extent->nr_pages = 0;
638 async_extent->pages = NULL;
639 unlock_extent(io_tree, async_extent->start,
640 async_extent->start +
641 async_extent->ram_size - 1, GFP_NOFS);
646 * here we're doing allocation and writeback of the
649 btrfs_drop_extent_cache(inode, async_extent->start,
650 async_extent->start +
651 async_extent->ram_size - 1, 0);
653 em = alloc_extent_map(GFP_NOFS);
655 em->start = async_extent->start;
656 em->len = async_extent->ram_size;
657 em->orig_start = em->start;
659 em->block_start = ins.objectid;
660 em->block_len = ins.offset;
661 em->bdev = root->fs_info->fs_devices->latest_bdev;
662 em->compress_type = async_extent->compress_type;
663 set_bit(EXTENT_FLAG_PINNED, &em->flags);
664 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
667 write_lock(&em_tree->lock);
668 ret = add_extent_mapping(em_tree, em);
669 write_unlock(&em_tree->lock);
670 if (ret != -EEXIST) {
674 btrfs_drop_extent_cache(inode, async_extent->start,
675 async_extent->start +
676 async_extent->ram_size - 1, 0);
679 ret = btrfs_add_ordered_extent_compress(inode,
682 async_extent->ram_size,
684 BTRFS_ORDERED_COMPRESSED,
685 async_extent->compress_type);
689 * clear dirty, set writeback and unlock the pages.
691 extent_clear_unlock_delalloc(inode,
692 &BTRFS_I(inode)->io_tree,
694 async_extent->start +
695 async_extent->ram_size - 1,
696 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
697 EXTENT_CLEAR_UNLOCK |
698 EXTENT_CLEAR_DELALLOC |
699 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
701 ret = btrfs_submit_compressed_write(inode,
703 async_extent->ram_size,
705 ins.offset, async_extent->pages,
706 async_extent->nr_pages);
709 alloc_hint = ins.objectid + ins.offset;
717 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
720 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
721 struct extent_map *em;
724 read_lock(&em_tree->lock);
725 em = search_extent_mapping(em_tree, start, num_bytes);
728 * if block start isn't an actual block number then find the
729 * first block in this inode and use that as a hint. If that
730 * block is also bogus then just don't worry about it.
732 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
734 em = search_extent_mapping(em_tree, 0, 0);
735 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
736 alloc_hint = em->block_start;
740 alloc_hint = em->block_start;
744 read_unlock(&em_tree->lock);
749 static inline bool is_free_space_inode(struct btrfs_root *root,
752 if (root == root->fs_info->tree_root ||
753 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
759 * when extent_io.c finds a delayed allocation range in the file,
760 * the call backs end up in this code. The basic idea is to
761 * allocate extents on disk for the range, and create ordered data structs
762 * in ram to track those extents.
764 * locked_page is the page that writepage had locked already. We use
765 * it to make sure we don't do extra locks or unlocks.
767 * *page_started is set to one if we unlock locked_page and do everything
768 * required to start IO on it. It may be clean and already done with
771 static noinline int cow_file_range(struct inode *inode,
772 struct page *locked_page,
773 u64 start, u64 end, int *page_started,
774 unsigned long *nr_written,
777 struct btrfs_root *root = BTRFS_I(inode)->root;
778 struct btrfs_trans_handle *trans;
781 unsigned long ram_size;
784 u64 blocksize = root->sectorsize;
785 struct btrfs_key ins;
786 struct extent_map *em;
787 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
790 BUG_ON(is_free_space_inode(root, inode));
791 trans = btrfs_join_transaction(root, 1);
792 BUG_ON(IS_ERR(trans));
793 btrfs_set_trans_block_group(trans, inode);
794 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
796 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
797 num_bytes = max(blocksize, num_bytes);
798 disk_num_bytes = num_bytes;
802 /* lets try to make an inline extent */
803 ret = cow_file_range_inline(trans, root, inode,
804 start, end, 0, 0, NULL);
806 extent_clear_unlock_delalloc(inode,
807 &BTRFS_I(inode)->io_tree,
809 EXTENT_CLEAR_UNLOCK_PAGE |
810 EXTENT_CLEAR_UNLOCK |
811 EXTENT_CLEAR_DELALLOC |
813 EXTENT_SET_WRITEBACK |
814 EXTENT_END_WRITEBACK);
816 *nr_written = *nr_written +
817 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
824 BUG_ON(disk_num_bytes >
825 btrfs_super_total_bytes(&root->fs_info->super_copy));
827 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
828 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
830 while (disk_num_bytes > 0) {
833 cur_alloc_size = disk_num_bytes;
834 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
835 root->sectorsize, 0, alloc_hint,
839 em = alloc_extent_map(GFP_NOFS);
842 em->orig_start = em->start;
843 ram_size = ins.offset;
844 em->len = ins.offset;
846 em->block_start = ins.objectid;
847 em->block_len = ins.offset;
848 em->bdev = root->fs_info->fs_devices->latest_bdev;
849 set_bit(EXTENT_FLAG_PINNED, &em->flags);
852 write_lock(&em_tree->lock);
853 ret = add_extent_mapping(em_tree, em);
854 write_unlock(&em_tree->lock);
855 if (ret != -EEXIST) {
859 btrfs_drop_extent_cache(inode, start,
860 start + ram_size - 1, 0);
863 cur_alloc_size = ins.offset;
864 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
865 ram_size, cur_alloc_size, 0);
868 if (root->root_key.objectid ==
869 BTRFS_DATA_RELOC_TREE_OBJECTID) {
870 ret = btrfs_reloc_clone_csums(inode, start,
875 if (disk_num_bytes < cur_alloc_size)
878 /* we're not doing compressed IO, don't unlock the first
879 * page (which the caller expects to stay locked), don't
880 * clear any dirty bits and don't set any writeback bits
882 * Do set the Private2 bit so we know this page was properly
883 * setup for writepage
885 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
886 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
889 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
890 start, start + ram_size - 1,
892 disk_num_bytes -= cur_alloc_size;
893 num_bytes -= cur_alloc_size;
894 alloc_hint = ins.objectid + ins.offset;
895 start += cur_alloc_size;
899 btrfs_end_transaction(trans, root);
905 * work queue call back to started compression on a file and pages
907 static noinline void async_cow_start(struct btrfs_work *work)
909 struct async_cow *async_cow;
911 async_cow = container_of(work, struct async_cow, work);
913 compress_file_range(async_cow->inode, async_cow->locked_page,
914 async_cow->start, async_cow->end, async_cow,
917 async_cow->inode = NULL;
921 * work queue call back to submit previously compressed pages
923 static noinline void async_cow_submit(struct btrfs_work *work)
925 struct async_cow *async_cow;
926 struct btrfs_root *root;
927 unsigned long nr_pages;
929 async_cow = container_of(work, struct async_cow, work);
931 root = async_cow->root;
932 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
935 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
937 if (atomic_read(&root->fs_info->async_delalloc_pages) <
939 waitqueue_active(&root->fs_info->async_submit_wait))
940 wake_up(&root->fs_info->async_submit_wait);
942 if (async_cow->inode)
943 submit_compressed_extents(async_cow->inode, async_cow);
946 static noinline void async_cow_free(struct btrfs_work *work)
948 struct async_cow *async_cow;
949 async_cow = container_of(work, struct async_cow, work);
953 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
954 u64 start, u64 end, int *page_started,
955 unsigned long *nr_written)
957 struct async_cow *async_cow;
958 struct btrfs_root *root = BTRFS_I(inode)->root;
959 unsigned long nr_pages;
961 int limit = 10 * 1024 * 1042;
963 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
964 1, 0, NULL, GFP_NOFS);
965 while (start < end) {
966 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
968 async_cow->inode = inode;
969 async_cow->root = root;
970 async_cow->locked_page = locked_page;
971 async_cow->start = start;
973 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
976 cur_end = min(end, start + 512 * 1024 - 1);
978 async_cow->end = cur_end;
979 INIT_LIST_HEAD(&async_cow->extents);
981 async_cow->work.func = async_cow_start;
982 async_cow->work.ordered_func = async_cow_submit;
983 async_cow->work.ordered_free = async_cow_free;
984 async_cow->work.flags = 0;
986 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
988 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
990 btrfs_queue_worker(&root->fs_info->delalloc_workers,
993 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
994 wait_event(root->fs_info->async_submit_wait,
995 (atomic_read(&root->fs_info->async_delalloc_pages) <
999 while (atomic_read(&root->fs_info->async_submit_draining) &&
1000 atomic_read(&root->fs_info->async_delalloc_pages)) {
1001 wait_event(root->fs_info->async_submit_wait,
1002 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1006 *nr_written += nr_pages;
1007 start = cur_end + 1;
1013 static noinline int csum_exist_in_range(struct btrfs_root *root,
1014 u64 bytenr, u64 num_bytes)
1017 struct btrfs_ordered_sum *sums;
1020 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1021 bytenr + num_bytes - 1, &list);
1022 if (ret == 0 && list_empty(&list))
1025 while (!list_empty(&list)) {
1026 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1027 list_del(&sums->list);
1034 * when nowcow writeback call back. This checks for snapshots or COW copies
1035 * of the extents that exist in the file, and COWs the file as required.
1037 * If no cow copies or snapshots exist, we write directly to the existing
1040 static noinline int run_delalloc_nocow(struct inode *inode,
1041 struct page *locked_page,
1042 u64 start, u64 end, int *page_started, int force,
1043 unsigned long *nr_written)
1045 struct btrfs_root *root = BTRFS_I(inode)->root;
1046 struct btrfs_trans_handle *trans;
1047 struct extent_buffer *leaf;
1048 struct btrfs_path *path;
1049 struct btrfs_file_extent_item *fi;
1050 struct btrfs_key found_key;
1063 u64 ino = btrfs_ino(inode);
1065 path = btrfs_alloc_path();
1068 nolock = is_free_space_inode(root, inode);
1071 trans = btrfs_join_transaction_nolock(root, 1);
1073 trans = btrfs_join_transaction(root, 1);
1074 BUG_ON(IS_ERR(trans));
1076 cow_start = (u64)-1;
1079 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1082 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1083 leaf = path->nodes[0];
1084 btrfs_item_key_to_cpu(leaf, &found_key,
1085 path->slots[0] - 1);
1086 if (found_key.objectid == ino &&
1087 found_key.type == BTRFS_EXTENT_DATA_KEY)
1092 leaf = path->nodes[0];
1093 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1094 ret = btrfs_next_leaf(root, path);
1099 leaf = path->nodes[0];
1105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1107 if (found_key.objectid > ino ||
1108 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1109 found_key.offset > end)
1112 if (found_key.offset > cur_offset) {
1113 extent_end = found_key.offset;
1118 fi = btrfs_item_ptr(leaf, path->slots[0],
1119 struct btrfs_file_extent_item);
1120 extent_type = btrfs_file_extent_type(leaf, fi);
1122 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1123 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1124 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1125 extent_offset = btrfs_file_extent_offset(leaf, fi);
1126 extent_end = found_key.offset +
1127 btrfs_file_extent_num_bytes(leaf, fi);
1128 if (extent_end <= start) {
1132 if (disk_bytenr == 0)
1134 if (btrfs_file_extent_compression(leaf, fi) ||
1135 btrfs_file_extent_encryption(leaf, fi) ||
1136 btrfs_file_extent_other_encoding(leaf, fi))
1138 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1140 if (btrfs_extent_readonly(root, disk_bytenr))
1142 if (btrfs_cross_ref_exist(trans, root, ino,
1144 extent_offset, disk_bytenr))
1146 disk_bytenr += extent_offset;
1147 disk_bytenr += cur_offset - found_key.offset;
1148 num_bytes = min(end + 1, extent_end) - cur_offset;
1150 * force cow if csum exists in the range.
1151 * this ensure that csum for a given extent are
1152 * either valid or do not exist.
1154 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1157 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1158 extent_end = found_key.offset +
1159 btrfs_file_extent_inline_len(leaf, fi);
1160 extent_end = ALIGN(extent_end, root->sectorsize);
1165 if (extent_end <= start) {
1170 if (cow_start == (u64)-1)
1171 cow_start = cur_offset;
1172 cur_offset = extent_end;
1173 if (cur_offset > end)
1179 btrfs_release_path(root, path);
1180 if (cow_start != (u64)-1) {
1181 ret = cow_file_range(inode, locked_page, cow_start,
1182 found_key.offset - 1, page_started,
1185 cow_start = (u64)-1;
1188 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1189 struct extent_map *em;
1190 struct extent_map_tree *em_tree;
1191 em_tree = &BTRFS_I(inode)->extent_tree;
1192 em = alloc_extent_map(GFP_NOFS);
1194 em->start = cur_offset;
1195 em->orig_start = em->start;
1196 em->len = num_bytes;
1197 em->block_len = num_bytes;
1198 em->block_start = disk_bytenr;
1199 em->bdev = root->fs_info->fs_devices->latest_bdev;
1200 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1202 write_lock(&em_tree->lock);
1203 ret = add_extent_mapping(em_tree, em);
1204 write_unlock(&em_tree->lock);
1205 if (ret != -EEXIST) {
1206 free_extent_map(em);
1209 btrfs_drop_extent_cache(inode, em->start,
1210 em->start + em->len - 1, 0);
1212 type = BTRFS_ORDERED_PREALLOC;
1214 type = BTRFS_ORDERED_NOCOW;
1217 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1218 num_bytes, num_bytes, type);
1221 if (root->root_key.objectid ==
1222 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1223 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1228 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1229 cur_offset, cur_offset + num_bytes - 1,
1230 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1231 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1232 EXTENT_SET_PRIVATE2);
1233 cur_offset = extent_end;
1234 if (cur_offset > end)
1237 btrfs_release_path(root, path);
1239 if (cur_offset <= end && cow_start == (u64)-1)
1240 cow_start = cur_offset;
1241 if (cow_start != (u64)-1) {
1242 ret = cow_file_range(inode, locked_page, cow_start, end,
1243 page_started, nr_written, 1);
1248 ret = btrfs_end_transaction_nolock(trans, root);
1251 ret = btrfs_end_transaction(trans, root);
1254 btrfs_free_path(path);
1259 * extent_io.c call back to do delayed allocation processing
1261 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1262 u64 start, u64 end, int *page_started,
1263 unsigned long *nr_written)
1266 struct btrfs_root *root = BTRFS_I(inode)->root;
1268 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1269 ret = run_delalloc_nocow(inode, locked_page, start, end,
1270 page_started, 1, nr_written);
1271 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1272 ret = run_delalloc_nocow(inode, locked_page, start, end,
1273 page_started, 0, nr_written);
1274 else if (!btrfs_test_opt(root, COMPRESS) &&
1275 !(BTRFS_I(inode)->force_compress) &&
1276 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
1277 ret = cow_file_range(inode, locked_page, start, end,
1278 page_started, nr_written, 1);
1280 ret = cow_file_range_async(inode, locked_page, start, end,
1281 page_started, nr_written);
1285 static int btrfs_split_extent_hook(struct inode *inode,
1286 struct extent_state *orig, u64 split)
1288 /* not delalloc, ignore it */
1289 if (!(orig->state & EXTENT_DELALLOC))
1292 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1297 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1298 * extents so we can keep track of new extents that are just merged onto old
1299 * extents, such as when we are doing sequential writes, so we can properly
1300 * account for the metadata space we'll need.
1302 static int btrfs_merge_extent_hook(struct inode *inode,
1303 struct extent_state *new,
1304 struct extent_state *other)
1306 /* not delalloc, ignore it */
1307 if (!(other->state & EXTENT_DELALLOC))
1310 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1315 * extent_io.c set_bit_hook, used to track delayed allocation
1316 * bytes in this file, and to maintain the list of inodes that
1317 * have pending delalloc work to be done.
1319 static int btrfs_set_bit_hook(struct inode *inode,
1320 struct extent_state *state, int *bits)
1324 * set_bit and clear bit hooks normally require _irqsave/restore
1325 * but in this case, we are only testeing for the DELALLOC
1326 * bit, which is only set or cleared with irqs on
1328 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1329 struct btrfs_root *root = BTRFS_I(inode)->root;
1330 u64 len = state->end + 1 - state->start;
1331 bool do_list = !is_free_space_inode(root, inode);
1333 if (*bits & EXTENT_FIRST_DELALLOC)
1334 *bits &= ~EXTENT_FIRST_DELALLOC;
1336 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1338 spin_lock(&root->fs_info->delalloc_lock);
1339 BTRFS_I(inode)->delalloc_bytes += len;
1340 root->fs_info->delalloc_bytes += len;
1341 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1342 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1343 &root->fs_info->delalloc_inodes);
1345 spin_unlock(&root->fs_info->delalloc_lock);
1351 * extent_io.c clear_bit_hook, see set_bit_hook for why
1353 static int btrfs_clear_bit_hook(struct inode *inode,
1354 struct extent_state *state, int *bits)
1357 * set_bit and clear bit hooks normally require _irqsave/restore
1358 * but in this case, we are only testeing for the DELALLOC
1359 * bit, which is only set or cleared with irqs on
1361 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1362 struct btrfs_root *root = BTRFS_I(inode)->root;
1363 u64 len = state->end + 1 - state->start;
1364 bool do_list = !is_free_space_inode(root, inode);
1366 if (*bits & EXTENT_FIRST_DELALLOC)
1367 *bits &= ~EXTENT_FIRST_DELALLOC;
1368 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1369 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1371 if (*bits & EXTENT_DO_ACCOUNTING)
1372 btrfs_delalloc_release_metadata(inode, len);
1374 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1376 btrfs_free_reserved_data_space(inode, len);
1378 spin_lock(&root->fs_info->delalloc_lock);
1379 root->fs_info->delalloc_bytes -= len;
1380 BTRFS_I(inode)->delalloc_bytes -= len;
1382 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1383 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1384 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1386 spin_unlock(&root->fs_info->delalloc_lock);
1392 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1393 * we don't create bios that span stripes or chunks
1395 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1396 size_t size, struct bio *bio,
1397 unsigned long bio_flags)
1399 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1400 struct btrfs_mapping_tree *map_tree;
1401 u64 logical = (u64)bio->bi_sector << 9;
1406 if (bio_flags & EXTENT_BIO_COMPRESSED)
1409 length = bio->bi_size;
1410 map_tree = &root->fs_info->mapping_tree;
1411 map_length = length;
1412 ret = btrfs_map_block(map_tree, READ, logical,
1413 &map_length, NULL, 0);
1415 if (map_length < length + size)
1421 * in order to insert checksums into the metadata in large chunks,
1422 * we wait until bio submission time. All the pages in the bio are
1423 * checksummed and sums are attached onto the ordered extent record.
1425 * At IO completion time the cums attached on the ordered extent record
1426 * are inserted into the btree
1428 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1429 struct bio *bio, int mirror_num,
1430 unsigned long bio_flags,
1433 struct btrfs_root *root = BTRFS_I(inode)->root;
1436 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1442 * in order to insert checksums into the metadata in large chunks,
1443 * we wait until bio submission time. All the pages in the bio are
1444 * checksummed and sums are attached onto the ordered extent record.
1446 * At IO completion time the cums attached on the ordered extent record
1447 * are inserted into the btree
1449 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1450 int mirror_num, unsigned long bio_flags,
1453 struct btrfs_root *root = BTRFS_I(inode)->root;
1454 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1458 * extent_io.c submission hook. This does the right thing for csum calculation
1459 * on write, or reading the csums from the tree before a read
1461 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1462 int mirror_num, unsigned long bio_flags,
1465 struct btrfs_root *root = BTRFS_I(inode)->root;
1469 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1471 if (is_free_space_inode(root, inode))
1472 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1474 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1477 if (!(rw & REQ_WRITE)) {
1478 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1479 return btrfs_submit_compressed_read(inode, bio,
1480 mirror_num, bio_flags);
1481 } else if (!skip_sum) {
1482 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1487 } else if (!skip_sum) {
1488 /* csum items have already been cloned */
1489 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1491 /* we're doing a write, do the async checksumming */
1492 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1493 inode, rw, bio, mirror_num,
1494 bio_flags, bio_offset,
1495 __btrfs_submit_bio_start,
1496 __btrfs_submit_bio_done);
1500 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1504 * given a list of ordered sums record them in the inode. This happens
1505 * at IO completion time based on sums calculated at bio submission time.
1507 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1508 struct inode *inode, u64 file_offset,
1509 struct list_head *list)
1511 struct btrfs_ordered_sum *sum;
1513 btrfs_set_trans_block_group(trans, inode);
1515 list_for_each_entry(sum, list, list) {
1516 btrfs_csum_file_blocks(trans,
1517 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1522 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1523 struct extent_state **cached_state)
1525 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1527 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1528 cached_state, GFP_NOFS);
1531 /* see btrfs_writepage_start_hook for details on why this is required */
1532 struct btrfs_writepage_fixup {
1534 struct btrfs_work work;
1537 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1539 struct btrfs_writepage_fixup *fixup;
1540 struct btrfs_ordered_extent *ordered;
1541 struct extent_state *cached_state = NULL;
1543 struct inode *inode;
1547 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1551 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1552 ClearPageChecked(page);
1556 inode = page->mapping->host;
1557 page_start = page_offset(page);
1558 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1560 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1561 &cached_state, GFP_NOFS);
1563 /* already ordered? We're done */
1564 if (PagePrivate2(page))
1567 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1569 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1570 page_end, &cached_state, GFP_NOFS);
1572 btrfs_start_ordered_extent(inode, ordered, 1);
1577 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1578 ClearPageChecked(page);
1580 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1581 &cached_state, GFP_NOFS);
1584 page_cache_release(page);
1589 * There are a few paths in the higher layers of the kernel that directly
1590 * set the page dirty bit without asking the filesystem if it is a
1591 * good idea. This causes problems because we want to make sure COW
1592 * properly happens and the data=ordered rules are followed.
1594 * In our case any range that doesn't have the ORDERED bit set
1595 * hasn't been properly setup for IO. We kick off an async process
1596 * to fix it up. The async helper will wait for ordered extents, set
1597 * the delalloc bit and make it safe to write the page.
1599 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1601 struct inode *inode = page->mapping->host;
1602 struct btrfs_writepage_fixup *fixup;
1603 struct btrfs_root *root = BTRFS_I(inode)->root;
1605 /* this page is properly in the ordered list */
1606 if (TestClearPagePrivate2(page))
1609 if (PageChecked(page))
1612 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1616 SetPageChecked(page);
1617 page_cache_get(page);
1618 fixup->work.func = btrfs_writepage_fixup_worker;
1620 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1624 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1625 struct inode *inode, u64 file_pos,
1626 u64 disk_bytenr, u64 disk_num_bytes,
1627 u64 num_bytes, u64 ram_bytes,
1628 u8 compression, u8 encryption,
1629 u16 other_encoding, int extent_type)
1631 struct btrfs_root *root = BTRFS_I(inode)->root;
1632 struct btrfs_file_extent_item *fi;
1633 struct btrfs_path *path;
1634 struct extent_buffer *leaf;
1635 struct btrfs_key ins;
1639 path = btrfs_alloc_path();
1642 path->leave_spinning = 1;
1645 * we may be replacing one extent in the tree with another.
1646 * The new extent is pinned in the extent map, and we don't want
1647 * to drop it from the cache until it is completely in the btree.
1649 * So, tell btrfs_drop_extents to leave this extent in the cache.
1650 * the caller is expected to unpin it and allow it to be merged
1653 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1657 ins.objectid = btrfs_ino(inode);
1658 ins.offset = file_pos;
1659 ins.type = BTRFS_EXTENT_DATA_KEY;
1660 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1662 leaf = path->nodes[0];
1663 fi = btrfs_item_ptr(leaf, path->slots[0],
1664 struct btrfs_file_extent_item);
1665 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1666 btrfs_set_file_extent_type(leaf, fi, extent_type);
1667 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1668 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1669 btrfs_set_file_extent_offset(leaf, fi, 0);
1670 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1671 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1672 btrfs_set_file_extent_compression(leaf, fi, compression);
1673 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1674 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1676 btrfs_unlock_up_safe(path, 1);
1677 btrfs_set_lock_blocking(leaf);
1679 btrfs_mark_buffer_dirty(leaf);
1681 inode_add_bytes(inode, num_bytes);
1683 ins.objectid = disk_bytenr;
1684 ins.offset = disk_num_bytes;
1685 ins.type = BTRFS_EXTENT_ITEM_KEY;
1686 ret = btrfs_alloc_reserved_file_extent(trans, root,
1687 root->root_key.objectid,
1688 btrfs_ino(inode), file_pos, &ins);
1690 btrfs_free_path(path);
1696 * helper function for btrfs_finish_ordered_io, this
1697 * just reads in some of the csum leaves to prime them into ram
1698 * before we start the transaction. It limits the amount of btree
1699 * reads required while inside the transaction.
1701 /* as ordered data IO finishes, this gets called so we can finish
1702 * an ordered extent if the range of bytes in the file it covers are
1705 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1707 struct btrfs_root *root = BTRFS_I(inode)->root;
1708 struct btrfs_trans_handle *trans = NULL;
1709 struct btrfs_ordered_extent *ordered_extent = NULL;
1710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1711 struct extent_state *cached_state = NULL;
1712 int compress_type = 0;
1716 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1720 BUG_ON(!ordered_extent);
1722 nolock = is_free_space_inode(root, inode);
1724 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1725 BUG_ON(!list_empty(&ordered_extent->list));
1726 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1729 trans = btrfs_join_transaction_nolock(root, 1);
1731 trans = btrfs_join_transaction(root, 1);
1732 BUG_ON(IS_ERR(trans));
1733 btrfs_set_trans_block_group(trans, inode);
1734 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1735 ret = btrfs_update_inode(trans, root, inode);
1741 lock_extent_bits(io_tree, ordered_extent->file_offset,
1742 ordered_extent->file_offset + ordered_extent->len - 1,
1743 0, &cached_state, GFP_NOFS);
1746 trans = btrfs_join_transaction_nolock(root, 1);
1748 trans = btrfs_join_transaction(root, 1);
1749 BUG_ON(IS_ERR(trans));
1750 btrfs_set_trans_block_group(trans, inode);
1751 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1753 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1754 compress_type = ordered_extent->compress_type;
1755 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1756 BUG_ON(compress_type);
1757 ret = btrfs_mark_extent_written(trans, inode,
1758 ordered_extent->file_offset,
1759 ordered_extent->file_offset +
1760 ordered_extent->len);
1763 BUG_ON(root == root->fs_info->tree_root);
1764 ret = insert_reserved_file_extent(trans, inode,
1765 ordered_extent->file_offset,
1766 ordered_extent->start,
1767 ordered_extent->disk_len,
1768 ordered_extent->len,
1769 ordered_extent->len,
1770 compress_type, 0, 0,
1771 BTRFS_FILE_EXTENT_REG);
1772 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1773 ordered_extent->file_offset,
1774 ordered_extent->len);
1777 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1778 ordered_extent->file_offset +
1779 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1781 add_pending_csums(trans, inode, ordered_extent->file_offset,
1782 &ordered_extent->list);
1784 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1786 ret = btrfs_update_inode(trans, root, inode);
1793 btrfs_end_transaction_nolock(trans, root);
1795 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1797 btrfs_end_transaction(trans, root);
1801 btrfs_put_ordered_extent(ordered_extent);
1802 /* once for the tree */
1803 btrfs_put_ordered_extent(ordered_extent);
1808 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1809 struct extent_state *state, int uptodate)
1811 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1813 ClearPagePrivate2(page);
1814 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1818 * When IO fails, either with EIO or csum verification fails, we
1819 * try other mirrors that might have a good copy of the data. This
1820 * io_failure_record is used to record state as we go through all the
1821 * mirrors. If another mirror has good data, the page is set up to date
1822 * and things continue. If a good mirror can't be found, the original
1823 * bio end_io callback is called to indicate things have failed.
1825 struct io_failure_record {
1830 unsigned long bio_flags;
1834 static int btrfs_io_failed_hook(struct bio *failed_bio,
1835 struct page *page, u64 start, u64 end,
1836 struct extent_state *state)
1838 struct io_failure_record *failrec = NULL;
1840 struct extent_map *em;
1841 struct inode *inode = page->mapping->host;
1842 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1843 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1850 ret = get_state_private(failure_tree, start, &private);
1852 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1855 failrec->start = start;
1856 failrec->len = end - start + 1;
1857 failrec->last_mirror = 0;
1858 failrec->bio_flags = 0;
1860 read_lock(&em_tree->lock);
1861 em = lookup_extent_mapping(em_tree, start, failrec->len);
1862 if (em->start > start || em->start + em->len < start) {
1863 free_extent_map(em);
1866 read_unlock(&em_tree->lock);
1868 if (!em || IS_ERR(em)) {
1872 logical = start - em->start;
1873 logical = em->block_start + logical;
1874 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1875 logical = em->block_start;
1876 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1877 extent_set_compress_type(&failrec->bio_flags,
1880 failrec->logical = logical;
1881 free_extent_map(em);
1882 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1883 EXTENT_DIRTY, GFP_NOFS);
1884 set_state_private(failure_tree, start,
1885 (u64)(unsigned long)failrec);
1887 failrec = (struct io_failure_record *)(unsigned long)private;
1889 num_copies = btrfs_num_copies(
1890 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1891 failrec->logical, failrec->len);
1892 failrec->last_mirror++;
1894 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1895 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1898 if (state && state->start != failrec->start)
1900 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1902 if (!state || failrec->last_mirror > num_copies) {
1903 set_state_private(failure_tree, failrec->start, 0);
1904 clear_extent_bits(failure_tree, failrec->start,
1905 failrec->start + failrec->len - 1,
1906 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1910 bio = bio_alloc(GFP_NOFS, 1);
1911 bio->bi_private = state;
1912 bio->bi_end_io = failed_bio->bi_end_io;
1913 bio->bi_sector = failrec->logical >> 9;
1914 bio->bi_bdev = failed_bio->bi_bdev;
1917 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1918 if (failed_bio->bi_rw & REQ_WRITE)
1923 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1924 failrec->last_mirror,
1925 failrec->bio_flags, 0);
1930 * each time an IO finishes, we do a fast check in the IO failure tree
1931 * to see if we need to process or clean up an io_failure_record
1933 static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1936 u64 private_failure;
1937 struct io_failure_record *failure;
1941 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1942 (u64)-1, 1, EXTENT_DIRTY, 0)) {
1943 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1944 start, &private_failure);
1946 failure = (struct io_failure_record *)(unsigned long)
1948 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1950 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1952 failure->start + failure->len - 1,
1953 EXTENT_DIRTY | EXTENT_LOCKED,
1962 * when reads are done, we need to check csums to verify the data is correct
1963 * if there's a match, we allow the bio to finish. If not, we go through
1964 * the io_failure_record routines to find good copies
1966 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1967 struct extent_state *state)
1969 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1970 struct inode *inode = page->mapping->host;
1971 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1973 u64 private = ~(u32)0;
1975 struct btrfs_root *root = BTRFS_I(inode)->root;
1978 if (PageChecked(page)) {
1979 ClearPageChecked(page);
1983 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1986 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1987 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1988 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1993 if (state && state->start == start) {
1994 private = state->private;
1997 ret = get_state_private(io_tree, start, &private);
1999 kaddr = kmap_atomic(page, KM_USER0);
2003 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2004 btrfs_csum_final(csum, (char *)&csum);
2005 if (csum != private)
2008 kunmap_atomic(kaddr, KM_USER0);
2010 /* if the io failure tree for this inode is non-empty,
2011 * check to see if we've recovered from a failed IO
2013 btrfs_clean_io_failures(inode, start);
2017 if (printk_ratelimit()) {
2018 printk(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2020 (unsigned long long)btrfs_ino(page->mapping->host),
2021 (unsigned long long)start, csum,
2022 (unsigned long long)private);
2024 memset(kaddr + offset, 1, end - start + 1);
2025 flush_dcache_page(page);
2026 kunmap_atomic(kaddr, KM_USER0);
2032 struct delayed_iput {
2033 struct list_head list;
2034 struct inode *inode;
2037 void btrfs_add_delayed_iput(struct inode *inode)
2039 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2040 struct delayed_iput *delayed;
2042 if (atomic_add_unless(&inode->i_count, -1, 1))
2045 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2046 delayed->inode = inode;
2048 spin_lock(&fs_info->delayed_iput_lock);
2049 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2050 spin_unlock(&fs_info->delayed_iput_lock);
2053 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2056 struct btrfs_fs_info *fs_info = root->fs_info;
2057 struct delayed_iput *delayed;
2060 spin_lock(&fs_info->delayed_iput_lock);
2061 empty = list_empty(&fs_info->delayed_iputs);
2062 spin_unlock(&fs_info->delayed_iput_lock);
2066 down_read(&root->fs_info->cleanup_work_sem);
2067 spin_lock(&fs_info->delayed_iput_lock);
2068 list_splice_init(&fs_info->delayed_iputs, &list);
2069 spin_unlock(&fs_info->delayed_iput_lock);
2071 while (!list_empty(&list)) {
2072 delayed = list_entry(list.next, struct delayed_iput, list);
2073 list_del(&delayed->list);
2074 iput(delayed->inode);
2077 up_read(&root->fs_info->cleanup_work_sem);
2081 * calculate extra metadata reservation when snapshotting a subvolume
2082 * contains orphan files.
2084 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2085 struct btrfs_pending_snapshot *pending,
2086 u64 *bytes_to_reserve)
2088 struct btrfs_root *root;
2089 struct btrfs_block_rsv *block_rsv;
2093 root = pending->root;
2094 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2097 block_rsv = root->orphan_block_rsv;
2099 /* orphan block reservation for the snapshot */
2100 num_bytes = block_rsv->size;
2103 * after the snapshot is created, COWing tree blocks may use more
2104 * space than it frees. So we should make sure there is enough
2107 index = trans->transid & 0x1;
2108 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2109 num_bytes += block_rsv->size -
2110 (block_rsv->reserved + block_rsv->freed[index]);
2113 *bytes_to_reserve += num_bytes;
2116 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2117 struct btrfs_pending_snapshot *pending)
2119 struct btrfs_root *root = pending->root;
2120 struct btrfs_root *snap = pending->snap;
2121 struct btrfs_block_rsv *block_rsv;
2126 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2129 /* refill source subvolume's orphan block reservation */
2130 block_rsv = root->orphan_block_rsv;
2131 index = trans->transid & 0x1;
2132 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2133 num_bytes = block_rsv->size -
2134 (block_rsv->reserved + block_rsv->freed[index]);
2135 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2136 root->orphan_block_rsv,
2141 /* setup orphan block reservation for the snapshot */
2142 block_rsv = btrfs_alloc_block_rsv(snap);
2145 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2146 snap->orphan_block_rsv = block_rsv;
2148 num_bytes = root->orphan_block_rsv->size;
2149 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2150 block_rsv, num_bytes);
2154 /* insert orphan item for the snapshot */
2155 WARN_ON(!root->orphan_item_inserted);
2156 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2157 snap->root_key.objectid);
2159 snap->orphan_item_inserted = 1;
2163 enum btrfs_orphan_cleanup_state {
2164 ORPHAN_CLEANUP_STARTED = 1,
2165 ORPHAN_CLEANUP_DONE = 2,
2169 * This is called in transaction commmit time. If there are no orphan
2170 * files in the subvolume, it removes orphan item and frees block_rsv
2173 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2174 struct btrfs_root *root)
2178 if (!list_empty(&root->orphan_list) ||
2179 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2182 if (root->orphan_item_inserted &&
2183 btrfs_root_refs(&root->root_item) > 0) {
2184 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2185 root->root_key.objectid);
2187 root->orphan_item_inserted = 0;
2190 if (root->orphan_block_rsv) {
2191 WARN_ON(root->orphan_block_rsv->size > 0);
2192 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2193 root->orphan_block_rsv = NULL;
2198 * This creates an orphan entry for the given inode in case something goes
2199 * wrong in the middle of an unlink/truncate.
2201 * NOTE: caller of this function should reserve 5 units of metadata for
2204 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2206 struct btrfs_root *root = BTRFS_I(inode)->root;
2207 struct btrfs_block_rsv *block_rsv = NULL;
2212 if (!root->orphan_block_rsv) {
2213 block_rsv = btrfs_alloc_block_rsv(root);
2217 spin_lock(&root->orphan_lock);
2218 if (!root->orphan_block_rsv) {
2219 root->orphan_block_rsv = block_rsv;
2220 } else if (block_rsv) {
2221 btrfs_free_block_rsv(root, block_rsv);
2225 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2226 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2229 * For proper ENOSPC handling, we should do orphan
2230 * cleanup when mounting. But this introduces backward
2231 * compatibility issue.
2233 if (!xchg(&root->orphan_item_inserted, 1))
2241 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2242 BTRFS_I(inode)->orphan_meta_reserved = 1;
2245 spin_unlock(&root->orphan_lock);
2248 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2250 /* grab metadata reservation from transaction handle */
2252 ret = btrfs_orphan_reserve_metadata(trans, inode);
2256 /* insert an orphan item to track this unlinked/truncated file */
2258 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2262 /* insert an orphan item to track subvolume contains orphan files */
2264 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2265 root->root_key.objectid);
2272 * We have done the truncate/delete so we can go ahead and remove the orphan
2273 * item for this particular inode.
2275 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2277 struct btrfs_root *root = BTRFS_I(inode)->root;
2278 int delete_item = 0;
2279 int release_rsv = 0;
2282 spin_lock(&root->orphan_lock);
2283 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2284 list_del_init(&BTRFS_I(inode)->i_orphan);
2288 if (BTRFS_I(inode)->orphan_meta_reserved) {
2289 BTRFS_I(inode)->orphan_meta_reserved = 0;
2292 spin_unlock(&root->orphan_lock);
2294 if (trans && delete_item) {
2295 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2300 btrfs_orphan_release_metadata(inode);
2306 * this cleans up any orphans that may be left on the list from the last use
2309 int btrfs_orphan_cleanup(struct btrfs_root *root)
2311 struct btrfs_path *path;
2312 struct extent_buffer *leaf;
2313 struct btrfs_key key, found_key;
2314 struct btrfs_trans_handle *trans;
2315 struct inode *inode;
2316 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2318 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2321 path = btrfs_alloc_path();
2328 key.objectid = BTRFS_ORPHAN_OBJECTID;
2329 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2330 key.offset = (u64)-1;
2333 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2338 * if ret == 0 means we found what we were searching for, which
2339 * is weird, but possible, so only screw with path if we didn't
2340 * find the key and see if we have stuff that matches
2344 if (path->slots[0] == 0)
2349 /* pull out the item */
2350 leaf = path->nodes[0];
2351 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2353 /* make sure the item matches what we want */
2354 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2356 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2359 /* release the path since we're done with it */
2360 btrfs_release_path(root, path);
2363 * this is where we are basically btrfs_lookup, without the
2364 * crossing root thing. we store the inode number in the
2365 * offset of the orphan item.
2367 found_key.objectid = found_key.offset;
2368 found_key.type = BTRFS_INODE_ITEM_KEY;
2369 found_key.offset = 0;
2370 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2371 if (IS_ERR(inode)) {
2372 ret = PTR_ERR(inode);
2377 * add this inode to the orphan list so btrfs_orphan_del does
2378 * the proper thing when we hit it
2380 spin_lock(&root->orphan_lock);
2381 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2382 spin_unlock(&root->orphan_lock);
2385 * if this is a bad inode, means we actually succeeded in
2386 * removing the inode, but not the orphan record, which means
2387 * we need to manually delete the orphan since iput will just
2388 * do a destroy_inode
2390 if (is_bad_inode(inode)) {
2391 trans = btrfs_start_transaction(root, 0);
2392 if (IS_ERR(trans)) {
2393 ret = PTR_ERR(trans);
2396 btrfs_orphan_del(trans, inode);
2397 btrfs_end_transaction(trans, root);
2402 /* if we have links, this was a truncate, lets do that */
2403 if (inode->i_nlink) {
2404 if (!S_ISREG(inode->i_mode)) {
2410 ret = btrfs_truncate(inode);
2415 /* this will do delete_inode and everything for us */
2420 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2422 if (root->orphan_block_rsv)
2423 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2426 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2427 trans = btrfs_join_transaction(root, 1);
2429 btrfs_end_transaction(trans, root);
2433 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2435 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2439 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2440 btrfs_free_path(path);
2445 * very simple check to peek ahead in the leaf looking for xattrs. If we
2446 * don't find any xattrs, we know there can't be any acls.
2448 * slot is the slot the inode is in, objectid is the objectid of the inode
2450 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2451 int slot, u64 objectid)
2453 u32 nritems = btrfs_header_nritems(leaf);
2454 struct btrfs_key found_key;
2458 while (slot < nritems) {
2459 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2461 /* we found a different objectid, there must not be acls */
2462 if (found_key.objectid != objectid)
2465 /* we found an xattr, assume we've got an acl */
2466 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2470 * we found a key greater than an xattr key, there can't
2471 * be any acls later on
2473 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2480 * it goes inode, inode backrefs, xattrs, extents,
2481 * so if there are a ton of hard links to an inode there can
2482 * be a lot of backrefs. Don't waste time searching too hard,
2483 * this is just an optimization
2488 /* we hit the end of the leaf before we found an xattr or
2489 * something larger than an xattr. We have to assume the inode
2496 * read an inode from the btree into the in-memory inode
2498 static void btrfs_read_locked_inode(struct inode *inode)
2500 struct btrfs_path *path;
2501 struct extent_buffer *leaf;
2502 struct btrfs_inode_item *inode_item;
2503 struct btrfs_timespec *tspec;
2504 struct btrfs_root *root = BTRFS_I(inode)->root;
2505 struct btrfs_key location;
2507 u64 alloc_group_block;
2511 path = btrfs_alloc_path();
2513 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2515 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2519 leaf = path->nodes[0];
2520 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2521 struct btrfs_inode_item);
2523 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2524 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2525 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2526 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2527 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2529 tspec = btrfs_inode_atime(inode_item);
2530 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2531 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2533 tspec = btrfs_inode_mtime(inode_item);
2534 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2535 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2537 tspec = btrfs_inode_ctime(inode_item);
2538 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2539 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2541 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2542 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2543 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2544 inode->i_generation = BTRFS_I(inode)->generation;
2546 rdev = btrfs_inode_rdev(leaf, inode_item);
2548 BTRFS_I(inode)->index_cnt = (u64)-1;
2549 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2551 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2554 * try to precache a NULL acl entry for files that don't have
2555 * any xattrs or acls
2557 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2560 cache_no_acl(inode);
2562 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2563 alloc_group_block, 0);
2564 btrfs_free_path(path);
2567 switch (inode->i_mode & S_IFMT) {
2569 inode->i_mapping->a_ops = &btrfs_aops;
2570 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2571 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2572 inode->i_fop = &btrfs_file_operations;
2573 inode->i_op = &btrfs_file_inode_operations;
2576 inode->i_fop = &btrfs_dir_file_operations;
2577 if (root == root->fs_info->tree_root)
2578 inode->i_op = &btrfs_dir_ro_inode_operations;
2580 inode->i_op = &btrfs_dir_inode_operations;
2583 inode->i_op = &btrfs_symlink_inode_operations;
2584 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2585 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2588 inode->i_op = &btrfs_special_inode_operations;
2589 init_special_inode(inode, inode->i_mode, rdev);
2593 btrfs_update_iflags(inode);
2597 btrfs_free_path(path);
2598 make_bad_inode(inode);
2602 * given a leaf and an inode, copy the inode fields into the leaf
2604 static void fill_inode_item(struct btrfs_trans_handle *trans,
2605 struct extent_buffer *leaf,
2606 struct btrfs_inode_item *item,
2607 struct inode *inode)
2609 if (!leaf->map_token)
2610 map_private_extent_buffer(leaf, (unsigned long)item,
2611 sizeof(struct btrfs_inode_item),
2612 &leaf->map_token, &leaf->kaddr,
2613 &leaf->map_start, &leaf->map_len,
2616 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2617 btrfs_set_inode_gid(leaf, item, inode->i_gid);
2618 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2619 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2620 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2622 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2623 inode->i_atime.tv_sec);
2624 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2625 inode->i_atime.tv_nsec);
2627 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2628 inode->i_mtime.tv_sec);
2629 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2630 inode->i_mtime.tv_nsec);
2632 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2633 inode->i_ctime.tv_sec);
2634 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2635 inode->i_ctime.tv_nsec);
2637 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2638 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2639 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2640 btrfs_set_inode_transid(leaf, item, trans->transid);
2641 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2642 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2643 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
2645 if (leaf->map_token) {
2646 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2647 leaf->map_token = NULL;
2652 * copy everything in the in-memory inode into the btree.
2654 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2655 struct btrfs_root *root, struct inode *inode)
2657 struct btrfs_inode_item *inode_item;
2658 struct btrfs_path *path;
2659 struct extent_buffer *leaf;
2663 * If root is tree root, it means this inode is used to
2664 * store free space information. And these inodes are updated
2665 * when committing the transaction, so they needn't delaye to
2666 * be updated, or deadlock will occured.
2668 if (!is_free_space_inode(root, inode)) {
2669 ret = btrfs_delayed_update_inode(trans, root, inode);
2671 btrfs_set_inode_last_trans(trans, inode);
2675 path = btrfs_alloc_path();
2679 path->leave_spinning = 1;
2680 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2688 btrfs_unlock_up_safe(path, 1);
2689 leaf = path->nodes[0];
2690 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2691 struct btrfs_inode_item);
2693 fill_inode_item(trans, leaf, inode_item, inode);
2694 btrfs_mark_buffer_dirty(leaf);
2695 btrfs_set_inode_last_trans(trans, inode);
2698 btrfs_free_path(path);
2703 * unlink helper that gets used here in inode.c and in the tree logging
2704 * recovery code. It remove a link in a directory with a given name, and
2705 * also drops the back refs in the inode to the directory
2707 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2708 struct btrfs_root *root,
2709 struct inode *dir, struct inode *inode,
2710 const char *name, int name_len)
2712 struct btrfs_path *path;
2714 struct extent_buffer *leaf;
2715 struct btrfs_dir_item *di;
2716 struct btrfs_key key;
2718 u64 ino = btrfs_ino(inode);
2719 u64 dir_ino = btrfs_ino(dir);
2721 path = btrfs_alloc_path();
2727 path->leave_spinning = 1;
2728 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2729 name, name_len, -1);
2738 leaf = path->nodes[0];
2739 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2740 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2743 btrfs_release_path(root, path);
2745 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2748 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2749 "inode %llu parent %llu\n", name_len, name,
2750 (unsigned long long)ino, (unsigned long long)dir_ino);
2754 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2758 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2760 BUG_ON(ret != 0 && ret != -ENOENT);
2762 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2767 btrfs_free_path(path);
2771 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2772 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2773 btrfs_update_inode(trans, root, dir);
2778 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root,
2780 struct inode *dir, struct inode *inode,
2781 const char *name, int name_len)
2784 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2786 btrfs_drop_nlink(inode);
2787 ret = btrfs_update_inode(trans, root, inode);
2793 /* helper to check if there is any shared block in the path */
2794 static int check_path_shared(struct btrfs_root *root,
2795 struct btrfs_path *path)
2797 struct extent_buffer *eb;
2801 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2804 if (!path->nodes[level])
2806 eb = path->nodes[level];
2807 if (!btrfs_block_can_be_shared(root, eb))
2809 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2818 * helper to start transaction for unlink and rmdir.
2820 * unlink and rmdir are special in btrfs, they do not always free space.
2821 * so in enospc case, we should make sure they will free space before
2822 * allowing them to use the global metadata reservation.
2824 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2825 struct dentry *dentry)
2827 struct btrfs_trans_handle *trans;
2828 struct btrfs_root *root = BTRFS_I(dir)->root;
2829 struct btrfs_path *path;
2830 struct btrfs_inode_ref *ref;
2831 struct btrfs_dir_item *di;
2832 struct inode *inode = dentry->d_inode;
2837 u64 ino = btrfs_ino(inode);
2838 u64 dir_ino = btrfs_ino(dir);
2840 trans = btrfs_start_transaction(root, 10);
2841 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2844 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2845 return ERR_PTR(-ENOSPC);
2847 /* check if there is someone else holds reference */
2848 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2849 return ERR_PTR(-ENOSPC);
2851 if (atomic_read(&inode->i_count) > 2)
2852 return ERR_PTR(-ENOSPC);
2854 if (xchg(&root->fs_info->enospc_unlink, 1))
2855 return ERR_PTR(-ENOSPC);
2857 path = btrfs_alloc_path();
2859 root->fs_info->enospc_unlink = 0;
2860 return ERR_PTR(-ENOMEM);
2863 trans = btrfs_start_transaction(root, 0);
2864 if (IS_ERR(trans)) {
2865 btrfs_free_path(path);
2866 root->fs_info->enospc_unlink = 0;
2870 path->skip_locking = 1;
2871 path->search_commit_root = 1;
2873 ret = btrfs_lookup_inode(trans, root, path,
2874 &BTRFS_I(dir)->location, 0);
2880 if (check_path_shared(root, path))
2885 btrfs_release_path(root, path);
2887 ret = btrfs_lookup_inode(trans, root, path,
2888 &BTRFS_I(inode)->location, 0);
2894 if (check_path_shared(root, path))
2899 btrfs_release_path(root, path);
2901 if (ret == 0 && S_ISREG(inode->i_mode)) {
2902 ret = btrfs_lookup_file_extent(trans, root, path,
2909 if (check_path_shared(root, path))
2911 btrfs_release_path(root, path);
2919 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2920 dentry->d_name.name, dentry->d_name.len, 0);
2926 if (check_path_shared(root, path))
2932 btrfs_release_path(root, path);
2934 ref = btrfs_lookup_inode_ref(trans, root, path,
2935 dentry->d_name.name, dentry->d_name.len,
2942 if (check_path_shared(root, path))
2944 index = btrfs_inode_ref_index(path->nodes[0], ref);
2945 btrfs_release_path(root, path);
2948 * This is a commit root search, if we can lookup inode item and other
2949 * relative items in the commit root, it means the transaction of
2950 * dir/file creation has been committed, and the dir index item that we
2951 * delay to insert has also been inserted into the commit root. So
2952 * we needn't worry about the delayed insertion of the dir index item
2955 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2956 dentry->d_name.name, dentry->d_name.len, 0);
2961 BUG_ON(ret == -ENOENT);
2962 if (check_path_shared(root, path))
2967 btrfs_free_path(path);
2969 btrfs_end_transaction(trans, root);
2970 root->fs_info->enospc_unlink = 0;
2971 return ERR_PTR(err);
2974 trans->block_rsv = &root->fs_info->global_block_rsv;
2978 static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2979 struct btrfs_root *root)
2981 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2982 BUG_ON(!root->fs_info->enospc_unlink);
2983 root->fs_info->enospc_unlink = 0;
2985 btrfs_end_transaction_throttle(trans, root);
2988 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2990 struct btrfs_root *root = BTRFS_I(dir)->root;
2991 struct btrfs_trans_handle *trans;
2992 struct inode *inode = dentry->d_inode;
2994 unsigned long nr = 0;
2996 trans = __unlink_start_trans(dir, dentry);
2998 return PTR_ERR(trans);
3000 btrfs_set_trans_block_group(trans, dir);
3002 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3004 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3005 dentry->d_name.name, dentry->d_name.len);
3008 if (inode->i_nlink == 0) {
3009 ret = btrfs_orphan_add(trans, inode);
3013 nr = trans->blocks_used;
3014 __unlink_end_trans(trans, root);
3015 btrfs_btree_balance_dirty(root, nr);
3019 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3020 struct btrfs_root *root,
3021 struct inode *dir, u64 objectid,
3022 const char *name, int name_len)
3024 struct btrfs_path *path;
3025 struct extent_buffer *leaf;
3026 struct btrfs_dir_item *di;
3027 struct btrfs_key key;
3030 u64 dir_ino = btrfs_ino(dir);
3032 path = btrfs_alloc_path();
3036 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3037 name, name_len, -1);
3038 BUG_ON(!di || IS_ERR(di));
3040 leaf = path->nodes[0];
3041 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3042 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3043 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3045 btrfs_release_path(root, path);
3047 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3048 objectid, root->root_key.objectid,
3049 dir_ino, &index, name, name_len);
3051 BUG_ON(ret != -ENOENT);
3052 di = btrfs_search_dir_index_item(root, path, dir_ino,
3054 BUG_ON(!di || IS_ERR(di));
3056 leaf = path->nodes[0];
3057 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3058 btrfs_release_path(root, path);
3061 btrfs_release_path(root, path);
3063 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3066 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3067 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3068 ret = btrfs_update_inode(trans, root, dir);
3074 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3076 struct inode *inode = dentry->d_inode;
3078 struct btrfs_root *root = BTRFS_I(dir)->root;
3079 struct btrfs_trans_handle *trans;
3080 unsigned long nr = 0;
3082 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3083 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3086 trans = __unlink_start_trans(dir, dentry);
3088 return PTR_ERR(trans);
3090 btrfs_set_trans_block_group(trans, dir);
3092 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3093 err = btrfs_unlink_subvol(trans, root, dir,
3094 BTRFS_I(inode)->location.objectid,
3095 dentry->d_name.name,
3096 dentry->d_name.len);
3100 err = btrfs_orphan_add(trans, inode);
3104 /* now the directory is empty */
3105 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3106 dentry->d_name.name, dentry->d_name.len);
3108 btrfs_i_size_write(inode, 0);
3110 nr = trans->blocks_used;
3111 __unlink_end_trans(trans, root);
3112 btrfs_btree_balance_dirty(root, nr);
3119 * when truncating bytes in a file, it is possible to avoid reading
3120 * the leaves that contain only checksum items. This can be the
3121 * majority of the IO required to delete a large file, but it must
3122 * be done carefully.
3124 * The keys in the level just above the leaves are checked to make sure
3125 * the lowest key in a given leaf is a csum key, and starts at an offset
3126 * after the new size.
3128 * Then the key for the next leaf is checked to make sure it also has
3129 * a checksum item for the same file. If it does, we know our target leaf
3130 * contains only checksum items, and it can be safely freed without reading
3133 * This is just an optimization targeted at large files. It may do
3134 * nothing. It will return 0 unless things went badly.
3136 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3137 struct btrfs_root *root,
3138 struct btrfs_path *path,
3139 struct inode *inode, u64 new_size)
3141 struct btrfs_key key;
3144 struct btrfs_key found_key;
3145 struct btrfs_key other_key;
3146 struct btrfs_leaf_ref *ref;
3150 path->lowest_level = 1;
3151 key.objectid = inode->i_ino;
3152 key.type = BTRFS_CSUM_ITEM_KEY;
3153 key.offset = new_size;
3155 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3159 if (path->nodes[1] == NULL) {
3164 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3165 nritems = btrfs_header_nritems(path->nodes[1]);
3170 if (path->slots[1] >= nritems)
3173 /* did we find a key greater than anything we want to delete? */
3174 if (found_key.objectid > inode->i_ino ||
3175 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3178 /* we check the next key in the node to make sure the leave contains
3179 * only checksum items. This comparison doesn't work if our
3180 * leaf is the last one in the node
3182 if (path->slots[1] + 1 >= nritems) {
3184 /* search forward from the last key in the node, this
3185 * will bring us into the next node in the tree
3187 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3189 /* unlikely, but we inc below, so check to be safe */
3190 if (found_key.offset == (u64)-1)
3193 /* search_forward needs a path with locks held, do the
3194 * search again for the original key. It is possible
3195 * this will race with a balance and return a path that
3196 * we could modify, but this drop is just an optimization
3197 * and is allowed to miss some leaves.
3199 btrfs_release_path(root, path);
3202 /* setup a max key for search_forward */
3203 other_key.offset = (u64)-1;
3204 other_key.type = key.type;
3205 other_key.objectid = key.objectid;
3207 path->keep_locks = 1;
3208 ret = btrfs_search_forward(root, &found_key, &other_key,
3210 path->keep_locks = 0;
3211 if (ret || found_key.objectid != key.objectid ||
3212 found_key.type != key.type) {
3217 key.offset = found_key.offset;
3218 btrfs_release_path(root, path);
3223 /* we know there's one more slot after us in the tree,
3224 * read that key so we can verify it is also a checksum item
3226 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3228 if (found_key.objectid < inode->i_ino)
3231 if (found_key.type != key.type || found_key.offset < new_size)
3235 * if the key for the next leaf isn't a csum key from this objectid,
3236 * we can't be sure there aren't good items inside this leaf.
3239 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3242 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3243 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
3245 * it is safe to delete this leaf, it contains only
3246 * csum items from this inode at an offset >= new_size
3248 ret = btrfs_del_leaf(trans, root, path, leaf_start);
3251 if (root->ref_cows && leaf_gen < trans->transid) {
3252 ref = btrfs_alloc_leaf_ref(root, 0);
3254 ref->root_gen = root->root_key.offset;
3255 ref->bytenr = leaf_start;
3257 ref->generation = leaf_gen;
3260 btrfs_sort_leaf_ref(ref);
3262 ret = btrfs_add_leaf_ref(root, ref, 0);
3264 btrfs_free_leaf_ref(root, ref);
3270 btrfs_release_path(root, path);
3272 if (other_key.objectid == inode->i_ino &&
3273 other_key.type == key.type && other_key.offset > key.offset) {
3274 key.offset = other_key.offset;
3280 /* fixup any changes we've made to the path */
3281 path->lowest_level = 0;
3282 path->keep_locks = 0;
3283 btrfs_release_path(root, path);
3290 * this can truncate away extent items, csum items and directory items.
3291 * It starts at a high offset and removes keys until it can't find
3292 * any higher than new_size
3294 * csum items that cross the new i_size are truncated to the new size
3297 * min_type is the minimum key type to truncate down to. If set to 0, this
3298 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3300 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3301 struct btrfs_root *root,
3302 struct inode *inode,
3303 u64 new_size, u32 min_type)
3305 struct btrfs_path *path;
3306 struct extent_buffer *leaf;
3307 struct btrfs_file_extent_item *fi;
3308 struct btrfs_key key;
3309 struct btrfs_key found_key;
3310 u64 extent_start = 0;
3311 u64 extent_num_bytes = 0;
3312 u64 extent_offset = 0;
3314 u64 mask = root->sectorsize - 1;
3315 u32 found_type = (u8)-1;
3318 int pending_del_nr = 0;
3319 int pending_del_slot = 0;
3320 int extent_type = -1;
3324 u64 ino = btrfs_ino(inode);
3326 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3328 if (root->ref_cows || root == root->fs_info->tree_root)
3329 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3332 * This function is also used to drop the items in the log tree before
3333 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3334 * it is used to drop the loged items. So we shouldn't kill the delayed
3337 if (min_type == 0 && root == BTRFS_I(inode)->root)
3338 btrfs_kill_delayed_inode_items(inode);
3340 path = btrfs_alloc_path();
3345 key.offset = (u64)-1;
3349 path->leave_spinning = 1;
3350 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3357 /* there are no items in the tree for us to truncate, we're
3360 if (path->slots[0] == 0)
3367 leaf = path->nodes[0];
3368 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3369 found_type = btrfs_key_type(&found_key);
3372 if (found_key.objectid != ino)
3375 if (found_type < min_type)
3378 item_end = found_key.offset;
3379 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3380 fi = btrfs_item_ptr(leaf, path->slots[0],
3381 struct btrfs_file_extent_item);
3382 extent_type = btrfs_file_extent_type(leaf, fi);
3383 encoding = btrfs_file_extent_compression(leaf, fi);
3384 encoding |= btrfs_file_extent_encryption(leaf, fi);
3385 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3387 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3389 btrfs_file_extent_num_bytes(leaf, fi);
3390 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3391 item_end += btrfs_file_extent_inline_len(leaf,
3396 if (found_type > min_type) {
3399 if (item_end < new_size)
3401 if (found_key.offset >= new_size)
3407 /* FIXME, shrink the extent if the ref count is only 1 */
3408 if (found_type != BTRFS_EXTENT_DATA_KEY)
3411 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3413 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
3414 if (!del_item && !encoding) {
3415 u64 orig_num_bytes =
3416 btrfs_file_extent_num_bytes(leaf, fi);
3417 extent_num_bytes = new_size -
3418 found_key.offset + root->sectorsize - 1;
3419 extent_num_bytes = extent_num_bytes &
3420 ~((u64)root->sectorsize - 1);
3421 btrfs_set_file_extent_num_bytes(leaf, fi,
3423 num_dec = (orig_num_bytes -
3425 if (root->ref_cows && extent_start != 0)
3426 inode_sub_bytes(inode, num_dec);
3427 btrfs_mark_buffer_dirty(leaf);
3430 btrfs_file_extent_disk_num_bytes(leaf,
3432 extent_offset = found_key.offset -
3433 btrfs_file_extent_offset(leaf, fi);
3435 /* FIXME blocksize != 4096 */
3436 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
3437 if (extent_start != 0) {
3440 inode_sub_bytes(inode, num_dec);
3443 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3445 * we can't truncate inline items that have had
3449 btrfs_file_extent_compression(leaf, fi) == 0 &&
3450 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3451 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3452 u32 size = new_size - found_key.offset;
3454 if (root->ref_cows) {
3455 inode_sub_bytes(inode, item_end + 1 -
3459 btrfs_file_extent_calc_inline_size(size);
3460 ret = btrfs_truncate_item(trans, root, path,
3463 } else if (root->ref_cows) {
3464 inode_sub_bytes(inode, item_end + 1 -
3470 if (!pending_del_nr) {
3471 /* no pending yet, add ourselves */
3472 pending_del_slot = path->slots[0];
3474 } else if (pending_del_nr &&
3475 path->slots[0] + 1 == pending_del_slot) {
3476 /* hop on the pending chunk */
3478 pending_del_slot = path->slots[0];
3485 if (found_extent && (root->ref_cows ||
3486 root == root->fs_info->tree_root)) {
3487 btrfs_set_path_blocking(path);
3488 ret = btrfs_free_extent(trans, root, extent_start,
3489 extent_num_bytes, 0,
3490 btrfs_header_owner(leaf),
3491 ino, extent_offset);
3495 if (found_type == BTRFS_INODE_ITEM_KEY)
3498 if (path->slots[0] == 0 ||
3499 path->slots[0] != pending_del_slot) {
3500 if (root->ref_cows &&
3501 BTRFS_I(inode)->location.objectid !=
3502 BTRFS_FREE_INO_OBJECTID) {
3506 if (pending_del_nr) {
3507 ret = btrfs_del_items(trans, root, path,
3513 btrfs_release_path(root, path);
3520 if (pending_del_nr) {
3521 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3525 btrfs_free_path(path);
3530 * taken from block_truncate_page, but does cow as it zeros out
3531 * any bytes left in the last page in the file.
3533 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3535 struct inode *inode = mapping->host;
3536 struct btrfs_root *root = BTRFS_I(inode)->root;
3537 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3538 struct btrfs_ordered_extent *ordered;
3539 struct extent_state *cached_state = NULL;
3541 u32 blocksize = root->sectorsize;
3542 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3543 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3549 if ((offset & (blocksize - 1)) == 0)
3551 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
3557 page = grab_cache_page(mapping, index);
3559 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3563 page_start = page_offset(page);
3564 page_end = page_start + PAGE_CACHE_SIZE - 1;
3566 if (!PageUptodate(page)) {
3567 ret = btrfs_readpage(NULL, page);
3569 if (page->mapping != mapping) {
3571 page_cache_release(page);
3574 if (!PageUptodate(page)) {
3579 wait_on_page_writeback(page);
3581 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3583 set_page_extent_mapped(page);
3585 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3587 unlock_extent_cached(io_tree, page_start, page_end,
3588 &cached_state, GFP_NOFS);
3590 page_cache_release(page);
3591 btrfs_start_ordered_extent(inode, ordered, 1);
3592 btrfs_put_ordered_extent(ordered);
3596 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
3597 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3598 0, 0, &cached_state, GFP_NOFS);
3600 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3603 unlock_extent_cached(io_tree, page_start, page_end,
3604 &cached_state, GFP_NOFS);
3609 if (offset != PAGE_CACHE_SIZE) {
3611 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3612 flush_dcache_page(page);
3615 ClearPageChecked(page);
3616 set_page_dirty(page);
3617 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3622 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3624 page_cache_release(page);
3630 * This function puts in dummy file extents for the area we're creating a hole
3631 * for. So if we are truncating this file to a larger size we need to insert
3632 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3633 * the range between oldsize and size
3635 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3637 struct btrfs_trans_handle *trans;
3638 struct btrfs_root *root = BTRFS_I(inode)->root;
3639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3640 struct extent_map *em = NULL;
3641 struct extent_state *cached_state = NULL;
3642 u64 mask = root->sectorsize - 1;
3643 u64 hole_start = (oldsize + mask) & ~mask;
3644 u64 block_end = (size + mask) & ~mask;
3650 if (size <= hole_start)
3654 struct btrfs_ordered_extent *ordered;
3655 btrfs_wait_ordered_range(inode, hole_start,
3656 block_end - hole_start);
3657 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3658 &cached_state, GFP_NOFS);
3659 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3662 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3663 &cached_state, GFP_NOFS);
3664 btrfs_put_ordered_extent(ordered);
3667 cur_offset = hole_start;
3669 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3670 block_end - cur_offset, 0);
3671 BUG_ON(IS_ERR(em) || !em);
3672 last_byte = min(extent_map_end(em), block_end);
3673 last_byte = (last_byte + mask) & ~mask;
3674 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3676 hole_size = last_byte - cur_offset;
3678 trans = btrfs_start_transaction(root, 2);
3679 if (IS_ERR(trans)) {
3680 err = PTR_ERR(trans);
3683 btrfs_set_trans_block_group(trans, inode);
3685 err = btrfs_drop_extents(trans, inode, cur_offset,
3686 cur_offset + hole_size,
3691 err = btrfs_insert_file_extent(trans, root,
3692 btrfs_ino(inode), cur_offset, 0,
3693 0, hole_size, 0, hole_size,
3698 btrfs_drop_extent_cache(inode, hole_start,
3701 btrfs_end_transaction(trans, root);
3703 free_extent_map(em);
3705 cur_offset = last_byte;
3706 if (cur_offset >= block_end)
3710 free_extent_map(em);
3711 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3716 static int btrfs_setsize(struct inode *inode, loff_t newsize)
3718 loff_t oldsize = i_size_read(inode);
3721 if (newsize == oldsize)
3724 if (newsize > oldsize) {
3725 i_size_write(inode, newsize);
3726 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3727 truncate_pagecache(inode, oldsize, newsize);
3728 ret = btrfs_cont_expand(inode, oldsize, newsize);
3730 btrfs_setsize(inode, oldsize);
3734 mark_inode_dirty(inode);
3738 * We're truncating a file that used to have good data down to
3739 * zero. Make sure it gets into the ordered flush list so that
3740 * any new writes get down to disk quickly.
3743 BTRFS_I(inode)->ordered_data_close = 1;
3745 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3746 truncate_setsize(inode, newsize);
3747 ret = btrfs_truncate(inode);
3753 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3755 struct inode *inode = dentry->d_inode;
3756 struct btrfs_root *root = BTRFS_I(inode)->root;
3759 if (btrfs_root_readonly(root))
3762 err = inode_change_ok(inode, attr);
3766 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3767 err = btrfs_setsize(inode, attr->ia_size);
3772 if (attr->ia_valid) {
3773 setattr_copy(inode, attr);
3774 mark_inode_dirty(inode);
3776 if (attr->ia_valid & ATTR_MODE)
3777 err = btrfs_acl_chmod(inode);
3783 void btrfs_evict_inode(struct inode *inode)
3785 struct btrfs_trans_handle *trans;
3786 struct btrfs_root *root = BTRFS_I(inode)->root;
3790 trace_btrfs_inode_evict(inode);
3792 truncate_inode_pages(&inode->i_data, 0);
3793 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3794 is_free_space_inode(root, inode)))
3797 if (is_bad_inode(inode)) {
3798 btrfs_orphan_del(NULL, inode);
3801 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3802 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3804 if (root->fs_info->log_root_recovering) {
3805 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3809 if (inode->i_nlink > 0) {
3810 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3814 btrfs_i_size_write(inode, 0);
3817 trans = btrfs_start_transaction(root, 0);
3818 BUG_ON(IS_ERR(trans));
3819 btrfs_set_trans_block_group(trans, inode);
3820 trans->block_rsv = root->orphan_block_rsv;
3822 ret = btrfs_block_rsv_check(trans, root,
3823 root->orphan_block_rsv, 0, 5);
3825 BUG_ON(ret != -EAGAIN);
3826 ret = btrfs_commit_transaction(trans, root);
3831 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3835 nr = trans->blocks_used;
3836 btrfs_end_transaction(trans, root);
3838 btrfs_btree_balance_dirty(root, nr);
3843 ret = btrfs_orphan_del(trans, inode);
3847 if (!(root == root->fs_info->tree_root ||
3848 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3849 btrfs_return_ino(root, btrfs_ino(inode));
3851 nr = trans->blocks_used;
3852 btrfs_end_transaction(trans, root);
3853 btrfs_btree_balance_dirty(root, nr);
3855 end_writeback(inode);
3860 * this returns the key found in the dir entry in the location pointer.
3861 * If no dir entries were found, location->objectid is 0.
3863 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3864 struct btrfs_key *location)
3866 const char *name = dentry->d_name.name;
3867 int namelen = dentry->d_name.len;
3868 struct btrfs_dir_item *di;
3869 struct btrfs_path *path;
3870 struct btrfs_root *root = BTRFS_I(dir)->root;
3873 path = btrfs_alloc_path();
3876 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3881 if (!di || IS_ERR(di))
3884 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3886 btrfs_free_path(path);
3889 location->objectid = 0;
3894 * when we hit a tree root in a directory, the btrfs part of the inode
3895 * needs to be changed to reflect the root directory of the tree root. This
3896 * is kind of like crossing a mount point.
3898 static int fixup_tree_root_location(struct btrfs_root *root,
3900 struct dentry *dentry,
3901 struct btrfs_key *location,
3902 struct btrfs_root **sub_root)
3904 struct btrfs_path *path;
3905 struct btrfs_root *new_root;
3906 struct btrfs_root_ref *ref;
3907 struct extent_buffer *leaf;
3911 path = btrfs_alloc_path();
3918 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3919 BTRFS_I(dir)->root->root_key.objectid,
3920 location->objectid);
3927 leaf = path->nodes[0];
3928 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3929 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3930 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3933 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3934 (unsigned long)(ref + 1),
3935 dentry->d_name.len);
3939 btrfs_release_path(root->fs_info->tree_root, path);
3941 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3942 if (IS_ERR(new_root)) {
3943 err = PTR_ERR(new_root);
3947 if (btrfs_root_refs(&new_root->root_item) == 0) {
3952 *sub_root = new_root;
3953 location->objectid = btrfs_root_dirid(&new_root->root_item);
3954 location->type = BTRFS_INODE_ITEM_KEY;
3955 location->offset = 0;
3958 btrfs_free_path(path);
3962 static void inode_tree_add(struct inode *inode)
3964 struct btrfs_root *root = BTRFS_I(inode)->root;
3965 struct btrfs_inode *entry;
3967 struct rb_node *parent;
3968 u64 ino = btrfs_ino(inode);
3970 p = &root->inode_tree.rb_node;
3973 if (inode_unhashed(inode))
3976 spin_lock(&root->inode_lock);
3979 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3981 if (ino < btrfs_ino(&entry->vfs_inode))
3982 p = &parent->rb_left;
3983 else if (ino > btrfs_ino(&entry->vfs_inode))
3984 p = &parent->rb_right;
3986 WARN_ON(!(entry->vfs_inode.i_state &
3987 (I_WILL_FREE | I_FREEING)));
3988 rb_erase(parent, &root->inode_tree);
3989 RB_CLEAR_NODE(parent);
3990 spin_unlock(&root->inode_lock);
3994 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3995 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3996 spin_unlock(&root->inode_lock);
3999 static void inode_tree_del(struct inode *inode)
4001 struct btrfs_root *root = BTRFS_I(inode)->root;
4004 spin_lock(&root->inode_lock);
4005 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
4006 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4007 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
4008 empty = RB_EMPTY_ROOT(&root->inode_tree);
4010 spin_unlock(&root->inode_lock);
4013 * Free space cache has inodes in the tree root, but the tree root has a
4014 * root_refs of 0, so this could end up dropping the tree root as a
4015 * snapshot, so we need the extra !root->fs_info->tree_root check to
4016 * make sure we don't drop it.
4018 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4019 root != root->fs_info->tree_root) {
4020 synchronize_srcu(&root->fs_info->subvol_srcu);
4021 spin_lock(&root->inode_lock);
4022 empty = RB_EMPTY_ROOT(&root->inode_tree);
4023 spin_unlock(&root->inode_lock);
4025 btrfs_add_dead_root(root);
4029 int btrfs_invalidate_inodes(struct btrfs_root *root)
4031 struct rb_node *node;
4032 struct rb_node *prev;
4033 struct btrfs_inode *entry;
4034 struct inode *inode;
4037 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4039 spin_lock(&root->inode_lock);
4041 node = root->inode_tree.rb_node;
4045 entry = rb_entry(node, struct btrfs_inode, rb_node);
4047 if (objectid < btrfs_ino(&entry->vfs_inode))
4048 node = node->rb_left;
4049 else if (objectid > btrfs_ino(&entry->vfs_inode))
4050 node = node->rb_right;
4056 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4057 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4061 prev = rb_next(prev);
4065 entry = rb_entry(node, struct btrfs_inode, rb_node);
4066 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4067 inode = igrab(&entry->vfs_inode);
4069 spin_unlock(&root->inode_lock);
4070 if (atomic_read(&inode->i_count) > 1)
4071 d_prune_aliases(inode);
4073 * btrfs_drop_inode will have it removed from
4074 * the inode cache when its usage count
4079 spin_lock(&root->inode_lock);
4083 if (cond_resched_lock(&root->inode_lock))
4086 node = rb_next(node);
4088 spin_unlock(&root->inode_lock);
4092 static int btrfs_init_locked_inode(struct inode *inode, void *p)
4094 struct btrfs_iget_args *args = p;
4095 inode->i_ino = args->ino;
4096 BTRFS_I(inode)->root = args->root;
4097 btrfs_set_inode_space_info(args->root, inode);
4101 static int btrfs_find_actor(struct inode *inode, void *opaque)
4103 struct btrfs_iget_args *args = opaque;
4104 return args->ino == btrfs_ino(inode) &&
4105 args->root == BTRFS_I(inode)->root;
4108 static struct inode *btrfs_iget_locked(struct super_block *s,
4110 struct btrfs_root *root)
4112 struct inode *inode;
4113 struct btrfs_iget_args args;
4114 args.ino = objectid;
4117 inode = iget5_locked(s, objectid, btrfs_find_actor,
4118 btrfs_init_locked_inode,
4123 /* Get an inode object given its location and corresponding root.
4124 * Returns in *is_new if the inode was read from disk
4126 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4127 struct btrfs_root *root, int *new)
4129 struct inode *inode;
4131 inode = btrfs_iget_locked(s, location->objectid, root);
4133 return ERR_PTR(-ENOMEM);
4135 if (inode->i_state & I_NEW) {
4136 BTRFS_I(inode)->root = root;
4137 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4138 btrfs_read_locked_inode(inode);
4139 inode_tree_add(inode);
4140 unlock_new_inode(inode);
4148 static struct inode *new_simple_dir(struct super_block *s,
4149 struct btrfs_key *key,
4150 struct btrfs_root *root)
4152 struct inode *inode = new_inode(s);
4155 return ERR_PTR(-ENOMEM);
4157 BTRFS_I(inode)->root = root;
4158 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4159 BTRFS_I(inode)->dummy_inode = 1;
4161 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4162 inode->i_op = &simple_dir_inode_operations;
4163 inode->i_fop = &simple_dir_operations;
4164 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4165 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4170 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4172 struct inode *inode;
4173 struct btrfs_root *root = BTRFS_I(dir)->root;
4174 struct btrfs_root *sub_root = root;
4175 struct btrfs_key location;
4179 if (dentry->d_name.len > BTRFS_NAME_LEN)
4180 return ERR_PTR(-ENAMETOOLONG);
4182 ret = btrfs_inode_by_name(dir, dentry, &location);
4185 return ERR_PTR(ret);
4187 if (location.objectid == 0)
4190 if (location.type == BTRFS_INODE_ITEM_KEY) {
4191 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4195 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4197 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4198 ret = fixup_tree_root_location(root, dir, dentry,
4199 &location, &sub_root);
4202 inode = ERR_PTR(ret);
4204 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4206 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
4208 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4210 if (!IS_ERR(inode) && root != sub_root) {
4211 down_read(&root->fs_info->cleanup_work_sem);
4212 if (!(inode->i_sb->s_flags & MS_RDONLY))
4213 ret = btrfs_orphan_cleanup(sub_root);
4214 up_read(&root->fs_info->cleanup_work_sem);
4216 inode = ERR_PTR(ret);
4222 static int btrfs_dentry_delete(const struct dentry *dentry)
4224 struct btrfs_root *root;
4226 if (!dentry->d_inode && !IS_ROOT(dentry))
4227 dentry = dentry->d_parent;
4229 if (dentry->d_inode) {
4230 root = BTRFS_I(dentry->d_inode)->root;
4231 if (btrfs_root_refs(&root->root_item) == 0)
4237 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4238 struct nameidata *nd)
4240 struct inode *inode;
4242 inode = btrfs_lookup_dentry(dir, dentry);
4244 return ERR_CAST(inode);
4246 return d_splice_alias(inode, dentry);
4249 unsigned char btrfs_filetype_table[] = {
4250 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4253 static int btrfs_real_readdir(struct file *filp, void *dirent,
4256 struct inode *inode = filp->f_dentry->d_inode;
4257 struct btrfs_root *root = BTRFS_I(inode)->root;
4258 struct btrfs_item *item;
4259 struct btrfs_dir_item *di;
4260 struct btrfs_key key;
4261 struct btrfs_key found_key;
4262 struct btrfs_path *path;
4263 struct list_head ins_list;
4264 struct list_head del_list;
4266 struct extent_buffer *leaf;
4268 unsigned char d_type;
4273 int key_type = BTRFS_DIR_INDEX_KEY;
4277 int is_curr = 0; /* filp->f_pos points to the current index? */
4279 /* FIXME, use a real flag for deciding about the key type */
4280 if (root->fs_info->tree_root == root)
4281 key_type = BTRFS_DIR_ITEM_KEY;
4283 /* special case for "." */
4284 if (filp->f_pos == 0) {
4285 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4290 /* special case for .., just use the back ref */
4291 if (filp->f_pos == 1) {
4292 u64 pino = parent_ino(filp->f_path.dentry);
4293 over = filldir(dirent, "..", 2,
4299 path = btrfs_alloc_path();
4304 if (key_type == BTRFS_DIR_INDEX_KEY) {
4305 INIT_LIST_HEAD(&ins_list);
4306 INIT_LIST_HEAD(&del_list);
4307 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4310 btrfs_set_key_type(&key, key_type);
4311 key.offset = filp->f_pos;
4312 key.objectid = btrfs_ino(inode);
4314 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4319 leaf = path->nodes[0];
4320 slot = path->slots[0];
4321 if (slot >= btrfs_header_nritems(leaf)) {
4322 ret = btrfs_next_leaf(root, path);
4330 item = btrfs_item_nr(leaf, slot);
4331 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4333 if (found_key.objectid != key.objectid)
4335 if (btrfs_key_type(&found_key) != key_type)
4337 if (found_key.offset < filp->f_pos)
4339 if (key_type == BTRFS_DIR_INDEX_KEY &&
4340 btrfs_should_delete_dir_index(&del_list,
4344 filp->f_pos = found_key.offset;
4347 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4349 di_total = btrfs_item_size(leaf, item);
4351 while (di_cur < di_total) {
4352 struct btrfs_key location;
4354 if (verify_dir_item(root, leaf, di))
4357 name_len = btrfs_dir_name_len(leaf, di);
4358 if (name_len <= sizeof(tmp_name)) {
4359 name_ptr = tmp_name;
4361 name_ptr = kmalloc(name_len, GFP_NOFS);
4367 read_extent_buffer(leaf, name_ptr,
4368 (unsigned long)(di + 1), name_len);
4370 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4371 btrfs_dir_item_key_to_cpu(leaf, di, &location);
4373 /* is this a reference to our own snapshot? If so
4376 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4377 location.objectid == root->root_key.objectid) {
4381 over = filldir(dirent, name_ptr, name_len,
4382 found_key.offset, location.objectid,
4386 if (name_ptr != tmp_name)
4391 di_len = btrfs_dir_name_len(leaf, di) +
4392 btrfs_dir_data_len(leaf, di) + sizeof(*di);
4394 di = (struct btrfs_dir_item *)((char *)di + di_len);
4400 if (key_type == BTRFS_DIR_INDEX_KEY) {
4403 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4409 /* Reached end of directory/root. Bump pos past the last item. */
4410 if (key_type == BTRFS_DIR_INDEX_KEY)
4412 * 32-bit glibc will use getdents64, but then strtol -
4413 * so the last number we can serve is this.
4415 filp->f_pos = 0x7fffffff;
4421 if (key_type == BTRFS_DIR_INDEX_KEY)
4422 btrfs_put_delayed_items(&ins_list, &del_list);
4423 btrfs_free_path(path);
4427 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4429 struct btrfs_root *root = BTRFS_I(inode)->root;
4430 struct btrfs_trans_handle *trans;
4432 bool nolock = false;
4434 if (BTRFS_I(inode)->dummy_inode)
4438 if (root->fs_info->closing && is_free_space_inode(root, inode))
4441 if (wbc->sync_mode == WB_SYNC_ALL) {
4443 trans = btrfs_join_transaction_nolock(root, 1);
4445 trans = btrfs_join_transaction(root, 1);
4447 return PTR_ERR(trans);
4448 btrfs_set_trans_block_group(trans, inode);
4450 ret = btrfs_end_transaction_nolock(trans, root);
4452 ret = btrfs_commit_transaction(trans, root);
4458 * This is somewhat expensive, updating the tree every time the
4459 * inode changes. But, it is most likely to find the inode in cache.
4460 * FIXME, needs more benchmarking...there are no reasons other than performance
4461 * to keep or drop this code.
4463 void btrfs_dirty_inode(struct inode *inode)
4465 struct btrfs_root *root = BTRFS_I(inode)->root;
4466 struct btrfs_trans_handle *trans;
4469 if (BTRFS_I(inode)->dummy_inode)
4472 trans = btrfs_join_transaction(root, 1);
4473 BUG_ON(IS_ERR(trans));
4474 btrfs_set_trans_block_group(trans, inode);
4476 ret = btrfs_update_inode(trans, root, inode);
4477 if (ret && ret == -ENOSPC) {
4478 /* whoops, lets try again with the full transaction */
4479 btrfs_end_transaction(trans, root);
4480 trans = btrfs_start_transaction(root, 1);
4481 if (IS_ERR(trans)) {
4482 if (printk_ratelimit()) {
4483 printk(KERN_ERR "btrfs: fail to "
4484 "dirty inode %llu error %ld\n",
4485 (unsigned long long)btrfs_ino(inode),
4490 btrfs_set_trans_block_group(trans, inode);
4492 ret = btrfs_update_inode(trans, root, inode);
4494 if (printk_ratelimit()) {
4495 printk(KERN_ERR "btrfs: fail to "
4496 "dirty inode %llu error %d\n",
4497 (unsigned long long)btrfs_ino(inode),
4502 btrfs_end_transaction(trans, root);
4503 if (BTRFS_I(inode)->delayed_node)
4504 btrfs_balance_delayed_items(root);
4508 * find the highest existing sequence number in a directory
4509 * and then set the in-memory index_cnt variable to reflect
4510 * free sequence numbers
4512 static int btrfs_set_inode_index_count(struct inode *inode)
4514 struct btrfs_root *root = BTRFS_I(inode)->root;
4515 struct btrfs_key key, found_key;
4516 struct btrfs_path *path;
4517 struct extent_buffer *leaf;
4520 key.objectid = btrfs_ino(inode);
4521 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4522 key.offset = (u64)-1;
4524 path = btrfs_alloc_path();
4528 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4531 /* FIXME: we should be able to handle this */
4537 * MAGIC NUMBER EXPLANATION:
4538 * since we search a directory based on f_pos we have to start at 2
4539 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4540 * else has to start at 2
4542 if (path->slots[0] == 0) {
4543 BTRFS_I(inode)->index_cnt = 2;
4549 leaf = path->nodes[0];
4550 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4552 if (found_key.objectid != btrfs_ino(inode) ||
4553 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4554 BTRFS_I(inode)->index_cnt = 2;
4558 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4560 btrfs_free_path(path);
4565 * helper to find a free sequence number in a given directory. This current
4566 * code is very simple, later versions will do smarter things in the btree
4568 int btrfs_set_inode_index(struct inode *dir, u64 *index)
4572 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4573 ret = btrfs_inode_delayed_dir_index_count(dir);
4575 ret = btrfs_set_inode_index_count(dir);
4581 *index = BTRFS_I(dir)->index_cnt;
4582 BTRFS_I(dir)->index_cnt++;
4587 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4588 struct btrfs_root *root,
4590 const char *name, int name_len,
4591 u64 ref_objectid, u64 objectid,
4592 u64 alloc_hint, int mode, u64 *index)
4594 struct inode *inode;
4595 struct btrfs_inode_item *inode_item;
4596 struct btrfs_key *location;
4597 struct btrfs_path *path;
4598 struct btrfs_inode_ref *ref;
4599 struct btrfs_key key[2];
4605 path = btrfs_alloc_path();
4608 inode = new_inode(root->fs_info->sb);
4610 btrfs_free_path(path);
4611 return ERR_PTR(-ENOMEM);
4615 * we have to initialize this early, so we can reclaim the inode
4616 * number if we fail afterwards in this function.
4618 inode->i_ino = objectid;
4621 trace_btrfs_inode_request(dir);
4623 ret = btrfs_set_inode_index(dir, index);
4625 btrfs_free_path(path);
4627 return ERR_PTR(ret);
4631 * index_cnt is ignored for everything but a dir,
4632 * btrfs_get_inode_index_count has an explanation for the magic
4635 BTRFS_I(inode)->index_cnt = 2;
4636 BTRFS_I(inode)->root = root;
4637 BTRFS_I(inode)->generation = trans->transid;
4638 inode->i_generation = BTRFS_I(inode)->generation;
4639 btrfs_set_inode_space_info(root, inode);
4645 BTRFS_I(inode)->block_group =
4646 btrfs_find_block_group(root, 0, alloc_hint, owner);
4648 key[0].objectid = objectid;
4649 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4652 key[1].objectid = objectid;
4653 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4654 key[1].offset = ref_objectid;
4656 sizes[0] = sizeof(struct btrfs_inode_item);
4657 sizes[1] = name_len + sizeof(*ref);
4659 path->leave_spinning = 1;
4660 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4664 inode_init_owner(inode, dir, mode);
4665 inode_set_bytes(inode, 0);
4666 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4667 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4668 struct btrfs_inode_item);
4669 fill_inode_item(trans, path->nodes[0], inode_item, inode);
4671 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4672 struct btrfs_inode_ref);
4673 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
4674 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
4675 ptr = (unsigned long)(ref + 1);
4676 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4678 btrfs_mark_buffer_dirty(path->nodes[0]);
4679 btrfs_free_path(path);
4681 location = &BTRFS_I(inode)->location;
4682 location->objectid = objectid;
4683 location->offset = 0;
4684 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4686 btrfs_inherit_iflags(inode, dir);
4688 if ((mode & S_IFREG)) {
4689 if (btrfs_test_opt(root, NODATASUM))
4690 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4691 if (btrfs_test_opt(root, NODATACOW) ||
4692 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4693 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4696 insert_inode_hash(inode);
4697 inode_tree_add(inode);
4699 trace_btrfs_inode_new(inode);
4704 BTRFS_I(dir)->index_cnt--;
4705 btrfs_free_path(path);
4707 return ERR_PTR(ret);
4710 static inline u8 btrfs_inode_type(struct inode *inode)
4712 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4716 * utility function to add 'inode' into 'parent_inode' with
4717 * a give name and a given sequence number.
4718 * if 'add_backref' is true, also insert a backref from the
4719 * inode to the parent directory.
4721 int btrfs_add_link(struct btrfs_trans_handle *trans,
4722 struct inode *parent_inode, struct inode *inode,
4723 const char *name, int name_len, int add_backref, u64 index)
4726 struct btrfs_key key;
4727 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4728 u64 ino = btrfs_ino(inode);
4729 u64 parent_ino = btrfs_ino(parent_inode);
4731 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4732 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4735 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4739 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4740 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4741 key.objectid, root->root_key.objectid,
4742 parent_ino, index, name, name_len);
4743 } else if (add_backref) {
4744 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4749 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4751 btrfs_inode_type(inode), index);
4754 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4756 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4757 ret = btrfs_update_inode(trans, root, parent_inode);
4762 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
4763 struct inode *dir, struct dentry *dentry,
4764 struct inode *inode, int backref, u64 index)
4766 int err = btrfs_add_link(trans, dir, inode,
4767 dentry->d_name.name, dentry->d_name.len,
4770 d_instantiate(dentry, inode);
4778 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4779 int mode, dev_t rdev)
4781 struct btrfs_trans_handle *trans;
4782 struct btrfs_root *root = BTRFS_I(dir)->root;
4783 struct inode *inode = NULL;
4787 unsigned long nr = 0;
4790 if (!new_valid_dev(rdev))
4794 * 2 for inode item and ref
4796 * 1 for xattr if selinux is on
4798 trans = btrfs_start_transaction(root, 5);
4800 return PTR_ERR(trans);
4802 btrfs_set_trans_block_group(trans, dir);
4804 err = btrfs_find_free_ino(root, &objectid);
4808 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4809 dentry->d_name.len, btrfs_ino(dir), objectid,
4810 BTRFS_I(dir)->block_group, mode, &index);
4811 if (IS_ERR(inode)) {
4812 err = PTR_ERR(inode);
4816 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4822 btrfs_set_trans_block_group(trans, inode);
4823 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4827 inode->i_op = &btrfs_special_inode_operations;
4828 init_special_inode(inode, inode->i_mode, rdev);
4829 btrfs_update_inode(trans, root, inode);
4831 btrfs_update_inode_block_group(trans, inode);
4832 btrfs_update_inode_block_group(trans, dir);
4834 nr = trans->blocks_used;
4835 btrfs_end_transaction_throttle(trans, root);
4836 btrfs_btree_balance_dirty(root, nr);
4838 inode_dec_link_count(inode);
4844 static int btrfs_create(struct inode *dir, struct dentry *dentry,
4845 int mode, struct nameidata *nd)
4847 struct btrfs_trans_handle *trans;
4848 struct btrfs_root *root = BTRFS_I(dir)->root;
4849 struct inode *inode = NULL;
4852 unsigned long nr = 0;
4857 * 2 for inode item and ref
4859 * 1 for xattr if selinux is on
4861 trans = btrfs_start_transaction(root, 5);
4863 return PTR_ERR(trans);
4865 btrfs_set_trans_block_group(trans, dir);
4867 err = btrfs_find_free_ino(root, &objectid);
4871 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4872 dentry->d_name.len, btrfs_ino(dir), objectid,
4873 BTRFS_I(dir)->block_group, mode, &index);
4874 if (IS_ERR(inode)) {
4875 err = PTR_ERR(inode);
4879 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4885 btrfs_set_trans_block_group(trans, inode);
4886 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4890 inode->i_mapping->a_ops = &btrfs_aops;
4891 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4892 inode->i_fop = &btrfs_file_operations;
4893 inode->i_op = &btrfs_file_inode_operations;
4894 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4896 btrfs_update_inode_block_group(trans, inode);
4897 btrfs_update_inode_block_group(trans, dir);
4899 nr = trans->blocks_used;
4900 btrfs_end_transaction_throttle(trans, root);
4902 inode_dec_link_count(inode);
4905 btrfs_btree_balance_dirty(root, nr);
4909 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4910 struct dentry *dentry)
4912 struct btrfs_trans_handle *trans;
4913 struct btrfs_root *root = BTRFS_I(dir)->root;
4914 struct inode *inode = old_dentry->d_inode;
4916 unsigned long nr = 0;
4920 /* do not allow sys_link's with other subvols of the same device */
4921 if (root->objectid != BTRFS_I(inode)->root->objectid)
4924 if (inode->i_nlink == ~0U)
4927 err = btrfs_set_inode_index(dir, &index);
4932 * 2 items for inode and inode ref
4933 * 2 items for dir items
4934 * 1 item for parent inode
4936 trans = btrfs_start_transaction(root, 5);
4937 if (IS_ERR(trans)) {
4938 err = PTR_ERR(trans);
4942 btrfs_inc_nlink(inode);
4943 inode->i_ctime = CURRENT_TIME;
4945 btrfs_set_trans_block_group(trans, dir);
4948 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
4953 struct dentry *parent = dget_parent(dentry);
4954 btrfs_update_inode_block_group(trans, dir);
4955 err = btrfs_update_inode(trans, root, inode);
4957 btrfs_log_new_name(trans, inode, NULL, parent);
4961 nr = trans->blocks_used;
4962 btrfs_end_transaction_throttle(trans, root);
4965 inode_dec_link_count(inode);
4968 btrfs_btree_balance_dirty(root, nr);
4972 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4974 struct inode *inode = NULL;
4975 struct btrfs_trans_handle *trans;
4976 struct btrfs_root *root = BTRFS_I(dir)->root;
4978 int drop_on_err = 0;
4981 unsigned long nr = 1;
4984 * 2 items for inode and ref
4985 * 2 items for dir items
4986 * 1 for xattr if selinux is on
4988 trans = btrfs_start_transaction(root, 5);
4990 return PTR_ERR(trans);
4991 btrfs_set_trans_block_group(trans, dir);
4993 err = btrfs_find_free_ino(root, &objectid);
4997 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4998 dentry->d_name.len, btrfs_ino(dir), objectid,
4999 BTRFS_I(dir)->block_group, S_IFDIR | mode,
5001 if (IS_ERR(inode)) {
5002 err = PTR_ERR(inode);
5008 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5012 inode->i_op = &btrfs_dir_inode_operations;
5013 inode->i_fop = &btrfs_dir_file_operations;
5014 btrfs_set_trans_block_group(trans, inode);
5016 btrfs_i_size_write(inode, 0);
5017 err = btrfs_update_inode(trans, root, inode);
5021 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5022 dentry->d_name.len, 0, index);
5026 d_instantiate(dentry, inode);
5028 btrfs_update_inode_block_group(trans, inode);
5029 btrfs_update_inode_block_group(trans, dir);
5032 nr = trans->blocks_used;
5033 btrfs_end_transaction_throttle(trans, root);
5036 btrfs_btree_balance_dirty(root, nr);
5040 /* helper for btfs_get_extent. Given an existing extent in the tree,
5041 * and an extent that you want to insert, deal with overlap and insert
5042 * the new extent into the tree.
5044 static int merge_extent_mapping(struct extent_map_tree *em_tree,
5045 struct extent_map *existing,
5046 struct extent_map *em,
5047 u64 map_start, u64 map_len)
5051 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5052 start_diff = map_start - em->start;
5053 em->start = map_start;
5055 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5056 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
5057 em->block_start += start_diff;
5058 em->block_len -= start_diff;
5060 return add_extent_mapping(em_tree, em);
5063 static noinline int uncompress_inline(struct btrfs_path *path,
5064 struct inode *inode, struct page *page,
5065 size_t pg_offset, u64 extent_offset,
5066 struct btrfs_file_extent_item *item)
5069 struct extent_buffer *leaf = path->nodes[0];
5072 unsigned long inline_size;
5076 WARN_ON(pg_offset != 0);
5077 compress_type = btrfs_file_extent_compression(leaf, item);
5078 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5079 inline_size = btrfs_file_extent_inline_item_len(leaf,
5080 btrfs_item_nr(leaf, path->slots[0]));
5081 tmp = kmalloc(inline_size, GFP_NOFS);
5084 ptr = btrfs_file_extent_inline_start(item);
5086 read_extent_buffer(leaf, tmp, ptr, inline_size);
5088 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
5089 ret = btrfs_decompress(compress_type, tmp, page,
5090 extent_offset, inline_size, max_size);
5092 char *kaddr = kmap_atomic(page, KM_USER0);
5093 unsigned long copy_size = min_t(u64,
5094 PAGE_CACHE_SIZE - pg_offset,
5095 max_size - extent_offset);
5096 memset(kaddr + pg_offset, 0, copy_size);
5097 kunmap_atomic(kaddr, KM_USER0);
5104 * a bit scary, this does extent mapping from logical file offset to the disk.
5105 * the ugly parts come from merging extents from the disk with the in-ram
5106 * representation. This gets more complex because of the data=ordered code,
5107 * where the in-ram extents might be locked pending data=ordered completion.
5109 * This also copies inline extents directly into the page.
5112 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5113 size_t pg_offset, u64 start, u64 len,
5119 u64 extent_start = 0;
5121 u64 objectid = btrfs_ino(inode);
5123 struct btrfs_path *path = NULL;
5124 struct btrfs_root *root = BTRFS_I(inode)->root;
5125 struct btrfs_file_extent_item *item;
5126 struct extent_buffer *leaf;
5127 struct btrfs_key found_key;
5128 struct extent_map *em = NULL;
5129 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5130 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5131 struct btrfs_trans_handle *trans = NULL;
5135 read_lock(&em_tree->lock);
5136 em = lookup_extent_mapping(em_tree, start, len);
5138 em->bdev = root->fs_info->fs_devices->latest_bdev;
5139 read_unlock(&em_tree->lock);
5142 if (em->start > start || em->start + em->len <= start)
5143 free_extent_map(em);
5144 else if (em->block_start == EXTENT_MAP_INLINE && page)
5145 free_extent_map(em);
5149 em = alloc_extent_map(GFP_NOFS);
5154 em->bdev = root->fs_info->fs_devices->latest_bdev;
5155 em->start = EXTENT_MAP_HOLE;
5156 em->orig_start = EXTENT_MAP_HOLE;
5158 em->block_len = (u64)-1;
5161 path = btrfs_alloc_path();
5165 ret = btrfs_lookup_file_extent(trans, root, path,
5166 objectid, start, trans != NULL);
5173 if (path->slots[0] == 0)
5178 leaf = path->nodes[0];
5179 item = btrfs_item_ptr(leaf, path->slots[0],
5180 struct btrfs_file_extent_item);
5181 /* are we inside the extent that was found? */
5182 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5183 found_type = btrfs_key_type(&found_key);
5184 if (found_key.objectid != objectid ||
5185 found_type != BTRFS_EXTENT_DATA_KEY) {
5189 found_type = btrfs_file_extent_type(leaf, item);
5190 extent_start = found_key.offset;
5191 compress_type = btrfs_file_extent_compression(leaf, item);
5192 if (found_type == BTRFS_FILE_EXTENT_REG ||
5193 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5194 extent_end = extent_start +
5195 btrfs_file_extent_num_bytes(leaf, item);
5196 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5198 size = btrfs_file_extent_inline_len(leaf, item);
5199 extent_end = (extent_start + size + root->sectorsize - 1) &
5200 ~((u64)root->sectorsize - 1);
5203 if (start >= extent_end) {
5205 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5206 ret = btrfs_next_leaf(root, path);
5213 leaf = path->nodes[0];
5215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5216 if (found_key.objectid != objectid ||
5217 found_key.type != BTRFS_EXTENT_DATA_KEY)
5219 if (start + len <= found_key.offset)
5222 em->len = found_key.offset - start;
5226 if (found_type == BTRFS_FILE_EXTENT_REG ||
5227 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5228 em->start = extent_start;
5229 em->len = extent_end - extent_start;
5230 em->orig_start = extent_start -
5231 btrfs_file_extent_offset(leaf, item);
5232 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5234 em->block_start = EXTENT_MAP_HOLE;
5237 if (compress_type != BTRFS_COMPRESS_NONE) {
5238 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5239 em->compress_type = compress_type;
5240 em->block_start = bytenr;
5241 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5244 bytenr += btrfs_file_extent_offset(leaf, item);
5245 em->block_start = bytenr;
5246 em->block_len = em->len;
5247 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5248 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5251 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5255 size_t extent_offset;
5258 em->block_start = EXTENT_MAP_INLINE;
5259 if (!page || create) {
5260 em->start = extent_start;
5261 em->len = extent_end - extent_start;
5265 size = btrfs_file_extent_inline_len(leaf, item);
5266 extent_offset = page_offset(page) + pg_offset - extent_start;
5267 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
5268 size - extent_offset);
5269 em->start = extent_start + extent_offset;
5270 em->len = (copy_size + root->sectorsize - 1) &
5271 ~((u64)root->sectorsize - 1);
5272 em->orig_start = EXTENT_MAP_INLINE;
5273 if (compress_type) {
5274 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5275 em->compress_type = compress_type;
5277 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5278 if (create == 0 && !PageUptodate(page)) {
5279 if (btrfs_file_extent_compression(leaf, item) !=
5280 BTRFS_COMPRESS_NONE) {
5281 ret = uncompress_inline(path, inode, page,
5283 extent_offset, item);
5287 read_extent_buffer(leaf, map + pg_offset, ptr,
5289 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5290 memset(map + pg_offset + copy_size, 0,
5291 PAGE_CACHE_SIZE - pg_offset -
5296 flush_dcache_page(page);
5297 } else if (create && PageUptodate(page)) {
5301 free_extent_map(em);
5303 btrfs_release_path(root, path);
5304 trans = btrfs_join_transaction(root, 1);
5306 return ERR_CAST(trans);
5310 write_extent_buffer(leaf, map + pg_offset, ptr,
5313 btrfs_mark_buffer_dirty(leaf);
5315 set_extent_uptodate(io_tree, em->start,
5316 extent_map_end(em) - 1, NULL, GFP_NOFS);
5319 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
5326 em->block_start = EXTENT_MAP_HOLE;
5327 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5329 btrfs_release_path(root, path);
5330 if (em->start > start || extent_map_end(em) <= start) {
5331 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5332 "[%llu %llu]\n", (unsigned long long)em->start,
5333 (unsigned long long)em->len,
5334 (unsigned long long)start,
5335 (unsigned long long)len);
5341 write_lock(&em_tree->lock);
5342 ret = add_extent_mapping(em_tree, em);
5343 /* it is possible that someone inserted the extent into the tree
5344 * while we had the lock dropped. It is also possible that
5345 * an overlapping map exists in the tree
5347 if (ret == -EEXIST) {
5348 struct extent_map *existing;
5352 existing = lookup_extent_mapping(em_tree, start, len);
5353 if (existing && (existing->start > start ||
5354 existing->start + existing->len <= start)) {
5355 free_extent_map(existing);
5359 existing = lookup_extent_mapping(em_tree, em->start,
5362 err = merge_extent_mapping(em_tree, existing,
5365 free_extent_map(existing);
5367 free_extent_map(em);
5372 free_extent_map(em);
5376 free_extent_map(em);
5381 write_unlock(&em_tree->lock);
5384 trace_btrfs_get_extent(root, em);
5387 btrfs_free_path(path);
5389 ret = btrfs_end_transaction(trans, root);
5394 free_extent_map(em);
5395 return ERR_PTR(err);
5400 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5401 size_t pg_offset, u64 start, u64 len,
5404 struct extent_map *em;
5405 struct extent_map *hole_em = NULL;
5406 u64 range_start = start;
5412 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5417 * if our em maps to a hole, there might
5418 * actually be delalloc bytes behind it
5420 if (em->block_start != EXTENT_MAP_HOLE)
5426 /* check to see if we've wrapped (len == -1 or similar) */
5435 /* ok, we didn't find anything, lets look for delalloc */
5436 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5437 end, len, EXTENT_DELALLOC, 1);
5438 found_end = range_start + found;
5439 if (found_end < range_start)
5440 found_end = (u64)-1;
5443 * we didn't find anything useful, return
5444 * the original results from get_extent()
5446 if (range_start > end || found_end <= start) {
5452 /* adjust the range_start to make sure it doesn't
5453 * go backwards from the start they passed in
5455 range_start = max(start,range_start);
5456 found = found_end - range_start;
5459 u64 hole_start = start;
5462 em = alloc_extent_map(GFP_NOFS);
5468 * when btrfs_get_extent can't find anything it
5469 * returns one huge hole
5471 * make sure what it found really fits our range, and
5472 * adjust to make sure it is based on the start from
5476 u64 calc_end = extent_map_end(hole_em);
5478 if (calc_end <= start || (hole_em->start > end)) {
5479 free_extent_map(hole_em);
5482 hole_start = max(hole_em->start, start);
5483 hole_len = calc_end - hole_start;
5487 if (hole_em && range_start > hole_start) {
5488 /* our hole starts before our delalloc, so we
5489 * have to return just the parts of the hole
5490 * that go until the delalloc starts
5492 em->len = min(hole_len,
5493 range_start - hole_start);
5494 em->start = hole_start;
5495 em->orig_start = hole_start;
5497 * don't adjust block start at all,
5498 * it is fixed at EXTENT_MAP_HOLE
5500 em->block_start = hole_em->block_start;
5501 em->block_len = hole_len;
5503 em->start = range_start;
5505 em->orig_start = range_start;
5506 em->block_start = EXTENT_MAP_DELALLOC;
5507 em->block_len = found;
5509 } else if (hole_em) {
5514 free_extent_map(hole_em);
5516 free_extent_map(em);
5517 return ERR_PTR(err);
5522 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5523 struct extent_map *em,
5526 struct btrfs_root *root = BTRFS_I(inode)->root;
5527 struct btrfs_trans_handle *trans;
5528 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5529 struct btrfs_key ins;
5532 bool insert = false;
5535 * Ok if the extent map we looked up is a hole and is for the exact
5536 * range we want, there is no reason to allocate a new one, however if
5537 * it is not right then we need to free this one and drop the cache for
5540 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5542 free_extent_map(em);
5545 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5548 trans = btrfs_join_transaction(root, 0);
5550 return ERR_CAST(trans);
5552 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5554 alloc_hint = get_extent_allocation_hint(inode, start, len);
5555 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5556 alloc_hint, (u64)-1, &ins, 1);
5563 em = alloc_extent_map(GFP_NOFS);
5565 em = ERR_PTR(-ENOMEM);
5571 em->orig_start = em->start;
5572 em->len = ins.offset;
5574 em->block_start = ins.objectid;
5575 em->block_len = ins.offset;
5576 em->bdev = root->fs_info->fs_devices->latest_bdev;
5579 * We need to do this because if we're using the original em we searched
5580 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5583 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5586 write_lock(&em_tree->lock);
5587 ret = add_extent_mapping(em_tree, em);
5588 write_unlock(&em_tree->lock);
5591 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5594 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5595 ins.offset, ins.offset, 0);
5597 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5601 btrfs_end_transaction(trans, root);
5606 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5607 * block must be cow'd
5609 static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5610 struct inode *inode, u64 offset, u64 len)
5612 struct btrfs_path *path;
5614 struct extent_buffer *leaf;
5615 struct btrfs_root *root = BTRFS_I(inode)->root;
5616 struct btrfs_file_extent_item *fi;
5617 struct btrfs_key key;
5625 path = btrfs_alloc_path();
5629 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5634 slot = path->slots[0];
5637 /* can't find the item, must cow */
5644 leaf = path->nodes[0];
5645 btrfs_item_key_to_cpu(leaf, &key, slot);
5646 if (key.objectid != btrfs_ino(inode) ||
5647 key.type != BTRFS_EXTENT_DATA_KEY) {
5648 /* not our file or wrong item type, must cow */
5652 if (key.offset > offset) {
5653 /* Wrong offset, must cow */
5657 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5658 found_type = btrfs_file_extent_type(leaf, fi);
5659 if (found_type != BTRFS_FILE_EXTENT_REG &&
5660 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5661 /* not a regular extent, must cow */
5664 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5665 backref_offset = btrfs_file_extent_offset(leaf, fi);
5667 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5668 if (extent_end < offset + len) {
5669 /* extent doesn't include our full range, must cow */
5673 if (btrfs_extent_readonly(root, disk_bytenr))
5677 * look for other files referencing this extent, if we
5678 * find any we must cow
5680 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5681 key.offset - backref_offset, disk_bytenr))
5685 * adjust disk_bytenr and num_bytes to cover just the bytes
5686 * in this extent we are about to write. If there
5687 * are any csums in that range we have to cow in order
5688 * to keep the csums correct
5690 disk_bytenr += backref_offset;
5691 disk_bytenr += offset - key.offset;
5692 num_bytes = min(offset + len, extent_end) - offset;
5693 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5696 * all of the above have passed, it is safe to overwrite this extent
5701 btrfs_free_path(path);
5705 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5706 struct buffer_head *bh_result, int create)
5708 struct extent_map *em;
5709 struct btrfs_root *root = BTRFS_I(inode)->root;
5710 u64 start = iblock << inode->i_blkbits;
5711 u64 len = bh_result->b_size;
5712 struct btrfs_trans_handle *trans;
5714 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5719 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5720 * io. INLINE is special, and we could probably kludge it in here, but
5721 * it's still buffered so for safety lets just fall back to the generic
5724 * For COMPRESSED we _have_ to read the entire extent in so we can
5725 * decompress it, so there will be buffering required no matter what we
5726 * do, so go ahead and fallback to buffered.
5728 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5729 * to buffered IO. Don't blame me, this is the price we pay for using
5732 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5733 em->block_start == EXTENT_MAP_INLINE) {
5734 free_extent_map(em);
5738 /* Just a good old fashioned hole, return */
5739 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5740 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5741 free_extent_map(em);
5742 /* DIO will do one hole at a time, so just unlock a sector */
5743 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5744 start + root->sectorsize - 1, GFP_NOFS);
5749 * We don't allocate a new extent in the following cases
5751 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5753 * 2) The extent is marked as PREALLOC. We're good to go here and can
5754 * just use the extent.
5758 len = em->len - (start - em->start);
5762 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5763 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5764 em->block_start != EXTENT_MAP_HOLE)) {
5769 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5770 type = BTRFS_ORDERED_PREALLOC;
5772 type = BTRFS_ORDERED_NOCOW;
5773 len = min(len, em->len - (start - em->start));
5774 block_start = em->block_start + (start - em->start);
5777 * we're not going to log anything, but we do need
5778 * to make sure the current transaction stays open
5779 * while we look for nocow cross refs
5781 trans = btrfs_join_transaction(root, 0);
5785 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5786 ret = btrfs_add_ordered_extent_dio(inode, start,
5787 block_start, len, len, type);
5788 btrfs_end_transaction(trans, root);
5790 free_extent_map(em);
5795 btrfs_end_transaction(trans, root);
5799 * this will cow the extent, reset the len in case we changed
5802 len = bh_result->b_size;
5803 em = btrfs_new_extent_direct(inode, em, start, len);
5806 len = min(len, em->len - (start - em->start));
5808 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5809 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5812 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5814 bh_result->b_size = len;
5815 bh_result->b_bdev = em->bdev;
5816 set_buffer_mapped(bh_result);
5817 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5818 set_buffer_new(bh_result);
5820 free_extent_map(em);
5825 struct btrfs_dio_private {
5826 struct inode *inode;
5833 /* number of bios pending for this dio */
5834 atomic_t pending_bios;
5839 struct bio *orig_bio;
5842 static void btrfs_endio_direct_read(struct bio *bio, int err)
5844 struct btrfs_dio_private *dip = bio->bi_private;
5845 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5846 struct bio_vec *bvec = bio->bi_io_vec;
5847 struct inode *inode = dip->inode;
5848 struct btrfs_root *root = BTRFS_I(inode)->root;
5850 u32 *private = dip->csums;
5852 start = dip->logical_offset;
5854 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5855 struct page *page = bvec->bv_page;
5858 unsigned long flags;
5860 local_irq_save(flags);
5861 kaddr = kmap_atomic(page, KM_IRQ0);
5862 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5863 csum, bvec->bv_len);
5864 btrfs_csum_final(csum, (char *)&csum);
5865 kunmap_atomic(kaddr, KM_IRQ0);
5866 local_irq_restore(flags);
5868 flush_dcache_page(bvec->bv_page);
5869 if (csum != *private) {
5870 printk(KERN_ERR "btrfs csum failed ino %llu off"
5871 " %llu csum %u private %u\n",
5872 (unsigned long long)btrfs_ino(inode),
5873 (unsigned long long)start,
5879 start += bvec->bv_len;
5882 } while (bvec <= bvec_end);
5884 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5885 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5886 bio->bi_private = dip->private;
5891 /* If we had a csum failure make sure to clear the uptodate flag */
5893 clear_bit(BIO_UPTODATE, &bio->bi_flags);
5894 dio_end_io(bio, err);
5897 static void btrfs_endio_direct_write(struct bio *bio, int err)
5899 struct btrfs_dio_private *dip = bio->bi_private;
5900 struct inode *inode = dip->inode;
5901 struct btrfs_root *root = BTRFS_I(inode)->root;
5902 struct btrfs_trans_handle *trans;
5903 struct btrfs_ordered_extent *ordered = NULL;
5904 struct extent_state *cached_state = NULL;
5905 u64 ordered_offset = dip->logical_offset;
5906 u64 ordered_bytes = dip->bytes;
5912 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5920 trans = btrfs_join_transaction(root, 1);
5921 if (IS_ERR(trans)) {
5925 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5927 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5928 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5930 ret = btrfs_update_inode(trans, root, inode);
5935 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5936 ordered->file_offset + ordered->len - 1, 0,
5937 &cached_state, GFP_NOFS);
5939 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5940 ret = btrfs_mark_extent_written(trans, inode,
5941 ordered->file_offset,
5942 ordered->file_offset +
5949 ret = insert_reserved_file_extent(trans, inode,
5950 ordered->file_offset,
5956 BTRFS_FILE_EXTENT_REG);
5957 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5958 ordered->file_offset, ordered->len);
5966 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5967 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5969 btrfs_update_inode(trans, root, inode);
5972 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5973 ordered->file_offset + ordered->len - 1,
5974 &cached_state, GFP_NOFS);
5976 btrfs_delalloc_release_metadata(inode, ordered->len);
5977 btrfs_end_transaction(trans, root);
5978 ordered_offset = ordered->file_offset + ordered->len;
5979 btrfs_put_ordered_extent(ordered);
5980 btrfs_put_ordered_extent(ordered);
5984 * our bio might span multiple ordered extents. If we haven't
5985 * completed the accounting for the whole dio, go back and try again
5987 if (ordered_offset < dip->logical_offset + dip->bytes) {
5988 ordered_bytes = dip->logical_offset + dip->bytes -
5993 bio->bi_private = dip->private;
5998 /* If we had an error make sure to clear the uptodate flag */
6000 clear_bit(BIO_UPTODATE, &bio->bi_flags);
6001 dio_end_io(bio, err);
6004 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6005 struct bio *bio, int mirror_num,
6006 unsigned long bio_flags, u64 offset)
6009 struct btrfs_root *root = BTRFS_I(inode)->root;
6010 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
6015 static void btrfs_end_dio_bio(struct bio *bio, int err)
6017 struct btrfs_dio_private *dip = bio->bi_private;
6020 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
6021 "sector %#Lx len %u err no %d\n",
6022 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
6023 (unsigned long long)bio->bi_sector, bio->bi_size, err);
6027 * before atomic variable goto zero, we must make sure
6028 * dip->errors is perceived to be set.
6030 smp_mb__before_atomic_dec();
6033 /* if there are more bios still pending for this dio, just exit */
6034 if (!atomic_dec_and_test(&dip->pending_bios))
6038 bio_io_error(dip->orig_bio);
6040 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6041 bio_endio(dip->orig_bio, 0);
6047 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6048 u64 first_sector, gfp_t gfp_flags)
6050 int nr_vecs = bio_get_nr_vecs(bdev);
6051 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6054 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6055 int rw, u64 file_offset, int skip_sum,
6056 u32 *csums, int async_submit)
6058 int write = rw & REQ_WRITE;
6059 struct btrfs_root *root = BTRFS_I(inode)->root;
6063 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6070 if (write && async_submit) {
6071 ret = btrfs_wq_submit_bio(root->fs_info,
6072 inode, rw, bio, 0, 0,
6074 __btrfs_submit_bio_start_direct_io,
6075 __btrfs_submit_bio_done);
6079 * If we aren't doing async submit, calculate the csum of the
6082 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6085 } else if (!skip_sum) {
6086 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
6087 file_offset, csums);
6093 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
6099 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6102 struct inode *inode = dip->inode;
6103 struct btrfs_root *root = BTRFS_I(inode)->root;
6104 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6106 struct bio *orig_bio = dip->orig_bio;
6107 struct bio_vec *bvec = orig_bio->bi_io_vec;
6108 u64 start_sector = orig_bio->bi_sector;
6109 u64 file_offset = dip->logical_offset;
6113 u32 *csums = dip->csums;
6115 int async_submit = 0;
6116 int write = rw & REQ_WRITE;
6118 map_length = orig_bio->bi_size;
6119 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6120 &map_length, NULL, 0);
6126 if (map_length >= orig_bio->bi_size) {
6132 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6135 bio->bi_private = dip;
6136 bio->bi_end_io = btrfs_end_dio_bio;
6137 atomic_inc(&dip->pending_bios);
6139 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6140 if (unlikely(map_length < submit_len + bvec->bv_len ||
6141 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6142 bvec->bv_offset) < bvec->bv_len)) {
6144 * inc the count before we submit the bio so
6145 * we know the end IO handler won't happen before
6146 * we inc the count. Otherwise, the dip might get freed
6147 * before we're done setting it up
6149 atomic_inc(&dip->pending_bios);
6150 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6151 file_offset, skip_sum,
6152 csums, async_submit);
6155 atomic_dec(&dip->pending_bios);
6159 /* Write's use the ordered csums */
6160 if (!write && !skip_sum)
6161 csums = csums + nr_pages;
6162 start_sector += submit_len >> 9;
6163 file_offset += submit_len;
6168 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6169 start_sector, GFP_NOFS);
6172 bio->bi_private = dip;
6173 bio->bi_end_io = btrfs_end_dio_bio;
6175 map_length = orig_bio->bi_size;
6176 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6177 &map_length, NULL, 0);
6183 submit_len += bvec->bv_len;
6190 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6191 csums, async_submit);
6199 * before atomic variable goto zero, we must
6200 * make sure dip->errors is perceived to be set.
6202 smp_mb__before_atomic_dec();
6203 if (atomic_dec_and_test(&dip->pending_bios))
6204 bio_io_error(dip->orig_bio);
6206 /* bio_end_io() will handle error, so we needn't return it */
6210 static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6213 struct btrfs_root *root = BTRFS_I(inode)->root;
6214 struct btrfs_dio_private *dip;
6215 struct bio_vec *bvec = bio->bi_io_vec;
6217 int write = rw & REQ_WRITE;
6220 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6222 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6229 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6230 if (!write && !skip_sum) {
6231 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6239 dip->private = bio->bi_private;
6241 dip->logical_offset = file_offset;
6245 dip->bytes += bvec->bv_len;
6247 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6249 dip->disk_bytenr = (u64)bio->bi_sector << 9;
6250 bio->bi_private = dip;
6252 dip->orig_bio = bio;
6253 atomic_set(&dip->pending_bios, 0);
6256 bio->bi_end_io = btrfs_endio_direct_write;
6258 bio->bi_end_io = btrfs_endio_direct_read;
6260 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6265 * If this is a write, we need to clean up the reserved space and kill
6266 * the ordered extent.
6269 struct btrfs_ordered_extent *ordered;
6270 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
6271 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6272 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6273 btrfs_free_reserved_extent(root, ordered->start,
6275 btrfs_put_ordered_extent(ordered);
6276 btrfs_put_ordered_extent(ordered);
6278 bio_endio(bio, ret);
6281 static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6282 const struct iovec *iov, loff_t offset,
6283 unsigned long nr_segs)
6289 unsigned blocksize_mask = root->sectorsize - 1;
6290 ssize_t retval = -EINVAL;
6291 loff_t end = offset;
6293 if (offset & blocksize_mask)
6296 /* Check the memory alignment. Blocks cannot straddle pages */
6297 for (seg = 0; seg < nr_segs; seg++) {
6298 addr = (unsigned long)iov[seg].iov_base;
6299 size = iov[seg].iov_len;
6301 if ((addr & blocksize_mask) || (size & blocksize_mask))
6304 /* If this is a write we don't need to check anymore */
6309 * Check to make sure we don't have duplicate iov_base's in this
6310 * iovec, if so return EINVAL, otherwise we'll get csum errors
6311 * when reading back.
6313 for (i = seg + 1; i < nr_segs; i++) {
6314 if (iov[seg].iov_base == iov[i].iov_base)
6322 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6323 const struct iovec *iov, loff_t offset,
6324 unsigned long nr_segs)
6326 struct file *file = iocb->ki_filp;
6327 struct inode *inode = file->f_mapping->host;
6328 struct btrfs_ordered_extent *ordered;
6329 struct extent_state *cached_state = NULL;
6330 u64 lockstart, lockend;
6332 int writing = rw & WRITE;
6334 size_t count = iov_length(iov, nr_segs);
6336 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6342 lockend = offset + count - 1;
6345 ret = btrfs_delalloc_reserve_space(inode, count);
6351 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6352 0, &cached_state, GFP_NOFS);
6354 * We're concerned with the entire range that we're going to be
6355 * doing DIO to, so we need to make sure theres no ordered
6356 * extents in this range.
6358 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6359 lockend - lockstart + 1);
6362 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6363 &cached_state, GFP_NOFS);
6364 btrfs_start_ordered_extent(inode, ordered, 1);
6365 btrfs_put_ordered_extent(ordered);
6370 * we don't use btrfs_set_extent_delalloc because we don't want
6371 * the dirty or uptodate bits
6374 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6375 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6376 EXTENT_DELALLOC, 0, NULL, &cached_state,
6379 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6380 lockend, EXTENT_LOCKED | write_bits,
6381 1, 0, &cached_state, GFP_NOFS);
6386 free_extent_state(cached_state);
6387 cached_state = NULL;
6389 ret = __blockdev_direct_IO(rw, iocb, inode,
6390 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6391 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6392 btrfs_submit_direct, 0);
6394 if (ret < 0 && ret != -EIOCBQUEUED) {
6395 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6396 offset + iov_length(iov, nr_segs) - 1,
6397 EXTENT_LOCKED | write_bits, 1, 0,
6398 &cached_state, GFP_NOFS);
6399 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6401 * We're falling back to buffered, unlock the section we didn't
6404 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6405 offset + iov_length(iov, nr_segs) - 1,
6406 EXTENT_LOCKED | write_bits, 1, 0,
6407 &cached_state, GFP_NOFS);
6410 free_extent_state(cached_state);
6414 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6415 __u64 start, __u64 len)
6417 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
6420 int btrfs_readpage(struct file *file, struct page *page)
6422 struct extent_io_tree *tree;
6423 tree = &BTRFS_I(page->mapping->host)->io_tree;
6424 return extent_read_full_page(tree, page, btrfs_get_extent);
6427 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6429 struct extent_io_tree *tree;
6432 if (current->flags & PF_MEMALLOC) {
6433 redirty_page_for_writepage(wbc, page);
6437 tree = &BTRFS_I(page->mapping->host)->io_tree;
6438 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6441 int btrfs_writepages(struct address_space *mapping,
6442 struct writeback_control *wbc)
6444 struct extent_io_tree *tree;
6446 tree = &BTRFS_I(mapping->host)->io_tree;
6447 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6451 btrfs_readpages(struct file *file, struct address_space *mapping,
6452 struct list_head *pages, unsigned nr_pages)
6454 struct extent_io_tree *tree;
6455 tree = &BTRFS_I(mapping->host)->io_tree;
6456 return extent_readpages(tree, mapping, pages, nr_pages,
6459 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6461 struct extent_io_tree *tree;
6462 struct extent_map_tree *map;
6465 tree = &BTRFS_I(page->mapping->host)->io_tree;
6466 map = &BTRFS_I(page->mapping->host)->extent_tree;
6467 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
6469 ClearPagePrivate(page);
6470 set_page_private(page, 0);
6471 page_cache_release(page);
6476 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6478 if (PageWriteback(page) || PageDirty(page))
6480 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
6483 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6485 struct extent_io_tree *tree;
6486 struct btrfs_ordered_extent *ordered;
6487 struct extent_state *cached_state = NULL;
6488 u64 page_start = page_offset(page);
6489 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
6493 * we have the page locked, so new writeback can't start,
6494 * and the dirty bit won't be cleared while we are here.
6496 * Wait for IO on this page so that we can safely clear
6497 * the PagePrivate2 bit and do ordered accounting
6499 wait_on_page_writeback(page);
6501 tree = &BTRFS_I(page->mapping->host)->io_tree;
6503 btrfs_releasepage(page, GFP_NOFS);
6506 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6508 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6512 * IO on this page will never be started, so we need
6513 * to account for any ordered extents now
6515 clear_extent_bit(tree, page_start, page_end,
6516 EXTENT_DIRTY | EXTENT_DELALLOC |
6517 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
6518 &cached_state, GFP_NOFS);
6520 * whoever cleared the private bit is responsible
6521 * for the finish_ordered_io
6523 if (TestClearPagePrivate2(page)) {
6524 btrfs_finish_ordered_io(page->mapping->host,
6525 page_start, page_end);
6527 btrfs_put_ordered_extent(ordered);
6528 cached_state = NULL;
6529 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6532 clear_extent_bit(tree, page_start, page_end,
6533 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
6534 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
6535 __btrfs_releasepage(page, GFP_NOFS);
6537 ClearPageChecked(page);
6538 if (PagePrivate(page)) {
6539 ClearPagePrivate(page);
6540 set_page_private(page, 0);
6541 page_cache_release(page);
6546 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6547 * called from a page fault handler when a page is first dirtied. Hence we must
6548 * be careful to check for EOF conditions here. We set the page up correctly
6549 * for a written page which means we get ENOSPC checking when writing into
6550 * holes and correct delalloc and unwritten extent mapping on filesystems that
6551 * support these features.
6553 * We are not allowed to take the i_mutex here so we have to play games to
6554 * protect against truncate races as the page could now be beyond EOF. Because
6555 * vmtruncate() writes the inode size before removing pages, once we have the
6556 * page lock we can determine safely if the page is beyond EOF. If it is not
6557 * beyond EOF, then the page is guaranteed safe against truncation until we
6560 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
6562 struct page *page = vmf->page;
6563 struct inode *inode = fdentry(vma->vm_file)->d_inode;
6564 struct btrfs_root *root = BTRFS_I(inode)->root;
6565 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6566 struct btrfs_ordered_extent *ordered;
6567 struct extent_state *cached_state = NULL;
6569 unsigned long zero_start;
6575 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
6579 else /* -ENOSPC, -EIO, etc */
6580 ret = VM_FAULT_SIGBUS;
6584 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
6587 size = i_size_read(inode);
6588 page_start = page_offset(page);
6589 page_end = page_start + PAGE_CACHE_SIZE - 1;
6591 if ((page->mapping != inode->i_mapping) ||
6592 (page_start >= size)) {
6593 /* page got truncated out from underneath us */
6596 wait_on_page_writeback(page);
6598 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6600 set_page_extent_mapped(page);
6603 * we can't set the delalloc bits if there are pending ordered
6604 * extents. Drop our locks and wait for them to finish
6606 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6608 unlock_extent_cached(io_tree, page_start, page_end,
6609 &cached_state, GFP_NOFS);
6611 btrfs_start_ordered_extent(inode, ordered, 1);
6612 btrfs_put_ordered_extent(ordered);
6617 * XXX - page_mkwrite gets called every time the page is dirtied, even
6618 * if it was already dirty, so for space accounting reasons we need to
6619 * clear any delalloc bits for the range we are fixing to save. There
6620 * is probably a better way to do this, but for now keep consistent with
6621 * prepare_pages in the normal write path.
6623 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
6624 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
6625 0, 0, &cached_state, GFP_NOFS);
6627 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6630 unlock_extent_cached(io_tree, page_start, page_end,
6631 &cached_state, GFP_NOFS);
6632 ret = VM_FAULT_SIGBUS;
6637 /* page is wholly or partially inside EOF */
6638 if (page_start + PAGE_CACHE_SIZE > size)
6639 zero_start = size & ~PAGE_CACHE_MASK;
6641 zero_start = PAGE_CACHE_SIZE;
6643 if (zero_start != PAGE_CACHE_SIZE) {
6645 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6646 flush_dcache_page(page);
6649 ClearPageChecked(page);
6650 set_page_dirty(page);
6651 SetPageUptodate(page);
6653 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6654 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6656 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
6660 return VM_FAULT_LOCKED;
6662 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
6667 static int btrfs_truncate(struct inode *inode)
6669 struct btrfs_root *root = BTRFS_I(inode)->root;
6672 struct btrfs_trans_handle *trans;
6674 u64 mask = root->sectorsize - 1;
6676 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6680 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
6681 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
6683 trans = btrfs_start_transaction(root, 5);
6685 return PTR_ERR(trans);
6687 btrfs_set_trans_block_group(trans, inode);
6689 ret = btrfs_orphan_add(trans, inode);
6691 btrfs_end_transaction(trans, root);
6695 nr = trans->blocks_used;
6696 btrfs_end_transaction(trans, root);
6697 btrfs_btree_balance_dirty(root, nr);
6699 /* Now start a transaction for the truncate */
6700 trans = btrfs_start_transaction(root, 0);
6702 return PTR_ERR(trans);
6703 btrfs_set_trans_block_group(trans, inode);
6704 trans->block_rsv = root->orphan_block_rsv;
6707 * setattr is responsible for setting the ordered_data_close flag,
6708 * but that is only tested during the last file release. That
6709 * could happen well after the next commit, leaving a great big
6710 * window where new writes may get lost if someone chooses to write
6711 * to this file after truncating to zero
6713 * The inode doesn't have any dirty data here, and so if we commit
6714 * this is a noop. If someone immediately starts writing to the inode
6715 * it is very likely we'll catch some of their writes in this
6716 * transaction, and the commit will find this file on the ordered
6717 * data list with good things to send down.
6719 * This is a best effort solution, there is still a window where
6720 * using truncate to replace the contents of the file will
6721 * end up with a zero length file after a crash.
6723 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6724 btrfs_add_ordered_operation(trans, root, inode);
6728 trans = btrfs_start_transaction(root, 0);
6730 return PTR_ERR(trans);
6731 btrfs_set_trans_block_group(trans, inode);
6732 trans->block_rsv = root->orphan_block_rsv;
6735 ret = btrfs_block_rsv_check(trans, root,
6736 root->orphan_block_rsv, 0, 5);
6737 if (ret == -EAGAIN) {
6738 ret = btrfs_commit_transaction(trans, root);
6748 ret = btrfs_truncate_inode_items(trans, root, inode,
6750 BTRFS_EXTENT_DATA_KEY);
6751 if (ret != -EAGAIN) {
6756 ret = btrfs_update_inode(trans, root, inode);
6762 nr = trans->blocks_used;
6763 btrfs_end_transaction(trans, root);
6765 btrfs_btree_balance_dirty(root, nr);
6768 if (ret == 0 && inode->i_nlink > 0) {
6769 ret = btrfs_orphan_del(trans, inode);
6772 } else if (ret && inode->i_nlink > 0) {
6774 * Failed to do the truncate, remove us from the in memory
6777 ret = btrfs_orphan_del(NULL, inode);
6780 ret = btrfs_update_inode(trans, root, inode);
6784 nr = trans->blocks_used;
6785 ret = btrfs_end_transaction_throttle(trans, root);
6788 btrfs_btree_balance_dirty(root, nr);
6794 * create a new subvolume directory/inode (helper for the ioctl).
6796 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
6797 struct btrfs_root *new_root,
6798 u64 new_dirid, u64 alloc_hint)
6800 struct inode *inode;
6804 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
6805 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
6807 return PTR_ERR(inode);
6808 inode->i_op = &btrfs_dir_inode_operations;
6809 inode->i_fop = &btrfs_dir_file_operations;
6812 btrfs_i_size_write(inode, 0);
6814 err = btrfs_update_inode(trans, new_root, inode);
6821 /* helper function for file defrag and space balancing. This
6822 * forces readahead on a given range of bytes in an inode
6824 unsigned long btrfs_force_ra(struct address_space *mapping,
6825 struct file_ra_state *ra, struct file *file,
6826 pgoff_t offset, pgoff_t last_index)
6828 pgoff_t req_size = last_index - offset + 1;
6830 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6831 return offset + req_size;
6834 struct inode *btrfs_alloc_inode(struct super_block *sb)
6836 struct btrfs_inode *ei;
6837 struct inode *inode;
6839 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6844 ei->space_info = NULL;
6848 ei->last_sub_trans = 0;
6849 ei->logged_trans = 0;
6850 ei->delalloc_bytes = 0;
6851 ei->reserved_bytes = 0;
6852 ei->disk_i_size = 0;
6854 ei->index_cnt = (u64)-1;
6855 ei->last_unlink_trans = 0;
6857 atomic_set(&ei->outstanding_extents, 0);
6858 atomic_set(&ei->reserved_extents, 0);
6860 ei->ordered_data_close = 0;
6861 ei->orphan_meta_reserved = 0;
6862 ei->dummy_inode = 0;
6863 ei->force_compress = BTRFS_COMPRESS_NONE;
6865 ei->delayed_node = NULL;
6867 inode = &ei->vfs_inode;
6868 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6869 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6870 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6871 mutex_init(&ei->log_mutex);
6872 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6873 INIT_LIST_HEAD(&ei->i_orphan);
6874 INIT_LIST_HEAD(&ei->delalloc_inodes);
6875 INIT_LIST_HEAD(&ei->ordered_operations);
6876 RB_CLEAR_NODE(&ei->rb_node);
6881 static void btrfs_i_callback(struct rcu_head *head)
6883 struct inode *inode = container_of(head, struct inode, i_rcu);
6884 INIT_LIST_HEAD(&inode->i_dentry);
6885 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6888 void btrfs_destroy_inode(struct inode *inode)
6890 struct btrfs_ordered_extent *ordered;
6891 struct btrfs_root *root = BTRFS_I(inode)->root;
6893 WARN_ON(!list_empty(&inode->i_dentry));
6894 WARN_ON(inode->i_data.nrpages);
6895 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6896 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
6899 * This can happen where we create an inode, but somebody else also
6900 * created the same inode and we need to destroy the one we already
6907 * Make sure we're properly removed from the ordered operation
6911 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6912 spin_lock(&root->fs_info->ordered_extent_lock);
6913 list_del_init(&BTRFS_I(inode)->ordered_operations);
6914 spin_unlock(&root->fs_info->ordered_extent_lock);
6917 if (root == root->fs_info->tree_root) {
6918 struct btrfs_block_group_cache *block_group;
6920 block_group = btrfs_lookup_block_group(root->fs_info,
6921 BTRFS_I(inode)->block_group);
6922 if (block_group && block_group->inode == inode) {
6923 spin_lock(&block_group->lock);
6924 block_group->inode = NULL;
6925 spin_unlock(&block_group->lock);
6926 btrfs_put_block_group(block_group);
6927 } else if (block_group) {
6928 btrfs_put_block_group(block_group);
6932 spin_lock(&root->orphan_lock);
6933 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6934 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6935 (unsigned long long)btrfs_ino(inode));
6936 list_del_init(&BTRFS_I(inode)->i_orphan);
6938 spin_unlock(&root->orphan_lock);
6941 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6945 printk(KERN_ERR "btrfs found ordered "
6946 "extent %llu %llu on inode cleanup\n",
6947 (unsigned long long)ordered->file_offset,
6948 (unsigned long long)ordered->len);
6949 btrfs_remove_ordered_extent(inode, ordered);
6950 btrfs_put_ordered_extent(ordered);
6951 btrfs_put_ordered_extent(ordered);
6954 inode_tree_del(inode);
6955 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6957 btrfs_remove_delayed_node(inode);
6958 call_rcu(&inode->i_rcu, btrfs_i_callback);
6961 int btrfs_drop_inode(struct inode *inode)
6963 struct btrfs_root *root = BTRFS_I(inode)->root;
6965 if (btrfs_root_refs(&root->root_item) == 0 &&
6966 !is_free_space_inode(root, inode))
6969 return generic_drop_inode(inode);
6972 static void init_once(void *foo)
6974 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6976 inode_init_once(&ei->vfs_inode);
6979 void btrfs_destroy_cachep(void)
6981 if (btrfs_inode_cachep)
6982 kmem_cache_destroy(btrfs_inode_cachep);
6983 if (btrfs_trans_handle_cachep)
6984 kmem_cache_destroy(btrfs_trans_handle_cachep);
6985 if (btrfs_transaction_cachep)
6986 kmem_cache_destroy(btrfs_transaction_cachep);
6987 if (btrfs_path_cachep)
6988 kmem_cache_destroy(btrfs_path_cachep);
6989 if (btrfs_free_space_cachep)
6990 kmem_cache_destroy(btrfs_free_space_cachep);
6993 int btrfs_init_cachep(void)
6995 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6996 sizeof(struct btrfs_inode), 0,
6997 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
6998 if (!btrfs_inode_cachep)
7001 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7002 sizeof(struct btrfs_trans_handle), 0,
7003 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7004 if (!btrfs_trans_handle_cachep)
7007 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7008 sizeof(struct btrfs_transaction), 0,
7009 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7010 if (!btrfs_transaction_cachep)
7013 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7014 sizeof(struct btrfs_path), 0,
7015 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7016 if (!btrfs_path_cachep)
7019 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7020 sizeof(struct btrfs_free_space), 0,
7021 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7022 if (!btrfs_free_space_cachep)
7027 btrfs_destroy_cachep();
7031 static int btrfs_getattr(struct vfsmount *mnt,
7032 struct dentry *dentry, struct kstat *stat)
7034 struct inode *inode = dentry->d_inode;
7035 generic_fillattr(inode, stat);
7036 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
7037 stat->blksize = PAGE_CACHE_SIZE;
7038 stat->blocks = (inode_get_bytes(inode) +
7039 BTRFS_I(inode)->delalloc_bytes) >> 9;
7044 * If a file is moved, it will inherit the cow and compression flags of the new
7047 static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7049 struct btrfs_inode *b_dir = BTRFS_I(dir);
7050 struct btrfs_inode *b_inode = BTRFS_I(inode);
7052 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7053 b_inode->flags |= BTRFS_INODE_NODATACOW;
7055 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7057 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7058 b_inode->flags |= BTRFS_INODE_COMPRESS;
7060 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7063 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7064 struct inode *new_dir, struct dentry *new_dentry)
7066 struct btrfs_trans_handle *trans;
7067 struct btrfs_root *root = BTRFS_I(old_dir)->root;
7068 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
7069 struct inode *new_inode = new_dentry->d_inode;
7070 struct inode *old_inode = old_dentry->d_inode;
7071 struct timespec ctime = CURRENT_TIME;
7075 u64 old_ino = btrfs_ino(old_inode);
7077 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7080 /* we only allow rename subvolume link between subvolumes */
7081 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7084 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7085 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7088 if (S_ISDIR(old_inode->i_mode) && new_inode &&
7089 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7092 * we're using rename to replace one file with another.
7093 * and the replacement file is large. Start IO on it now so
7094 * we don't add too much work to the end of the transaction
7096 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
7097 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7098 filemap_flush(old_inode->i_mapping);
7100 /* close the racy window with snapshot create/destroy ioctl */
7101 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7102 down_read(&root->fs_info->subvol_sem);
7104 * We want to reserve the absolute worst case amount of items. So if
7105 * both inodes are subvols and we need to unlink them then that would
7106 * require 4 item modifications, but if they are both normal inodes it
7107 * would require 5 item modifications, so we'll assume their normal
7108 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7109 * should cover the worst case number of items we'll modify.
7111 trans = btrfs_start_transaction(root, 20);
7112 if (IS_ERR(trans)) {
7113 ret = PTR_ERR(trans);
7117 btrfs_set_trans_block_group(trans, new_dir);
7120 btrfs_record_root_in_trans(trans, dest);
7122 ret = btrfs_set_inode_index(new_dir, &index);
7126 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7127 /* force full log commit if subvolume involved. */
7128 root->fs_info->last_trans_log_full_commit = trans->transid;
7130 ret = btrfs_insert_inode_ref(trans, dest,
7131 new_dentry->d_name.name,
7132 new_dentry->d_name.len,
7134 btrfs_ino(new_dir), index);
7138 * this is an ugly little race, but the rename is required
7139 * to make sure that if we crash, the inode is either at the
7140 * old name or the new one. pinning the log transaction lets
7141 * us make sure we don't allow a log commit to come in after
7142 * we unlink the name but before we add the new name back in.
7144 btrfs_pin_log_trans(root);
7147 * make sure the inode gets flushed if it is replacing
7150 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7151 btrfs_add_ordered_operation(trans, root, old_inode);
7153 old_dir->i_ctime = old_dir->i_mtime = ctime;
7154 new_dir->i_ctime = new_dir->i_mtime = ctime;
7155 old_inode->i_ctime = ctime;
7157 if (old_dentry->d_parent != new_dentry->d_parent)
7158 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7160 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7161 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7162 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7163 old_dentry->d_name.name,
7164 old_dentry->d_name.len);
7166 ret = __btrfs_unlink_inode(trans, root, old_dir,
7167 old_dentry->d_inode,
7168 old_dentry->d_name.name,
7169 old_dentry->d_name.len);
7171 ret = btrfs_update_inode(trans, root, old_inode);
7176 new_inode->i_ctime = CURRENT_TIME;
7177 if (unlikely(btrfs_ino(new_inode) ==
7178 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7179 root_objectid = BTRFS_I(new_inode)->location.objectid;
7180 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7182 new_dentry->d_name.name,
7183 new_dentry->d_name.len);
7184 BUG_ON(new_inode->i_nlink == 0);
7186 ret = btrfs_unlink_inode(trans, dest, new_dir,
7187 new_dentry->d_inode,
7188 new_dentry->d_name.name,
7189 new_dentry->d_name.len);
7192 if (new_inode->i_nlink == 0) {
7193 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7198 fixup_inode_flags(new_dir, old_inode);
7200 ret = btrfs_add_link(trans, new_dir, old_inode,
7201 new_dentry->d_name.name,
7202 new_dentry->d_name.len, 0, index);
7205 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7206 struct dentry *parent = dget_parent(new_dentry);
7207 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7209 btrfs_end_log_trans(root);
7212 btrfs_end_transaction_throttle(trans, root);
7214 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7215 up_read(&root->fs_info->subvol_sem);
7221 * some fairly slow code that needs optimization. This walks the list
7222 * of all the inodes with pending delalloc and forces them to disk.
7224 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7226 struct list_head *head = &root->fs_info->delalloc_inodes;
7227 struct btrfs_inode *binode;
7228 struct inode *inode;
7230 if (root->fs_info->sb->s_flags & MS_RDONLY)
7233 spin_lock(&root->fs_info->delalloc_lock);
7234 while (!list_empty(head)) {
7235 binode = list_entry(head->next, struct btrfs_inode,
7237 inode = igrab(&binode->vfs_inode);
7239 list_del_init(&binode->delalloc_inodes);
7240 spin_unlock(&root->fs_info->delalloc_lock);
7242 filemap_flush(inode->i_mapping);
7244 btrfs_add_delayed_iput(inode);
7249 spin_lock(&root->fs_info->delalloc_lock);
7251 spin_unlock(&root->fs_info->delalloc_lock);
7253 /* the filemap_flush will queue IO into the worker threads, but
7254 * we have to make sure the IO is actually started and that
7255 * ordered extents get created before we return
7257 atomic_inc(&root->fs_info->async_submit_draining);
7258 while (atomic_read(&root->fs_info->nr_async_submits) ||
7259 atomic_read(&root->fs_info->async_delalloc_pages)) {
7260 wait_event(root->fs_info->async_submit_wait,
7261 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7262 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7264 atomic_dec(&root->fs_info->async_submit_draining);
7268 int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7271 struct btrfs_inode *binode;
7272 struct inode *inode = NULL;
7274 spin_lock(&root->fs_info->delalloc_lock);
7275 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7276 binode = list_entry(root->fs_info->delalloc_inodes.next,
7277 struct btrfs_inode, delalloc_inodes);
7278 inode = igrab(&binode->vfs_inode);
7280 list_move_tail(&binode->delalloc_inodes,
7281 &root->fs_info->delalloc_inodes);
7285 list_del_init(&binode->delalloc_inodes);
7286 cond_resched_lock(&root->fs_info->delalloc_lock);
7288 spin_unlock(&root->fs_info->delalloc_lock);
7292 filemap_write_and_wait(inode->i_mapping);
7294 * We have to do this because compression doesn't
7295 * actually set PG_writeback until it submits the pages
7296 * for IO, which happens in an async thread, so we could
7297 * race and not actually wait for any writeback pages
7298 * because they've not been submitted yet. Technically
7299 * this could still be the case for the ordered stuff
7300 * since the async thread may not have started to do its
7301 * work yet. If this becomes the case then we need to
7302 * figure out a way to make sure that in writepage we
7303 * wait for any async pages to be submitted before
7304 * returning so that fdatawait does what its supposed to
7307 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7309 filemap_flush(inode->i_mapping);
7312 btrfs_add_delayed_iput(inode);
7320 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7321 const char *symname)
7323 struct btrfs_trans_handle *trans;
7324 struct btrfs_root *root = BTRFS_I(dir)->root;
7325 struct btrfs_path *path;
7326 struct btrfs_key key;
7327 struct inode *inode = NULL;
7335 struct btrfs_file_extent_item *ei;
7336 struct extent_buffer *leaf;
7337 unsigned long nr = 0;
7339 name_len = strlen(symname) + 1;
7340 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7341 return -ENAMETOOLONG;
7344 * 2 items for inode item and ref
7345 * 2 items for dir items
7346 * 1 item for xattr if selinux is on
7348 trans = btrfs_start_transaction(root, 5);
7350 return PTR_ERR(trans);
7352 btrfs_set_trans_block_group(trans, dir);
7354 err = btrfs_find_free_ino(root, &objectid);
7358 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7359 dentry->d_name.len, btrfs_ino(dir), objectid,
7360 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7362 if (IS_ERR(inode)) {
7363 err = PTR_ERR(inode);
7367 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
7373 btrfs_set_trans_block_group(trans, inode);
7374 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
7378 inode->i_mapping->a_ops = &btrfs_aops;
7379 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7380 inode->i_fop = &btrfs_file_operations;
7381 inode->i_op = &btrfs_file_inode_operations;
7382 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
7384 btrfs_update_inode_block_group(trans, inode);
7385 btrfs_update_inode_block_group(trans, dir);
7389 path = btrfs_alloc_path();
7391 key.objectid = btrfs_ino(inode);
7393 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7394 datasize = btrfs_file_extent_calc_inline_size(name_len);
7395 err = btrfs_insert_empty_item(trans, root, path, &key,
7401 leaf = path->nodes[0];
7402 ei = btrfs_item_ptr(leaf, path->slots[0],
7403 struct btrfs_file_extent_item);
7404 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7405 btrfs_set_file_extent_type(leaf, ei,
7406 BTRFS_FILE_EXTENT_INLINE);
7407 btrfs_set_file_extent_encryption(leaf, ei, 0);
7408 btrfs_set_file_extent_compression(leaf, ei, 0);
7409 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7410 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7412 ptr = btrfs_file_extent_inline_start(ei);
7413 write_extent_buffer(leaf, symname, ptr, name_len);
7414 btrfs_mark_buffer_dirty(leaf);
7415 btrfs_free_path(path);
7417 inode->i_op = &btrfs_symlink_inode_operations;
7418 inode->i_mapping->a_ops = &btrfs_symlink_aops;
7419 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7420 inode_set_bytes(inode, name_len);
7421 btrfs_i_size_write(inode, name_len - 1);
7422 err = btrfs_update_inode(trans, root, inode);
7427 nr = trans->blocks_used;
7428 btrfs_end_transaction_throttle(trans, root);
7430 inode_dec_link_count(inode);
7433 btrfs_btree_balance_dirty(root, nr);
7437 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7438 u64 start, u64 num_bytes, u64 min_size,
7439 loff_t actual_len, u64 *alloc_hint,
7440 struct btrfs_trans_handle *trans)
7442 struct btrfs_root *root = BTRFS_I(inode)->root;
7443 struct btrfs_key ins;
7444 u64 cur_offset = start;
7447 bool own_trans = true;
7451 while (num_bytes > 0) {
7453 trans = btrfs_start_transaction(root, 3);
7454 if (IS_ERR(trans)) {
7455 ret = PTR_ERR(trans);
7460 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7461 0, *alloc_hint, (u64)-1, &ins, 1);
7464 btrfs_end_transaction(trans, root);
7468 ret = insert_reserved_file_extent(trans, inode,
7469 cur_offset, ins.objectid,
7470 ins.offset, ins.offset,
7471 ins.offset, 0, 0, 0,
7472 BTRFS_FILE_EXTENT_PREALLOC);
7474 btrfs_drop_extent_cache(inode, cur_offset,
7475 cur_offset + ins.offset -1, 0);
7477 num_bytes -= ins.offset;
7478 cur_offset += ins.offset;
7479 *alloc_hint = ins.objectid + ins.offset;
7481 inode->i_ctime = CURRENT_TIME;
7482 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
7483 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
7484 (actual_len > inode->i_size) &&
7485 (cur_offset > inode->i_size)) {
7486 if (cur_offset > actual_len)
7487 i_size = actual_len;
7489 i_size = cur_offset;
7490 i_size_write(inode, i_size);
7491 btrfs_ordered_update_i_size(inode, i_size, NULL);
7494 ret = btrfs_update_inode(trans, root, inode);
7498 btrfs_end_transaction(trans, root);
7503 int btrfs_prealloc_file_range(struct inode *inode, int mode,
7504 u64 start, u64 num_bytes, u64 min_size,
7505 loff_t actual_len, u64 *alloc_hint)
7507 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7508 min_size, actual_len, alloc_hint,
7512 int btrfs_prealloc_file_range_trans(struct inode *inode,
7513 struct btrfs_trans_handle *trans, int mode,
7514 u64 start, u64 num_bytes, u64 min_size,
7515 loff_t actual_len, u64 *alloc_hint)
7517 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7518 min_size, actual_len, alloc_hint, trans);
7521 static int btrfs_set_page_dirty(struct page *page)
7523 return __set_page_dirty_nobuffers(page);
7526 static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
7528 struct btrfs_root *root = BTRFS_I(inode)->root;
7530 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7532 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
7534 return generic_permission(inode, mask, flags, btrfs_check_acl);
7537 static const struct inode_operations btrfs_dir_inode_operations = {
7538 .getattr = btrfs_getattr,
7539 .lookup = btrfs_lookup,
7540 .create = btrfs_create,
7541 .unlink = btrfs_unlink,
7543 .mkdir = btrfs_mkdir,
7544 .rmdir = btrfs_rmdir,
7545 .rename = btrfs_rename,
7546 .symlink = btrfs_symlink,
7547 .setattr = btrfs_setattr,
7548 .mknod = btrfs_mknod,
7549 .setxattr = btrfs_setxattr,
7550 .getxattr = btrfs_getxattr,
7551 .listxattr = btrfs_listxattr,
7552 .removexattr = btrfs_removexattr,
7553 .permission = btrfs_permission,
7555 static const struct inode_operations btrfs_dir_ro_inode_operations = {
7556 .lookup = btrfs_lookup,
7557 .permission = btrfs_permission,
7560 static const struct file_operations btrfs_dir_file_operations = {
7561 .llseek = generic_file_llseek,
7562 .read = generic_read_dir,
7563 .readdir = btrfs_real_readdir,
7564 .unlocked_ioctl = btrfs_ioctl,
7565 #ifdef CONFIG_COMPAT
7566 .compat_ioctl = btrfs_ioctl,
7568 .release = btrfs_release_file,
7569 .fsync = btrfs_sync_file,
7572 static struct extent_io_ops btrfs_extent_io_ops = {
7573 .fill_delalloc = run_delalloc_range,
7574 .submit_bio_hook = btrfs_submit_bio_hook,
7575 .merge_bio_hook = btrfs_merge_bio_hook,
7576 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
7577 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
7578 .writepage_start_hook = btrfs_writepage_start_hook,
7579 .readpage_io_failed_hook = btrfs_io_failed_hook,
7580 .set_bit_hook = btrfs_set_bit_hook,
7581 .clear_bit_hook = btrfs_clear_bit_hook,
7582 .merge_extent_hook = btrfs_merge_extent_hook,
7583 .split_extent_hook = btrfs_split_extent_hook,
7587 * btrfs doesn't support the bmap operation because swapfiles
7588 * use bmap to make a mapping of extents in the file. They assume
7589 * these extents won't change over the life of the file and they
7590 * use the bmap result to do IO directly to the drive.
7592 * the btrfs bmap call would return logical addresses that aren't
7593 * suitable for IO and they also will change frequently as COW
7594 * operations happen. So, swapfile + btrfs == corruption.
7596 * For now we're avoiding this by dropping bmap.
7598 static const struct address_space_operations btrfs_aops = {
7599 .readpage = btrfs_readpage,
7600 .writepage = btrfs_writepage,
7601 .writepages = btrfs_writepages,
7602 .readpages = btrfs_readpages,
7603 .direct_IO = btrfs_direct_IO,
7604 .invalidatepage = btrfs_invalidatepage,
7605 .releasepage = btrfs_releasepage,
7606 .set_page_dirty = btrfs_set_page_dirty,
7607 .error_remove_page = generic_error_remove_page,
7610 static const struct address_space_operations btrfs_symlink_aops = {
7611 .readpage = btrfs_readpage,
7612 .writepage = btrfs_writepage,
7613 .invalidatepage = btrfs_invalidatepage,
7614 .releasepage = btrfs_releasepage,
7617 static const struct inode_operations btrfs_file_inode_operations = {
7618 .getattr = btrfs_getattr,
7619 .setattr = btrfs_setattr,
7620 .setxattr = btrfs_setxattr,
7621 .getxattr = btrfs_getxattr,
7622 .listxattr = btrfs_listxattr,
7623 .removexattr = btrfs_removexattr,
7624 .permission = btrfs_permission,
7625 .fiemap = btrfs_fiemap,
7627 static const struct inode_operations btrfs_special_inode_operations = {
7628 .getattr = btrfs_getattr,
7629 .setattr = btrfs_setattr,
7630 .permission = btrfs_permission,
7631 .setxattr = btrfs_setxattr,
7632 .getxattr = btrfs_getxattr,
7633 .listxattr = btrfs_listxattr,
7634 .removexattr = btrfs_removexattr,
7636 static const struct inode_operations btrfs_symlink_inode_operations = {
7637 .readlink = generic_readlink,
7638 .follow_link = page_follow_link_light,
7639 .put_link = page_put_link,
7640 .getattr = btrfs_getattr,
7641 .permission = btrfs_permission,
7642 .setxattr = btrfs_setxattr,
7643 .getxattr = btrfs_getxattr,
7644 .listxattr = btrfs_listxattr,
7645 .removexattr = btrfs_removexattr,
7648 const struct dentry_operations btrfs_dentry_operations = {
7649 .d_delete = btrfs_dentry_delete,