Btrfs: skip writeback of last page when truncating file to same size
[sfrench/cifs-2.6.git] / fs / btrfs / inode.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/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/xattr.h>
36 #include <linux/posix_acl.h>
37 #include <linux/falloc.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/mount.h>
41 #include <linux/btrfs.h>
42 #include <linux/blkdev.h>
43 #include <linux/posix_acl_xattr.h>
44 #include <linux/uio.h>
45 #include <linux/magic.h>
46 #include <linux/iversion.h>
47 #include "ctree.h"
48 #include "disk-io.h"
49 #include "transaction.h"
50 #include "btrfs_inode.h"
51 #include "print-tree.h"
52 #include "ordered-data.h"
53 #include "xattr.h"
54 #include "tree-log.h"
55 #include "volumes.h"
56 #include "compression.h"
57 #include "locking.h"
58 #include "free-space-cache.h"
59 #include "inode-map.h"
60 #include "backref.h"
61 #include "props.h"
62 #include "qgroup.h"
63 #include "dedupe.h"
64
65 struct btrfs_iget_args {
66         struct btrfs_key *location;
67         struct btrfs_root *root;
68 };
69
70 struct btrfs_dio_data {
71         u64 reserve;
72         u64 unsubmitted_oe_range_start;
73         u64 unsubmitted_oe_range_end;
74         int overwrite;
75 };
76
77 static const struct inode_operations btrfs_dir_inode_operations;
78 static const struct inode_operations btrfs_symlink_inode_operations;
79 static const struct inode_operations btrfs_dir_ro_inode_operations;
80 static const struct inode_operations btrfs_special_inode_operations;
81 static const struct inode_operations btrfs_file_inode_operations;
82 static const struct address_space_operations btrfs_aops;
83 static const struct address_space_operations btrfs_symlink_aops;
84 static const struct file_operations btrfs_dir_file_operations;
85 static const struct extent_io_ops btrfs_extent_io_ops;
86
87 static struct kmem_cache *btrfs_inode_cachep;
88 struct kmem_cache *btrfs_trans_handle_cachep;
89 struct kmem_cache *btrfs_path_cachep;
90 struct kmem_cache *btrfs_free_space_cachep;
91
92 #define S_SHIFT 12
93 static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
94         [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
95         [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
96         [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
97         [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
98         [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
99         [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
100         [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
101 };
102
103 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
104 static int btrfs_truncate(struct inode *inode, bool skip_writeback);
105 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
106 static noinline int cow_file_range(struct inode *inode,
107                                    struct page *locked_page,
108                                    u64 start, u64 end, u64 delalloc_end,
109                                    int *page_started, unsigned long *nr_written,
110                                    int unlock, struct btrfs_dedupe_hash *hash);
111 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112                                        u64 orig_start, u64 block_start,
113                                        u64 block_len, u64 orig_block_len,
114                                        u64 ram_bytes, int compress_type,
115                                        int type);
116
117 static void __endio_write_update_ordered(struct inode *inode,
118                                          const u64 offset, const u64 bytes,
119                                          const bool uptodate);
120
121 /*
122  * Cleanup all submitted ordered extents in specified range to handle errors
123  * from the fill_dellaloc() callback.
124  *
125  * NOTE: caller must ensure that when an error happens, it can not call
126  * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127  * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128  * to be released, which we want to happen only when finishing the ordered
129  * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130  * fill_delalloc() callback already does proper cleanup for the first page of
131  * the range, that is, it invokes the callback writepage_end_io_hook() for the
132  * range of the first page.
133  */
134 static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135                                                  const u64 offset,
136                                                  const u64 bytes)
137 {
138         unsigned long index = offset >> PAGE_SHIFT;
139         unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140         struct page *page;
141
142         while (index <= end_index) {
143                 page = find_get_page(inode->i_mapping, index);
144                 index++;
145                 if (!page)
146                         continue;
147                 ClearPagePrivate2(page);
148                 put_page(page);
149         }
150         return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151                                             bytes - PAGE_SIZE, false);
152 }
153
154 static int btrfs_dirty_inode(struct inode *inode);
155
156 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157 void btrfs_test_inode_set_ops(struct inode *inode)
158 {
159         BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160 }
161 #endif
162
163 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
164                                      struct inode *inode,  struct inode *dir,
165                                      const struct qstr *qstr)
166 {
167         int err;
168
169         err = btrfs_init_acl(trans, inode, dir);
170         if (!err)
171                 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
172         return err;
173 }
174
175 /*
176  * this does all the hard work for inserting an inline extent into
177  * the btree.  The caller should have done a btrfs_drop_extents so that
178  * no overlapping inline items exist in the btree
179  */
180 static int insert_inline_extent(struct btrfs_trans_handle *trans,
181                                 struct btrfs_path *path, int extent_inserted,
182                                 struct btrfs_root *root, struct inode *inode,
183                                 u64 start, size_t size, size_t compressed_size,
184                                 int compress_type,
185                                 struct page **compressed_pages)
186 {
187         struct extent_buffer *leaf;
188         struct page *page = NULL;
189         char *kaddr;
190         unsigned long ptr;
191         struct btrfs_file_extent_item *ei;
192         int ret;
193         size_t cur_size = size;
194         unsigned long offset;
195
196         if (compressed_size && compressed_pages)
197                 cur_size = compressed_size;
198
199         inode_add_bytes(inode, size);
200
201         if (!extent_inserted) {
202                 struct btrfs_key key;
203                 size_t datasize;
204
205                 key.objectid = btrfs_ino(BTRFS_I(inode));
206                 key.offset = start;
207                 key.type = BTRFS_EXTENT_DATA_KEY;
208
209                 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210                 path->leave_spinning = 1;
211                 ret = btrfs_insert_empty_item(trans, root, path, &key,
212                                               datasize);
213                 if (ret)
214                         goto fail;
215         }
216         leaf = path->nodes[0];
217         ei = btrfs_item_ptr(leaf, path->slots[0],
218                             struct btrfs_file_extent_item);
219         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220         btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221         btrfs_set_file_extent_encryption(leaf, ei, 0);
222         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223         btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224         ptr = btrfs_file_extent_inline_start(ei);
225
226         if (compress_type != BTRFS_COMPRESS_NONE) {
227                 struct page *cpage;
228                 int i = 0;
229                 while (compressed_size > 0) {
230                         cpage = compressed_pages[i];
231                         cur_size = min_t(unsigned long, compressed_size,
232                                        PAGE_SIZE);
233
234                         kaddr = kmap_atomic(cpage);
235                         write_extent_buffer(leaf, kaddr, ptr, cur_size);
236                         kunmap_atomic(kaddr);
237
238                         i++;
239                         ptr += cur_size;
240                         compressed_size -= cur_size;
241                 }
242                 btrfs_set_file_extent_compression(leaf, ei,
243                                                   compress_type);
244         } else {
245                 page = find_get_page(inode->i_mapping,
246                                      start >> PAGE_SHIFT);
247                 btrfs_set_file_extent_compression(leaf, ei, 0);
248                 kaddr = kmap_atomic(page);
249                 offset = start & (PAGE_SIZE - 1);
250                 write_extent_buffer(leaf, kaddr + offset, ptr, size);
251                 kunmap_atomic(kaddr);
252                 put_page(page);
253         }
254         btrfs_mark_buffer_dirty(leaf);
255         btrfs_release_path(path);
256
257         /*
258          * we're an inline extent, so nobody can
259          * extend the file past i_size without locking
260          * a page we already have locked.
261          *
262          * We must do any isize and inode updates
263          * before we unlock the pages.  Otherwise we
264          * could end up racing with unlink.
265          */
266         BTRFS_I(inode)->disk_i_size = inode->i_size;
267         ret = btrfs_update_inode(trans, root, inode);
268
269 fail:
270         return ret;
271 }
272
273
274 /*
275  * conditionally insert an inline extent into the file.  This
276  * does the checks required to make sure the data is small enough
277  * to fit as an inline extent.
278  */
279 static noinline int cow_file_range_inline(struct btrfs_root *root,
280                                           struct inode *inode, u64 start,
281                                           u64 end, size_t compressed_size,
282                                           int compress_type,
283                                           struct page **compressed_pages)
284 {
285         struct btrfs_fs_info *fs_info = root->fs_info;
286         struct btrfs_trans_handle *trans;
287         u64 isize = i_size_read(inode);
288         u64 actual_end = min(end + 1, isize);
289         u64 inline_len = actual_end - start;
290         u64 aligned_end = ALIGN(end, fs_info->sectorsize);
291         u64 data_len = inline_len;
292         int ret;
293         struct btrfs_path *path;
294         int extent_inserted = 0;
295         u32 extent_item_size;
296
297         if (compressed_size)
298                 data_len = compressed_size;
299
300         if (start > 0 ||
301             actual_end > fs_info->sectorsize ||
302             data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
303             (!compressed_size &&
304             (actual_end & (fs_info->sectorsize - 1)) == 0) ||
305             end + 1 < isize ||
306             data_len > fs_info->max_inline) {
307                 return 1;
308         }
309
310         path = btrfs_alloc_path();
311         if (!path)
312                 return -ENOMEM;
313
314         trans = btrfs_join_transaction(root);
315         if (IS_ERR(trans)) {
316                 btrfs_free_path(path);
317                 return PTR_ERR(trans);
318         }
319         trans->block_rsv = &BTRFS_I(inode)->block_rsv;
320
321         if (compressed_size && compressed_pages)
322                 extent_item_size = btrfs_file_extent_calc_inline_size(
323                    compressed_size);
324         else
325                 extent_item_size = btrfs_file_extent_calc_inline_size(
326                     inline_len);
327
328         ret = __btrfs_drop_extents(trans, root, inode, path,
329                                    start, aligned_end, NULL,
330                                    1, 1, extent_item_size, &extent_inserted);
331         if (ret) {
332                 btrfs_abort_transaction(trans, ret);
333                 goto out;
334         }
335
336         if (isize > actual_end)
337                 inline_len = min_t(u64, isize, actual_end);
338         ret = insert_inline_extent(trans, path, extent_inserted,
339                                    root, inode, start,
340                                    inline_len, compressed_size,
341                                    compress_type, compressed_pages);
342         if (ret && ret != -ENOSPC) {
343                 btrfs_abort_transaction(trans, ret);
344                 goto out;
345         } else if (ret == -ENOSPC) {
346                 ret = 1;
347                 goto out;
348         }
349
350         set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
351         btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
352 out:
353         /*
354          * Don't forget to free the reserved space, as for inlined extent
355          * it won't count as data extent, free them directly here.
356          * And at reserve time, it's always aligned to page size, so
357          * just free one page here.
358          */
359         btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
360         btrfs_free_path(path);
361         btrfs_end_transaction(trans);
362         return ret;
363 }
364
365 struct async_extent {
366         u64 start;
367         u64 ram_size;
368         u64 compressed_size;
369         struct page **pages;
370         unsigned long nr_pages;
371         int compress_type;
372         struct list_head list;
373 };
374
375 struct async_cow {
376         struct inode *inode;
377         struct btrfs_root *root;
378         struct page *locked_page;
379         u64 start;
380         u64 end;
381         unsigned int write_flags;
382         struct list_head extents;
383         struct btrfs_work work;
384 };
385
386 static noinline int add_async_extent(struct async_cow *cow,
387                                      u64 start, u64 ram_size,
388                                      u64 compressed_size,
389                                      struct page **pages,
390                                      unsigned long nr_pages,
391                                      int compress_type)
392 {
393         struct async_extent *async_extent;
394
395         async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
396         BUG_ON(!async_extent); /* -ENOMEM */
397         async_extent->start = start;
398         async_extent->ram_size = ram_size;
399         async_extent->compressed_size = compressed_size;
400         async_extent->pages = pages;
401         async_extent->nr_pages = nr_pages;
402         async_extent->compress_type = compress_type;
403         list_add_tail(&async_extent->list, &cow->extents);
404         return 0;
405 }
406
407 static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
408 {
409         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
410
411         /* force compress */
412         if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
413                 return 1;
414         /* defrag ioctl */
415         if (BTRFS_I(inode)->defrag_compress)
416                 return 1;
417         /* bad compression ratios */
418         if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419                 return 0;
420         if (btrfs_test_opt(fs_info, COMPRESS) ||
421             BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
422             BTRFS_I(inode)->prop_compress)
423                 return btrfs_compress_heuristic(inode, start, end);
424         return 0;
425 }
426
427 static inline void inode_should_defrag(struct btrfs_inode *inode,
428                 u64 start, u64 end, u64 num_bytes, u64 small_write)
429 {
430         /* If this is a small write inside eof, kick off a defrag */
431         if (num_bytes < small_write &&
432             (start > 0 || end + 1 < inode->disk_i_size))
433                 btrfs_add_inode_defrag(NULL, inode);
434 }
435
436 /*
437  * we create compressed extents in two phases.  The first
438  * phase compresses a range of pages that have already been
439  * locked (both pages and state bits are locked).
440  *
441  * This is done inside an ordered work queue, and the compression
442  * is spread across many cpus.  The actual IO submission is step
443  * two, and the ordered work queue takes care of making sure that
444  * happens in the same order things were put onto the queue by
445  * writepages and friends.
446  *
447  * If this code finds it can't get good compression, it puts an
448  * entry onto the work queue to write the uncompressed bytes.  This
449  * makes sure that both compressed inodes and uncompressed inodes
450  * are written in the same order that the flusher thread sent them
451  * down.
452  */
453 static noinline void compress_file_range(struct inode *inode,
454                                         struct page *locked_page,
455                                         u64 start, u64 end,
456                                         struct async_cow *async_cow,
457                                         int *num_added)
458 {
459         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
460         struct btrfs_root *root = BTRFS_I(inode)->root;
461         u64 blocksize = fs_info->sectorsize;
462         u64 actual_end;
463         u64 isize = i_size_read(inode);
464         int ret = 0;
465         struct page **pages = NULL;
466         unsigned long nr_pages;
467         unsigned long total_compressed = 0;
468         unsigned long total_in = 0;
469         int i;
470         int will_compress;
471         int compress_type = fs_info->compress_type;
472         int redirty = 0;
473
474         inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475                         SZ_16K);
476
477         actual_end = min_t(u64, isize, end + 1);
478 again:
479         will_compress = 0;
480         nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
481         BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482         nr_pages = min_t(unsigned long, nr_pages,
483                         BTRFS_MAX_COMPRESSED / PAGE_SIZE);
484
485         /*
486          * we don't want to send crud past the end of i_size through
487          * compression, that's just a waste of CPU time.  So, if the
488          * end of the file is before the start of our current
489          * requested range of bytes, we bail out to the uncompressed
490          * cleanup code that can deal with all of this.
491          *
492          * It isn't really the fastest way to fix things, but this is a
493          * very uncommon corner.
494          */
495         if (actual_end <= start)
496                 goto cleanup_and_bail_uncompressed;
497
498         total_compressed = actual_end - start;
499
500         /*
501          * skip compression for a small file range(<=blocksize) that
502          * isn't an inline extent, since it doesn't save disk space at all.
503          */
504         if (total_compressed <= blocksize &&
505            (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506                 goto cleanup_and_bail_uncompressed;
507
508         total_compressed = min_t(unsigned long, total_compressed,
509                         BTRFS_MAX_UNCOMPRESSED);
510         total_in = 0;
511         ret = 0;
512
513         /*
514          * we do compression for mount -o compress and when the
515          * inode has not been flagged as nocompress.  This flag can
516          * change at any time if we discover bad compression ratios.
517          */
518         if (inode_need_compress(inode, start, end)) {
519                 WARN_ON(pages);
520                 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
521                 if (!pages) {
522                         /* just bail out to the uncompressed code */
523                         goto cont;
524                 }
525
526                 if (BTRFS_I(inode)->defrag_compress)
527                         compress_type = BTRFS_I(inode)->defrag_compress;
528                 else if (BTRFS_I(inode)->prop_compress)
529                         compress_type = BTRFS_I(inode)->prop_compress;
530
531                 /*
532                  * we need to call clear_page_dirty_for_io on each
533                  * page in the range.  Otherwise applications with the file
534                  * mmap'd can wander in and change the page contents while
535                  * we are compressing them.
536                  *
537                  * If the compression fails for any reason, we set the pages
538                  * dirty again later on.
539                  *
540                  * Note that the remaining part is redirtied, the start pointer
541                  * has moved, the end is the original one.
542                  */
543                 if (!redirty) {
544                         extent_range_clear_dirty_for_io(inode, start, end);
545                         redirty = 1;
546                 }
547
548                 /* Compression level is applied here and only here */
549                 ret = btrfs_compress_pages(
550                         compress_type | (fs_info->compress_level << 4),
551                                            inode->i_mapping, start,
552                                            pages,
553                                            &nr_pages,
554                                            &total_in,
555                                            &total_compressed);
556
557                 if (!ret) {
558                         unsigned long offset = total_compressed &
559                                 (PAGE_SIZE - 1);
560                         struct page *page = pages[nr_pages - 1];
561                         char *kaddr;
562
563                         /* zero the tail end of the last page, we might be
564                          * sending it down to disk
565                          */
566                         if (offset) {
567                                 kaddr = kmap_atomic(page);
568                                 memset(kaddr + offset, 0,
569                                        PAGE_SIZE - offset);
570                                 kunmap_atomic(kaddr);
571                         }
572                         will_compress = 1;
573                 }
574         }
575 cont:
576         if (start == 0) {
577                 /* lets try to make an inline extent */
578                 if (ret || total_in < actual_end) {
579                         /* we didn't compress the entire range, try
580                          * to make an uncompressed inline extent.
581                          */
582                         ret = cow_file_range_inline(root, inode, start, end,
583                                             0, BTRFS_COMPRESS_NONE, NULL);
584                 } else {
585                         /* try making a compressed inline extent */
586                         ret = cow_file_range_inline(root, inode, start, end,
587                                                     total_compressed,
588                                                     compress_type, pages);
589                 }
590                 if (ret <= 0) {
591                         unsigned long clear_flags = EXTENT_DELALLOC |
592                                 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
593                                 EXTENT_DO_ACCOUNTING;
594                         unsigned long page_error_op;
595
596                         page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
597
598                         /*
599                          * inline extent creation worked or returned error,
600                          * we don't need to create any more async work items.
601                          * Unlock and free up our temp pages.
602                          *
603                          * We use DO_ACCOUNTING here because we need the
604                          * delalloc_release_metadata to be done _after_ we drop
605                          * our outstanding extent for clearing delalloc for this
606                          * range.
607                          */
608                         extent_clear_unlock_delalloc(inode, start, end, end,
609                                                      NULL, clear_flags,
610                                                      PAGE_UNLOCK |
611                                                      PAGE_CLEAR_DIRTY |
612                                                      PAGE_SET_WRITEBACK |
613                                                      page_error_op |
614                                                      PAGE_END_WRITEBACK);
615                         goto free_pages_out;
616                 }
617         }
618
619         if (will_compress) {
620                 /*
621                  * we aren't doing an inline extent round the compressed size
622                  * up to a block size boundary so the allocator does sane
623                  * things
624                  */
625                 total_compressed = ALIGN(total_compressed, blocksize);
626
627                 /*
628                  * one last check to make sure the compression is really a
629                  * win, compare the page count read with the blocks on disk,
630                  * compression must free at least one sector size
631                  */
632                 total_in = ALIGN(total_in, PAGE_SIZE);
633                 if (total_compressed + blocksize <= total_in) {
634                         *num_added += 1;
635
636                         /*
637                          * The async work queues will take care of doing actual
638                          * allocation on disk for these compressed pages, and
639                          * will submit them to the elevator.
640                          */
641                         add_async_extent(async_cow, start, total_in,
642                                         total_compressed, pages, nr_pages,
643                                         compress_type);
644
645                         if (start + total_in < end) {
646                                 start += total_in;
647                                 pages = NULL;
648                                 cond_resched();
649                                 goto again;
650                         }
651                         return;
652                 }
653         }
654         if (pages) {
655                 /*
656                  * the compression code ran but failed to make things smaller,
657                  * free any pages it allocated and our page pointer array
658                  */
659                 for (i = 0; i < nr_pages; i++) {
660                         WARN_ON(pages[i]->mapping);
661                         put_page(pages[i]);
662                 }
663                 kfree(pages);
664                 pages = NULL;
665                 total_compressed = 0;
666                 nr_pages = 0;
667
668                 /* flag the file so we don't compress in the future */
669                 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
670                     !(BTRFS_I(inode)->prop_compress)) {
671                         BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
672                 }
673         }
674 cleanup_and_bail_uncompressed:
675         /*
676          * No compression, but we still need to write the pages in the file
677          * we've been given so far.  redirty the locked page if it corresponds
678          * to our extent and set things up for the async work queue to run
679          * cow_file_range to do the normal delalloc dance.
680          */
681         if (page_offset(locked_page) >= start &&
682             page_offset(locked_page) <= end)
683                 __set_page_dirty_nobuffers(locked_page);
684                 /* unlocked later on in the async handlers */
685
686         if (redirty)
687                 extent_range_redirty_for_io(inode, start, end);
688         add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
689                          BTRFS_COMPRESS_NONE);
690         *num_added += 1;
691
692         return;
693
694 free_pages_out:
695         for (i = 0; i < nr_pages; i++) {
696                 WARN_ON(pages[i]->mapping);
697                 put_page(pages[i]);
698         }
699         kfree(pages);
700 }
701
702 static void free_async_extent_pages(struct async_extent *async_extent)
703 {
704         int i;
705
706         if (!async_extent->pages)
707                 return;
708
709         for (i = 0; i < async_extent->nr_pages; i++) {
710                 WARN_ON(async_extent->pages[i]->mapping);
711                 put_page(async_extent->pages[i]);
712         }
713         kfree(async_extent->pages);
714         async_extent->nr_pages = 0;
715         async_extent->pages = NULL;
716 }
717
718 /*
719  * phase two of compressed writeback.  This is the ordered portion
720  * of the code, which only gets called in the order the work was
721  * queued.  We walk all the async extents created by compress_file_range
722  * and send them down to the disk.
723  */
724 static noinline void submit_compressed_extents(struct inode *inode,
725                                               struct async_cow *async_cow)
726 {
727         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
728         struct async_extent *async_extent;
729         u64 alloc_hint = 0;
730         struct btrfs_key ins;
731         struct extent_map *em;
732         struct btrfs_root *root = BTRFS_I(inode)->root;
733         struct extent_io_tree *io_tree;
734         int ret = 0;
735
736 again:
737         while (!list_empty(&async_cow->extents)) {
738                 async_extent = list_entry(async_cow->extents.next,
739                                           struct async_extent, list);
740                 list_del(&async_extent->list);
741
742                 io_tree = &BTRFS_I(inode)->io_tree;
743
744 retry:
745                 /* did the compression code fall back to uncompressed IO? */
746                 if (!async_extent->pages) {
747                         int page_started = 0;
748                         unsigned long nr_written = 0;
749
750                         lock_extent(io_tree, async_extent->start,
751                                          async_extent->start +
752                                          async_extent->ram_size - 1);
753
754                         /* allocate blocks */
755                         ret = cow_file_range(inode, async_cow->locked_page,
756                                              async_extent->start,
757                                              async_extent->start +
758                                              async_extent->ram_size - 1,
759                                              async_extent->start +
760                                              async_extent->ram_size - 1,
761                                              &page_started, &nr_written, 0,
762                                              NULL);
763
764                         /* JDM XXX */
765
766                         /*
767                          * if page_started, cow_file_range inserted an
768                          * inline extent and took care of all the unlocking
769                          * and IO for us.  Otherwise, we need to submit
770                          * all those pages down to the drive.
771                          */
772                         if (!page_started && !ret)
773                                 extent_write_locked_range(inode,
774                                                   async_extent->start,
775                                                   async_extent->start +
776                                                   async_extent->ram_size - 1,
777                                                   WB_SYNC_ALL);
778                         else if (ret)
779                                 unlock_page(async_cow->locked_page);
780                         kfree(async_extent);
781                         cond_resched();
782                         continue;
783                 }
784
785                 lock_extent(io_tree, async_extent->start,
786                             async_extent->start + async_extent->ram_size - 1);
787
788                 ret = btrfs_reserve_extent(root, async_extent->ram_size,
789                                            async_extent->compressed_size,
790                                            async_extent->compressed_size,
791                                            0, alloc_hint, &ins, 1, 1);
792                 if (ret) {
793                         free_async_extent_pages(async_extent);
794
795                         if (ret == -ENOSPC) {
796                                 unlock_extent(io_tree, async_extent->start,
797                                               async_extent->start +
798                                               async_extent->ram_size - 1);
799
800                                 /*
801                                  * we need to redirty the pages if we decide to
802                                  * fallback to uncompressed IO, otherwise we
803                                  * will not submit these pages down to lower
804                                  * layers.
805                                  */
806                                 extent_range_redirty_for_io(inode,
807                                                 async_extent->start,
808                                                 async_extent->start +
809                                                 async_extent->ram_size - 1);
810
811                                 goto retry;
812                         }
813                         goto out_free;
814                 }
815                 /*
816                  * here we're doing allocation and writeback of the
817                  * compressed pages
818                  */
819                 em = create_io_em(inode, async_extent->start,
820                                   async_extent->ram_size, /* len */
821                                   async_extent->start, /* orig_start */
822                                   ins.objectid, /* block_start */
823                                   ins.offset, /* block_len */
824                                   ins.offset, /* orig_block_len */
825                                   async_extent->ram_size, /* ram_bytes */
826                                   async_extent->compress_type,
827                                   BTRFS_ORDERED_COMPRESSED);
828                 if (IS_ERR(em))
829                         /* ret value is not necessary due to void function */
830                         goto out_free_reserve;
831                 free_extent_map(em);
832
833                 ret = btrfs_add_ordered_extent_compress(inode,
834                                                 async_extent->start,
835                                                 ins.objectid,
836                                                 async_extent->ram_size,
837                                                 ins.offset,
838                                                 BTRFS_ORDERED_COMPRESSED,
839                                                 async_extent->compress_type);
840                 if (ret) {
841                         btrfs_drop_extent_cache(BTRFS_I(inode),
842                                                 async_extent->start,
843                                                 async_extent->start +
844                                                 async_extent->ram_size - 1, 0);
845                         goto out_free_reserve;
846                 }
847                 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
848
849                 /*
850                  * clear dirty, set writeback and unlock the pages.
851                  */
852                 extent_clear_unlock_delalloc(inode, async_extent->start,
853                                 async_extent->start +
854                                 async_extent->ram_size - 1,
855                                 async_extent->start +
856                                 async_extent->ram_size - 1,
857                                 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
858                                 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
859                                 PAGE_SET_WRITEBACK);
860                 if (btrfs_submit_compressed_write(inode,
861                                     async_extent->start,
862                                     async_extent->ram_size,
863                                     ins.objectid,
864                                     ins.offset, async_extent->pages,
865                                     async_extent->nr_pages,
866                                     async_cow->write_flags)) {
867                         struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
868                         struct page *p = async_extent->pages[0];
869                         const u64 start = async_extent->start;
870                         const u64 end = start + async_extent->ram_size - 1;
871
872                         p->mapping = inode->i_mapping;
873                         tree->ops->writepage_end_io_hook(p, start, end,
874                                                          NULL, 0);
875                         p->mapping = NULL;
876                         extent_clear_unlock_delalloc(inode, start, end, end,
877                                                      NULL, 0,
878                                                      PAGE_END_WRITEBACK |
879                                                      PAGE_SET_ERROR);
880                         free_async_extent_pages(async_extent);
881                 }
882                 alloc_hint = ins.objectid + ins.offset;
883                 kfree(async_extent);
884                 cond_resched();
885         }
886         return;
887 out_free_reserve:
888         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
889         btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
890 out_free:
891         extent_clear_unlock_delalloc(inode, async_extent->start,
892                                      async_extent->start +
893                                      async_extent->ram_size - 1,
894                                      async_extent->start +
895                                      async_extent->ram_size - 1,
896                                      NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
897                                      EXTENT_DELALLOC_NEW |
898                                      EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
899                                      PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
900                                      PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
901                                      PAGE_SET_ERROR);
902         free_async_extent_pages(async_extent);
903         kfree(async_extent);
904         goto again;
905 }
906
907 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
908                                       u64 num_bytes)
909 {
910         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
911         struct extent_map *em;
912         u64 alloc_hint = 0;
913
914         read_lock(&em_tree->lock);
915         em = search_extent_mapping(em_tree, start, num_bytes);
916         if (em) {
917                 /*
918                  * if block start isn't an actual block number then find the
919                  * first block in this inode and use that as a hint.  If that
920                  * block is also bogus then just don't worry about it.
921                  */
922                 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
923                         free_extent_map(em);
924                         em = search_extent_mapping(em_tree, 0, 0);
925                         if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
926                                 alloc_hint = em->block_start;
927                         if (em)
928                                 free_extent_map(em);
929                 } else {
930                         alloc_hint = em->block_start;
931                         free_extent_map(em);
932                 }
933         }
934         read_unlock(&em_tree->lock);
935
936         return alloc_hint;
937 }
938
939 /*
940  * when extent_io.c finds a delayed allocation range in the file,
941  * the call backs end up in this code.  The basic idea is to
942  * allocate extents on disk for the range, and create ordered data structs
943  * in ram to track those extents.
944  *
945  * locked_page is the page that writepage had locked already.  We use
946  * it to make sure we don't do extra locks or unlocks.
947  *
948  * *page_started is set to one if we unlock locked_page and do everything
949  * required to start IO on it.  It may be clean and already done with
950  * IO when we return.
951  */
952 static noinline int cow_file_range(struct inode *inode,
953                                    struct page *locked_page,
954                                    u64 start, u64 end, u64 delalloc_end,
955                                    int *page_started, unsigned long *nr_written,
956                                    int unlock, struct btrfs_dedupe_hash *hash)
957 {
958         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
959         struct btrfs_root *root = BTRFS_I(inode)->root;
960         u64 alloc_hint = 0;
961         u64 num_bytes;
962         unsigned long ram_size;
963         u64 cur_alloc_size = 0;
964         u64 blocksize = fs_info->sectorsize;
965         struct btrfs_key ins;
966         struct extent_map *em;
967         unsigned clear_bits;
968         unsigned long page_ops;
969         bool extent_reserved = false;
970         int ret = 0;
971
972         if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
973                 WARN_ON_ONCE(1);
974                 ret = -EINVAL;
975                 goto out_unlock;
976         }
977
978         num_bytes = ALIGN(end - start + 1, blocksize);
979         num_bytes = max(blocksize,  num_bytes);
980         ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
981
982         inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
983
984         if (start == 0) {
985                 /* lets try to make an inline extent */
986                 ret = cow_file_range_inline(root, inode, start, end, 0,
987                                         BTRFS_COMPRESS_NONE, NULL);
988                 if (ret == 0) {
989                         /*
990                          * We use DO_ACCOUNTING here because we need the
991                          * delalloc_release_metadata to be run _after_ we drop
992                          * our outstanding extent for clearing delalloc for this
993                          * range.
994                          */
995                         extent_clear_unlock_delalloc(inode, start, end,
996                                      delalloc_end, NULL,
997                                      EXTENT_LOCKED | EXTENT_DELALLOC |
998                                      EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
999                                      EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1000                                      PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1001                                      PAGE_END_WRITEBACK);
1002                         *nr_written = *nr_written +
1003                              (end - start + PAGE_SIZE) / PAGE_SIZE;
1004                         *page_started = 1;
1005                         goto out;
1006                 } else if (ret < 0) {
1007                         goto out_unlock;
1008                 }
1009         }
1010
1011         alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1012         btrfs_drop_extent_cache(BTRFS_I(inode), start,
1013                         start + num_bytes - 1, 0);
1014
1015         while (num_bytes > 0) {
1016                 cur_alloc_size = num_bytes;
1017                 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1018                                            fs_info->sectorsize, 0, alloc_hint,
1019                                            &ins, 1, 1);
1020                 if (ret < 0)
1021                         goto out_unlock;
1022                 cur_alloc_size = ins.offset;
1023                 extent_reserved = true;
1024
1025                 ram_size = ins.offset;
1026                 em = create_io_em(inode, start, ins.offset, /* len */
1027                                   start, /* orig_start */
1028                                   ins.objectid, /* block_start */
1029                                   ins.offset, /* block_len */
1030                                   ins.offset, /* orig_block_len */
1031                                   ram_size, /* ram_bytes */
1032                                   BTRFS_COMPRESS_NONE, /* compress_type */
1033                                   BTRFS_ORDERED_REGULAR /* type */);
1034                 if (IS_ERR(em))
1035                         goto out_reserve;
1036                 free_extent_map(em);
1037
1038                 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1039                                                ram_size, cur_alloc_size, 0);
1040                 if (ret)
1041                         goto out_drop_extent_cache;
1042
1043                 if (root->root_key.objectid ==
1044                     BTRFS_DATA_RELOC_TREE_OBJECTID) {
1045                         ret = btrfs_reloc_clone_csums(inode, start,
1046                                                       cur_alloc_size);
1047                         /*
1048                          * Only drop cache here, and process as normal.
1049                          *
1050                          * We must not allow extent_clear_unlock_delalloc()
1051                          * at out_unlock label to free meta of this ordered
1052                          * extent, as its meta should be freed by
1053                          * btrfs_finish_ordered_io().
1054                          *
1055                          * So we must continue until @start is increased to
1056                          * skip current ordered extent.
1057                          */
1058                         if (ret)
1059                                 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1060                                                 start + ram_size - 1, 0);
1061                 }
1062
1063                 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1064
1065                 /* we're not doing compressed IO, don't unlock the first
1066                  * page (which the caller expects to stay locked), don't
1067                  * clear any dirty bits and don't set any writeback bits
1068                  *
1069                  * Do set the Private2 bit so we know this page was properly
1070                  * setup for writepage
1071                  */
1072                 page_ops = unlock ? PAGE_UNLOCK : 0;
1073                 page_ops |= PAGE_SET_PRIVATE2;
1074
1075                 extent_clear_unlock_delalloc(inode, start,
1076                                              start + ram_size - 1,
1077                                              delalloc_end, locked_page,
1078                                              EXTENT_LOCKED | EXTENT_DELALLOC,
1079                                              page_ops);
1080                 if (num_bytes < cur_alloc_size)
1081                         num_bytes = 0;
1082                 else
1083                         num_bytes -= cur_alloc_size;
1084                 alloc_hint = ins.objectid + ins.offset;
1085                 start += cur_alloc_size;
1086                 extent_reserved = false;
1087
1088                 /*
1089                  * btrfs_reloc_clone_csums() error, since start is increased
1090                  * extent_clear_unlock_delalloc() at out_unlock label won't
1091                  * free metadata of current ordered extent, we're OK to exit.
1092                  */
1093                 if (ret)
1094                         goto out_unlock;
1095         }
1096 out:
1097         return ret;
1098
1099 out_drop_extent_cache:
1100         btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
1101 out_reserve:
1102         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1103         btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1104 out_unlock:
1105         clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1106                 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1107         page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1108                 PAGE_END_WRITEBACK;
1109         /*
1110          * If we reserved an extent for our delalloc range (or a subrange) and
1111          * failed to create the respective ordered extent, then it means that
1112          * when we reserved the extent we decremented the extent's size from
1113          * the data space_info's bytes_may_use counter and incremented the
1114          * space_info's bytes_reserved counter by the same amount. We must make
1115          * sure extent_clear_unlock_delalloc() does not try to decrement again
1116          * the data space_info's bytes_may_use counter, therefore we do not pass
1117          * it the flag EXTENT_CLEAR_DATA_RESV.
1118          */
1119         if (extent_reserved) {
1120                 extent_clear_unlock_delalloc(inode, start,
1121                                              start + cur_alloc_size,
1122                                              start + cur_alloc_size,
1123                                              locked_page,
1124                                              clear_bits,
1125                                              page_ops);
1126                 start += cur_alloc_size;
1127                 if (start >= end)
1128                         goto out;
1129         }
1130         extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1131                                      locked_page,
1132                                      clear_bits | EXTENT_CLEAR_DATA_RESV,
1133                                      page_ops);
1134         goto out;
1135 }
1136
1137 /*
1138  * work queue call back to started compression on a file and pages
1139  */
1140 static noinline void async_cow_start(struct btrfs_work *work)
1141 {
1142         struct async_cow *async_cow;
1143         int num_added = 0;
1144         async_cow = container_of(work, struct async_cow, work);
1145
1146         compress_file_range(async_cow->inode, async_cow->locked_page,
1147                             async_cow->start, async_cow->end, async_cow,
1148                             &num_added);
1149         if (num_added == 0) {
1150                 btrfs_add_delayed_iput(async_cow->inode);
1151                 async_cow->inode = NULL;
1152         }
1153 }
1154
1155 /*
1156  * work queue call back to submit previously compressed pages
1157  */
1158 static noinline void async_cow_submit(struct btrfs_work *work)
1159 {
1160         struct btrfs_fs_info *fs_info;
1161         struct async_cow *async_cow;
1162         struct btrfs_root *root;
1163         unsigned long nr_pages;
1164
1165         async_cow = container_of(work, struct async_cow, work);
1166
1167         root = async_cow->root;
1168         fs_info = root->fs_info;
1169         nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1170                 PAGE_SHIFT;
1171
1172         /*
1173          * atomic_sub_return implies a barrier for waitqueue_active
1174          */
1175         if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1176             5 * SZ_1M &&
1177             waitqueue_active(&fs_info->async_submit_wait))
1178                 wake_up(&fs_info->async_submit_wait);
1179
1180         if (async_cow->inode)
1181                 submit_compressed_extents(async_cow->inode, async_cow);
1182 }
1183
1184 static noinline void async_cow_free(struct btrfs_work *work)
1185 {
1186         struct async_cow *async_cow;
1187         async_cow = container_of(work, struct async_cow, work);
1188         if (async_cow->inode)
1189                 btrfs_add_delayed_iput(async_cow->inode);
1190         kfree(async_cow);
1191 }
1192
1193 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1194                                 u64 start, u64 end, int *page_started,
1195                                 unsigned long *nr_written,
1196                                 unsigned int write_flags)
1197 {
1198         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1199         struct async_cow *async_cow;
1200         struct btrfs_root *root = BTRFS_I(inode)->root;
1201         unsigned long nr_pages;
1202         u64 cur_end;
1203
1204         clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1205                          1, 0, NULL);
1206         while (start < end) {
1207                 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1208                 BUG_ON(!async_cow); /* -ENOMEM */
1209                 async_cow->inode = igrab(inode);
1210                 async_cow->root = root;
1211                 async_cow->locked_page = locked_page;
1212                 async_cow->start = start;
1213                 async_cow->write_flags = write_flags;
1214
1215                 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1216                     !btrfs_test_opt(fs_info, FORCE_COMPRESS))
1217                         cur_end = end;
1218                 else
1219                         cur_end = min(end, start + SZ_512K - 1);
1220
1221                 async_cow->end = cur_end;
1222                 INIT_LIST_HEAD(&async_cow->extents);
1223
1224                 btrfs_init_work(&async_cow->work,
1225                                 btrfs_delalloc_helper,
1226                                 async_cow_start, async_cow_submit,
1227                                 async_cow_free);
1228
1229                 nr_pages = (cur_end - start + PAGE_SIZE) >>
1230                         PAGE_SHIFT;
1231                 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1232
1233                 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
1234
1235                 *nr_written += nr_pages;
1236                 start = cur_end + 1;
1237         }
1238         *page_started = 1;
1239         return 0;
1240 }
1241
1242 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1243                                         u64 bytenr, u64 num_bytes)
1244 {
1245         int ret;
1246         struct btrfs_ordered_sum *sums;
1247         LIST_HEAD(list);
1248
1249         ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1250                                        bytenr + num_bytes - 1, &list, 0);
1251         if (ret == 0 && list_empty(&list))
1252                 return 0;
1253
1254         while (!list_empty(&list)) {
1255                 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1256                 list_del(&sums->list);
1257                 kfree(sums);
1258         }
1259         if (ret < 0)
1260                 return ret;
1261         return 1;
1262 }
1263
1264 /*
1265  * when nowcow writeback call back.  This checks for snapshots or COW copies
1266  * of the extents that exist in the file, and COWs the file as required.
1267  *
1268  * If no cow copies or snapshots exist, we write directly to the existing
1269  * blocks on disk
1270  */
1271 static noinline int run_delalloc_nocow(struct inode *inode,
1272                                        struct page *locked_page,
1273                               u64 start, u64 end, int *page_started, int force,
1274                               unsigned long *nr_written)
1275 {
1276         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1277         struct btrfs_root *root = BTRFS_I(inode)->root;
1278         struct extent_buffer *leaf;
1279         struct btrfs_path *path;
1280         struct btrfs_file_extent_item *fi;
1281         struct btrfs_key found_key;
1282         struct extent_map *em;
1283         u64 cow_start;
1284         u64 cur_offset;
1285         u64 extent_end;
1286         u64 extent_offset;
1287         u64 disk_bytenr;
1288         u64 num_bytes;
1289         u64 disk_num_bytes;
1290         u64 ram_bytes;
1291         int extent_type;
1292         int ret, err;
1293         int type;
1294         int nocow;
1295         int check_prev = 1;
1296         bool nolock;
1297         u64 ino = btrfs_ino(BTRFS_I(inode));
1298
1299         path = btrfs_alloc_path();
1300         if (!path) {
1301                 extent_clear_unlock_delalloc(inode, start, end, end,
1302                                              locked_page,
1303                                              EXTENT_LOCKED | EXTENT_DELALLOC |
1304                                              EXTENT_DO_ACCOUNTING |
1305                                              EXTENT_DEFRAG, PAGE_UNLOCK |
1306                                              PAGE_CLEAR_DIRTY |
1307                                              PAGE_SET_WRITEBACK |
1308                                              PAGE_END_WRITEBACK);
1309                 return -ENOMEM;
1310         }
1311
1312         nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
1313
1314         cow_start = (u64)-1;
1315         cur_offset = start;
1316         while (1) {
1317                 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1318                                                cur_offset, 0);
1319                 if (ret < 0)
1320                         goto error;
1321                 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1322                         leaf = path->nodes[0];
1323                         btrfs_item_key_to_cpu(leaf, &found_key,
1324                                               path->slots[0] - 1);
1325                         if (found_key.objectid == ino &&
1326                             found_key.type == BTRFS_EXTENT_DATA_KEY)
1327                                 path->slots[0]--;
1328                 }
1329                 check_prev = 0;
1330 next_slot:
1331                 leaf = path->nodes[0];
1332                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1333                         ret = btrfs_next_leaf(root, path);
1334                         if (ret < 0) {
1335                                 if (cow_start != (u64)-1)
1336                                         cur_offset = cow_start;
1337                                 goto error;
1338                         }
1339                         if (ret > 0)
1340                                 break;
1341                         leaf = path->nodes[0];
1342                 }
1343
1344                 nocow = 0;
1345                 disk_bytenr = 0;
1346                 num_bytes = 0;
1347                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1348
1349                 if (found_key.objectid > ino)
1350                         break;
1351                 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1352                     found_key.type < BTRFS_EXTENT_DATA_KEY) {
1353                         path->slots[0]++;
1354                         goto next_slot;
1355                 }
1356                 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1357                     found_key.offset > end)
1358                         break;
1359
1360                 if (found_key.offset > cur_offset) {
1361                         extent_end = found_key.offset;
1362                         extent_type = 0;
1363                         goto out_check;
1364                 }
1365
1366                 fi = btrfs_item_ptr(leaf, path->slots[0],
1367                                     struct btrfs_file_extent_item);
1368                 extent_type = btrfs_file_extent_type(leaf, fi);
1369
1370                 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1371                 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1372                     extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1373                         disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1374                         extent_offset = btrfs_file_extent_offset(leaf, fi);
1375                         extent_end = found_key.offset +
1376                                 btrfs_file_extent_num_bytes(leaf, fi);
1377                         disk_num_bytes =
1378                                 btrfs_file_extent_disk_num_bytes(leaf, fi);
1379                         if (extent_end <= start) {
1380                                 path->slots[0]++;
1381                                 goto next_slot;
1382                         }
1383                         if (disk_bytenr == 0)
1384                                 goto out_check;
1385                         if (btrfs_file_extent_compression(leaf, fi) ||
1386                             btrfs_file_extent_encryption(leaf, fi) ||
1387                             btrfs_file_extent_other_encoding(leaf, fi))
1388                                 goto out_check;
1389                         if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1390                                 goto out_check;
1391                         if (btrfs_extent_readonly(fs_info, disk_bytenr))
1392                                 goto out_check;
1393                         ret = btrfs_cross_ref_exist(root, ino,
1394                                                     found_key.offset -
1395                                                     extent_offset, disk_bytenr);
1396                         if (ret) {
1397                                 /*
1398                                  * ret could be -EIO if the above fails to read
1399                                  * metadata.
1400                                  */
1401                                 if (ret < 0) {
1402                                         if (cow_start != (u64)-1)
1403                                                 cur_offset = cow_start;
1404                                         goto error;
1405                                 }
1406
1407                                 WARN_ON_ONCE(nolock);
1408                                 goto out_check;
1409                         }
1410                         disk_bytenr += extent_offset;
1411                         disk_bytenr += cur_offset - found_key.offset;
1412                         num_bytes = min(end + 1, extent_end) - cur_offset;
1413                         /*
1414                          * if there are pending snapshots for this root,
1415                          * we fall into common COW way.
1416                          */
1417                         if (!nolock) {
1418                                 err = btrfs_start_write_no_snapshotting(root);
1419                                 if (!err)
1420                                         goto out_check;
1421                         }
1422                         /*
1423                          * force cow if csum exists in the range.
1424                          * this ensure that csum for a given extent are
1425                          * either valid or do not exist.
1426                          */
1427                         ret = csum_exist_in_range(fs_info, disk_bytenr,
1428                                                   num_bytes);
1429                         if (ret) {
1430                                 if (!nolock)
1431                                         btrfs_end_write_no_snapshotting(root);
1432
1433                                 /*
1434                                  * ret could be -EIO if the above fails to read
1435                                  * metadata.
1436                                  */
1437                                 if (ret < 0) {
1438                                         if (cow_start != (u64)-1)
1439                                                 cur_offset = cow_start;
1440                                         goto error;
1441                                 }
1442                                 WARN_ON_ONCE(nolock);
1443                                 goto out_check;
1444                         }
1445                         if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1446                                 if (!nolock)
1447                                         btrfs_end_write_no_snapshotting(root);
1448                                 goto out_check;
1449                         }
1450                         nocow = 1;
1451                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1452                         extent_end = found_key.offset +
1453                                 btrfs_file_extent_inline_len(leaf,
1454                                                      path->slots[0], fi);
1455                         extent_end = ALIGN(extent_end,
1456                                            fs_info->sectorsize);
1457                 } else {
1458                         BUG_ON(1);
1459                 }
1460 out_check:
1461                 if (extent_end <= start) {
1462                         path->slots[0]++;
1463                         if (!nolock && nocow)
1464                                 btrfs_end_write_no_snapshotting(root);
1465                         if (nocow)
1466                                 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1467                         goto next_slot;
1468                 }
1469                 if (!nocow) {
1470                         if (cow_start == (u64)-1)
1471                                 cow_start = cur_offset;
1472                         cur_offset = extent_end;
1473                         if (cur_offset > end)
1474                                 break;
1475                         path->slots[0]++;
1476                         goto next_slot;
1477                 }
1478
1479                 btrfs_release_path(path);
1480                 if (cow_start != (u64)-1) {
1481                         ret = cow_file_range(inode, locked_page,
1482                                              cow_start, found_key.offset - 1,
1483                                              end, page_started, nr_written, 1,
1484                                              NULL);
1485                         if (ret) {
1486                                 if (!nolock && nocow)
1487                                         btrfs_end_write_no_snapshotting(root);
1488                                 if (nocow)
1489                                         btrfs_dec_nocow_writers(fs_info,
1490                                                                 disk_bytenr);
1491                                 goto error;
1492                         }
1493                         cow_start = (u64)-1;
1494                 }
1495
1496                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1497                         u64 orig_start = found_key.offset - extent_offset;
1498
1499                         em = create_io_em(inode, cur_offset, num_bytes,
1500                                           orig_start,
1501                                           disk_bytenr, /* block_start */
1502                                           num_bytes, /* block_len */
1503                                           disk_num_bytes, /* orig_block_len */
1504                                           ram_bytes, BTRFS_COMPRESS_NONE,
1505                                           BTRFS_ORDERED_PREALLOC);
1506                         if (IS_ERR(em)) {
1507                                 if (!nolock && nocow)
1508                                         btrfs_end_write_no_snapshotting(root);
1509                                 if (nocow)
1510                                         btrfs_dec_nocow_writers(fs_info,
1511                                                                 disk_bytenr);
1512                                 ret = PTR_ERR(em);
1513                                 goto error;
1514                         }
1515                         free_extent_map(em);
1516                 }
1517
1518                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1519                         type = BTRFS_ORDERED_PREALLOC;
1520                 } else {
1521                         type = BTRFS_ORDERED_NOCOW;
1522                 }
1523
1524                 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1525                                                num_bytes, num_bytes, type);
1526                 if (nocow)
1527                         btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1528                 BUG_ON(ret); /* -ENOMEM */
1529
1530                 if (root->root_key.objectid ==
1531                     BTRFS_DATA_RELOC_TREE_OBJECTID)
1532                         /*
1533                          * Error handled later, as we must prevent
1534                          * extent_clear_unlock_delalloc() in error handler
1535                          * from freeing metadata of created ordered extent.
1536                          */
1537                         ret = btrfs_reloc_clone_csums(inode, cur_offset,
1538                                                       num_bytes);
1539
1540                 extent_clear_unlock_delalloc(inode, cur_offset,
1541                                              cur_offset + num_bytes - 1, end,
1542                                              locked_page, EXTENT_LOCKED |
1543                                              EXTENT_DELALLOC |
1544                                              EXTENT_CLEAR_DATA_RESV,
1545                                              PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1546
1547                 if (!nolock && nocow)
1548                         btrfs_end_write_no_snapshotting(root);
1549                 cur_offset = extent_end;
1550
1551                 /*
1552                  * btrfs_reloc_clone_csums() error, now we're OK to call error
1553                  * handler, as metadata for created ordered extent will only
1554                  * be freed by btrfs_finish_ordered_io().
1555                  */
1556                 if (ret)
1557                         goto error;
1558                 if (cur_offset > end)
1559                         break;
1560         }
1561         btrfs_release_path(path);
1562
1563         if (cur_offset <= end && cow_start == (u64)-1) {
1564                 cow_start = cur_offset;
1565                 cur_offset = end;
1566         }
1567
1568         if (cow_start != (u64)-1) {
1569                 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1570                                      page_started, nr_written, 1, NULL);
1571                 if (ret)
1572                         goto error;
1573         }
1574
1575 error:
1576         if (ret && cur_offset < end)
1577                 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
1578                                              locked_page, EXTENT_LOCKED |
1579                                              EXTENT_DELALLOC | EXTENT_DEFRAG |
1580                                              EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1581                                              PAGE_CLEAR_DIRTY |
1582                                              PAGE_SET_WRITEBACK |
1583                                              PAGE_END_WRITEBACK);
1584         btrfs_free_path(path);
1585         return ret;
1586 }
1587
1588 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1589 {
1590
1591         if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1592             !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1593                 return 0;
1594
1595         /*
1596          * @defrag_bytes is a hint value, no spinlock held here,
1597          * if is not zero, it means the file is defragging.
1598          * Force cow if given extent needs to be defragged.
1599          */
1600         if (BTRFS_I(inode)->defrag_bytes &&
1601             test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1602                            EXTENT_DEFRAG, 0, NULL))
1603                 return 1;
1604
1605         return 0;
1606 }
1607
1608 /*
1609  * extent_io.c call back to do delayed allocation processing
1610  */
1611 static int run_delalloc_range(void *private_data, struct page *locked_page,
1612                               u64 start, u64 end, int *page_started,
1613                               unsigned long *nr_written,
1614                               struct writeback_control *wbc)
1615 {
1616         struct inode *inode = private_data;
1617         int ret;
1618         int force_cow = need_force_cow(inode, start, end);
1619         unsigned int write_flags = wbc_to_write_flags(wbc);
1620
1621         if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1622                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1623                                          page_started, 1, nr_written);
1624         } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1625                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1626                                          page_started, 0, nr_written);
1627         } else if (!inode_need_compress(inode, start, end)) {
1628                 ret = cow_file_range(inode, locked_page, start, end, end,
1629                                       page_started, nr_written, 1, NULL);
1630         } else {
1631                 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1632                         &BTRFS_I(inode)->runtime_flags);
1633                 ret = cow_file_range_async(inode, locked_page, start, end,
1634                                            page_started, nr_written,
1635                                            write_flags);
1636         }
1637         if (ret)
1638                 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
1639         return ret;
1640 }
1641
1642 static void btrfs_split_extent_hook(void *private_data,
1643                                     struct extent_state *orig, u64 split)
1644 {
1645         struct inode *inode = private_data;
1646         u64 size;
1647
1648         /* not delalloc, ignore it */
1649         if (!(orig->state & EXTENT_DELALLOC))
1650                 return;
1651
1652         size = orig->end - orig->start + 1;
1653         if (size > BTRFS_MAX_EXTENT_SIZE) {
1654                 u32 num_extents;
1655                 u64 new_size;
1656
1657                 /*
1658                  * See the explanation in btrfs_merge_extent_hook, the same
1659                  * applies here, just in reverse.
1660                  */
1661                 new_size = orig->end - split + 1;
1662                 num_extents = count_max_extents(new_size);
1663                 new_size = split - orig->start;
1664                 num_extents += count_max_extents(new_size);
1665                 if (count_max_extents(size) >= num_extents)
1666                         return;
1667         }
1668
1669         spin_lock(&BTRFS_I(inode)->lock);
1670         btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1671         spin_unlock(&BTRFS_I(inode)->lock);
1672 }
1673
1674 /*
1675  * extent_io.c merge_extent_hook, used to track merged delayed allocation
1676  * extents so we can keep track of new extents that are just merged onto old
1677  * extents, such as when we are doing sequential writes, so we can properly
1678  * account for the metadata space we'll need.
1679  */
1680 static void btrfs_merge_extent_hook(void *private_data,
1681                                     struct extent_state *new,
1682                                     struct extent_state *other)
1683 {
1684         struct inode *inode = private_data;
1685         u64 new_size, old_size;
1686         u32 num_extents;
1687
1688         /* not delalloc, ignore it */
1689         if (!(other->state & EXTENT_DELALLOC))
1690                 return;
1691
1692         if (new->start > other->start)
1693                 new_size = new->end - other->start + 1;
1694         else
1695                 new_size = other->end - new->start + 1;
1696
1697         /* we're not bigger than the max, unreserve the space and go */
1698         if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1699                 spin_lock(&BTRFS_I(inode)->lock);
1700                 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1701                 spin_unlock(&BTRFS_I(inode)->lock);
1702                 return;
1703         }
1704
1705         /*
1706          * We have to add up either side to figure out how many extents were
1707          * accounted for before we merged into one big extent.  If the number of
1708          * extents we accounted for is <= the amount we need for the new range
1709          * then we can return, otherwise drop.  Think of it like this
1710          *
1711          * [ 4k][MAX_SIZE]
1712          *
1713          * So we've grown the extent by a MAX_SIZE extent, this would mean we
1714          * need 2 outstanding extents, on one side we have 1 and the other side
1715          * we have 1 so they are == and we can return.  But in this case
1716          *
1717          * [MAX_SIZE+4k][MAX_SIZE+4k]
1718          *
1719          * Each range on their own accounts for 2 extents, but merged together
1720          * they are only 3 extents worth of accounting, so we need to drop in
1721          * this case.
1722          */
1723         old_size = other->end - other->start + 1;
1724         num_extents = count_max_extents(old_size);
1725         old_size = new->end - new->start + 1;
1726         num_extents += count_max_extents(old_size);
1727         if (count_max_extents(new_size) >= num_extents)
1728                 return;
1729
1730         spin_lock(&BTRFS_I(inode)->lock);
1731         btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1732         spin_unlock(&BTRFS_I(inode)->lock);
1733 }
1734
1735 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1736                                       struct inode *inode)
1737 {
1738         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1739
1740         spin_lock(&root->delalloc_lock);
1741         if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1742                 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1743                               &root->delalloc_inodes);
1744                 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1745                         &BTRFS_I(inode)->runtime_flags);
1746                 root->nr_delalloc_inodes++;
1747                 if (root->nr_delalloc_inodes == 1) {
1748                         spin_lock(&fs_info->delalloc_root_lock);
1749                         BUG_ON(!list_empty(&root->delalloc_root));
1750                         list_add_tail(&root->delalloc_root,
1751                                       &fs_info->delalloc_roots);
1752                         spin_unlock(&fs_info->delalloc_root_lock);
1753                 }
1754         }
1755         spin_unlock(&root->delalloc_lock);
1756 }
1757
1758 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1759                                      struct btrfs_inode *inode)
1760 {
1761         struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1762
1763         spin_lock(&root->delalloc_lock);
1764         if (!list_empty(&inode->delalloc_inodes)) {
1765                 list_del_init(&inode->delalloc_inodes);
1766                 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1767                           &inode->runtime_flags);
1768                 root->nr_delalloc_inodes--;
1769                 if (!root->nr_delalloc_inodes) {
1770                         spin_lock(&fs_info->delalloc_root_lock);
1771                         BUG_ON(list_empty(&root->delalloc_root));
1772                         list_del_init(&root->delalloc_root);
1773                         spin_unlock(&fs_info->delalloc_root_lock);
1774                 }
1775         }
1776         spin_unlock(&root->delalloc_lock);
1777 }
1778
1779 /*
1780  * extent_io.c set_bit_hook, used to track delayed allocation
1781  * bytes in this file, and to maintain the list of inodes that
1782  * have pending delalloc work to be done.
1783  */
1784 static void btrfs_set_bit_hook(void *private_data,
1785                                struct extent_state *state, unsigned *bits)
1786 {
1787         struct inode *inode = private_data;
1788
1789         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1790
1791         if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1792                 WARN_ON(1);
1793         /*
1794          * set_bit and clear bit hooks normally require _irqsave/restore
1795          * but in this case, we are only testing for the DELALLOC
1796          * bit, which is only set or cleared with irqs on
1797          */
1798         if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1799                 struct btrfs_root *root = BTRFS_I(inode)->root;
1800                 u64 len = state->end + 1 - state->start;
1801                 u32 num_extents = count_max_extents(len);
1802                 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
1803
1804                 spin_lock(&BTRFS_I(inode)->lock);
1805                 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1806                 spin_unlock(&BTRFS_I(inode)->lock);
1807
1808                 /* For sanity tests */
1809                 if (btrfs_is_testing(fs_info))
1810                         return;
1811
1812                 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1813                                          fs_info->delalloc_batch);
1814                 spin_lock(&BTRFS_I(inode)->lock);
1815                 BTRFS_I(inode)->delalloc_bytes += len;
1816                 if (*bits & EXTENT_DEFRAG)
1817                         BTRFS_I(inode)->defrag_bytes += len;
1818                 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1819                                          &BTRFS_I(inode)->runtime_flags))
1820                         btrfs_add_delalloc_inodes(root, inode);
1821                 spin_unlock(&BTRFS_I(inode)->lock);
1822         }
1823
1824         if (!(state->state & EXTENT_DELALLOC_NEW) &&
1825             (*bits & EXTENT_DELALLOC_NEW)) {
1826                 spin_lock(&BTRFS_I(inode)->lock);
1827                 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1828                         state->start;
1829                 spin_unlock(&BTRFS_I(inode)->lock);
1830         }
1831 }
1832
1833 /*
1834  * extent_io.c clear_bit_hook, see set_bit_hook for why
1835  */
1836 static void btrfs_clear_bit_hook(void *private_data,
1837                                  struct extent_state *state,
1838                                  unsigned *bits)
1839 {
1840         struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
1841         struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1842         u64 len = state->end + 1 - state->start;
1843         u32 num_extents = count_max_extents(len);
1844
1845         if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1846                 spin_lock(&inode->lock);
1847                 inode->defrag_bytes -= len;
1848                 spin_unlock(&inode->lock);
1849         }
1850
1851         /*
1852          * set_bit and clear bit hooks normally require _irqsave/restore
1853          * but in this case, we are only testing for the DELALLOC
1854          * bit, which is only set or cleared with irqs on
1855          */
1856         if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1857                 struct btrfs_root *root = inode->root;
1858                 bool do_list = !btrfs_is_free_space_inode(inode);
1859
1860                 spin_lock(&inode->lock);
1861                 btrfs_mod_outstanding_extents(inode, -num_extents);
1862                 spin_unlock(&inode->lock);
1863
1864                 /*
1865                  * We don't reserve metadata space for space cache inodes so we
1866                  * don't need to call dellalloc_release_metadata if there is an
1867                  * error.
1868                  */
1869                 if (*bits & EXTENT_CLEAR_META_RESV &&
1870                     root != fs_info->tree_root)
1871                         btrfs_delalloc_release_metadata(inode, len);
1872
1873                 /* For sanity tests. */
1874                 if (btrfs_is_testing(fs_info))
1875                         return;
1876
1877                 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1878                     do_list && !(state->state & EXTENT_NORESERVE) &&
1879                     (*bits & EXTENT_CLEAR_DATA_RESV))
1880                         btrfs_free_reserved_data_space_noquota(
1881                                         &inode->vfs_inode,
1882                                         state->start, len);
1883
1884                 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1885                                          fs_info->delalloc_batch);
1886                 spin_lock(&inode->lock);
1887                 inode->delalloc_bytes -= len;
1888                 if (do_list && inode->delalloc_bytes == 0 &&
1889                     test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1890                                         &inode->runtime_flags))
1891                         btrfs_del_delalloc_inode(root, inode);
1892                 spin_unlock(&inode->lock);
1893         }
1894
1895         if ((state->state & EXTENT_DELALLOC_NEW) &&
1896             (*bits & EXTENT_DELALLOC_NEW)) {
1897                 spin_lock(&inode->lock);
1898                 ASSERT(inode->new_delalloc_bytes >= len);
1899                 inode->new_delalloc_bytes -= len;
1900                 spin_unlock(&inode->lock);
1901         }
1902 }
1903
1904 /*
1905  * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1906  * we don't create bios that span stripes or chunks
1907  *
1908  * return 1 if page cannot be merged to bio
1909  * return 0 if page can be merged to bio
1910  * return error otherwise
1911  */
1912 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1913                          size_t size, struct bio *bio,
1914                          unsigned long bio_flags)
1915 {
1916         struct inode *inode = page->mapping->host;
1917         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1918         u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1919         u64 length = 0;
1920         u64 map_length;
1921         int ret;
1922
1923         if (bio_flags & EXTENT_BIO_COMPRESSED)
1924                 return 0;
1925
1926         length = bio->bi_iter.bi_size;
1927         map_length = length;
1928         ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1929                               NULL, 0);
1930         if (ret < 0)
1931                 return ret;
1932         if (map_length < length + size)
1933                 return 1;
1934         return 0;
1935 }
1936
1937 /*
1938  * in order to insert checksums into the metadata in large chunks,
1939  * we wait until bio submission time.   All the pages in the bio are
1940  * checksummed and sums are attached onto the ordered extent record.
1941  *
1942  * At IO completion time the cums attached on the ordered extent record
1943  * are inserted into the btree
1944  */
1945 static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
1946                                     int mirror_num, unsigned long bio_flags,
1947                                     u64 bio_offset)
1948 {
1949         struct inode *inode = private_data;
1950         blk_status_t ret = 0;
1951
1952         ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1953         BUG_ON(ret); /* -ENOMEM */
1954         return 0;
1955 }
1956
1957 /*
1958  * in order to insert checksums into the metadata in large chunks,
1959  * we wait until bio submission time.   All the pages in the bio are
1960  * checksummed and sums are attached onto the ordered extent record.
1961  *
1962  * At IO completion time the cums attached on the ordered extent record
1963  * are inserted into the btree
1964  */
1965 static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
1966                           int mirror_num, unsigned long bio_flags,
1967                           u64 bio_offset)
1968 {
1969         struct inode *inode = private_data;
1970         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1971         blk_status_t ret;
1972
1973         ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
1974         if (ret) {
1975                 bio->bi_status = ret;
1976                 bio_endio(bio);
1977         }
1978         return ret;
1979 }
1980
1981 /*
1982  * extent_io.c submission hook. This does the right thing for csum calculation
1983  * on write, or reading the csums from the tree before a read.
1984  *
1985  * Rules about async/sync submit,
1986  * a) read:                             sync submit
1987  *
1988  * b) write without checksum:           sync submit
1989  *
1990  * c) write with checksum:
1991  *    c-1) if bio is issued by fsync:   sync submit
1992  *         (sync_writers != 0)
1993  *
1994  *    c-2) if root is reloc root:       sync submit
1995  *         (only in case of buffered IO)
1996  *
1997  *    c-3) otherwise:                   async submit
1998  */
1999 static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
2000                                  int mirror_num, unsigned long bio_flags,
2001                                  u64 bio_offset)
2002 {
2003         struct inode *inode = private_data;
2004         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2005         struct btrfs_root *root = BTRFS_I(inode)->root;
2006         enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
2007         blk_status_t ret = 0;
2008         int skip_sum;
2009         int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
2010
2011         skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
2012
2013         if (btrfs_is_free_space_inode(BTRFS_I(inode)))
2014                 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
2015
2016         if (bio_op(bio) != REQ_OP_WRITE) {
2017                 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
2018                 if (ret)
2019                         goto out;
2020
2021                 if (bio_flags & EXTENT_BIO_COMPRESSED) {
2022                         ret = btrfs_submit_compressed_read(inode, bio,
2023                                                            mirror_num,
2024                                                            bio_flags);
2025                         goto out;
2026                 } else if (!skip_sum) {
2027                         ret = btrfs_lookup_bio_sums(inode, bio, NULL);
2028                         if (ret)
2029                                 goto out;
2030                 }
2031                 goto mapit;
2032         } else if (async && !skip_sum) {
2033                 /* csum items have already been cloned */
2034                 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2035                         goto mapit;
2036                 /* we're doing a write, do the async checksumming */
2037                 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2038                                           bio_offset, inode,
2039                                           __btrfs_submit_bio_start,
2040                                           __btrfs_submit_bio_done);
2041                 goto out;
2042         } else if (!skip_sum) {
2043                 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
2044                 if (ret)
2045                         goto out;
2046         }
2047
2048 mapit:
2049         ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
2050
2051 out:
2052         if (ret) {
2053                 bio->bi_status = ret;
2054                 bio_endio(bio);
2055         }
2056         return ret;
2057 }
2058
2059 /*
2060  * given a list of ordered sums record them in the inode.  This happens
2061  * at IO completion time based on sums calculated at bio submission time.
2062  */
2063 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
2064                              struct inode *inode, struct list_head *list)
2065 {
2066         struct btrfs_ordered_sum *sum;
2067         int ret;
2068
2069         list_for_each_entry(sum, list, list) {
2070                 trans->adding_csums = true;
2071                 ret = btrfs_csum_file_blocks(trans,
2072                        BTRFS_I(inode)->root->fs_info->csum_root, sum);
2073                 trans->adding_csums = false;
2074                 if (ret)
2075                         return ret;
2076         }
2077         return 0;
2078 }
2079
2080 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
2081                               unsigned int extra_bits,
2082                               struct extent_state **cached_state, int dedupe)
2083 {
2084         WARN_ON((end & (PAGE_SIZE - 1)) == 0);
2085         return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
2086                                    extra_bits, cached_state);
2087 }
2088
2089 /* see btrfs_writepage_start_hook for details on why this is required */
2090 struct btrfs_writepage_fixup {
2091         struct page *page;
2092         struct btrfs_work work;
2093 };
2094
2095 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2096 {
2097         struct btrfs_writepage_fixup *fixup;
2098         struct btrfs_ordered_extent *ordered;
2099         struct extent_state *cached_state = NULL;
2100         struct extent_changeset *data_reserved = NULL;
2101         struct page *page;
2102         struct inode *inode;
2103         u64 page_start;
2104         u64 page_end;
2105         int ret;
2106
2107         fixup = container_of(work, struct btrfs_writepage_fixup, work);
2108         page = fixup->page;
2109 again:
2110         lock_page(page);
2111         if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2112                 ClearPageChecked(page);
2113                 goto out_page;
2114         }
2115
2116         inode = page->mapping->host;
2117         page_start = page_offset(page);
2118         page_end = page_offset(page) + PAGE_SIZE - 1;
2119
2120         lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
2121                          &cached_state);
2122
2123         /* already ordered? We're done */
2124         if (PagePrivate2(page))
2125                 goto out;
2126
2127         ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
2128                                         PAGE_SIZE);
2129         if (ordered) {
2130                 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2131                                      page_end, &cached_state);
2132                 unlock_page(page);
2133                 btrfs_start_ordered_extent(inode, ordered, 1);
2134                 btrfs_put_ordered_extent(ordered);
2135                 goto again;
2136         }
2137
2138         ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2139                                            PAGE_SIZE);
2140         if (ret) {
2141                 mapping_set_error(page->mapping, ret);
2142                 end_extent_writepage(page, ret, page_start, page_end);
2143                 ClearPageChecked(page);
2144                 goto out;
2145          }
2146
2147         ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2148                                         &cached_state, 0);
2149         if (ret) {
2150                 mapping_set_error(page->mapping, ret);
2151                 end_extent_writepage(page, ret, page_start, page_end);
2152                 ClearPageChecked(page);
2153                 goto out;
2154         }
2155
2156         ClearPageChecked(page);
2157         set_page_dirty(page);
2158         btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2159 out:
2160         unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2161                              &cached_state);
2162 out_page:
2163         unlock_page(page);
2164         put_page(page);
2165         kfree(fixup);
2166         extent_changeset_free(data_reserved);
2167 }
2168
2169 /*
2170  * There are a few paths in the higher layers of the kernel that directly
2171  * set the page dirty bit without asking the filesystem if it is a
2172  * good idea.  This causes problems because we want to make sure COW
2173  * properly happens and the data=ordered rules are followed.
2174  *
2175  * In our case any range that doesn't have the ORDERED bit set
2176  * hasn't been properly setup for IO.  We kick off an async process
2177  * to fix it up.  The async helper will wait for ordered extents, set
2178  * the delalloc bit and make it safe to write the page.
2179  */
2180 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2181 {
2182         struct inode *inode = page->mapping->host;
2183         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2184         struct btrfs_writepage_fixup *fixup;
2185
2186         /* this page is properly in the ordered list */
2187         if (TestClearPagePrivate2(page))
2188                 return 0;
2189
2190         if (PageChecked(page))
2191                 return -EAGAIN;
2192
2193         fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2194         if (!fixup)
2195                 return -EAGAIN;
2196
2197         SetPageChecked(page);
2198         get_page(page);
2199         btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2200                         btrfs_writepage_fixup_worker, NULL, NULL);
2201         fixup->page = page;
2202         btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2203         return -EBUSY;
2204 }
2205
2206 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2207                                        struct inode *inode, u64 file_pos,
2208                                        u64 disk_bytenr, u64 disk_num_bytes,
2209                                        u64 num_bytes, u64 ram_bytes,
2210                                        u8 compression, u8 encryption,
2211                                        u16 other_encoding, int extent_type)
2212 {
2213         struct btrfs_root *root = BTRFS_I(inode)->root;
2214         struct btrfs_file_extent_item *fi;
2215         struct btrfs_path *path;
2216         struct extent_buffer *leaf;
2217         struct btrfs_key ins;
2218         u64 qg_released;
2219         int extent_inserted = 0;
2220         int ret;
2221
2222         path = btrfs_alloc_path();
2223         if (!path)
2224                 return -ENOMEM;
2225
2226         /*
2227          * we may be replacing one extent in the tree with another.
2228          * The new extent is pinned in the extent map, and we don't want
2229          * to drop it from the cache until it is completely in the btree.
2230          *
2231          * So, tell btrfs_drop_extents to leave this extent in the cache.
2232          * the caller is expected to unpin it and allow it to be merged
2233          * with the others.
2234          */
2235         ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2236                                    file_pos + num_bytes, NULL, 0,
2237                                    1, sizeof(*fi), &extent_inserted);
2238         if (ret)
2239                 goto out;
2240
2241         if (!extent_inserted) {
2242                 ins.objectid = btrfs_ino(BTRFS_I(inode));
2243                 ins.offset = file_pos;
2244                 ins.type = BTRFS_EXTENT_DATA_KEY;
2245
2246                 path->leave_spinning = 1;
2247                 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2248                                               sizeof(*fi));
2249                 if (ret)
2250                         goto out;
2251         }
2252         leaf = path->nodes[0];
2253         fi = btrfs_item_ptr(leaf, path->slots[0],
2254                             struct btrfs_file_extent_item);
2255         btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2256         btrfs_set_file_extent_type(leaf, fi, extent_type);
2257         btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2258         btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2259         btrfs_set_file_extent_offset(leaf, fi, 0);
2260         btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2261         btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2262         btrfs_set_file_extent_compression(leaf, fi, compression);
2263         btrfs_set_file_extent_encryption(leaf, fi, encryption);
2264         btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2265
2266         btrfs_mark_buffer_dirty(leaf);
2267         btrfs_release_path(path);
2268
2269         inode_add_bytes(inode, num_bytes);
2270
2271         ins.objectid = disk_bytenr;
2272         ins.offset = disk_num_bytes;
2273         ins.type = BTRFS_EXTENT_ITEM_KEY;
2274
2275         /*
2276          * Release the reserved range from inode dirty range map, as it is
2277          * already moved into delayed_ref_head
2278          */
2279         ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2280         if (ret < 0)
2281                 goto out;
2282         qg_released = ret;
2283         ret = btrfs_alloc_reserved_file_extent(trans, root,
2284                                                btrfs_ino(BTRFS_I(inode)),
2285                                                file_pos, qg_released, &ins);
2286 out:
2287         btrfs_free_path(path);
2288
2289         return ret;
2290 }
2291
2292 /* snapshot-aware defrag */
2293 struct sa_defrag_extent_backref {
2294         struct rb_node node;
2295         struct old_sa_defrag_extent *old;
2296         u64 root_id;
2297         u64 inum;
2298         u64 file_pos;
2299         u64 extent_offset;
2300         u64 num_bytes;
2301         u64 generation;
2302 };
2303
2304 struct old_sa_defrag_extent {
2305         struct list_head list;
2306         struct new_sa_defrag_extent *new;
2307
2308         u64 extent_offset;
2309         u64 bytenr;
2310         u64 offset;
2311         u64 len;
2312         int count;
2313 };
2314
2315 struct new_sa_defrag_extent {
2316         struct rb_root root;
2317         struct list_head head;
2318         struct btrfs_path *path;
2319         struct inode *inode;
2320         u64 file_pos;
2321         u64 len;
2322         u64 bytenr;
2323         u64 disk_len;
2324         u8 compress_type;
2325 };
2326
2327 static int backref_comp(struct sa_defrag_extent_backref *b1,
2328                         struct sa_defrag_extent_backref *b2)
2329 {
2330         if (b1->root_id < b2->root_id)
2331                 return -1;
2332         else if (b1->root_id > b2->root_id)
2333                 return 1;
2334
2335         if (b1->inum < b2->inum)
2336                 return -1;
2337         else if (b1->inum > b2->inum)
2338                 return 1;
2339
2340         if (b1->file_pos < b2->file_pos)
2341                 return -1;
2342         else if (b1->file_pos > b2->file_pos)
2343                 return 1;
2344
2345         /*
2346          * [------------------------------] ===> (a range of space)
2347          *     |<--->|   |<---->| =============> (fs/file tree A)
2348          * |<---------------------------->| ===> (fs/file tree B)
2349          *
2350          * A range of space can refer to two file extents in one tree while
2351          * refer to only one file extent in another tree.
2352          *
2353          * So we may process a disk offset more than one time(two extents in A)
2354          * and locate at the same extent(one extent in B), then insert two same
2355          * backrefs(both refer to the extent in B).
2356          */
2357         return 0;
2358 }
2359
2360 static void backref_insert(struct rb_root *root,
2361                            struct sa_defrag_extent_backref *backref)
2362 {
2363         struct rb_node **p = &root->rb_node;
2364         struct rb_node *parent = NULL;
2365         struct sa_defrag_extent_backref *entry;
2366         int ret;
2367
2368         while (*p) {
2369                 parent = *p;
2370                 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2371
2372                 ret = backref_comp(backref, entry);
2373                 if (ret < 0)
2374                         p = &(*p)->rb_left;
2375                 else
2376                         p = &(*p)->rb_right;
2377         }
2378
2379         rb_link_node(&backref->node, parent, p);
2380         rb_insert_color(&backref->node, root);
2381 }
2382
2383 /*
2384  * Note the backref might has changed, and in this case we just return 0.
2385  */
2386 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2387                                        void *ctx)
2388 {
2389         struct btrfs_file_extent_item *extent;
2390         struct old_sa_defrag_extent *old = ctx;
2391         struct new_sa_defrag_extent *new = old->new;
2392         struct btrfs_path *path = new->path;
2393         struct btrfs_key key;
2394         struct btrfs_root *root;
2395         struct sa_defrag_extent_backref *backref;
2396         struct extent_buffer *leaf;
2397         struct inode *inode = new->inode;
2398         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2399         int slot;
2400         int ret;
2401         u64 extent_offset;
2402         u64 num_bytes;
2403
2404         if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2405             inum == btrfs_ino(BTRFS_I(inode)))
2406                 return 0;
2407
2408         key.objectid = root_id;
2409         key.type = BTRFS_ROOT_ITEM_KEY;
2410         key.offset = (u64)-1;
2411
2412         root = btrfs_read_fs_root_no_name(fs_info, &key);
2413         if (IS_ERR(root)) {
2414                 if (PTR_ERR(root) == -ENOENT)
2415                         return 0;
2416                 WARN_ON(1);
2417                 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
2418                          inum, offset, root_id);
2419                 return PTR_ERR(root);
2420         }
2421
2422         key.objectid = inum;
2423         key.type = BTRFS_EXTENT_DATA_KEY;
2424         if (offset > (u64)-1 << 32)
2425                 key.offset = 0;
2426         else
2427                 key.offset = offset;
2428
2429         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2430         if (WARN_ON(ret < 0))
2431                 return ret;
2432         ret = 0;
2433
2434         while (1) {
2435                 cond_resched();
2436
2437                 leaf = path->nodes[0];
2438                 slot = path->slots[0];
2439
2440                 if (slot >= btrfs_header_nritems(leaf)) {
2441                         ret = btrfs_next_leaf(root, path);
2442                         if (ret < 0) {
2443                                 goto out;
2444                         } else if (ret > 0) {
2445                                 ret = 0;
2446                                 goto out;
2447                         }
2448                         continue;
2449                 }
2450
2451                 path->slots[0]++;
2452
2453                 btrfs_item_key_to_cpu(leaf, &key, slot);
2454
2455                 if (key.objectid > inum)
2456                         goto out;
2457
2458                 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2459                         continue;
2460
2461                 extent = btrfs_item_ptr(leaf, slot,
2462                                         struct btrfs_file_extent_item);
2463
2464                 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2465                         continue;
2466
2467                 /*
2468                  * 'offset' refers to the exact key.offset,
2469                  * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2470                  * (key.offset - extent_offset).
2471                  */
2472                 if (key.offset != offset)
2473                         continue;
2474
2475                 extent_offset = btrfs_file_extent_offset(leaf, extent);
2476                 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2477
2478                 if (extent_offset >= old->extent_offset + old->offset +
2479                     old->len || extent_offset + num_bytes <=
2480                     old->extent_offset + old->offset)
2481                         continue;
2482                 break;
2483         }
2484
2485         backref = kmalloc(sizeof(*backref), GFP_NOFS);
2486         if (!backref) {
2487                 ret = -ENOENT;
2488                 goto out;
2489         }
2490
2491         backref->root_id = root_id;
2492         backref->inum = inum;
2493         backref->file_pos = offset;
2494         backref->num_bytes = num_bytes;
2495         backref->extent_offset = extent_offset;
2496         backref->generation = btrfs_file_extent_generation(leaf, extent);
2497         backref->old = old;
2498         backref_insert(&new->root, backref);
2499         old->count++;
2500 out:
2501         btrfs_release_path(path);
2502         WARN_ON(ret);
2503         return ret;
2504 }
2505
2506 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2507                                    struct new_sa_defrag_extent *new)
2508 {
2509         struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2510         struct old_sa_defrag_extent *old, *tmp;
2511         int ret;
2512
2513         new->path = path;
2514
2515         list_for_each_entry_safe(old, tmp, &new->head, list) {
2516                 ret = iterate_inodes_from_logical(old->bytenr +
2517                                                   old->extent_offset, fs_info,
2518                                                   path, record_one_backref,
2519                                                   old, false);
2520                 if (ret < 0 && ret != -ENOENT)
2521                         return false;
2522
2523                 /* no backref to be processed for this extent */
2524                 if (!old->count) {
2525                         list_del(&old->list);
2526                         kfree(old);
2527                 }
2528         }
2529
2530         if (list_empty(&new->head))
2531                 return false;
2532
2533         return true;
2534 }
2535
2536 static int relink_is_mergable(struct extent_buffer *leaf,
2537                               struct btrfs_file_extent_item *fi,
2538                               struct new_sa_defrag_extent *new)
2539 {
2540         if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2541                 return 0;
2542
2543         if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2544                 return 0;
2545
2546         if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2547                 return 0;
2548
2549         if (btrfs_file_extent_encryption(leaf, fi) ||
2550             btrfs_file_extent_other_encoding(leaf, fi))
2551                 return 0;
2552
2553         return 1;
2554 }
2555
2556 /*
2557  * Note the backref might has changed, and in this case we just return 0.
2558  */
2559 static noinline int relink_extent_backref(struct btrfs_path *path,
2560                                  struct sa_defrag_extent_backref *prev,
2561                                  struct sa_defrag_extent_backref *backref)
2562 {
2563         struct btrfs_file_extent_item *extent;
2564         struct btrfs_file_extent_item *item;
2565         struct btrfs_ordered_extent *ordered;
2566         struct btrfs_trans_handle *trans;
2567         struct btrfs_root *root;
2568         struct btrfs_key key;
2569         struct extent_buffer *leaf;
2570         struct old_sa_defrag_extent *old = backref->old;
2571         struct new_sa_defrag_extent *new = old->new;
2572         struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2573         struct inode *inode;
2574         struct extent_state *cached = NULL;
2575         int ret = 0;
2576         u64 start;
2577         u64 len;
2578         u64 lock_start;
2579         u64 lock_end;
2580         bool merge = false;
2581         int index;
2582
2583         if (prev && prev->root_id == backref->root_id &&
2584             prev->inum == backref->inum &&
2585             prev->file_pos + prev->num_bytes == backref->file_pos)
2586                 merge = true;
2587
2588         /* step 1: get root */
2589         key.objectid = backref->root_id;
2590         key.type = BTRFS_ROOT_ITEM_KEY;
2591         key.offset = (u64)-1;
2592
2593         index = srcu_read_lock(&fs_info->subvol_srcu);
2594
2595         root = btrfs_read_fs_root_no_name(fs_info, &key);
2596         if (IS_ERR(root)) {
2597                 srcu_read_unlock(&fs_info->subvol_srcu, index);
2598                 if (PTR_ERR(root) == -ENOENT)
2599                         return 0;
2600                 return PTR_ERR(root);
2601         }
2602
2603         if (btrfs_root_readonly(root)) {
2604                 srcu_read_unlock(&fs_info->subvol_srcu, index);
2605                 return 0;
2606         }
2607
2608         /* step 2: get inode */
2609         key.objectid = backref->inum;
2610         key.type = BTRFS_INODE_ITEM_KEY;
2611         key.offset = 0;
2612
2613         inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2614         if (IS_ERR(inode)) {
2615                 srcu_read_unlock(&fs_info->subvol_srcu, index);
2616                 return 0;
2617         }
2618
2619         srcu_read_unlock(&fs_info->subvol_srcu, index);
2620
2621         /* step 3: relink backref */
2622         lock_start = backref->file_pos;
2623         lock_end = backref->file_pos + backref->num_bytes - 1;
2624         lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2625                          &cached);
2626
2627         ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2628         if (ordered) {
2629                 btrfs_put_ordered_extent(ordered);
2630                 goto out_unlock;
2631         }
2632
2633         trans = btrfs_join_transaction(root);
2634         if (IS_ERR(trans)) {
2635                 ret = PTR_ERR(trans);
2636                 goto out_unlock;
2637         }
2638
2639         key.objectid = backref->inum;
2640         key.type = BTRFS_EXTENT_DATA_KEY;
2641         key.offset = backref->file_pos;
2642
2643         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2644         if (ret < 0) {
2645                 goto out_free_path;
2646         } else if (ret > 0) {
2647                 ret = 0;
2648                 goto out_free_path;
2649         }
2650
2651         extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2652                                 struct btrfs_file_extent_item);
2653
2654         if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2655             backref->generation)
2656                 goto out_free_path;
2657
2658         btrfs_release_path(path);
2659
2660         start = backref->file_pos;
2661         if (backref->extent_offset < old->extent_offset + old->offset)
2662                 start += old->extent_offset + old->offset -
2663                          backref->extent_offset;
2664
2665         len = min(backref->extent_offset + backref->num_bytes,
2666                   old->extent_offset + old->offset + old->len);
2667         len -= max(backref->extent_offset, old->extent_offset + old->offset);
2668
2669         ret = btrfs_drop_extents(trans, root, inode, start,
2670                                  start + len, 1);
2671         if (ret)
2672                 goto out_free_path;
2673 again:
2674         key.objectid = btrfs_ino(BTRFS_I(inode));
2675         key.type = BTRFS_EXTENT_DATA_KEY;
2676         key.offset = start;
2677
2678         path->leave_spinning = 1;
2679         if (merge) {
2680                 struct btrfs_file_extent_item *fi;
2681                 u64 extent_len;
2682                 struct btrfs_key found_key;
2683
2684                 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2685                 if (ret < 0)
2686                         goto out_free_path;
2687
2688                 path->slots[0]--;
2689                 leaf = path->nodes[0];
2690                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2691
2692                 fi = btrfs_item_ptr(leaf, path->slots[0],
2693                                     struct btrfs_file_extent_item);
2694                 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2695
2696                 if (extent_len + found_key.offset == start &&
2697                     relink_is_mergable(leaf, fi, new)) {
2698                         btrfs_set_file_extent_num_bytes(leaf, fi,
2699                                                         extent_len + len);
2700                         btrfs_mark_buffer_dirty(leaf);
2701                         inode_add_bytes(inode, len);
2702
2703                         ret = 1;
2704                         goto out_free_path;
2705                 } else {
2706                         merge = false;
2707                         btrfs_release_path(path);
2708                         goto again;
2709                 }
2710         }
2711
2712         ret = btrfs_insert_empty_item(trans, root, path, &key,
2713                                         sizeof(*extent));
2714         if (ret) {
2715                 btrfs_abort_transaction(trans, ret);
2716                 goto out_free_path;
2717         }
2718
2719         leaf = path->nodes[0];
2720         item = btrfs_item_ptr(leaf, path->slots[0],
2721                                 struct btrfs_file_extent_item);
2722         btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2723         btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2724         btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2725         btrfs_set_file_extent_num_bytes(leaf, item, len);
2726         btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2727         btrfs_set_file_extent_generation(leaf, item, trans->transid);
2728         btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2729         btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2730         btrfs_set_file_extent_encryption(leaf, item, 0);
2731         btrfs_set_file_extent_other_encoding(leaf, item, 0);
2732
2733         btrfs_mark_buffer_dirty(leaf);
2734         inode_add_bytes(inode, len);
2735         btrfs_release_path(path);
2736
2737         ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2738                         new->disk_len, 0,
2739                         backref->root_id, backref->inum,
2740                         new->file_pos); /* start - extent_offset */
2741         if (ret) {
2742                 btrfs_abort_transaction(trans, ret);
2743                 goto out_free_path;
2744         }
2745
2746         ret = 1;
2747 out_free_path:
2748         btrfs_release_path(path);
2749         path->leave_spinning = 0;
2750         btrfs_end_transaction(trans);
2751 out_unlock:
2752         unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2753                              &cached);
2754         iput(inode);
2755         return ret;
2756 }
2757
2758 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2759 {
2760         struct old_sa_defrag_extent *old, *tmp;
2761
2762         if (!new)
2763                 return;
2764
2765         list_for_each_entry_safe(old, tmp, &new->head, list) {
2766                 kfree(old);
2767         }
2768         kfree(new);
2769 }
2770
2771 static void relink_file_extents(struct new_sa_defrag_extent *new)
2772 {
2773         struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2774         struct btrfs_path *path;
2775         struct sa_defrag_extent_backref *backref;
2776         struct sa_defrag_extent_backref *prev = NULL;
2777         struct inode *inode;
2778         struct btrfs_root *root;
2779         struct rb_node *node;
2780         int ret;
2781
2782         inode = new->inode;
2783         root = BTRFS_I(inode)->root;
2784
2785         path = btrfs_alloc_path();
2786         if (!path)
2787                 return;
2788
2789         if (!record_extent_backrefs(path, new)) {
2790                 btrfs_free_path(path);
2791                 goto out;
2792         }
2793         btrfs_release_path(path);
2794
2795         while (1) {
2796                 node = rb_first(&new->root);
2797                 if (!node)
2798                         break;
2799                 rb_erase(node, &new->root);
2800
2801                 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2802
2803                 ret = relink_extent_backref(path, prev, backref);
2804                 WARN_ON(ret < 0);
2805
2806                 kfree(prev);
2807
2808                 if (ret == 1)
2809                         prev = backref;
2810                 else
2811                         prev = NULL;
2812                 cond_resched();
2813         }
2814         kfree(prev);
2815
2816         btrfs_free_path(path);
2817 out:
2818         free_sa_defrag_extent(new);
2819
2820         atomic_dec(&fs_info->defrag_running);
2821         wake_up(&fs_info->transaction_wait);
2822 }
2823
2824 static struct new_sa_defrag_extent *
2825 record_old_file_extents(struct inode *inode,
2826                         struct btrfs_ordered_extent *ordered)
2827 {
2828         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2829         struct btrfs_root *root = BTRFS_I(inode)->root;
2830         struct btrfs_path *path;
2831         struct btrfs_key key;
2832         struct old_sa_defrag_extent *old;
2833         struct new_sa_defrag_extent *new;
2834         int ret;
2835
2836         new = kmalloc(sizeof(*new), GFP_NOFS);
2837         if (!new)
2838                 return NULL;
2839
2840         new->inode = inode;
2841         new->file_pos = ordered->file_offset;
2842         new->len = ordered->len;
2843         new->bytenr = ordered->start;
2844         new->disk_len = ordered->disk_len;
2845         new->compress_type = ordered->compress_type;
2846         new->root = RB_ROOT;
2847         INIT_LIST_HEAD(&new->head);
2848
2849         path = btrfs_alloc_path();
2850         if (!path)
2851                 goto out_kfree;
2852
2853         key.objectid = btrfs_ino(BTRFS_I(inode));
2854         key.type = BTRFS_EXTENT_DATA_KEY;
2855         key.offset = new->file_pos;
2856
2857         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2858         if (ret < 0)
2859                 goto out_free_path;
2860         if (ret > 0 && path->slots[0] > 0)
2861                 path->slots[0]--;
2862
2863         /* find out all the old extents for the file range */
2864         while (1) {
2865                 struct btrfs_file_extent_item *extent;
2866                 struct extent_buffer *l;
2867                 int slot;
2868                 u64 num_bytes;
2869                 u64 offset;
2870                 u64 end;
2871                 u64 disk_bytenr;
2872                 u64 extent_offset;
2873
2874                 l = path->nodes[0];
2875                 slot = path->slots[0];
2876
2877                 if (slot >= btrfs_header_nritems(l)) {
2878                         ret = btrfs_next_leaf(root, path);
2879                         if (ret < 0)
2880                                 goto out_free_path;
2881                         else if (ret > 0)
2882                                 break;
2883                         continue;
2884                 }
2885
2886                 btrfs_item_key_to_cpu(l, &key, slot);
2887
2888                 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
2889                         break;
2890                 if (key.type != BTRFS_EXTENT_DATA_KEY)
2891                         break;
2892                 if (key.offset >= new->file_pos + new->len)
2893                         break;
2894
2895                 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2896
2897                 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2898                 if (key.offset + num_bytes < new->file_pos)
2899                         goto next;
2900
2901                 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2902                 if (!disk_bytenr)
2903                         goto next;
2904
2905                 extent_offset = btrfs_file_extent_offset(l, extent);
2906
2907                 old = kmalloc(sizeof(*old), GFP_NOFS);
2908                 if (!old)
2909                         goto out_free_path;
2910
2911                 offset = max(new->file_pos, key.offset);
2912                 end = min(new->file_pos + new->len, key.offset + num_bytes);
2913
2914                 old->bytenr = disk_bytenr;
2915                 old->extent_offset = extent_offset;
2916                 old->offset = offset - key.offset;
2917                 old->len = end - offset;
2918                 old->new = new;
2919                 old->count = 0;
2920                 list_add_tail(&old->list, &new->head);
2921 next:
2922                 path->slots[0]++;
2923                 cond_resched();
2924         }
2925
2926         btrfs_free_path(path);
2927         atomic_inc(&fs_info->defrag_running);
2928
2929         return new;
2930
2931 out_free_path:
2932         btrfs_free_path(path);
2933 out_kfree:
2934         free_sa_defrag_extent(new);
2935         return NULL;
2936 }
2937
2938 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2939                                          u64 start, u64 len)
2940 {
2941         struct btrfs_block_group_cache *cache;
2942
2943         cache = btrfs_lookup_block_group(fs_info, start);
2944         ASSERT(cache);
2945
2946         spin_lock(&cache->lock);
2947         cache->delalloc_bytes -= len;
2948         spin_unlock(&cache->lock);
2949
2950         btrfs_put_block_group(cache);
2951 }
2952
2953 /* as ordered data IO finishes, this gets called so we can finish
2954  * an ordered extent if the range of bytes in the file it covers are
2955  * fully written.
2956  */
2957 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2958 {
2959         struct inode *inode = ordered_extent->inode;
2960         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2961         struct btrfs_root *root = BTRFS_I(inode)->root;
2962         struct btrfs_trans_handle *trans = NULL;
2963         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2964         struct extent_state *cached_state = NULL;
2965         struct new_sa_defrag_extent *new = NULL;
2966         int compress_type = 0;
2967         int ret = 0;
2968         u64 logical_len = ordered_extent->len;
2969         bool nolock;
2970         bool truncated = false;
2971         bool range_locked = false;
2972         bool clear_new_delalloc_bytes = false;
2973
2974         if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2975             !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2976             !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2977                 clear_new_delalloc_bytes = true;
2978
2979         nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
2980
2981         if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2982                 ret = -EIO;
2983                 goto out;
2984         }
2985
2986         btrfs_free_io_failure_record(BTRFS_I(inode),
2987                         ordered_extent->file_offset,
2988                         ordered_extent->file_offset +
2989                         ordered_extent->len - 1);
2990
2991         if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2992                 truncated = true;
2993                 logical_len = ordered_extent->truncated_len;
2994                 /* Truncated the entire extent, don't bother adding */
2995                 if (!logical_len)
2996                         goto out;
2997         }
2998
2999         if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
3000                 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
3001
3002                 /*
3003                  * For mwrite(mmap + memset to write) case, we still reserve
3004                  * space for NOCOW range.
3005                  * As NOCOW won't cause a new delayed ref, just free the space
3006                  */
3007                 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3008                                        ordered_extent->len);
3009                 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3010                 if (nolock)
3011                         trans = btrfs_join_transaction_nolock(root);
3012                 else
3013                         trans = btrfs_join_transaction(root);
3014                 if (IS_ERR(trans)) {
3015                         ret = PTR_ERR(trans);
3016                         trans = NULL;
3017                         goto out;
3018                 }
3019                 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
3020                 ret = btrfs_update_inode_fallback(trans, root, inode);
3021                 if (ret) /* -ENOMEM or corruption */
3022                         btrfs_abort_transaction(trans, ret);
3023                 goto out;
3024         }
3025
3026         range_locked = true;
3027         lock_extent_bits(io_tree, ordered_extent->file_offset,
3028                          ordered_extent->file_offset + ordered_extent->len - 1,
3029                          &cached_state);
3030
3031         ret = test_range_bit(io_tree, ordered_extent->file_offset,
3032                         ordered_extent->file_offset + ordered_extent->len - 1,
3033                         EXTENT_DEFRAG, 0, cached_state);
3034         if (ret) {
3035                 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
3036                 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
3037                         /* the inode is shared */
3038                         new = record_old_file_extents(inode, ordered_extent);
3039
3040                 clear_extent_bit(io_tree, ordered_extent->file_offset,
3041                         ordered_extent->file_offset + ordered_extent->len - 1,
3042                         EXTENT_DEFRAG, 0, 0, &cached_state);
3043         }
3044
3045         if (nolock)
3046                 trans = btrfs_join_transaction_nolock(root);
3047         else
3048                 trans = btrfs_join_transaction(root);
3049         if (IS_ERR(trans)) {
3050                 ret = PTR_ERR(trans);
3051                 trans = NULL;
3052                 goto out;
3053         }
3054
3055         trans->block_rsv = &BTRFS_I(inode)->block_rsv;
3056
3057         if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3058                 compress_type = ordered_extent->compress_type;
3059         if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3060                 BUG_ON(compress_type);
3061                 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3062                                        ordered_extent->len);
3063                 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
3064                                                 ordered_extent->file_offset,
3065                                                 ordered_extent->file_offset +
3066                                                 logical_len);
3067         } else {
3068                 BUG_ON(root == fs_info->tree_root);
3069                 ret = insert_reserved_file_extent(trans, inode,
3070                                         &n