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