]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - fs/btrfs/file.c
Btrfs: fix race between snapshot deletion and getting inode
[~shefty/rdma-dev.git] / fs / btrfs / file.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <linux/fs.h>
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/falloc.h>
28 #include <linux/swap.h>
29 #include <linux/writeback.h>
30 #include <linux/statfs.h>
31 #include <linux/compat.h>
32 #include <linux/slab.h>
33 #include "ctree.h"
34 #include "disk-io.h"
35 #include "transaction.h"
36 #include "btrfs_inode.h"
37 #include "ioctl.h"
38 #include "print-tree.h"
39 #include "tree-log.h"
40 #include "locking.h"
41 #include "compat.h"
42 #include "volumes.h"
43
44 static struct kmem_cache *btrfs_inode_defrag_cachep;
45 /*
46  * when auto defrag is enabled we
47  * queue up these defrag structs to remember which
48  * inodes need defragging passes
49  */
50 struct inode_defrag {
51         struct rb_node rb_node;
52         /* objectid */
53         u64 ino;
54         /*
55          * transid where the defrag was added, we search for
56          * extents newer than this
57          */
58         u64 transid;
59
60         /* root objectid */
61         u64 root;
62
63         /* last offset we were able to defrag */
64         u64 last_offset;
65
66         /* if we've wrapped around back to zero once already */
67         int cycled;
68 };
69
70 static int __compare_inode_defrag(struct inode_defrag *defrag1,
71                                   struct inode_defrag *defrag2)
72 {
73         if (defrag1->root > defrag2->root)
74                 return 1;
75         else if (defrag1->root < defrag2->root)
76                 return -1;
77         else if (defrag1->ino > defrag2->ino)
78                 return 1;
79         else if (defrag1->ino < defrag2->ino)
80                 return -1;
81         else
82                 return 0;
83 }
84
85 /* pop a record for an inode into the defrag tree.  The lock
86  * must be held already
87  *
88  * If you're inserting a record for an older transid than an
89  * existing record, the transid already in the tree is lowered
90  *
91  * If an existing record is found the defrag item you
92  * pass in is freed
93  */
94 static int __btrfs_add_inode_defrag(struct inode *inode,
95                                     struct inode_defrag *defrag)
96 {
97         struct btrfs_root *root = BTRFS_I(inode)->root;
98         struct inode_defrag *entry;
99         struct rb_node **p;
100         struct rb_node *parent = NULL;
101         int ret;
102
103         p = &root->fs_info->defrag_inodes.rb_node;
104         while (*p) {
105                 parent = *p;
106                 entry = rb_entry(parent, struct inode_defrag, rb_node);
107
108                 ret = __compare_inode_defrag(defrag, entry);
109                 if (ret < 0)
110                         p = &parent->rb_left;
111                 else if (ret > 0)
112                         p = &parent->rb_right;
113                 else {
114                         /* if we're reinserting an entry for
115                          * an old defrag run, make sure to
116                          * lower the transid of our existing record
117                          */
118                         if (defrag->transid < entry->transid)
119                                 entry->transid = defrag->transid;
120                         if (defrag->last_offset > entry->last_offset)
121                                 entry->last_offset = defrag->last_offset;
122                         return -EEXIST;
123                 }
124         }
125         set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
126         rb_link_node(&defrag->rb_node, parent, p);
127         rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
128         return 0;
129 }
130
131 static inline int __need_auto_defrag(struct btrfs_root *root)
132 {
133         if (!btrfs_test_opt(root, AUTO_DEFRAG))
134                 return 0;
135
136         if (btrfs_fs_closing(root->fs_info))
137                 return 0;
138
139         return 1;
140 }
141
142 /*
143  * insert a defrag record for this inode if auto defrag is
144  * enabled
145  */
146 int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
147                            struct inode *inode)
148 {
149         struct btrfs_root *root = BTRFS_I(inode)->root;
150         struct inode_defrag *defrag;
151         u64 transid;
152         int ret;
153
154         if (!__need_auto_defrag(root))
155                 return 0;
156
157         if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
158                 return 0;
159
160         if (trans)
161                 transid = trans->transid;
162         else
163                 transid = BTRFS_I(inode)->root->last_trans;
164
165         defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
166         if (!defrag)
167                 return -ENOMEM;
168
169         defrag->ino = btrfs_ino(inode);
170         defrag->transid = transid;
171         defrag->root = root->root_key.objectid;
172
173         spin_lock(&root->fs_info->defrag_inodes_lock);
174         if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
175                 /*
176                  * If we set IN_DEFRAG flag and evict the inode from memory,
177                  * and then re-read this inode, this new inode doesn't have
178                  * IN_DEFRAG flag. At the case, we may find the existed defrag.
179                  */
180                 ret = __btrfs_add_inode_defrag(inode, defrag);
181                 if (ret)
182                         kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
183         } else {
184                 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
185         }
186         spin_unlock(&root->fs_info->defrag_inodes_lock);
187         return 0;
188 }
189
190 /*
191  * Requeue the defrag object. If there is a defrag object that points to
192  * the same inode in the tree, we will merge them together (by
193  * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
194  */
195 void btrfs_requeue_inode_defrag(struct inode *inode,
196                                 struct inode_defrag *defrag)
197 {
198         struct btrfs_root *root = BTRFS_I(inode)->root;
199         int ret;
200
201         if (!__need_auto_defrag(root))
202                 goto out;
203
204         /*
205          * Here we don't check the IN_DEFRAG flag, because we need merge
206          * them together.
207          */
208         spin_lock(&root->fs_info->defrag_inodes_lock);
209         ret = __btrfs_add_inode_defrag(inode, defrag);
210         spin_unlock(&root->fs_info->defrag_inodes_lock);
211         if (ret)
212                 goto out;
213         return;
214 out:
215         kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
216 }
217
218 /*
219  * pick the defragable inode that we want, if it doesn't exist, we will get
220  * the next one.
221  */
222 static struct inode_defrag *
223 btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
224 {
225         struct inode_defrag *entry = NULL;
226         struct inode_defrag tmp;
227         struct rb_node *p;
228         struct rb_node *parent = NULL;
229         int ret;
230
231         tmp.ino = ino;
232         tmp.root = root;
233
234         spin_lock(&fs_info->defrag_inodes_lock);
235         p = fs_info->defrag_inodes.rb_node;
236         while (p) {
237                 parent = p;
238                 entry = rb_entry(parent, struct inode_defrag, rb_node);
239
240                 ret = __compare_inode_defrag(&tmp, entry);
241                 if (ret < 0)
242                         p = parent->rb_left;
243                 else if (ret > 0)
244                         p = parent->rb_right;
245                 else
246                         goto out;
247         }
248
249         if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
250                 parent = rb_next(parent);
251                 if (parent)
252                         entry = rb_entry(parent, struct inode_defrag, rb_node);
253                 else
254                         entry = NULL;
255         }
256 out:
257         if (entry)
258                 rb_erase(parent, &fs_info->defrag_inodes);
259         spin_unlock(&fs_info->defrag_inodes_lock);
260         return entry;
261 }
262
263 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
264 {
265         struct inode_defrag *defrag;
266         struct rb_node *node;
267
268         spin_lock(&fs_info->defrag_inodes_lock);
269         node = rb_first(&fs_info->defrag_inodes);
270         while (node) {
271                 rb_erase(node, &fs_info->defrag_inodes);
272                 defrag = rb_entry(node, struct inode_defrag, rb_node);
273                 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
274
275                 if (need_resched()) {
276                         spin_unlock(&fs_info->defrag_inodes_lock);
277                         cond_resched();
278                         spin_lock(&fs_info->defrag_inodes_lock);
279                 }
280
281                 node = rb_first(&fs_info->defrag_inodes);
282         }
283         spin_unlock(&fs_info->defrag_inodes_lock);
284 }
285
286 #define BTRFS_DEFRAG_BATCH      1024
287
288 static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
289                                     struct inode_defrag *defrag)
290 {
291         struct btrfs_root *inode_root;
292         struct inode *inode;
293         struct btrfs_key key;
294         struct btrfs_ioctl_defrag_range_args range;
295         int num_defrag;
296         int index;
297         int ret;
298
299         /* get the inode */
300         key.objectid = defrag->root;
301         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
302         key.offset = (u64)-1;
303
304         index = srcu_read_lock(&fs_info->subvol_srcu);
305
306         inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
307         if (IS_ERR(inode_root)) {
308                 ret = PTR_ERR(inode_root);
309                 goto cleanup;
310         }
311         if (btrfs_root_refs(&inode_root->root_item) == 0) {
312                 ret = -ENOENT;
313                 goto cleanup;
314         }
315
316         key.objectid = defrag->ino;
317         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
318         key.offset = 0;
319         inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
320         if (IS_ERR(inode)) {
321                 ret = PTR_ERR(inode);
322                 goto cleanup;
323         }
324         srcu_read_unlock(&fs_info->subvol_srcu, index);
325
326         /* do a chunk of defrag */
327         clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
328         memset(&range, 0, sizeof(range));
329         range.len = (u64)-1;
330         range.start = defrag->last_offset;
331
332         sb_start_write(fs_info->sb);
333         num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
334                                        BTRFS_DEFRAG_BATCH);
335         sb_end_write(fs_info->sb);
336         /*
337          * if we filled the whole defrag batch, there
338          * must be more work to do.  Queue this defrag
339          * again
340          */
341         if (num_defrag == BTRFS_DEFRAG_BATCH) {
342                 defrag->last_offset = range.start;
343                 btrfs_requeue_inode_defrag(inode, defrag);
344         } else if (defrag->last_offset && !defrag->cycled) {
345                 /*
346                  * we didn't fill our defrag batch, but
347                  * we didn't start at zero.  Make sure we loop
348                  * around to the start of the file.
349                  */
350                 defrag->last_offset = 0;
351                 defrag->cycled = 1;
352                 btrfs_requeue_inode_defrag(inode, defrag);
353         } else {
354                 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
355         }
356
357         iput(inode);
358         return 0;
359 cleanup:
360         srcu_read_unlock(&fs_info->subvol_srcu, index);
361         kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
362         return ret;
363 }
364
365 /*
366  * run through the list of inodes in the FS that need
367  * defragging
368  */
369 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
370 {
371         struct inode_defrag *defrag;
372         u64 first_ino = 0;
373         u64 root_objectid = 0;
374
375         atomic_inc(&fs_info->defrag_running);
376         while(1) {
377                 if (!__need_auto_defrag(fs_info->tree_root))
378                         break;
379
380                 /* find an inode to defrag */
381                 defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
382                                                  first_ino);
383                 if (!defrag) {
384                         if (root_objectid || first_ino) {
385                                 root_objectid = 0;
386                                 first_ino = 0;
387                                 continue;
388                         } else {
389                                 break;
390                         }
391                 }
392
393                 first_ino = defrag->ino + 1;
394                 root_objectid = defrag->root;
395
396                 __btrfs_run_defrag_inode(fs_info, defrag);
397         }
398         atomic_dec(&fs_info->defrag_running);
399
400         /*
401          * during unmount, we use the transaction_wait queue to
402          * wait for the defragger to stop
403          */
404         wake_up(&fs_info->transaction_wait);
405         return 0;
406 }
407
408 /* simple helper to fault in pages and copy.  This should go away
409  * and be replaced with calls into generic code.
410  */
411 static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
412                                          size_t write_bytes,
413                                          struct page **prepared_pages,
414                                          struct iov_iter *i)
415 {
416         size_t copied = 0;
417         size_t total_copied = 0;
418         int pg = 0;
419         int offset = pos & (PAGE_CACHE_SIZE - 1);
420
421         while (write_bytes > 0) {
422                 size_t count = min_t(size_t,
423                                      PAGE_CACHE_SIZE - offset, write_bytes);
424                 struct page *page = prepared_pages[pg];
425                 /*
426                  * Copy data from userspace to the current page
427                  *
428                  * Disable pagefault to avoid recursive lock since
429                  * the pages are already locked
430                  */
431                 pagefault_disable();
432                 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
433                 pagefault_enable();
434
435                 /* Flush processor's dcache for this page */
436                 flush_dcache_page(page);
437
438                 /*
439                  * if we get a partial write, we can end up with
440                  * partially up to date pages.  These add
441                  * a lot of complexity, so make sure they don't
442                  * happen by forcing this copy to be retried.
443                  *
444                  * The rest of the btrfs_file_write code will fall
445                  * back to page at a time copies after we return 0.
446                  */
447                 if (!PageUptodate(page) && copied < count)
448                         copied = 0;
449
450                 iov_iter_advance(i, copied);
451                 write_bytes -= copied;
452                 total_copied += copied;
453
454                 /* Return to btrfs_file_aio_write to fault page */
455                 if (unlikely(copied == 0))
456                         break;
457
458                 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
459                         offset += copied;
460                 } else {
461                         pg++;
462                         offset = 0;
463                 }
464         }
465         return total_copied;
466 }
467
468 /*
469  * unlocks pages after btrfs_file_write is done with them
470  */
471 void btrfs_drop_pages(struct page **pages, size_t num_pages)
472 {
473         size_t i;
474         for (i = 0; i < num_pages; i++) {
475                 /* page checked is some magic around finding pages that
476                  * have been modified without going through btrfs_set_page_dirty
477                  * clear it here
478                  */
479                 ClearPageChecked(pages[i]);
480                 unlock_page(pages[i]);
481                 mark_page_accessed(pages[i]);
482                 page_cache_release(pages[i]);
483         }
484 }
485
486 /*
487  * after copy_from_user, pages need to be dirtied and we need to make
488  * sure holes are created between the current EOF and the start of
489  * any next extents (if required).
490  *
491  * this also makes the decision about creating an inline extent vs
492  * doing real data extents, marking pages dirty and delalloc as required.
493  */
494 int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
495                       struct page **pages, size_t num_pages,
496                       loff_t pos, size_t write_bytes,
497                       struct extent_state **cached)
498 {
499         int err = 0;
500         int i;
501         u64 num_bytes;
502         u64 start_pos;
503         u64 end_of_last_block;
504         u64 end_pos = pos + write_bytes;
505         loff_t isize = i_size_read(inode);
506
507         start_pos = pos & ~((u64)root->sectorsize - 1);
508         num_bytes = (write_bytes + pos - start_pos +
509                     root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
510
511         end_of_last_block = start_pos + num_bytes - 1;
512         err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
513                                         cached);
514         if (err)
515                 return err;
516
517         for (i = 0; i < num_pages; i++) {
518                 struct page *p = pages[i];
519                 SetPageUptodate(p);
520                 ClearPageChecked(p);
521                 set_page_dirty(p);
522         }
523
524         /*
525          * we've only changed i_size in ram, and we haven't updated
526          * the disk i_size.  There is no need to log the inode
527          * at this time.
528          */
529         if (end_pos > isize)
530                 i_size_write(inode, end_pos);
531         return 0;
532 }
533
534 /*
535  * this drops all the extents in the cache that intersect the range
536  * [start, end].  Existing extents are split as required.
537  */
538 void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
539                              int skip_pinned)
540 {
541         struct extent_map *em;
542         struct extent_map *split = NULL;
543         struct extent_map *split2 = NULL;
544         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
545         u64 len = end - start + 1;
546         u64 gen;
547         int ret;
548         int testend = 1;
549         unsigned long flags;
550         int compressed = 0;
551
552         WARN_ON(end < start);
553         if (end == (u64)-1) {
554                 len = (u64)-1;
555                 testend = 0;
556         }
557         while (1) {
558                 int no_splits = 0;
559
560                 if (!split)
561                         split = alloc_extent_map();
562                 if (!split2)
563                         split2 = alloc_extent_map();
564                 if (!split || !split2)
565                         no_splits = 1;
566
567                 write_lock(&em_tree->lock);
568                 em = lookup_extent_mapping(em_tree, start, len);
569                 if (!em) {
570                         write_unlock(&em_tree->lock);
571                         break;
572                 }
573                 flags = em->flags;
574                 gen = em->generation;
575                 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
576                         if (testend && em->start + em->len >= start + len) {
577                                 free_extent_map(em);
578                                 write_unlock(&em_tree->lock);
579                                 break;
580                         }
581                         start = em->start + em->len;
582                         if (testend)
583                                 len = start + len - (em->start + em->len);
584                         free_extent_map(em);
585                         write_unlock(&em_tree->lock);
586                         continue;
587                 }
588                 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
589                 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
590                 remove_extent_mapping(em_tree, em);
591                 if (no_splits)
592                         goto next;
593
594                 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
595                     em->start < start) {
596                         split->start = em->start;
597                         split->len = start - em->start;
598                         split->orig_start = em->orig_start;
599                         split->block_start = em->block_start;
600
601                         if (compressed)
602                                 split->block_len = em->block_len;
603                         else
604                                 split->block_len = split->len;
605                         split->orig_block_len = max(split->block_len,
606                                                     em->orig_block_len);
607                         split->generation = gen;
608                         split->bdev = em->bdev;
609                         split->flags = flags;
610                         split->compress_type = em->compress_type;
611                         ret = add_extent_mapping(em_tree, split);
612                         BUG_ON(ret); /* Logic error */
613                         list_move(&split->list, &em_tree->modified_extents);
614                         free_extent_map(split);
615                         split = split2;
616                         split2 = NULL;
617                 }
618                 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
619                     testend && em->start + em->len > start + len) {
620                         u64 diff = start + len - em->start;
621
622                         split->start = start + len;
623                         split->len = em->start + em->len - (start + len);
624                         split->bdev = em->bdev;
625                         split->flags = flags;
626                         split->compress_type = em->compress_type;
627                         split->generation = gen;
628                         split->orig_block_len = max(em->block_len,
629                                                     em->orig_block_len);
630
631                         if (compressed) {
632                                 split->block_len = em->block_len;
633                                 split->block_start = em->block_start;
634                                 split->orig_start = em->orig_start;
635                         } else {
636                                 split->block_len = split->len;
637                                 split->block_start = em->block_start + diff;
638                                 split->orig_start = em->orig_start;
639                         }
640
641                         ret = add_extent_mapping(em_tree, split);
642                         BUG_ON(ret); /* Logic error */
643                         list_move(&split->list, &em_tree->modified_extents);
644                         free_extent_map(split);
645                         split = NULL;
646                 }
647 next:
648                 write_unlock(&em_tree->lock);
649
650                 /* once for us */
651                 free_extent_map(em);
652                 /* once for the tree*/
653                 free_extent_map(em);
654         }
655         if (split)
656                 free_extent_map(split);
657         if (split2)
658                 free_extent_map(split2);
659 }
660
661 /*
662  * this is very complex, but the basic idea is to drop all extents
663  * in the range start - end.  hint_block is filled in with a block number
664  * that would be a good hint to the block allocator for this file.
665  *
666  * If an extent intersects the range but is not entirely inside the range
667  * it is either truncated or split.  Anything entirely inside the range
668  * is deleted from the tree.
669  */
670 int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
671                          struct btrfs_root *root, struct inode *inode,
672                          struct btrfs_path *path, u64 start, u64 end,
673                          u64 *drop_end, int drop_cache)
674 {
675         struct extent_buffer *leaf;
676         struct btrfs_file_extent_item *fi;
677         struct btrfs_key key;
678         struct btrfs_key new_key;
679         u64 ino = btrfs_ino(inode);
680         u64 search_start = start;
681         u64 disk_bytenr = 0;
682         u64 num_bytes = 0;
683         u64 extent_offset = 0;
684         u64 extent_end = 0;
685         int del_nr = 0;
686         int del_slot = 0;
687         int extent_type;
688         int recow;
689         int ret;
690         int modify_tree = -1;
691         int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
692         int found = 0;
693
694         if (drop_cache)
695                 btrfs_drop_extent_cache(inode, start, end - 1, 0);
696
697         if (start >= BTRFS_I(inode)->disk_i_size)
698                 modify_tree = 0;
699
700         while (1) {
701                 recow = 0;
702                 ret = btrfs_lookup_file_extent(trans, root, path, ino,
703                                                search_start, modify_tree);
704                 if (ret < 0)
705                         break;
706                 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
707                         leaf = path->nodes[0];
708                         btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
709                         if (key.objectid == ino &&
710                             key.type == BTRFS_EXTENT_DATA_KEY)
711                                 path->slots[0]--;
712                 }
713                 ret = 0;
714 next_slot:
715                 leaf = path->nodes[0];
716                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
717                         BUG_ON(del_nr > 0);
718                         ret = btrfs_next_leaf(root, path);
719                         if (ret < 0)
720                                 break;
721                         if (ret > 0) {
722                                 ret = 0;
723                                 break;
724                         }
725                         leaf = path->nodes[0];
726                         recow = 1;
727                 }
728
729                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
730                 if (key.objectid > ino ||
731                     key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
732                         break;
733
734                 fi = btrfs_item_ptr(leaf, path->slots[0],
735                                     struct btrfs_file_extent_item);
736                 extent_type = btrfs_file_extent_type(leaf, fi);
737
738                 if (extent_type == BTRFS_FILE_EXTENT_REG ||
739                     extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
740                         disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
741                         num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
742                         extent_offset = btrfs_file_extent_offset(leaf, fi);
743                         extent_end = key.offset +
744                                 btrfs_file_extent_num_bytes(leaf, fi);
745                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
746                         extent_end = key.offset +
747                                 btrfs_file_extent_inline_len(leaf, fi);
748                 } else {
749                         WARN_ON(1);
750                         extent_end = search_start;
751                 }
752
753                 if (extent_end <= search_start) {
754                         path->slots[0]++;
755                         goto next_slot;
756                 }
757
758                 found = 1;
759                 search_start = max(key.offset, start);
760                 if (recow || !modify_tree) {
761                         modify_tree = -1;
762                         btrfs_release_path(path);
763                         continue;
764                 }
765
766                 /*
767                  *     | - range to drop - |
768                  *  | -------- extent -------- |
769                  */
770                 if (start > key.offset && end < extent_end) {
771                         BUG_ON(del_nr > 0);
772                         BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
773
774                         memcpy(&new_key, &key, sizeof(new_key));
775                         new_key.offset = start;
776                         ret = btrfs_duplicate_item(trans, root, path,
777                                                    &new_key);
778                         if (ret == -EAGAIN) {
779                                 btrfs_release_path(path);
780                                 continue;
781                         }
782                         if (ret < 0)
783                                 break;
784
785                         leaf = path->nodes[0];
786                         fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
787                                             struct btrfs_file_extent_item);
788                         btrfs_set_file_extent_num_bytes(leaf, fi,
789                                                         start - key.offset);
790
791                         fi = btrfs_item_ptr(leaf, path->slots[0],
792                                             struct btrfs_file_extent_item);
793
794                         extent_offset += start - key.offset;
795                         btrfs_set_file_extent_offset(leaf, fi, extent_offset);
796                         btrfs_set_file_extent_num_bytes(leaf, fi,
797                                                         extent_end - start);
798                         btrfs_mark_buffer_dirty(leaf);
799
800                         if (update_refs && disk_bytenr > 0) {
801                                 ret = btrfs_inc_extent_ref(trans, root,
802                                                 disk_bytenr, num_bytes, 0,
803                                                 root->root_key.objectid,
804                                                 new_key.objectid,
805                                                 start - extent_offset, 0);
806                                 BUG_ON(ret); /* -ENOMEM */
807                         }
808                         key.offset = start;
809                 }
810                 /*
811                  *  | ---- range to drop ----- |
812                  *      | -------- extent -------- |
813                  */
814                 if (start <= key.offset && end < extent_end) {
815                         BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
816
817                         memcpy(&new_key, &key, sizeof(new_key));
818                         new_key.offset = end;
819                         btrfs_set_item_key_safe(trans, root, path, &new_key);
820
821                         extent_offset += end - key.offset;
822                         btrfs_set_file_extent_offset(leaf, fi, extent_offset);
823                         btrfs_set_file_extent_num_bytes(leaf, fi,
824                                                         extent_end - end);
825                         btrfs_mark_buffer_dirty(leaf);
826                         if (update_refs && disk_bytenr > 0)
827                                 inode_sub_bytes(inode, end - key.offset);
828                         break;
829                 }
830
831                 search_start = extent_end;
832                 /*
833                  *       | ---- range to drop ----- |
834                  *  | -------- extent -------- |
835                  */
836                 if (start > key.offset && end >= extent_end) {
837                         BUG_ON(del_nr > 0);
838                         BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
839
840                         btrfs_set_file_extent_num_bytes(leaf, fi,
841                                                         start - key.offset);
842                         btrfs_mark_buffer_dirty(leaf);
843                         if (update_refs && disk_bytenr > 0)
844                                 inode_sub_bytes(inode, extent_end - start);
845                         if (end == extent_end)
846                                 break;
847
848                         path->slots[0]++;
849                         goto next_slot;
850                 }
851
852                 /*
853                  *  | ---- range to drop ----- |
854                  *    | ------ extent ------ |
855                  */
856                 if (start <= key.offset && end >= extent_end) {
857                         if (del_nr == 0) {
858                                 del_slot = path->slots[0];
859                                 del_nr = 1;
860                         } else {
861                                 BUG_ON(del_slot + del_nr != path->slots[0]);
862                                 del_nr++;
863                         }
864
865                         if (update_refs &&
866                             extent_type == BTRFS_FILE_EXTENT_INLINE) {
867                                 inode_sub_bytes(inode,
868                                                 extent_end - key.offset);
869                                 extent_end = ALIGN(extent_end,
870                                                    root->sectorsize);
871                         } else if (update_refs && disk_bytenr > 0) {
872                                 ret = btrfs_free_extent(trans, root,
873                                                 disk_bytenr, num_bytes, 0,
874                                                 root->root_key.objectid,
875                                                 key.objectid, key.offset -
876                                                 extent_offset, 0);
877                                 BUG_ON(ret); /* -ENOMEM */
878                                 inode_sub_bytes(inode,
879                                                 extent_end - key.offset);
880                         }
881
882                         if (end == extent_end)
883                                 break;
884
885                         if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
886                                 path->slots[0]++;
887                                 goto next_slot;
888                         }
889
890                         ret = btrfs_del_items(trans, root, path, del_slot,
891                                               del_nr);
892                         if (ret) {
893                                 btrfs_abort_transaction(trans, root, ret);
894                                 break;
895                         }
896
897                         del_nr = 0;
898                         del_slot = 0;
899
900                         btrfs_release_path(path);
901                         continue;
902                 }
903
904                 BUG_ON(1);
905         }
906
907         if (!ret && del_nr > 0) {
908                 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
909                 if (ret)
910                         btrfs_abort_transaction(trans, root, ret);
911         }
912
913         if (drop_end)
914                 *drop_end = found ? min(end, extent_end) : end;
915         btrfs_release_path(path);
916         return ret;
917 }
918
919 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
920                        struct btrfs_root *root, struct inode *inode, u64 start,
921                        u64 end, int drop_cache)
922 {
923         struct btrfs_path *path;
924         int ret;
925
926         path = btrfs_alloc_path();
927         if (!path)
928                 return -ENOMEM;
929         ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
930                                    drop_cache);
931         btrfs_free_path(path);
932         return ret;
933 }
934
935 static int extent_mergeable(struct extent_buffer *leaf, int slot,
936                             u64 objectid, u64 bytenr, u64 orig_offset,
937                             u64 *start, u64 *end)
938 {
939         struct btrfs_file_extent_item *fi;
940         struct btrfs_key key;
941         u64 extent_end;
942
943         if (slot < 0 || slot >= btrfs_header_nritems(leaf))
944                 return 0;
945
946         btrfs_item_key_to_cpu(leaf, &key, slot);
947         if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
948                 return 0;
949
950         fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
951         if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
952             btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
953             btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
954             btrfs_file_extent_compression(leaf, fi) ||
955             btrfs_file_extent_encryption(leaf, fi) ||
956             btrfs_file_extent_other_encoding(leaf, fi))
957                 return 0;
958
959         extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
960         if ((*start && *start != key.offset) || (*end && *end != extent_end))
961                 return 0;
962
963         *start = key.offset;
964         *end = extent_end;
965         return 1;
966 }
967
968 /*
969  * Mark extent in the range start - end as written.
970  *
971  * This changes extent type from 'pre-allocated' to 'regular'. If only
972  * part of extent is marked as written, the extent will be split into
973  * two or three.
974  */
975 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
976                               struct inode *inode, u64 start, u64 end)
977 {
978         struct btrfs_root *root = BTRFS_I(inode)->root;
979         struct extent_buffer *leaf;
980         struct btrfs_path *path;
981         struct btrfs_file_extent_item *fi;
982         struct btrfs_key key;
983         struct btrfs_key new_key;
984         u64 bytenr;
985         u64 num_bytes;
986         u64 extent_end;
987         u64 orig_offset;
988         u64 other_start;
989         u64 other_end;
990         u64 split;
991         int del_nr = 0;
992         int del_slot = 0;
993         int recow;
994         int ret;
995         u64 ino = btrfs_ino(inode);
996
997         path = btrfs_alloc_path();
998         if (!path)
999                 return -ENOMEM;
1000 again:
1001         recow = 0;
1002         split = start;
1003         key.objectid = ino;
1004         key.type = BTRFS_EXTENT_DATA_KEY;
1005         key.offset = split;
1006
1007         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1008         if (ret < 0)
1009                 goto out;
1010         if (ret > 0 && path->slots[0] > 0)
1011                 path->slots[0]--;
1012
1013         leaf = path->nodes[0];
1014         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1015         BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
1016         fi = btrfs_item_ptr(leaf, path->slots[0],
1017                             struct btrfs_file_extent_item);
1018         BUG_ON(btrfs_file_extent_type(leaf, fi) !=
1019                BTRFS_FILE_EXTENT_PREALLOC);
1020         extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1021         BUG_ON(key.offset > start || extent_end < end);
1022
1023         bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1024         num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1025         orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
1026         memcpy(&new_key, &key, sizeof(new_key));
1027
1028         if (start == key.offset && end < extent_end) {
1029                 other_start = 0;
1030                 other_end = start;
1031                 if (extent_mergeable(leaf, path->slots[0] - 1,
1032                                      ino, bytenr, orig_offset,
1033                                      &other_start, &other_end)) {
1034                         new_key.offset = end;
1035                         btrfs_set_item_key_safe(trans, root, path, &new_key);
1036                         fi = btrfs_item_ptr(leaf, path->slots[0],
1037                                             struct btrfs_file_extent_item);
1038                         btrfs_set_file_extent_generation(leaf, fi,
1039                                                          trans->transid);
1040                         btrfs_set_file_extent_num_bytes(leaf, fi,
1041                                                         extent_end - end);
1042                         btrfs_set_file_extent_offset(leaf, fi,
1043                                                      end - orig_offset);
1044                         fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1045                                             struct btrfs_file_extent_item);
1046                         btrfs_set_file_extent_generation(leaf, fi,
1047                                                          trans->transid);
1048                         btrfs_set_file_extent_num_bytes(leaf, fi,
1049                                                         end - other_start);
1050                         btrfs_mark_buffer_dirty(leaf);
1051                         goto out;
1052                 }
1053         }
1054
1055         if (start > key.offset && end == extent_end) {
1056                 other_start = end;
1057                 other_end = 0;
1058                 if (extent_mergeable(leaf, path->slots[0] + 1,
1059                                      ino, bytenr, orig_offset,
1060                                      &other_start, &other_end)) {
1061                         fi = btrfs_item_ptr(leaf, path->slots[0],
1062                                             struct btrfs_file_extent_item);
1063                         btrfs_set_file_extent_num_bytes(leaf, fi,
1064                                                         start - key.offset);
1065                         btrfs_set_file_extent_generation(leaf, fi,
1066                                                          trans->transid);
1067                         path->slots[0]++;
1068                         new_key.offset = start;
1069                         btrfs_set_item_key_safe(trans, root, path, &new_key);
1070
1071                         fi = btrfs_item_ptr(leaf, path->slots[0],
1072                                             struct btrfs_file_extent_item);
1073                         btrfs_set_file_extent_generation(leaf, fi,
1074                                                          trans->transid);
1075                         btrfs_set_file_extent_num_bytes(leaf, fi,
1076                                                         other_end - start);
1077                         btrfs_set_file_extent_offset(leaf, fi,
1078                                                      start - orig_offset);
1079                         btrfs_mark_buffer_dirty(leaf);
1080                         goto out;
1081                 }
1082         }
1083
1084         while (start > key.offset || end < extent_end) {
1085                 if (key.offset == start)
1086                         split = end;
1087
1088                 new_key.offset = split;
1089                 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1090                 if (ret == -EAGAIN) {
1091                         btrfs_release_path(path);
1092                         goto again;
1093                 }
1094                 if (ret < 0) {
1095                         btrfs_abort_transaction(trans, root, ret);
1096                         goto out;
1097                 }
1098
1099                 leaf = path->nodes[0];
1100                 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1101                                     struct btrfs_file_extent_item);
1102                 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1103                 btrfs_set_file_extent_num_bytes(leaf, fi,
1104                                                 split - key.offset);
1105
1106                 fi = btrfs_item_ptr(leaf, path->slots[0],
1107                                     struct btrfs_file_extent_item);
1108
1109                 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1110                 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1111                 btrfs_set_file_extent_num_bytes(leaf, fi,
1112                                                 extent_end - split);
1113                 btrfs_mark_buffer_dirty(leaf);
1114
1115                 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1116                                            root->root_key.objectid,
1117                                            ino, orig_offset, 0);
1118                 BUG_ON(ret); /* -ENOMEM */
1119
1120                 if (split == start) {
1121                         key.offset = start;
1122                 } else {
1123                         BUG_ON(start != key.offset);
1124                         path->slots[0]--;
1125                         extent_end = end;
1126                 }
1127                 recow = 1;
1128         }
1129
1130         other_start = end;
1131         other_end = 0;
1132         if (extent_mergeable(leaf, path->slots[0] + 1,
1133                              ino, bytenr, orig_offset,
1134                              &other_start, &other_end)) {
1135                 if (recow) {
1136                         btrfs_release_path(path);
1137                         goto again;
1138                 }
1139                 extent_end = other_end;
1140                 del_slot = path->slots[0] + 1;
1141                 del_nr++;
1142                 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1143                                         0, root->root_key.objectid,
1144                                         ino, orig_offset, 0);
1145                 BUG_ON(ret); /* -ENOMEM */
1146         }
1147         other_start = 0;
1148         other_end = start;
1149         if (extent_mergeable(leaf, path->slots[0] - 1,
1150                              ino, bytenr, orig_offset,
1151                              &other_start, &other_end)) {
1152                 if (recow) {
1153                         btrfs_release_path(path);
1154                         goto again;
1155                 }
1156                 key.offset = other_start;
1157                 del_slot = path->slots[0];
1158                 del_nr++;
1159                 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1160                                         0, root->root_key.objectid,
1161                                         ino, orig_offset, 0);
1162                 BUG_ON(ret); /* -ENOMEM */
1163         }
1164         if (del_nr == 0) {
1165                 fi = btrfs_item_ptr(leaf, path->slots[0],
1166                            struct btrfs_file_extent_item);
1167                 btrfs_set_file_extent_type(leaf, fi,
1168                                            BTRFS_FILE_EXTENT_REG);
1169                 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1170                 btrfs_mark_buffer_dirty(leaf);
1171         } else {
1172                 fi = btrfs_item_ptr(leaf, del_slot - 1,
1173                            struct btrfs_file_extent_item);
1174                 btrfs_set_file_extent_type(leaf, fi,
1175                                            BTRFS_FILE_EXTENT_REG);
1176                 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1177                 btrfs_set_file_extent_num_bytes(leaf, fi,
1178                                                 extent_end - key.offset);
1179                 btrfs_mark_buffer_dirty(leaf);
1180
1181                 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
1182                 if (ret < 0) {
1183                         btrfs_abort_transaction(trans, root, ret);
1184                         goto out;
1185                 }
1186         }
1187 out:
1188         btrfs_free_path(path);
1189         return 0;
1190 }
1191
1192 /*
1193  * on error we return an unlocked page and the error value
1194  * on success we return a locked page and 0
1195  */
1196 static int prepare_uptodate_page(struct page *page, u64 pos,
1197                                  bool force_uptodate)
1198 {
1199         int ret = 0;
1200
1201         if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
1202             !PageUptodate(page)) {
1203                 ret = btrfs_readpage(NULL, page);
1204                 if (ret)
1205                         return ret;
1206                 lock_page(page);
1207                 if (!PageUptodate(page)) {
1208                         unlock_page(page);
1209                         return -EIO;
1210                 }
1211         }
1212         return 0;
1213 }
1214
1215 /*
1216  * this gets pages into the page cache and locks them down, it also properly
1217  * waits for data=ordered extents to finish before allowing the pages to be
1218  * modified.
1219  */
1220 static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
1221                          struct page **pages, size_t num_pages,
1222                          loff_t pos, unsigned long first_index,
1223                          size_t write_bytes, bool force_uptodate)
1224 {
1225         struct extent_state *cached_state = NULL;
1226         int i;
1227         unsigned long index = pos >> PAGE_CACHE_SHIFT;
1228         struct inode *inode = fdentry(file)->d_inode;
1229         gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1230         int err = 0;
1231         int faili = 0;
1232         u64 start_pos;
1233         u64 last_pos;
1234
1235         start_pos = pos & ~((u64)root->sectorsize - 1);
1236         last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
1237
1238 again:
1239         for (i = 0; i < num_pages; i++) {
1240                 pages[i] = find_or_create_page(inode->i_mapping, index + i,
1241                                                mask | __GFP_WRITE);
1242                 if (!pages[i]) {
1243                         faili = i - 1;
1244                         err = -ENOMEM;
1245                         goto fail;
1246                 }
1247
1248                 if (i == 0)
1249                         err = prepare_uptodate_page(pages[i], pos,
1250                                                     force_uptodate);
1251                 if (i == num_pages - 1)
1252                         err = prepare_uptodate_page(pages[i],
1253                                                     pos + write_bytes, false);
1254                 if (err) {
1255                         page_cache_release(pages[i]);
1256                         faili = i - 1;
1257                         goto fail;
1258                 }
1259                 wait_on_page_writeback(pages[i]);
1260         }
1261         err = 0;
1262         if (start_pos < inode->i_size) {
1263                 struct btrfs_ordered_extent *ordered;
1264                 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1265                                  start_pos, last_pos - 1, 0, &cached_state);
1266                 ordered = btrfs_lookup_first_ordered_extent(inode,
1267                                                             last_pos - 1);
1268                 if (ordered &&
1269                     ordered->file_offset + ordered->len > start_pos &&
1270                     ordered->file_offset < last_pos) {
1271                         btrfs_put_ordered_extent(ordered);
1272                         unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1273                                              start_pos, last_pos - 1,
1274                                              &cached_state, GFP_NOFS);
1275                         for (i = 0; i < num_pages; i++) {
1276                                 unlock_page(pages[i]);
1277                                 page_cache_release(pages[i]);
1278                         }
1279                         btrfs_wait_ordered_range(inode, start_pos,
1280                                                  last_pos - start_pos);
1281                         goto again;
1282                 }
1283                 if (ordered)
1284                         btrfs_put_ordered_extent(ordered);
1285
1286                 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
1287                                   last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1288                                   EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
1289                                   0, 0, &cached_state, GFP_NOFS);
1290                 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1291                                      start_pos, last_pos - 1, &cached_state,
1292                                      GFP_NOFS);
1293         }
1294         for (i = 0; i < num_pages; i++) {
1295                 if (clear_page_dirty_for_io(pages[i]))
1296                         account_page_redirty(pages[i]);
1297                 set_page_extent_mapped(pages[i]);
1298                 WARN_ON(!PageLocked(pages[i]));
1299         }
1300         return 0;
1301 fail:
1302         while (faili >= 0) {
1303                 unlock_page(pages[faili]);
1304                 page_cache_release(pages[faili]);
1305                 faili--;
1306         }
1307         return err;
1308
1309 }
1310
1311 static noinline ssize_t __btrfs_buffered_write(struct file *file,
1312                                                struct iov_iter *i,
1313                                                loff_t pos)
1314 {
1315         struct inode *inode = fdentry(file)->d_inode;
1316         struct btrfs_root *root = BTRFS_I(inode)->root;
1317         struct page **pages = NULL;
1318         unsigned long first_index;
1319         size_t num_written = 0;
1320         int nrptrs;
1321         int ret = 0;
1322         bool force_page_uptodate = false;
1323
1324         nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
1325                      PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1326                      (sizeof(struct page *)));
1327         nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1328         nrptrs = max(nrptrs, 8);
1329         pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
1330         if (!pages)
1331                 return -ENOMEM;
1332
1333         first_index = pos >> PAGE_CACHE_SHIFT;
1334
1335         while (iov_iter_count(i) > 0) {
1336                 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1337                 size_t write_bytes = min(iov_iter_count(i),
1338                                          nrptrs * (size_t)PAGE_CACHE_SIZE -
1339                                          offset);
1340                 size_t num_pages = (write_bytes + offset +
1341                                     PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1342                 size_t dirty_pages;
1343                 size_t copied;
1344
1345                 WARN_ON(num_pages > nrptrs);
1346
1347                 /*
1348                  * Fault pages before locking them in prepare_pages
1349                  * to avoid recursive lock
1350                  */
1351                 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
1352                         ret = -EFAULT;
1353                         break;
1354                 }
1355
1356                 ret = btrfs_delalloc_reserve_space(inode,
1357                                         num_pages << PAGE_CACHE_SHIFT);
1358                 if (ret)
1359                         break;
1360
1361                 /*
1362                  * This is going to setup the pages array with the number of
1363                  * pages we want, so we don't really need to worry about the
1364                  * contents of pages from loop to loop
1365                  */
1366                 ret = prepare_pages(root, file, pages, num_pages,
1367                                     pos, first_index, write_bytes,
1368                                     force_page_uptodate);
1369                 if (ret) {
1370                         btrfs_delalloc_release_space(inode,
1371                                         num_pages << PAGE_CACHE_SHIFT);
1372                         break;
1373                 }
1374
1375                 copied = btrfs_copy_from_user(pos, num_pages,
1376                                            write_bytes, pages, i);
1377
1378                 /*
1379                  * if we have trouble faulting in the pages, fall
1380                  * back to one page at a time
1381                  */
1382                 if (copied < write_bytes)
1383                         nrptrs = 1;
1384
1385                 if (copied == 0) {
1386                         force_page_uptodate = true;
1387                         dirty_pages = 0;
1388                 } else {
1389                         force_page_uptodate = false;
1390                         dirty_pages = (copied + offset +
1391                                        PAGE_CACHE_SIZE - 1) >>
1392                                        PAGE_CACHE_SHIFT;
1393                 }
1394
1395                 /*
1396                  * If we had a short copy we need to release the excess delaloc
1397                  * bytes we reserved.  We need to increment outstanding_extents
1398                  * because btrfs_delalloc_release_space will decrement it, but
1399                  * we still have an outstanding extent for the chunk we actually
1400                  * managed to copy.
1401                  */
1402                 if (num_pages > dirty_pages) {
1403                         if (copied > 0) {
1404                                 spin_lock(&BTRFS_I(inode)->lock);
1405                                 BTRFS_I(inode)->outstanding_extents++;
1406                                 spin_unlock(&BTRFS_I(inode)->lock);
1407                         }
1408                         btrfs_delalloc_release_space(inode,
1409                                         (num_pages - dirty_pages) <<
1410                                         PAGE_CACHE_SHIFT);
1411                 }
1412
1413                 if (copied > 0) {
1414                         ret = btrfs_dirty_pages(root, inode, pages,
1415                                                 dirty_pages, pos, copied,
1416                                                 NULL);
1417                         if (ret) {
1418                                 btrfs_delalloc_release_space(inode,
1419                                         dirty_pages << PAGE_CACHE_SHIFT);
1420                                 btrfs_drop_pages(pages, num_pages);
1421                                 break;
1422                         }
1423                 }
1424
1425                 btrfs_drop_pages(pages, num_pages);
1426
1427                 cond_resched();
1428
1429                 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1430                                                    dirty_pages);
1431                 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1432                         btrfs_btree_balance_dirty(root);
1433
1434                 pos += copied;
1435                 num_written += copied;
1436         }
1437
1438         kfree(pages);
1439
1440         return num_written ? num_written : ret;
1441 }
1442
1443 static ssize_t __btrfs_direct_write(struct kiocb *iocb,
1444                                     const struct iovec *iov,
1445                                     unsigned long nr_segs, loff_t pos,
1446                                     loff_t *ppos, size_t count, size_t ocount)
1447 {
1448         struct file *file = iocb->ki_filp;
1449         struct iov_iter i;
1450         ssize_t written;
1451         ssize_t written_buffered;
1452         loff_t endbyte;
1453         int err;
1454
1455         written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
1456                                             count, ocount);
1457
1458         if (written < 0 || written == count)
1459                 return written;
1460
1461         pos += written;
1462         count -= written;
1463         iov_iter_init(&i, iov, nr_segs, count, written);
1464         written_buffered = __btrfs_buffered_write(file, &i, pos);
1465         if (written_buffered < 0) {
1466                 err = written_buffered;
1467                 goto out;
1468         }
1469         endbyte = pos + written_buffered - 1;
1470         err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1471         if (err)
1472                 goto out;
1473         written += written_buffered;
1474         *ppos = pos + written_buffered;
1475         invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1476                                  endbyte >> PAGE_CACHE_SHIFT);
1477 out:
1478         return written ? written : err;
1479 }
1480
1481 static void update_time_for_write(struct inode *inode)
1482 {
1483         struct timespec now;
1484
1485         if (IS_NOCMTIME(inode))
1486                 return;
1487
1488         now = current_fs_time(inode->i_sb);
1489         if (!timespec_equal(&inode->i_mtime, &now))
1490                 inode->i_mtime = now;
1491
1492         if (!timespec_equal(&inode->i_ctime, &now))
1493                 inode->i_ctime = now;
1494
1495         if (IS_I_VERSION(inode))
1496                 inode_inc_iversion(inode);
1497 }
1498
1499 static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1500                                     const struct iovec *iov,
1501                                     unsigned long nr_segs, loff_t pos)
1502 {
1503         struct file *file = iocb->ki_filp;
1504         struct inode *inode = fdentry(file)->d_inode;
1505         struct btrfs_root *root = BTRFS_I(inode)->root;
1506         loff_t *ppos = &iocb->ki_pos;
1507         u64 start_pos;
1508         ssize_t num_written = 0;
1509         ssize_t err = 0;
1510         size_t count, ocount;
1511         bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
1512
1513         sb_start_write(inode->i_sb);
1514
1515         mutex_lock(&inode->i_mutex);
1516
1517         err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1518         if (err) {
1519                 mutex_unlock(&inode->i_mutex);
1520                 goto out;
1521         }
1522         count = ocount;
1523
1524         current->backing_dev_info = inode->i_mapping->backing_dev_info;
1525         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1526         if (err) {
1527                 mutex_unlock(&inode->i_mutex);
1528                 goto out;
1529         }
1530
1531         if (count == 0) {
1532                 mutex_unlock(&inode->i_mutex);
1533                 goto out;
1534         }
1535
1536         err = file_remove_suid(file);
1537         if (err) {
1538                 mutex_unlock(&inode->i_mutex);
1539                 goto out;
1540         }
1541
1542         /*
1543          * If BTRFS flips readonly due to some impossible error
1544          * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1545          * although we have opened a file as writable, we have
1546          * to stop this write operation to ensure FS consistency.
1547          */
1548         if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
1549                 mutex_unlock(&inode->i_mutex);
1550                 err = -EROFS;
1551                 goto out;
1552         }
1553
1554         /*
1555          * We reserve space for updating the inode when we reserve space for the
1556          * extent we are going to write, so we will enospc out there.  We don't
1557          * need to start yet another transaction to update the inode as we will
1558          * update the inode when we finish writing whatever data we write.
1559          */
1560         update_time_for_write(inode);
1561
1562         start_pos = round_down(pos, root->sectorsize);
1563         if (start_pos > i_size_read(inode)) {
1564                 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
1565                 if (err) {
1566                         mutex_unlock(&inode->i_mutex);
1567                         goto out;
1568                 }
1569         }
1570
1571         if (sync)
1572                 atomic_inc(&BTRFS_I(inode)->sync_writers);
1573
1574         if (unlikely(file->f_flags & O_DIRECT)) {
1575                 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1576                                                    pos, ppos, count, ocount);
1577         } else {
1578                 struct iov_iter i;
1579
1580                 iov_iter_init(&i, iov, nr_segs, count, num_written);
1581
1582                 num_written = __btrfs_buffered_write(file, &i, pos);
1583                 if (num_written > 0)
1584                         *ppos = pos + num_written;
1585         }
1586
1587         mutex_unlock(&inode->i_mutex);
1588
1589         /*
1590          * we want to make sure fsync finds this change
1591          * but we haven't joined a transaction running right now.
1592          *
1593          * Later on, someone is sure to update the inode and get the
1594          * real transid recorded.
1595          *
1596          * We set last_trans now to the fs_info generation + 1,
1597          * this will either be one more than the running transaction
1598          * or the generation used for the next transaction if there isn't
1599          * one running right now.
1600          *
1601          * We also have to set last_sub_trans to the current log transid,
1602          * otherwise subsequent syncs to a file that's been synced in this
1603          * transaction will appear to have already occured.
1604          */
1605         BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1606         BTRFS_I(inode)->last_sub_trans = root->log_transid;
1607         if (num_written > 0 || num_written == -EIOCBQUEUED) {
1608                 err = generic_write_sync(file, pos, num_written);
1609                 if (err < 0 && num_written > 0)
1610                         num_written = err;
1611         }
1612
1613         if (sync)
1614                 atomic_dec(&BTRFS_I(inode)->sync_writers);
1615 out:
1616         sb_end_write(inode->i_sb);
1617         current->backing_dev_info = NULL;
1618         return num_written ? num_written : err;
1619 }
1620
1621 int btrfs_release_file(struct inode *inode, struct file *filp)
1622 {
1623         /*
1624          * ordered_data_close is set by settattr when we are about to truncate
1625          * a file from a non-zero size to a zero size.  This tries to
1626          * flush down new bytes that may have been written if the
1627          * application were using truncate to replace a file in place.
1628          */
1629         if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1630                                &BTRFS_I(inode)->runtime_flags)) {
1631                 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1632                 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1633                         filemap_flush(inode->i_mapping);
1634         }
1635         if (filp->private_data)
1636                 btrfs_ioctl_trans_end(filp);
1637         return 0;
1638 }
1639
1640 /*
1641  * fsync call for both files and directories.  This logs the inode into
1642  * the tree log instead of forcing full commits whenever possible.
1643  *
1644  * It needs to call filemap_fdatawait so that all ordered extent updates are
1645  * in the metadata btree are up to date for copying to the log.
1646  *
1647  * It drops the inode mutex before doing the tree log commit.  This is an
1648  * important optimization for directories because holding the mutex prevents
1649  * new operations on the dir while we write to disk.
1650  */
1651 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1652 {
1653         struct dentry *dentry = file->f_path.dentry;
1654         struct inode *inode = dentry->d_inode;
1655         struct btrfs_root *root = BTRFS_I(inode)->root;
1656         int ret = 0;
1657         struct btrfs_trans_handle *trans;
1658
1659         trace_btrfs_sync_file(file, datasync);
1660
1661         /*
1662          * We write the dirty pages in the range and wait until they complete
1663          * out of the ->i_mutex. If so, we can flush the dirty pages by
1664          * multi-task, and make the performance up.
1665          */
1666         atomic_inc(&BTRFS_I(inode)->sync_writers);
1667         ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
1668         atomic_dec(&BTRFS_I(inode)->sync_writers);
1669         if (ret)
1670                 return ret;
1671
1672         mutex_lock(&inode->i_mutex);
1673
1674         /*
1675          * We flush the dirty pages again to avoid some dirty pages in the
1676          * range being left.
1677          */
1678         atomic_inc(&root->log_batch);
1679         btrfs_wait_ordered_range(inode, start, end - start + 1);
1680         atomic_inc(&root->log_batch);
1681
1682         /*
1683          * check the transaction that last modified this inode
1684          * and see if its already been committed
1685          */
1686         if (!BTRFS_I(inode)->last_trans) {
1687                 mutex_unlock(&inode->i_mutex);
1688                 goto out;
1689         }
1690
1691         /*
1692          * if the last transaction that changed this file was before
1693          * the current transaction, we can bail out now without any
1694          * syncing
1695          */
1696         smp_mb();
1697         if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
1698             BTRFS_I(inode)->last_trans <=
1699             root->fs_info->last_trans_committed) {
1700                 BTRFS_I(inode)->last_trans = 0;
1701
1702                 /*
1703                  * We'v had everything committed since the last time we were
1704                  * modified so clear this flag in case it was set for whatever
1705                  * reason, it's no longer relevant.
1706                  */
1707                 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1708                           &BTRFS_I(inode)->runtime_flags);
1709                 mutex_unlock(&inode->i_mutex);
1710                 goto out;
1711         }
1712
1713         /*
1714          * ok we haven't committed the transaction yet, lets do a commit
1715          */
1716         if (file->private_data)
1717                 btrfs_ioctl_trans_end(file);
1718
1719         trans = btrfs_start_transaction(root, 0);
1720         if (IS_ERR(trans)) {
1721                 ret = PTR_ERR(trans);
1722                 mutex_unlock(&inode->i_mutex);
1723                 goto out;
1724         }
1725
1726         ret = btrfs_log_dentry_safe(trans, root, dentry);
1727         if (ret < 0) {
1728                 mutex_unlock(&inode->i_mutex);
1729                 goto out;
1730         }
1731
1732         /* we've logged all the items and now have a consistent
1733          * version of the file in the log.  It is possible that
1734          * someone will come in and modify the file, but that's
1735          * fine because the log is consistent on disk, and we
1736          * have references to all of the file's extents
1737          *
1738          * It is possible that someone will come in and log the
1739          * file again, but that will end up using the synchronization
1740          * inside btrfs_sync_log to keep things safe.
1741          */
1742         mutex_unlock(&inode->i_mutex);
1743
1744         if (ret != BTRFS_NO_LOG_SYNC) {
1745                 if (ret > 0) {
1746                         ret = btrfs_commit_transaction(trans, root);
1747                 } else {
1748                         ret = btrfs_sync_log(trans, root);
1749                         if (ret == 0)
1750                                 ret = btrfs_end_transaction(trans, root);
1751                         else
1752                                 ret = btrfs_commit_transaction(trans, root);
1753                 }
1754         } else {
1755                 ret = btrfs_end_transaction(trans, root);
1756         }
1757 out:
1758         return ret > 0 ? -EIO : ret;
1759 }
1760
1761 static const struct vm_operations_struct btrfs_file_vm_ops = {
1762         .fault          = filemap_fault,
1763         .page_mkwrite   = btrfs_page_mkwrite,
1764         .remap_pages    = generic_file_remap_pages,
1765 };
1766
1767 static int btrfs_file_mmap(struct file  *filp, struct vm_area_struct *vma)
1768 {
1769         struct address_space *mapping = filp->f_mapping;
1770
1771         if (!mapping->a_ops->readpage)
1772                 return -ENOEXEC;
1773
1774         file_accessed(filp);
1775         vma->vm_ops = &btrfs_file_vm_ops;
1776
1777         return 0;
1778 }
1779
1780 static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
1781                           int slot, u64 start, u64 end)
1782 {
1783         struct btrfs_file_extent_item *fi;
1784         struct btrfs_key key;
1785
1786         if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1787                 return 0;
1788
1789         btrfs_item_key_to_cpu(leaf, &key, slot);
1790         if (key.objectid != btrfs_ino(inode) ||
1791             key.type != BTRFS_EXTENT_DATA_KEY)
1792                 return 0;
1793
1794         fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1795
1796         if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1797                 return 0;
1798
1799         if (btrfs_file_extent_disk_bytenr(leaf, fi))
1800                 return 0;
1801
1802         if (key.offset == end)
1803                 return 1;
1804         if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
1805                 return 1;
1806         return 0;
1807 }
1808
1809 static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
1810                       struct btrfs_path *path, u64 offset, u64 end)
1811 {
1812         struct btrfs_root *root = BTRFS_I(inode)->root;
1813         struct extent_buffer *leaf;
1814         struct btrfs_file_extent_item *fi;
1815         struct extent_map *hole_em;
1816         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1817         struct btrfs_key key;
1818         int ret;
1819
1820         key.objectid = btrfs_ino(inode);
1821         key.type = BTRFS_EXTENT_DATA_KEY;
1822         key.offset = offset;
1823
1824
1825         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1826         if (ret < 0)
1827                 return ret;
1828         BUG_ON(!ret);
1829
1830         leaf = path->nodes[0];
1831         if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
1832                 u64 num_bytes;
1833
1834                 path->slots[0]--;
1835                 fi = btrfs_item_ptr(leaf, path->slots[0],
1836                                     struct btrfs_file_extent_item);
1837                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
1838                         end - offset;
1839                 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1840                 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1841                 btrfs_set_file_extent_offset(leaf, fi, 0);
1842                 btrfs_mark_buffer_dirty(leaf);
1843                 goto out;
1844         }
1845
1846         if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
1847                 u64 num_bytes;
1848
1849                 path->slots[0]++;
1850                 key.offset = offset;
1851                 btrfs_set_item_key_safe(trans, root, path, &key);
1852                 fi = btrfs_item_ptr(leaf, path->slots[0],
1853                                     struct btrfs_file_extent_item);
1854                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
1855                         offset;
1856                 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1857                 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1858                 btrfs_set_file_extent_offset(leaf, fi, 0);
1859                 btrfs_mark_buffer_dirty(leaf);
1860                 goto out;
1861         }
1862         btrfs_release_path(path);
1863
1864         ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
1865                                        0, 0, end - offset, 0, end - offset,
1866                                        0, 0, 0);
1867         if (ret)
1868                 return ret;
1869
1870 out:
1871         btrfs_release_path(path);
1872
1873         hole_em = alloc_extent_map();
1874         if (!hole_em) {
1875                 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1876                 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1877                         &BTRFS_I(inode)->runtime_flags);
1878         } else {
1879                 hole_em->start = offset;
1880                 hole_em->len = end - offset;
1881                 hole_em->orig_start = offset;
1882
1883                 hole_em->block_start = EXTENT_MAP_HOLE;
1884                 hole_em->block_len = 0;
1885                 hole_em->orig_block_len = 0;
1886                 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
1887                 hole_em->compress_type = BTRFS_COMPRESS_NONE;
1888                 hole_em->generation = trans->transid;
1889
1890                 do {
1891                         btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1892                         write_lock(&em_tree->lock);
1893                         ret = add_extent_mapping(em_tree, hole_em);
1894                         if (!ret)
1895                                 list_move(&hole_em->list,
1896                                           &em_tree->modified_extents);
1897                         write_unlock(&em_tree->lock);
1898                 } while (ret == -EEXIST);
1899                 free_extent_map(hole_em);
1900                 if (ret)
1901                         set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1902                                 &BTRFS_I(inode)->runtime_flags);
1903         }
1904
1905         return 0;
1906 }
1907
1908 static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1909 {
1910         struct btrfs_root *root = BTRFS_I(inode)->root;
1911         struct extent_state *cached_state = NULL;
1912         struct btrfs_path *path;
1913         struct btrfs_block_rsv *rsv;
1914         struct btrfs_trans_handle *trans;
1915         u64 lockstart = round_up(offset, BTRFS_I(inode)->root->sectorsize);
1916         u64 lockend = round_down(offset + len,
1917                                  BTRFS_I(inode)->root->sectorsize) - 1;
1918         u64 cur_offset = lockstart;
1919         u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
1920         u64 drop_end;
1921         int ret = 0;
1922         int err = 0;
1923         bool same_page = ((offset >> PAGE_CACHE_SHIFT) ==
1924                           ((offset + len - 1) >> PAGE_CACHE_SHIFT));
1925
1926         btrfs_wait_ordered_range(inode, offset, len);
1927
1928         mutex_lock(&inode->i_mutex);
1929         /*
1930          * We needn't truncate any page which is beyond the end of the file
1931          * because we are sure there is no data there.
1932          */
1933         /*
1934          * Only do this if we are in the same page and we aren't doing the
1935          * entire page.
1936          */
1937         if (same_page && len < PAGE_CACHE_SIZE) {
1938                 if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE))
1939                         ret = btrfs_truncate_page(inode, offset, len, 0);
1940                 mutex_unlock(&inode->i_mutex);
1941                 return ret;
1942         }
1943
1944         /* zero back part of the first page */
1945         if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
1946                 ret = btrfs_truncate_page(inode, offset, 0, 0);
1947                 if (ret) {
1948                         mutex_unlock(&inode->i_mutex);
1949                         return ret;
1950                 }
1951         }
1952
1953         /* zero the front end of the last page */
1954         if (offset + len < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
1955                 ret = btrfs_truncate_page(inode, offset + len, 0, 1);
1956                 if (ret) {
1957                         mutex_unlock(&inode->i_mutex);
1958                         return ret;
1959                 }
1960         }
1961
1962         if (lockend < lockstart) {
1963                 mutex_unlock(&inode->i_mutex);
1964                 return 0;
1965         }
1966
1967         while (1) {
1968                 struct btrfs_ordered_extent *ordered;
1969
1970                 truncate_pagecache_range(inode, lockstart, lockend);
1971
1972                 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1973                                  0, &cached_state);
1974                 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
1975
1976                 /*
1977                  * We need to make sure we have no ordered extents in this range
1978                  * and nobody raced in and read a page in this range, if we did
1979                  * we need to try again.
1980                  */
1981                 if ((!ordered ||
1982                     (ordered->file_offset + ordered->len < lockstart ||
1983                      ordered->file_offset > lockend)) &&
1984                      !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
1985                                      lockend, EXTENT_UPTODATE, 0,
1986                                      cached_state)) {
1987                         if (ordered)
1988                                 btrfs_put_ordered_extent(ordered);
1989                         break;
1990                 }
1991                 if (ordered)
1992                         btrfs_put_ordered_extent(ordered);
1993                 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
1994                                      lockend, &cached_state, GFP_NOFS);
1995                 btrfs_wait_ordered_range(inode, lockstart,
1996                                          lockend - lockstart + 1);
1997         }
1998
1999         path = btrfs_alloc_path();
2000         if (!path) {
2001                 ret = -ENOMEM;
2002                 goto out;
2003         }
2004
2005         rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
2006         if (!rsv) {
2007                 ret = -ENOMEM;
2008                 goto out_free;
2009         }
2010         rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
2011         rsv->failfast = 1;
2012
2013         /*
2014          * 1 - update the inode
2015          * 1 - removing the extents in the range
2016          * 1 - adding the hole extent
2017          */
2018         trans = btrfs_start_transaction(root, 3);
2019         if (IS_ERR(trans)) {
2020                 err = PTR_ERR(trans);
2021                 goto out_free;
2022         }
2023
2024         ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
2025                                       min_size);
2026         BUG_ON(ret);
2027         trans->block_rsv = rsv;
2028
2029         while (cur_offset < lockend) {
2030                 ret = __btrfs_drop_extents(trans, root, inode, path,
2031                                            cur_offset, lockend + 1,
2032                                            &drop_end, 1);
2033                 if (ret != -ENOSPC)
2034                         break;
2035
2036                 trans->block_rsv = &root->fs_info->trans_block_rsv;
2037
2038                 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2039                 if (ret) {
2040                         err = ret;
2041                         break;
2042                 }
2043
2044                 cur_offset = drop_end;
2045
2046                 ret = btrfs_update_inode(trans, root, inode);
2047                 if (ret) {
2048                         err = ret;
2049                         break;
2050                 }
2051
2052                 btrfs_end_transaction(trans, root);
2053                 btrfs_btree_balance_dirty(root);
2054
2055                 trans = btrfs_start_transaction(root, 3);
2056                 if (IS_ERR(trans)) {
2057                         ret = PTR_ERR(trans);
2058                         trans = NULL;
2059                         break;
2060                 }
2061
2062                 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
2063                                               rsv, min_size);
2064                 BUG_ON(ret);    /* shouldn't happen */
2065                 trans->block_rsv = rsv;
2066         }
2067
2068         if (ret) {
2069                 err = ret;
2070                 goto out_trans;
2071         }
2072
2073         trans->block_rsv = &root->fs_info->trans_block_rsv;
2074         ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2075         if (ret) {
2076                 err = ret;
2077                 goto out_trans;
2078         }
2079
2080 out_trans:
2081         if (!trans)
2082                 goto out_free;
2083
2084         inode_inc_iversion(inode);
2085         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2086
2087         trans->block_rsv = &root->fs_info->trans_block_rsv;
2088         ret = btrfs_update_inode(trans, root, inode);
2089         btrfs_end_transaction(trans, root);
2090         btrfs_btree_balance_dirty(root);
2091 out_free:
2092         btrfs_free_path(path);
2093         btrfs_free_block_rsv(root, rsv);
2094 out:
2095         unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2096                              &cached_state, GFP_NOFS);
2097         mutex_unlock(&inode->i_mutex);
2098         if (ret && !err)
2099                 err = ret;
2100         return err;
2101 }
2102
2103 static long btrfs_fallocate(struct file *file, int mode,
2104                             loff_t offset, loff_t len)
2105 {
2106         struct inode *inode = file->f_path.dentry->d_inode;
2107         struct extent_state *cached_state = NULL;
2108         u64 cur_offset;
2109         u64 last_byte;
2110         u64 alloc_start;
2111         u64 alloc_end;
2112         u64 alloc_hint = 0;
2113         u64 locked_end;
2114         struct extent_map *em;
2115         int blocksize = BTRFS_I(inode)->root->sectorsize;
2116         int ret;
2117
2118         alloc_start = round_down(offset, blocksize);
2119         alloc_end = round_up(offset + len, blocksize);
2120
2121         /* Make sure we aren't being give some crap mode */
2122         if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2123                 return -EOPNOTSUPP;
2124
2125         if (mode & FALLOC_FL_PUNCH_HOLE)
2126                 return btrfs_punch_hole(inode, offset, len);
2127
2128         /*
2129          * Make sure we have enough space before we do the
2130          * allocation.
2131          */
2132         ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
2133         if (ret)
2134                 return ret;
2135
2136         /*
2137          * wait for ordered IO before we have any locks.  We'll loop again
2138          * below with the locks held.
2139          */
2140         btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
2141
2142         mutex_lock(&inode->i_mutex);
2143         ret = inode_newsize_ok(inode, alloc_end);
2144         if (ret)
2145                 goto out;
2146
2147         if (alloc_start > inode->i_size) {
2148                 ret = btrfs_cont_expand(inode, i_size_read(inode),
2149                                         alloc_start);
2150                 if (ret)
2151                         goto out;
2152         }
2153
2154         locked_end = alloc_end - 1;
2155         while (1) {
2156                 struct btrfs_ordered_extent *ordered;
2157
2158                 /* the extent lock is ordered inside the running
2159                  * transaction
2160                  */
2161                 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
2162                                  locked_end, 0, &cached_state);
2163                 ordered = btrfs_lookup_first_ordered_extent(inode,
2164                                                             alloc_end - 1);
2165                 if (ordered &&
2166                     ordered->file_offset + ordered->len > alloc_start &&
2167                     ordered->file_offset < alloc_end) {
2168                         btrfs_put_ordered_extent(ordered);
2169                         unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2170                                              alloc_start, locked_end,
2171                                              &cached_state, GFP_NOFS);
2172                         /*
2173                          * we can't wait on the range with the transaction
2174                          * running or with the extent lock held
2175                          */
2176                         btrfs_wait_ordered_range(inode, alloc_start,
2177                                                  alloc_end - alloc_start);
2178                 } else {
2179                         if (ordered)
2180                                 btrfs_put_ordered_extent(ordered);
2181                         break;
2182                 }
2183         }
2184
2185         cur_offset = alloc_start;
2186         while (1) {
2187                 u64 actual_end;
2188
2189                 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2190                                       alloc_end - cur_offset, 0);
2191                 if (IS_ERR_OR_NULL(em)) {
2192                         if (!em)
2193                                 ret = -ENOMEM;
2194                         else
2195                                 ret = PTR_ERR(em);
2196                         break;
2197                 }
2198                 last_byte = min(extent_map_end(em), alloc_end);
2199                 actual_end = min_t(u64, extent_map_end(em), offset + len);
2200                 last_byte = ALIGN(last_byte, blocksize);
2201
2202                 if (em->block_start == EXTENT_MAP_HOLE ||
2203                     (cur_offset >= inode->i_size &&
2204                      !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2205                         ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2206                                                         last_byte - cur_offset,
2207                                                         1 << inode->i_blkbits,
2208                                                         offset + len,
2209                                                         &alloc_hint);
2210
2211                         if (ret < 0) {
2212                                 free_extent_map(em);
2213                                 break;
2214                         }
2215                 } else if (actual_end > inode->i_size &&
2216                            !(mode & FALLOC_FL_KEEP_SIZE)) {
2217                         /*
2218                          * We didn't need to allocate any more space, but we
2219                          * still extended the size of the file so we need to
2220                          * update i_size.
2221                          */
2222                         inode->i_ctime = CURRENT_TIME;
2223                         i_size_write(inode, actual_end);
2224                         btrfs_ordered_update_i_size(inode, actual_end, NULL);
2225                 }
2226                 free_extent_map(em);
2227
2228                 cur_offset = last_byte;
2229                 if (cur_offset >= alloc_end) {
2230                         ret = 0;
2231                         break;
2232                 }
2233         }
2234         unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2235                              &cached_state, GFP_NOFS);
2236 out:
2237         mutex_unlock(&inode->i_mutex);
2238         /* Let go of our reservation. */
2239         btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
2240         return ret;
2241 }
2242
2243 static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
2244 {
2245         struct btrfs_root *root = BTRFS_I(inode)->root;
2246         struct extent_map *em;
2247         struct extent_state *cached_state = NULL;
2248         u64 lockstart = *offset;
2249         u64 lockend = i_size_read(inode);
2250         u64 start = *offset;
2251         u64 orig_start = *offset;
2252         u64 len = i_size_read(inode);
2253         u64 last_end = 0;
2254         int ret = 0;
2255
2256         lockend = max_t(u64, root->sectorsize, lockend);
2257         if (lockend <= lockstart)
2258                 lockend = lockstart + root->sectorsize;
2259
2260         lockend--;
2261         len = lockend - lockstart + 1;
2262
2263         len = max_t(u64, len, root->sectorsize);
2264         if (inode->i_size == 0)
2265                 return -ENXIO;
2266
2267         lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
2268                          &cached_state);
2269
2270         /*
2271          * Delalloc is such a pain.  If we have a hole and we have pending
2272          * delalloc for a portion of the hole we will get back a hole that
2273          * exists for the entire range since it hasn't been actually written
2274          * yet.  So to take care of this case we need to look for an extent just
2275          * before the position we want in case there is outstanding delalloc
2276          * going on here.
2277          */
2278         if (origin == SEEK_HOLE && start != 0) {
2279                 if (start <= root->sectorsize)
2280                         em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
2281                                                      root->sectorsize, 0);
2282                 else
2283                         em = btrfs_get_extent_fiemap(inode, NULL, 0,
2284                                                      start - root->sectorsize,
2285                                                      root->sectorsize, 0);
2286                 if (IS_ERR(em)) {
2287                         ret = PTR_ERR(em);
2288                         goto out;
2289                 }
2290                 last_end = em->start + em->len;
2291                 if (em->block_start == EXTENT_MAP_DELALLOC)
2292                         last_end = min_t(u64, last_end, inode->i_size);
2293                 free_extent_map(em);
2294         }
2295
2296         while (1) {
2297                 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2298                 if (IS_ERR(em)) {
2299                         ret = PTR_ERR(em);
2300                         break;
2301                 }
2302
2303                 if (em->block_start == EXTENT_MAP_HOLE) {
2304                         if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2305                                 if (last_end <= orig_start) {
2306                                         free_extent_map(em);
2307                                         ret = -ENXIO;
2308                                         break;
2309                                 }
2310                         }
2311
2312                         if (origin == SEEK_HOLE) {
2313                                 *offset = start;
2314                                 free_extent_map(em);
2315                                 break;
2316                         }
2317                 } else {
2318                         if (origin == SEEK_DATA) {
2319                                 if (em->block_start == EXTENT_MAP_DELALLOC) {
2320                                         if (start >= inode->i_size) {
2321                                                 free_extent_map(em);
2322                                                 ret = -ENXIO;
2323                                                 break;
2324                                         }
2325                                 }
2326
2327                                 if (!test_bit(EXTENT_FLAG_PREALLOC,
2328                                               &em->flags)) {
2329                                         *offset = start;
2330                                         free_extent_map(em);
2331                                         break;
2332                                 }
2333                         }
2334                 }
2335
2336                 start = em->start + em->len;
2337                 last_end = em->start + em->len;
2338
2339                 if (em->block_start == EXTENT_MAP_DELALLOC)
2340                         last_end = min_t(u64, last_end, inode->i_size);
2341
2342                 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2343                         free_extent_map(em);
2344                         ret = -ENXIO;
2345                         break;
2346                 }
2347                 free_extent_map(em);
2348                 cond_resched();
2349         }
2350         if (!ret)
2351                 *offset = min(*offset, inode->i_size);
2352 out:
2353         unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2354                              &cached_state, GFP_NOFS);
2355         return ret;
2356 }
2357
2358 static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
2359 {
2360         struct inode *inode = file->f_mapping->host;
2361         int ret;
2362
2363         mutex_lock(&inode->i_mutex);
2364         switch (origin) {
2365         case SEEK_END:
2366         case SEEK_CUR:
2367                 offset = generic_file_llseek(file, offset, origin);
2368                 goto out;
2369         case SEEK_DATA:
2370         case SEEK_HOLE:
2371                 if (offset >= i_size_read(inode)) {
2372                         mutex_unlock(&inode->i_mutex);
2373                         return -ENXIO;
2374                 }
2375
2376                 ret = find_desired_extent(inode, &offset, origin);
2377                 if (ret) {
2378                         mutex_unlock(&inode->i_mutex);
2379                         return ret;
2380                 }
2381         }
2382
2383         if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
2384                 offset = -EINVAL;
2385                 goto out;
2386         }
2387         if (offset > inode->i_sb->s_maxbytes) {
2388                 offset = -EINVAL;
2389                 goto out;
2390         }
2391
2392         /* Special lock needed here? */
2393         if (offset != file->f_pos) {
2394                 file->f_pos = offset;
2395                 file->f_version = 0;
2396         }
2397 out:
2398         mutex_unlock(&inode->i_mutex);
2399         return offset;
2400 }
2401
2402 const struct file_operations btrfs_file_operations = {
2403         .llseek         = btrfs_file_llseek,
2404         .read           = do_sync_read,
2405         .write          = do_sync_write,
2406         .aio_read       = generic_file_aio_read,
2407         .splice_read    = generic_file_splice_read,
2408         .aio_write      = btrfs_file_aio_write,
2409         .mmap           = btrfs_file_mmap,
2410         .open           = generic_file_open,
2411         .release        = btrfs_release_file,
2412         .fsync          = btrfs_sync_file,
2413         .fallocate      = btrfs_fallocate,
2414         .unlocked_ioctl = btrfs_ioctl,
2415 #ifdef CONFIG_COMPAT
2416         .compat_ioctl   = btrfs_ioctl,
2417 #endif
2418 };
2419
2420 void btrfs_auto_defrag_exit(void)
2421 {
2422         if (btrfs_inode_defrag_cachep)
2423                 kmem_cache_destroy(btrfs_inode_defrag_cachep);
2424 }
2425
2426 int btrfs_auto_defrag_init(void)
2427 {
2428         btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
2429                                         sizeof(struct inode_defrag), 0,
2430                                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2431                                         NULL);
2432         if (!btrfs_inode_defrag_cachep)
2433                 return -ENOMEM;
2434
2435         return 0;
2436 }