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.
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include <linux/time.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/backing-dev.h>
26 #include <linux/mpage.h>
27 #include <linux/aio.h>
28 #include <linux/falloc.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/slab.h>
34 #include <linux/btrfs.h>
37 #include "transaction.h"
38 #include "btrfs_inode.h"
39 #include "print-tree.h"
45 static struct kmem_cache *btrfs_inode_defrag_cachep;
47 * when auto defrag is enabled we
48 * queue up these defrag structs to remember which
49 * inodes need defragging passes
52 struct rb_node rb_node;
56 * transid where the defrag was added, we search for
57 * extents newer than this
64 /* last offset we were able to defrag */
67 /* if we've wrapped around back to zero once already */
71 static int __compare_inode_defrag(struct inode_defrag *defrag1,
72 struct inode_defrag *defrag2)
74 if (defrag1->root > defrag2->root)
76 else if (defrag1->root < defrag2->root)
78 else if (defrag1->ino > defrag2->ino)
80 else if (defrag1->ino < defrag2->ino)
86 /* pop a record for an inode into the defrag tree. The lock
87 * must be held already
89 * If you're inserting a record for an older transid than an
90 * existing record, the transid already in the tree is lowered
92 * If an existing record is found the defrag item you
95 static int __btrfs_add_inode_defrag(struct inode *inode,
96 struct inode_defrag *defrag)
98 struct btrfs_root *root = BTRFS_I(inode)->root;
99 struct inode_defrag *entry;
101 struct rb_node *parent = NULL;
104 p = &root->fs_info->defrag_inodes.rb_node;
107 entry = rb_entry(parent, struct inode_defrag, rb_node);
109 ret = __compare_inode_defrag(defrag, entry);
111 p = &parent->rb_left;
113 p = &parent->rb_right;
115 /* if we're reinserting an entry for
116 * an old defrag run, make sure to
117 * lower the transid of our existing record
119 if (defrag->transid < entry->transid)
120 entry->transid = defrag->transid;
121 if (defrag->last_offset > entry->last_offset)
122 entry->last_offset = defrag->last_offset;
126 set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
127 rb_link_node(&defrag->rb_node, parent, p);
128 rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
132 static inline int __need_auto_defrag(struct btrfs_root *root)
134 if (!btrfs_test_opt(root, AUTO_DEFRAG))
137 if (btrfs_fs_closing(root->fs_info))
144 * insert a defrag record for this inode if auto defrag is
147 int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root = BTRFS_I(inode)->root;
151 struct inode_defrag *defrag;
155 if (!__need_auto_defrag(root))
158 if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
162 transid = trans->transid;
164 transid = BTRFS_I(inode)->root->last_trans;
166 defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
170 defrag->ino = btrfs_ino(inode);
171 defrag->transid = transid;
172 defrag->root = root->root_key.objectid;
174 spin_lock(&root->fs_info->defrag_inodes_lock);
175 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
177 * If we set IN_DEFRAG flag and evict the inode from memory,
178 * and then re-read this inode, this new inode doesn't have
179 * IN_DEFRAG flag. At the case, we may find the existed defrag.
181 ret = __btrfs_add_inode_defrag(inode, defrag);
183 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
185 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
187 spin_unlock(&root->fs_info->defrag_inodes_lock);
192 * Requeue the defrag object. If there is a defrag object that points to
193 * the same inode in the tree, we will merge them together (by
194 * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
196 static void btrfs_requeue_inode_defrag(struct inode *inode,
197 struct inode_defrag *defrag)
199 struct btrfs_root *root = BTRFS_I(inode)->root;
202 if (!__need_auto_defrag(root))
206 * Here we don't check the IN_DEFRAG flag, because we need merge
209 spin_lock(&root->fs_info->defrag_inodes_lock);
210 ret = __btrfs_add_inode_defrag(inode, defrag);
211 spin_unlock(&root->fs_info->defrag_inodes_lock);
216 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
220 * pick the defragable inode that we want, if it doesn't exist, we will get
223 static struct inode_defrag *
224 btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
226 struct inode_defrag *entry = NULL;
227 struct inode_defrag tmp;
229 struct rb_node *parent = NULL;
235 spin_lock(&fs_info->defrag_inodes_lock);
236 p = fs_info->defrag_inodes.rb_node;
239 entry = rb_entry(parent, struct inode_defrag, rb_node);
241 ret = __compare_inode_defrag(&tmp, entry);
245 p = parent->rb_right;
250 if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
251 parent = rb_next(parent);
253 entry = rb_entry(parent, struct inode_defrag, rb_node);
259 rb_erase(parent, &fs_info->defrag_inodes);
260 spin_unlock(&fs_info->defrag_inodes_lock);
264 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
266 struct inode_defrag *defrag;
267 struct rb_node *node;
269 spin_lock(&fs_info->defrag_inodes_lock);
270 node = rb_first(&fs_info->defrag_inodes);
272 rb_erase(node, &fs_info->defrag_inodes);
273 defrag = rb_entry(node, struct inode_defrag, rb_node);
274 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
276 if (need_resched()) {
277 spin_unlock(&fs_info->defrag_inodes_lock);
279 spin_lock(&fs_info->defrag_inodes_lock);
282 node = rb_first(&fs_info->defrag_inodes);
284 spin_unlock(&fs_info->defrag_inodes_lock);
287 #define BTRFS_DEFRAG_BATCH 1024
289 static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
290 struct inode_defrag *defrag)
292 struct btrfs_root *inode_root;
294 struct btrfs_key key;
295 struct btrfs_ioctl_defrag_range_args range;
301 key.objectid = defrag->root;
302 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
303 key.offset = (u64)-1;
305 index = srcu_read_lock(&fs_info->subvol_srcu);
307 inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
308 if (IS_ERR(inode_root)) {
309 ret = PTR_ERR(inode_root);
313 key.objectid = defrag->ino;
314 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
316 inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
318 ret = PTR_ERR(inode);
321 srcu_read_unlock(&fs_info->subvol_srcu, index);
323 /* do a chunk of defrag */
324 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
325 memset(&range, 0, sizeof(range));
327 range.start = defrag->last_offset;
329 sb_start_write(fs_info->sb);
330 num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
332 sb_end_write(fs_info->sb);
334 * if we filled the whole defrag batch, there
335 * must be more work to do. Queue this defrag
338 if (num_defrag == BTRFS_DEFRAG_BATCH) {
339 defrag->last_offset = range.start;
340 btrfs_requeue_inode_defrag(inode, defrag);
341 } else if (defrag->last_offset && !defrag->cycled) {
343 * we didn't fill our defrag batch, but
344 * we didn't start at zero. Make sure we loop
345 * around to the start of the file.
347 defrag->last_offset = 0;
349 btrfs_requeue_inode_defrag(inode, defrag);
351 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
357 srcu_read_unlock(&fs_info->subvol_srcu, index);
358 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
363 * run through the list of inodes in the FS that need
366 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
368 struct inode_defrag *defrag;
370 u64 root_objectid = 0;
372 atomic_inc(&fs_info->defrag_running);
374 /* Pause the auto defragger. */
375 if (test_bit(BTRFS_FS_STATE_REMOUNTING,
379 if (!__need_auto_defrag(fs_info->tree_root))
382 /* find an inode to defrag */
383 defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
386 if (root_objectid || first_ino) {
395 first_ino = defrag->ino + 1;
396 root_objectid = defrag->root;
398 __btrfs_run_defrag_inode(fs_info, defrag);
400 atomic_dec(&fs_info->defrag_running);
403 * during unmount, we use the transaction_wait queue to
404 * wait for the defragger to stop
406 wake_up(&fs_info->transaction_wait);
410 /* simple helper to fault in pages and copy. This should go away
411 * and be replaced with calls into generic code.
413 static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
415 struct page **prepared_pages,
419 size_t total_copied = 0;
421 int offset = pos & (PAGE_CACHE_SIZE - 1);
423 while (write_bytes > 0) {
424 size_t count = min_t(size_t,
425 PAGE_CACHE_SIZE - offset, write_bytes);
426 struct page *page = prepared_pages[pg];
428 * Copy data from userspace to the current page
430 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
432 /* Flush processor's dcache for this page */
433 flush_dcache_page(page);
436 * if we get a partial write, we can end up with
437 * partially up to date pages. These add
438 * a lot of complexity, so make sure they don't
439 * happen by forcing this copy to be retried.
441 * The rest of the btrfs_file_write code will fall
442 * back to page at a time copies after we return 0.
444 if (!PageUptodate(page) && copied < count)
447 iov_iter_advance(i, copied);
448 write_bytes -= copied;
449 total_copied += copied;
451 /* Return to btrfs_file_aio_write to fault page */
452 if (unlikely(copied == 0))
455 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
466 * unlocks pages after btrfs_file_write is done with them
468 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
471 for (i = 0; i < num_pages; i++) {
472 /* page checked is some magic around finding pages that
473 * have been modified without going through btrfs_set_page_dirty
476 ClearPageChecked(pages[i]);
477 unlock_page(pages[i]);
478 mark_page_accessed(pages[i]);
479 page_cache_release(pages[i]);
484 * after copy_from_user, pages need to be dirtied and we need to make
485 * sure holes are created between the current EOF and the start of
486 * any next extents (if required).
488 * this also makes the decision about creating an inline extent vs
489 * doing real data extents, marking pages dirty and delalloc as required.
491 int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
492 struct page **pages, size_t num_pages,
493 loff_t pos, size_t write_bytes,
494 struct extent_state **cached)
500 u64 end_of_last_block;
501 u64 end_pos = pos + write_bytes;
502 loff_t isize = i_size_read(inode);
504 start_pos = pos & ~((u64)root->sectorsize - 1);
505 num_bytes = ALIGN(write_bytes + pos - start_pos, root->sectorsize);
507 end_of_last_block = start_pos + num_bytes - 1;
508 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
513 for (i = 0; i < num_pages; i++) {
514 struct page *p = pages[i];
521 * we've only changed i_size in ram, and we haven't updated
522 * the disk i_size. There is no need to log the inode
526 i_size_write(inode, end_pos);
531 * this drops all the extents in the cache that intersect the range
532 * [start, end]. Existing extents are split as required.
534 void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
537 struct extent_map *em;
538 struct extent_map *split = NULL;
539 struct extent_map *split2 = NULL;
540 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
541 u64 len = end - start + 1;
549 WARN_ON(end < start);
550 if (end == (u64)-1) {
559 split = alloc_extent_map();
561 split2 = alloc_extent_map();
562 if (!split || !split2)
565 write_lock(&em_tree->lock);
566 em = lookup_extent_mapping(em_tree, start, len);
568 write_unlock(&em_tree->lock);
572 gen = em->generation;
573 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
574 if (testend && em->start + em->len >= start + len) {
576 write_unlock(&em_tree->lock);
579 start = em->start + em->len;
581 len = start + len - (em->start + em->len);
583 write_unlock(&em_tree->lock);
586 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
587 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
588 clear_bit(EXTENT_FLAG_LOGGING, &flags);
589 modified = !list_empty(&em->list);
593 if (em->start < start) {
594 split->start = em->start;
595 split->len = start - em->start;
597 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
598 split->orig_start = em->orig_start;
599 split->block_start = em->block_start;
602 split->block_len = em->block_len;
604 split->block_len = split->len;
605 split->orig_block_len = max(split->block_len,
607 split->ram_bytes = em->ram_bytes;
609 split->orig_start = split->start;
610 split->block_len = 0;
611 split->block_start = em->block_start;
612 split->orig_block_len = 0;
613 split->ram_bytes = split->len;
616 split->generation = gen;
617 split->bdev = em->bdev;
618 split->flags = flags;
619 split->compress_type = em->compress_type;
620 replace_extent_mapping(em_tree, em, split, modified);
621 free_extent_map(split);
625 if (testend && em->start + em->len > start + len) {
626 u64 diff = start + len - em->start;
628 split->start = start + len;
629 split->len = em->start + em->len - (start + len);
630 split->bdev = em->bdev;
631 split->flags = flags;
632 split->compress_type = em->compress_type;
633 split->generation = gen;
635 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
636 split->orig_block_len = max(em->block_len,
639 split->ram_bytes = em->ram_bytes;
641 split->block_len = em->block_len;
642 split->block_start = em->block_start;
643 split->orig_start = em->orig_start;
645 split->block_len = split->len;
646 split->block_start = em->block_start
648 split->orig_start = em->orig_start;
651 split->ram_bytes = split->len;
652 split->orig_start = split->start;
653 split->block_len = 0;
654 split->block_start = em->block_start;
655 split->orig_block_len = 0;
658 if (extent_map_in_tree(em)) {
659 replace_extent_mapping(em_tree, em, split,
662 ret = add_extent_mapping(em_tree, split,
664 ASSERT(ret == 0); /* Logic error */
666 free_extent_map(split);
670 if (extent_map_in_tree(em))
671 remove_extent_mapping(em_tree, em);
672 write_unlock(&em_tree->lock);
676 /* once for the tree*/
680 free_extent_map(split);
682 free_extent_map(split2);
686 * this is very complex, but the basic idea is to drop all extents
687 * in the range start - end. hint_block is filled in with a block number
688 * that would be a good hint to the block allocator for this file.
690 * If an extent intersects the range but is not entirely inside the range
691 * it is either truncated or split. Anything entirely inside the range
692 * is deleted from the tree.
694 int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
695 struct btrfs_root *root, struct inode *inode,
696 struct btrfs_path *path, u64 start, u64 end,
697 u64 *drop_end, int drop_cache,
699 u32 extent_item_size,
702 struct extent_buffer *leaf;
703 struct btrfs_file_extent_item *fi;
704 struct btrfs_key key;
705 struct btrfs_key new_key;
706 u64 ino = btrfs_ino(inode);
707 u64 search_start = start;
710 u64 extent_offset = 0;
717 int modify_tree = -1;
720 int leafs_visited = 0;
723 btrfs_drop_extent_cache(inode, start, end - 1, 0);
725 if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)
728 update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
729 root == root->fs_info->tree_root);
732 ret = btrfs_lookup_file_extent(trans, root, path, ino,
733 search_start, modify_tree);
736 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
737 leaf = path->nodes[0];
738 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
739 if (key.objectid == ino &&
740 key.type == BTRFS_EXTENT_DATA_KEY)
746 leaf = path->nodes[0];
747 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
749 ret = btrfs_next_leaf(root, path);
757 leaf = path->nodes[0];
761 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
762 if (key.objectid > ino ||
763 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
766 fi = btrfs_item_ptr(leaf, path->slots[0],
767 struct btrfs_file_extent_item);
768 extent_type = btrfs_file_extent_type(leaf, fi);
770 if (extent_type == BTRFS_FILE_EXTENT_REG ||
771 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
772 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
773 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
774 extent_offset = btrfs_file_extent_offset(leaf, fi);
775 extent_end = key.offset +
776 btrfs_file_extent_num_bytes(leaf, fi);
777 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
778 extent_end = key.offset +
779 btrfs_file_extent_inline_len(leaf,
783 extent_end = search_start;
787 * Don't skip extent items representing 0 byte lengths. They
788 * used to be created (bug) if while punching holes we hit
789 * -ENOSPC condition. So if we find one here, just ensure we
790 * delete it, otherwise we would insert a new file extent item
791 * with the same key (offset) as that 0 bytes length file
792 * extent item in the call to setup_items_for_insert() later
795 if (extent_end == key.offset && extent_end >= search_start)
796 goto delete_extent_item;
798 if (extent_end <= search_start) {
804 search_start = max(key.offset, start);
805 if (recow || !modify_tree) {
807 btrfs_release_path(path);
812 * | - range to drop - |
813 * | -------- extent -------- |
815 if (start > key.offset && end < extent_end) {
817 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
822 memcpy(&new_key, &key, sizeof(new_key));
823 new_key.offset = start;
824 ret = btrfs_duplicate_item(trans, root, path,
826 if (ret == -EAGAIN) {
827 btrfs_release_path(path);
833 leaf = path->nodes[0];
834 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
835 struct btrfs_file_extent_item);
836 btrfs_set_file_extent_num_bytes(leaf, fi,
839 fi = btrfs_item_ptr(leaf, path->slots[0],
840 struct btrfs_file_extent_item);
842 extent_offset += start - key.offset;
843 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
844 btrfs_set_file_extent_num_bytes(leaf, fi,
846 btrfs_mark_buffer_dirty(leaf);
848 if (update_refs && disk_bytenr > 0) {
849 ret = btrfs_inc_extent_ref(trans, root,
850 disk_bytenr, num_bytes, 0,
851 root->root_key.objectid,
853 start - extent_offset, 1);
854 BUG_ON(ret); /* -ENOMEM */
859 * | ---- range to drop ----- |
860 * | -------- extent -------- |
862 if (start <= key.offset && end < extent_end) {
863 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
868 memcpy(&new_key, &key, sizeof(new_key));
869 new_key.offset = end;
870 btrfs_set_item_key_safe(root, path, &new_key);
872 extent_offset += end - key.offset;
873 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
874 btrfs_set_file_extent_num_bytes(leaf, fi,
876 btrfs_mark_buffer_dirty(leaf);
877 if (update_refs && disk_bytenr > 0)
878 inode_sub_bytes(inode, end - key.offset);
882 search_start = extent_end;
884 * | ---- range to drop ----- |
885 * | -------- extent -------- |
887 if (start > key.offset && end >= extent_end) {
889 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
894 btrfs_set_file_extent_num_bytes(leaf, fi,
896 btrfs_mark_buffer_dirty(leaf);
897 if (update_refs && disk_bytenr > 0)
898 inode_sub_bytes(inode, extent_end - start);
899 if (end == extent_end)
907 * | ---- range to drop ----- |
908 * | ------ extent ------ |
910 if (start <= key.offset && end >= extent_end) {
913 del_slot = path->slots[0];
916 BUG_ON(del_slot + del_nr != path->slots[0]);
921 extent_type == BTRFS_FILE_EXTENT_INLINE) {
922 inode_sub_bytes(inode,
923 extent_end - key.offset);
924 extent_end = ALIGN(extent_end,
926 } else if (update_refs && disk_bytenr > 0) {
927 ret = btrfs_free_extent(trans, root,
928 disk_bytenr, num_bytes, 0,
929 root->root_key.objectid,
930 key.objectid, key.offset -
932 BUG_ON(ret); /* -ENOMEM */
933 inode_sub_bytes(inode,
934 extent_end - key.offset);
937 if (end == extent_end)
940 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
945 ret = btrfs_del_items(trans, root, path, del_slot,
948 btrfs_abort_transaction(trans, root, ret);
955 btrfs_release_path(path);
962 if (!ret && del_nr > 0) {
964 * Set path->slots[0] to first slot, so that after the delete
965 * if items are move off from our leaf to its immediate left or
966 * right neighbor leafs, we end up with a correct and adjusted
967 * path->slots[0] for our insertion (if replace_extent != 0).
969 path->slots[0] = del_slot;
970 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
972 btrfs_abort_transaction(trans, root, ret);
975 leaf = path->nodes[0];
977 * If btrfs_del_items() was called, it might have deleted a leaf, in
978 * which case it unlocked our path, so check path->locks[0] matches a
981 if (!ret && replace_extent && leafs_visited == 1 &&
982 (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
983 path->locks[0] == BTRFS_WRITE_LOCK) &&
984 btrfs_leaf_free_space(root, leaf) >=
985 sizeof(struct btrfs_item) + extent_item_size) {
988 key.type = BTRFS_EXTENT_DATA_KEY;
990 if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
991 struct btrfs_key slot_key;
993 btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
994 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
997 setup_items_for_insert(root, path, &key,
1000 sizeof(struct btrfs_item) +
1001 extent_item_size, 1);
1005 if (!replace_extent || !(*key_inserted))
1006 btrfs_release_path(path);
1008 *drop_end = found ? min(end, extent_end) : end;
1012 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
1013 struct btrfs_root *root, struct inode *inode, u64 start,
1014 u64 end, int drop_cache)
1016 struct btrfs_path *path;
1019 path = btrfs_alloc_path();
1022 ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
1023 drop_cache, 0, 0, NULL);
1024 btrfs_free_path(path);
1028 static int extent_mergeable(struct extent_buffer *leaf, int slot,
1029 u64 objectid, u64 bytenr, u64 orig_offset,
1030 u64 *start, u64 *end)
1032 struct btrfs_file_extent_item *fi;
1033 struct btrfs_key key;
1036 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1039 btrfs_item_key_to_cpu(leaf, &key, slot);
1040 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
1043 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1044 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
1045 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
1046 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
1047 btrfs_file_extent_compression(leaf, fi) ||
1048 btrfs_file_extent_encryption(leaf, fi) ||
1049 btrfs_file_extent_other_encoding(leaf, fi))
1052 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1053 if ((*start && *start != key.offset) || (*end && *end != extent_end))
1056 *start = key.offset;
1062 * Mark extent in the range start - end as written.
1064 * This changes extent type from 'pre-allocated' to 'regular'. If only
1065 * part of extent is marked as written, the extent will be split into
1068 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
1069 struct inode *inode, u64 start, u64 end)
1071 struct btrfs_root *root = BTRFS_I(inode)->root;
1072 struct extent_buffer *leaf;
1073 struct btrfs_path *path;
1074 struct btrfs_file_extent_item *fi;
1075 struct btrfs_key key;
1076 struct btrfs_key new_key;
1088 u64 ino = btrfs_ino(inode);
1090 path = btrfs_alloc_path();
1097 key.type = BTRFS_EXTENT_DATA_KEY;
1100 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1103 if (ret > 0 && path->slots[0] > 0)
1106 leaf = path->nodes[0];
1107 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1108 BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
1109 fi = btrfs_item_ptr(leaf, path->slots[0],
1110 struct btrfs_file_extent_item);
1111 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
1112 BTRFS_FILE_EXTENT_PREALLOC);
1113 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1114 BUG_ON(key.offset > start || extent_end < end);
1116 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1117 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1118 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
1119 memcpy(&new_key, &key, sizeof(new_key));
1121 if (start == key.offset && end < extent_end) {
1124 if (extent_mergeable(leaf, path->slots[0] - 1,
1125 ino, bytenr, orig_offset,
1126 &other_start, &other_end)) {
1127 new_key.offset = end;
1128 btrfs_set_item_key_safe(root, path, &new_key);
1129 fi = btrfs_item_ptr(leaf, path->slots[0],
1130 struct btrfs_file_extent_item);
1131 btrfs_set_file_extent_generation(leaf, fi,
1133 btrfs_set_file_extent_num_bytes(leaf, fi,
1135 btrfs_set_file_extent_offset(leaf, fi,
1137 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1138 struct btrfs_file_extent_item);
1139 btrfs_set_file_extent_generation(leaf, fi,
1141 btrfs_set_file_extent_num_bytes(leaf, fi,
1143 btrfs_mark_buffer_dirty(leaf);
1148 if (start > key.offset && end == extent_end) {
1151 if (extent_mergeable(leaf, path->slots[0] + 1,
1152 ino, bytenr, orig_offset,
1153 &other_start, &other_end)) {
1154 fi = btrfs_item_ptr(leaf, path->slots[0],
1155 struct btrfs_file_extent_item);
1156 btrfs_set_file_extent_num_bytes(leaf, fi,
1157 start - key.offset);
1158 btrfs_set_file_extent_generation(leaf, fi,
1161 new_key.offset = start;
1162 btrfs_set_item_key_safe(root, path, &new_key);
1164 fi = btrfs_item_ptr(leaf, path->slots[0],
1165 struct btrfs_file_extent_item);
1166 btrfs_set_file_extent_generation(leaf, fi,
1168 btrfs_set_file_extent_num_bytes(leaf, fi,
1170 btrfs_set_file_extent_offset(leaf, fi,
1171 start - orig_offset);
1172 btrfs_mark_buffer_dirty(leaf);
1177 while (start > key.offset || end < extent_end) {
1178 if (key.offset == start)
1181 new_key.offset = split;
1182 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1183 if (ret == -EAGAIN) {
1184 btrfs_release_path(path);
1188 btrfs_abort_transaction(trans, root, ret);
1192 leaf = path->nodes[0];
1193 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1194 struct btrfs_file_extent_item);
1195 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1196 btrfs_set_file_extent_num_bytes(leaf, fi,
1197 split - key.offset);
1199 fi = btrfs_item_ptr(leaf, path->slots[0],
1200 struct btrfs_file_extent_item);
1202 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1203 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1204 btrfs_set_file_extent_num_bytes(leaf, fi,
1205 extent_end - split);
1206 btrfs_mark_buffer_dirty(leaf);
1208 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1209 root->root_key.objectid,
1210 ino, orig_offset, 1);
1211 BUG_ON(ret); /* -ENOMEM */
1213 if (split == start) {
1216 BUG_ON(start != key.offset);
1225 if (extent_mergeable(leaf, path->slots[0] + 1,
1226 ino, bytenr, orig_offset,
1227 &other_start, &other_end)) {
1229 btrfs_release_path(path);
1232 extent_end = other_end;
1233 del_slot = path->slots[0] + 1;
1235 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1236 0, root->root_key.objectid,
1237 ino, orig_offset, 0);
1238 BUG_ON(ret); /* -ENOMEM */
1242 if (extent_mergeable(leaf, path->slots[0] - 1,
1243 ino, bytenr, orig_offset,
1244 &other_start, &other_end)) {
1246 btrfs_release_path(path);
1249 key.offset = other_start;
1250 del_slot = path->slots[0];
1252 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1253 0, root->root_key.objectid,
1254 ino, orig_offset, 0);
1255 BUG_ON(ret); /* -ENOMEM */
1258 fi = btrfs_item_ptr(leaf, path->slots[0],
1259 struct btrfs_file_extent_item);
1260 btrfs_set_file_extent_type(leaf, fi,
1261 BTRFS_FILE_EXTENT_REG);
1262 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1263 btrfs_mark_buffer_dirty(leaf);
1265 fi = btrfs_item_ptr(leaf, del_slot - 1,
1266 struct btrfs_file_extent_item);
1267 btrfs_set_file_extent_type(leaf, fi,
1268 BTRFS_FILE_EXTENT_REG);
1269 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1270 btrfs_set_file_extent_num_bytes(leaf, fi,
1271 extent_end - key.offset);
1272 btrfs_mark_buffer_dirty(leaf);
1274 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
1276 btrfs_abort_transaction(trans, root, ret);
1281 btrfs_free_path(path);
1286 * on error we return an unlocked page and the error value
1287 * on success we return a locked page and 0
1289 static int prepare_uptodate_page(struct page *page, u64 pos,
1290 bool force_uptodate)
1294 if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
1295 !PageUptodate(page)) {
1296 ret = btrfs_readpage(NULL, page);
1300 if (!PageUptodate(page)) {
1309 * this just gets pages into the page cache and locks them down.
1311 static noinline int prepare_pages(struct inode *inode, struct page **pages,
1312 size_t num_pages, loff_t pos,
1313 size_t write_bytes, bool force_uptodate)
1316 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1317 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1321 for (i = 0; i < num_pages; i++) {
1322 pages[i] = find_or_create_page(inode->i_mapping, index + i,
1323 mask | __GFP_WRITE);
1331 err = prepare_uptodate_page(pages[i], pos,
1333 if (i == num_pages - 1)
1334 err = prepare_uptodate_page(pages[i],
1335 pos + write_bytes, false);
1337 page_cache_release(pages[i]);
1341 wait_on_page_writeback(pages[i]);
1346 while (faili >= 0) {
1347 unlock_page(pages[faili]);
1348 page_cache_release(pages[faili]);
1356 * This function locks the extent and properly waits for data=ordered extents
1357 * to finish before allowing the pages to be modified if need.
1360 * 1 - the extent is locked
1361 * 0 - the extent is not locked, and everything is OK
1362 * -EAGAIN - need re-prepare the pages
1363 * the other < 0 number - Something wrong happens
1366 lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages,
1367 size_t num_pages, loff_t pos,
1368 u64 *lockstart, u64 *lockend,
1369 struct extent_state **cached_state)
1376 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1377 last_pos = start_pos + ((u64)num_pages << PAGE_CACHE_SHIFT) - 1;
1379 if (start_pos < inode->i_size) {
1380 struct btrfs_ordered_extent *ordered;
1381 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1382 start_pos, last_pos, 0, cached_state);
1383 ordered = btrfs_lookup_ordered_range(inode, start_pos,
1384 last_pos - start_pos + 1);
1386 ordered->file_offset + ordered->len > start_pos &&
1387 ordered->file_offset <= last_pos) {
1388 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1389 start_pos, last_pos,
1390 cached_state, GFP_NOFS);
1391 for (i = 0; i < num_pages; i++) {
1392 unlock_page(pages[i]);
1393 page_cache_release(pages[i]);
1395 btrfs_start_ordered_extent(inode, ordered, 1);
1396 btrfs_put_ordered_extent(ordered);
1400 btrfs_put_ordered_extent(ordered);
1402 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
1403 last_pos, EXTENT_DIRTY | EXTENT_DELALLOC |
1404 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
1405 0, 0, cached_state, GFP_NOFS);
1406 *lockstart = start_pos;
1407 *lockend = last_pos;
1411 for (i = 0; i < num_pages; i++) {
1412 if (clear_page_dirty_for_io(pages[i]))
1413 account_page_redirty(pages[i]);
1414 set_page_extent_mapped(pages[i]);
1415 WARN_ON(!PageLocked(pages[i]));
1421 static noinline int check_can_nocow(struct inode *inode, loff_t pos,
1422 size_t *write_bytes)
1424 struct btrfs_root *root = BTRFS_I(inode)->root;
1425 struct btrfs_ordered_extent *ordered;
1426 u64 lockstart, lockend;
1430 ret = btrfs_start_nocow_write(root);
1434 lockstart = round_down(pos, root->sectorsize);
1435 lockend = round_up(pos + *write_bytes, root->sectorsize) - 1;
1438 lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1439 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1440 lockend - lockstart + 1);
1444 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1445 btrfs_start_ordered_extent(inode, ordered, 1);
1446 btrfs_put_ordered_extent(ordered);
1449 num_bytes = lockend - lockstart + 1;
1450 ret = can_nocow_extent(inode, lockstart, &num_bytes, NULL, NULL, NULL);
1453 btrfs_end_nocow_write(root);
1455 *write_bytes = min_t(size_t, *write_bytes ,
1456 num_bytes - pos + lockstart);
1459 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1464 static noinline ssize_t __btrfs_buffered_write(struct file *file,
1468 struct inode *inode = file_inode(file);
1469 struct btrfs_root *root = BTRFS_I(inode)->root;
1470 struct page **pages = NULL;
1471 struct extent_state *cached_state = NULL;
1472 u64 release_bytes = 0;
1475 unsigned long first_index;
1476 size_t num_written = 0;
1479 bool only_release_metadata = false;
1480 bool force_page_uptodate = false;
1483 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
1484 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1485 (sizeof(struct page *)));
1486 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1487 nrptrs = max(nrptrs, 8);
1488 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
1492 first_index = pos >> PAGE_CACHE_SHIFT;
1494 while (iov_iter_count(i) > 0) {
1495 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1496 size_t write_bytes = min(iov_iter_count(i),
1497 nrptrs * (size_t)PAGE_CACHE_SIZE -
1499 size_t num_pages = (write_bytes + offset +
1500 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1501 size_t reserve_bytes;
1505 WARN_ON(num_pages > nrptrs);
1508 * Fault pages before locking them in prepare_pages
1509 * to avoid recursive lock
1511 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
1516 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1517 ret = btrfs_check_data_free_space(inode, reserve_bytes);
1518 if (ret == -ENOSPC &&
1519 (BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
1520 BTRFS_INODE_PREALLOC))) {
1521 ret = check_can_nocow(inode, pos, &write_bytes);
1523 only_release_metadata = true;
1525 * our prealloc extent may be smaller than
1526 * write_bytes, so scale down.
1528 num_pages = (write_bytes + offset +
1529 PAGE_CACHE_SIZE - 1) >>
1531 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1541 ret = btrfs_delalloc_reserve_metadata(inode, reserve_bytes);
1543 if (!only_release_metadata)
1544 btrfs_free_reserved_data_space(inode,
1547 btrfs_end_nocow_write(root);
1551 release_bytes = reserve_bytes;
1552 need_unlock = false;
1555 * This is going to setup the pages array with the number of
1556 * pages we want, so we don't really need to worry about the
1557 * contents of pages from loop to loop
1559 ret = prepare_pages(inode, pages, num_pages,
1561 force_page_uptodate);
1565 ret = lock_and_cleanup_extent_if_need(inode, pages, num_pages,
1566 pos, &lockstart, &lockend,
1572 } else if (ret > 0) {
1577 copied = btrfs_copy_from_user(pos, num_pages,
1578 write_bytes, pages, i);
1581 * if we have trouble faulting in the pages, fall
1582 * back to one page at a time
1584 if (copied < write_bytes)
1588 force_page_uptodate = true;
1591 force_page_uptodate = false;
1592 dirty_pages = (copied + offset +
1593 PAGE_CACHE_SIZE - 1) >>
1598 * If we had a short copy we need to release the excess delaloc
1599 * bytes we reserved. We need to increment outstanding_extents
1600 * because btrfs_delalloc_release_space will decrement it, but
1601 * we still have an outstanding extent for the chunk we actually
1604 if (num_pages > dirty_pages) {
1605 release_bytes = (num_pages - dirty_pages) <<
1608 spin_lock(&BTRFS_I(inode)->lock);
1609 BTRFS_I(inode)->outstanding_extents++;
1610 spin_unlock(&BTRFS_I(inode)->lock);
1612 if (only_release_metadata)
1613 btrfs_delalloc_release_metadata(inode,
1616 btrfs_delalloc_release_space(inode,
1620 release_bytes = dirty_pages << PAGE_CACHE_SHIFT;
1623 ret = btrfs_dirty_pages(root, inode, pages,
1624 dirty_pages, pos, copied,
1627 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1628 lockstart, lockend, &cached_state,
1631 btrfs_drop_pages(pages, num_pages);
1636 if (only_release_metadata)
1637 btrfs_end_nocow_write(root);
1639 if (only_release_metadata && copied > 0) {
1640 u64 lockstart = round_down(pos, root->sectorsize);
1641 u64 lockend = lockstart +
1642 (dirty_pages << PAGE_CACHE_SHIFT) - 1;
1644 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1645 lockend, EXTENT_NORESERVE, NULL,
1647 only_release_metadata = false;
1650 btrfs_drop_pages(pages, num_pages);
1654 balance_dirty_pages_ratelimited(inode->i_mapping);
1655 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1656 btrfs_btree_balance_dirty(root);
1659 num_written += copied;
1664 if (release_bytes) {
1665 if (only_release_metadata) {
1666 btrfs_end_nocow_write(root);
1667 btrfs_delalloc_release_metadata(inode, release_bytes);
1669 btrfs_delalloc_release_space(inode, release_bytes);
1673 return num_written ? num_written : ret;
1676 static ssize_t __btrfs_direct_write(struct kiocb *iocb,
1677 const struct iovec *iov,
1678 unsigned long nr_segs, loff_t pos,
1679 size_t count, size_t ocount)
1681 struct file *file = iocb->ki_filp;
1684 ssize_t written_buffered;
1688 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
1691 if (written < 0 || written == count)
1696 iov_iter_init(&i, iov, nr_segs, count, written);
1697 written_buffered = __btrfs_buffered_write(file, &i, pos);
1698 if (written_buffered < 0) {
1699 err = written_buffered;
1702 endbyte = pos + written_buffered - 1;
1703 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1706 written += written_buffered;
1707 iocb->ki_pos = pos + written_buffered;
1708 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1709 endbyte >> PAGE_CACHE_SHIFT);
1711 return written ? written : err;
1714 static void update_time_for_write(struct inode *inode)
1716 struct timespec now;
1718 if (IS_NOCMTIME(inode))
1721 now = current_fs_time(inode->i_sb);
1722 if (!timespec_equal(&inode->i_mtime, &now))
1723 inode->i_mtime = now;
1725 if (!timespec_equal(&inode->i_ctime, &now))
1726 inode->i_ctime = now;
1728 if (IS_I_VERSION(inode))
1729 inode_inc_iversion(inode);
1732 static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1733 const struct iovec *iov,
1734 unsigned long nr_segs, loff_t pos)
1736 struct file *file = iocb->ki_filp;
1737 struct inode *inode = file_inode(file);
1738 struct btrfs_root *root = BTRFS_I(inode)->root;
1741 ssize_t num_written = 0;
1743 size_t count, ocount;
1744 bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
1746 mutex_lock(&inode->i_mutex);
1748 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1750 mutex_unlock(&inode->i_mutex);
1755 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1756 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1758 mutex_unlock(&inode->i_mutex);
1763 mutex_unlock(&inode->i_mutex);
1767 err = file_remove_suid(file);
1769 mutex_unlock(&inode->i_mutex);
1774 * If BTRFS flips readonly due to some impossible error
1775 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1776 * although we have opened a file as writable, we have
1777 * to stop this write operation to ensure FS consistency.
1779 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
1780 mutex_unlock(&inode->i_mutex);
1786 * We reserve space for updating the inode when we reserve space for the
1787 * extent we are going to write, so we will enospc out there. We don't
1788 * need to start yet another transaction to update the inode as we will
1789 * update the inode when we finish writing whatever data we write.
1791 update_time_for_write(inode);
1793 start_pos = round_down(pos, root->sectorsize);
1794 if (start_pos > i_size_read(inode)) {
1795 /* Expand hole size to cover write data, preventing empty gap */
1796 end_pos = round_up(pos + count, root->sectorsize);
1797 err = btrfs_cont_expand(inode, i_size_read(inode), end_pos);
1799 mutex_unlock(&inode->i_mutex);
1805 atomic_inc(&BTRFS_I(inode)->sync_writers);
1807 if (unlikely(file->f_flags & O_DIRECT)) {
1808 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1809 pos, count, ocount);
1813 iov_iter_init(&i, iov, nr_segs, count, num_written);
1815 num_written = __btrfs_buffered_write(file, &i, pos);
1816 if (num_written > 0)
1817 iocb->ki_pos = pos + num_written;
1820 mutex_unlock(&inode->i_mutex);
1823 * we want to make sure fsync finds this change
1824 * but we haven't joined a transaction running right now.
1826 * Later on, someone is sure to update the inode and get the
1827 * real transid recorded.
1829 * We set last_trans now to the fs_info generation + 1,
1830 * this will either be one more than the running transaction
1831 * or the generation used for the next transaction if there isn't
1832 * one running right now.
1834 * We also have to set last_sub_trans to the current log transid,
1835 * otherwise subsequent syncs to a file that's been synced in this
1836 * transaction will appear to have already occured.
1838 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1839 BTRFS_I(inode)->last_sub_trans = root->log_transid;
1840 if (num_written > 0) {
1841 err = generic_write_sync(file, pos, num_written);
1847 atomic_dec(&BTRFS_I(inode)->sync_writers);
1849 current->backing_dev_info = NULL;
1850 return num_written ? num_written : err;
1853 int btrfs_release_file(struct inode *inode, struct file *filp)
1856 * ordered_data_close is set by settattr when we are about to truncate
1857 * a file from a non-zero size to a zero size. This tries to
1858 * flush down new bytes that may have been written if the
1859 * application were using truncate to replace a file in place.
1861 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1862 &BTRFS_I(inode)->runtime_flags)) {
1863 struct btrfs_trans_handle *trans;
1864 struct btrfs_root *root = BTRFS_I(inode)->root;
1867 * We need to block on a committing transaction to keep us from
1868 * throwing a ordered operation on to the list and causing
1869 * something like sync to deadlock trying to flush out this
1872 trans = btrfs_start_transaction(root, 0);
1874 return PTR_ERR(trans);
1875 btrfs_add_ordered_operation(trans, BTRFS_I(inode)->root, inode);
1876 btrfs_end_transaction(trans, root);
1877 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1878 filemap_flush(inode->i_mapping);
1880 if (filp->private_data)
1881 btrfs_ioctl_trans_end(filp);
1886 * fsync call for both files and directories. This logs the inode into
1887 * the tree log instead of forcing full commits whenever possible.
1889 * It needs to call filemap_fdatawait so that all ordered extent updates are
1890 * in the metadata btree are up to date for copying to the log.
1892 * It drops the inode mutex before doing the tree log commit. This is an
1893 * important optimization for directories because holding the mutex prevents
1894 * new operations on the dir while we write to disk.
1896 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1898 struct dentry *dentry = file->f_path.dentry;
1899 struct inode *inode = dentry->d_inode;
1900 struct btrfs_root *root = BTRFS_I(inode)->root;
1901 struct btrfs_trans_handle *trans;
1902 struct btrfs_log_ctx ctx;
1906 trace_btrfs_sync_file(file, datasync);
1909 * We write the dirty pages in the range and wait until they complete
1910 * out of the ->i_mutex. If so, we can flush the dirty pages by
1911 * multi-task, and make the performance up. See
1912 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
1914 atomic_inc(&BTRFS_I(inode)->sync_writers);
1915 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
1916 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1917 &BTRFS_I(inode)->runtime_flags))
1918 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
1919 atomic_dec(&BTRFS_I(inode)->sync_writers);
1923 mutex_lock(&inode->i_mutex);
1926 * We flush the dirty pages again to avoid some dirty pages in the
1929 atomic_inc(&root->log_batch);
1930 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1931 &BTRFS_I(inode)->runtime_flags);
1933 ret = btrfs_wait_ordered_range(inode, start, end - start + 1);
1935 mutex_unlock(&inode->i_mutex);
1939 atomic_inc(&root->log_batch);
1942 * check the transaction that last modified this inode
1943 * and see if its already been committed
1945 if (!BTRFS_I(inode)->last_trans) {
1946 mutex_unlock(&inode->i_mutex);
1951 * if the last transaction that changed this file was before
1952 * the current transaction, we can bail out now without any
1956 if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
1957 BTRFS_I(inode)->last_trans <=
1958 root->fs_info->last_trans_committed) {
1959 BTRFS_I(inode)->last_trans = 0;
1962 * We'v had everything committed since the last time we were
1963 * modified so clear this flag in case it was set for whatever
1964 * reason, it's no longer relevant.
1966 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1967 &BTRFS_I(inode)->runtime_flags);
1968 mutex_unlock(&inode->i_mutex);
1973 * ok we haven't committed the transaction yet, lets do a commit
1975 if (file->private_data)
1976 btrfs_ioctl_trans_end(file);
1979 * We use start here because we will need to wait on the IO to complete
1980 * in btrfs_sync_log, which could require joining a transaction (for
1981 * example checking cross references in the nocow path). If we use join
1982 * here we could get into a situation where we're waiting on IO to
1983 * happen that is blocked on a transaction trying to commit. With start
1984 * we inc the extwriter counter, so we wait for all extwriters to exit
1985 * before we start blocking join'ers. This comment is to keep somebody
1986 * from thinking they are super smart and changing this to
1987 * btrfs_join_transaction *cough*Josef*cough*.
1989 trans = btrfs_start_transaction(root, 0);
1990 if (IS_ERR(trans)) {
1991 ret = PTR_ERR(trans);
1992 mutex_unlock(&inode->i_mutex);
1997 btrfs_init_log_ctx(&ctx);
1999 ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx);
2001 /* Fallthrough and commit/free transaction. */
2005 /* we've logged all the items and now have a consistent
2006 * version of the file in the log. It is possible that
2007 * someone will come in and modify the file, but that's
2008 * fine because the log is consistent on disk, and we
2009 * have references to all of the file's extents
2011 * It is possible that someone will come in and log the
2012 * file again, but that will end up using the synchronization
2013 * inside btrfs_sync_log to keep things safe.
2015 mutex_unlock(&inode->i_mutex);
2017 if (ret != BTRFS_NO_LOG_SYNC) {
2019 ret = btrfs_sync_log(trans, root, &ctx);
2021 ret = btrfs_end_transaction(trans, root);
2026 ret = btrfs_wait_ordered_range(inode, start,
2029 btrfs_end_transaction(trans, root);
2033 ret = btrfs_commit_transaction(trans, root);
2035 ret = btrfs_end_transaction(trans, root);
2038 return ret > 0 ? -EIO : ret;
2041 static const struct vm_operations_struct btrfs_file_vm_ops = {
2042 .fault = filemap_fault,
2043 .map_pages = filemap_map_pages,
2044 .page_mkwrite = btrfs_page_mkwrite,
2045 .remap_pages = generic_file_remap_pages,
2048 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
2050 struct address_space *mapping = filp->f_mapping;
2052 if (!mapping->a_ops->readpage)
2055 file_accessed(filp);
2056 vma->vm_ops = &btrfs_file_vm_ops;
2061 static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
2062 int slot, u64 start, u64 end)
2064 struct btrfs_file_extent_item *fi;
2065 struct btrfs_key key;
2067 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2070 btrfs_item_key_to_cpu(leaf, &key, slot);
2071 if (key.objectid != btrfs_ino(inode) ||
2072 key.type != BTRFS_EXTENT_DATA_KEY)
2075 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2077 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2080 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2083 if (key.offset == end)
2085 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2090 static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
2091 struct btrfs_path *path, u64 offset, u64 end)
2093 struct btrfs_root *root = BTRFS_I(inode)->root;
2094 struct extent_buffer *leaf;
2095 struct btrfs_file_extent_item *fi;
2096 struct extent_map *hole_em;
2097 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2098 struct btrfs_key key;
2101 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
2104 key.objectid = btrfs_ino(inode);
2105 key.type = BTRFS_EXTENT_DATA_KEY;
2106 key.offset = offset;
2108 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2113 leaf = path->nodes[0];
2114 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
2118 fi = btrfs_item_ptr(leaf, path->slots[0],
2119 struct btrfs_file_extent_item);
2120 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2122 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2123 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2124 btrfs_set_file_extent_offset(leaf, fi, 0);
2125 btrfs_mark_buffer_dirty(leaf);
2129 if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
2133 key.offset = offset;
2134 btrfs_set_item_key_safe(root, path, &key);
2135 fi = btrfs_item_ptr(leaf, path->slots[0],
2136 struct btrfs_file_extent_item);
2137 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2139 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2140 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2141 btrfs_set_file_extent_offset(leaf, fi, 0);
2142 btrfs_mark_buffer_dirty(leaf);
2145 btrfs_release_path(path);
2147 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
2148 0, 0, end - offset, 0, end - offset,
2154 btrfs_release_path(path);
2156 hole_em = alloc_extent_map();
2158 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2159 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2160 &BTRFS_I(inode)->runtime_flags);
2162 hole_em->start = offset;
2163 hole_em->len = end - offset;
2164 hole_em->ram_bytes = hole_em->len;
2165 hole_em->orig_start = offset;
2167 hole_em->block_start = EXTENT_MAP_HOLE;
2168 hole_em->block_len = 0;
2169 hole_em->orig_block_len = 0;
2170 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
2171 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2172 hole_em->generation = trans->transid;
2175 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2176 write_lock(&em_tree->lock);
2177 ret = add_extent_mapping(em_tree, hole_em, 1);
2178 write_unlock(&em_tree->lock);
2179 } while (ret == -EEXIST);
2180 free_extent_map(hole_em);
2182 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2183 &BTRFS_I(inode)->runtime_flags);
2190 * Find a hole extent on given inode and change start/len to the end of hole
2191 * extent.(hole/vacuum extent whose em->start <= start &&
2192 * em->start + em->len > start)
2193 * When a hole extent is found, return 1 and modify start/len.
2195 static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len)
2197 struct extent_map *em;
2200 em = btrfs_get_extent(inode, NULL, 0, *start, *len, 0);
2201 if (IS_ERR_OR_NULL(em)) {
2209 /* Hole or vacuum extent(only exists in no-hole mode) */
2210 if (em->block_start == EXTENT_MAP_HOLE) {
2212 *len = em->start + em->len > *start + *len ?
2213 0 : *start + *len - em->start - em->len;
2214 *start = em->start + em->len;
2216 free_extent_map(em);
2220 static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2222 struct btrfs_root *root = BTRFS_I(inode)->root;
2223 struct extent_state *cached_state = NULL;
2224 struct btrfs_path *path;
2225 struct btrfs_block_rsv *rsv;
2226 struct btrfs_trans_handle *trans;
2231 u64 orig_start = offset;
2233 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
2239 bool no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
2242 ret = btrfs_wait_ordered_range(inode, offset, len);
2246 mutex_lock(&inode->i_mutex);
2247 ino_size = round_up(inode->i_size, PAGE_CACHE_SIZE);
2248 ret = find_first_non_hole(inode, &offset, &len);
2250 goto out_only_mutex;
2252 /* Already in a large hole */
2254 goto out_only_mutex;
2257 lockstart = round_up(offset , BTRFS_I(inode)->root->sectorsize);
2258 lockend = round_down(offset + len,
2259 BTRFS_I(inode)->root->sectorsize) - 1;
2260 same_page = ((offset >> PAGE_CACHE_SHIFT) ==
2261 ((offset + len - 1) >> PAGE_CACHE_SHIFT));
2264 * We needn't truncate any page which is beyond the end of the file
2265 * because we are sure there is no data there.
2268 * Only do this if we are in the same page and we aren't doing the
2271 if (same_page && len < PAGE_CACHE_SIZE) {
2272 if (offset < ino_size)
2273 ret = btrfs_truncate_page(inode, offset, len, 0);
2274 goto out_only_mutex;
2277 /* zero back part of the first page */
2278 if (offset < ino_size) {
2279 ret = btrfs_truncate_page(inode, offset, 0, 0);
2281 mutex_unlock(&inode->i_mutex);
2286 /* Check the aligned pages after the first unaligned page,
2287 * if offset != orig_start, which means the first unaligned page
2288 * including serveral following pages are already in holes,
2289 * the extra check can be skipped */
2290 if (offset == orig_start) {
2291 /* after truncate page, check hole again */
2292 len = offset + len - lockstart;
2294 ret = find_first_non_hole(inode, &offset, &len);
2296 goto out_only_mutex;
2299 goto out_only_mutex;
2304 /* Check the tail unaligned part is in a hole */
2305 tail_start = lockend + 1;
2306 tail_len = offset + len - tail_start;
2308 ret = find_first_non_hole(inode, &tail_start, &tail_len);
2309 if (unlikely(ret < 0))
2310 goto out_only_mutex;
2312 /* zero the front end of the last page */
2313 if (tail_start + tail_len < ino_size) {
2314 ret = btrfs_truncate_page(inode,
2315 tail_start + tail_len, 0, 1);
2317 goto out_only_mutex;
2322 if (lockend < lockstart) {
2323 mutex_unlock(&inode->i_mutex);
2328 struct btrfs_ordered_extent *ordered;
2330 truncate_pagecache_range(inode, lockstart, lockend);
2332 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2334 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
2337 * We need to make sure we have no ordered extents in this range
2338 * and nobody raced in and read a page in this range, if we did
2339 * we need to try again.
2342 (ordered->file_offset + ordered->len <= lockstart ||
2343 ordered->file_offset > lockend)) &&
2344 !btrfs_page_exists_in_range(inode, lockstart, lockend)) {
2346 btrfs_put_ordered_extent(ordered);
2350 btrfs_put_ordered_extent(ordered);
2351 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2352 lockend, &cached_state, GFP_NOFS);
2353 ret = btrfs_wait_ordered_range(inode, lockstart,
2354 lockend - lockstart + 1);
2356 mutex_unlock(&inode->i_mutex);
2361 path = btrfs_alloc_path();
2367 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
2372 rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
2376 * 1 - update the inode
2377 * 1 - removing the extents in the range
2378 * 1 - adding the hole extent if no_holes isn't set
2380 rsv_count = no_holes ? 2 : 3;
2381 trans = btrfs_start_transaction(root, rsv_count);
2382 if (IS_ERR(trans)) {
2383 err = PTR_ERR(trans);
2387 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
2390 trans->block_rsv = rsv;
2392 cur_offset = lockstart;
2393 len = lockend - cur_offset;
2394 while (cur_offset < lockend) {
2395 ret = __btrfs_drop_extents(trans, root, inode, path,
2396 cur_offset, lockend + 1,
2397 &drop_end, 1, 0, 0, NULL);
2401 trans->block_rsv = &root->fs_info->trans_block_rsv;
2403 if (cur_offset < ino_size) {
2404 ret = fill_holes(trans, inode, path, cur_offset,
2412 cur_offset = drop_end;
2414 ret = btrfs_update_inode(trans, root, inode);
2420 btrfs_end_transaction(trans, root);
2421 btrfs_btree_balance_dirty(root);
2423 trans = btrfs_start_transaction(root, rsv_count);
2424 if (IS_ERR(trans)) {
2425 ret = PTR_ERR(trans);
2430 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
2432 BUG_ON(ret); /* shouldn't happen */
2433 trans->block_rsv = rsv;
2435 ret = find_first_non_hole(inode, &cur_offset, &len);
2436 if (unlikely(ret < 0))
2449 trans->block_rsv = &root->fs_info->trans_block_rsv;
2451 * Don't insert file hole extent item if it's for a range beyond eof
2452 * (because it's useless) or if it represents a 0 bytes range (when
2453 * cur_offset == drop_end).
2455 if (cur_offset < ino_size && cur_offset < drop_end) {
2456 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2467 inode_inc_iversion(inode);
2468 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2470 trans->block_rsv = &root->fs_info->trans_block_rsv;
2471 ret = btrfs_update_inode(trans, root, inode);
2472 btrfs_end_transaction(trans, root);
2473 btrfs_btree_balance_dirty(root);
2475 btrfs_free_path(path);
2476 btrfs_free_block_rsv(root, rsv);
2478 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2479 &cached_state, GFP_NOFS);
2481 mutex_unlock(&inode->i_mutex);
2487 static long btrfs_fallocate(struct file *file, int mode,
2488 loff_t offset, loff_t len)
2490 struct inode *inode = file_inode(file);
2491 struct extent_state *cached_state = NULL;
2492 struct btrfs_root *root = BTRFS_I(inode)->root;
2499 struct extent_map *em;
2500 int blocksize = BTRFS_I(inode)->root->sectorsize;
2503 alloc_start = round_down(offset, blocksize);
2504 alloc_end = round_up(offset + len, blocksize);
2506 /* Make sure we aren't being give some crap mode */
2507 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2510 if (mode & FALLOC_FL_PUNCH_HOLE)
2511 return btrfs_punch_hole(inode, offset, len);
2514 * Make sure we have enough space before we do the
2517 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
2520 if (root->fs_info->quota_enabled) {
2521 ret = btrfs_qgroup_reserve(root, alloc_end - alloc_start);
2523 goto out_reserve_fail;
2526 mutex_lock(&inode->i_mutex);
2527 ret = inode_newsize_ok(inode, alloc_end);
2531 if (alloc_start > inode->i_size) {
2532 ret = btrfs_cont_expand(inode, i_size_read(inode),
2538 * If we are fallocating from the end of the file onward we
2539 * need to zero out the end of the page if i_size lands in the
2542 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
2548 * wait for ordered IO before we have any locks. We'll loop again
2549 * below with the locks held.
2551 ret = btrfs_wait_ordered_range(inode, alloc_start,
2552 alloc_end - alloc_start);
2556 locked_end = alloc_end - 1;
2558 struct btrfs_ordered_extent *ordered;
2560 /* the extent lock is ordered inside the running
2563 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
2564 locked_end, 0, &cached_state);
2565 ordered = btrfs_lookup_first_ordered_extent(inode,
2568 ordered->file_offset + ordered->len > alloc_start &&
2569 ordered->file_offset < alloc_end) {
2570 btrfs_put_ordered_extent(ordered);
2571 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2572 alloc_start, locked_end,
2573 &cached_state, GFP_NOFS);
2575 * we can't wait on the range with the transaction
2576 * running or with the extent lock held
2578 ret = btrfs_wait_ordered_range(inode, alloc_start,
2579 alloc_end - alloc_start);
2584 btrfs_put_ordered_extent(ordered);
2589 cur_offset = alloc_start;
2593 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2594 alloc_end - cur_offset, 0);
2595 if (IS_ERR_OR_NULL(em)) {
2602 last_byte = min(extent_map_end(em), alloc_end);
2603 actual_end = min_t(u64, extent_map_end(em), offset + len);
2604 last_byte = ALIGN(last_byte, blocksize);
2606 if (em->block_start == EXTENT_MAP_HOLE ||
2607 (cur_offset >= inode->i_size &&
2608 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2609 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2610 last_byte - cur_offset,
2611 1 << inode->i_blkbits,
2616 free_extent_map(em);
2619 } else if (actual_end > inode->i_size &&
2620 !(mode & FALLOC_FL_KEEP_SIZE)) {
2622 * We didn't need to allocate any more space, but we
2623 * still extended the size of the file so we need to
2626 inode->i_ctime = CURRENT_TIME;
2627 i_size_write(inode, actual_end);
2628 btrfs_ordered_update_i_size(inode, actual_end, NULL);
2630 free_extent_map(em);
2632 cur_offset = last_byte;
2633 if (cur_offset >= alloc_end) {
2638 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2639 &cached_state, GFP_NOFS);
2641 mutex_unlock(&inode->i_mutex);
2642 if (root->fs_info->quota_enabled)
2643 btrfs_qgroup_free(root, alloc_end - alloc_start);
2645 /* Let go of our reservation. */
2646 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
2650 static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
2652 struct btrfs_root *root = BTRFS_I(inode)->root;
2653 struct extent_map *em = NULL;
2654 struct extent_state *cached_state = NULL;
2655 u64 lockstart = *offset;
2656 u64 lockend = i_size_read(inode);
2657 u64 start = *offset;
2658 u64 len = i_size_read(inode);
2661 lockend = max_t(u64, root->sectorsize, lockend);
2662 if (lockend <= lockstart)
2663 lockend = lockstart + root->sectorsize;
2666 len = lockend - lockstart + 1;
2668 len = max_t(u64, len, root->sectorsize);
2669 if (inode->i_size == 0)
2672 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
2675 while (start < inode->i_size) {
2676 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2683 if (whence == SEEK_HOLE &&
2684 (em->block_start == EXTENT_MAP_HOLE ||
2685 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2687 else if (whence == SEEK_DATA &&
2688 (em->block_start != EXTENT_MAP_HOLE &&
2689 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2692 start = em->start + em->len;
2693 free_extent_map(em);
2697 free_extent_map(em);
2699 if (whence == SEEK_DATA && start >= inode->i_size)
2702 *offset = min_t(loff_t, start, inode->i_size);
2704 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2705 &cached_state, GFP_NOFS);
2709 static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
2711 struct inode *inode = file->f_mapping->host;
2714 mutex_lock(&inode->i_mutex);
2718 offset = generic_file_llseek(file, offset, whence);
2722 if (offset >= i_size_read(inode)) {
2723 mutex_unlock(&inode->i_mutex);
2727 ret = find_desired_extent(inode, &offset, whence);
2729 mutex_unlock(&inode->i_mutex);
2734 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
2736 mutex_unlock(&inode->i_mutex);
2740 const struct file_operations btrfs_file_operations = {
2741 .llseek = btrfs_file_llseek,
2742 .read = do_sync_read,
2743 .write = do_sync_write,
2744 .aio_read = generic_file_aio_read,
2745 .splice_read = generic_file_splice_read,
2746 .aio_write = btrfs_file_aio_write,
2747 .mmap = btrfs_file_mmap,
2748 .open = generic_file_open,
2749 .release = btrfs_release_file,
2750 .fsync = btrfs_sync_file,
2751 .fallocate = btrfs_fallocate,
2752 .unlocked_ioctl = btrfs_ioctl,
2753 #ifdef CONFIG_COMPAT
2754 .compat_ioctl = btrfs_ioctl,
2758 void btrfs_auto_defrag_exit(void)
2760 if (btrfs_inode_defrag_cachep)
2761 kmem_cache_destroy(btrfs_inode_defrag_cachep);
2764 int btrfs_auto_defrag_init(void)
2766 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
2767 sizeof(struct inode_defrag), 0,
2768 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2770 if (!btrfs_inode_defrag_cachep)