btrfs: don't redirty locked_page in run_delalloc_zoned
[platform/kernel/linux-starfive.git] / fs / btrfs / inode.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5
6 #include <crypto/hash.h>
7 #include <linux/kernel.h>
8 #include <linux/bio.h>
9 #include <linux/blk-cgroup.h>
10 #include <linux/file.h>
11 #include <linux/fs.h>
12 #include <linux/pagemap.h>
13 #include <linux/highmem.h>
14 #include <linux/time.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include <linux/backing-dev.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/xattr.h>
21 #include <linux/posix_acl.h>
22 #include <linux/falloc.h>
23 #include <linux/slab.h>
24 #include <linux/ratelimit.h>
25 #include <linux/btrfs.h>
26 #include <linux/blkdev.h>
27 #include <linux/posix_acl_xattr.h>
28 #include <linux/uio.h>
29 #include <linux/magic.h>
30 #include <linux/iversion.h>
31 #include <linux/swap.h>
32 #include <linux/migrate.h>
33 #include <linux/sched/mm.h>
34 #include <linux/iomap.h>
35 #include <asm/unaligned.h>
36 #include <linux/fsverity.h>
37 #include "misc.h"
38 #include "ctree.h"
39 #include "disk-io.h"
40 #include "transaction.h"
41 #include "btrfs_inode.h"
42 #include "print-tree.h"
43 #include "ordered-data.h"
44 #include "xattr.h"
45 #include "tree-log.h"
46 #include "bio.h"
47 #include "compression.h"
48 #include "locking.h"
49 #include "free-space-cache.h"
50 #include "props.h"
51 #include "qgroup.h"
52 #include "delalloc-space.h"
53 #include "block-group.h"
54 #include "space-info.h"
55 #include "zoned.h"
56 #include "subpage.h"
57 #include "inode-item.h"
58 #include "fs.h"
59 #include "accessors.h"
60 #include "extent-tree.h"
61 #include "root-tree.h"
62 #include "defrag.h"
63 #include "dir-item.h"
64 #include "file-item.h"
65 #include "uuid-tree.h"
66 #include "ioctl.h"
67 #include "file.h"
68 #include "acl.h"
69 #include "relocation.h"
70 #include "verity.h"
71 #include "super.h"
72 #include "orphan.h"
73 #include "backref.h"
74
75 struct btrfs_iget_args {
76         u64 ino;
77         struct btrfs_root *root;
78 };
79
80 struct btrfs_dio_data {
81         ssize_t submitted;
82         struct extent_changeset *data_reserved;
83         struct btrfs_ordered_extent *ordered;
84         bool data_space_reserved;
85         bool nocow_done;
86 };
87
88 struct btrfs_dio_private {
89         /* Range of I/O */
90         u64 file_offset;
91         u32 bytes;
92
93         /* This must be last */
94         struct btrfs_bio bbio;
95 };
96
97 static struct bio_set btrfs_dio_bioset;
98
99 struct btrfs_rename_ctx {
100         /* Output field. Stores the index number of the old directory entry. */
101         u64 index;
102 };
103
104 /*
105  * Used by data_reloc_print_warning_inode() to pass needed info for filename
106  * resolution and output of error message.
107  */
108 struct data_reloc_warn {
109         struct btrfs_path path;
110         struct btrfs_fs_info *fs_info;
111         u64 extent_item_size;
112         u64 logical;
113         int mirror_num;
114 };
115
116 static const struct inode_operations btrfs_dir_inode_operations;
117 static const struct inode_operations btrfs_symlink_inode_operations;
118 static const struct inode_operations btrfs_special_inode_operations;
119 static const struct inode_operations btrfs_file_inode_operations;
120 static const struct address_space_operations btrfs_aops;
121 static const struct file_operations btrfs_dir_file_operations;
122
123 static struct kmem_cache *btrfs_inode_cachep;
124
125 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
126 static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback);
127
128 static noinline int cow_file_range(struct btrfs_inode *inode,
129                                    struct page *locked_page,
130                                    u64 start, u64 end, u64 *done_offset,
131                                    bool keep_locked, bool no_inline);
132 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
133                                        u64 len, u64 orig_start, u64 block_start,
134                                        u64 block_len, u64 orig_block_len,
135                                        u64 ram_bytes, int compress_type,
136                                        int type);
137
138 static int data_reloc_print_warning_inode(u64 inum, u64 offset, u64 num_bytes,
139                                           u64 root, void *warn_ctx)
140 {
141         struct data_reloc_warn *warn = warn_ctx;
142         struct btrfs_fs_info *fs_info = warn->fs_info;
143         struct extent_buffer *eb;
144         struct btrfs_inode_item *inode_item;
145         struct inode_fs_paths *ipath = NULL;
146         struct btrfs_root *local_root;
147         struct btrfs_key key;
148         unsigned int nofs_flag;
149         u32 nlink;
150         int ret;
151
152         local_root = btrfs_get_fs_root(fs_info, root, true);
153         if (IS_ERR(local_root)) {
154                 ret = PTR_ERR(local_root);
155                 goto err;
156         }
157
158         /* This makes the path point to (inum INODE_ITEM ioff). */
159         key.objectid = inum;
160         key.type = BTRFS_INODE_ITEM_KEY;
161         key.offset = 0;
162
163         ret = btrfs_search_slot(NULL, local_root, &key, &warn->path, 0, 0);
164         if (ret) {
165                 btrfs_put_root(local_root);
166                 btrfs_release_path(&warn->path);
167                 goto err;
168         }
169
170         eb = warn->path.nodes[0];
171         inode_item = btrfs_item_ptr(eb, warn->path.slots[0], struct btrfs_inode_item);
172         nlink = btrfs_inode_nlink(eb, inode_item);
173         btrfs_release_path(&warn->path);
174
175         nofs_flag = memalloc_nofs_save();
176         ipath = init_ipath(4096, local_root, &warn->path);
177         memalloc_nofs_restore(nofs_flag);
178         if (IS_ERR(ipath)) {
179                 btrfs_put_root(local_root);
180                 ret = PTR_ERR(ipath);
181                 ipath = NULL;
182                 /*
183                  * -ENOMEM, not a critical error, just output an generic error
184                  * without filename.
185                  */
186                 btrfs_warn(fs_info,
187 "checksum error at logical %llu mirror %u root %llu, inode %llu offset %llu",
188                            warn->logical, warn->mirror_num, root, inum, offset);
189                 return ret;
190         }
191         ret = paths_from_inode(inum, ipath);
192         if (ret < 0)
193                 goto err;
194
195         /*
196          * We deliberately ignore the bit ipath might have been too small to
197          * hold all of the paths here
198          */
199         for (int i = 0; i < ipath->fspath->elem_cnt; i++) {
200                 btrfs_warn(fs_info,
201 "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu length %u links %u (path: %s)",
202                            warn->logical, warn->mirror_num, root, inum, offset,
203                            fs_info->sectorsize, nlink,
204                            (char *)(unsigned long)ipath->fspath->val[i]);
205         }
206
207         btrfs_put_root(local_root);
208         free_ipath(ipath);
209         return 0;
210
211 err:
212         btrfs_warn(fs_info,
213 "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu, path resolving failed with ret=%d",
214                    warn->logical, warn->mirror_num, root, inum, offset, ret);
215
216         free_ipath(ipath);
217         return ret;
218 }
219
220 /*
221  * Do extra user-friendly error output (e.g. lookup all the affected files).
222  *
223  * Return true if we succeeded doing the backref lookup.
224  * Return false if such lookup failed, and has to fallback to the old error message.
225  */
226 static void print_data_reloc_error(const struct btrfs_inode *inode, u64 file_off,
227                                    const u8 *csum, const u8 *csum_expected,
228                                    int mirror_num)
229 {
230         struct btrfs_fs_info *fs_info = inode->root->fs_info;
231         struct btrfs_path path = { 0 };
232         struct btrfs_key found_key = { 0 };
233         struct extent_buffer *eb;
234         struct btrfs_extent_item *ei;
235         const u32 csum_size = fs_info->csum_size;
236         u64 logical;
237         u64 flags;
238         u32 item_size;
239         int ret;
240
241         mutex_lock(&fs_info->reloc_mutex);
242         logical = btrfs_get_reloc_bg_bytenr(fs_info);
243         mutex_unlock(&fs_info->reloc_mutex);
244
245         if (logical == U64_MAX) {
246                 btrfs_warn_rl(fs_info, "has data reloc tree but no running relocation");
247                 btrfs_warn_rl(fs_info,
248 "csum failed root %lld ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
249                         inode->root->root_key.objectid, btrfs_ino(inode), file_off,
250                         CSUM_FMT_VALUE(csum_size, csum),
251                         CSUM_FMT_VALUE(csum_size, csum_expected),
252                         mirror_num);
253                 return;
254         }
255
256         logical += file_off;
257         btrfs_warn_rl(fs_info,
258 "csum failed root %lld ino %llu off %llu logical %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
259                         inode->root->root_key.objectid,
260                         btrfs_ino(inode), file_off, logical,
261                         CSUM_FMT_VALUE(csum_size, csum),
262                         CSUM_FMT_VALUE(csum_size, csum_expected),
263                         mirror_num);
264
265         ret = extent_from_logical(fs_info, logical, &path, &found_key, &flags);
266         if (ret < 0) {
267                 btrfs_err_rl(fs_info, "failed to lookup extent item for logical %llu: %d",
268                              logical, ret);
269                 return;
270         }
271         eb = path.nodes[0];
272         ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item);
273         item_size = btrfs_item_size(eb, path.slots[0]);
274         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
275                 unsigned long ptr = 0;
276                 u64 ref_root;
277                 u8 ref_level;
278
279                 while (true) {
280                         ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
281                                                       item_size, &ref_root,
282                                                       &ref_level);
283                         if (ret < 0) {
284                                 btrfs_warn_rl(fs_info,
285                                 "failed to resolve tree backref for logical %llu: %d",
286                                               logical, ret);
287                                 break;
288                         }
289                         if (ret > 0)
290                                 break;
291
292                         btrfs_warn_rl(fs_info,
293 "csum error at logical %llu mirror %u: metadata %s (level %d) in tree %llu",
294                                 logical, mirror_num,
295                                 (ref_level ? "node" : "leaf"),
296                                 ref_level, ref_root);
297                 }
298                 btrfs_release_path(&path);
299         } else {
300                 struct btrfs_backref_walk_ctx ctx = { 0 };
301                 struct data_reloc_warn reloc_warn = { 0 };
302
303                 btrfs_release_path(&path);
304
305                 ctx.bytenr = found_key.objectid;
306                 ctx.extent_item_pos = logical - found_key.objectid;
307                 ctx.fs_info = fs_info;
308
309                 reloc_warn.logical = logical;
310                 reloc_warn.extent_item_size = found_key.offset;
311                 reloc_warn.mirror_num = mirror_num;
312                 reloc_warn.fs_info = fs_info;
313
314                 iterate_extent_inodes(&ctx, true,
315                                       data_reloc_print_warning_inode, &reloc_warn);
316         }
317 }
318
319 static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode,
320                 u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num)
321 {
322         struct btrfs_root *root = inode->root;
323         const u32 csum_size = root->fs_info->csum_size;
324
325         /* For data reloc tree, it's better to do a backref lookup instead. */
326         if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
327                 return print_data_reloc_error(inode, logical_start, csum,
328                                               csum_expected, mirror_num);
329
330         /* Output without objectid, which is more meaningful */
331         if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID) {
332                 btrfs_warn_rl(root->fs_info,
333 "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
334                         root->root_key.objectid, btrfs_ino(inode),
335                         logical_start,
336                         CSUM_FMT_VALUE(csum_size, csum),
337                         CSUM_FMT_VALUE(csum_size, csum_expected),
338                         mirror_num);
339         } else {
340                 btrfs_warn_rl(root->fs_info,
341 "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
342                         root->root_key.objectid, btrfs_ino(inode),
343                         logical_start,
344                         CSUM_FMT_VALUE(csum_size, csum),
345                         CSUM_FMT_VALUE(csum_size, csum_expected),
346                         mirror_num);
347         }
348 }
349
350 /*
351  * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
352  *
353  * ilock_flags can have the following bit set:
354  *
355  * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
356  * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
357  *                   return -EAGAIN
358  * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
359  */
360 int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags)
361 {
362         if (ilock_flags & BTRFS_ILOCK_SHARED) {
363                 if (ilock_flags & BTRFS_ILOCK_TRY) {
364                         if (!inode_trylock_shared(&inode->vfs_inode))
365                                 return -EAGAIN;
366                         else
367                                 return 0;
368                 }
369                 inode_lock_shared(&inode->vfs_inode);
370         } else {
371                 if (ilock_flags & BTRFS_ILOCK_TRY) {
372                         if (!inode_trylock(&inode->vfs_inode))
373                                 return -EAGAIN;
374                         else
375                                 return 0;
376                 }
377                 inode_lock(&inode->vfs_inode);
378         }
379         if (ilock_flags & BTRFS_ILOCK_MMAP)
380                 down_write(&inode->i_mmap_lock);
381         return 0;
382 }
383
384 /*
385  * btrfs_inode_unlock - unock inode i_rwsem
386  *
387  * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
388  * to decide whether the lock acquired is shared or exclusive.
389  */
390 void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags)
391 {
392         if (ilock_flags & BTRFS_ILOCK_MMAP)
393                 up_write(&inode->i_mmap_lock);
394         if (ilock_flags & BTRFS_ILOCK_SHARED)
395                 inode_unlock_shared(&inode->vfs_inode);
396         else
397                 inode_unlock(&inode->vfs_inode);
398 }
399
400 /*
401  * Cleanup all submitted ordered extents in specified range to handle errors
402  * from the btrfs_run_delalloc_range() callback.
403  *
404  * NOTE: caller must ensure that when an error happens, it can not call
405  * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
406  * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
407  * to be released, which we want to happen only when finishing the ordered
408  * extent (btrfs_finish_ordered_io()).
409  */
410 static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
411                                                  struct page *locked_page,
412                                                  u64 offset, u64 bytes)
413 {
414         unsigned long index = offset >> PAGE_SHIFT;
415         unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
416         u64 page_start = 0, page_end = 0;
417         struct page *page;
418
419         if (locked_page) {
420                 page_start = page_offset(locked_page);
421                 page_end = page_start + PAGE_SIZE - 1;
422         }
423
424         while (index <= end_index) {
425                 /*
426                  * For locked page, we will call btrfs_mark_ordered_io_finished
427                  * through btrfs_mark_ordered_io_finished() on it
428                  * in run_delalloc_range() for the error handling, which will
429                  * clear page Ordered and run the ordered extent accounting.
430                  *
431                  * Here we can't just clear the Ordered bit, or
432                  * btrfs_mark_ordered_io_finished() would skip the accounting
433                  * for the page range, and the ordered extent will never finish.
434                  */
435                 if (locked_page && index == (page_start >> PAGE_SHIFT)) {
436                         index++;
437                         continue;
438                 }
439                 page = find_get_page(inode->vfs_inode.i_mapping, index);
440                 index++;
441                 if (!page)
442                         continue;
443
444                 /*
445                  * Here we just clear all Ordered bits for every page in the
446                  * range, then btrfs_mark_ordered_io_finished() will handle
447                  * the ordered extent accounting for the range.
448                  */
449                 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
450                                                offset, bytes);
451                 put_page(page);
452         }
453
454         if (locked_page) {
455                 /* The locked page covers the full range, nothing needs to be done */
456                 if (bytes + offset <= page_start + PAGE_SIZE)
457                         return;
458                 /*
459                  * In case this page belongs to the delalloc range being
460                  * instantiated then skip it, since the first page of a range is
461                  * going to be properly cleaned up by the caller of
462                  * run_delalloc_range
463                  */
464                 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
465                         bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
466                         offset = page_offset(locked_page) + PAGE_SIZE;
467                 }
468         }
469
470         return btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, false);
471 }
472
473 static int btrfs_dirty_inode(struct btrfs_inode *inode);
474
475 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
476                                      struct btrfs_new_inode_args *args)
477 {
478         int err;
479
480         if (args->default_acl) {
481                 err = __btrfs_set_acl(trans, args->inode, args->default_acl,
482                                       ACL_TYPE_DEFAULT);
483                 if (err)
484                         return err;
485         }
486         if (args->acl) {
487                 err = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS);
488                 if (err)
489                         return err;
490         }
491         if (!args->default_acl && !args->acl)
492                 cache_no_acl(args->inode);
493         return btrfs_xattr_security_init(trans, args->inode, args->dir,
494                                          &args->dentry->d_name);
495 }
496
497 /*
498  * this does all the hard work for inserting an inline extent into
499  * the btree.  The caller should have done a btrfs_drop_extents so that
500  * no overlapping inline items exist in the btree
501  */
502 static int insert_inline_extent(struct btrfs_trans_handle *trans,
503                                 struct btrfs_path *path,
504                                 struct btrfs_inode *inode, bool extent_inserted,
505                                 size_t size, size_t compressed_size,
506                                 int compress_type,
507                                 struct page **compressed_pages,
508                                 bool update_i_size)
509 {
510         struct btrfs_root *root = inode->root;
511         struct extent_buffer *leaf;
512         struct page *page = NULL;
513         char *kaddr;
514         unsigned long ptr;
515         struct btrfs_file_extent_item *ei;
516         int ret;
517         size_t cur_size = size;
518         u64 i_size;
519
520         ASSERT((compressed_size > 0 && compressed_pages) ||
521                (compressed_size == 0 && !compressed_pages));
522
523         if (compressed_size && compressed_pages)
524                 cur_size = compressed_size;
525
526         if (!extent_inserted) {
527                 struct btrfs_key key;
528                 size_t datasize;
529
530                 key.objectid = btrfs_ino(inode);
531                 key.offset = 0;
532                 key.type = BTRFS_EXTENT_DATA_KEY;
533
534                 datasize = btrfs_file_extent_calc_inline_size(cur_size);
535                 ret = btrfs_insert_empty_item(trans, root, path, &key,
536                                               datasize);
537                 if (ret)
538                         goto fail;
539         }
540         leaf = path->nodes[0];
541         ei = btrfs_item_ptr(leaf, path->slots[0],
542                             struct btrfs_file_extent_item);
543         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
544         btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
545         btrfs_set_file_extent_encryption(leaf, ei, 0);
546         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
547         btrfs_set_file_extent_ram_bytes(leaf, ei, size);
548         ptr = btrfs_file_extent_inline_start(ei);
549
550         if (compress_type != BTRFS_COMPRESS_NONE) {
551                 struct page *cpage;
552                 int i = 0;
553                 while (compressed_size > 0) {
554                         cpage = compressed_pages[i];
555                         cur_size = min_t(unsigned long, compressed_size,
556                                        PAGE_SIZE);
557
558                         kaddr = kmap_local_page(cpage);
559                         write_extent_buffer(leaf, kaddr, ptr, cur_size);
560                         kunmap_local(kaddr);
561
562                         i++;
563                         ptr += cur_size;
564                         compressed_size -= cur_size;
565                 }
566                 btrfs_set_file_extent_compression(leaf, ei,
567                                                   compress_type);
568         } else {
569                 page = find_get_page(inode->vfs_inode.i_mapping, 0);
570                 btrfs_set_file_extent_compression(leaf, ei, 0);
571                 kaddr = kmap_local_page(page);
572                 write_extent_buffer(leaf, kaddr, ptr, size);
573                 kunmap_local(kaddr);
574                 put_page(page);
575         }
576         btrfs_mark_buffer_dirty(leaf);
577         btrfs_release_path(path);
578
579         /*
580          * We align size to sectorsize for inline extents just for simplicity
581          * sake.
582          */
583         ret = btrfs_inode_set_file_extent_range(inode, 0,
584                                         ALIGN(size, root->fs_info->sectorsize));
585         if (ret)
586                 goto fail;
587
588         /*
589          * We're an inline extent, so nobody can extend the file past i_size
590          * without locking a page we already have locked.
591          *
592          * We must do any i_size and inode updates before we unlock the pages.
593          * Otherwise we could end up racing with unlink.
594          */
595         i_size = i_size_read(&inode->vfs_inode);
596         if (update_i_size && size > i_size) {
597                 i_size_write(&inode->vfs_inode, size);
598                 i_size = size;
599         }
600         inode->disk_i_size = i_size;
601
602 fail:
603         return ret;
604 }
605
606
607 /*
608  * conditionally insert an inline extent into the file.  This
609  * does the checks required to make sure the data is small enough
610  * to fit as an inline extent.
611  */
612 static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size,
613                                           size_t compressed_size,
614                                           int compress_type,
615                                           struct page **compressed_pages,
616                                           bool update_i_size)
617 {
618         struct btrfs_drop_extents_args drop_args = { 0 };
619         struct btrfs_root *root = inode->root;
620         struct btrfs_fs_info *fs_info = root->fs_info;
621         struct btrfs_trans_handle *trans;
622         u64 data_len = (compressed_size ?: size);
623         int ret;
624         struct btrfs_path *path;
625
626         /*
627          * We can create an inline extent if it ends at or beyond the current
628          * i_size, is no larger than a sector (decompressed), and the (possibly
629          * compressed) data fits in a leaf and the configured maximum inline
630          * size.
631          */
632         if (size < i_size_read(&inode->vfs_inode) ||
633             size > fs_info->sectorsize ||
634             data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
635             data_len > fs_info->max_inline)
636                 return 1;
637
638         path = btrfs_alloc_path();
639         if (!path)
640                 return -ENOMEM;
641
642         trans = btrfs_join_transaction(root);
643         if (IS_ERR(trans)) {
644                 btrfs_free_path(path);
645                 return PTR_ERR(trans);
646         }
647         trans->block_rsv = &inode->block_rsv;
648
649         drop_args.path = path;
650         drop_args.start = 0;
651         drop_args.end = fs_info->sectorsize;
652         drop_args.drop_cache = true;
653         drop_args.replace_extent = true;
654         drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(data_len);
655         ret = btrfs_drop_extents(trans, root, inode, &drop_args);
656         if (ret) {
657                 btrfs_abort_transaction(trans, ret);
658                 goto out;
659         }
660
661         ret = insert_inline_extent(trans, path, inode, drop_args.extent_inserted,
662                                    size, compressed_size, compress_type,
663                                    compressed_pages, update_i_size);
664         if (ret && ret != -ENOSPC) {
665                 btrfs_abort_transaction(trans, ret);
666                 goto out;
667         } else if (ret == -ENOSPC) {
668                 ret = 1;
669                 goto out;
670         }
671
672         btrfs_update_inode_bytes(inode, size, drop_args.bytes_found);
673         ret = btrfs_update_inode(trans, root, inode);
674         if (ret && ret != -ENOSPC) {
675                 btrfs_abort_transaction(trans, ret);
676                 goto out;
677         } else if (ret == -ENOSPC) {
678                 ret = 1;
679                 goto out;
680         }
681
682         btrfs_set_inode_full_sync(inode);
683 out:
684         /*
685          * Don't forget to free the reserved space, as for inlined extent
686          * it won't count as data extent, free them directly here.
687          * And at reserve time, it's always aligned to page size, so
688          * just free one page here.
689          */
690         btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
691         btrfs_free_path(path);
692         btrfs_end_transaction(trans);
693         return ret;
694 }
695
696 struct async_extent {
697         u64 start;
698         u64 ram_size;
699         u64 compressed_size;
700         struct page **pages;
701         unsigned long nr_pages;
702         int compress_type;
703         struct list_head list;
704 };
705
706 struct async_chunk {
707         struct btrfs_inode *inode;
708         struct page *locked_page;
709         u64 start;
710         u64 end;
711         blk_opf_t write_flags;
712         struct list_head extents;
713         struct cgroup_subsys_state *blkcg_css;
714         struct btrfs_work work;
715         struct async_cow *async_cow;
716 };
717
718 struct async_cow {
719         atomic_t num_chunks;
720         struct async_chunk chunks[];
721 };
722
723 static noinline int add_async_extent(struct async_chunk *cow,
724                                      u64 start, u64 ram_size,
725                                      u64 compressed_size,
726                                      struct page **pages,
727                                      unsigned long nr_pages,
728                                      int compress_type)
729 {
730         struct async_extent *async_extent;
731
732         async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
733         BUG_ON(!async_extent); /* -ENOMEM */
734         async_extent->start = start;
735         async_extent->ram_size = ram_size;
736         async_extent->compressed_size = compressed_size;
737         async_extent->pages = pages;
738         async_extent->nr_pages = nr_pages;
739         async_extent->compress_type = compress_type;
740         list_add_tail(&async_extent->list, &cow->extents);
741         return 0;
742 }
743
744 /*
745  * Check if the inode needs to be submitted to compression, based on mount
746  * options, defragmentation, properties or heuristics.
747  */
748 static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
749                                       u64 end)
750 {
751         struct btrfs_fs_info *fs_info = inode->root->fs_info;
752
753         if (!btrfs_inode_can_compress(inode)) {
754                 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
755                         KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
756                         btrfs_ino(inode));
757                 return 0;
758         }
759         /*
760          * Special check for subpage.
761          *
762          * We lock the full page then run each delalloc range in the page, thus
763          * for the following case, we will hit some subpage specific corner case:
764          *
765          * 0            32K             64K
766          * |    |///////|       |///////|
767          *              \- A            \- B
768          *
769          * In above case, both range A and range B will try to unlock the full
770          * page [0, 64K), causing the one finished later will have page
771          * unlocked already, triggering various page lock requirement BUG_ON()s.
772          *
773          * So here we add an artificial limit that subpage compression can only
774          * if the range is fully page aligned.
775          *
776          * In theory we only need to ensure the first page is fully covered, but
777          * the tailing partial page will be locked until the full compression
778          * finishes, delaying the write of other range.
779          *
780          * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range
781          * first to prevent any submitted async extent to unlock the full page.
782          * By this, we can ensure for subpage case that only the last async_cow
783          * will unlock the full page.
784          */
785         if (fs_info->sectorsize < PAGE_SIZE) {
786                 if (!PAGE_ALIGNED(start) ||
787                     !PAGE_ALIGNED(end + 1))
788                         return 0;
789         }
790
791         /* force compress */
792         if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
793                 return 1;
794         /* defrag ioctl */
795         if (inode->defrag_compress)
796                 return 1;
797         /* bad compression ratios */
798         if (inode->flags & BTRFS_INODE_NOCOMPRESS)
799                 return 0;
800         if (btrfs_test_opt(fs_info, COMPRESS) ||
801             inode->flags & BTRFS_INODE_COMPRESS ||
802             inode->prop_compress)
803                 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
804         return 0;
805 }
806
807 static inline void inode_should_defrag(struct btrfs_inode *inode,
808                 u64 start, u64 end, u64 num_bytes, u32 small_write)
809 {
810         /* If this is a small write inside eof, kick off a defrag */
811         if (num_bytes < small_write &&
812             (start > 0 || end + 1 < inode->disk_i_size))
813                 btrfs_add_inode_defrag(NULL, inode, small_write);
814 }
815
816 /*
817  * Work queue call back to started compression on a file and pages.
818  *
819  * This is done inside an ordered work queue, and the compression is spread
820  * across many cpus.  The actual IO submission is step two, and the ordered work
821  * queue takes care of making sure that happens in the same order things were
822  * put onto the queue by writepages and friends.
823  *
824  * If this code finds it can't get good compression, it puts an entry onto the
825  * work queue to write the uncompressed bytes.  This makes sure that both
826  * compressed inodes and uncompressed inodes are written in the same order that
827  * the flusher thread sent them down.
828  */
829 static void compress_file_range(struct btrfs_work *work)
830 {
831         struct async_chunk *async_chunk =
832                 container_of(work, struct async_chunk, work);
833         struct btrfs_inode *inode = async_chunk->inode;
834         struct btrfs_fs_info *fs_info = inode->root->fs_info;
835         struct address_space *mapping = inode->vfs_inode.i_mapping;
836         u64 blocksize = fs_info->sectorsize;
837         u64 start = async_chunk->start;
838         u64 end = async_chunk->end;
839         u64 actual_end;
840         u64 i_size;
841         int ret = 0;
842         struct page **pages;
843         unsigned long nr_pages;
844         unsigned long total_compressed = 0;
845         unsigned long total_in = 0;
846         unsigned int poff;
847         int i;
848         int compress_type = fs_info->compress_type;
849
850         inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
851
852         /*
853          * We need to call clear_page_dirty_for_io on each page in the range.
854          * Otherwise applications with the file mmap'd can wander in and change
855          * the page contents while we are compressing them.
856          */
857         extent_range_clear_dirty_for_io(&inode->vfs_inode, start, end);
858
859         /*
860          * We need to save i_size before now because it could change in between
861          * us evaluating the size and assigning it.  This is because we lock and
862          * unlock the page in truncate and fallocate, and then modify the i_size
863          * later on.
864          *
865          * The barriers are to emulate READ_ONCE, remove that once i_size_read
866          * does that for us.
867          */
868         barrier();
869         i_size = i_size_read(&inode->vfs_inode);
870         barrier();
871         actual_end = min_t(u64, i_size, end + 1);
872 again:
873         pages = NULL;
874         nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
875         nr_pages = min_t(unsigned long, nr_pages, BTRFS_MAX_COMPRESSED_PAGES);
876
877         /*
878          * we don't want to send crud past the end of i_size through
879          * compression, that's just a waste of CPU time.  So, if the
880          * end of the file is before the start of our current
881          * requested range of bytes, we bail out to the uncompressed
882          * cleanup code that can deal with all of this.
883          *
884          * It isn't really the fastest way to fix things, but this is a
885          * very uncommon corner.
886          */
887         if (actual_end <= start)
888                 goto cleanup_and_bail_uncompressed;
889
890         total_compressed = actual_end - start;
891
892         /*
893          * Skip compression for a small file range(<=blocksize) that
894          * isn't an inline extent, since it doesn't save disk space at all.
895          */
896         if (total_compressed <= blocksize &&
897            (start > 0 || end + 1 < inode->disk_i_size))
898                 goto cleanup_and_bail_uncompressed;
899
900         /*
901          * For subpage case, we require full page alignment for the sector
902          * aligned range.
903          * Thus we must also check against @actual_end, not just @end.
904          */
905         if (blocksize < PAGE_SIZE) {
906                 if (!PAGE_ALIGNED(start) ||
907                     !PAGE_ALIGNED(round_up(actual_end, blocksize)))
908                         goto cleanup_and_bail_uncompressed;
909         }
910
911         total_compressed = min_t(unsigned long, total_compressed,
912                         BTRFS_MAX_UNCOMPRESSED);
913         total_in = 0;
914         ret = 0;
915
916         /*
917          * We do compression for mount -o compress and when the inode has not
918          * been flagged as NOCOMPRESS.  This flag can change at any time if we
919          * discover bad compression ratios.
920          */
921         if (!inode_need_compress(inode, start, end))
922                 goto cleanup_and_bail_uncompressed;
923
924         pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
925         if (!pages) {
926                 /*
927                  * Memory allocation failure is not a fatal error, we can fall
928                  * back to uncompressed code.
929                  */
930                 goto cleanup_and_bail_uncompressed;
931         }
932
933         if (inode->defrag_compress)
934                 compress_type = inode->defrag_compress;
935         else if (inode->prop_compress)
936                 compress_type = inode->prop_compress;
937
938         /* Compression level is applied here. */
939         ret = btrfs_compress_pages(compress_type | (fs_info->compress_level << 4),
940                                    mapping, start, pages, &nr_pages, &total_in,
941                                    &total_compressed);
942         if (ret)
943                 goto mark_incompressible;
944
945         /*
946          * Zero the tail end of the last page, as we might be sending it down
947          * to disk.
948          */
949         poff = offset_in_page(total_compressed);
950         if (poff)
951                 memzero_page(pages[nr_pages - 1], poff, PAGE_SIZE - poff);
952
953         /*
954          * Try to create an inline extent.
955          *
956          * If we didn't compress the entire range, try to create an uncompressed
957          * inline extent, else a compressed one.
958          *
959          * Check cow_file_range() for why we don't even try to create inline
960          * extent for the subpage case.
961          */
962         if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
963                 if (total_in < actual_end) {
964                         ret = cow_file_range_inline(inode, actual_end, 0,
965                                                     BTRFS_COMPRESS_NONE, NULL,
966                                                     false);
967                 } else {
968                         ret = cow_file_range_inline(inode, actual_end,
969                                                     total_compressed,
970                                                     compress_type, pages,
971                                                     false);
972                 }
973                 if (ret <= 0) {
974                         unsigned long clear_flags = EXTENT_DELALLOC |
975                                 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
976                                 EXTENT_DO_ACCOUNTING;
977
978                         if (ret < 0)
979                                 mapping_set_error(mapping, -EIO);
980
981                         /*
982                          * inline extent creation worked or returned error,
983                          * we don't need to create any more async work items.
984                          * Unlock and free up our temp pages.
985                          *
986                          * We use DO_ACCOUNTING here because we need the
987                          * delalloc_release_metadata to be done _after_ we drop
988                          * our outstanding extent for clearing delalloc for this
989                          * range.
990                          */
991                         extent_clear_unlock_delalloc(inode, start, end,
992                                                      NULL,
993                                                      clear_flags,
994                                                      PAGE_UNLOCK |
995                                                      PAGE_START_WRITEBACK |
996                                                      PAGE_END_WRITEBACK);
997                         goto free_pages;
998                 }
999         }
1000
1001         /*
1002          * We aren't doing an inline extent. Round the compressed size up to a
1003          * block size boundary so the allocator does sane things.
1004          */
1005         total_compressed = ALIGN(total_compressed, blocksize);
1006
1007         /*
1008          * One last check to make sure the compression is really a win, compare
1009          * the page count read with the blocks on disk, compression must free at
1010          * least one sector.
1011          */
1012         total_in = round_up(total_in, fs_info->sectorsize);
1013         if (total_compressed + blocksize > total_in)
1014                 goto mark_incompressible;
1015
1016         /*
1017          * The async work queues will take care of doing actual allocation on
1018          * disk for these compressed pages, and will submit the bios.
1019          */
1020         add_async_extent(async_chunk, start, total_in, total_compressed, pages,
1021                          nr_pages, compress_type);
1022         if (start + total_in < end) {
1023                 start += total_in;
1024                 cond_resched();
1025                 goto again;
1026         }
1027         return;
1028
1029 mark_incompressible:
1030         if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && !inode->prop_compress)
1031                 inode->flags |= BTRFS_INODE_NOCOMPRESS;
1032 cleanup_and_bail_uncompressed:
1033         add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
1034                          BTRFS_COMPRESS_NONE);
1035 free_pages:
1036         if (pages) {
1037                 for (i = 0; i < nr_pages; i++) {
1038                         WARN_ON(pages[i]->mapping);
1039                         put_page(pages[i]);
1040                 }
1041                 kfree(pages);
1042         }
1043 }
1044
1045 static void free_async_extent_pages(struct async_extent *async_extent)
1046 {
1047         int i;
1048
1049         if (!async_extent->pages)
1050                 return;
1051
1052         for (i = 0; i < async_extent->nr_pages; i++) {
1053                 WARN_ON(async_extent->pages[i]->mapping);
1054                 put_page(async_extent->pages[i]);
1055         }
1056         kfree(async_extent->pages);
1057         async_extent->nr_pages = 0;
1058         async_extent->pages = NULL;
1059 }
1060
1061 static void submit_uncompressed_range(struct btrfs_inode *inode,
1062                                       struct async_extent *async_extent,
1063                                       struct page *locked_page)
1064 {
1065         u64 start = async_extent->start;
1066         u64 end = async_extent->start + async_extent->ram_size - 1;
1067         int ret;
1068         struct writeback_control wbc = {
1069                 .sync_mode              = WB_SYNC_ALL,
1070                 .range_start            = start,
1071                 .range_end              = end,
1072                 .no_cgroup_owner        = 1,
1073         };
1074
1075         /*
1076          * Call cow_file_range() to run the delalloc range directly, since we
1077          * won't go to NOCOW or async path again.
1078          *
1079          * Also we call cow_file_range() with @unlock_page == 0, so that we
1080          * can directly submit them without interruption.
1081          */
1082         ret = cow_file_range(inode, locked_page, start, end, NULL, true, false);
1083         /* Inline extent inserted, page gets unlocked and everything is done */
1084         if (ret == 1)
1085                 return;
1086
1087         if (ret < 0) {
1088                 btrfs_cleanup_ordered_extents(inode, locked_page, start, end - start + 1);
1089                 if (locked_page) {
1090                         const u64 page_start = page_offset(locked_page);
1091
1092                         set_page_writeback(locked_page);
1093                         end_page_writeback(locked_page);
1094                         btrfs_mark_ordered_io_finished(inode, locked_page,
1095                                                        page_start, PAGE_SIZE,
1096                                                        !ret);
1097                         btrfs_page_clear_uptodate(inode->root->fs_info,
1098                                                   locked_page, page_start,
1099                                                   PAGE_SIZE);
1100                         mapping_set_error(locked_page->mapping, ret);
1101                         unlock_page(locked_page);
1102                 }
1103                 return;
1104         }
1105
1106         /* All pages will be unlocked, including @locked_page */
1107         wbc_attach_fdatawrite_inode(&wbc, &inode->vfs_inode);
1108         extent_write_locked_range(&inode->vfs_inode, NULL, start, end, &wbc,
1109                                   false);
1110         wbc_detach_inode(&wbc);
1111 }
1112
1113 static void submit_one_async_extent(struct async_chunk *async_chunk,
1114                                     struct async_extent *async_extent,
1115                                     u64 *alloc_hint)
1116 {
1117         struct btrfs_inode *inode = async_chunk->inode;
1118         struct extent_io_tree *io_tree = &inode->io_tree;
1119         struct btrfs_root *root = inode->root;
1120         struct btrfs_fs_info *fs_info = root->fs_info;
1121         struct btrfs_ordered_extent *ordered;
1122         struct btrfs_key ins;
1123         struct page *locked_page = NULL;
1124         struct extent_map *em;
1125         int ret = 0;
1126         u64 start = async_extent->start;
1127         u64 end = async_extent->start + async_extent->ram_size - 1;
1128
1129         if (async_chunk->blkcg_css)
1130                 kthread_associate_blkcg(async_chunk->blkcg_css);
1131
1132         /*
1133          * If async_chunk->locked_page is in the async_extent range, we need to
1134          * handle it.
1135          */
1136         if (async_chunk->locked_page) {
1137                 u64 locked_page_start = page_offset(async_chunk->locked_page);
1138                 u64 locked_page_end = locked_page_start + PAGE_SIZE - 1;
1139
1140                 if (!(start >= locked_page_end || end <= locked_page_start))
1141                         locked_page = async_chunk->locked_page;
1142         }
1143         lock_extent(io_tree, start, end, NULL);
1144
1145         if (async_extent->compress_type == BTRFS_COMPRESS_NONE) {
1146                 submit_uncompressed_range(inode, async_extent, locked_page);
1147                 goto done;
1148         }
1149
1150         ret = btrfs_reserve_extent(root, async_extent->ram_size,
1151                                    async_extent->compressed_size,
1152                                    async_extent->compressed_size,
1153                                    0, *alloc_hint, &ins, 1, 1);
1154         if (ret) {
1155                 /*
1156                  * Here we used to try again by going back to non-compressed
1157                  * path for ENOSPC.  But we can't reserve space even for
1158                  * compressed size, how could it work for uncompressed size
1159                  * which requires larger size?  So here we directly go error
1160                  * path.
1161                  */
1162                 goto out_free;
1163         }
1164
1165         /* Here we're doing allocation and writeback of the compressed pages */
1166         em = create_io_em(inode, start,
1167                           async_extent->ram_size,       /* len */
1168                           start,                        /* orig_start */
1169                           ins.objectid,                 /* block_start */
1170                           ins.offset,                   /* block_len */
1171                           ins.offset,                   /* orig_block_len */
1172                           async_extent->ram_size,       /* ram_bytes */
1173                           async_extent->compress_type,
1174                           BTRFS_ORDERED_COMPRESSED);
1175         if (IS_ERR(em)) {
1176                 ret = PTR_ERR(em);
1177                 goto out_free_reserve;
1178         }
1179         free_extent_map(em);
1180
1181         ordered = btrfs_alloc_ordered_extent(inode, start,      /* file_offset */
1182                                        async_extent->ram_size,  /* num_bytes */
1183                                        async_extent->ram_size,  /* ram_bytes */
1184                                        ins.objectid,            /* disk_bytenr */
1185                                        ins.offset,              /* disk_num_bytes */
1186                                        0,                       /* offset */
1187                                        1 << BTRFS_ORDERED_COMPRESSED,
1188                                        async_extent->compress_type);
1189         if (IS_ERR(ordered)) {
1190                 btrfs_drop_extent_map_range(inode, start, end, false);
1191                 ret = PTR_ERR(ordered);
1192                 goto out_free_reserve;
1193         }
1194         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1195
1196         /* Clear dirty, set writeback and unlock the pages. */
1197         extent_clear_unlock_delalloc(inode, start, end,
1198                         NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
1199                         PAGE_UNLOCK | PAGE_START_WRITEBACK);
1200         btrfs_submit_compressed_write(ordered,
1201                             async_extent->pages,        /* compressed_pages */
1202                             async_extent->nr_pages,
1203                             async_chunk->write_flags, true);
1204         *alloc_hint = ins.objectid + ins.offset;
1205 done:
1206         if (async_chunk->blkcg_css)
1207                 kthread_associate_blkcg(NULL);
1208         kfree(async_extent);
1209         return;
1210
1211 out_free_reserve:
1212         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1213         btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1214 out_free:
1215         mapping_set_error(inode->vfs_inode.i_mapping, -EIO);
1216         extent_clear_unlock_delalloc(inode, start, end,
1217                                      NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
1218                                      EXTENT_DELALLOC_NEW |
1219                                      EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
1220                                      PAGE_UNLOCK | PAGE_START_WRITEBACK |
1221                                      PAGE_END_WRITEBACK);
1222         free_async_extent_pages(async_extent);
1223         if (async_chunk->blkcg_css)
1224                 kthread_associate_blkcg(NULL);
1225         btrfs_debug(fs_info,
1226 "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d",
1227                     root->root_key.objectid, btrfs_ino(inode), start,
1228                     async_extent->ram_size, ret);
1229         kfree(async_extent);
1230 }
1231
1232 static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
1233                                       u64 num_bytes)
1234 {
1235         struct extent_map_tree *em_tree = &inode->extent_tree;
1236         struct extent_map *em;
1237         u64 alloc_hint = 0;
1238
1239         read_lock(&em_tree->lock);
1240         em = search_extent_mapping(em_tree, start, num_bytes);
1241         if (em) {
1242                 /*
1243                  * if block start isn't an actual block number then find the
1244                  * first block in this inode and use that as a hint.  If that
1245                  * block is also bogus then just don't worry about it.
1246                  */
1247                 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1248                         free_extent_map(em);
1249                         em = search_extent_mapping(em_tree, 0, 0);
1250                         if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1251                                 alloc_hint = em->block_start;
1252                         if (em)
1253                                 free_extent_map(em);
1254                 } else {
1255                         alloc_hint = em->block_start;
1256                         free_extent_map(em);
1257                 }
1258         }
1259         read_unlock(&em_tree->lock);
1260
1261         return alloc_hint;
1262 }
1263
1264 /*
1265  * when extent_io.c finds a delayed allocation range in the file,
1266  * the call backs end up in this code.  The basic idea is to
1267  * allocate extents on disk for the range, and create ordered data structs
1268  * in ram to track those extents.
1269  *
1270  * locked_page is the page that writepage had locked already.  We use
1271  * it to make sure we don't do extra locks or unlocks.
1272  *
1273  * When this function fails, it unlocks all pages except @locked_page.
1274  *
1275  * When this function successfully creates an inline extent, it returns 1 and
1276  * unlocks all pages including locked_page and starts I/O on them.
1277  * (In reality inline extents are limited to a single page, so locked_page is
1278  * the only page handled anyway).
1279  *
1280  * When this function succeed and creates a normal extent, the page locking
1281  * status depends on the passed in flags:
1282  *
1283  * - If @keep_locked is set, all pages are kept locked.
1284  * - Else all pages except for @locked_page are unlocked.
1285  *
1286  * When a failure happens in the second or later iteration of the
1287  * while-loop, the ordered extents created in previous iterations are kept
1288  * intact. So, the caller must clean them up by calling
1289  * btrfs_cleanup_ordered_extents(). See btrfs_run_delalloc_range() for
1290  * example.
1291  */
1292 static noinline int cow_file_range(struct btrfs_inode *inode,
1293                                    struct page *locked_page, u64 start, u64 end,
1294                                    u64 *done_offset,
1295                                    bool keep_locked, bool no_inline)
1296 {
1297         struct btrfs_root *root = inode->root;
1298         struct btrfs_fs_info *fs_info = root->fs_info;
1299         u64 alloc_hint = 0;
1300         u64 orig_start = start;
1301         u64 num_bytes;
1302         unsigned long ram_size;
1303         u64 cur_alloc_size = 0;
1304         u64 min_alloc_size;
1305         u64 blocksize = fs_info->sectorsize;
1306         struct btrfs_key ins;
1307         struct extent_map *em;
1308         unsigned clear_bits;
1309         unsigned long page_ops;
1310         bool extent_reserved = false;
1311         int ret = 0;
1312
1313         if (btrfs_is_free_space_inode(inode)) {
1314                 ret = -EINVAL;
1315                 goto out_unlock;
1316         }
1317
1318         num_bytes = ALIGN(end - start + 1, blocksize);
1319         num_bytes = max(blocksize,  num_bytes);
1320         ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
1321
1322         inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
1323
1324         /*
1325          * Due to the page size limit, for subpage we can only trigger the
1326          * writeback for the dirty sectors of page, that means data writeback
1327          * is doing more writeback than what we want.
1328          *
1329          * This is especially unexpected for some call sites like fallocate,
1330          * where we only increase i_size after everything is done.
1331          * This means we can trigger inline extent even if we didn't want to.
1332          * So here we skip inline extent creation completely.
1333          */
1334         if (start == 0 && fs_info->sectorsize == PAGE_SIZE && !no_inline) {
1335                 u64 actual_end = min_t(u64, i_size_read(&inode->vfs_inode),
1336                                        end + 1);
1337
1338                 /* lets try to make an inline extent */
1339                 ret = cow_file_range_inline(inode, actual_end, 0,
1340                                             BTRFS_COMPRESS_NONE, NULL, false);
1341                 if (ret == 0) {
1342                         /*
1343                          * We use DO_ACCOUNTING here because we need the
1344                          * delalloc_release_metadata to be run _after_ we drop
1345                          * our outstanding extent for clearing delalloc for this
1346                          * range.
1347                          */
1348                         extent_clear_unlock_delalloc(inode, start, end,
1349                                      locked_page,
1350                                      EXTENT_LOCKED | EXTENT_DELALLOC |
1351                                      EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1352                                      EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1353                                      PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
1354                         /*
1355                          * locked_page is locked by the caller of
1356                          * writepage_delalloc(), not locked by
1357                          * __process_pages_contig().
1358                          *
1359                          * We can't let __process_pages_contig() to unlock it,
1360                          * as it doesn't have any subpage::writers recorded.
1361                          *
1362                          * Here we manually unlock the page, since the caller
1363                          * can't determine if it's an inline extent or a
1364                          * compressed extent.
1365                          */
1366                         unlock_page(locked_page);
1367                         ret = 1;
1368                         goto done;
1369                 } else if (ret < 0) {
1370                         goto out_unlock;
1371                 }
1372         }
1373
1374         alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1375
1376         /*
1377          * Relocation relies on the relocated extents to have exactly the same
1378          * size as the original extents. Normally writeback for relocation data
1379          * extents follows a NOCOW path because relocation preallocates the
1380          * extents. However, due to an operation such as scrub turning a block
1381          * group to RO mode, it may fallback to COW mode, so we must make sure
1382          * an extent allocated during COW has exactly the requested size and can
1383          * not be split into smaller extents, otherwise relocation breaks and
1384          * fails during the stage where it updates the bytenr of file extent
1385          * items.
1386          */
1387         if (btrfs_is_data_reloc_root(root))
1388                 min_alloc_size = num_bytes;
1389         else
1390                 min_alloc_size = fs_info->sectorsize;
1391
1392         while (num_bytes > 0) {
1393                 struct btrfs_ordered_extent *ordered;
1394
1395                 cur_alloc_size = num_bytes;
1396                 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1397                                            min_alloc_size, 0, alloc_hint,
1398                                            &ins, 1, 1);
1399                 if (ret == -EAGAIN) {
1400                         /*
1401                          * btrfs_reserve_extent only returns -EAGAIN for zoned
1402                          * file systems, which is an indication that there are
1403                          * no active zones to allocate from at the moment.
1404                          *
1405                          * If this is the first loop iteration, wait for at
1406                          * least one zone to finish before retrying the
1407                          * allocation.  Otherwise ask the caller to write out
1408                          * the already allocated blocks before coming back to
1409                          * us, or return -ENOSPC if it can't handle retries.
1410                          */
1411                         ASSERT(btrfs_is_zoned(fs_info));
1412                         if (start == orig_start) {
1413                                 wait_on_bit_io(&inode->root->fs_info->flags,
1414                                                BTRFS_FS_NEED_ZONE_FINISH,
1415                                                TASK_UNINTERRUPTIBLE);
1416                                 continue;
1417                         }
1418                         if (done_offset) {
1419                                 *done_offset = start - 1;
1420                                 return 0;
1421                         }
1422                         ret = -ENOSPC;
1423                 }
1424                 if (ret < 0)
1425                         goto out_unlock;
1426                 cur_alloc_size = ins.offset;
1427                 extent_reserved = true;
1428
1429                 ram_size = ins.offset;
1430                 em = create_io_em(inode, start, ins.offset, /* len */
1431                                   start, /* orig_start */
1432                                   ins.objectid, /* block_start */
1433                                   ins.offset, /* block_len */
1434                                   ins.offset, /* orig_block_len */
1435                                   ram_size, /* ram_bytes */
1436                                   BTRFS_COMPRESS_NONE, /* compress_type */
1437                                   BTRFS_ORDERED_REGULAR /* type */);
1438                 if (IS_ERR(em)) {
1439                         ret = PTR_ERR(em);
1440                         goto out_reserve;
1441                 }
1442                 free_extent_map(em);
1443
1444                 ordered = btrfs_alloc_ordered_extent(inode, start, ram_size,
1445                                         ram_size, ins.objectid, cur_alloc_size,
1446                                         0, 1 << BTRFS_ORDERED_REGULAR,
1447                                         BTRFS_COMPRESS_NONE);
1448                 if (IS_ERR(ordered)) {
1449                         ret = PTR_ERR(ordered);
1450                         goto out_drop_extent_cache;
1451                 }
1452
1453                 if (btrfs_is_data_reloc_root(root)) {
1454                         ret = btrfs_reloc_clone_csums(ordered);
1455
1456                         /*
1457                          * Only drop cache here, and process as normal.
1458                          *
1459                          * We must not allow extent_clear_unlock_delalloc()
1460                          * at out_unlock label to free meta of this ordered
1461                          * extent, as its meta should be freed by
1462                          * btrfs_finish_ordered_io().
1463                          *
1464                          * So we must continue until @start is increased to
1465                          * skip current ordered extent.
1466                          */
1467                         if (ret)
1468                                 btrfs_drop_extent_map_range(inode, start,
1469                                                             start + ram_size - 1,
1470                                                             false);
1471                 }
1472                 btrfs_put_ordered_extent(ordered);
1473
1474                 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1475
1476                 /*
1477                  * We're not doing compressed IO, don't unlock the first page
1478                  * (which the caller expects to stay locked), don't clear any
1479                  * dirty bits and don't set any writeback bits
1480                  *
1481                  * Do set the Ordered (Private2) bit so we know this page was
1482                  * properly setup for writepage.
1483                  */
1484                 page_ops = (keep_locked ? 0 : PAGE_UNLOCK);
1485                 page_ops |= PAGE_SET_ORDERED;
1486
1487                 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
1488                                              locked_page,
1489                                              EXTENT_LOCKED | EXTENT_DELALLOC,
1490                                              page_ops);
1491                 if (num_bytes < cur_alloc_size)
1492                         num_bytes = 0;
1493                 else
1494                         num_bytes -= cur_alloc_size;
1495                 alloc_hint = ins.objectid + ins.offset;
1496                 start += cur_alloc_size;
1497                 extent_reserved = false;
1498
1499                 /*
1500                  * btrfs_reloc_clone_csums() error, since start is increased
1501                  * extent_clear_unlock_delalloc() at out_unlock label won't
1502                  * free metadata of current ordered extent, we're OK to exit.
1503                  */
1504                 if (ret)
1505                         goto out_unlock;
1506         }
1507 done:
1508         if (done_offset)
1509                 *done_offset = end;
1510         return ret;
1511
1512 out_drop_extent_cache:
1513         btrfs_drop_extent_map_range(inode, start, start + ram_size - 1, false);
1514 out_reserve:
1515         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1516         btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1517 out_unlock:
1518         /*
1519          * Now, we have three regions to clean up:
1520          *
1521          * |-------(1)----|---(2)---|-------------(3)----------|
1522          * `- orig_start  `- start  `- start + cur_alloc_size  `- end
1523          *
1524          * We process each region below.
1525          */
1526
1527         clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1528                 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1529         page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
1530
1531         /*
1532          * For the range (1). We have already instantiated the ordered extents
1533          * for this region. They are cleaned up by
1534          * btrfs_cleanup_ordered_extents() in e.g,
1535          * btrfs_run_delalloc_range(). EXTENT_LOCKED | EXTENT_DELALLOC are
1536          * already cleared in the above loop. And, EXTENT_DELALLOC_NEW |
1537          * EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV are handled by the cleanup
1538          * function.
1539          *
1540          * However, in case of @keep_locked, we still need to unlock the pages
1541          * (except @locked_page) to ensure all the pages are unlocked.
1542          */
1543         if (keep_locked && orig_start < start) {
1544                 if (!locked_page)
1545                         mapping_set_error(inode->vfs_inode.i_mapping, ret);
1546                 extent_clear_unlock_delalloc(inode, orig_start, start - 1,
1547                                              locked_page, 0, page_ops);
1548         }
1549
1550         /*
1551          * For the range (2). If we reserved an extent for our delalloc range
1552          * (or a subrange) and failed to create the respective ordered extent,
1553          * then it means that when we reserved the extent we decremented the
1554          * extent's size from the data space_info's bytes_may_use counter and
1555          * incremented the space_info's bytes_reserved counter by the same
1556          * amount. We must make sure extent_clear_unlock_delalloc() does not try
1557          * to decrement again the data space_info's bytes_may_use counter,
1558          * therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV.
1559          */
1560         if (extent_reserved) {
1561                 extent_clear_unlock_delalloc(inode, start,
1562                                              start + cur_alloc_size - 1,
1563                                              locked_page,
1564                                              clear_bits,
1565                                              page_ops);
1566                 start += cur_alloc_size;
1567         }
1568
1569         /*
1570          * For the range (3). We never touched the region. In addition to the
1571          * clear_bits above, we add EXTENT_CLEAR_DATA_RESV to release the data
1572          * space_info's bytes_may_use counter, reserved in
1573          * btrfs_check_data_free_space().
1574          */
1575         if (start < end) {
1576                 clear_bits |= EXTENT_CLEAR_DATA_RESV;
1577                 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1578                                              clear_bits, page_ops);
1579         }
1580         return ret;
1581 }
1582
1583 /*
1584  * Phase two of compressed writeback.  This is the ordered portion of the code,
1585  * which only gets called in the order the work was queued.  We walk all the
1586  * async extents created by compress_file_range and send them down to the disk.
1587  */
1588 static noinline void submit_compressed_extents(struct btrfs_work *work)
1589 {
1590         struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1591                                                      work);
1592         struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
1593         struct async_extent *async_extent;
1594         unsigned long nr_pages;
1595         u64 alloc_hint = 0;
1596
1597         nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
1598                 PAGE_SHIFT;
1599
1600         while (!list_empty(&async_chunk->extents)) {
1601                 async_extent = list_entry(async_chunk->extents.next,
1602                                           struct async_extent, list);
1603                 list_del(&async_extent->list);
1604                 submit_one_async_extent(async_chunk, async_extent, &alloc_hint);
1605         }
1606
1607         /* atomic_sub_return implies a barrier */
1608         if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1609             5 * SZ_1M)
1610                 cond_wake_up_nomb(&fs_info->async_submit_wait);
1611 }
1612
1613 static noinline void async_cow_free(struct btrfs_work *work)
1614 {
1615         struct async_chunk *async_chunk;
1616         struct async_cow *async_cow;
1617
1618         async_chunk = container_of(work, struct async_chunk, work);
1619         btrfs_add_delayed_iput(async_chunk->inode);
1620         if (async_chunk->blkcg_css)
1621                 css_put(async_chunk->blkcg_css);
1622
1623         async_cow = async_chunk->async_cow;
1624         if (atomic_dec_and_test(&async_cow->num_chunks))
1625                 kvfree(async_cow);
1626 }
1627
1628 static bool run_delalloc_compressed(struct btrfs_inode *inode,
1629                                     struct page *locked_page, u64 start,
1630                                     u64 end, struct writeback_control *wbc)
1631 {
1632         struct btrfs_fs_info *fs_info = inode->root->fs_info;
1633         struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
1634         struct async_cow *ctx;
1635         struct async_chunk *async_chunk;
1636         unsigned long nr_pages;
1637         u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1638         int i;
1639         unsigned nofs_flag;
1640         const blk_opf_t write_flags = wbc_to_write_flags(wbc);
1641
1642         nofs_flag = memalloc_nofs_save();
1643         ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1644         memalloc_nofs_restore(nofs_flag);
1645         if (!ctx)
1646                 return false;
1647
1648         unlock_extent(&inode->io_tree, start, end, NULL);
1649         set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1650
1651         async_chunk = ctx->chunks;
1652         atomic_set(&ctx->num_chunks, num_chunks);
1653
1654         for (i = 0; i < num_chunks; i++) {
1655                 u64 cur_end = min(end, start + SZ_512K - 1);
1656
1657                 /*
1658                  * igrab is called higher up in the call chain, take only the
1659                  * lightweight reference for the callback lifetime
1660                  */
1661                 ihold(&inode->vfs_inode);
1662                 async_chunk[i].async_cow = ctx;
1663                 async_chunk[i].inode = inode;
1664                 async_chunk[i].start = start;
1665                 async_chunk[i].end = cur_end;
1666                 async_chunk[i].write_flags = write_flags;
1667                 INIT_LIST_HEAD(&async_chunk[i].extents);
1668
1669                 /*
1670                  * The locked_page comes all the way from writepage and its
1671                  * the original page we were actually given.  As we spread
1672                  * this large delalloc region across multiple async_chunk
1673                  * structs, only the first struct needs a pointer to locked_page
1674                  *
1675                  * This way we don't need racey decisions about who is supposed
1676                  * to unlock it.
1677                  */
1678                 if (locked_page) {
1679                         /*
1680                          * Depending on the compressibility, the pages might or
1681                          * might not go through async.  We want all of them to
1682                          * be accounted against wbc once.  Let's do it here
1683                          * before the paths diverge.  wbc accounting is used
1684                          * only for foreign writeback detection and doesn't
1685                          * need full accuracy.  Just account the whole thing
1686                          * against the first page.
1687                          */
1688                         wbc_account_cgroup_owner(wbc, locked_page,
1689                                                  cur_end - start);
1690                         async_chunk[i].locked_page = locked_page;
1691                         locked_page = NULL;
1692                 } else {
1693                         async_chunk[i].locked_page = NULL;
1694                 }
1695
1696                 if (blkcg_css != blkcg_root_css) {
1697                         css_get(blkcg_css);
1698                         async_chunk[i].blkcg_css = blkcg_css;
1699                         async_chunk[i].write_flags |= REQ_BTRFS_CGROUP_PUNT;
1700                 } else {
1701                         async_chunk[i].blkcg_css = NULL;
1702                 }
1703
1704                 btrfs_init_work(&async_chunk[i].work, compress_file_range,
1705                                 submit_compressed_extents, async_cow_free);
1706
1707                 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
1708                 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1709
1710                 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
1711
1712                 start = cur_end + 1;
1713         }
1714         return true;
1715 }
1716
1717 static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1718                                        struct page *locked_page, u64 start,
1719                                        u64 end, struct writeback_control *wbc)
1720 {
1721         u64 done_offset = end;
1722         int ret;
1723
1724         while (start <= end) {
1725                 ret = cow_file_range(inode, locked_page, start, end, &done_offset,
1726                                      true, false);
1727                 if (ret)
1728                         return ret;
1729
1730                 extent_write_locked_range(&inode->vfs_inode, locked_page, start,
1731                                           done_offset, wbc, true);
1732                 start = done_offset + 1;
1733         }
1734
1735         return 1;
1736 }
1737
1738 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1739                                         u64 bytenr, u64 num_bytes, bool nowait)
1740 {
1741         struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr);
1742         struct btrfs_ordered_sum *sums;
1743         int ret;
1744         LIST_HEAD(list);
1745
1746         ret = btrfs_lookup_csums_list(csum_root, bytenr, bytenr + num_bytes - 1,
1747                                       &list, 0, nowait);
1748         if (ret == 0 && list_empty(&list))
1749                 return 0;
1750
1751         while (!list_empty(&list)) {
1752                 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1753                 list_del(&sums->list);
1754                 kfree(sums);
1755         }
1756         if (ret < 0)
1757                 return ret;
1758         return 1;
1759 }
1760
1761 static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
1762                            const u64 start, const u64 end)
1763 {
1764         const bool is_space_ino = btrfs_is_free_space_inode(inode);
1765         const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
1766         const u64 range_bytes = end + 1 - start;
1767         struct extent_io_tree *io_tree = &inode->io_tree;
1768         u64 range_start = start;
1769         u64 count;
1770         int ret;
1771
1772         /*
1773          * If EXTENT_NORESERVE is set it means that when the buffered write was
1774          * made we had not enough available data space and therefore we did not
1775          * reserve data space for it, since we though we could do NOCOW for the
1776          * respective file range (either there is prealloc extent or the inode
1777          * has the NOCOW bit set).
1778          *
1779          * However when we need to fallback to COW mode (because for example the
1780          * block group for the corresponding extent was turned to RO mode by a
1781          * scrub or relocation) we need to do the following:
1782          *
1783          * 1) We increment the bytes_may_use counter of the data space info.
1784          *    If COW succeeds, it allocates a new data extent and after doing
1785          *    that it decrements the space info's bytes_may_use counter and
1786          *    increments its bytes_reserved counter by the same amount (we do
1787          *    this at btrfs_add_reserved_bytes()). So we need to increment the
1788          *    bytes_may_use counter to compensate (when space is reserved at
1789          *    buffered write time, the bytes_may_use counter is incremented);
1790          *
1791          * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1792          *    that if the COW path fails for any reason, it decrements (through
1793          *    extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1794          *    data space info, which we incremented in the step above.
1795          *
1796          * If we need to fallback to cow and the inode corresponds to a free
1797          * space cache inode or an inode of the data relocation tree, we must
1798          * also increment bytes_may_use of the data space_info for the same
1799          * reason. Space caches and relocated data extents always get a prealloc
1800          * extent for them, however scrub or balance may have set the block
1801          * group that contains that extent to RO mode and therefore force COW
1802          * when starting writeback.
1803          */
1804         count = count_range_bits(io_tree, &range_start, end, range_bytes,
1805                                  EXTENT_NORESERVE, 0, NULL);
1806         if (count > 0 || is_space_ino || is_reloc_ino) {
1807                 u64 bytes = count;
1808                 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1809                 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1810
1811                 if (is_space_ino || is_reloc_ino)
1812                         bytes = range_bytes;
1813
1814                 spin_lock(&sinfo->lock);
1815                 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
1816                 spin_unlock(&sinfo->lock);
1817
1818                 if (count > 0)
1819                         clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1820                                          NULL);
1821         }
1822
1823         /*
1824          * Don't try to create inline extents, as a mix of inline extent that
1825          * is written out and unlocked directly and a normal NOCOW extent
1826          * doesn't work.
1827          */
1828         ret = cow_file_range(inode, locked_page, start, end, NULL, false, true);
1829         ASSERT(ret != 1);
1830         return ret;
1831 }
1832
1833 struct can_nocow_file_extent_args {
1834         /* Input fields. */
1835
1836         /* Start file offset of the range we want to NOCOW. */
1837         u64 start;
1838         /* End file offset (inclusive) of the range we want to NOCOW. */
1839         u64 end;
1840         bool writeback_path;
1841         bool strict;
1842         /*
1843          * Free the path passed to can_nocow_file_extent() once it's not needed
1844          * anymore.
1845          */
1846         bool free_path;
1847
1848         /* Output fields. Only set when can_nocow_file_extent() returns 1. */
1849
1850         u64 disk_bytenr;
1851         u64 disk_num_bytes;
1852         u64 extent_offset;
1853         /* Number of bytes that can be written to in NOCOW mode. */
1854         u64 num_bytes;
1855 };
1856
1857 /*
1858  * Check if we can NOCOW the file extent that the path points to.
1859  * This function may return with the path released, so the caller should check
1860  * if path->nodes[0] is NULL or not if it needs to use the path afterwards.
1861  *
1862  * Returns: < 0 on error
1863  *            0 if we can not NOCOW
1864  *            1 if we can NOCOW
1865  */
1866 static int can_nocow_file_extent(struct btrfs_path *path,
1867                                  struct btrfs_key *key,
1868                                  struct btrfs_inode *inode,
1869                                  struct can_nocow_file_extent_args *args)
1870 {
1871         const bool is_freespace_inode = btrfs_is_free_space_inode(inode);
1872         struct extent_buffer *leaf = path->nodes[0];
1873         struct btrfs_root *root = inode->root;
1874         struct btrfs_file_extent_item *fi;
1875         u64 extent_end;
1876         u8 extent_type;
1877         int can_nocow = 0;
1878         int ret = 0;
1879         bool nowait = path->nowait;
1880
1881         fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
1882         extent_type = btrfs_file_extent_type(leaf, fi);
1883
1884         if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1885                 goto out;
1886
1887         /* Can't access these fields unless we know it's not an inline extent. */
1888         args->disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1889         args->disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1890         args->extent_offset = btrfs_file_extent_offset(leaf, fi);
1891
1892         if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1893             extent_type == BTRFS_FILE_EXTENT_REG)
1894                 goto out;
1895
1896         /*
1897          * If the extent was created before the generation where the last snapshot
1898          * for its subvolume was created, then this implies the extent is shared,
1899          * hence we must COW.
1900          */
1901         if (!args->strict &&
1902             btrfs_file_extent_generation(leaf, fi) <=
1903             btrfs_root_last_snapshot(&root->root_item))
1904                 goto out;
1905
1906         /* An explicit hole, must COW. */
1907         if (args->disk_bytenr == 0)
1908                 goto out;
1909
1910         /* Compressed/encrypted/encoded extents must be COWed. */
1911         if (btrfs_file_extent_compression(leaf, fi) ||
1912             btrfs_file_extent_encryption(leaf, fi) ||
1913             btrfs_file_extent_other_encoding(leaf, fi))
1914                 goto out;
1915
1916         extent_end = btrfs_file_extent_end(path);
1917
1918         /*
1919          * The following checks can be expensive, as they need to take other
1920          * locks and do btree or rbtree searches, so release the path to avoid
1921          * blocking other tasks for too long.
1922          */
1923         btrfs_release_path(path);
1924
1925         ret = btrfs_cross_ref_exist(root, btrfs_ino(inode),
1926                                     key->offset - args->extent_offset,
1927                                     args->disk_bytenr, args->strict, path);
1928         WARN_ON_ONCE(ret > 0 && is_freespace_inode);
1929         if (ret != 0)
1930                 goto out;
1931
1932         if (args->free_path) {
1933                 /*
1934                  * We don't need the path anymore, plus through the
1935                  * csum_exist_in_range() call below we will end up allocating
1936                  * another path. So free the path to avoid unnecessary extra
1937                  * memory usage.
1938                  */
1939                 btrfs_free_path(path);
1940                 path = NULL;
1941         }
1942
1943         /* If there are pending snapshots for this root, we must COW. */
1944         if (args->writeback_path && !is_freespace_inode &&
1945             atomic_read(&root->snapshot_force_cow))
1946                 goto out;
1947
1948         args->disk_bytenr += args->extent_offset;
1949         args->disk_bytenr += args->start - key->offset;
1950         args->num_bytes = min(args->end + 1, extent_end) - args->start;
1951
1952         /*
1953          * Force COW if csums exist in the range. This ensures that csums for a
1954          * given extent are either valid or do not exist.
1955          */
1956         ret = csum_exist_in_range(root->fs_info, args->disk_bytenr, args->num_bytes,
1957                                   nowait);
1958         WARN_ON_ONCE(ret > 0 && is_freespace_inode);
1959         if (ret != 0)
1960                 goto out;
1961
1962         can_nocow = 1;
1963  out:
1964         if (args->free_path && path)
1965                 btrfs_free_path(path);
1966
1967         return ret < 0 ? ret : can_nocow;
1968 }
1969
1970 /*
1971  * when nowcow writeback call back.  This checks for snapshots or COW copies
1972  * of the extents that exist in the file, and COWs the file as required.
1973  *
1974  * If no cow copies or snapshots exist, we write directly to the existing
1975  * blocks on disk
1976  */
1977 static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
1978                                        struct page *locked_page,
1979                                        const u64 start, const u64 end)
1980 {
1981         struct btrfs_fs_info *fs_info = inode->root->fs_info;
1982         struct btrfs_root *root = inode->root;
1983         struct btrfs_path *path;
1984         u64 cow_start = (u64)-1;
1985         u64 cur_offset = start;
1986         int ret;
1987         bool check_prev = true;
1988         u64 ino = btrfs_ino(inode);
1989         struct btrfs_block_group *bg;
1990         bool nocow = false;
1991         struct can_nocow_file_extent_args nocow_args = { 0 };
1992
1993         path = btrfs_alloc_path();
1994         if (!path) {
1995                 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1996                                              EXTENT_LOCKED | EXTENT_DELALLOC |
1997                                              EXTENT_DO_ACCOUNTING |
1998                                              EXTENT_DEFRAG, PAGE_UNLOCK |
1999                                              PAGE_START_WRITEBACK |
2000                                              PAGE_END_WRITEBACK);
2001                 return -ENOMEM;
2002         }
2003
2004         nocow_args.end = end;
2005         nocow_args.writeback_path = true;
2006
2007         while (1) {
2008                 struct btrfs_ordered_extent *ordered;
2009                 struct btrfs_key found_key;
2010                 struct btrfs_file_extent_item *fi;
2011                 struct extent_buffer *leaf;
2012                 u64 extent_end;
2013                 u64 ram_bytes;
2014                 u64 nocow_end;
2015                 int extent_type;
2016                 bool is_prealloc;
2017
2018                 nocow = false;
2019
2020                 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
2021                                                cur_offset, 0);
2022                 if (ret < 0)
2023                         goto error;
2024
2025                 /*
2026                  * If there is no extent for our range when doing the initial
2027                  * search, then go back to the previous slot as it will be the
2028                  * one containing the search offset
2029                  */
2030                 if (ret > 0 && path->slots[0] > 0 && check_prev) {
2031                         leaf = path->nodes[0];
2032                         btrfs_item_key_to_cpu(leaf, &found_key,
2033                                               path->slots[0] - 1);
2034                         if (found_key.objectid == ino &&
2035                             found_key.type == BTRFS_EXTENT_DATA_KEY)
2036                                 path->slots[0]--;
2037                 }
2038                 check_prev = false;
2039 next_slot:
2040                 /* Go to next leaf if we have exhausted the current one */
2041                 leaf = path->nodes[0];
2042                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2043                         ret = btrfs_next_leaf(root, path);
2044                         if (ret < 0) {
2045                                 if (cow_start != (u64)-1)
2046                                         cur_offset = cow_start;
2047                                 goto error;
2048                         }
2049                         if (ret > 0)
2050                                 break;
2051                         leaf = path->nodes[0];
2052                 }
2053
2054                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2055
2056                 /* Didn't find anything for our INO */
2057                 if (found_key.objectid > ino)
2058                         break;
2059                 /*
2060                  * Keep searching until we find an EXTENT_ITEM or there are no
2061                  * more extents for this inode
2062                  */
2063                 if (WARN_ON_ONCE(found_key.objectid < ino) ||
2064                     found_key.type < BTRFS_EXTENT_DATA_KEY) {
2065                         path->slots[0]++;
2066                         goto next_slot;
2067                 }
2068
2069                 /* Found key is not EXTENT_DATA_KEY or starts after req range */
2070                 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
2071                     found_key.offset > end)
2072                         break;
2073
2074                 /*
2075                  * If the found extent starts after requested offset, then
2076                  * adjust extent_end to be right before this extent begins
2077                  */
2078                 if (found_key.offset > cur_offset) {
2079                         extent_end = found_key.offset;
2080                         extent_type = 0;
2081                         goto out_check;
2082                 }
2083
2084                 /*
2085                  * Found extent which begins before our range and potentially
2086                  * intersect it
2087                  */
2088                 fi = btrfs_item_ptr(leaf, path->slots[0],
2089                                     struct btrfs_file_extent_item);
2090                 extent_type = btrfs_file_extent_type(leaf, fi);
2091                 /* If this is triggered then we have a memory corruption. */
2092                 ASSERT(extent_type < BTRFS_NR_FILE_EXTENT_TYPES);
2093                 if (WARN_ON(extent_type >= BTRFS_NR_FILE_EXTENT_TYPES)) {
2094                         ret = -EUCLEAN;
2095                         goto error;
2096                 }
2097                 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
2098                 extent_end = btrfs_file_extent_end(path);
2099
2100                 /*
2101                  * If the extent we got ends before our current offset, skip to
2102                  * the next extent.
2103                  */
2104                 if (extent_end <= cur_offset) {
2105                         path->slots[0]++;
2106                         goto next_slot;
2107                 }
2108
2109                 nocow_args.start = cur_offset;
2110                 ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args);
2111                 if (ret < 0) {
2112                         if (cow_start != (u64)-1)
2113                                 cur_offset = cow_start;
2114                         goto error;
2115                 } else if (ret == 0) {
2116                         goto out_check;
2117                 }
2118
2119                 ret = 0;
2120                 bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr);
2121                 if (bg)
2122                         nocow = true;
2123 out_check:
2124                 /*
2125                  * If nocow is false then record the beginning of the range
2126                  * that needs to be COWed
2127                  */
2128                 if (!nocow) {
2129                         if (cow_start == (u64)-1)
2130                                 cow_start = cur_offset;
2131                         cur_offset = extent_end;
2132                         if (cur_offset > end)
2133                                 break;
2134                         if (!path->nodes[0])
2135                                 continue;
2136                         path->slots[0]++;
2137                         goto next_slot;
2138                 }
2139
2140                 /*
2141                  * COW range from cow_start to found_key.offset - 1. As the key
2142                  * will contain the beginning of the first extent that can be
2143                  * NOCOW, following one which needs to be COW'ed
2144                  */
2145                 if (cow_start != (u64)-1) {
2146                         ret = fallback_to_cow(inode, locked_page,
2147                                               cow_start, found_key.offset - 1);
2148                         if (ret)
2149                                 goto error;
2150                         cow_start = (u64)-1;
2151                 }
2152
2153                 nocow_end = cur_offset + nocow_args.num_bytes - 1;
2154                 is_prealloc = extent_type == BTRFS_FILE_EXTENT_PREALLOC;
2155                 if (is_prealloc) {
2156                         u64 orig_start = found_key.offset - nocow_args.extent_offset;
2157                         struct extent_map *em;
2158
2159                         em = create_io_em(inode, cur_offset, nocow_args.num_bytes,
2160                                           orig_start,
2161                                           nocow_args.disk_bytenr, /* block_start */
2162                                           nocow_args.num_bytes, /* block_len */
2163                                           nocow_args.disk_num_bytes, /* orig_block_len */
2164                                           ram_bytes, BTRFS_COMPRESS_NONE,
2165                                           BTRFS_ORDERED_PREALLOC);
2166                         if (IS_ERR(em)) {
2167                                 ret = PTR_ERR(em);
2168                                 goto error;
2169                         }
2170                         free_extent_map(em);
2171                 }
2172
2173                 ordered = btrfs_alloc_ordered_extent(inode, cur_offset,
2174                                 nocow_args.num_bytes, nocow_args.num_bytes,
2175                                 nocow_args.disk_bytenr, nocow_args.num_bytes, 0,
2176                                 is_prealloc
2177                                 ? (1 << BTRFS_ORDERED_PREALLOC)
2178                                 : (1 << BTRFS_ORDERED_NOCOW),
2179                                 BTRFS_COMPRESS_NONE);
2180                 if (IS_ERR(ordered)) {
2181                         if (is_prealloc) {
2182                                 btrfs_drop_extent_map_range(inode, cur_offset,
2183                                                             nocow_end, false);
2184                         }
2185                         ret = PTR_ERR(ordered);
2186                         goto error;
2187                 }
2188
2189                 if (nocow) {
2190                         btrfs_dec_nocow_writers(bg);
2191                         nocow = false;
2192                 }
2193
2194                 if (btrfs_is_data_reloc_root(root))
2195                         /*
2196                          * Error handled later, as we must prevent
2197                          * extent_clear_unlock_delalloc() in error handler
2198                          * from freeing metadata of created ordered extent.
2199                          */
2200                         ret = btrfs_reloc_clone_csums(ordered);
2201                 btrfs_put_ordered_extent(ordered);
2202
2203                 extent_clear_unlock_delalloc(inode, cur_offset, nocow_end,
2204                                              locked_page, EXTENT_LOCKED |
2205                                              EXTENT_DELALLOC |
2206                                              EXTENT_CLEAR_DATA_RESV,
2207                                              PAGE_UNLOCK | PAGE_SET_ORDERED);
2208
2209                 cur_offset = extent_end;
2210
2211                 /*
2212                  * btrfs_reloc_clone_csums() error, now we're OK to call error
2213                  * handler, as metadata for created ordered extent will only
2214                  * be freed by btrfs_finish_ordered_io().
2215                  */
2216                 if (ret)
2217                         goto error;
2218                 if (cur_offset > end)
2219                         break;
2220         }
2221         btrfs_release_path(path);
2222
2223         if (cur_offset <= end && cow_start == (u64)-1)
2224                 cow_start = cur_offset;
2225
2226         if (cow_start != (u64)-1) {
2227                 cur_offset = end;
2228                 ret = fallback_to_cow(inode, locked_page, cow_start, end);
2229                 if (ret)
2230                         goto error;
2231         }
2232
2233 error:
2234         if (nocow)
2235                 btrfs_dec_nocow_writers(bg);
2236
2237         if (ret && cur_offset < end)
2238                 extent_clear_unlock_delalloc(inode, cur_offset, end,
2239                                              locked_page, EXTENT_LOCKED |
2240                                              EXTENT_DELALLOC | EXTENT_DEFRAG |
2241                                              EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
2242                                              PAGE_START_WRITEBACK |
2243                                              PAGE_END_WRITEBACK);
2244         btrfs_free_path(path);
2245         return ret;
2246 }
2247
2248 static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
2249 {
2250         if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
2251                 if (inode->defrag_bytes &&
2252                     test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
2253                                    0, NULL))
2254                         return false;
2255                 return true;
2256         }
2257         return false;
2258 }
2259
2260 /*
2261  * Function to process delayed allocation (create CoW) for ranges which are
2262  * being touched for the first time.
2263  */
2264 int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
2265                              u64 start, u64 end, struct writeback_control *wbc)
2266 {
2267         const bool zoned = btrfs_is_zoned(inode->root->fs_info);
2268         int ret;
2269
2270         /*
2271          * The range must cover part of the @locked_page, or a return of 1
2272          * can confuse the caller.
2273          */
2274         ASSERT(!(end <= page_offset(locked_page) ||
2275                  start >= page_offset(locked_page) + PAGE_SIZE));
2276
2277         if (should_nocow(inode, start, end)) {
2278                 /*
2279                  * Normally on a zoned device we're only doing COW writes, but
2280                  * in case of relocation on a zoned filesystem we have taken
2281                  * precaution, that we're only writing sequentially. It's safe
2282                  * to use run_delalloc_nocow() here, like for  regular
2283                  * preallocated inodes.
2284                  */
2285                 ASSERT(!zoned || btrfs_is_data_reloc_root(inode->root));
2286                 ret = run_delalloc_nocow(inode, locked_page, start, end);
2287                 goto out;
2288         }
2289
2290         if (btrfs_inode_can_compress(inode) &&
2291             inode_need_compress(inode, start, end) &&
2292             run_delalloc_compressed(inode, locked_page, start, end, wbc))
2293                 return 1;
2294
2295         if (zoned)
2296                 ret = run_delalloc_zoned(inode, locked_page, start, end, wbc);
2297         else
2298                 ret = cow_file_range(inode, locked_page, start, end, NULL,
2299                                      false, false);
2300
2301 out:
2302         if (ret < 0)
2303                 btrfs_cleanup_ordered_extents(inode, locked_page, start,
2304                                               end - start + 1);
2305         return ret;
2306 }
2307
2308 void btrfs_split_delalloc_extent(struct btrfs_inode *inode,
2309                                  struct extent_state *orig, u64 split)
2310 {
2311         struct btrfs_fs_info *fs_info = inode->root->fs_info;
2312         u64 size;
2313
2314         /* not delalloc, ignore it */
2315         if (!(orig->state & EXTENT_DELALLOC))
2316                 return;
2317
2318         size = orig->end - orig->start + 1;
2319         if (size > fs_info->max_extent_size) {
2320                 u32 num_extents;
2321                 u64 new_size;
2322
2323                 /*
2324                  * See the explanation in btrfs_merge_delalloc_extent, the same
2325                  * applies here, just in reverse.
2326                  */
2327                 new_size = orig->end - split + 1;
2328                 num_extents = count_max_extents(fs_info, new_size);
2329                 new_size = split - orig->start;
2330                 num_extents += count_max_extents(fs_info, new_size);
2331                 if (count_max_extents(fs_info, size) >= num_extents)
2332                         return;
2333         }
2334
2335         spin_lock(&inode->lock);
2336         btrfs_mod_outstanding_extents(inode, 1);
2337         spin_unlock(&inode->lock);
2338 }
2339
2340 /*
2341  * Handle merged delayed allocation extents so we can keep track of new extents
2342  * that are just merged onto old extents, such as when we are doing sequential
2343  * writes, so we can properly account for the metadata space we'll need.
2344  */
2345 void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new,
2346                                  struct extent_state *other)
2347 {
2348         struct btrfs_fs_info *fs_info = inode->root->fs_info;
2349         u64 new_size, old_size;
2350         u32 num_extents;
2351
2352         /* not delalloc, ignore it */
2353         if (!(other->state & EXTENT_DELALLOC))
2354                 return;
2355
2356         if (new->start > other->start)
2357                 new_size = new->end - other->start + 1;
2358         else
2359                 new_size = other->end - new->start + 1;
2360
2361         /* we're not bigger than the max, unreserve the space and go */
2362         if (new_size <= fs_info->max_extent_size) {
2363                 spin_lock(&inode->lock);
2364                 btrfs_mod_outstanding_extents(inode, -1);
2365                 spin_unlock(&inode->lock);
2366                 return;
2367         }
2368
2369         /*
2370          * We have to add up either side to figure out how many extents were
2371          * accounted for before we merged into one big extent.  If the number of
2372          * extents we accounted for is <= the amount we need for the new range
2373          * then we can return, otherwise drop.  Think of it like this
2374          *
2375          * [ 4k][MAX_SIZE]
2376          *
2377          * So we've grown the extent by a MAX_SIZE extent, this would mean we
2378          * need 2 outstanding extents, on one side we have 1 and the other side
2379          * we have 1 so they are == and we can return.  But in this case
2380          *
2381          * [MAX_SIZE+4k][MAX_SIZE+4k]
2382          *
2383          * Each range on their own accounts for 2 extents, but merged together
2384          * they are only 3 extents worth of accounting, so we need to drop in
2385          * this case.
2386          */
2387         old_size = other->end - other->start + 1;
2388         num_extents = count_max_extents(fs_info, old_size);
2389         old_size = new->end - new->start + 1;
2390         num_extents += count_max_extents(fs_info, old_size);
2391         if (count_max_extents(fs_info, new_size) >= num_extents)
2392                 return;
2393
2394         spin_lock(&inode->lock);
2395         btrfs_mod_outstanding_extents(inode, -1);
2396         spin_unlock(&inode->lock);
2397 }
2398
2399 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2400                                       struct btrfs_inode *inode)
2401 {
2402         struct btrfs_fs_info *fs_info = inode->root->fs_info;
2403
2404         spin_lock(&root->delalloc_lock);
2405         if (list_empty(&inode->delalloc_inodes)) {
2406                 list_add_tail(&inode->delalloc_inodes, &root->delalloc_inodes);
2407                 set_bit(BTRFS_INODE_IN_DELALLOC_LIST, &inode->runtime_flags);
2408                 root->nr_delalloc_inodes++;
2409                 if (root->nr_delalloc_inodes == 1) {
2410                         spin_lock(&fs_info->delalloc_root_lock);
2411                         BUG_ON(!list_empty(&root->delalloc_root));
2412                         list_add_tail(&root->delalloc_root,
2413                                       &fs_info->delalloc_roots);
2414                         spin_unlock(&fs_info->delalloc_root_lock);
2415                 }
2416         }
2417         spin_unlock(&root->delalloc_lock);
2418 }
2419
2420 void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2421                                 struct btrfs_inode *inode)
2422 {
2423         struct btrfs_fs_info *fs_info = root->fs_info;
2424
2425         if (!list_empty(&inode->delalloc_inodes)) {
2426                 list_del_init(&inode->delalloc_inodes);
2427                 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2428                           &inode->runtime_flags);
2429                 root->nr_delalloc_inodes--;
2430                 if (!root->nr_delalloc_inodes) {
2431                         ASSERT(list_empty(&root->delalloc_inodes));
2432                         spin_lock(&fs_info->delalloc_root_lock);
2433                         BUG_ON(list_empty(&root->delalloc_root));
2434                         list_del_init(&root->delalloc_root);
2435                         spin_unlock(&fs_info->delalloc_root_lock);
2436                 }
2437         }
2438 }
2439
2440 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2441                                      struct btrfs_inode *inode)
2442 {
2443         spin_lock(&root->delalloc_lock);
2444         __btrfs_del_delalloc_inode(root, inode);
2445         spin_unlock(&root->delalloc_lock);
2446 }
2447
2448 /*
2449  * Properly track delayed allocation bytes in the inode and to maintain the
2450  * list of inodes that have pending delalloc work to be done.
2451  */
2452 void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state,
2453                                u32 bits)
2454 {
2455         struct btrfs_fs_info *fs_info = inode->root->fs_info;
2456
2457         if ((bits & EXTENT_DEFRAG) && !(bits & EXTENT_DELALLOC))
2458                 WARN_ON(1);
2459         /*
2460          * set_bit and clear bit hooks normally require _irqsave/restore
2461          * but in this case, we are only testing for the DELALLOC
2462          * bit, which is only set or cleared with irqs on
2463          */
2464         if (!(state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
2465                 struct btrfs_root *root = inode->root;
2466                 u64 len = state->end + 1 - state->start;
2467                 u32 num_extents = count_max_extents(fs_info, len);
2468                 bool do_list = !btrfs_is_free_space_inode(inode);
2469
2470                 spin_lock(&inode->lock);
2471                 btrfs_mod_outstanding_extents(inode, num_extents);
2472                 spin_unlock(&inode->lock);
2473
2474                 /* For sanity tests */
2475                 if (btrfs_is_testing(fs_info))
2476                         return;
2477
2478                 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2479                                          fs_info->delalloc_batch);
2480                 spin_lock(&inode->lock);
2481                 inode->delalloc_bytes += len;
2482                 if (bits & EXTENT_DEFRAG)
2483                         inode->defrag_bytes += len;
2484                 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2485                                          &inode->runtime_flags))
2486                         btrfs_add_delalloc_inodes(root, inode);
2487                 spin_unlock(&inode->lock);
2488         }
2489
2490         if (!(state->state & EXTENT_DELALLOC_NEW) &&
2491             (bits & EXTENT_DELALLOC_NEW)) {
2492                 spin_lock(&inode->lock);
2493                 inode->new_delalloc_bytes += state->end + 1 - state->start;
2494                 spin_unlock(&inode->lock);
2495         }
2496 }
2497
2498 /*
2499  * Once a range is no longer delalloc this function ensures that proper
2500  * accounting happens.
2501  */
2502 void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
2503                                  struct extent_state *state, u32 bits)
2504 {
2505         struct btrfs_fs_info *fs_info = inode->root->fs_info;
2506         u64 len = state->end + 1 - state->start;
2507         u32 num_extents = count_max_extents(fs_info, len);
2508
2509         if ((state->state & EXTENT_DEFRAG) && (bits & EXTENT_DEFRAG)) {
2510                 spin_lock(&inode->lock);
2511                 inode->defrag_bytes -= len;
2512                 spin_unlock(&inode->lock);
2513         }
2514
2515         /*
2516          * set_bit and clear bit hooks normally require _irqsave/restore
2517          * but in this case, we are only testing for the DELALLOC
2518          * bit, which is only set or cleared with irqs on
2519          */
2520         if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
2521                 struct btrfs_root *root = inode->root;
2522                 bool do_list = !btrfs_is_free_space_inode(inode);
2523
2524                 spin_lock(&inode->lock);
2525                 btrfs_mod_outstanding_extents(inode, -num_extents);
2526                 spin_unlock(&inode->lock);
2527
2528                 /*
2529                  * We don't reserve metadata space for space cache inodes so we
2530                  * don't need to call delalloc_release_metadata if there is an
2531                  * error.
2532                  */
2533                 if (bits & EXTENT_CLEAR_META_RESV &&
2534                     root != fs_info->tree_root)
2535                         btrfs_delalloc_release_metadata(inode, len, false);
2536
2537                 /* For sanity tests. */
2538                 if (btrfs_is_testing(fs_info))
2539                         return;
2540
2541                 if (!btrfs_is_data_reloc_root(root) &&
2542                     do_list && !(state->state & EXTENT_NORESERVE) &&
2543                     (bits & EXTENT_CLEAR_DATA_RESV))
2544                         btrfs_free_reserved_data_space_noquota(fs_info, len);
2545
2546                 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2547                                          fs_info->delalloc_batch);
2548                 spin_lock(&inode->lock);
2549                 inode->delalloc_bytes -= len;
2550                 if (do_list && inode->delalloc_bytes == 0 &&
2551                     test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2552                                         &inode->runtime_flags))
2553                         btrfs_del_delalloc_inode(root, inode);
2554                 spin_unlock(&inode->lock);
2555         }
2556
2557         if ((state->state & EXTENT_DELALLOC_NEW) &&
2558             (bits & EXTENT_DELALLOC_NEW)) {
2559                 spin_lock(&inode->lock);
2560                 ASSERT(inode->new_delalloc_bytes >= len);
2561                 inode->new_delalloc_bytes -= len;
2562                 if (bits & EXTENT_ADD_INODE_BYTES)
2563                         inode_add_bytes(&inode->vfs_inode, len);
2564                 spin_unlock(&inode->lock);
2565         }
2566 }
2567
2568 static int btrfs_extract_ordered_extent(struct btrfs_bio *bbio,
2569                                         struct btrfs_ordered_extent *ordered)
2570 {
2571         u64 start = (u64)bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
2572         u64 len = bbio->bio.bi_iter.bi_size;
2573         struct btrfs_ordered_extent *new;
2574         int ret;
2575
2576         /* Must always be called for the beginning of an ordered extent. */
2577         if (WARN_ON_ONCE(start != ordered->disk_bytenr))
2578                 return -EINVAL;
2579
2580         /* No need to split if the ordered extent covers the entire bio. */
2581         if (ordered->disk_num_bytes == len) {
2582                 refcount_inc(&ordered->refs);
2583                 bbio->ordered = ordered;
2584                 return 0;
2585         }
2586
2587         /*
2588          * Don't split the extent_map for NOCOW extents, as we're writing into
2589          * a pre-existing one.
2590          */
2591         if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
2592                 ret = split_extent_map(bbio->inode, bbio->file_offset,
2593                                        ordered->num_bytes, len,
2594                                        ordered->disk_bytenr);
2595                 if (ret)
2596                         return ret;
2597         }
2598
2599         new = btrfs_split_ordered_extent(ordered, len);
2600         if (IS_ERR(new))
2601                 return PTR_ERR(new);
2602         bbio->ordered = new;
2603         return 0;
2604 }
2605
2606 /*
2607  * given a list of ordered sums record them in the inode.  This happens
2608  * at IO completion time based on sums calculated at bio submission time.
2609  */
2610 static int add_pending_csums(struct btrfs_trans_handle *trans,
2611                              struct list_head *list)
2612 {
2613         struct btrfs_ordered_sum *sum;
2614         struct btrfs_root *csum_root = NULL;
2615         int ret;
2616
2617         list_for_each_entry(sum, list, list) {
2618                 trans->adding_csums = true;
2619                 if (!csum_root)
2620                         csum_root = btrfs_csum_root(trans->fs_info,
2621                                                     sum->logical);
2622                 ret = btrfs_csum_file_blocks(trans, csum_root, sum);
2623                 trans->adding_csums = false;
2624                 if (ret)
2625                         return ret;
2626         }
2627         return 0;
2628 }
2629
2630 static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2631                                          const u64 start,
2632                                          const u64 len,
2633                                          struct extent_state **cached_state)
2634 {
2635         u64 search_start = start;
2636         const u64 end = start + len - 1;
2637
2638         while (search_start < end) {
2639                 const u64 search_len = end - search_start + 1;
2640                 struct extent_map *em;
2641                 u64 em_len;
2642                 int ret = 0;
2643
2644                 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2645                 if (IS_ERR(em))
2646                         return PTR_ERR(em);
2647
2648                 if (em->block_start != EXTENT_MAP_HOLE)
2649                         goto next;
2650
2651                 em_len = em->len;
2652                 if (em->start < search_start)
2653                         em_len -= search_start - em->start;
2654                 if (em_len > search_len)
2655                         em_len = search_len;
2656
2657                 ret = set_extent_bit(&inode->io_tree, search_start,
2658                                      search_start + em_len - 1,
2659                                      EXTENT_DELALLOC_NEW, cached_state);
2660 next:
2661                 search_start = extent_map_end(em);
2662                 free_extent_map(em);
2663                 if (ret)
2664                         return ret;
2665         }
2666         return 0;
2667 }
2668
2669 int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2670                               unsigned int extra_bits,
2671                               struct extent_state **cached_state)
2672 {
2673         WARN_ON(PAGE_ALIGNED(end));
2674
2675         if (start >= i_size_read(&inode->vfs_inode) &&
2676             !(inode->flags & BTRFS_INODE_PREALLOC)) {
2677                 /*
2678                  * There can't be any extents following eof in this case so just
2679                  * set the delalloc new bit for the range directly.
2680                  */
2681                 extra_bits |= EXTENT_DELALLOC_NEW;
2682         } else {
2683                 int ret;
2684
2685                 ret = btrfs_find_new_delalloc_bytes(inode, start,
2686                                                     end + 1 - start,
2687                                                     cached_state);
2688                 if (ret)
2689                         return ret;
2690         }
2691
2692         return set_extent_bit(&inode->io_tree, start, end,
2693                               EXTENT_DELALLOC | extra_bits, cached_state);
2694 }
2695
2696 /* see btrfs_writepage_start_hook for details on why this is required */
2697 struct btrfs_writepage_fixup {
2698         struct page *page;
2699         struct btrfs_inode *inode;
2700         struct btrfs_work work;
2701 };
2702
2703 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2704 {
2705         struct btrfs_writepage_fixup *fixup =
2706                 container_of(work, struct btrfs_writepage_fixup, work);
2707         struct btrfs_ordered_extent *ordered;
2708         struct extent_state *cached_state = NULL;
2709         struct extent_changeset *data_reserved = NULL;
2710         struct page *page = fixup->page;
2711         struct btrfs_inode *inode = fixup->inode;
2712         struct btrfs_fs_info *fs_info = inode->root->fs_info;
2713         u64 page_start = page_offset(page);
2714         u64 page_end = page_offset(page) + PAGE_SIZE - 1;
2715         int ret = 0;
2716         bool free_delalloc_space = true;
2717
2718         /*
2719          * This is similar to page_mkwrite, we need to reserve the space before
2720          * we take the page lock.
2721          */
2722         ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2723                                            PAGE_SIZE);
2724 again:
2725         lock_page(page);
2726
2727         /*
2728          * Before we queued this fixup, we took a reference on the page.
2729          * page->mapping may go NULL, but it shouldn't be moved to a different
2730          * address space.
2731          */
2732         if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2733                 /*
2734                  * Unfortunately this is a little tricky, either
2735                  *
2736                  * 1) We got here and our page had already been dealt with and
2737                  *    we reserved our space, thus ret == 0, so we need to just
2738                  *    drop our space reservation and bail.  This can happen the
2739                  *    first time we come into the fixup worker, or could happen
2740                  *    while waiting for the ordered extent.
2741                  * 2) Our page was already dealt with, but we happened to get an
2742                  *    ENOSPC above from the btrfs_delalloc_reserve_space.  In
2743                  *    this case we obviously don't have anything to release, but
2744                  *    because the page was already dealt with we don't want to
2745                  *    mark the page with an error, so make sure we're resetting
2746                  *    ret to 0.  This is why we have this check _before_ the ret
2747                  *    check, because we do not want to have a surprise ENOSPC
2748                  *    when the page was already properly dealt with.
2749                  */
2750                 if (!ret) {
2751                         btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2752                         btrfs_delalloc_release_space(inode, data_reserved,
2753                                                      page_start, PAGE_SIZE,
2754                                                      true);
2755                 }
2756                 ret = 0;
2757                 goto out_page;
2758         }
2759
2760         /*
2761          * We can't mess with the page state unless it is locked, so now that
2762          * it is locked bail if we failed to make our space reservation.
2763          */
2764         if (ret)
2765                 goto out_page;
2766
2767         lock_extent(&inode->io_tree, page_start, page_end, &cached_state);
2768
2769         /* already ordered? We're done */
2770         if (PageOrdered(page))
2771                 goto out_reserved;
2772
2773         ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
2774         if (ordered) {
2775                 unlock_extent(&inode->io_tree, page_start, page_end,
2776                               &cached_state);
2777                 unlock_page(page);
2778                 btrfs_start_ordered_extent(ordered);
2779                 btrfs_put_ordered_extent(ordered);
2780                 goto again;
2781         }
2782
2783         ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2784                                         &cached_state);
2785         if (ret)
2786                 goto out_reserved;
2787
2788         /*
2789          * Everything went as planned, we're now the owner of a dirty page with
2790          * delayed allocation bits set and space reserved for our COW
2791          * destination.
2792          *
2793          * The page was dirty when we started, nothing should have cleaned it.
2794          */
2795         BUG_ON(!PageDirty(page));
2796         free_delalloc_space = false;
2797 out_reserved:
2798         btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2799         if (free_delalloc_space)
2800                 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2801                                              PAGE_SIZE, true);
2802         unlock_extent(&inode->io_tree, page_start, page_end, &cached_state);
2803 out_page:
2804         if (ret) {
2805                 /*
2806                  * We hit ENOSPC or other errors.  Update the mapping and page
2807                  * to reflect the errors and clean the page.
2808                  */
2809                 mapping_set_error(page->mapping, ret);
2810                 btrfs_mark_ordered_io_finished(inode, page, page_start,
2811                                                PAGE_SIZE, !ret);
2812                 btrfs_page_clear_uptodate(fs_info, page, page_start, PAGE_SIZE);
2813                 clear_page_dirty_for_io(page);
2814         }
2815         btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);
2816         unlock_page(page);
2817         put_page(page);
2818         kfree(fixup);
2819         extent_changeset_free(data_reserved);
2820         /*
2821          * As a precaution, do a delayed iput in case it would be the last iput
2822          * that could need flushing space. Recursing back to fixup worker would
2823          * deadlock.
2824          */
2825         btrfs_add_delayed_iput(inode);
2826 }
2827
2828 /*
2829  * There are a few paths in the higher layers of the kernel that directly
2830  * set the page dirty bit without asking the filesystem if it is a
2831  * good idea.  This causes problems because we want to make sure COW
2832  * properly happens and the data=ordered rules are followed.
2833  *
2834  * In our case any range that doesn't have the ORDERED bit set
2835  * hasn't been properly setup for IO.  We kick off an async process
2836  * to fix it up.  The async helper will wait for ordered extents, set
2837  * the delalloc bit and make it safe to write the page.
2838  */
2839 int btrfs_writepage_cow_fixup(struct page *page)
2840 {
2841         struct inode *inode = page->mapping->host;
2842         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2843         struct btrfs_writepage_fixup *fixup;
2844
2845         /* This page has ordered extent covering it already */
2846         if (PageOrdered(page))
2847                 return 0;
2848
2849         /*
2850          * PageChecked is set below when we create a fixup worker for this page,
2851          * don't try to create another one if we're already PageChecked()
2852          *
2853          * The extent_io writepage code will redirty the page if we send back
2854          * EAGAIN.
2855          */
2856         if (PageChecked(page))
2857                 return -EAGAIN;
2858
2859         fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2860         if (!fixup)
2861                 return -EAGAIN;
2862
2863         /*
2864          * We are already holding a reference to this inode from
2865          * write_cache_pages.  We need to hold it because the space reservation
2866          * takes place outside of the page lock, and we can't trust
2867          * page->mapping outside of the page lock.
2868          */
2869         ihold(inode);
2870         btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);
2871         get_page(page);
2872         btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
2873         fixup->page = page;
2874         fixup->inode = BTRFS_I(inode);
2875         btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2876
2877         return -EAGAIN;
2878 }
2879
2880 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2881                                        struct btrfs_inode *inode, u64 file_pos,
2882                                        struct btrfs_file_extent_item *stack_fi,
2883                                        const bool update_inode_bytes,
2884                                        u64 qgroup_reserved)
2885 {
2886         struct btrfs_root *root = inode->root;
2887         const u64 sectorsize = root->fs_info->sectorsize;
2888         struct btrfs_path *path;
2889         struct extent_buffer *leaf;
2890         struct btrfs_key ins;
2891         u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2892         u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2893         u64 offset = btrfs_stack_file_extent_offset(stack_fi);
2894         u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2895         u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
2896         struct btrfs_drop_extents_args drop_args = { 0 };
2897         int ret;
2898
2899         path = btrfs_alloc_path();
2900         if (!path)
2901                 return -ENOMEM;
2902
2903         /*
2904          * we may be replacing one extent in the tree with another.
2905          * The new extent is pinned in the extent map, and we don't want
2906          * to drop it from the cache until it is completely in the btree.
2907          *
2908          * So, tell btrfs_drop_extents to leave this extent in the cache.
2909          * the caller is expected to unpin it and allow it to be merged
2910          * with the others.
2911          */
2912         drop_args.path = path;
2913         drop_args.start = file_pos;
2914         drop_args.end = file_pos + num_bytes;
2915         drop_args.replace_extent = true;
2916         drop_args.extent_item_size = sizeof(*stack_fi);
2917         ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2918         if (ret)
2919                 goto out;
2920
2921         if (!drop_args.extent_inserted) {
2922                 ins.objectid = btrfs_ino(inode);
2923                 ins.offset = file_pos;
2924                 ins.type = BTRFS_EXTENT_DATA_KEY;
2925
2926                 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2927                                               sizeof(*stack_fi));
2928                 if (ret)
2929                         goto out;
2930         }
2931         leaf = path->nodes[0];
2932         btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2933         write_extent_buffer(leaf, stack_fi,
2934                         btrfs_item_ptr_offset(leaf, path->slots[0]),
2935                         sizeof(struct btrfs_file_extent_item));
2936
2937         btrfs_mark_buffer_dirty(leaf);
2938         btrfs_release_path(path);
2939
2940         /*
2941          * If we dropped an inline extent here, we know the range where it is
2942          * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2943          * number of bytes only for that range containing the inline extent.
2944          * The remaining of the range will be processed when clearning the
2945          * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2946          */
2947         if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2948                 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2949
2950                 inline_size = drop_args.bytes_found - inline_size;
2951                 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2952                 drop_args.bytes_found -= inline_size;
2953                 num_bytes -= sectorsize;
2954         }
2955
2956         if (update_inode_bytes)
2957                 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
2958
2959         ins.objectid = disk_bytenr;
2960         ins.offset = disk_num_bytes;
2961         ins.type = BTRFS_EXTENT_ITEM_KEY;
2962
2963         ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
2964         if (ret)
2965                 goto out;
2966
2967         ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
2968                                                file_pos - offset,
2969                                                qgroup_reserved, &ins);
2970 out:
2971         btrfs_free_path(path);
2972
2973         return ret;
2974 }
2975
2976 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2977                                          u64 start, u64 len)
2978 {
2979         struct btrfs_block_group *cache;
2980
2981         cache = btrfs_lookup_block_group(fs_info, start);
2982         ASSERT(cache);
2983
2984         spin_lock(&cache->lock);
2985         cache->delalloc_bytes -= len;
2986         spin_unlock(&cache->lock);
2987
2988         btrfs_put_block_group(cache);
2989 }
2990
2991 static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2992                                              struct btrfs_ordered_extent *oe)
2993 {
2994         struct btrfs_file_extent_item stack_fi;
2995         bool update_inode_bytes;
2996         u64 num_bytes = oe->num_bytes;
2997         u64 ram_bytes = oe->ram_bytes;
2998
2999         memset(&stack_fi, 0, sizeof(stack_fi));
3000         btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
3001         btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
3002         btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
3003                                                    oe->disk_num_bytes);
3004         btrfs_set_stack_file_extent_offset(&stack_fi, oe->offset);
3005         if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) {
3006                 num_bytes = oe->truncated_len;
3007                 ram_bytes = num_bytes;
3008         }
3009         btrfs_set_stack_file_extent_num_bytes(&stack_fi, num_bytes);
3010         btrfs_set_stack_file_extent_ram_bytes(&stack_fi, ram_bytes);
3011         btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
3012         /* Encryption and other encoding is reserved and all 0 */
3013
3014         /*
3015          * For delalloc, when completing an ordered extent we update the inode's
3016          * bytes when clearing the range in the inode's io tree, so pass false
3017          * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
3018          * except if the ordered extent was truncated.
3019          */
3020         update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
3021                              test_bit(BTRFS_ORDERED_ENCODED, &oe->flags) ||
3022                              test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
3023
3024         return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
3025                                            oe->file_offset, &stack_fi,
3026                                            update_inode_bytes, oe->qgroup_rsv);
3027 }
3028
3029 /*
3030  * As ordered data IO finishes, this gets called so we can finish
3031  * an ordered extent if the range of bytes in the file it covers are
3032  * fully written.
3033  */
3034 int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
3035 {
3036         struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
3037         struct btrfs_root *root = inode->root;
3038         struct btrfs_fs_info *fs_info = root->fs_info;
3039         struct btrfs_trans_handle *trans = NULL;
3040         struct extent_io_tree *io_tree = &inode->io_tree;
3041         struct extent_state *cached_state = NULL;
3042         u64 start, end;
3043         int compress_type = 0;
3044         int ret = 0;
3045         u64 logical_len = ordered_extent->num_bytes;
3046         bool freespace_inode;
3047         bool truncated = false;
3048         bool clear_reserved_extent = true;
3049         unsigned int clear_bits = EXTENT_DEFRAG;
3050
3051         start = ordered_extent->file_offset;
3052         end = start + ordered_extent->num_bytes - 1;
3053
3054         if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3055             !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3056             !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags) &&
3057             !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags))
3058                 clear_bits |= EXTENT_DELALLOC_NEW;
3059
3060         freespace_inode = btrfs_is_free_space_inode(inode);
3061         if (!freespace_inode)
3062                 btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent);
3063
3064         if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3065                 ret = -EIO;
3066                 goto out;
3067         }
3068
3069         if (btrfs_is_zoned(fs_info))
3070                 btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr,
3071                                         ordered_extent->disk_num_bytes);
3072
3073         if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3074                 truncated = true;
3075                 logical_len = ordered_extent->truncated_len;
3076                 /* Truncated the entire extent, don't bother adding */
3077                 if (!logical_len)
3078                         goto out;
3079         }
3080
3081         if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
3082                 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
3083
3084                 btrfs_inode_safe_disk_i_size_write(inode, 0);
3085                 if (freespace_inode)
3086                         trans = btrfs_join_transaction_spacecache(root);
3087                 else
3088                         trans = btrfs_join_transaction(root);
3089                 if (IS_ERR(trans)) {
3090                         ret = PTR_ERR(trans);
3091                         trans = NULL;
3092                         goto out;
3093                 }
3094                 trans->block_rsv = &inode->block_rsv;
3095                 ret = btrfs_update_inode_fallback(trans, root, inode);
3096                 if (ret) /* -ENOMEM or corruption */
3097                         btrfs_abort_transaction(trans, ret);
3098                 goto out;
3099         }
3100
3101         clear_bits |= EXTENT_LOCKED;
3102         lock_extent(io_tree, start, end, &cached_state);
3103
3104         if (freespace_inode)
3105                 trans = btrfs_join_transaction_spacecache(root);
3106         else
3107                 trans = btrfs_join_transaction(root);
3108         if (IS_ERR(trans)) {
3109                 ret = PTR_ERR(trans);
3110                 trans = NULL;
3111                 goto out;
3112         }
3113
3114         trans->block_rsv = &inode->block_rsv;
3115
3116         if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3117                 compress_type = ordered_extent->compress_type;
3118         if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3119                 BUG_ON(compress_type);
3120                 ret = btrfs_mark_extent_written(trans, inode,
3121                                                 ordered_extent->file_offset,
3122                                                 ordered_extent->file_offset +
3123                                                 logical_len);
3124                 btrfs_zoned_release_data_reloc_bg(fs_info, ordered_extent->disk_bytenr,
3125                                                   ordered_extent->disk_num_bytes);
3126         } else {
3127                 BUG_ON(root == fs_info->tree_root);
3128                 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
3129                 if (!ret) {
3130                         clear_reserved_extent = false;
3131                         btrfs_release_delalloc_bytes(fs_info,
3132                                                 ordered_extent->disk_bytenr,
3133                                                 ordered_extent->disk_num_bytes);
3134                 }
3135         }
3136         unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
3137                            ordered_extent->num_bytes, trans->transid);
3138         if (ret < 0) {
3139                 btrfs_abort_transaction(trans, ret);
3140                 goto out;
3141         }
3142
3143         ret = add_pending_csums(trans, &ordered_extent->list);
3144         if (ret) {
3145                 btrfs_abort_transaction(trans, ret);
3146                 goto out;
3147         }
3148
3149         /*
3150          * If this is a new delalloc range, clear its new delalloc flag to
3151          * update the inode's number of bytes. This needs to be done first
3152          * before updating the inode item.
3153          */
3154         if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3155             !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
3156                 clear_extent_bit(&inode->io_tree, start, end,
3157                                  EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3158                                  &cached_state);
3159
3160         btrfs_inode_safe_disk_i_size_write(inode, 0);
3161         ret = btrfs_update_inode_fallback(trans, root, inode);
3162         if (ret) { /* -ENOMEM or corruption */
3163                 btrfs_abort_transaction(trans, ret);
3164                 goto out;
3165         }
3166         ret = 0;
3167 out:
3168         clear_extent_bit(&inode->io_tree, start, end, clear_bits,
3169                          &cached_state);
3170
3171         if (trans)
3172                 btrfs_end_transaction(trans);
3173
3174         if (ret || truncated) {
3175                 u64 unwritten_start = start;
3176
3177                 /*
3178                  * If we failed to finish this ordered extent for any reason we
3179                  * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3180                  * extent, and mark the inode with the error if it wasn't
3181                  * already set.  Any error during writeback would have already
3182                  * set the mapping error, so we need to set it if we're the ones
3183                  * marking this ordered extent as failed.
3184                  */
3185                 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3186                                              &ordered_extent->flags))
3187                         mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3188
3189                 if (truncated)
3190                         unwritten_start += logical_len;
3191                 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
3192
3193                 /* Drop extent maps for the part of the extent we didn't write. */
3194                 btrfs_drop_extent_map_range(inode, unwritten_start, end, false);
3195
3196                 /*
3197                  * If the ordered extent had an IOERR or something else went
3198                  * wrong we need to return the space for this ordered extent
3199                  * back to the allocator.  We only free the extent in the
3200                  * truncated case if we didn't write out the extent at all.
3201                  *
3202                  * If we made it past insert_reserved_file_extent before we
3203                  * errored out then we don't need to do this as the accounting
3204                  * has already been done.
3205                  */
3206                 if ((ret || !logical_len) &&
3207                     clear_reserved_extent &&
3208                     !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3209                     !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3210                         /*
3211                          * Discard the range before returning it back to the
3212                          * free space pool
3213                          */
3214                         if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
3215                                 btrfs_discard_extent(fs_info,
3216                                                 ordered_extent->disk_bytenr,
3217                                                 ordered_extent->disk_num_bytes,
3218                                                 NULL);
3219                         btrfs_free_reserved_extent(fs_info,
3220                                         ordered_extent->disk_bytenr,
3221                                         ordered_extent->disk_num_bytes, 1);
3222                         /*
3223                          * Actually free the qgroup rsv which was released when
3224                          * the ordered extent was created.
3225                          */
3226                         btrfs_qgroup_free_refroot(fs_info, inode->root->root_key.objectid,
3227                                                   ordered_extent->qgroup_rsv,
3228                                                   BTRFS_QGROUP_RSV_DATA);
3229                 }
3230         }
3231
3232         /*
3233          * This needs to be done to make sure anybody waiting knows we are done
3234          * updating everything for this ordered extent.
3235          */
3236         btrfs_remove_ordered_extent(inode, ordered_extent);
3237
3238         /* once for us */
3239         btrfs_put_ordered_extent(ordered_extent);
3240         /* once for the tree */
3241         btrfs_put_ordered_extent(ordered_extent);
3242
3243         return ret;
3244 }
3245
3246 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered)
3247 {
3248         if (btrfs_is_zoned(btrfs_sb(ordered->inode->i_sb)) &&
3249             !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
3250                 btrfs_finish_ordered_zoned(ordered);
3251         return btrfs_finish_one_ordered(ordered);
3252 }
3253
3254 /*
3255  * Verify the checksum for a single sector without any extra action that depend
3256  * on the type of I/O.
3257  */
3258 int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
3259                             u32 pgoff, u8 *csum, const u8 * const csum_expected)
3260 {
3261         SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
3262         char *kaddr;
3263
3264         ASSERT(pgoff + fs_info->sectorsize <= PAGE_SIZE);
3265
3266         shash->tfm = fs_info->csum_shash;
3267
3268         kaddr = kmap_local_page(page) + pgoff;
3269         crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum);
3270         kunmap_local(kaddr);
3271
3272         if (memcmp(csum, csum_expected, fs_info->csum_size))
3273                 return -EIO;
3274         return 0;
3275 }
3276
3277 /*
3278  * Verify the checksum of a single data sector.
3279  *
3280  * @bbio:       btrfs_io_bio which contains the csum
3281  * @dev:        device the sector is on
3282  * @bio_offset: offset to the beginning of the bio (in bytes)
3283  * @bv:         bio_vec to check
3284  *
3285  * Check if the checksum on a data block is valid.  When a checksum mismatch is
3286  * detected, report the error and fill the corrupted range with zero.
3287  *
3288  * Return %true if the sector is ok or had no checksum to start with, else %false.
3289  */
3290 bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
3291                         u32 bio_offset, struct bio_vec *bv)
3292 {
3293         struct btrfs_inode *inode = bbio->inode;
3294         struct btrfs_fs_info *fs_info = inode->root->fs_info;
3295         u64 file_offset = bbio->file_offset + bio_offset;
3296         u64 end = file_offset + bv->bv_len - 1;
3297         u8 *csum_expected;
3298         u8 csum[BTRFS_CSUM_SIZE];
3299
3300         ASSERT(bv->bv_len == fs_info->sectorsize);
3301
3302         if (!bbio->csum)
3303                 return true;
3304
3305         if (btrfs_is_data_reloc_root(inode->root) &&
3306             test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM,
3307                            1, NULL)) {
3308                 /* Skip the range without csum for data reloc inode */
3309                 clear_extent_bits(&inode->io_tree, file_offset, end,
3310                                   EXTENT_NODATASUM);
3311                 return true;
3312         }
3313
3314         csum_expected = bbio->csum + (bio_offset >> fs_info->sectorsize_bits) *
3315                                 fs_info->csum_size;
3316         if (btrfs_check_sector_csum(fs_info, bv->bv_page, bv->bv_offset, csum,
3317                                     csum_expected))
3318                 goto zeroit;
3319         return true;
3320
3321 zeroit:
3322         btrfs_print_data_csum_error(inode, file_offset, csum, csum_expected,
3323                                     bbio->mirror_num);
3324         if (dev)
3325                 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS);
3326         memzero_bvec(bv);
3327         return false;
3328 }
3329
3330 /*
3331  * btrfs_add_delayed_iput - perform a delayed iput on @inode
3332  *
3333  * @inode: The inode we want to perform iput on
3334  *
3335  * This function uses the generic vfs_inode::i_count to track whether we should
3336  * just decrement it (in case it's > 1) or if this is the last iput then link
3337  * the inode to the delayed iput machinery. Delayed iputs are processed at
3338  * transaction commit time/superblock commit/cleaner kthread.
3339  */
3340 void btrfs_add_delayed_iput(struct btrfs_inode *inode)
3341 {
3342         struct btrfs_fs_info *fs_info = inode->root->fs_info;
3343         unsigned long flags;
3344
3345         if (atomic_add_unless(&inode->vfs_inode.i_count, -1, 1))
3346                 return;
3347
3348         atomic_inc(&fs_info->nr_delayed_iputs);
3349         /*
3350          * Need to be irq safe here because we can be called from either an irq
3351          * context (see bio.c and btrfs_put_ordered_extent()) or a non-irq
3352          * context.
3353          */
3354         spin_lock_irqsave(&fs_info->delayed_iput_lock, flags);
3355         ASSERT(list_empty(&inode->delayed_iput));
3356         list_add_tail(&inode->delayed_iput, &fs_info->delayed_iputs);
3357         spin_unlock_irqrestore(&fs_info->delayed_iput_lock, flags);
3358         if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3359                 wake_up_process(fs_info->cleaner_kthread);
3360 }
3361
3362 static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3363                                     struct btrfs_inode *inode)
3364 {
3365         list_del_init(&inode->delayed_iput);
3366         spin_unlock_irq(&fs_info->delayed_iput_lock);
3367         iput(&inode->vfs_inode);
3368         if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3369                 wake_up(&fs_info->delayed_iputs_wait);
3370         spin_lock_irq(&fs_info->delayed_iput_lock);
3371 }
3372
3373 static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3374                                    struct btrfs_inode *inode)
3375 {
3376         if (!list_empty(&inode->delayed_iput)) {
3377                 spin_lock_irq(&fs_info->delayed_iput_lock);
3378                 if (!list_empty(&inode->delayed_iput))
3379                         run_delayed_iput_locked(fs_info, inode);
3380                 spin_unlock_irq(&fs_info->delayed_iput_lock);
3381         }
3382 }
3383
3384 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3385 {
3386         /*
3387          * btrfs_put_ordered_extent() can run in irq context (see bio.c), which
3388          * calls btrfs_add_delayed_iput() and that needs to lock
3389          * fs_info->delayed_iput_lock. So we need to disable irqs here to
3390          * prevent a deadlock.
3391          */
3392         spin_lock_irq(&fs_info->delayed_iput_lock);
3393         while (!list_empty(&fs_info->delayed_iputs)) {
3394                 struct btrfs_inode *inode;
3395
3396                 inode = list_first_entry(&fs_info->delayed_iputs,
3397                                 struct btrfs_inode, delayed_iput);
3398                 run_delayed_iput_locked(fs_info, inode);
3399                 if (need_resched()) {
3400                         spin_unlock_irq(&fs_info->delayed_iput_lock);
3401                         cond_resched();
3402                         spin_lock_irq(&fs_info->delayed_iput_lock);
3403                 }
3404         }
3405         spin_unlock_irq(&fs_info->delayed_iput_lock);
3406 }
3407
3408 /*
3409  * Wait for flushing all delayed iputs
3410  *
3411  * @fs_info:  the filesystem
3412  *
3413  * This will wait on any delayed iputs that are currently running with KILLABLE
3414  * set.  Once they are all done running we will return, unless we are killed in
3415  * which case we return EINTR. This helps in user operations like fallocate etc
3416  * that might get blocked on the iputs.
3417  *
3418  * Return EINTR if we were killed, 0 if nothing's pending
3419  */
3420 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3421 {
3422         int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3423                         atomic_read(&fs_info->nr_delayed_iputs) == 0);
3424         if (ret)
3425                 return -EINTR;
3426         return 0;
3427 }
3428
3429 /*
3430  * This creates an orphan entry for the given inode in case something goes wrong
3431  * in the middle of an unlink.
3432  */
3433 int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3434                      struct btrfs_inode *inode)
3435 {
3436         int ret;
3437
3438         ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3439         if (ret && ret != -EEXIST) {
3440                 btrfs_abort_transaction(trans, ret);
3441                 return ret;
3442         }
3443
3444         return 0;
3445 }
3446
3447 /*
3448  * We have done the delete so we can go ahead and remove the orphan item for
3449  * this particular inode.
3450  */
3451 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3452                             struct btrfs_inode *inode)
3453 {
3454         return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
3455 }
3456
3457 /*
3458  * this cleans up any orphans that may be left on the list from the last use
3459  * of this root.
3460  */
3461 int btrfs_orphan_cleanup(struct btrfs_root *root)
3462 {
3463         struct btrfs_fs_info *fs_info = root->fs_info;
3464         struct btrfs_path *path;
3465         struct extent_buffer *leaf;
3466         struct btrfs_key key, found_key;
3467         struct btrfs_trans_handle *trans;
3468         struct inode *inode;
3469         u64 last_objectid = 0;
3470         int ret = 0, nr_unlink = 0;
3471
3472         if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state))
3473                 return 0;
3474
3475         path = btrfs_alloc_path();
3476         if (!path) {
3477                 ret = -ENOMEM;
3478                 goto out;
3479         }
3480         path->reada = READA_BACK;
3481
3482         key.objectid = BTRFS_ORPHAN_OBJECTID;
3483         key.type = BTRFS_ORPHAN_ITEM_KEY;
3484         key.offset = (u64)-1;
3485
3486         while (1) {
3487                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3488                 if (ret < 0)
3489                         goto out;
3490
3491                 /*
3492                  * if ret == 0 means we found what we were searching for, which
3493                  * is weird, but possible, so only screw with path if we didn't
3494                  * find the key and see if we have stuff that matches
3495                  */
3496                 if (ret > 0) {
3497                         ret = 0;
3498                         if (path->slots[0] == 0)
3499                                 break;
3500                         path->slots[0]--;
3501                 }
3502
3503                 /* pull out the item */
3504                 leaf = path->nodes[0];
3505                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3506
3507                 /* make sure the item matches what we want */
3508                 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3509                         break;
3510                 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3511                         break;
3512
3513                 /* release the path since we're done with it */
3514                 btrfs_release_path(path);
3515
3516                 /*
3517                  * this is where we are basically btrfs_lookup, without the
3518                  * crossing root thing.  we store the inode number in the
3519                  * offset of the orphan item.
3520                  */
3521
3522                 if (found_key.offset == last_objectid) {
3523                         btrfs_err(fs_info,
3524                                   "Error removing orphan entry, stopping orphan cleanup");
3525                         ret = -EINVAL;
3526                         goto out;
3527                 }
3528
3529                 last_objectid = found_key.offset;
3530
3531                 found_key.objectid = found_key.offset;
3532                 found_key.type = BTRFS_INODE_ITEM_KEY;
3533                 found_key.offset = 0;
3534                 inode = btrfs_iget(fs_info->sb, last_objectid, root);
3535                 if (IS_ERR(inode)) {
3536                         ret = PTR_ERR(inode);
3537                         inode = NULL;
3538                         if (ret != -ENOENT)
3539                                 goto out;
3540                 }
3541
3542                 if (!inode && root == fs_info->tree_root) {
3543                         struct btrfs_root *dead_root;
3544                         int is_dead_root = 0;
3545
3546                         /*
3547                          * This is an orphan in the tree root. Currently these
3548                          * could come from 2 sources:
3549                          *  a) a root (snapshot/subvolume) deletion in progress
3550                          *  b) a free space cache inode
3551                          * We need to distinguish those two, as the orphan item
3552                          * for a root must not get deleted before the deletion
3553                          * of the snapshot/subvolume's tree completes.
3554                          *
3555                          * btrfs_find_orphan_roots() ran before us, which has
3556                          * found all deleted roots and loaded them into
3557                          * fs_info->fs_roots_radix. So here we can find if an
3558                          * orphan item corresponds to a deleted root by looking
3559                          * up the root from that radix tree.
3560                          */
3561
3562                         spin_lock(&fs_info->fs_roots_radix_lock);
3563                         dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3564                                                          (unsigned long)found_key.objectid);
3565                         if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3566                                 is_dead_root = 1;
3567                         spin_unlock(&fs_info->fs_roots_radix_lock);
3568
3569                         if (is_dead_root) {
3570                                 /* prevent this orphan from being found again */
3571                                 key.offset = found_key.objectid - 1;
3572                                 continue;
3573                         }
3574
3575                 }
3576
3577                 /*
3578                  * If we have an inode with links, there are a couple of
3579                  * possibilities:
3580                  *
3581                  * 1. We were halfway through creating fsverity metadata for the
3582                  * file. In that case, the orphan item represents incomplete
3583                  * fsverity metadata which must be cleaned up with
3584                  * btrfs_drop_verity_items and deleting the orphan item.
3585
3586                  * 2. Old kernels (before v3.12) used to create an
3587                  * orphan item for truncate indicating that there were possibly
3588                  * extent items past i_size that needed to be deleted. In v3.12,
3589                  * truncate was changed to update i_size in sync with the extent
3590                  * items, but the (useless) orphan item was still created. Since
3591                  * v4.18, we don't create the orphan item for truncate at all.
3592                  *
3593                  * So, this item could mean that we need to do a truncate, but
3594                  * only if this filesystem was last used on a pre-v3.12 kernel
3595                  * and was not cleanly unmounted. The odds of that are quite
3596                  * slim, and it's a pain to do the truncate now, so just delete
3597                  * the orphan item.
3598                  *
3599                  * It's also possible that this orphan item was supposed to be
3600                  * deleted but wasn't. The inode number may have been reused,
3601                  * but either way, we can delete the orphan item.
3602                  */
3603                 if (!inode || inode->i_nlink) {
3604                         if (inode) {
3605                                 ret = btrfs_drop_verity_items(BTRFS_I(inode));
3606                                 iput(inode);
3607                                 inode = NULL;
3608                                 if (ret)
3609                                         goto out;
3610                         }
3611                         trans = btrfs_start_transaction(root, 1);
3612                         if (IS_ERR(trans)) {
3613                                 ret = PTR_ERR(trans);
3614                                 goto out;
3615                         }
3616                         btrfs_debug(fs_info, "auto deleting %Lu",
3617                                     found_key.objectid);
3618                         ret = btrfs_del_orphan_item(trans, root,
3619                                                     found_key.objectid);
3620                         btrfs_end_transaction(trans);
3621                         if (ret)
3622                                 goto out;
3623                         continue;
3624                 }
3625
3626                 nr_unlink++;
3627
3628                 /* this will do delete_inode and everything for us */
3629                 iput(inode);
3630         }
3631         /* release the path since we're done with it */
3632         btrfs_release_path(path);
3633
3634         if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3635                 trans = btrfs_join_transaction(root);
3636                 if (!IS_ERR(trans))
3637                         btrfs_end_transaction(trans);
3638         }
3639
3640         if (nr_unlink)
3641                 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3642
3643 out:
3644         if (ret)
3645                 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3646         btrfs_free_path(path);
3647         return ret;
3648 }
3649
3650 /*
3651  * very simple check to peek ahead in the leaf looking for xattrs.  If we
3652  * don't find any xattrs, we know there can't be any acls.
3653  *
3654  * slot is the slot the inode is in, objectid is the objectid of the inode
3655  */
3656 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3657                                           int slot, u64 objectid,
3658                                           int *first_xattr_slot)
3659 {
3660         u32 nritems = btrfs_header_nritems(leaf);
3661         struct btrfs_key found_key;
3662         static u64 xattr_access = 0;
3663         static u64 xattr_default = 0;
3664         int scanned = 0;
3665
3666         if (!xattr_access) {
3667                 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3668                                         strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3669                 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3670                                         strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3671         }
3672
3673         slot++;
3674         *first_xattr_slot = -1;
3675         while (slot < nritems) {
3676                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3677
3678                 /* we found a different objectid, there must not be acls */
3679                 if (found_key.objectid != objectid)
3680                         return 0;
3681
3682                 /* we found an xattr, assume we've got an acl */
3683                 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3684                         if (*first_xattr_slot == -1)
3685                                 *first_xattr_slot = slot;
3686                         if (found_key.offset == xattr_access ||
3687                             found_key.offset == xattr_default)
3688                                 return 1;
3689                 }
3690
3691                 /*
3692                  * we found a key greater than an xattr key, there can't
3693                  * be any acls later on
3694                  */
3695                 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3696                         return 0;
3697
3698                 slot++;
3699                 scanned++;
3700
3701                 /*
3702                  * it goes inode, inode backrefs, xattrs, extents,
3703                  * so if there are a ton of hard links to an inode there can
3704                  * be a lot of backrefs.  Don't waste time searching too hard,
3705                  * this is just an optimization
3706                  */
3707                 if (scanned >= 8)
3708                         break;
3709         }
3710         /* we hit the end of the leaf before we found an xattr or
3711          * something larger than an xattr.  We have to assume the inode
3712          * has acls
3713          */
3714         if (*first_xattr_slot == -1)
3715                 *first_xattr_slot = slot;
3716         return 1;
3717 }
3718
3719 /*
3720  * read an inode from the btree into the in-memory inode
3721  */
3722 static int btrfs_read_locked_inode(struct inode *inode,
3723                                    struct btrfs_path *in_path)
3724 {
3725         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3726         struct btrfs_path *path = in_path;
3727         struct extent_buffer *leaf;
3728         struct btrfs_inode_item *inode_item;
3729         struct btrfs_root *root = BTRFS_I(inode)->root;
3730         struct btrfs_key location;
3731         unsigned long ptr;
3732         int maybe_acls;
3733         u32 rdev;
3734         int ret;
3735         bool filled = false;
3736         int first_xattr_slot;
3737
3738         ret = btrfs_fill_inode(inode, &rdev);
3739         if (!ret)
3740                 filled = true;
3741
3742         if (!path) {
3743                 path = btrfs_alloc_path();
3744                 if (!path)
3745                         return -ENOMEM;
3746         }
3747
3748         memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3749
3750         ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3751         if (ret) {
3752                 if (path != in_path)
3753                         btrfs_free_path(path);
3754                 return ret;
3755         }
3756
3757         leaf = path->nodes[0];
3758
3759         if (filled)
3760                 goto cache_index;
3761
3762         inode_item = btrfs_item_ptr(leaf, path->slots[0],
3763                                     struct btrfs_inode_item);
3764         inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3765         set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3766         i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3767         i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3768         btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
3769         btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3770                         round_up(i_size_read(inode), fs_info->sectorsize));
3771
3772         inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3773         inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3774
3775         inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3776         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3777
3778         inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3779         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3780
3781         BTRFS_I(inode)->i_otime.tv_sec =
3782                 btrfs_timespec_sec(leaf, &inode_item->otime);
3783         BTRFS_I(inode)->i_otime.tv_nsec =
3784                 btrfs_timespec_nsec(leaf, &inode_item->otime);
3785
3786         inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3787         BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3788         BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3789
3790         inode_set_iversion_queried(inode,
3791                                    btrfs_inode_sequence(leaf, inode_item));
3792         inode->i_generation = BTRFS_I(inode)->generation;
3793         inode->i_rdev = 0;
3794         rdev = btrfs_inode_rdev(leaf, inode_item);
3795
3796         BTRFS_I(inode)->index_cnt = (u64)-1;
3797         btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3798                                 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
3799
3800 cache_index:
3801         /*
3802          * If we were modified in the current generation and evicted from memory
3803          * and then re-read we need to do a full sync since we don't have any
3804          * idea about which extents were modified before we were evicted from
3805          * cache.
3806          *
3807          * This is required for both inode re-read from disk and delayed inode
3808          * in delayed_nodes_tree.
3809          */
3810         if (BTRFS_I(inode)->last_trans == fs_info->generation)
3811                 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3812                         &BTRFS_I(inode)->runtime_flags);
3813
3814         /*
3815          * We don't persist the id of the transaction where an unlink operation
3816          * against the inode was last made. So here we assume the inode might
3817          * have been evicted, and therefore the exact value of last_unlink_trans
3818          * lost, and set it to last_trans to avoid metadata inconsistencies
3819          * between the inode and its parent if the inode is fsync'ed and the log
3820          * replayed. For example, in the scenario:
3821          *
3822          * touch mydir/foo
3823          * ln mydir/foo mydir/bar
3824          * sync
3825          * unlink mydir/bar
3826          * echo 2 > /proc/sys/vm/drop_caches   # evicts inode
3827          * xfs_io -c fsync mydir/foo
3828          * <power failure>
3829          * mount fs, triggers fsync log replay
3830          *
3831          * We must make sure that when we fsync our inode foo we also log its
3832          * parent inode, otherwise after log replay the parent still has the
3833          * dentry with the "bar" name but our inode foo has a link count of 1
3834          * and doesn't have an inode ref with the name "bar" anymore.
3835          *
3836          * Setting last_unlink_trans to last_trans is a pessimistic approach,
3837          * but it guarantees correctness at the expense of occasional full
3838          * transaction commits on fsync if our inode is a directory, or if our
3839          * inode is not a directory, logging its parent unnecessarily.
3840          */
3841         BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3842
3843         /*
3844          * Same logic as for last_unlink_trans. We don't persist the generation
3845          * of the last transaction where this inode was used for a reflink
3846          * operation, so after eviction and reloading the inode we must be
3847          * pessimistic and assume the last transaction that modified the inode.
3848          */
3849         BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3850
3851         path->slots[0]++;
3852         if (inode->i_nlink != 1 ||
3853             path->slots[0] >= btrfs_header_nritems(leaf))
3854                 goto cache_acl;
3855
3856         btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3857         if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3858                 goto cache_acl;
3859
3860         ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3861         if (location.type == BTRFS_INODE_REF_KEY) {
3862                 struct btrfs_inode_ref *ref;
3863
3864                 ref = (struct btrfs_inode_ref *)ptr;
3865                 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3866         } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3867                 struct btrfs_inode_extref *extref;
3868
3869                 extref = (struct btrfs_inode_extref *)ptr;
3870                 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3871                                                                      extref);
3872         }
3873 cache_acl:
3874         /*
3875          * try to precache a NULL acl entry for files that don't have
3876          * any xattrs or acls
3877          */
3878         maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3879                         btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3880         if (first_xattr_slot != -1) {
3881                 path->slots[0] = first_xattr_slot;
3882                 ret = btrfs_load_inode_props(inode, path);
3883                 if (ret)
3884                         btrfs_err(fs_info,
3885                                   "error loading props for ino %llu (root %llu): %d",
3886                                   btrfs_ino(BTRFS_I(inode)),
3887                                   root->root_key.objectid, ret);
3888         }
3889         if (path != in_path)
3890                 btrfs_free_path(path);
3891
3892         if (!maybe_acls)
3893                 cache_no_acl(inode);
3894
3895         switch (inode->i_mode & S_IFMT) {
3896         case S_IFREG:
3897                 inode->i_mapping->a_ops = &btrfs_aops;
3898                 inode->i_fop = &btrfs_file_operations;
3899                 inode->i_op = &btrfs_file_inode_operations;
3900                 break;
3901         case S_IFDIR:
3902                 inode->i_fop = &btrfs_dir_file_operations;
3903                 inode->i_op = &btrfs_dir_inode_operations;
3904                 break;
3905         case S_IFLNK:
3906                 inode->i_op = &btrfs_symlink_inode_operations;
3907                 inode_nohighmem(inode);
3908                 inode->i_mapping->a_ops = &btrfs_aops;
3909                 break;
3910         default:
3911                 inode->i_op = &btrfs_special_inode_operations;
3912                 init_special_inode(inode, inode->i_mode, rdev);
3913                 break;
3914         }
3915
3916         btrfs_sync_inode_flags_to_i_flags(inode);
3917         return 0;
3918 }
3919
3920 /*
3921  * given a leaf and an inode, copy the inode fields into the leaf
3922  */
3923 static void fill_inode_item(struct btrfs_trans_handle *trans,
3924                             struct extent_buffer *leaf,
3925                             struct btrfs_inode_item *item,
3926                             struct inode *inode)
3927 {
3928         struct btrfs_map_token token;
3929         u64 flags;
3930
3931         btrfs_init_map_token(&token, leaf);
3932
3933         btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3934         btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3935         btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3936         btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3937         btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3938
3939         btrfs_set_token_timespec_sec(&token, &item->atime,
3940                                      inode->i_atime.tv_sec);
3941         btrfs_set_token_timespec_nsec(&token, &item->atime,
3942                                       inode->i_atime.tv_nsec);
3943
3944         btrfs_set_token_timespec_sec(&token, &item->mtime,
3945                                      inode->i_mtime.tv_sec);
3946         btrfs_set_token_timespec_nsec(&token, &item->mtime,
3947                                       inode->i_mtime.tv_nsec);
3948
3949         btrfs_set_token_timespec_sec(&token, &item->ctime,
3950                                      inode->i_ctime.tv_sec);
3951         btrfs_set_token_timespec_nsec(&token, &item->ctime,
3952                                       inode->i_ctime.tv_nsec);
3953
3954         btrfs_set_token_timespec_sec(&token, &item->otime,
3955                                      BTRFS_I(inode)->i_otime.tv_sec);
3956         btrfs_set_token_timespec_nsec(&token, &item->otime,
3957                                       BTRFS_I(inode)->i_otime.tv_nsec);
3958
3959         btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3960         btrfs_set_token_inode_generation(&token, item,
3961                                          BTRFS_I(inode)->generation);
3962         btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3963         btrfs_set_token_inode_transid(&token, item, trans->transid);
3964         btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3965         flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3966                                           BTRFS_I(inode)->ro_flags);
3967         btrfs_set_token_inode_flags(&token, item, flags);
3968         btrfs_set_token_inode_block_group(&token, item, 0);
3969 }
3970
3971 /*
3972  * copy everything in the in-memory inode into the btree.
3973  */
3974 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3975                                 struct btrfs_root *root,
3976                                 struct btrfs_inode *inode)
3977 {
3978         struct btrfs_inode_item *inode_item;
3979         struct btrfs_path *path;
3980         struct extent_buffer *leaf;
3981         int ret;
3982
3983         path = btrfs_alloc_path();
3984         if (!path)
3985                 return -ENOMEM;
3986
3987         ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
3988         if (ret) {
3989                 if (ret > 0)
3990                         ret = -ENOENT;
3991                 goto failed;
3992         }
3993
3994         leaf = path->nodes[0];
3995         inode_item = btrfs_item_ptr(leaf, path->slots[0],
3996                                     struct btrfs_inode_item);
3997
3998         fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
3999         btrfs_mark_buffer_dirty(leaf);
4000         btrfs_set_inode_last_trans(trans, inode);
4001         ret = 0;
4002 failed:
4003         btrfs_free_path(path);
4004         return ret;
4005 }
4006
4007 /*
4008  * copy everything in the in-memory inode into the btree.
4009  */
4010 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4011                                 struct btrfs_root *root,
4012                                 struct btrfs_inode *inode)
4013 {
4014         struct btrfs_fs_info *fs_info = root->fs_info;
4015         int ret;
4016
4017         /*
4018          * If the inode is a free space inode, we can deadlock during commit
4019          * if we put it into the delayed code.
4020          *
4021          * The data relocation inode should also be directly updated
4022          * without delay
4023          */
4024         if (!btrfs_is_free_space_inode(inode)
4025             && !btrfs_is_data_reloc_root(root)
4026             && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
4027                 btrfs_update_root_times(trans, root);
4028
4029                 ret = btrfs_delayed_update_inode(trans, root, inode);
4030                 if (!ret)
4031                         btrfs_set_inode_last_trans(trans, inode);
4032                 return ret;
4033         }
4034
4035         return btrfs_update_inode_item(trans, root, inode);
4036 }
4037
4038 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4039                                 struct btrfs_root *root, struct btrfs_inode *inode)
4040 {
4041         int ret;
4042
4043         ret = btrfs_update_inode(trans, root, inode);
4044         if (ret == -ENOSPC)
4045                 return btrfs_update_inode_item(trans, root, inode);
4046         return ret;
4047 }
4048
4049 /*
4050  * unlink helper that gets used here in inode.c and in the tree logging
4051  * recovery code.  It remove a link in a directory with a given name, and
4052  * also drops the back refs in the inode to the directory
4053  */
4054 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4055                                 struct btrfs_inode *dir,
4056                                 struct btrfs_inode *inode,
4057                                 const struct fscrypt_str *name,
4058                                 struct btrfs_rename_ctx *rename_ctx)
4059 {
4060         struct btrfs_root *root = dir->root;
4061         struct btrfs_fs_info *fs_info = root->fs_info;
4062         struct btrfs_path *path;
4063         int ret = 0;
4064         struct btrfs_dir_item *di;
4065         u64 index;
4066         u64 ino = btrfs_ino(inode);
4067         u64 dir_ino = btrfs_ino(dir);
4068
4069         path = btrfs_alloc_path();
4070         if (!path) {
4071                 ret = -ENOMEM;
4072                 goto out;
4073         }
4074
4075         di = btrfs_lookup_dir_item(trans, root, path, dir_ino, name, -1);
4076         if (IS_ERR_OR_NULL(di)) {
4077                 ret = di ? PTR_ERR(di) : -ENOENT;
4078                 goto err;
4079         }
4080         ret = btrfs_delete_one_dir_name(trans, root, path, di);
4081         if (ret)
4082                 goto err;
4083         btrfs_release_path(path);
4084
4085         /*
4086          * If we don't have dir index, we have to get it by looking up
4087          * the inode ref, since we get the inode ref, remove it directly,
4088          * it is unnecessary to do delayed deletion.
4089          *
4090          * But if we have dir index, needn't search inode ref to get it.
4091          * Since the inode ref is close to the inode item, it is better
4092          * that we delay to delete it, and just do this deletion when
4093          * we update the inode item.
4094          */
4095         if (inode->dir_index) {
4096                 ret = btrfs_delayed_delete_inode_ref(inode);
4097                 if (!ret) {
4098                         index = inode->dir_index;
4099                         goto skip_backref;
4100                 }
4101         }
4102
4103         ret = btrfs_del_inode_ref(trans, root, name, ino, dir_ino, &index);
4104         if (ret) {
4105                 btrfs_info(fs_info,
4106                         "failed to delete reference to %.*s, inode %llu parent %llu",
4107                         name->len, name->name, ino, dir_ino);
4108                 btrfs_abort_transaction(trans, ret);
4109                 goto err;
4110         }
4111 skip_backref:
4112         if (rename_ctx)
4113                 rename_ctx->index = index;
4114
4115         ret = btrfs_delete_delayed_dir_index(trans, dir, index);
4116         if (ret) {
4117                 btrfs_abort_transaction(trans, ret);
4118                 goto err;
4119         }
4120
4121         /*
4122          * If we are in a rename context, we don't need to update anything in the
4123          * log. That will be done later during the rename by btrfs_log_new_name().
4124          * Besides that, doing it here would only cause extra unnecessary btree
4125          * operations on the log tree, increasing latency for applications.
4126          */
4127         if (!rename_ctx) {
4128                 btrfs_del_inode_ref_in_log(trans, root, name, inode, dir_ino);
4129                 btrfs_del_dir_entries_in_log(trans, root, name, dir, index);
4130         }
4131
4132         /*
4133          * If we have a pending delayed iput we could end up with the final iput
4134          * being run in btrfs-cleaner context.  If we have enough of these built
4135          * up we can end up burning a lot of time in btrfs-cleaner without any
4136          * way to throttle the unlinks.  Since we're currently holding a ref on
4137          * the inode we can run the delayed iput here without any issues as the
4138          * final iput won't be done until after we drop the ref we're currently
4139          * holding.
4140          */
4141         btrfs_run_delayed_iput(fs_info, inode);
4142 err:
4143         btrfs_free_path(path);
4144         if (ret)
4145                 goto out;
4146
4147         btrfs_i_size_write(dir, dir->vfs_inode.i_size - name->len * 2);
4148         inode_inc_iversion(&inode->vfs_inode);
4149         inode_inc_iversion(&dir->vfs_inode);
4150         inode->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4151         dir->vfs_inode.i_mtime = inode->vfs_inode.i_ctime;
4152         dir->vfs_inode.i_ctime = inode->vfs_inode.i_ctime;
4153         ret = btrfs_update_inode(trans, root, dir);
4154 out:
4155         return ret;
4156 }
4157
4158 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4159                        struct btrfs_inode *dir, struct btrfs_inode *inode,
4160                        const struct fscrypt_str *name)
4161 {
4162         int ret;
4163
4164         ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL);
4165         if (!ret) {
4166                 drop_nlink(&inode->vfs_inode);
4167                 ret = btrfs_update_inode(trans, inode->root, inode);
4168         }
4169         return ret;
4170 }
4171
4172 /*
4173  * helper to start transaction for unlink and rmdir.
4174  *
4175  * unlink and rmdir are special in btrfs, they do not always free space, so
4176  * if we cannot make our reservations the normal way try and see if there is
4177  * plenty of slack room in the global reserve to migrate, otherwise we cannot
4178  * allow the unlink to occur.
4179  */
4180 static struct btrfs_trans_handle *__unlink_start_trans(struct btrfs_inode *dir)
4181 {
4182         struct btrfs_root *root = dir->root;
4183
4184         return btrfs_start_transaction_fallback_global_rsv(root,
4185                                                    BTRFS_UNLINK_METADATA_UNITS);
4186 }
4187
4188 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4189 {
4190         struct btrfs_trans_handle *trans;
4191         struct inode *inode = d_inode(dentry);
4192         int ret;
4193         struct fscrypt_name fname;
4194
4195         ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname);
4196         if (ret)
4197                 return ret;
4198
4199         /* This needs to handle no-key deletions later on */
4200
4201         trans = __unlink_start_trans(BTRFS_I(dir));
4202         if (IS_ERR(trans)) {
4203                 ret = PTR_ERR(trans);
4204                 goto fscrypt_free;
4205         }
4206
4207         btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4208                                 false);
4209
4210         ret = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4211                                  &fname.disk_name);
4212         if (ret)
4213                 goto end_trans;
4214
4215         if (inode->i_nlink == 0) {
4216                 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
4217                 if (ret)
4218                         goto end_trans;
4219         }
4220
4221 end_trans:
4222         btrfs_end_transaction(trans);
4223         btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info);
4224 fscrypt_free:
4225         fscrypt_free_filename(&fname);
4226         return ret;
4227 }
4228
4229 static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4230                                struct btrfs_inode *dir, struct dentry *dentry)
4231 {
4232         struct btrfs_root *root = dir->root;
4233         struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
4234         struct btrfs_path *path;
4235         struct extent_buffer *leaf;
4236         struct btrfs_dir_item *di;
4237         struct btrfs_key key;
4238         u64 index;
4239         int ret;
4240         u64 objectid;
4241         u64 dir_ino = btrfs_ino(dir);
4242         struct fscrypt_name fname;
4243
4244         ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname);
4245         if (ret)
4246                 return ret;
4247
4248         /* This needs to handle no-key deletions later on */
4249
4250         if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4251                 objectid = inode->root->root_key.objectid;
4252         } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4253                 objectid = inode->location.objectid;
4254         } else {
4255                 WARN_ON(1);
4256                 fscrypt_free_filename(&fname);
4257                 return -EINVAL;
4258         }
4259
4260         path = btrfs_alloc_path();
4261         if (!path) {
4262                 ret = -ENOMEM;
4263                 goto out;
4264         }
4265
4266         di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4267                                    &fname.disk_name, -1);
4268         if (IS_ERR_OR_NULL(di)) {
4269                 ret = di ? PTR_ERR(di) : -ENOENT;
4270                 goto out;
4271         }
4272
4273         leaf = path->nodes[0];
4274         btrfs_dir_item_key_to_cpu(leaf, di, &key);
4275         WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4276         ret = btrfs_delete_one_dir_name(trans, root, path, di);
4277         if (ret) {
4278                 btrfs_abort_transaction(trans, ret);
4279                 goto out;
4280         }
4281         btrfs_release_path(path);
4282
4283         /*
4284          * This is a placeholder inode for a subvolume we didn't have a
4285          * reference to at the time of the snapshot creation.  In the meantime
4286          * we could have renamed the real subvol link into our snapshot, so
4287          * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
4288          * Instead simply lookup the dir_index_item for this entry so we can
4289          * remove it.  Otherwise we know we have a ref to the root and we can
4290          * call btrfs_del_root_ref, and it _shouldn't_ fail.
4291          */
4292         if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4293                 di = btrfs_search_dir_index_item(root, path, dir_ino, &fname.disk_name);
4294                 if (IS_ERR_OR_NULL(di)) {
4295                         if (!di)
4296                                 ret = -ENOENT;
4297                         else
4298                                 ret = PTR_ERR(di);
4299                         btrfs_abort_transaction(trans, ret);
4300                         goto out;
4301                 }
4302
4303                 leaf = path->nodes[0];
4304                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4305                 index = key.offset;
4306                 btrfs_release_path(path);
4307         } else {
4308                 ret = btrfs_del_root_ref(trans, objectid,
4309                                          root->root_key.objectid, dir_ino,
4310                                          &index, &fname.disk_name);
4311                 if (ret) {
4312                         btrfs_abort_transaction(trans, ret);
4313                         goto out;
4314                 }
4315         }
4316
4317         ret = btrfs_delete_delayed_dir_index(trans, dir, index);
4318         if (ret) {
4319                 btrfs_abort_transaction(trans, ret);
4320                 goto out;
4321         }
4322
4323         btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2);
4324         inode_inc_iversion(&dir->vfs_inode);
4325         dir->vfs_inode.i_mtime = current_time(&dir->vfs_inode);
4326         dir->vfs_inode.i_ctime = dir->vfs_inode.i_mtime;
4327         ret = btrfs_update_inode_fallback(trans, root, dir);
4328         if (ret)
4329                 btrfs_abort_transaction(trans, ret);
4330 out:
4331         btrfs_free_path(path);
4332         fscrypt_free_filename(&fname);
4333         return ret;
4334 }
4335
4336 /*
4337  * Helper to check if the subvolume references other subvolumes or if it's
4338  * default.
4339  */
4340 static noinline int may_destroy_subvol(struct btrfs_root *root)
4341 {
4342         struct btrfs_fs_info *fs_info = root->fs_info;
4343         struct btrfs_path *path;
4344         struct btrfs_dir_item *di;
4345         struct btrfs_key key;
4346         struct fscrypt_str name = FSTR_INIT("default", 7);
4347         u64 dir_id;
4348         int ret;
4349
4350         path = btrfs_alloc_path();
4351         if (!path)
4352                 return -ENOMEM;
4353
4354         /* Make sure this root isn't set as the default subvol */
4355         dir_id = btrfs_super_root_dir(fs_info->super_copy);
4356         di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4357                                    dir_id, &name, 0);
4358         if (di && !IS_ERR(di)) {
4359                 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4360                 if (key.objectid == root->root_key.objectid) {
4361                         ret = -EPERM;
4362                         btrfs_err(fs_info,
4363                                   "deleting default subvolume %llu is not allowed",
4364                                   key.objectid);
4365                         goto out;
4366                 }
4367                 btrfs_release_path(path);
4368         }
4369
4370         key.objectid = root->root_key.objectid;
4371         key.type = BTRFS_ROOT_REF_KEY;
4372         key.offset = (u64)-1;
4373
4374         ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4375         if (ret < 0)
4376                 goto out;
4377         BUG_ON(ret == 0);
4378
4379         ret = 0;
4380         if (path->slots[0] > 0) {
4381                 path->slots[0]--;
4382                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4383                 if (key.objectid == root->root_key.objectid &&
4384                     key.type == BTRFS_ROOT_REF_KEY)
4385                         ret = -ENOTEMPTY;
4386         }
4387 out:
4388         btrfs_free_path(path);
4389         return ret;
4390 }
4391
4392 /* Delete all dentries for inodes belonging to the root */
4393 static void btrfs_prune_dentries(struct btrfs_root *root)
4394 {
4395         struct btrfs_fs_info *fs_info = root->fs_info;
4396         struct rb_node *node;
4397         struct rb_node *prev;
4398         struct btrfs_inode *entry;
4399         struct inode *inode;
4400         u64 objectid = 0;
4401
4402         if (!BTRFS_FS_ERROR(fs_info))
4403                 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4404
4405         spin_lock(&root->inode_lock);
4406 again:
4407         node = root->inode_tree.rb_node;
4408         prev = NULL;
4409         while (node) {
4410                 prev = node;
4411                 entry = rb_entry(node, struct btrfs_inode, rb_node);
4412
4413                 if (objectid < btrfs_ino(entry))
4414                         node = node->rb_left;
4415                 else if (objectid > btrfs_ino(entry))
4416                         node = node->rb_right;
4417                 else
4418                         break;
4419         }
4420         if (!node) {
4421                 while (prev) {
4422                         entry = rb_entry(prev, struct btrfs_inode, rb_node);
4423                         if (objectid <= btrfs_ino(entry)) {
4424                                 node = prev;
4425                                 break;
4426                         }
4427                         prev = rb_next(prev);
4428                 }
4429         }
4430         while (node) {
4431                 entry = rb_entry(node, struct btrfs_inode, rb_node);
4432                 objectid = btrfs_ino(entry) + 1;
4433                 inode = igrab(&entry->vfs_inode);
4434                 if (inode) {
4435                         spin_unlock(&root->inode_lock);
4436                         if (atomic_read(&inode->i_count) > 1)
4437                                 d_prune_aliases(inode);
4438                         /*
4439                          * btrfs_drop_inode will have it removed from the inode
4440                          * cache when its usage count hits zero.
4441                          */
4442                         iput(inode);
4443                         cond_resched();
4444                         spin_lock(&root->inode_lock);
4445                         goto again;
4446                 }
4447
4448                 if (cond_resched_lock(&root->inode_lock))
4449                         goto again;
4450
4451                 node = rb_next(node);
4452         }
4453         spin_unlock(&root->inode_lock);
4454 }
4455
4456 int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry)
4457 {
4458         struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4459         struct btrfs_root *root = dir->root;
4460         struct inode *inode = d_inode(dentry);
4461         struct btrfs_root *dest = BTRFS_I(inode)->root;
4462         struct btrfs_trans_handle *trans;
4463         struct btrfs_block_rsv block_rsv;
4464         u64 root_flags;
4465         int ret;
4466
4467         /*
4468          * Don't allow to delete a subvolume with send in progress. This is
4469          * inside the inode lock so the error handling that has to drop the bit
4470          * again is not run concurrently.
4471          */
4472         spin_lock(&dest->root_item_lock);
4473         if (dest->send_in_progress) {
4474                 spin_unlock(&dest->root_item_lock);
4475                 btrfs_warn(fs_info,
4476                            "attempt to delete subvolume %llu during send",
4477                            dest->root_key.objectid);
4478                 return -EPERM;
4479         }
4480         if (atomic_read(&dest->nr_swapfiles)) {
4481                 spin_unlock(&dest->root_item_lock);
4482                 btrfs_warn(fs_info,
4483                            "attempt to delete subvolume %llu with active swapfile",
4484                            root->root_key.objectid);
4485                 return -EPERM;
4486         }
4487         root_flags = btrfs_root_flags(&dest->root_item);
4488         btrfs_set_root_flags(&dest->root_item,
4489                              root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4490         spin_unlock(&dest->root_item_lock);
4491
4492         down_write(&fs_info->subvol_sem);
4493
4494         ret = may_destroy_subvol(dest);
4495         if (ret)
4496                 goto out_up_write;
4497
4498         btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4499         /*
4500          * One for dir inode,
4501          * two for dir entries,
4502          * two for root ref/backref.
4503          */
4504         ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4505         if (ret)
4506                 goto out_up_write;
4507
4508         trans = btrfs_start_transaction(root, 0);
4509         if (IS_ERR(trans)) {
4510                 ret = PTR_ERR(trans);
4511                 goto out_release;
4512         }
4513         trans->block_rsv = &block_rsv;
4514         trans->bytes_reserved = block_rsv.size;
4515
4516         btrfs_record_snapshot_destroy(trans, dir);
4517
4518         ret = btrfs_unlink_subvol(trans, dir, dentry);
4519         if (ret) {
4520                 btrfs_abort_transaction(trans, ret);
4521                 goto out_end_trans;
4522         }
4523
4524         ret = btrfs_record_root_in_trans(trans, dest);
4525         if (ret) {
4526                 btrfs_abort_transaction(trans, ret);
4527                 goto out_end_trans;
4528         }
4529
4530         memset(&dest->root_item.drop_progress, 0,
4531                 sizeof(dest->root_item.drop_progress));
4532         btrfs_set_root_drop_level(&dest->root_item, 0);
4533         btrfs_set_root_refs(&dest->root_item, 0);
4534
4535         if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4536                 ret = btrfs_insert_orphan_item(trans,
4537                                         fs_info->tree_root,
4538                                         dest->root_key.objectid);
4539                 if (ret) {
4540                         btrfs_abort_transaction(trans, ret);
4541                         goto out_end_trans;
4542                 }
4543         }
4544
4545         ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
4546                                   BTRFS_UUID_KEY_SUBVOL,
4547                                   dest->root_key.objectid);
4548         if (ret && ret != -ENOENT) {
4549                 btrfs_abort_transaction(trans, ret);
4550                 goto out_end_trans;
4551         }
4552         if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
4553                 ret = btrfs_uuid_tree_remove(trans,
4554                                           dest->root_item.received_uuid,
4555                                           BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4556                                           dest->root_key.objectid);
4557                 if (ret && ret != -ENOENT) {
4558                         btrfs_abort_transaction(trans, ret);
4559                         goto out_end_trans;
4560                 }
4561         }
4562
4563         free_anon_bdev(dest->anon_dev);
4564         dest->anon_dev = 0;
4565 out_end_trans:
4566         trans->block_rsv = NULL;
4567         trans->bytes_reserved = 0;
4568         ret = btrfs_end_transaction(trans);
4569         inode->i_flags |= S_DEAD;
4570 out_release:
4571         btrfs_subvolume_release_metadata(root, &block_rsv);
4572 out_up_write:
4573         up_write(&fs_info->subvol_sem);
4574         if (ret) {
4575                 spin_lock(&dest->root_item_lock);
4576                 root_flags = btrfs_root_flags(&dest->root_item);
4577                 btrfs_set_root_flags(&dest->root_item,
4578                                 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4579                 spin_unlock(&dest->root_item_lock);
4580         } else {
4581                 d_invalidate(dentry);
4582                 btrfs_prune_dentries(dest);
4583                 ASSERT(dest->send_in_progress == 0);
4584         }
4585
4586         return ret;
4587 }
4588
4589 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4590 {
4591         struct inode *inode = d_inode(dentry);
4592         struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
4593         int err = 0;
4594         struct btrfs_trans_handle *trans;
4595         u64 last_unlink_trans;
4596         struct fscrypt_name fname;
4597
4598         if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4599                 return -ENOTEMPTY;
4600         if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) {
4601                 if (unlikely(btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))) {
4602                         btrfs_err(fs_info,
4603                         "extent tree v2 doesn't support snapshot deletion yet");
4604                         return -EOPNOTSUPP;
4605                 }
4606                 return btrfs_delete_subvolume(BTRFS_I(dir), dentry);
4607         }
4608
4609         err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname);
4610         if (err)
4611                 return err;
4612
4613         /* This needs to handle no-key deletions later on */
4614
4615         trans = __unlink_start_trans(BTRFS_I(dir));
4616         if (IS_ERR(trans)) {
4617                 err = PTR_ERR(trans);
4618                 goto out_notrans;
4619         }
4620
4621         if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4622                 err = btrfs_unlink_subvol(trans, BTRFS_I(dir), dentry);
4623                 goto out;
4624         }
4625
4626         err = btrfs_orphan_add(trans, BTRFS_I(inode));
4627         if (err)
4628                 goto out;
4629
4630         last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4631
4632         /* now the directory is empty */
4633         err = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4634                                  &fname.disk_name);
4635         if (!err) {
4636                 btrfs_i_size_write(BTRFS_I(inode), 0);
4637                 /*
4638                  * Propagate the last_unlink_trans value of the deleted dir to
4639                  * its parent directory. This is to prevent an unrecoverable
4640                  * log tree in the case we do something like this:
4641                  * 1) create dir foo
4642                  * 2) create snapshot under dir foo
4643                  * 3) delete the snapshot
4644                  * 4) rmdir foo
4645                  * 5) mkdir foo
4646                  * 6) fsync foo or some file inside foo
4647                  */
4648                 if (last_unlink_trans >= trans->transid)
4649                         BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4650         }
4651 out:
4652         btrfs_end_transaction(trans);
4653 out_notrans:
4654         btrfs_btree_balance_dirty(fs_info);
4655         fscrypt_free_filename(&fname);
4656
4657         return err;
4658 }
4659
4660 /*
4661  * btrfs_truncate_block - read, zero a chunk and write a block
4662  * @inode - inode that we're zeroing
4663  * @from - the offset to start zeroing
4664  * @len - the length to zero, 0 to zero the entire range respective to the
4665  *      offset
4666  * @front - zero up to the offset instead of from the offset on
4667  *
4668  * This will find the block for the "from" offset and cow the block and zero the
4669  * part we want to zero.  This is used with truncate and hole punching.
4670  */
4671 int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4672                          int front)
4673 {
4674         struct btrfs_fs_info *fs_info = inode->root->fs_info;
4675         struct address_space *mapping = inode->vfs_inode.i_mapping;
4676         struct extent_io_tree *io_tree = &inode->io_tree;
4677         struct btrfs_ordered_extent *ordered;
4678         struct extent_state *cached_state = NULL;
4679         struct extent_changeset *data_reserved = NULL;
4680         bool only_release_metadata = false;
4681         u32 blocksize = fs_info->sectorsize;
4682         pgoff_t index = from >> PAGE_SHIFT;
4683         unsigned offset = from & (blocksize - 1);
4684         struct page *page;
4685         gfp_t mask = btrfs_alloc_write_mask(mapping);
4686         size_t write_bytes = blocksize;
4687         int ret = 0;
4688         u64 block_start;
4689         u64 block_end;
4690
4691         if (IS_ALIGNED(offset, blocksize) &&
4692             (!len || IS_ALIGNED(len, blocksize)))
4693                 goto out;
4694
4695         block_start = round_down(from, blocksize);
4696         block_end = block_start + blocksize - 1;
4697
4698         ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4699                                           blocksize, false);
4700         if (ret < 0) {
4701                 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes, false) > 0) {
4702                         /* For nocow case, no need to reserve data space */
4703                         only_release_metadata = true;
4704                 } else {
4705                         goto out;
4706                 }
4707         }
4708         ret = btrfs_delalloc_reserve_metadata(inode, blocksize, blocksize, false);
4709         if (ret < 0) {
4710                 if (!only_release_metadata)
4711                         btrfs_free_reserved_data_space(inode, data_reserved,
4712                                                        block_start, blocksize);
4713                 goto out;
4714         }
4715 again:
4716         page = find_or_create_page(mapping, index, mask);
4717         if (!page) {
4718                 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4719                                              blocksize, true);
4720                 btrfs_delalloc_release_extents(inode, blocksize);
4721                 ret = -ENOMEM;
4722                 goto out;
4723         }
4724
4725         if (!PageUptodate(page)) {
4726                 ret = btrfs_read_folio(NULL, page_folio(page));
4727                 lock_page(page);
4728                 if (page->mapping != mapping) {
4729                         unlock_page(page);
4730                         put_page(page);
4731                         goto again;
4732                 }
4733                 if (!PageUptodate(page)) {
4734                         ret = -EIO;
4735                         goto out_unlock;
4736                 }
4737         }
4738
4739         /*
4740          * We unlock the page after the io is completed and then re-lock it
4741          * above.  release_folio() could have come in between that and cleared
4742          * PagePrivate(), but left the page in the mapping.  Set the page mapped
4743          * here to make sure it's properly set for the subpage stuff.
4744          */
4745         ret = set_page_extent_mapped(page);
4746         if (ret < 0)
4747                 goto out_unlock;
4748
4749         wait_on_page_writeback(page);
4750
4751         lock_extent(io_tree, block_start, block_end, &cached_state);
4752
4753         ordered = btrfs_lookup_ordered_extent(inode, block_start);
4754         if (ordered) {
4755                 unlock_extent(io_tree, block_start, block_end, &cached_state);
4756                 unlock_page(page);
4757                 put_page(page);
4758                 btrfs_start_ordered_extent(ordered);
4759                 btrfs_put_ordered_extent(ordered);
4760                 goto again;
4761         }
4762
4763         clear_extent_bit(&inode->io_tree, block_start, block_end,
4764                          EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4765                          &cached_state);
4766
4767         ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
4768                                         &cached_state);
4769         if (ret) {
4770                 unlock_extent(io_tree, block_start, block_end, &cached_state);
4771                 goto out_unlock;
4772         }
4773
4774         if (offset != blocksize) {
4775                 if (!len)
4776                         len = blocksize - offset;
4777                 if (front)
4778                         memzero_page(page, (block_start - page_offset(page)),
4779                                      offset);
4780                 else
4781                         memzero_page(page, (block_start - page_offset(page)) + offset,
4782                                      len);
4783         }
4784         btrfs_page_clear_checked(fs_info, page, block_start,
4785                                  block_end + 1 - block_start);
4786         btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
4787         unlock_extent(io_tree, block_start, block_end, &cached_state);
4788
4789         if (only_release_metadata)
4790                 set_extent_bit(&inode->io_tree, block_start, block_end,
4791                                EXTENT_NORESERVE, NULL);
4792
4793 out_unlock:
4794         if (ret) {
4795                 if (only_release_metadata)
4796                         btrfs_delalloc_release_metadata(inode, blocksize, true);
4797                 else
4798                         btrfs_delalloc_release_space(inode, data_reserved,
4799                                         block_start, blocksize, true);
4800         }
4801         btrfs_delalloc_release_extents(inode, blocksize);
4802         unlock_page(page);
4803         put_page(page);
4804 out:
4805         if (only_release_metadata)
4806                 btrfs_check_nocow_unlock(inode);
4807         extent_changeset_free(data_reserved);
4808         return ret;
4809 }
4810
4811 static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
4812                              u64 offset, u64 len)
4813 {
4814         struct btrfs_fs_info *fs_info = root->fs_info;
4815         struct btrfs_trans_handle *trans;
4816         struct btrfs_drop_extents_args drop_args = { 0 };
4817         int ret;
4818
4819         /*
4820          * If NO_HOLES is enabled, we don't need to do anything.
4821          * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
4822          * or btrfs_update_inode() will be called, which guarantee that the next
4823          * fsync will know this inode was changed and needs to be logged.
4824          */
4825         if (btrfs_fs_incompat(fs_info, NO_HOLES))
4826                 return 0;
4827
4828         /*
4829          * 1 - for the one we're dropping
4830          * 1 - for the one we're adding
4831          * 1 - for updating the inode.
4832          */
4833         trans = btrfs_start_transaction(root, 3);
4834         if (IS_ERR(trans))
4835                 return PTR_ERR(trans);
4836
4837         drop_args.start = offset;
4838         drop_args.end = offset + len;
4839         drop_args.drop_cache = true;
4840
4841         ret = btrfs_drop_extents(trans, root, inode, &drop_args);
4842         if (ret) {
4843                 btrfs_abort_transaction(trans, ret);
4844                 btrfs_end_transaction(trans);
4845                 return ret;
4846         }
4847
4848         ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset, len);
4849         if (ret) {
4850                 btrfs_abort_transaction(trans, ret);
4851         } else {
4852                 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4853                 btrfs_update_inode(trans, root, inode);
4854         }
4855         btrfs_end_transaction(trans);
4856         return ret;
4857 }
4858
4859 /*
4860  * This function puts in dummy file extents for the area we're creating a hole
4861  * for.  So if we are truncating this file to a larger size we need to insert
4862  * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4863  * the range between oldsize and size
4864  */
4865 int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
4866 {
4867         struct btrfs_root *root = inode->root;
4868         struct btrfs_fs_info *fs_info = root->fs_info;
4869         struct extent_io_tree *io_tree = &inode->io_tree;
4870         struct extent_map *em = NULL;
4871         struct extent_state *cached_state = NULL;
4872         u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4873         u64 block_end = ALIGN(size, fs_info->sectorsize);
4874         u64 last_byte;
4875         u64 cur_offset;
4876         u64 hole_size;
4877         int err = 0;
4878
4879         /*
4880          * If our size started in the middle of a block we need to zero out the
4881          * rest of the block before we expand the i_size, otherwise we could
4882          * expose stale data.
4883          */
4884         err = btrfs_truncate_block(inode, oldsize, 0, 0);
4885         if (err)
4886                 return err;
4887
4888         if (size <= hole_start)
4889                 return 0;
4890
4891         btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
4892                                            &cached_state);
4893         cur_offset = hole_start;
4894         while (1) {
4895                 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4896                                       block_end - cur_offset);
4897                 if (IS_ERR(em)) {
4898                         err = PTR_ERR(em);
4899                         em = NULL;
4900                         break;
4901                 }
4902                 last_byte = min(extent_map_end(em), block_end);
4903                 last_byte = ALIGN(last_byte, fs_info->sectorsize);
4904                 hole_size = last_byte - cur_offset;
4905
4906                 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4907                         struct extent_map *hole_em;
4908
4909                         err = maybe_insert_hole(root, inode, cur_offset,
4910                                                 hole_size);
4911                         if (err)
4912                                 break;
4913
4914                         err = btrfs_inode_set_file_extent_range(inode,
4915                                                         cur_offset, hole_size);
4916                         if (err)
4917                                 break;
4918
4919                         hole_em = alloc_extent_map();
4920                         if (!hole_em) {
4921                                 btrfs_drop_extent_map_range(inode, cur_offset,
4922                                                     cur_offset + hole_size - 1,
4923                                                     false);
4924                                 btrfs_set_inode_full_sync(inode);
4925                                 goto next;
4926                         }
4927                         hole_em->start = cur_offset;
4928                         hole_em->len = hole_size;
4929                         hole_em->orig_start = cur_offset;
4930
4931                         hole_em->block_start = EXTENT_MAP_HOLE;
4932                         hole_em->block_len = 0;
4933                         hole_em->orig_block_len = 0;
4934                         hole_em->ram_bytes = hole_size;
4935                         hole_em->compress_type = BTRFS_COMPRESS_NONE;
4936                         hole_em->generation = fs_info->generation;
4937
4938                         err = btrfs_replace_extent_map_range(inode, hole_em, true);
4939                         free_extent_map(hole_em);
4940                 } else {
4941                         err = btrfs_inode_set_file_extent_range(inode,
4942                                                         cur_offset, hole_size);
4943                         if (err)
4944                                 break;
4945                 }
4946 next:
4947                 free_extent_map(em);
4948                 em = NULL;
4949                 cur_offset = last_byte;
4950                 if (cur_offset >= block_end)
4951                         break;
4952         }
4953         free_extent_map(em);
4954         unlock_extent(io_tree, hole_start, block_end - 1, &cached_state);
4955         return err;
4956 }
4957
4958 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4959 {
4960         struct btrfs_root *root = BTRFS_I(inode)->root;
4961         struct btrfs_trans_handle *trans;
4962         loff_t oldsize = i_size_read(inode);
4963         loff_t newsize = attr->ia_size;
4964         int mask = attr->ia_valid;
4965         int ret;
4966
4967         /*
4968          * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4969          * special case where we need to update the times despite not having
4970          * these flags set.  For all other operations the VFS set these flags
4971          * explicitly if it wants a timestamp update.
4972          */
4973         if (newsize != oldsize) {
4974                 inode_inc_iversion(inode);
4975                 if (!(mask & (ATTR_CTIME | ATTR_MTIME))) {
4976                         inode->i_mtime = current_time(inode);
4977                         inode->i_ctime = inode->i_mtime;
4978                 }
4979         }
4980
4981         if (newsize > oldsize) {
4982                 /*
4983                  * Don't do an expanding truncate while snapshotting is ongoing.
4984                  * This is to ensure the snapshot captures a fully consistent
4985                  * state of this file - if the snapshot captures this expanding
4986                  * truncation, it must capture all writes that happened before
4987                  * this truncation.
4988                  */
4989                 btrfs_drew_write_lock(&root->snapshot_lock);
4990                 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
4991                 if (ret) {
4992                         btrfs_drew_write_unlock(&root->snapshot_lock);
4993                         return ret;
4994                 }
4995
4996                 trans = btrfs_start_transaction(root, 1);
4997                 if (IS_ERR(trans)) {
4998                         btrfs_drew_write_unlock(&root->snapshot_lock);
4999                         return PTR_ERR(trans);
5000                 }
5001
5002                 i_size_write(inode, newsize);
5003                 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
5004                 pagecache_isize_extended(inode, oldsize, newsize);
5005                 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
5006                 btrfs_drew_write_unlock(&root->snapshot_lock);
5007                 btrfs_end_transaction(trans);
5008         } else {
5009                 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5010
5011                 if (btrfs_is_zoned(fs_info)) {
5012                         ret = btrfs_wait_ordered_range(inode,
5013                                         ALIGN(newsize, fs_info->sectorsize),
5014                                         (u64)-1);
5015                         if (ret)
5016                                 return ret;
5017                 }
5018
5019                 /*
5020                  * We're truncating a file that used to have good data down to
5021                  * zero. Make sure any new writes to the file get on disk
5022                  * on close.
5023                  */
5024                 if (newsize == 0)
5025                         set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
5026                                 &BTRFS_I(inode)->runtime_flags);
5027
5028                 truncate_setsize(inode, newsize);
5029
5030                 inode_dio_wait(inode);
5031
5032                 ret = btrfs_truncate(BTRFS_I(inode), newsize == oldsize);
5033                 if (ret && inode->i_nlink) {
5034                         int err;
5035
5036                         /*
5037                          * Truncate failed, so fix up the in-memory size. We
5038                          * adjusted disk_i_size down as we removed extents, so
5039                          * wait for disk_i_size to be stable and then update the
5040                          * in-memory size to match.
5041                          */
5042                         err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5043                         if (err)
5044                                 return err;
5045                         i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5046                 }
5047         }
5048
5049         return ret;
5050 }
5051
5052 static int btrfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
5053                          struct iattr *attr)
5054 {
5055         struct inode *inode = d_inode(dentry);
5056         struct btrfs_root *root = BTRFS_I(inode)->root;
5057         int err;
5058
5059         if (btrfs_root_readonly(root))
5060                 return -EROFS;
5061
5062         err = setattr_prepare(idmap, dentry, attr);
5063         if (err)
5064                 return err;
5065
5066         if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5067                 err = btrfs_setsize(inode, attr);
5068                 if (err)
5069                         return err;
5070         }
5071
5072         if (attr->ia_valid) {
5073                 setattr_copy(idmap, inode, attr);
5074                 inode_inc_iversion(inode);
5075                 err = btrfs_dirty_inode(BTRFS_I(inode));
5076
5077                 if (!err && attr->ia_valid & ATTR_MODE)
5078                         err = posix_acl_chmod(idmap, dentry, inode->i_mode);
5079         }
5080
5081         return err;
5082 }
5083
5084 /*
5085  * While truncating the inode pages during eviction, we get the VFS
5086  * calling btrfs_invalidate_folio() against each folio of the inode. This
5087  * is slow because the calls to btrfs_invalidate_folio() result in a
5088  * huge amount of calls to lock_extent() and clear_extent_bit(),
5089  * which keep merging and splitting extent_state structures over and over,
5090  * wasting lots of time.
5091  *
5092  * Therefore if the inode is being evicted, let btrfs_invalidate_folio()
5093  * skip all those expensive operations on a per folio basis and do only
5094  * the ordered io finishing, while we release here the extent_map and
5095  * extent_state structures, without the excessive merging and splitting.
5096  */
5097 static void evict_inode_truncate_pages(struct inode *inode)
5098 {
5099         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5100         struct rb_node *node;
5101
5102         ASSERT(inode->i_state & I_FREEING);
5103         truncate_inode_pages_final(&inode->i_data);
5104
5105         btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
5106
5107         /*
5108          * Keep looping until we have no more ranges in the io tree.
5109          * We can have ongoing bios started by readahead that have
5110          * their endio callback (extent_io.c:end_bio_extent_readpage)
5111          * still in progress (unlocked the pages in the bio but did not yet
5112          * unlocked the ranges in the io tree). Therefore this means some
5113          * ranges can still be locked and eviction started because before
5114          * submitting those bios, which are executed by a separate task (work
5115          * queue kthread), inode references (inode->i_count) were not taken
5116          * (which would be dropped in the end io callback of each bio).
5117          * Therefore here we effectively end up waiting for those bios and
5118          * anyone else holding locked ranges without having bumped the inode's
5119          * reference count - if we don't do it, when they access the inode's
5120          * io_tree to unlock a range it may be too late, leading to an
5121          * use-after-free issue.
5122          */
5123         spin_lock(&io_tree->lock);
5124         while (!RB_EMPTY_ROOT(&io_tree->state)) {
5125                 struct extent_state *state;
5126                 struct extent_state *cached_state = NULL;
5127                 u64 start;
5128                 u64 end;
5129                 unsigned state_flags;
5130
5131                 node = rb_first(&io_tree->state);
5132                 state = rb_entry(node, struct extent_state, rb_node);
5133                 start = state->start;
5134                 end = state->end;
5135                 state_flags = state->state;
5136                 spin_unlock(&io_tree->lock);
5137
5138                 lock_extent(io_tree, start, end, &cached_state);
5139
5140                 /*
5141                  * If still has DELALLOC flag, the extent didn't reach disk,
5142                  * and its reserved space won't be freed by delayed_ref.
5143                  * So we need to free its reserved space here.
5144                  * (Refer to comment in btrfs_invalidate_folio, case 2)
5145                  *
5146                  * Note, end is the bytenr of last byte, so we need + 1 here.
5147                  */
5148                 if (state_flags & EXTENT_DELALLOC)
5149                         btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5150                                                end - start + 1);
5151
5152                 clear_extent_bit(io_tree, start, end,
5153                                  EXTENT_CLEAR_ALL_BITS | EXTENT_DO_ACCOUNTING,
5154                                  &cached_state);
5155
5156                 cond_resched();
5157                 spin_lock(&io_tree->lock);
5158         }
5159         spin_unlock(&io_tree->lock);
5160 }
5161
5162 static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5163                                                         struct btrfs_block_rsv *rsv)
5164 {
5165         struct btrfs_fs_info *fs_info = root->fs_info;
5166         struct btrfs_trans_handle *trans;
5167         u64 delayed_refs_extra = btrfs_calc_delayed_ref_bytes(fs_info, 1);
5168         int ret;
5169
5170         /*
5171          * Eviction should be taking place at some place safe because of our
5172          * delayed iputs.  However the normal flushing code will run delayed
5173          * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5174          *
5175          * We reserve the delayed_refs_extra here again because we can't use
5176          * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5177          * above.  We reserve our extra bit here because we generate a ton of
5178          * delayed refs activity by truncating.
5179          *
5180          * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can,
5181          * if we fail to make this reservation we can re-try without the
5182          * delayed_refs_extra so we can make some forward progress.
5183          */
5184         ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra,
5185                                      BTRFS_RESERVE_FLUSH_EVICT);
5186         if (ret) {
5187                 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size,
5188                                              BTRFS_RESERVE_FLUSH_EVICT);
5189                 if (ret) {
5190                         btrfs_warn(fs_info,
5191                                    "could not allocate space for delete; will truncate on mount");
5192                         return ERR_PTR(-ENOSPC);
5193                 }
5194                 delayed_refs_extra = 0;
5195         }
5196
5197         trans = btrfs_join_transaction(root);
5198         if (IS_ERR(trans))
5199                 return trans;
5200
5201         if (delayed_refs_extra) {
5202                 trans->block_rsv = &fs_info->trans_block_rsv;
5203                 trans->bytes_reserved = delayed_refs_extra;
5204                 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5205                                         delayed_refs_extra, true);
5206         }
5207         return trans;
5208 }
5209
5210 void btrfs_evict_inode(struct inode *inode)
5211 {
5212         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5213         struct btrfs_trans_handle *trans;
5214         struct btrfs_root *root = BTRFS_I(inode)->root;
5215         struct btrfs_block_rsv *rsv = NULL;
5216         int ret;
5217
5218         trace_btrfs_inode_evict(inode);
5219
5220         if (!root) {
5221                 fsverity_cleanup_inode(inode);
5222                 clear_inode(inode);
5223                 return;
5224         }
5225
5226         evict_inode_truncate_pages(inode);
5227
5228         if (inode->i_nlink &&
5229             ((btrfs_root_refs(&root->root_item) != 0 &&
5230               root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5231              btrfs_is_free_space_inode(BTRFS_I(inode))))
5232                 goto out;
5233
5234         if (is_bad_inode(inode))
5235                 goto out;
5236
5237         if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
5238                 goto out;
5239
5240         if (inode->i_nlink > 0) {
5241                 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5242                        root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5243                 goto out;
5244         }
5245
5246         /*
5247          * This makes sure the inode item in tree is uptodate and the space for
5248          * the inode update is released.
5249          */
5250         ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5251         if (ret)
5252                 goto out;
5253
5254         /*
5255          * This drops any pending insert or delete operations we have for this
5256          * inode.  We could have a delayed dir index deletion queued up, but
5257          * we're removing the inode completely so that'll be taken care of in
5258          * the truncate.
5259          */
5260         btrfs_kill_delayed_inode_items(BTRFS_I(inode));
5261
5262         rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5263         if (!rsv)
5264                 goto out;
5265         rsv->size = btrfs_calc_metadata_size(fs_info, 1);
5266         rsv->failfast = true;
5267
5268         btrfs_i_size_write(BTRFS_I(inode), 0);
5269
5270         while (1) {
5271                 struct btrfs_truncate_control control = {
5272                         .inode = BTRFS_I(inode),
5273                         .ino = btrfs_ino(BTRFS_I(inode)),
5274                         .new_size = 0,
5275                         .min_type = 0,
5276                 };
5277
5278                 trans = evict_refill_and_join(root, rsv);
5279                 if (IS_ERR(trans))
5280                         goto out;
5281
5282                 trans->block_rsv = rsv;
5283
5284                 ret = btrfs_truncate_inode_items(trans, root, &control);
5285                 trans->block_rsv = &fs_info->trans_block_rsv;
5286                 btrfs_end_transaction(trans);
5287                 /*
5288                  * We have not added new delayed items for our inode after we
5289                  * have flushed its delayed items, so no need to throttle on
5290                  * delayed items. However we have modified extent buffers.
5291                  */
5292                 btrfs_btree_balance_dirty_nodelay(fs_info);
5293                 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5294                         goto out;
5295                 else if (!ret)
5296                         break;
5297         }
5298
5299         /*
5300          * Errors here aren't a big deal, it just means we leave orphan items in
5301          * the tree. They will be cleaned up on the next mount. If the inode
5302          * number gets reused, cleanup deletes the orphan item without doing
5303          * anything, and unlink reuses the existing orphan item.
5304          *
5305          * If it turns out that we are dropping too many of these, we might want
5306          * to add a mechanism for retrying these after a commit.
5307          */
5308         trans = evict_refill_and_join(root, rsv);
5309         if (!IS_ERR(trans)) {
5310                 trans->block_rsv = rsv;
5311                 btrfs_orphan_del(trans, BTRFS_I(inode));
5312                 trans->block_rsv = &fs_info->trans_block_rsv;
5313                 btrfs_end_transaction(trans);
5314         }
5315
5316 out:
5317         btrfs_free_block_rsv(fs_info, rsv);
5318         /*
5319          * If we didn't successfully delete, the orphan item will still be in
5320          * the tree and we'll retry on the next mount. Again, we might also want
5321          * to retry these periodically in the future.
5322          */
5323         btrfs_remove_delayed_node(BTRFS_I(inode));
5324         fsverity_cleanup_inode(inode);
5325         clear_inode(inode);
5326 }
5327
5328 /*
5329  * Return the key found in the dir entry in the location pointer, fill @type
5330  * with BTRFS_FT_*, and return 0.
5331  *
5332  * If no dir entries were found, returns -ENOENT.
5333  * If found a corrupted location in dir entry, returns -EUCLEAN.
5334  */
5335 static int btrfs_inode_by_name(struct btrfs_inode *dir, struct dentry *dentry,
5336                                struct btrfs_key *location, u8 *type)
5337 {
5338         struct btrfs_dir_item *di;
5339         struct btrfs_path *path;
5340         struct btrfs_root *root = dir->root;
5341         int ret = 0;
5342         struct fscrypt_name fname;
5343
5344         path = btrfs_alloc_path();
5345         if (!path)
5346                 return -ENOMEM;
5347
5348         ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname);
5349         if (ret < 0)
5350                 goto out;
5351         /*
5352          * fscrypt_setup_filename() should never return a positive value, but
5353          * gcc on sparc/parisc thinks it can, so assert that doesn't happen.
5354          */
5355         ASSERT(ret == 0);
5356
5357         /* This needs to handle no-key deletions later on */
5358
5359         di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir),
5360                                    &fname.disk_name, 0);
5361         if (IS_ERR_OR_NULL(di)) {
5362                 ret = di ? PTR_ERR(di) : -ENOENT;
5363                 goto out;
5364         }
5365
5366         btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5367         if (location->type != BTRFS_INODE_ITEM_KEY &&
5368             location->type != BTRFS_ROOT_ITEM_KEY) {
5369                 ret = -EUCLEAN;
5370                 btrfs_warn(root->fs_info,
5371 "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5372                            __func__, fname.disk_name.name, btrfs_ino(dir),
5373                            location->objectid, location->type, location->offset);
5374         }
5375         if (!ret)
5376                 *type = btrfs_dir_ftype(path->nodes[0], di);
5377 out:
5378         fscrypt_free_filename(&fname);
5379         btrfs_free_path(path);
5380         return ret;
5381 }
5382
5383 /*
5384  * when we hit a tree root in a directory, the btrfs part of the inode
5385  * needs to be changed to reflect the root directory of the tree root.  This
5386  * is kind of like crossing a mount point.
5387  */
5388 static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5389                                     struct btrfs_inode *dir,
5390                                     struct dentry *dentry,
5391                                     struct btrfs_key *location,
5392                                     struct btrfs_root **sub_root)
5393 {
5394         struct btrfs_path *path;
5395         struct btrfs_root *new_root;
5396         struct btrfs_root_ref *ref;
5397         struct extent_buffer *leaf;
5398         struct btrfs_key key;
5399         int ret;
5400         int err = 0;
5401         struct fscrypt_name fname;
5402
5403         ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 0, &fname);
5404         if (ret)
5405                 return ret;
5406
5407         path = btrfs_alloc_path();
5408         if (!path) {
5409                 err = -ENOMEM;
5410                 goto out;
5411         }
5412
5413         err = -ENOENT;
5414         key.objectid = dir->root->root_key.objectid;
5415         key.type = BTRFS_ROOT_REF_KEY;
5416         key.offset = location->objectid;
5417
5418         ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5419         if (ret) {
5420                 if (ret < 0)
5421                         err = ret;
5422                 goto out;
5423         }
5424
5425         leaf = path->nodes[0];
5426         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5427         if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
5428             btrfs_root_ref_name_len(leaf, ref) != fname.disk_name.len)
5429                 goto out;
5430
5431         ret = memcmp_extent_buffer(leaf, fname.disk_name.name,
5432                                    (unsigned long)(ref + 1), fname.disk_name.len);
5433         if (ret)
5434                 goto out;
5435
5436         btrfs_release_path(path);
5437
5438         new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
5439         if (IS_ERR(new_root)) {
5440                 err = PTR_ERR(new_root);
5441                 goto out;
5442         }
5443
5444         *sub_root = new_root;
5445         location->objectid = btrfs_root_dirid(&new_root->root_item);
5446         location->type = BTRFS_INODE_ITEM_KEY;
5447         location->offset = 0;
5448         err = 0;
5449 out:
5450         btrfs_free_path(path);
5451         fscrypt_free_filename(&fname);
5452         return err;
5453 }
5454
5455 static void inode_tree_add(struct btrfs_inode *inode)
5456 {
5457         struct btrfs_root *root = inode->root;
5458         struct btrfs_inode *entry;
5459         struct rb_node **p;
5460         struct rb_node *parent;
5461         struct rb_node *new = &inode->rb_node;
5462         u64 ino = btrfs_ino(inode);
5463
5464         if (inode_unhashed(&inode->vfs_inode))
5465                 return;
5466         parent = NULL;
5467         spin_lock(&root->inode_lock);
5468         p = &root->inode_tree.rb_node;
5469         while (*p) {
5470                 parent = *p;
5471                 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5472
5473                 if (ino < btrfs_ino(entry))
5474                         p = &parent->rb_left;
5475                 else if (ino > btrfs_ino(entry))
5476                         p = &parent->rb_right;
5477                 else {
5478                         WARN_ON(!(entry->vfs_inode.i_state &
5479                                   (I_WILL_FREE | I_FREEING)));
5480                         rb_replace_node(parent, new, &root->inode_tree);
5481                         RB_CLEAR_NODE(parent);
5482                         spin_unlock(&root->inode_lock);
5483                         return;
5484                 }
5485         }
5486         rb_link_node(new, parent, p);
5487         rb_insert_color(new, &root->inode_tree);
5488         spin_unlock(&root->inode_lock);
5489 }
5490
5491 static void inode_tree_del(struct btrfs_inode *inode)
5492 {
5493         struct btrfs_root *root = inode->root;
5494         int empty = 0;
5495
5496         spin_lock(&root->inode_lock);
5497         if (!RB_EMPTY_NODE(&inode->rb_node)) {
5498                 rb_erase(&inode->rb_node, &root->inode_tree);
5499                 RB_CLEAR_NODE(&inode->rb_node);
5500                 empty = RB_EMPTY_ROOT(&root->inode_tree);
5501         }
5502         spin_unlock(&root->inode_lock);
5503
5504         if (empty && btrfs_root_refs(&root->root_item) == 0) {
5505                 spin_lock(&root->inode_lock);
5506                 empty = RB_EMPTY_ROOT(&root->inode_tree);
5507                 spin_unlock(&root->inode_lock);
5508                 if (empty)
5509                         btrfs_add_dead_root(root);
5510         }
5511 }
5512
5513
5514 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5515 {
5516         struct btrfs_iget_args *args = p;
5517
5518         inode->i_ino = args->ino;
5519         BTRFS_I(inode)->location.objectid = args->ino;
5520         BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5521         BTRFS_I(inode)->location.offset = 0;
5522         BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5523         BUG_ON(args->root && !BTRFS_I(inode)->root);
5524
5525         if (args->root && args->root == args->root->fs_info->tree_root &&
5526             args->ino != BTRFS_BTREE_INODE_OBJECTID)
5527                 set_bit(BTRFS_INODE_FREE_SPACE_INODE,
5528                         &BTRFS_I(inode)->runtime_flags);
5529         return 0;
5530 }
5531
5532 static int btrfs_find_actor(struct inode *inode, void *opaque)
5533 {
5534         struct btrfs_iget_args *args = opaque;
5535
5536         return args->ino == BTRFS_I(inode)->location.objectid &&
5537                 args->root == BTRFS_I(inode)->root;
5538 }
5539
5540 static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5541                                        struct btrfs_root *root)
5542 {
5543         struct inode *inode;
5544         struct btrfs_iget_args args;
5545         unsigned long hashval = btrfs_inode_hash(ino, root);
5546
5547         args.ino = ino;
5548         args.root = root;
5549
5550         inode = iget5_locked(s, hashval, btrfs_find_actor,
5551                              btrfs_init_locked_inode,
5552                              (void *)&args);
5553         return inode;
5554 }
5555
5556 /*
5557  * Get an inode object given its inode number and corresponding root.
5558  * Path can be preallocated to prevent recursing back to iget through
5559  * allocator. NULL is also valid but may require an additional allocation
5560  * later.
5561  */
5562 struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5563                               struct btrfs_root *root, struct btrfs_path *path)
5564 {
5565         struct inode *inode;
5566
5567         inode = btrfs_iget_locked(s, ino, root);
5568         if (!inode)
5569                 return ERR_PTR(-ENOMEM);
5570
5571         if (inode->i_state & I_NEW) {
5572                 int ret;
5573
5574                 ret = btrfs_read_locked_inode(inode, path);
5575                 if (!ret) {
5576                         inode_tree_add(BTRFS_I(inode));
5577                         unlock_new_inode(inode);
5578                 } else {
5579                         iget_failed(inode);
5580                         /*
5581                          * ret > 0 can come from btrfs_search_slot called by
5582                          * btrfs_read_locked_inode, this means the inode item
5583                          * was not found.
5584                          */
5585                         if (ret > 0)
5586                                 ret = -ENOENT;
5587                         inode = ERR_PTR(ret);
5588                 }
5589         }
5590
5591         return inode;
5592 }
5593
5594 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
5595 {
5596         return btrfs_iget_path(s, ino, root, NULL);
5597 }
5598
5599 static struct inode *new_simple_dir(struct super_block *s,
5600                                     struct btrfs_key *key,
5601                                     struct btrfs_root *root)
5602 {
5603         struct inode *inode = new_inode(s);
5604
5605         if (!inode)
5606                 return ERR_PTR(-ENOMEM);
5607
5608         BTRFS_I(inode)->root = btrfs_grab_root(root);
5609         memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5610         set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5611
5612         inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5613         /*
5614          * We only need lookup, the rest is read-only and there's no inode
5615          * associated with the dentry
5616          */
5617         inode->i_op = &simple_dir_inode_operations;
5618         inode->i_opflags &= ~IOP_XATTR;
5619         inode->i_fop = &simple_dir_operations;
5620         inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5621         inode->i_mtime = current_time(inode);
5622         inode->i_atime = inode->i_mtime;
5623         inode->i_ctime = inode->i_mtime;
5624         BTRFS_I(inode)->i_otime = inode->i_mtime;
5625
5626         return inode;
5627 }
5628
5629 static_assert(BTRFS_FT_UNKNOWN == FT_UNKNOWN);
5630 static_assert(BTRFS_FT_REG_FILE == FT_REG_FILE);
5631 static_assert(BTRFS_FT_DIR == FT_DIR);
5632 static_assert(BTRFS_FT_CHRDEV == FT_CHRDEV);
5633 static_assert(BTRFS_FT_BLKDEV == FT_BLKDEV);
5634 static_assert(BTRFS_FT_FIFO == FT_FIFO);
5635 static_assert(BTRFS_FT_SOCK == FT_SOCK);
5636 static_assert(BTRFS_FT_SYMLINK == FT_SYMLINK);
5637
5638 static inline u8 btrfs_inode_type(struct inode *inode)
5639 {
5640         return fs_umode_to_ftype(inode->i_mode);
5641 }
5642
5643 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5644 {
5645         struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5646         struct inode *inode;
5647         struct btrfs_root *root = BTRFS_I(dir)->root;
5648         struct btrfs_root *sub_root = root;
5649         struct btrfs_key location;
5650         u8 di_type = 0;
5651         int ret = 0;
5652
5653         if (dentry->d_name.len > BTRFS_NAME_LEN)
5654                 return ERR_PTR(-ENAMETOOLONG);
5655
5656         ret = btrfs_inode_by_name(BTRFS_I(dir), dentry, &location, &di_type);
5657         if (ret < 0)
5658                 return ERR_PTR(ret);
5659
5660         if (location.type == BTRFS_INODE_ITEM_KEY) {
5661                 inode = btrfs_iget(dir->i_sb, location.objectid, root);
5662                 if (IS_ERR(inode))
5663                         return inode;
5664
5665                 /* Do extra check against inode mode with di_type */
5666                 if (btrfs_inode_type(inode) != di_type) {
5667                         btrfs_crit(fs_info,
5668 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5669                                   inode->i_mode, btrfs_inode_type(inode),
5670                                   di_type);
5671                         iput(inode);
5672                         return ERR_PTR(-EUCLEAN);
5673                 }
5674                 return inode;
5675         }
5676
5677         ret = fixup_tree_root_location(fs_info, BTRFS_I(dir), dentry,
5678                                        &location, &sub_root);
5679         if (ret < 0) {
5680                 if (ret != -ENOENT)
5681                         inode = ERR_PTR(ret);
5682                 else
5683                         inode = new_simple_dir(dir->i_sb, &location, root);
5684         } else {
5685                 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
5686                 btrfs_put_root(sub_root);
5687
5688                 if (IS_ERR(inode))
5689                         return inode;
5690
5691                 down_read(&fs_info->cleanup_work_sem);
5692                 if (!sb_rdonly(inode->i_sb))
5693                         ret = btrfs_orphan_cleanup(sub_root);
5694                 up_read(&fs_info->cleanup_work_sem);
5695                 if (ret) {
5696                         iput(inode);
5697                         inode = ERR_PTR(ret);
5698                 }
5699         }
5700
5701         return inode;
5702 }
5703
5704 static int btrfs_dentry_delete(const struct dentry *dentry)
5705 {
5706         struct btrfs_root *root;
5707         struct inode *inode = d_inode(dentry);
5708
5709         if (!inode && !IS_ROOT(dentry))
5710                 inode = d_inode(dentry->d_parent);
5711
5712         if (inode) {
5713                 root = BTRFS_I(inode)->root;
5714                 if (btrfs_root_refs(&root->root_item) == 0)
5715                         return 1;
5716
5717                 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5718                         return 1;
5719         }
5720         return 0;
5721 }
5722
5723 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5724                                    unsigned int flags)
5725 {
5726         struct inode *inode = btrfs_lookup_dentry(dir, dentry);
5727
5728         if (inode == ERR_PTR(-ENOENT))
5729                 inode = NULL;
5730         return d_splice_alias(inode, dentry);
5731 }
5732
5733 /*
5734  * Find the highest existing sequence number in a directory and then set the
5735  * in-memory index_cnt variable to the first free sequence number.
5736  */
5737 static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
5738 {
5739         struct btrfs_root *root = inode->root;
5740         struct btrfs_key key, found_key;
5741         struct btrfs_path *path;
5742         struct extent_buffer *leaf;
5743         int ret;
5744
5745         key.objectid = btrfs_ino(inode);
5746         key.type = BTRFS_DIR_INDEX_KEY;
5747         key.offset = (u64)-1;
5748
5749         path = btrfs_alloc_path();
5750         if (!path)
5751                 return -ENOMEM;
5752
5753         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5754         if (ret < 0)
5755                 goto out;
5756         /* FIXME: we should be able to handle this */
5757         if (ret == 0)
5758                 goto out;
5759         ret = 0;
5760
5761         if (path->slots[0] == 0) {
5762                 inode->index_cnt = BTRFS_DIR_START_INDEX;
5763                 goto out;
5764         }
5765
5766         path->slots[0]--;
5767
5768         leaf = path->nodes[0];
5769         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5770
5771         if (found_key.objectid != btrfs_ino(inode) ||
5772             found_key.type != BTRFS_DIR_INDEX_KEY) {
5773                 inode->index_cnt = BTRFS_DIR_START_INDEX;
5774                 goto out;
5775         }
5776
5777         inode->index_cnt = found_key.offset + 1;
5778 out:
5779         btrfs_free_path(path);
5780         return ret;
5781 }
5782
5783 static int btrfs_get_dir_last_index(struct btrfs_inode *dir, u64 *index)
5784 {
5785         if (dir->index_cnt == (u64)-1) {
5786                 int ret;
5787
5788                 ret = btrfs_inode_delayed_dir_index_count(dir);
5789                 if (ret) {
5790                         ret = btrfs_set_inode_index_count(dir);
5791                         if (ret)
5792                                 return ret;
5793                 }
5794         }
5795
5796         *index = dir->index_cnt;
5797
5798         return 0;
5799 }
5800
5801 /*
5802  * All this infrastructure exists because dir_emit can fault, and we are holding
5803  * the tree lock when doing readdir.  For now just allocate a buffer and copy
5804  * our information into that, and then dir_emit from the buffer.  This is
5805  * similar to what NFS does, only we don't keep the buffer around in pagecache
5806  * because I'm afraid I'll mess that up.  Long term we need to make filldir do
5807  * copy_to_user_inatomic so we don't have to worry about page faulting under the
5808  * tree lock.
5809  */
5810 static int btrfs_opendir(struct inode *inode, struct file *file)
5811 {
5812         struct btrfs_file_private *private;
5813         u64 last_index;
5814         int ret;
5815
5816         ret = btrfs_get_dir_last_index(BTRFS_I(inode), &last_index);
5817         if (ret)
5818                 return ret;
5819
5820         private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5821         if (!private)
5822                 return -ENOMEM;
5823         private->last_index = last_index;
5824         private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5825         if (!private->filldir_buf) {
5826                 kfree(private);
5827                 return -ENOMEM;
5828         }
5829         file->private_data = private;
5830         return 0;
5831 }
5832
5833 struct dir_entry {
5834         u64 ino;
5835         u64 offset;
5836         unsigned type;
5837         int name_len;
5838 };
5839
5840 static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5841 {
5842         while (entries--) {
5843                 struct dir_entry *entry = addr;
5844                 char *name = (char *)(entry + 1);
5845
5846                 ctx->pos = get_unaligned(&entry->offset);
5847                 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5848                                          get_unaligned(&entry->ino),
5849                                          get_unaligned(&entry->type)))
5850                         return 1;
5851                 addr += sizeof(struct dir_entry) +
5852                         get_unaligned(&entry->name_len);
5853                 ctx->pos++;
5854         }
5855         return 0;
5856 }
5857
5858 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5859 {
5860         struct inode *inode = file_inode(file);
5861         struct btrfs_root *root = BTRFS_I(inode)->root;
5862         struct btrfs_file_private *private = file->private_data;
5863         struct btrfs_dir_item *di;
5864         struct btrfs_key key;
5865         struct btrfs_key found_key;
5866         struct btrfs_path *path;
5867         void *addr;
5868         struct list_head ins_list;
5869         struct list_head del_list;
5870         int ret;
5871         char *name_ptr;
5872         int name_len;
5873         int entries = 0;
5874         int total_len = 0;
5875         bool put = false;
5876         struct btrfs_key location;
5877
5878         if (!dir_emit_dots(file, ctx))
5879                 return 0;
5880
5881         path = btrfs_alloc_path();
5882         if (!path)
5883                 return -ENOMEM;
5884
5885         addr = private->filldir_buf;
5886         path->reada = READA_FORWARD;
5887
5888         INIT_LIST_HEAD(&ins_list);
5889         INIT_LIST_HEAD(&del_list);
5890         put = btrfs_readdir_get_delayed_items(inode, private->last_index,
5891                                               &ins_list, &del_list);
5892
5893 again:
5894         key.type = BTRFS_DIR_INDEX_KEY;
5895         key.offset = ctx->pos;
5896         key.objectid = btrfs_ino(BTRFS_I(inode));
5897
5898         btrfs_for_each_slot(root, &key, &found_key, path, ret) {
5899                 struct dir_entry *entry;
5900                 struct extent_buffer *leaf = path->nodes[0];
5901                 u8 ftype;
5902
5903                 if (found_key.objectid != key.objectid)
5904                         break;
5905                 if (found_key.type != BTRFS_DIR_INDEX_KEY)
5906                         break;
5907                 if (found_key.offset < ctx->pos)
5908                         continue;
5909                 if (found_key.offset > private->last_index)
5910                         break;
5911                 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
5912                         continue;
5913                 di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
5914                 name_len = btrfs_dir_name_len(leaf, di);
5915                 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5916                     PAGE_SIZE) {
5917                         btrfs_release_path(path);
5918                         ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5919                         if (ret)
5920                                 goto nopos;
5921                         addr = private->filldir_buf;
5922                         entries = 0;
5923                         total_len = 0;
5924                         goto again;
5925                 }
5926
5927                 ftype = btrfs_dir_flags_to_ftype(btrfs_dir_flags(leaf, di));
5928                 entry = addr;
5929                 name_ptr = (char *)(entry + 1);
5930                 read_extent_buffer(leaf, name_ptr,
5931                                    (unsigned long)(di + 1), name_len);
5932                 put_unaligned(name_len, &entry->name_len);
5933                 put_unaligned(fs_ftype_to_dtype(ftype), &entry->type);
5934                 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5935                 put_unaligned(location.objectid, &entry->ino);
5936                 put_unaligned(found_key.offset, &entry->offset);
5937                 entries++;
5938                 addr += sizeof(struct dir_entry) + name_len;
5939                 total_len += sizeof(struct dir_entry) + name_len;
5940         }
5941         /* Catch error encountered during iteration */
5942         if (ret < 0)
5943                 goto err;
5944
5945         btrfs_release_path(path);
5946
5947         ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5948         if (ret)
5949                 goto nopos;
5950
5951         ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5952         if (ret)
5953                 goto nopos;
5954
5955         /*
5956          * Stop new entries from being returned after we return the last
5957          * entry.
5958          *
5959          * New directory entries are assigned a strictly increasing
5960          * offset.  This means that new entries created during readdir
5961          * are *guaranteed* to be seen in the future by that readdir.
5962          * This has broken buggy programs which operate on names as
5963          * they're returned by readdir.  Until we re-use freed offsets
5964          * we have this hack to stop new entries from being returned
5965          * under the assumption that they'll never reach this huge
5966          * offset.
5967          *
5968          * This is being careful not to overflow 32bit loff_t unless the
5969          * last entry requires it because doing so has broken 32bit apps
5970          * in the past.
5971          */
5972         if (ctx->pos >= INT_MAX)
5973                 ctx->pos = LLONG_MAX;
5974         else
5975                 ctx->pos = INT_MAX;
5976 nopos:
5977         ret = 0;
5978 err:
5979         if (put)
5980                 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
5981         btrfs_free_path(path);
5982         return ret;
5983 }
5984
5985 /*
5986  * This is somewhat expensive, updating the tree every time the
5987  * inode changes.  But, it is most likely to find the inode in cache.
5988  * FIXME, needs more benchmarking...there are no reasons other than performance
5989  * to keep or drop this code.
5990  */
5991 static int btrfs_dirty_inode(struct btrfs_inode *inode)
5992 {
5993         struct btrfs_root *root = inode->root;
5994         struct btrfs_fs_info *fs_info = root->fs_info;
5995         struct btrfs_trans_handle *trans;
5996         int ret;
5997
5998         if (test_bit(BTRFS_INODE_DUMMY, &inode->runtime_flags))
5999                 return 0;
6000
6001         trans = btrfs_join_transaction(root);
6002         if (IS_ERR(trans))
6003                 return PTR_ERR(trans);
6004
6005         ret = btrfs_update_inode(trans, root, inode);
6006         if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
6007                 /* whoops, lets try again with the full transaction */
6008                 btrfs_end_transaction(trans);
6009                 trans = btrfs_start_transaction(root, 1);
6010                 if (IS_ERR(trans))
6011                         return PTR_ERR(trans);
6012
6013                 ret = btrfs_update_inode(trans, root, inode);
6014         }
6015         btrfs_end_transaction(trans);
6016         if (inode->delayed_node)
6017                 btrfs_balance_delayed_items(fs_info);
6018
6019         return ret;
6020 }
6021
6022 /*
6023  * This is a copy of file_update_time.  We need this so we can return error on
6024  * ENOSPC for updating the inode in the case of file write and mmap writes.
6025  */
6026 static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
6027                              int flags)
6028 {
6029         struct btrfs_root *root = BTRFS_I(inode)->root;
6030         bool dirty = flags & ~S_VERSION;
6031
6032         if (btrfs_root_readonly(root))
6033                 return -EROFS;
6034
6035         if (flags & S_VERSION)
6036                 dirty |= inode_maybe_inc_iversion(inode, dirty);
6037         if (flags & S_CTIME)
6038                 inode->i_ctime = *now;
6039         if (flags & S_MTIME)
6040                 inode->i_mtime = *now;
6041         if (flags & S_ATIME)
6042                 inode->i_atime = *now;
6043         return dirty ? btrfs_dirty_inode(BTRFS_I(inode)) : 0;
6044 }
6045
6046 /*
6047  * helper to find a free sequence number in a given directory.  This current
6048  * code is very simple, later versions will do smarter things in the btree
6049  */
6050 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
6051 {
6052         int ret = 0;
6053
6054         if (dir->index_cnt == (u64)-1) {
6055                 ret = btrfs_inode_delayed_dir_index_count(dir);
6056                 if (ret) {
6057                         ret = btrfs_set_inode_index_count(dir);
6058                         if (ret)
6059                                 return ret;
6060                 }
6061         }
6062
6063         *index = dir->index_cnt;
6064         dir->index_cnt++;
6065
6066         return ret;
6067 }
6068
6069 static int btrfs_insert_inode_locked(struct inode *inode)
6070 {
6071         struct btrfs_iget_args args;
6072
6073         args.ino = BTRFS_I(inode)->location.objectid;
6074         args.root = BTRFS_I(inode)->root;
6075
6076         return insert_inode_locked4(inode,
6077                    btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6078                    btrfs_find_actor, &args);
6079 }
6080
6081 int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
6082                             unsigned int *trans_num_items)
6083 {
6084         struct inode *dir = args->dir;
6085         struct inode *inode = args->inode;
6086         int ret;
6087
6088         if (!args->orphan) {
6089                 ret = fscrypt_setup_filename(dir, &args->dentry->d_name, 0,
6090                                              &args->fname);
6091                 if (ret)
6092                         return ret;
6093         }
6094
6095         ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl);
6096         if (ret) {
6097                 fscrypt_free_filename(&args->fname);
6098                 return ret;
6099         }
6100
6101         /* 1 to add inode item */
6102         *trans_num_items = 1;
6103         /* 1 to add compression property */
6104         if (BTRFS_I(dir)->prop_compress)
6105                 (*trans_num_items)++;
6106         /* 1 to add default ACL xattr */
6107         if (args->default_acl)
6108                 (*trans_num_items)++;
6109         /* 1 to add access ACL xattr */
6110         if (args->acl)
6111                 (*trans_num_items)++;
6112 #ifdef CONFIG_SECURITY
6113         /* 1 to add LSM xattr */
6114         if (dir->i_security)
6115                 (*trans_num_items)++;
6116 #endif
6117         if (args->orphan) {
6118                 /* 1 to add orphan item */
6119                 (*trans_num_items)++;
6120         } else {
6121                 /*
6122                  * 1 to add dir item
6123                  * 1 to add dir index
6124                  * 1 to update parent inode item
6125                  *
6126                  * No need for 1 unit for the inode ref item because it is
6127                  * inserted in a batch together with the inode item at
6128                  * btrfs_create_new_inode().
6129                  */
6130                 *trans_num_items += 3;
6131         }
6132         return 0;
6133 }
6134
6135 void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args)
6136 {
6137         posix_acl_release(args->acl);
6138         posix_acl_release(args->default_acl);
6139         fscrypt_free_filename(&args->fname);
6140 }
6141
6142 /*
6143  * Inherit flags from the parent inode.
6144  *
6145  * Currently only the compression flags and the cow flags are inherited.
6146  */
6147 static void btrfs_inherit_iflags(struct btrfs_inode *inode, struct btrfs_inode *dir)
6148 {
6149         unsigned int flags;
6150
6151         flags = dir->flags;
6152
6153         if (flags & BTRFS_INODE_NOCOMPRESS) {
6154                 inode->flags &= ~BTRFS_INODE_COMPRESS;
6155                 inode->flags |= BTRFS_INODE_NOCOMPRESS;
6156         } else if (flags & BTRFS_INODE_COMPRESS) {
6157                 inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
6158                 inode->flags |= BTRFS_INODE_COMPRESS;
6159         }
6160
6161         if (flags & BTRFS_INODE_NODATACOW) {
6162                 inode->flags |= BTRFS_INODE_NODATACOW;
6163                 if (S_ISREG(inode->vfs_inode.i_mode))
6164                         inode->flags |= BTRFS_INODE_NODATASUM;
6165         }
6166
6167         btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);
6168 }
6169
6170 int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
6171                            struct btrfs_new_inode_args *args)
6172 {
6173         struct inode *dir = args->dir;
6174         struct inode *inode = args->inode;
6175         const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name;
6176         struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6177         struct btrfs_root *root;
6178         struct btrfs_inode_item *inode_item;
6179         struct btrfs_key *location;
6180         struct btrfs_path *path;
6181         u64 objectid;
6182         struct btrfs_inode_ref *ref;
6183         struct btrfs_key key[2];
6184         u32 sizes[2];
6185         struct btrfs_item_batch batch;
6186         unsigned long ptr;
6187         int ret;
6188
6189         path = btrfs_alloc_path();
6190         if (!path)
6191                 return -ENOMEM;
6192
6193         if (!args->subvol)
6194                 BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root);
6195         root = BTRFS_I(inode)->root;
6196
6197         ret = btrfs_get_free_objectid(root, &objectid);
6198         if (ret)
6199                 goto out;
6200         inode->i_ino = objectid;
6201
6202         if (args->orphan) {
6203                 /*
6204                  * O_TMPFILE, set link count to 0, so that after this point, we
6205                  * fill in an inode item with the correct link count.
6206                  */
6207                 set_nlink(inode, 0);
6208         } else {
6209                 trace_btrfs_inode_request(dir);
6210
6211                 ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index);
6212                 if (ret)
6213                         goto out;
6214         }
6215         /* index_cnt is ignored for everything but a dir. */
6216         BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX;
6217         BTRFS_I(inode)->generation = trans->transid;
6218         inode->i_generation = BTRFS_I(inode)->generation;
6219
6220         /*
6221          * Subvolumes don't inherit flags from their parent directory.
6222          * Originally this was probably by accident, but we probably can't
6223          * change it now without compatibility issues.
6224          */
6225         if (!args->subvol)
6226                 btrfs_inherit_iflags(BTRFS_I(inode), BTRFS_I(dir));
6227
6228         if (S_ISREG(inode->i_mode)) {
6229                 if (btrfs_test_opt(fs_info, NODATASUM))
6230                         BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6231                 if (btrfs_test_opt(fs_info, NODATACOW))
6232                         BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6233                                 BTRFS_INODE_NODATASUM;
6234         }
6235
6236         location = &BTRFS_I(inode)->location;
6237         location->objectid = objectid;
6238         location->offset = 0;
6239         location->type = BTRFS_INODE_ITEM_KEY;
6240
6241         ret = btrfs_insert_inode_locked(inode);
6242         if (ret < 0) {
6243                 if (!args->orphan)
6244                         BTRFS_I(dir)->index_cnt--;
6245                 goto out;
6246         }
6247
6248         /*
6249          * We could have gotten an inode number from somebody who was fsynced
6250          * and then removed in this same transaction, so let's just set full
6251          * sync since it will be a full sync anyway and this will blow away the
6252          * old info in the log.
6253          */
6254         btrfs_set_inode_full_sync(BTRFS_I(inode));
6255
6256         key[0].objectid = objectid;
6257         key[0].type = BTRFS_INODE_ITEM_KEY;
6258         key[0].offset = 0;
6259
6260         sizes[0] = sizeof(struct btrfs_inode_item);
6261
6262         if (!args->orphan) {
6263                 /*
6264                  * Start new inodes with an inode_ref. This is slightly more
6265                  * efficient for small numbers of hard links since they will
6266                  * be packed into one item. Extended refs will kick in if we
6267                  * add more hard links than can fit in the ref item.
6268                  */
6269                 key[1].objectid = objectid;
6270                 key[1].type = BTRFS_INODE_REF_KEY;
6271                 if (args->subvol) {
6272                         key[1].offset = objectid;
6273                         sizes[1] = 2 + sizeof(*ref);
6274                 } else {
6275                         key[1].offset = btrfs_ino(BTRFS_I(dir));
6276                         sizes[1] = name->len + sizeof(*ref);
6277                 }
6278         }
6279
6280         batch.keys = &key[0];
6281         batch.data_sizes = &sizes[0];
6282         batch.total_data_size = sizes[0] + (args->orphan ? 0 : sizes[1]);
6283         batch.nr = args->orphan ? 1 : 2;
6284         ret = btrfs_insert_empty_items(trans, root, path, &batch);
6285         if (ret != 0) {
6286                 btrfs_abort_transaction(trans, ret);
6287                 goto discard;
6288         }
6289
6290         inode->i_mtime = current_time(inode);
6291         inode->i_atime = inode->i_mtime;
6292         inode->i_ctime = inode->i_mtime;
6293         BTRFS_I(inode)->i_otime = inode->i_mtime;
6294
6295         /*
6296          * We're going to fill the inode item now, so at this point the inode
6297          * must be fully initialized.
6298          */
6299
6300         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6301                                   struct btrfs_inode_item);
6302         memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
6303                              sizeof(*inode_item));
6304         fill_inode_item(trans, path->nodes[0], inode_item, inode);
6305
6306         if (!args->orphan) {
6307                 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6308                                      struct btrfs_inode_ref);
6309                 ptr = (unsigned long)(ref + 1);
6310                 if (args->subvol) {
6311                         btrfs_set_inode_ref_name_len(path->nodes[0], ref, 2);
6312                         btrfs_set_inode_ref_index(path->nodes[0], ref, 0);
6313                         write_extent_buffer(path->nodes[0], "..", ptr, 2);
6314                 } else {
6315                         btrfs_set_inode_ref_name_len(path->nodes[0], ref,
6316                                                      name->len);
6317                         btrfs_set_inode_ref_index(path->nodes[0], ref,
6318                                                   BTRFS_I(inode)->dir_index);
6319                         write_extent_buffer(path->nodes[0], name->name, ptr,
6320                                             name->len);
6321                 }
6322         }
6323
6324         btrfs_mark_buffer_dirty(path->nodes[0]);
6325         /*
6326          * We don't need the path anymore, plus inheriting properties, adding
6327          * ACLs, security xattrs, orphan item or adding the link, will result in
6328          * allocating yet another path. So just free our path.
6329          */
6330         btrfs_free_path(path);
6331         path = NULL;
6332
6333         if (args->subvol) {
6334                 struct inode *parent;
6335
6336                 /*
6337                  * Subvolumes inherit properties from their parent subvolume,
6338                  * not the directory they were created in.
6339                  */
6340                 parent = btrfs_iget(fs_info->sb, BTRFS_FIRST_FREE_OBJECTID,
6341                                     BTRFS_I(dir)->root);
6342                 if (IS_ERR(parent)) {
6343                         ret = PTR_ERR(parent);
6344                 } else {
6345                         ret = btrfs_inode_inherit_props(trans, inode, parent);
6346                         iput(parent);
6347                 }
6348         } else {
6349                 ret = btrfs_inode_inherit_props(trans, inode, dir);
6350         }
6351         if (ret) {
6352                 btrfs_err(fs_info,
6353                           "error inheriting props for ino %llu (root %llu): %d",
6354                           btrfs_ino(BTRFS_I(inode)), root->root_key.objectid,
6355                           ret);
6356         }
6357
6358         /*
6359          * Subvolumes don't inherit ACLs or get passed to the LSM. This is
6360          * probably a bug.
6361          */
6362         if (!args->subvol) {
6363                 ret = btrfs_init_inode_security(trans, args);
6364                 if (ret) {
6365                         btrfs_abort_transaction(trans, ret);
6366                         goto discard;
6367                 }
6368         }
6369
6370         inode_tree_add(BTRFS_I(inode));
6371
6372         trace_btrfs_inode_new(inode);
6373         btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
6374
6375         btrfs_update_root_times(trans, root);
6376
6377         if (args->orphan) {
6378                 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
6379         } else {
6380                 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
6381                                      0, BTRFS_I(inode)->dir_index);
6382         }
6383         if (ret) {
6384                 btrfs_abort_transaction(trans, ret);
6385                 goto discard;
6386         }
6387
6388         return 0;
6389
6390 discard:
6391         /*
6392          * discard_new_inode() calls iput(), but the caller owns the reference
6393          * to the inode.
6394          */
6395         ihold(inode);
6396         discard_new_inode(inode);
6397 out:
6398         btrfs_free_path(path);
6399         return ret;
6400 }
6401
6402 /*
6403  * utility function to add 'inode' into 'parent_inode' with
6404  * a give name and a given sequence number.
6405  * if 'add_backref' is true, also insert a backref from the
6406  * inode to the parent directory.
6407  */
6408 int btrfs_add_link(struct btrfs_trans_handle *trans,
6409                    struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
6410                    const struct fscrypt_str *name, int add_backref, u64 index)
6411 {
6412         int ret = 0;
6413         struct btrfs_key key;
6414         struct btrfs_root *root = parent_inode->root;
6415         u64 ino = btrfs_ino(inode);
6416         u64 parent_ino = btrfs_ino(parent_inode);
6417
6418         if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6419                 memcpy(&key, &inode->root->root_key, sizeof(key));
6420         } else {
6421                 key.objectid = ino;
6422                 key.type = BTRFS_INODE_ITEM_KEY;
6423                 key.offset = 0;
6424         }
6425
6426         if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6427                 ret = btrfs_add_root_ref(trans, key.objectid,
6428                                          root->root_key.objectid, parent_ino,
6429                                          index, name);
6430         } else if (add_backref) {
6431                 ret = btrfs_insert_inode_ref(trans, root, name,
6432                                              ino, parent_ino, index);
6433         }
6434
6435         /* Nothing to clean up yet */
6436         if (ret)
6437                 return ret;
6438
6439         ret = btrfs_insert_dir_item(trans, name, parent_inode, &key,
6440                                     btrfs_inode_type(&inode->vfs_inode), index);
6441         if (ret == -EEXIST || ret == -EOVERFLOW)
6442                 goto fail_dir_item;
6443         else if (ret) {
6444                 btrfs_abort_transaction(trans, ret);
6445                 return ret;
6446         }
6447
6448         btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
6449                            name->len * 2);
6450         inode_inc_iversion(&parent_inode->vfs_inode);
6451         /*
6452          * If we are replaying a log tree, we do not want to update the mtime
6453          * and ctime of the parent directory with the current time, since the
6454          * log replay procedure is responsible for setting them to their correct
6455          * values (the ones it had when the fsync was done).
6456          */
6457         if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6458                 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6459
6460                 parent_inode->vfs_inode.i_mtime = now;
6461                 parent_inode->vfs_inode.i_ctime = now;
6462         }
6463         ret = btrfs_update_inode(trans, root, parent_inode);
6464         if (ret)
6465                 btrfs_abort_transaction(trans, ret);
6466         return ret;
6467
6468 fail_dir_item:
6469         if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6470                 u64 local_index;
6471                 int err;
6472                 err = btrfs_del_root_ref(trans, key.objectid,
6473                                          root->root_key.objectid, parent_ino,
6474                                          &local_index, name);
6475                 if (err)
6476                         btrfs_abort_transaction(trans, err);
6477         } else if (add_backref) {
6478                 u64 local_index;
6479                 int err;
6480
6481                 err = btrfs_del_inode_ref(trans, root, name, ino, parent_ino,
6482                                           &local_index);
6483                 if (err)
6484                         btrfs_abort_transaction(trans, err);
6485         }
6486
6487         /* Return the original error code */
6488         return ret;
6489 }
6490
6491 static int btrfs_create_common(struct inode *dir, struct dentry *dentry,
6492                                struct inode *inode)
6493 {
6494         struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6495         struct btrfs_root *root = BTRFS_I(dir)->root;
6496         struct btrfs_new_inode_args new_inode_args = {
6497                 .dir = dir,
6498                 .dentry = dentry,
6499                 .inode = inode,
6500         };
6501         unsigned int trans_num_items;
6502         struct btrfs_trans_handle *trans;
6503         int err;
6504
6505         err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
6506         if (err)
6507                 goto out_inode;
6508
6509         trans = btrfs_start_transaction(root, trans_num_items);
6510         if (IS_ERR(trans)) {
6511                 err = PTR_ERR(trans);
6512                 goto out_new_inode_args;
6513         }
6514
6515         err = btrfs_create_new_inode(trans, &new_inode_args);
6516         if (!err)
6517                 d_instantiate_new(dentry, inode);
6518
6519         btrfs_end_transaction(trans);
6520         btrfs_btree_balance_dirty(fs_info);
6521 out_new_inode_args:
6522         btrfs_new_inode_args_destroy(&new_inode_args);
6523 out_inode:
6524         if (err)
6525                 iput(inode);
6526         return err;
6527 }
6528
6529 static int btrfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
6530                        struct dentry *dentry, umode_t mode, dev_t rdev)
6531 {
6532         struct inode *inode;
6533
6534         inode = new_inode(dir->i_sb);
6535         if (!inode)
6536                 return -ENOMEM;
6537         inode_init_owner(idmap, inode, dir, mode);
6538         inode->i_op = &btrfs_special_inode_operations;
6539         init_special_inode(inode, inode->i_mode, rdev);
6540         return btrfs_create_common(dir, dentry, inode);
6541 }
6542
6543 static int btrfs_create(struct mnt_idmap *idmap, struct inode *dir,
6544                         struct dentry *dentry, umode_t mode, bool excl)
6545 {
6546         struct inode *inode;
6547
6548         inode = new_inode(dir->i_sb);
6549         if (!inode)
6550                 return -ENOMEM;
6551         inode_init_owner(idmap, inode, dir, mode);
6552         inode->i_fop = &btrfs_file_operations;
6553         inode->i_op = &btrfs_file_inode_operations;
6554         inode->i_mapping->a_ops = &btrfs_aops;
6555         return btrfs_create_common(dir, dentry, inode);
6556 }
6557
6558 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6559                       struct dentry *dentry)
6560 {
6561         struct btrfs_trans_handle *trans = NULL;
6562         struct btrfs_root *root = BTRFS_I(dir)->root;
6563         struct inode *inode = d_inode(old_dentry);
6564         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6565         struct fscrypt_name fname;
6566         u64 index;
6567         int err;
6568         int drop_inode = 0;
6569
6570         /* do not allow sys_link's with other subvols of the same device */
6571         if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
6572                 return -EXDEV;
6573
6574         if (inode->i_nlink >= BTRFS_LINK_MAX)
6575                 return -EMLINK;
6576
6577         err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &fname);
6578         if (err)
6579                 goto fail;
6580
6581         err = btrfs_set_inode_index(BTRFS_I(dir), &index);
6582         if (err)
6583                 goto fail;
6584
6585         /*
6586          * 2 items for inode and inode ref
6587          * 2 items for dir items
6588          * 1 item for parent inode
6589          * 1 item for orphan item deletion if O_TMPFILE
6590          */
6591         trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
6592         if (IS_ERR(trans)) {
6593                 err = PTR_ERR(trans);
6594                 trans = NULL;
6595                 goto fail;
6596         }
6597
6598         /* There are several dir indexes for this inode, clear the cache. */
6599         BTRFS_I(inode)->dir_index = 0ULL;
6600         inc_nlink(inode);
6601         inode_inc_iversion(inode);
6602         inode->i_ctime = current_time(inode);
6603         ihold(inode);
6604         set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6605
6606         err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6607                              &fname.disk_name, 1, index);
6608
6609         if (err) {
6610                 drop_inode = 1;
6611         } else {
6612                 struct dentry *parent = dentry->d_parent;
6613
6614                 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
6615                 if (err)
6616                         goto fail;
6617                 if (inode->i_nlink == 1) {
6618                         /*
6619                          * If new hard link count is 1, it's a file created
6620                          * with open(2) O_TMPFILE flag.
6621                          */
6622                         err = btrfs_orphan_del(trans, BTRFS_I(inode));
6623                         if (err)
6624                                 goto fail;
6625                 }
6626                 d_instantiate(dentry, inode);
6627                 btrfs_log_new_name(trans, old_dentry, NULL, 0, parent);
6628         }
6629
6630 fail:
6631         fscrypt_free_filename(&fname);
6632         if (trans)
6633                 btrfs_end_transaction(trans);
6634         if (drop_inode) {
6635                 inode_dec_link_count(inode);
6636                 iput(inode);
6637         }
6638         btrfs_btree_balance_dirty(fs_info);
6639         return err;
6640 }
6641
6642 static int btrfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
6643                        struct dentry *dentry, umode_t mode)
6644 {
6645         struct inode *inode;
6646
6647         inode = new_inode(dir->i_sb);
6648         if (!inode)
6649                 return -ENOMEM;
6650         inode_init_owner(idmap, inode, dir, S_IFDIR | mode);
6651         inode->i_op = &btrfs_dir_inode_operations;
6652         inode->i_fop = &btrfs_dir_file_operations;
6653         return btrfs_create_common(dir, dentry, inode);
6654 }
6655
6656 static noinline int uncompress_inline(struct btrfs_path *path,
6657                                       struct page *page,
6658                                       struct btrfs_file_extent_item *item)
6659 {
6660         int ret;
6661         struct extent_buffer *leaf = path->nodes[0];
6662         char *tmp;
6663         size_t max_size;
6664         unsigned long inline_size;
6665         unsigned long ptr;
6666         int compress_type;
6667
6668         compress_type = btrfs_file_extent_compression(leaf, item);
6669         max_size = btrfs_file_extent_ram_bytes(leaf, item);
6670         inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
6671         tmp = kmalloc(inline_size, GFP_NOFS);
6672         if (!tmp)
6673                 return -ENOMEM;
6674         ptr = btrfs_file_extent_inline_start(item);
6675
6676         read_extent_buffer(leaf, tmp, ptr, inline_size);
6677
6678         max_size = min_t(unsigned long, PAGE_SIZE, max_size);
6679         ret = btrfs_decompress(compress_type, tmp, page, 0, inline_size, max_size);
6680
6681         /*
6682          * decompression code contains a memset to fill in any space between the end
6683          * of the uncompressed data and the end of max_size in case the decompressed
6684          * data ends up shorter than ram_bytes.  That doesn't cover the hole between
6685          * the end of an inline extent and the beginning of the next block, so we
6686          * cover that region here.
6687          */
6688
6689         if (max_size < PAGE_SIZE)
6690                 memzero_page(page, max_size, PAGE_SIZE - max_size);
6691         kfree(tmp);
6692         return ret;
6693 }
6694
6695 static int read_inline_extent(struct btrfs_inode *inode, struct btrfs_path *path,
6696                               struct page *page)
6697 {
6698         struct btrfs_file_extent_item *fi;
6699         void *kaddr;
6700         size_t copy_size;
6701
6702         if (!page || PageUptodate(page))
6703                 return 0;
6704
6705         ASSERT(page_offset(page) == 0);
6706
6707         fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
6708                             struct btrfs_file_extent_item);
6709         if (btrfs_file_extent_compression(path->nodes[0], fi) != BTRFS_COMPRESS_NONE)
6710                 return uncompress_inline(path, page, fi);
6711
6712         copy_size = min_t(u64, PAGE_SIZE,
6713                           btrfs_file_extent_ram_bytes(path->nodes[0], fi));
6714         kaddr = kmap_local_page(page);
6715         read_extent_buffer(path->nodes[0], kaddr,
6716                            btrfs_file_extent_inline_start(fi), copy_size);
6717         kunmap_local(kaddr);
6718         if (copy_size < PAGE_SIZE)
6719                 memzero_page(page, copy_size, PAGE_SIZE - copy_size);
6720         return 0;
6721 }
6722
6723 /*
6724  * Lookup the first extent overlapping a range in a file.
6725  *
6726  * @inode:      file to search in
6727  * @page:       page to read extent data into if the extent is inline
6728  * @pg_offset:  offset into @page to copy to
6729  * @start:      file offset
6730  * @len:        length of range starting at @start
6731  *
6732  * Return the first &struct extent_map which overlaps the given range, reading
6733  * it from the B-tree and caching it if necessary. Note that there may be more
6734  * extents which overlap the given range after the returned extent_map.
6735  *
6736  * If @page is not NULL and the extent is inline, this also reads the extent
6737  * data directly into the page and marks the extent up to date in the io_tree.
6738  *
6739  * Return: ERR_PTR on error, non-NULL extent_map on success.
6740  */
6741 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6742                                     struct page *page, size_t pg_offset,
6743                                     u64 start, u64 len)
6744 {
6745         struct btrfs_fs_info *fs_info = inode->root->fs_info;
6746         int ret = 0;
6747         u64 extent_start = 0;
6748         u64 extent_end = 0;
6749         u64 objectid = btrfs_ino(inode);
6750         int extent_type = -1;
6751         struct btrfs_path *path = NULL;
6752         struct btrfs_root *root = inode->root;
6753         struct btrfs_file_extent_item *item;
6754         struct extent_buffer *leaf;
6755         struct btrfs_key found_key;
6756         struct extent_map *em = NULL;
6757         struct extent_map_tree *em_tree = &inode->extent_tree;
6758
6759         read_lock(&em_tree->lock);
6760         em = lookup_extent_mapping(em_tree, start, len);
6761         read_unlock(&em_tree->lock);
6762
6763         if (em) {
6764                 if (em->start > start || em->start + em->len <= start)
6765                         free_extent_map(em);
6766                 else if (em->block_start == EXTENT_MAP_INLINE && page)
6767                         free_extent_map(em);
6768                 else
6769                         goto out;
6770         }
6771         em = alloc_extent_map();
6772         if (!em) {
6773                 ret = -ENOMEM;
6774                 goto out;
6775         }
6776         em->start = EXTENT_MAP_HOLE;
6777         em->orig_start = EXTENT_MAP_HOLE;
6778         em->len = (u64)-1;
6779         em->block_len = (u64)-1;
6780
6781         path = btrfs_alloc_path();
6782         if (!path) {
6783                 ret = -ENOMEM;
6784                 goto out;
6785         }
6786
6787         /* Chances are we'll be called again, so go ahead and do readahead */
6788         path->reada = READA_FORWARD;
6789
6790         /*
6791          * The same explanation in load_free_space_cache applies here as well,
6792          * we only read when we're loading the free space cache, and at that
6793          * point the commit_root has everything we need.
6794          */
6795         if (btrfs_is_free_space_inode(inode)) {
6796                 path->search_commit_root = 1;
6797                 path->skip_locking = 1;
6798         }
6799
6800         ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
6801         if (ret < 0) {
6802                 goto out;
6803         } else if (ret > 0) {
6804                 if (path->slots[0] == 0)
6805                         goto not_found;
6806                 path->slots[0]--;
6807                 ret = 0;
6808         }
6809
6810         leaf = path->nodes[0];
6811         item = btrfs_item_ptr(leaf, path->slots[0],
6812                               struct btrfs_file_extent_item);
6813         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6814         if (found_key.objectid != objectid ||
6815             found_key.type != BTRFS_EXTENT_DATA_KEY) {
6816                 /*
6817                  * If we backup past the first extent we want to move forward
6818                  * and see if there is an extent in front of us, otherwise we'll
6819                  * say there is a hole for our whole search range which can
6820                  * cause problems.
6821                  */
6822                 extent_end = start;
6823                 goto next;
6824         }
6825
6826         extent_type = btrfs_file_extent_type(leaf, item);
6827         extent_start = found_key.offset;
6828         extent_end = btrfs_file_extent_end(path);
6829         if (extent_type == BTRFS_FILE_EXTENT_REG ||
6830             extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
6831                 /* Only regular file could have regular/prealloc extent */
6832                 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6833                         ret = -EUCLEAN;
6834                         btrfs_crit(fs_info,
6835                 "regular/prealloc extent found for non-regular inode %llu",
6836                                    btrfs_ino(inode));
6837                         goto out;
6838                 }
6839                 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6840                                                        extent_start);
6841         } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
6842                 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6843                                                       path->slots[0],
6844                                                       extent_start);
6845         }
6846 next:
6847         if (start >= extent_end) {
6848                 path->slots[0]++;
6849                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6850                         ret = btrfs_next_leaf(root, path);
6851                         if (ret < 0)
6852                                 goto out;
6853                         else if (ret > 0)
6854                                 goto not_found;
6855
6856                         leaf = path->nodes[0];
6857                 }
6858                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6859                 if (found_key.objectid != objectid ||
6860                     found_key.type != BTRFS_EXTENT_DATA_KEY)
6861                         goto not_found;
6862                 if (start + len <= found_key.offset)
6863                         goto not_found;
6864                 if (start > found_key.offset)
6865                         goto next;
6866
6867                 /* New extent overlaps with existing one */
6868                 em->start = start;
6869                 em->orig_start = start;
6870                 em->len = found_key.offset - start;
6871                 em->block_start = EXTENT_MAP_HOLE;
6872                 goto insert;
6873         }
6874
6875         btrfs_extent_item_to_extent_map(inode, path, item, em);
6876
6877         if (extent_type == BTRFS_FILE_EXTENT_REG ||
6878             extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
6879                 goto insert;
6880         } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
6881                 /*
6882                  * Inline extent can only exist at file offset 0. This is
6883                  * ensured by tree-checker and inline extent creation path.
6884                  * Thus all members representing file offsets should be zero.
6885                  */
6886                 ASSERT(pg_offset == 0);
6887                 ASSERT(extent_start == 0);
6888                 ASSERT(em->start == 0);
6889
6890                 /*
6891                  * btrfs_extent_item_to_extent_map() should have properly
6892                  * initialized em members already.
6893                  *
6894                  * Other members are not utilized for inline extents.
6895                  */
6896                 ASSERT(em->block_start == EXTENT_MAP_INLINE);
6897                 ASSERT(em->len == fs_info->sectorsize);
6898
6899                 ret = read_inline_extent(inode, path, page);
6900                 if (ret < 0)
6901                         goto out;
6902                 goto insert;
6903         }
6904 not_found:
6905         em->start = start;
6906         em->orig_start = start;
6907         em->len = len;
6908         em->block_start = EXTENT_MAP_HOLE;
6909 insert:
6910         ret = 0;
6911         btrfs_release_path(path);
6912         if (em->start > start || extent_map_end(em) <= start) {
6913                 btrfs_err(fs_info,
6914                           "bad extent! em: [%llu %llu] passed [%llu %llu]",
6915                           em->start, em->len, start, len);
6916                 ret = -EIO;
6917                 goto out;
6918         }
6919
6920         write_lock(&em_tree->lock);
6921         ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
6922         write_unlock(&em_tree->lock);
6923 out:
6924         btrfs_free_path(path);
6925
6926         trace_btrfs_get_extent(root, inode, em);
6927
6928         if (ret) {
6929                 free_extent_map(em);
6930                 return ERR_PTR(ret);
6931         }
6932         return em;
6933 }
6934
6935 static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
6936                                                   struct btrfs_dio_data *dio_data,
6937                                                   const u64 start,
6938                                                   const u64 len,
6939                                                   const u64 orig_start,
6940                                                   const u64 block_start,
6941                                                   const u64 block_len,
6942                                                   const u64 orig_block_len,
6943                                                   const u64 ram_bytes,
6944                                                   const int type)
6945 {
6946         struct extent_map *em = NULL;
6947         struct btrfs_ordered_extent *ordered;
6948
6949         if (type != BTRFS_ORDERED_NOCOW) {
6950                 em = create_io_em(inode, start, len, orig_start, block_start,
6951                                   block_len, orig_block_len, ram_bytes,
6952                                   BTRFS_COMPRESS_NONE, /* compress_type */
6953                                   type);
6954                 if (IS_ERR(em))
6955                         goto out;
6956         }
6957         ordered = btrfs_alloc_ordered_extent(inode, start, len, len,
6958                                              block_start, block_len, 0,
6959                                              (1 << type) |
6960                                              (1 << BTRFS_ORDERED_DIRECT),
6961                                              BTRFS_COMPRESS_NONE);
6962         if (IS_ERR(ordered)) {
6963                 if (em) {
6964                         free_extent_map(em);
6965                         btrfs_drop_extent_map_range(inode, start,
6966                                                     start + len - 1, false);
6967                 }
6968                 em = ERR_CAST(ordered);
6969         } else {
6970                 ASSERT(!dio_data->ordered);
6971                 dio_data->ordered = ordered;
6972         }
6973  out:
6974
6975         return em;
6976 }
6977
6978 static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
6979                                                   struct btrfs_dio_data *dio_data,
6980                                                   u64 start, u64 len)
6981 {
6982         struct btrfs_root *root = inode->root;
6983         struct btrfs_fs_info *fs_info = root->fs_info;
6984         struct extent_map *em;
6985         struct btrfs_key ins;
6986         u64 alloc_hint;
6987         int ret;
6988
6989         alloc_hint = get_extent_allocation_hint(inode, start, len);
6990         ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
6991                                    0, alloc_hint, &ins, 1, 1);
6992         if (ret)
6993                 return ERR_PTR(ret);
6994
6995         em = btrfs_create_dio_extent(inode, dio_data, start, ins.offset, start,
6996                                      ins.objectid, ins.offset, ins.offset,
6997                                      ins.offset, BTRFS_ORDERED_REGULAR);
6998         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
6999         if (IS_ERR(em))
7000                 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7001                                            1);
7002
7003         return em;
7004 }
7005
7006 static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
7007 {
7008         struct btrfs_block_group *block_group;
7009         bool readonly = false;
7010
7011         block_group = btrfs_lookup_block_group(fs_info, bytenr);
7012         if (!block_group || block_group->ro)
7013                 readonly = true;
7014         if (block_group)
7015                 btrfs_put_block_group(block_group);
7016         return readonly;
7017 }
7018
7019 /*
7020  * Check if we can do nocow write into the range [@offset, @offset + @len)
7021  *
7022  * @offset:     File offset
7023  * @len:        The length to write, will be updated to the nocow writeable
7024  *              range
7025  * @orig_start: (optional) Return the original file offset of the file extent
7026  * @orig_len:   (optional) Return the original on-disk length of the file extent
7027  * @ram_bytes:  (optional) Return the ram_bytes of the file extent
7028  * @strict:     if true, omit optimizations that might force us into unnecessary
7029  *              cow. e.g., don't trust generation number.
7030  *
7031  * Return:
7032  * >0   and update @len if we can do nocow write
7033  *  0   if we can't do nocow write
7034  * <0   if error happened
7035  *
7036  * NOTE: This only checks the file extents, caller is responsible to wait for
7037  *       any ordered extents.
7038  */
7039 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7040                               u64 *orig_start, u64 *orig_block_len,
7041                               u64 *ram_bytes, bool nowait, bool strict)
7042 {
7043         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7044         struct can_nocow_file_extent_args nocow_args = { 0 };
7045         struct btrfs_path *path;
7046         int ret;
7047         struct extent_buffer *leaf;
7048         struct btrfs_root *root = BTRFS_I(inode)->root;
7049         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7050         struct btrfs_file_extent_item *fi;
7051         struct btrfs_key key;
7052         int found_type;
7053
7054         path = btrfs_alloc_path();
7055         if (!path)
7056                 return -ENOMEM;
7057         path->nowait = nowait;
7058
7059         ret = btrfs_lookup_file_extent(NULL, root, path,
7060                         btrfs_ino(BTRFS_I(inode)), offset, 0);
7061         if (ret < 0)
7062                 goto out;
7063
7064         if (ret == 1) {
7065                 if (path->slots[0] == 0) {
7066                         /* can't find the item, must cow */
7067                         ret = 0;
7068                         goto out;
7069                 }
7070                 path->slots[0]--;
7071         }
7072         ret = 0;
7073         leaf = path->nodes[0];
7074         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7075         if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7076             key.type != BTRFS_EXTENT_DATA_KEY) {
7077                 /* not our file or wrong item type, must cow */
7078                 goto out;
7079         }
7080
7081         if (key.offset > offset) {
7082                 /* Wrong offset, must cow */
7083                 goto out;
7084         }
7085
7086         if (btrfs_file_extent_end(path) <= offset)
7087                 goto out;
7088
7089         fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
7090         found_type = btrfs_file_extent_type(leaf, fi);
7091         if (ram_bytes)
7092                 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7093
7094         nocow_args.start = offset;
7095         nocow_args.end = offset + *len - 1;
7096         nocow_args.strict = strict;
7097         nocow_args.free_path = true;
7098
7099         ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args);
7100         /* can_nocow_file_extent() has freed the path. */
7101         path = NULL;
7102
7103         if (ret != 1) {
7104                 /* Treat errors as not being able to NOCOW. */
7105                 ret = 0;
7106                 goto out;
7107         }
7108
7109         ret = 0;
7110         if (btrfs_extent_readonly(fs_info, nocow_args.disk_bytenr))
7111                 goto out;
7112
7113         if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7114             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7115                 u64 range_end;
7116
7117                 range_end = round_up(offset + nocow_args.num_bytes,
7118                                      root->fs_info->sectorsize) - 1;
7119                 ret = test_range_bit(io_tree, offset, range_end,
7120                                      EXTENT_DELALLOC, 0, NULL);
7121                 if (ret) {
7122                         ret = -EAGAIN;
7123                         goto out;
7124                 }
7125         }
7126
7127         if (orig_start)
7128                 *orig_start = key.offset - nocow_args.extent_offset;
7129         if (orig_block_len)
7130                 *orig_block_len = nocow_args.disk_num_bytes;
7131
7132         *len = nocow_args.num_bytes;
7133         ret = 1;
7134 out:
7135         btrfs_free_path(path);
7136         return ret;
7137 }
7138
7139 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7140                               struct extent_state **cached_state,
7141                               unsigned int iomap_flags)
7142 {
7143         const bool writing = (iomap_flags & IOMAP_WRITE);
7144         const bool nowait = (iomap_flags & IOMAP_NOWAIT);
7145         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7146         struct btrfs_ordered_extent *ordered;
7147         int ret = 0;
7148
7149         while (1) {
7150                 if (nowait) {
7151                         if (!try_lock_extent(io_tree, lockstart, lockend,
7152                                              cached_state))
7153                                 return -EAGAIN;
7154                 } else {
7155                         lock_extent(io_tree, lockstart, lockend, cached_state);
7156                 }
7157                 /*
7158                  * We're concerned with the entire range that we're going to be
7159                  * doing DIO to, so we need to make sure there's no ordered
7160                  * extents in this range.
7161                  */
7162                 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
7163                                                      lockend - lockstart + 1);
7164
7165                 /*
7166                  * We need to make sure there are no buffered pages in this
7167                  * range either, we could have raced between the invalidate in
7168                  * generic_file_direct_write and locking the extent.  The
7169                  * invalidate needs to happen so that reads after a write do not
7170                  * get stale data.
7171                  */
7172                 if (!ordered &&
7173                     (!writing || !filemap_range_has_page(inode->i_mapping,
7174                                                          lockstart, lockend)))
7175                         break;
7176
7177                 unlock_extent(io_tree, lockstart, lockend, cached_state);
7178
7179                 if (ordered) {
7180                         if (nowait) {
7181                                 btrfs_put_ordered_extent(ordered);
7182                                 ret = -EAGAIN;
7183                                 break;
7184                         }
7185                         /*
7186                          * If we are doing a DIO read and the ordered extent we
7187                          * found is for a buffered write, we can not wait for it
7188                          * to complete and retry, because if we do so we can
7189                          * deadlock with concurrent buffered writes on page
7190                          * locks. This happens only if our DIO read covers more
7191                          * than one extent map, if at this point has already
7192                          * created an ordered extent for a previous extent map
7193                          * and locked its range in the inode's io tree, and a
7194                          * concurrent write against that previous extent map's
7195                          * range and this range started (we unlock the ranges
7196                          * in the io tree only when the bios complete and
7197                          * buffered writes always lock pages before attempting
7198                          * to lock range in the io tree).
7199                          */
7200                         if (writing ||
7201                             test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7202                                 btrfs_start_ordered_extent(ordered);
7203                         else
7204                                 ret = nowait ? -EAGAIN : -ENOTBLK;
7205                         btrfs_put_ordered_extent(ordered);
7206                 } else {
7207                         /*
7208                          * We could trigger writeback for this range (and wait
7209                          * for it to complete) and then invalidate the pages for
7210                          * this range (through invalidate_inode_pages2_range()),
7211                          * but that can lead us to a deadlock with a concurrent
7212                          * call to readahead (a buffered read or a defrag call
7213                          * triggered a readahead) on a page lock due to an
7214                          * ordered dio extent we created before but did not have
7215                          * yet a corresponding bio submitted (whence it can not
7216                          * complete), which makes readahead wait for that
7217                          * ordered extent to complete while holding a lock on
7218                          * that page.
7219                          */
7220                         ret = nowait ? -EAGAIN : -ENOTBLK;
7221                 }
7222
7223                 if (ret)
7224                         break;
7225
7226                 cond_resched();
7227         }
7228
7229         return ret;
7230 }
7231
7232 /* The callers of this must take lock_extent() */
7233 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7234                                        u64 len, u64 orig_start, u64 block_start,
7235                                        u64 block_len, u64 orig_block_len,
7236                                        u64 ram_bytes, int compress_type,
7237                                        int type)
7238 {
7239         struct extent_map *em;
7240         int ret;
7241
7242         ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7243                type == BTRFS_ORDERED_COMPRESSED ||
7244                type == BTRFS_ORDERED_NOCOW ||
7245                type == BTRFS_ORDERED_REGULAR);
7246
7247         em = alloc_extent_map();
7248         if (!em)
7249                 return ERR_PTR(-ENOMEM);
7250
7251         em->start = start;
7252         em->orig_start = orig_start;
7253         em->len = len;
7254         em->block_len = block_len;
7255         em->block_start = block_start;
7256         em->orig_block_len = orig_block_len;
7257         em->ram_bytes = ram_bytes;
7258         em->generation = -1;
7259         set_bit(EXTENT_FLAG_PINNED, &em->flags);
7260         if (type == BTRFS_ORDERED_PREALLOC) {
7261                 set_bit(EXTENT_FLAG_FILLING, &em->flags);
7262         } else if (type == BTRFS_ORDERED_COMPRESSED) {
7263                 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7264                 em->compress_type = compress_type;
7265         }
7266
7267         ret = btrfs_replace_extent_map_range(inode, em, true);
7268         if (ret) {
7269                 free_extent_map(em);
7270                 return ERR_PTR(ret);
7271         }
7272
7273         /* em got 2 refs now, callers needs to do free_extent_map once. */
7274         return em;
7275 }
7276
7277
7278 static int btrfs_get_blocks_direct_write(struct extent_map **map,
7279                                          struct inode *inode,
7280                                          struct btrfs_dio_data *dio_data,
7281                                          u64 start, u64 *lenp,
7282                                          unsigned int iomap_flags)
7283 {
7284         const bool nowait = (iomap_flags & IOMAP_NOWAIT);
7285         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7286         struct extent_map *em = *map;
7287         int type;
7288         u64 block_start, orig_start, orig_block_len, ram_bytes;
7289         struct btrfs_block_group *bg;
7290         bool can_nocow = false;
7291         bool space_reserved = false;
7292         u64 len = *lenp;
7293         u64 prev_len;
7294         int ret = 0;
7295
7296         /*
7297          * We don't allocate a new extent in the following cases
7298          *
7299          * 1) The inode is marked as NODATACOW. In this case we'll just use the
7300          * existing extent.
7301          * 2) The extent is marked as PREALLOC. We're good to go here and can
7302          * just use the extent.
7303          *
7304          */
7305         if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7306             ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7307              em->block_start != EXTENT_MAP_HOLE)) {
7308                 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7309                         type = BTRFS_ORDERED_PREALLOC;
7310                 else
7311                         type = BTRFS_ORDERED_NOCOW;
7312                 len = min(len, em->len - (start - em->start));
7313                 block_start = em->block_start + (start - em->start);
7314
7315                 if (can_nocow_extent(inode, start, &len, &orig_start,
7316                                      &orig_block_len, &ram_bytes, false, false) == 1) {
7317                         bg = btrfs_inc_nocow_writers(fs_info, block_start);
7318                         if (bg)
7319                                 can_nocow = true;
7320                 }
7321         }
7322
7323         prev_len = len;
7324         if (can_nocow) {
7325                 struct extent_map *em2;
7326
7327                 /* We can NOCOW, so only need to reserve metadata space. */
7328                 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7329                                                       nowait);
7330                 if (ret < 0) {
7331                         /* Our caller expects us to free the input extent map. */
7332                         free_extent_map(em);
7333                         *map = NULL;
7334                         btrfs_dec_nocow_writers(bg);
7335                         if (nowait && (ret == -ENOSPC || ret == -EDQUOT))
7336                                 ret = -EAGAIN;
7337                         goto out;
7338                 }
7339                 space_reserved = true;
7340
7341                 em2 = btrfs_create_dio_extent(BTRFS_I(inode), dio_data, start, len,
7342                                               orig_start, block_start,
7343                                               len, orig_block_len,
7344                                               ram_bytes, type);
7345                 btrfs_dec_nocow_writers(bg);
7346                 if (type == BTRFS_ORDERED_PREALLOC) {
7347                         free_extent_map(em);
7348                         *map = em2;
7349                         em = em2;
7350                 }
7351
7352                 if (IS_ERR(em2)) {
7353                         ret = PTR_ERR(em2);
7354                         goto out;
7355                 }
7356
7357                 dio_data->nocow_done = true;
7358         } else {
7359                 /* Our caller expects us to free the input extent map. */
7360                 free_extent_map(em);
7361                 *map = NULL;
7362
7363                 if (nowait) {
7364                         ret = -EAGAIN;
7365                         goto out;
7366                 }
7367
7368                 /*
7369                  * If we could not allocate data space before locking the file
7370                  * range and we can't do a NOCOW write, then we have to fail.
7371                  */
7372                 if (!dio_data->data_space_reserved) {
7373                         ret = -ENOSPC;
7374                         goto out;
7375                 }
7376
7377                 /*
7378                  * We have to COW and we have already reserved data space before,
7379                  * so now we reserve only metadata.
7380                  */
7381                 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7382                                                       false);
7383                 if (ret < 0)
7384                         goto out;
7385                 space_reserved = true;
7386
7387                 em = btrfs_new_extent_direct(BTRFS_I(inode), dio_data, start, len);
7388                 if (IS_ERR(em)) {
7389                         ret = PTR_ERR(em);
7390                         goto out;
7391                 }
7392                 *map = em;
7393                 len = min(len, em->len - (start - em->start));
7394                 if (len < prev_len)
7395                         btrfs_delalloc_release_metadata(BTRFS_I(inode),
7396                                                         prev_len - len, true);
7397         }
7398
7399         /*
7400          * We have created our ordered extent, so we can now release our reservation
7401          * for an outstanding extent.
7402          */
7403         btrfs_delalloc_release_extents(BTRFS_I(inode), prev_len);
7404
7405         /*
7406          * Need to update the i_size under the extent lock so buffered
7407          * readers will get the updated i_size when we unlock.
7408          */
7409         if (start + len > i_size_read(inode))
7410                 i_size_write(inode, start + len);
7411 out:
7412         if (ret && space_reserved) {
7413                 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
7414                 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true);
7415         }
7416         *lenp = len;
7417         return ret;
7418 }
7419
7420 static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7421                 loff_t length, unsigned int flags, struct iomap *iomap,
7422                 struct iomap *srcmap)
7423 {
7424         struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
7425         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7426         struct extent_map *em;
7427         struct extent_state *cached_state = NULL;
7428         struct btrfs_dio_data *dio_data = iter->private;
7429         u64 lockstart, lockend;
7430         const bool write = !!(flags & IOMAP_WRITE);
7431         int ret = 0;
7432         u64 len = length;
7433         const u64 data_alloc_len = length;
7434         bool unlock_extents = false;
7435
7436         /*
7437          * We could potentially fault if we have a buffer > PAGE_SIZE, and if
7438          * we're NOWAIT we may submit a bio for a partial range and return
7439          * EIOCBQUEUED, which would result in an errant short read.
7440          *
7441          * The best way to handle this would be to allow for partial completions
7442          * of iocb's, so we could submit the partial bio, return and fault in
7443          * the rest of the pages, and then submit the io for the rest of the
7444          * range.  However we don't have that currently, so simply return
7445          * -EAGAIN at this point so that the normal path is used.
7446          */
7447         if (!write && (flags & IOMAP_NOWAIT) && length > PAGE_SIZE)
7448                 return -EAGAIN;
7449
7450         /*
7451          * Cap the size of reads to that usually seen in buffered I/O as we need
7452          * to allocate a contiguous array for the checksums.
7453          */
7454         if (!write)
7455                 len = min_t(u64, len, fs_info->sectorsize * BTRFS_MAX_BIO_SECTORS);
7456
7457         lockstart = start;
7458         lockend = start + len - 1;
7459
7460         /*
7461          * iomap_dio_rw() only does filemap_write_and_wait_range(), which isn't
7462          * enough if we've written compressed pages to this area, so we need to
7463          * flush the dirty pages again to make absolutely sure that any
7464          * outstanding dirty pages are on disk - the first flush only starts
7465          * compression on the data, while keeping the pages locked, so by the
7466          * time the second flush returns we know bios for the compressed pages
7467          * were submitted and finished, and the pages no longer under writeback.
7468          *
7469          * If we have a NOWAIT request and we have any pages in the range that
7470          * are locked, likely due to compression still in progress, we don't want
7471          * to block on page locks. We also don't want to block on pages marked as
7472          * dirty or under writeback (same as for the non-compression case).
7473          * iomap_dio_rw() did the same check, but after that and before we got
7474          * here, mmap'ed writes may have happened or buffered reads started
7475          * (readpage() and readahead(), which lock pages), as we haven't locked
7476          * the file range yet.
7477          */
7478         if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7479                      &BTRFS_I(inode)->runtime_flags)) {
7480                 if (flags & IOMAP_NOWAIT) {
7481                         if (filemap_range_needs_writeback(inode->i_mapping,
7482                                                           lockstart, lockend))
7483                                 return -EAGAIN;
7484                 } else {
7485                         ret = filemap_fdatawrite_range(inode->i_mapping, start,
7486                                                        start + length - 1);
7487                         if (ret)
7488                                 return ret;
7489                 }
7490         }
7491
7492         memset(dio_data, 0, sizeof(*dio_data));
7493
7494         /*
7495          * We always try to allocate data space and must do it before locking
7496          * the file range, to avoid deadlocks with concurrent writes to the same
7497          * range if the range has several extents and the writes don't expand the
7498          * current i_size (the inode lock is taken in shared mode). If we fail to
7499          * allocate data space here we continue and later, after locking the
7500          * file range, we fail with ENOSPC only if we figure out we can not do a
7501          * NOCOW write.
7502          */
7503         if (write && !(flags & IOMAP_NOWAIT)) {
7504                 ret = btrfs_check_data_free_space(BTRFS_I(inode),
7505                                                   &dio_data->data_reserved,
7506                                                   start, data_alloc_len, false);
7507                 if (!ret)
7508                         dio_data->data_space_reserved = true;
7509                 else if (ret && !(BTRFS_I(inode)->flags &
7510                                   (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
7511                         goto err;
7512         }
7513
7514         /*
7515          * If this errors out it's because we couldn't invalidate pagecache for
7516          * this range and we need to fallback to buffered IO, or we are doing a
7517          * NOWAIT read/write and we need to block.
7518          */
7519         ret = lock_extent_direct(inode, lockstart, lockend, &cached_state, flags);
7520         if (ret < 0)
7521                 goto err;
7522
7523         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
7524         if (IS_ERR(em)) {
7525                 ret = PTR_ERR(em);
7526                 goto unlock_err;
7527         }
7528
7529         /*
7530          * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7531          * io.  INLINE is special, and we could probably kludge it in here, but
7532          * it's still buffered so for safety lets just fall back to the generic
7533          * buffered path.
7534          *
7535          * For COMPRESSED we _have_ to read the entire extent in so we can
7536          * decompress it, so there will be buffering required no matter what we
7537          * do, so go ahead and fallback to buffered.
7538          *
7539          * We return -ENOTBLK because that's what makes DIO go ahead and go back
7540          * to buffered IO.  Don't blame me, this is the price we pay for using
7541          * the generic code.
7542          */
7543         if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7544             em->block_start == EXTENT_MAP_INLINE) {
7545                 free_extent_map(em);
7546                 /*
7547                  * If we are in a NOWAIT context, return -EAGAIN in order to
7548                  * fallback to buffered IO. This is not only because we can
7549                  * block with buffered IO (no support for NOWAIT semantics at
7550                  * the moment) but also to avoid returning short reads to user
7551                  * space - this happens if we were able to read some data from
7552                  * previous non-compressed extents and then when we fallback to
7553                  * buffered IO, at btrfs_file_read_iter() by calling
7554                  * filemap_read(), we fail to fault in pages for the read buffer,
7555                  * in which case filemap_read() returns a short read (the number
7556                  * of bytes previously read is > 0, so it does not return -EFAULT).
7557                  */
7558                 ret = (flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOTBLK;
7559                 goto unlock_err;
7560         }
7561
7562         len = min(len, em->len - (start - em->start));
7563
7564         /*
7565          * If we have a NOWAIT request and the range contains multiple extents
7566          * (or a mix of extents and holes), then we return -EAGAIN to make the
7567          * caller fallback to a context where it can do a blocking (without
7568          * NOWAIT) request. This way we avoid doing partial IO and returning
7569          * success to the caller, which is not optimal for writes and for reads
7570          * it can result in unexpected behaviour for an application.
7571          *
7572          * When doing a read, because we use IOMAP_DIO_PARTIAL when calling
7573          * iomap_dio_rw(), we can end up returning less data then what the caller
7574          * asked for, resulting in an unexpected, and incorrect, short read.
7575          * That is, the caller asked to read N bytes and we return less than that,
7576          * which is wrong unless we are crossing EOF. This happens if we get a
7577          * page fault error when trying to fault in pages for the buffer that is
7578          * associated to the struct iov_iter passed to iomap_dio_rw(), and we
7579          * have previously submitted bios for other extents in the range, in
7580          * which case iomap_dio_rw() may return us EIOCBQUEUED if not all of
7581          * those bios have completed by the time we get the page fault error,
7582          * which we return back to our caller - we should only return EIOCBQUEUED
7583          * after we have submitted bios for all the extents in the range.
7584          */
7585         if ((flags & IOMAP_NOWAIT) && len < length) {
7586                 free_extent_map(em);
7587                 ret = -EAGAIN;
7588                 goto unlock_err;
7589         }
7590
7591         if (write) {
7592                 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7593                                                     start, &len, flags);
7594                 if (ret < 0)
7595                         goto unlock_err;
7596                 unlock_extents = true;
7597                 /* Recalc len in case the new em is smaller than requested */
7598                 len = min(len, em->len - (start - em->start));
7599                 if (dio_data->data_space_reserved) {
7600                         u64 release_offset;
7601                         u64 release_len = 0;
7602
7603                         if (dio_data->nocow_done) {
7604                                 release_offset = start;
7605                                 release_len = data_alloc_len;
7606                         } else if (len < data_alloc_len) {
7607                                 release_offset = start + len;
7608                                 release_len = data_alloc_len - len;
7609                         }
7610
7611                         if (release_len > 0)
7612                                 btrfs_free_reserved_data_space(BTRFS_I(inode),
7613                                                                dio_data->data_reserved,
7614                                                                release_offset,
7615                                                                release_len);
7616                 }
7617         } else {
7618                 /*
7619                  * We need to unlock only the end area that we aren't using.
7620                  * The rest is going to be unlocked by the endio routine.
7621                  */
7622                 lockstart = start + len;
7623                 if (lockstart < lockend)
7624                         unlock_extents = true;
7625         }
7626
7627         if (unlock_extents)
7628                 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7629                               &cached_state);
7630         else
7631                 free_extent_state(cached_state);
7632
7633         /*
7634          * Translate extent map information to iomap.
7635          * We trim the extents (and move the addr) even though iomap code does
7636          * that, since we have locked only the parts we are performing I/O in.
7637          */
7638         if ((em->block_start == EXTENT_MAP_HOLE) ||
7639             (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7640                 iomap->addr = IOMAP_NULL_ADDR;
7641                 iomap->type = IOMAP_HOLE;
7642         } else {
7643                 iomap->addr = em->block_start + (start - em->start);
7644                 iomap->type = IOMAP_MAPPED;
7645         }
7646         iomap->offset = start;
7647         iomap->bdev = fs_info->fs_devices->latest_dev->bdev;
7648         iomap->length = len;
7649         free_extent_map(em);
7650
7651         return 0;
7652
7653 unlock_err:
7654         unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7655                       &cached_state);
7656 err:
7657         if (dio_data->data_space_reserved) {
7658                 btrfs_free_reserved_data_space(BTRFS_I(inode),
7659                                                dio_data->data_reserved,
7660                                                start, data_alloc_len);
7661                 extent_changeset_free(dio_data->data_reserved);
7662         }
7663
7664         return ret;
7665 }
7666
7667 static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7668                 ssize_t written, unsigned int flags, struct iomap *iomap)
7669 {
7670         struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
7671         struct btrfs_dio_data *dio_data = iter->private;
7672         size_t submitted = dio_data->submitted;
7673         const bool write = !!(flags & IOMAP_WRITE);
7674         int ret = 0;
7675
7676         if (!write && (iomap->type == IOMAP_HOLE)) {
7677                 /* If reading from a hole, unlock and return */
7678                 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1,
7679                               NULL);
7680                 return 0;
7681         }
7682
7683         if (submitted < length) {
7684                 pos += submitted;
7685                 length -= submitted;
7686                 if (write)
7687                         btrfs_finish_ordered_extent(dio_data->ordered, NULL,
7688                                                     pos, length, false);
7689                 else
7690                         unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7691                                       pos + length - 1, NULL);
7692                 ret = -ENOTBLK;
7693         }
7694         if (write) {
7695                 btrfs_put_ordered_extent(dio_data->ordered);
7696                 dio_data->ordered = NULL;
7697         }
7698
7699         if (write)
7700                 extent_changeset_free(dio_data->data_reserved);
7701         return ret;
7702 }
7703
7704 static void btrfs_dio_end_io(struct btrfs_bio *bbio)
7705 {
7706         struct btrfs_dio_private *dip =
7707                 container_of(bbio, struct btrfs_dio_private, bbio);
7708         struct btrfs_inode *inode = bbio->inode;
7709         struct bio *bio = &bbio->bio;
7710
7711         if (bio->bi_status) {
7712                 btrfs_warn(inode->root->fs_info,
7713                 "direct IO failed ino %llu op 0x%0x offset %#llx len %u err no %d",
7714                            btrfs_ino(inode), bio->bi_opf,
7715                            dip->file_offset, dip->bytes, bio->bi_status);
7716         }
7717
7718         if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
7719                 btrfs_finish_ordered_extent(bbio->ordered, NULL,
7720                                             dip->file_offset, dip->bytes,
7721                                             !bio->bi_status);
7722         } else {
7723                 unlock_extent(&inode->io_tree, dip->file_offset,
7724                               dip->file_offset + dip->bytes - 1, NULL);
7725         }
7726
7727         bbio->bio.bi_private = bbio->private;
7728         iomap_dio_bio_end_io(bio);
7729 }
7730
7731 static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
7732                                 loff_t file_offset)
7733 {
7734         struct btrfs_bio *bbio = btrfs_bio(bio);
7735         struct btrfs_dio_private *dip =
7736                 container_of(bbio, struct btrfs_dio_private, bbio);
7737         struct btrfs_dio_data *dio_data = iter->private;
7738
7739         btrfs_bio_init(bbio, BTRFS_I(iter->inode)->root->fs_info,
7740                        btrfs_dio_end_io, bio->bi_private);
7741         bbio->inode = BTRFS_I(iter->inode);
7742         bbio->file_offset = file_offset;
7743
7744         dip->file_offset = file_offset;
7745         dip->bytes = bio->bi_iter.bi_size;
7746
7747         dio_data->submitted += bio->bi_iter.bi_size;
7748
7749         /*
7750          * Check if we are doing a partial write.  If we are, we need to split
7751          * the ordered extent to match the submitted bio.  Hang on to the
7752          * remaining unfinishable ordered_extent in dio_data so that it can be
7753          * cancelled in iomap_end to avoid a deadlock wherein faulting the
7754          * remaining pages is blocked on the outstanding ordered extent.
7755          */
7756         if (iter->flags & IOMAP_WRITE) {
7757                 int ret;
7758
7759                 ret = btrfs_extract_ordered_extent(bbio, dio_data->ordered);
7760                 if (ret) {
7761                         btrfs_finish_ordered_extent(dio_data->ordered, NULL,
7762                                                     file_offset, dip->bytes,
7763                                                     !ret);
7764                         bio->bi_status = errno_to_blk_status(ret);
7765                         iomap_dio_bio_end_io(bio);
7766                         return;
7767                 }
7768         }
7769
7770         btrfs_submit_bio(bbio, 0);
7771 }
7772
7773 static const struct iomap_ops btrfs_dio_iomap_ops = {
7774         .iomap_begin            = btrfs_dio_iomap_begin,
7775         .iomap_end              = btrfs_dio_iomap_end,
7776 };
7777
7778 static const struct iomap_dio_ops btrfs_dio_ops = {
7779         .submit_io              = btrfs_dio_submit_io,
7780         .bio_set                = &btrfs_dio_bioset,
7781 };
7782
7783 ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter, size_t done_before)
7784 {
7785         struct btrfs_dio_data data = { 0 };
7786
7787         return iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
7788                             IOMAP_DIO_PARTIAL, &data, done_before);
7789 }
7790
7791 struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter,
7792                                   size_t done_before)
7793 {
7794         struct btrfs_dio_data data = { 0 };
7795
7796         return __iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
7797                             IOMAP_DIO_PARTIAL, &data, done_before);
7798 }
7799
7800 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7801                         u64 start, u64 len)
7802 {
7803         int     ret;
7804
7805         ret = fiemap_prep(inode, fieinfo, start, &len, 0);
7806         if (ret)
7807                 return ret;
7808
7809         /*
7810          * fiemap_prep() called filemap_write_and_wait() for the whole possible
7811          * file range (0 to LLONG_MAX), but that is not enough if we have
7812          * compression enabled. The first filemap_fdatawrite_range() only kicks
7813          * in the compression of data (in an async thread) and will return
7814          * before the compression is done and writeback is started. A second
7815          * filemap_fdatawrite_range() is needed to wait for the compression to
7816          * complete and writeback to start. We also need to wait for ordered
7817          * extents to complete, because our fiemap implementation uses mainly
7818          * file extent items to list the extents, searching for extent maps
7819          * only for file ranges with holes or prealloc extents to figure out
7820          * if we have delalloc in those ranges.
7821          */
7822         if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) {
7823                 ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX);
7824                 if (ret)
7825                         return ret;
7826         }
7827
7828         return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
7829 }
7830
7831 static int btrfs_writepages(struct address_space *mapping,
7832                             struct writeback_control *wbc)
7833 {
7834         return extent_writepages(mapping, wbc);
7835 }
7836
7837 static void btrfs_readahead(struct readahead_control *rac)
7838 {
7839         extent_readahead(rac);
7840 }
7841
7842 /*
7843  * For release_folio() and invalidate_folio() we have a race window where
7844  * folio_end_writeback() is called but the subpage spinlock is not yet released.
7845  * If we continue to release/invalidate the page, we could cause use-after-free
7846  * for subpage spinlock.  So this function is to spin and wait for subpage
7847  * spinlock.
7848  */
7849 static void wait_subpage_spinlock(struct page *page)
7850 {
7851         struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
7852         struct btrfs_subpage *subpage;
7853
7854         if (!btrfs_is_subpage(fs_info, page))
7855                 return;
7856
7857         ASSERT(PagePrivate(page) && page->private);
7858         subpage = (struct btrfs_subpage *)page->private;
7859
7860         /*
7861          * This may look insane as we just acquire the spinlock and release it,
7862          * without doing anything.  But we just want to make sure no one is
7863          * still holding the subpage spinlock.
7864          * And since the page is not dirty nor writeback, and we have page
7865          * locked, the only possible way to hold a spinlock is from the endio
7866          * function to clear page writeback.
7867          *
7868          * Here we just acquire the spinlock so that all existing callers
7869          * should exit and we're safe to release/invalidate the page.
7870          */
7871         spin_lock_irq(&subpage->lock);
7872         spin_unlock_irq(&subpage->lock);
7873 }
7874
7875 static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7876 {
7877         int ret = try_release_extent_mapping(&folio->page, gfp_flags);
7878
7879         if (ret == 1) {
7880                 wait_subpage_spinlock(&folio->page);
7881                 clear_page_extent_mapped(&folio->page);
7882         }
7883         return ret;
7884 }
7885
7886 static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7887 {
7888         if (folio_test_writeback(folio) || folio_test_dirty(folio))
7889                 return false;
7890         return __btrfs_release_folio(folio, gfp_flags);
7891 }
7892
7893 #ifdef CONFIG_MIGRATION
7894 static int btrfs_migrate_folio(struct address_space *mapping,
7895                              struct folio *dst, struct folio *src,
7896                              enum migrate_mode mode)
7897 {
7898         int ret = filemap_migrate_folio(mapping, dst, src, mode);
7899
7900         if (ret != MIGRATEPAGE_SUCCESS)
7901                 return ret;
7902
7903         if (folio_test_ordered(src)) {
7904                 folio_clear_ordered(src);
7905                 folio_set_ordered(dst);
7906         }
7907
7908         return MIGRATEPAGE_SUCCESS;
7909 }
7910 #else
7911 #define btrfs_migrate_folio NULL
7912 #endif
7913
7914 static void btrfs_invalidate_folio(struct folio *folio, size_t offset,
7915                                  size_t length)
7916 {
7917         struct btrfs_inode *inode = BTRFS_I(folio->mapping->host);
7918         struct btrfs_fs_info *fs_info = inode->root->fs_info;
7919         struct extent_io_tree *tree = &inode->io_tree;
7920         struct extent_state *cached_state = NULL;
7921         u64 page_start = folio_pos(folio);
7922         u64 page_end = page_start + folio_size(folio) - 1;
7923         u64 cur;
7924         int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
7925
7926         /*
7927          * We have folio locked so no new ordered extent can be created on this
7928          * page, nor bio can be submitted for this folio.
7929          *
7930          * But already submitted bio can still be finished on this folio.
7931          * Furthermore, endio function won't skip folio which has Ordered
7932          * (Private2) already cleared, so it's possible for endio and
7933          * invalidate_folio to do the same ordered extent accounting twice
7934          * on one folio.
7935          *
7936          * So here we wait for any submitted bios to finish, so that we won't
7937          * do double ordered extent accounting on the same folio.
7938          */
7939         folio_wait_writeback(folio);
7940         wait_subpage_spinlock(&folio->page);
7941
7942         /*
7943          * For subpage case, we have call sites like
7944          * btrfs_punch_hole_lock_range() which passes range not aligned to
7945          * sectorsize.
7946          * If the range doesn't cover the full folio, we don't need to and
7947          * shouldn't clear page extent mapped, as folio->private can still
7948          * record subpage dirty bits for other part of the range.
7949          *
7950          * For cases that invalidate the full folio even the range doesn't
7951          * cover the full folio, like invalidating the last folio, we're
7952          * still safe to wait for ordered extent to finish.
7953          */
7954         if (!(offset == 0 && length == folio_size(folio))) {
7955                 btrfs_release_folio(folio, GFP_NOFS);
7956                 return;
7957         }
7958
7959         if (!inode_evicting)
7960                 lock_extent(tree, page_start, page_end, &cached_state);
7961
7962         cur = page_start;
7963         while (cur < page_end) {
7964                 struct btrfs_ordered_extent *ordered;
7965                 u64 range_end;
7966                 u32 range_len;
7967                 u32 extra_flags = 0;
7968
7969                 ordered = btrfs_lookup_first_ordered_range(inode, cur,
7970                                                            page_end + 1 - cur);
7971                 if (!ordered) {
7972                         range_end = page_end;
7973                         /*
7974                          * No ordered extent covering this range, we are safe
7975                          * to delete all extent states in the range.
7976                          */
7977                         extra_flags = EXTENT_CLEAR_ALL_BITS;
7978                         goto next;
7979                 }
7980                 if (ordered->file_offset > cur) {
7981                         /*
7982                          * There is a range between [cur, oe->file_offset) not
7983                          * covered by any ordered extent.
7984                          * We are safe to delete all extent states, and handle
7985                          * the ordered extent in the next iteration.
7986                          */
7987                         range_end = ordered->file_offset - 1;
7988                         extra_flags = EXTENT_CLEAR_ALL_BITS;
7989                         goto next;
7990                 }
7991
7992                 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
7993                                 page_end);
7994                 ASSERT(range_end + 1 - cur < U32_MAX);
7995                 range_len = range_end + 1 - cur;
7996                 if (!btrfs_page_test_ordered(fs_info, &folio->page, cur, range_len)) {
7997                         /*
7998                          * If Ordered (Private2) is cleared, it means endio has
7999                          * already been executed for the range.
8000                          * We can't delete the extent states as
8001                          * btrfs_finish_ordered_io() may still use some of them.
8002                          */
8003                         goto next;
8004                 }
8005                 btrfs_page_clear_ordered(fs_info, &folio->page, cur, range_len);
8006
8007                 /*
8008                  * IO on this page will never be started, so we need to account
8009                  * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8010                  * here, must leave that up for the ordered extent completion.
8011                  *
8012                  * This will also unlock the range for incoming
8013                  * btrfs_finish_ordered_io().
8014                  */
8015                 if (!inode_evicting)
8016                         clear_extent_bit(tree, cur, range_end,
8017                                          EXTENT_DELALLOC |
8018                                          EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8019                                          EXTENT_DEFRAG, &cached_state);
8020
8021                 spin_lock_irq(&inode->ordered_tree.lock);
8022                 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8023                 ordered->truncated_len = min(ordered->truncated_len,
8024                                              cur - ordered->file_offset);
8025                 spin_unlock_irq(&inode->ordered_tree.lock);
8026
8027                 /*
8028                  * If the ordered extent has finished, we're safe to delete all
8029                  * the extent states of the range, otherwise
8030                  * btrfs_finish_ordered_io() will get executed by endio for
8031                  * other pages, so we can't delete extent states.
8032                  */
8033                 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8034                                                    cur, range_end + 1 - cur)) {
8035                         btrfs_finish_ordered_io(ordered);
8036                         /*
8037                          * The ordered extent has finished, now we're again
8038                          * safe to delete all extent states of the range.
8039                          */
8040                         extra_flags = EXTENT_CLEAR_ALL_BITS;
8041                 }
8042 next:
8043                 if (ordered)
8044                         btrfs_put_ordered_extent(ordered);
8045                 /*
8046                  * Qgroup reserved space handler
8047                  * Sector(s) here will be either:
8048                  *
8049                  * 1) Already written to disk or bio already finished
8050                  *    Then its QGROUP_RESERVED bit in io_tree is already cleared.
8051                  *    Qgroup will be handled by its qgroup_record then.
8052                  *    btrfs_qgroup_free_data() call will do nothing here.
8053                  *
8054                  * 2) Not written to disk yet
8055                  *    Then btrfs_qgroup_free_data() call will clear the
8056                  *    QGROUP_RESERVED bit of its io_tree, and free the qgroup
8057                  *    reserved data space.
8058                  *    Since the IO will never happen for this page.
8059                  */
8060                 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
8061                 if (!inode_evicting) {
8062                         clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
8063                                  EXTENT_DELALLOC | EXTENT_UPTODATE |
8064                                  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG |
8065                                  extra_flags, &cached_state);
8066                 }
8067                 cur = range_end + 1;
8068         }
8069         /*
8070          * We have iterated through all ordered extents of the page, the page
8071          * should not have Ordered (Private2) anymore, or the above iteration
8072          * did something wrong.
8073          */
8074         ASSERT(!folio_test_ordered(folio));
8075         btrfs_page_clear_checked(fs_info, &folio->page, folio_pos(folio), folio_size(folio));
8076         if (!inode_evicting)
8077                 __btrfs_release_folio(folio, GFP_NOFS);
8078         clear_page_extent_mapped(&folio->page);
8079 }
8080
8081 /*
8082  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8083  * called from a page fault handler when a page is first dirtied. Hence we must
8084  * be careful to check for EOF conditions here. We set the page up correctly
8085  * for a written page which means we get ENOSPC checking when writing into
8086  * holes and correct delalloc and unwritten extent mapping on filesystems that
8087  * support these features.
8088  *
8089  * We are not allowed to take the i_mutex here so we have to play games to
8090  * protect against truncate races as the page could now be beyond EOF.  Because
8091  * truncate_setsize() writes the inode size before removing pages, once we have
8092  * the page lock we can determine safely if the page is beyond EOF. If it is not
8093  * beyond EOF, then the page is guaranteed safe against truncation until we
8094  * unlock the page.
8095  */
8096 vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
8097 {
8098         struct page *page = vmf->page;
8099         struct inode *inode = file_inode(vmf->vma->vm_file);
8100         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8101         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8102         struct btrfs_ordered_extent *ordered;
8103         struct extent_state *cached_state = NULL;
8104         struct extent_changeset *data_reserved = NULL;
8105         unsigned long zero_start;
8106         loff_t size;
8107         vm_fault_t ret;
8108         int ret2;
8109         int reserved = 0;
8110         u64 reserved_space;
8111         u64 page_start;
8112         u64 page_end;
8113         u64 end;
8114
8115         reserved_space = PAGE_SIZE;
8116
8117         sb_start_pagefault(inode->i_sb);
8118         page_start = page_offset(page);
8119         page_end = page_start + PAGE_SIZE - 1;
8120         end = page_end;
8121
8122         /*
8123          * Reserving delalloc space after obtaining the page lock can lead to
8124          * deadlock. For example, if a dirty page is locked by this function
8125          * and the call to btrfs_delalloc_reserve_space() ends up triggering
8126          * dirty page write out, then the btrfs_writepages() function could
8127          * end up waiting indefinitely to get a lock on the page currently
8128          * being processed by btrfs_page_mkwrite() function.
8129          */
8130         ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8131                                             page_start, reserved_space);
8132         if (!ret2) {
8133                 ret2 = file_update_time(vmf->vma->vm_file);
8134                 reserved = 1;
8135         }
8136         if (ret2) {
8137                 ret = vmf_error(ret2);
8138                 if (reserved)
8139                         goto out;
8140                 goto out_noreserve;
8141         }
8142
8143         ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
8144 again:
8145         down_read(&BTRFS_I(inode)->i_mmap_lock);
8146         lock_page(page);
8147         size = i_size_read(inode);
8148
8149         if ((page->mapping != inode->i_mapping) ||
8150             (page_start >= size)) {
8151                 /* page got truncated out from underneath us */
8152                 goto out_unlock;
8153         }
8154         wait_on_page_writeback(page);
8155
8156         lock_extent(io_tree, page_start, page_end, &cached_state);
8157         ret2 = set_page_extent_mapped(page);
8158         if (ret2 < 0) {
8159                 ret = vmf_error(ret2);
8160                 unlock_extent(io_tree, page_start, page_end, &cached_state);
8161                 goto out_unlock;
8162         }
8163
8164         /*
8165          * we can't set the delalloc bits if there are pending ordered
8166          * extents.  Drop our locks and wait for them to finish
8167          */
8168         ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8169                         PAGE_SIZE);
8170         if (ordered) {
8171                 unlock_extent(io_tree, page_start, page_end, &cached_state);
8172                 unlock_page(page);
8173                 up_read(&BTRFS_I(inode)->i_mmap_lock);
8174                 btrfs_start_ordered_extent(ordered);
8175                 btrfs_put_ordered_extent(ordered);
8176                 goto again;
8177         }
8178
8179         if (page->index == ((size - 1) >> PAGE_SHIFT)) {
8180                 reserved_space = round_up(size - page_start,
8181                                           fs_info->sectorsize);
8182                 if (reserved_space < PAGE_SIZE) {
8183                         end = page_start + reserved_space - 1;
8184                         btrfs_delalloc_release_space(BTRFS_I(inode),
8185                                         data_reserved, page_start,
8186                                         PAGE_SIZE - reserved_space, true);
8187                 }
8188         }
8189
8190         /*
8191          * page_mkwrite gets called when the page is firstly dirtied after it's
8192          * faulted in, but write(2) could also dirty a page and set delalloc
8193          * bits, thus in this case for space account reason, we still need to
8194          * clear any delalloc bits within this page range since we have to
8195          * reserve data&meta space before lock_page() (see above comments).
8196          */
8197         clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
8198                           EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8199                           EXTENT_DEFRAG, &cached_state);
8200
8201         ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
8202                                         &cached_state);
8203         if (ret2) {
8204                 unlock_extent(io_tree, page_start, page_end, &cached_state);
8205                 ret = VM_FAULT_SIGBUS;
8206                 goto out_unlock;
8207         }
8208
8209         /* page is wholly or partially inside EOF */
8210         if (page_start + PAGE_SIZE > size)
8211                 zero_start = offset_in_page(size);
8212         else
8213                 zero_start = PAGE_SIZE;
8214
8215         if (zero_start != PAGE_SIZE)
8216                 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
8217
8218         btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);
8219         btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8220         btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
8221
8222         btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
8223
8224         unlock_extent(io_tree, page_start, page_end, &cached_state);
8225         up_read(&BTRFS_I(inode)->i_mmap_lock);
8226
8227         btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8228         sb_end_pagefault(inode->i_sb);
8229         extent_changeset_free(data_reserved);
8230         return VM_FAULT_LOCKED;
8231
8232 out_unlock:
8233         unlock_page(page);
8234         up_read(&BTRFS_I(inode)->i_mmap_lock);
8235 out:
8236         btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8237         btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
8238                                      reserved_space, (ret != 0));
8239 out_noreserve:
8240         sb_end_pagefault(inode->i_sb);
8241         extent_changeset_free(data_reserved);
8242         return ret;
8243 }
8244
8245 static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback)
8246 {
8247         struct btrfs_truncate_control control = {
8248                 .inode = inode,
8249                 .ino = btrfs_ino(inode),
8250                 .min_type = BTRFS_EXTENT_DATA_KEY,
8251                 .clear_extent_range = true,
8252         };
8253         struct btrfs_root *root = inode->root;
8254         struct btrfs_fs_info *fs_info = root->fs_info;
8255         struct btrfs_block_rsv *rsv;
8256         int ret;
8257         struct btrfs_trans_handle *trans;
8258         u64 mask = fs_info->sectorsize - 1;
8259         const u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
8260
8261         if (!skip_writeback) {
8262                 ret = btrfs_wait_ordered_range(&inode->vfs_inode,
8263                                                inode->vfs_inode.i_size & (~mask),
8264                                                (u64)-1);
8265                 if (ret)
8266                         return ret;
8267         }
8268
8269         /*
8270          * Yes ladies and gentlemen, this is indeed ugly.  We have a couple of
8271          * things going on here:
8272          *
8273          * 1) We need to reserve space to update our inode.
8274          *
8275          * 2) We need to have something to cache all the space that is going to
8276          * be free'd up by the truncate operation, but also have some slack
8277          * space reserved in case it uses space during the truncate (thank you
8278          * very much snapshotting).
8279          *
8280          * And we need these to be separate.  The fact is we can use a lot of
8281          * space doing the truncate, and we have no earthly idea how much space
8282          * we will use, so we need the truncate reservation to be separate so it
8283          * doesn't end up using space reserved for updating the inode.  We also
8284          * need to be able to stop the transaction and start a new one, which
8285          * means we need to be able to update the inode several times, and we
8286          * have no idea of knowing how many times that will be, so we can't just
8287          * reserve 1 item for the entirety of the operation, so that has to be
8288          * done separately as well.
8289          *
8290          * So that leaves us with
8291          *
8292          * 1) rsv - for the truncate reservation, which we will steal from the
8293          * transaction reservation.
8294          * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
8295          * updating the inode.
8296          */
8297         rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
8298         if (!rsv)
8299                 return -ENOMEM;
8300         rsv->size = min_size;
8301         rsv->failfast = true;
8302
8303         /*
8304          * 1 for the truncate slack space
8305          * 1 for updating the inode.
8306          */
8307         trans = btrfs_start_transaction(root, 2);
8308         if (IS_ERR(trans)) {
8309                 ret = PTR_ERR(trans);
8310                 goto out;
8311         }
8312
8313         /* Migrate the slack space for the truncate to our reserve */
8314         ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
8315                                       min_size, false);
8316         /*
8317          * We have reserved 2 metadata units when we started the transaction and
8318          * min_size matches 1 unit, so this should never fail, but if it does,
8319          * it's not critical we just fail truncation.
8320          */
8321         if (WARN_ON(ret)) {
8322                 btrfs_end_transaction(trans);
8323                 goto out;
8324         }
8325
8326         trans->block_rsv = rsv;
8327
8328         while (1) {
8329                 struct extent_state *cached_state = NULL;
8330                 const u64 new_size = inode->vfs_inode.i_size;
8331                 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
8332
8333                 control.new_size = new_size;
8334                 lock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state);
8335                 /*
8336                  * We want to drop from the next block forward in case this new
8337                  * size is not block aligned since we will be keeping the last
8338                  * block of the extent just the way it is.
8339                  */
8340                 btrfs_drop_extent_map_range(inode,
8341                                             ALIGN(new_size, fs_info->sectorsize),
8342                                             (u64)-1, false);
8343
8344                 ret = btrfs_truncate_inode_items(trans, root, &control);
8345
8346                 inode_sub_bytes(&inode->vfs_inode, control.sub_bytes);
8347                 btrfs_inode_safe_disk_i_size_write(inode, control.last_size);
8348
8349                 unlock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state);
8350
8351                 trans->block_rsv = &fs_info->trans_block_rsv;
8352                 if (ret != -ENOSPC && ret != -EAGAIN)
8353                         break;
8354
8355                 ret = btrfs_update_inode(trans, root, inode);
8356                 if (ret)
8357                         break;
8358
8359                 btrfs_end_transaction(trans);
8360                 btrfs_btree_balance_dirty(fs_info);
8361
8362                 trans = btrfs_start_transaction(root, 2);
8363                 if (IS_ERR(trans)) {
8364                         ret = PTR_ERR(trans);
8365                         trans = NULL;
8366                         break;
8367                 }
8368
8369                 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
8370                 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
8371                                               rsv, min_size, false);
8372                 /*
8373                  * We have reserved 2 metadata units when we started the
8374                  * transaction and min_size matches 1 unit, so this should never
8375                  * fail, but if it does, it's not critical we just fail truncation.
8376                  */
8377                 if (WARN_ON(ret))
8378                         break;
8379
8380                 trans->block_rsv = rsv;
8381         }
8382
8383         /*
8384          * We can't call btrfs_truncate_block inside a trans handle as we could
8385          * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we
8386          * know we've truncated everything except the last little bit, and can
8387          * do btrfs_truncate_block and then update the disk_i_size.
8388          */
8389         if (ret == BTRFS_NEED_TRUNCATE_BLOCK) {
8390                 btrfs_end_transaction(trans);
8391                 btrfs_btree_balance_dirty(fs_info);
8392
8393                 ret = btrfs_truncate_block(inode, inode->vfs_inode.i_size, 0, 0);
8394                 if (ret)
8395                         goto out;
8396                 trans = btrfs_start_transaction(root, 1);
8397                 if (IS_ERR(trans)) {
8398                         ret = PTR_ERR(trans);
8399                         goto out;
8400                 }
8401                 btrfs_inode_safe_disk_i_size_write(inode, 0);
8402         }
8403
8404         if (trans) {
8405                 int ret2;
8406
8407                 trans->block_rsv = &fs_info->trans_block_rsv;
8408                 ret2 = btrfs_update_inode(trans, root, inode);
8409                 if (ret2 && !ret)
8410                         ret = ret2;
8411
8412                 ret2 = btrfs_end_transaction(trans);
8413                 if (ret2 && !ret)
8414                         ret = ret2;
8415                 btrfs_btree_balance_dirty(fs_info);
8416         }
8417 out:
8418         btrfs_free_block_rsv(fs_info, rsv);
8419         /*
8420          * So if we truncate and then write and fsync we normally would just
8421          * write the extents that changed, which is a problem if we need to
8422          * first truncate that entire inode.  So set this flag so we write out
8423          * all of the extents in the inode to the sync log so we're completely
8424          * safe.
8425          *
8426          * If no extents were dropped or trimmed we don't need to force the next
8427          * fsync to truncate all the inode's items from the log and re-log them
8428          * all. This means the truncate operation did not change the file size,
8429          * or changed it to a smaller size but there was only an implicit hole
8430          * between the old i_size and the new i_size, and there were no prealloc
8431          * extents beyond i_size to drop.
8432          */
8433         if (control.extents_found > 0)
8434                 btrfs_set_inode_full_sync(inode);
8435
8436         return ret;
8437 }
8438
8439 struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap,
8440                                      struct inode *dir)
8441 {
8442         struct inode *inode;
8443
8444         inode = new_inode(dir->i_sb);
8445         if (inode) {
8446                 /*
8447                  * Subvolumes don't inherit the sgid bit or the parent's gid if
8448                  * the parent's sgid bit is set. This is probably a bug.
8449                  */
8450                 inode_init_owner(idmap, inode, NULL,
8451                                  S_IFDIR | (~current_umask() & S_IRWXUGO));
8452                 inode->i_op = &btrfs_dir_inode_operations;
8453                 inode->i_fop = &btrfs_dir_file_operations;
8454         }
8455         return inode;
8456 }
8457
8458 struct inode *btrfs_alloc_inode(struct super_block *sb)
8459 {
8460         struct btrfs_fs_info *fs_info = btrfs_sb(sb);
8461         struct btrfs_inode *ei;
8462         struct inode *inode;
8463
8464         ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL);
8465         if (!ei)
8466                 return NULL;
8467
8468         ei->root = NULL;
8469         ei->generation = 0;
8470         ei->last_trans = 0;
8471         ei->last_sub_trans = 0;
8472         ei->logged_trans = 0;
8473         ei->delalloc_bytes = 0;
8474         ei->new_delalloc_bytes = 0;
8475         ei->defrag_bytes = 0;
8476         ei->disk_i_size = 0;
8477         ei->flags = 0;
8478         ei->ro_flags = 0;
8479         ei->csum_bytes = 0;
8480         ei->index_cnt = (u64)-1;
8481         ei->dir_index = 0;
8482         ei->last_unlink_trans = 0;
8483         ei->last_reflink_trans = 0;
8484         ei->last_log_commit = 0;
8485
8486         spin_lock_init(&ei->lock);
8487         ei->outstanding_extents = 0;
8488         if (sb->s_magic != BTRFS_TEST_MAGIC)
8489                 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8490                                               BTRFS_BLOCK_RSV_DELALLOC);
8491         ei->runtime_flags = 0;
8492         ei->prop_compress = BTRFS_COMPRESS_NONE;
8493         ei->defrag_compress = BTRFS_COMPRESS_NONE;
8494
8495         ei->delayed_node = NULL;
8496
8497         ei->i_otime.tv_sec = 0;
8498         ei->i_otime.tv_nsec = 0;
8499
8500         inode = &ei->vfs_inode;
8501         extent_map_tree_init(&ei->extent_tree);
8502         extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO);
8503         ei->io_tree.inode = ei;
8504         extent_io_tree_init(fs_info, &ei->file_extent_tree,
8505                             IO_TREE_INODE_FILE_EXTENT);
8506         mutex_init(&ei->log_mutex);
8507         btrfs_ordered_inode_tree_init(&ei->ordered_tree);
8508         INIT_LIST_HEAD(&ei->delalloc_inodes);
8509         INIT_LIST_HEAD(&ei->delayed_iput);
8510         RB_CLEAR_NODE(&ei->rb_node);
8511         init_rwsem(&ei->i_mmap_lock);
8512
8513         return inode;
8514 }
8515
8516 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8517 void btrfs_test_destroy_inode(struct inode *inode)
8518 {
8519         btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
8520         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8521 }
8522 #endif
8523
8524 void btrfs_free_inode(struct inode *inode)
8525 {
8526         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8527 }
8528
8529 void btrfs_destroy_inode(struct inode *vfs_inode)
8530 {
8531         struct btrfs_ordered_extent *ordered;
8532         struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8533         struct btrfs_root *root = inode->root;
8534         bool freespace_inode;
8535
8536         WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8537         WARN_ON(vfs_inode->i_data.nrpages);
8538         WARN_ON(inode->block_rsv.reserved);
8539         WARN_ON(inode->block_rsv.size);
8540         WARN_ON(inode->outstanding_extents);
8541         if (!S_ISDIR(vfs_inode->i_mode)) {
8542                 WARN_ON(inode->delalloc_bytes);
8543                 WARN_ON(inode->new_delalloc_bytes);
8544         }
8545         WARN_ON(inode->csum_bytes);
8546         WARN_ON(inode->defrag_bytes);
8547
8548         /*
8549          * This can happen where we create an inode, but somebody else also
8550          * created the same inode and we need to destroy the one we already
8551          * created.
8552          */
8553         if (!root)
8554                 return;
8555
8556         /*
8557          * If this is a free space inode do not take the ordered extents lockdep
8558          * map.
8559          */
8560         freespace_inode = btrfs_is_free_space_inode(inode);
8561
8562         while (1) {
8563                 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8564                 if (!ordered)
8565                         break;
8566                 else {
8567                         btrfs_err(root->fs_info,
8568                                   "found ordered extent %llu %llu on inode cleanup",
8569                                   ordered->file_offset, ordered->num_bytes);
8570
8571                         if (!freespace_inode)
8572                                 btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent);
8573
8574                         btrfs_remove_ordered_extent(inode, ordered);
8575                         btrfs_put_ordered_extent(ordered);
8576                         btrfs_put_ordered_extent(ordered);
8577                 }
8578         }
8579         btrfs_qgroup_check_reserved_leak(inode);
8580         inode_tree_del(inode);
8581         btrfs_drop_extent_map_range(inode, 0, (u64)-1, false);
8582         btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8583         btrfs_put_root(inode->root);
8584 }
8585
8586 int btrfs_drop_inode(struct inode *inode)
8587 {
8588         struct btrfs_root *root = BTRFS_I(inode)->root;
8589
8590         if (root == NULL)
8591                 return 1;
8592
8593         /* the snap/subvol tree is on deleting */
8594         if (btrfs_root_refs(&root->root_item) == 0)
8595                 return 1;
8596         else
8597                 return generic_drop_inode(inode);
8598 }
8599
8600 static void init_once(void *foo)
8601 {
8602         struct btrfs_inode *ei = foo;
8603
8604         inode_init_once(&ei->vfs_inode);
8605 }
8606
8607 void __cold btrfs_destroy_cachep(void)
8608 {
8609         /*
8610          * Make sure all delayed rcu free inodes are flushed before we
8611          * destroy cache.
8612          */
8613         rcu_barrier();
8614         bioset_exit(&btrfs_dio_bioset);
8615         kmem_cache_destroy(btrfs_inode_cachep);
8616 }
8617
8618 int __init btrfs_init_cachep(void)
8619 {
8620         btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
8621                         sizeof(struct btrfs_inode), 0,
8622                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8623                         init_once);
8624         if (!btrfs_inode_cachep)
8625                 goto fail;
8626
8627         if (bioset_init(&btrfs_dio_bioset, BIO_POOL_SIZE,
8628                         offsetof(struct btrfs_dio_private, bbio.bio),
8629                         BIOSET_NEED_BVECS))
8630                 goto fail;
8631
8632         return 0;
8633 fail:
8634         btrfs_destroy_cachep();
8635         return -ENOMEM;
8636 }
8637
8638 static int btrfs_getattr(struct mnt_idmap *idmap,
8639                          const struct path *path, struct kstat *stat,
8640                          u32 request_mask, unsigned int flags)
8641 {
8642         u64 delalloc_bytes;
8643         u64 inode_bytes;
8644         struct inode *inode = d_inode(path->dentry);
8645         u32 blocksize = inode->i_sb->s_blocksize;
8646         u32 bi_flags = BTRFS_I(inode)->flags;
8647         u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
8648
8649         stat->result_mask |= STATX_BTIME;
8650         stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8651         stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8652         if (bi_flags & BTRFS_INODE_APPEND)
8653                 stat->attributes |= STATX_ATTR_APPEND;
8654         if (bi_flags & BTRFS_INODE_COMPRESS)
8655                 stat->attributes |= STATX_ATTR_COMPRESSED;
8656         if (bi_flags & BTRFS_INODE_IMMUTABLE)
8657                 stat->attributes |= STATX_ATTR_IMMUTABLE;
8658         if (bi_flags & BTRFS_INODE_NODUMP)
8659                 stat->attributes |= STATX_ATTR_NODUMP;
8660         if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
8661                 stat->attributes |= STATX_ATTR_VERITY;
8662
8663         stat->attributes_mask |= (STATX_ATTR_APPEND |
8664                                   STATX_ATTR_COMPRESSED |
8665                                   STATX_ATTR_IMMUTABLE |
8666                                   STATX_ATTR_NODUMP);
8667
8668         generic_fillattr(idmap, inode, stat);
8669         stat->dev = BTRFS_I(inode)->root->anon_dev;
8670
8671         spin_lock(&BTRFS_I(inode)->lock);
8672         delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
8673         inode_bytes = inode_get_bytes(inode);
8674         spin_unlock(&BTRFS_I(inode)->lock);
8675         stat->blocks = (ALIGN(inode_bytes, blocksize) +
8676                         ALIGN(delalloc_bytes, blocksize)) >> SECTOR_SHIFT;
8677         return 0;
8678 }
8679
8680 static int btrfs_rename_exchange(struct inode *old_dir,
8681                               struct dentry *old_dentry,
8682                               struct inode *new_dir,
8683                               struct dentry *new_dentry)
8684 {
8685         struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
8686         struct btrfs_trans_handle *trans;
8687         unsigned int trans_num_items;
8688         struct btrfs_root *root = BTRFS_I(old_dir)->root;
8689         struct btrfs_root *dest = BTRFS_I(new_dir)->root;
8690         struct inode *new_inode = new_dentry->d_inode;
8691         struct inode *old_inode = old_dentry->d_inode;
8692         struct timespec64 ctime = current_time(old_inode);
8693         struct btrfs_rename_ctx old_rename_ctx;
8694         struct btrfs_rename_ctx new_rename_ctx;
8695         u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8696         u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
8697         u64 old_idx = 0;
8698         u64 new_idx = 0;
8699         int ret;
8700         int ret2;
8701         bool need_abort = false;
8702         struct fscrypt_name old_fname, new_fname;
8703         struct fscrypt_str *old_name, *new_name;
8704
8705         /*
8706          * For non-subvolumes allow exchange only within one subvolume, in the
8707          * same inode namespace. Two subvolumes (represented as directory) can
8708          * be exchanged as they're a logical link and have a fixed inode number.
8709          */
8710         if (root != dest &&
8711             (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
8712              new_ino != BTRFS_FIRST_FREE_OBJECTID))
8713                 return -EXDEV;
8714
8715         ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname);
8716         if (ret)
8717                 return ret;
8718
8719         ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname);
8720         if (ret) {
8721                 fscrypt_free_filename(&old_fname);
8722                 return ret;
8723         }
8724
8725         old_name = &old_fname.disk_name;
8726         new_name = &new_fname.disk_name;
8727
8728         /* close the race window with snapshot create/destroy ioctl */
8729         if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8730             new_ino == BTRFS_FIRST_FREE_OBJECTID)
8731                 down_read(&fs_info->subvol_sem);
8732
8733         /*
8734          * For each inode:
8735          * 1 to remove old dir item
8736          * 1 to remove old dir index
8737          * 1 to add new dir item
8738          * 1 to add new dir index
8739          * 1 to update parent inode
8740          *
8741          * If the parents are the same, we only need to account for one
8742          */
8743         trans_num_items = (old_dir == new_dir ? 9 : 10);
8744         if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8745                 /*
8746                  * 1 to remove old root ref
8747                  * 1 to remove old root backref
8748                  * 1 to add new root ref
8749                  * 1 to add new root backref
8750                  */
8751                 trans_num_items += 4;
8752         } else {
8753                 /*
8754                  * 1 to update inode item
8755                  * 1 to remove old inode ref
8756                  * 1 to add new inode ref
8757                  */
8758                 trans_num_items += 3;
8759         }
8760         if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
8761                 trans_num_items += 4;
8762         else
8763                 trans_num_items += 3;
8764         trans = btrfs_start_transaction(root, trans_num_items);
8765         if (IS_ERR(trans)) {
8766                 ret = PTR_ERR(trans);
8767                 goto out_notrans;
8768         }
8769
8770         if (dest != root) {
8771                 ret = btrfs_record_root_in_trans(trans, dest);
8772                 if (ret)
8773                         goto out_fail;
8774         }
8775
8776         /*
8777          * We need to find a free sequence number both in the source and
8778          * in the destination directory for the exchange.
8779          */
8780         ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
8781         if (ret)
8782                 goto out_fail;
8783         ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
8784         if (ret)
8785                 goto out_fail;
8786
8787         BTRFS_I(old_inode)->dir_index = 0ULL;
8788         BTRFS_I(new_inode)->dir_index = 0ULL;
8789
8790         /* Reference for the source. */
8791         if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8792                 /* force full log commit if subvolume involved. */
8793                 btrfs_set_log_full_commit(trans);
8794         } else {
8795                 ret = btrfs_insert_inode_ref(trans, dest, new_name, old_ino,
8796                                              btrfs_ino(BTRFS_I(new_dir)),
8797                                              old_idx);
8798                 if (ret)
8799                         goto out_fail;
8800                 need_abort = true;
8801         }
8802
8803         /* And now for the dest. */
8804         if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8805                 /* force full log commit if subvolume involved. */
8806                 btrfs_set_log_full_commit(trans);
8807         } else {
8808                 ret = btrfs_insert_inode_ref(trans, root, old_name, new_ino,
8809                                              btrfs_ino(BTRFS_I(old_dir)),
8810                                              new_idx);
8811                 if (ret) {
8812                         if (need_abort)
8813                                 btrfs_abort_transaction(trans, ret);
8814                         goto out_fail;
8815                 }
8816         }
8817
8818         /* Update inode version and ctime/mtime. */
8819         inode_inc_iversion(old_dir);
8820         inode_inc_iversion(new_dir);
8821         inode_inc_iversion(old_inode);
8822         inode_inc_iversion(new_inode);
8823         old_dir->i_mtime = ctime;
8824         old_dir->i_ctime = ctime;
8825         new_dir->i_mtime = ctime;
8826         new_dir->i_ctime = ctime;
8827         old_inode->i_ctime = ctime;
8828         new_inode->i_ctime = ctime;
8829
8830         if (old_dentry->d_parent != new_dentry->d_parent) {
8831                 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8832                                         BTRFS_I(old_inode), true);
8833                 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8834                                         BTRFS_I(new_inode), true);
8835         }
8836
8837         /* src is a subvolume */
8838         if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8839                 ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry);
8840         } else { /* src is an inode */
8841                 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
8842                                            BTRFS_I(old_dentry->d_inode),
8843                                            old_name, &old_rename_ctx);
8844                 if (!ret)
8845                         ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
8846         }
8847         if (ret) {
8848                 btrfs_abort_transaction(trans, ret);
8849                 goto out_fail;
8850         }
8851
8852         /* dest is a subvolume */
8853         if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8854                 ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry);
8855         } else { /* dest is an inode */
8856                 ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir),
8857                                            BTRFS_I(new_dentry->d_inode),
8858                                            new_name, &new_rename_ctx);
8859                 if (!ret)
8860                         ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
8861         }
8862         if (ret) {
8863                 btrfs_abort_transaction(trans, ret);
8864                 goto out_fail;
8865         }
8866
8867         ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
8868                              new_name, 0, old_idx);
8869         if (ret) {
8870                 btrfs_abort_transaction(trans, ret);
8871                 goto out_fail;
8872         }
8873
8874         ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
8875                              old_name, 0, new_idx);
8876         if (ret) {
8877                 btrfs_abort_transaction(trans, ret);
8878                 goto out_fail;
8879         }
8880
8881         if (old_inode->i_nlink == 1)
8882                 BTRFS_I(old_inode)->dir_index = old_idx;
8883         if (new_inode->i_nlink == 1)
8884                 BTRFS_I(new_inode)->dir_index = new_idx;
8885
8886         /*
8887          * Now pin the logs of the roots. We do it to ensure that no other task
8888          * can sync the logs while we are in progress with the rename, because
8889          * that could result in an inconsistency in case any of the inodes that
8890          * are part of this rename operation were logged before.
8891          */
8892         if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
8893                 btrfs_pin_log_trans(root);
8894         if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
8895                 btrfs_pin_log_trans(dest);
8896
8897         /* Do the log updates for all inodes. */
8898         if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
8899                 btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir),
8900                                    old_rename_ctx.index, new_dentry->d_parent);
8901         if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
8902                 btrfs_log_new_name(trans, new_dentry, BTRFS_I(new_dir),
8903                                    new_rename_ctx.index, old_dentry->d_parent);
8904
8905         /* Now unpin the logs. */
8906         if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
8907                 btrfs_end_log_trans(root);
8908         if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
8909                 btrfs_end_log_trans(dest);
8910 out_fail:
8911         ret2 = btrfs_end_transaction(trans);
8912         ret = ret ? ret : ret2;
8913 out_notrans:
8914         if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8915             old_ino == BTRFS_FIRST_FREE_OBJECTID)
8916                 up_read(&fs_info->subvol_sem);
8917
8918         fscrypt_free_filename(&new_fname);
8919         fscrypt_free_filename(&old_fname);
8920         return ret;
8921 }
8922
8923 static struct inode *new_whiteout_inode(struct mnt_idmap *idmap,
8924                                         struct inode *dir)
8925 {
8926         struct inode *inode;
8927
8928         inode = new_inode(dir->i_sb);
8929         if (inode) {
8930                 inode_init_owner(idmap, inode, dir,
8931                                  S_IFCHR | WHITEOUT_MODE);
8932                 inode->i_op = &btrfs_special_inode_operations;
8933                 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
8934         }
8935         return inode;
8936 }
8937
8938 static int btrfs_rename(struct mnt_idmap *idmap,
8939                         struct inode *old_dir, struct dentry *old_dentry,
8940                         struct inode *new_dir, struct dentry *new_dentry,
8941                         unsigned int flags)
8942 {
8943         struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
8944         struct btrfs_new_inode_args whiteout_args = {
8945                 .dir = old_dir,
8946                 .dentry = old_dentry,
8947         };
8948         struct btrfs_trans_handle *trans;
8949         unsigned int trans_num_items;
8950         struct btrfs_root *root = BTRFS_I(old_dir)->root;
8951         struct btrfs_root *dest = BTRFS_I(new_dir)->root;
8952         struct inode *new_inode = d_inode(new_dentry);
8953         struct inode *old_inode = d_inode(old_dentry);
8954         struct btrfs_rename_ctx rename_ctx;
8955         u64 index = 0;
8956         int ret;
8957         int ret2;
8958         u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8959         struct fscrypt_name old_fname, new_fname;
8960
8961         if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
8962                 return -EPERM;
8963
8964         /* we only allow rename subvolume link between subvolumes */
8965         if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8966                 return -EXDEV;
8967
8968         if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8969             (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
8970                 return -ENOTEMPTY;
8971
8972         if (S_ISDIR(old_inode->i_mode) && new_inode &&
8973             new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
8974                 return -ENOTEMPTY;
8975
8976         ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname);
8977         if (ret)
8978                 return ret;
8979
8980         ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname);
8981         if (ret) {
8982                 fscrypt_free_filename(&old_fname);
8983                 return ret;
8984         }
8985
8986         /* check for collisions, even if the  name isn't there */
8987         ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, &new_fname.disk_name);
8988         if (ret) {
8989                 if (ret == -EEXIST) {
8990                         /* we shouldn't get
8991                          * eexist without a new_inode */
8992                         if (WARN_ON(!new_inode)) {
8993                                 goto out_fscrypt_names;
8994                         }
8995                 } else {
8996                         /* maybe -EOVERFLOW */
8997                         goto out_fscrypt_names;
8998                 }
8999         }
9000         ret = 0;
9001
9002         /*
9003          * we're using rename to replace one file with another.  Start IO on it
9004          * now so  we don't add too much work to the end of the transaction
9005          */
9006         if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
9007                 filemap_flush(old_inode->i_mapping);
9008
9009         if (flags & RENAME_WHITEOUT) {
9010                 whiteout_args.inode = new_whiteout_inode(idmap, old_dir);
9011                 if (!whiteout_args.inode) {
9012                         ret = -ENOMEM;
9013                         goto out_fscrypt_names;
9014                 }
9015                 ret = btrfs_new_inode_prepare(&whiteout_args, &trans_num_items);
9016                 if (ret)
9017                         goto out_whiteout_inode;
9018         } else {
9019                 /* 1 to update the old parent inode. */
9020                 trans_num_items = 1;
9021         }
9022
9023         if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9024                 /* Close the race window with snapshot create/destroy ioctl */
9025                 down_read(&fs_info->subvol_sem);
9026                 /*
9027                  * 1 to remove old root ref
9028                  * 1 to remove old root backref
9029                  * 1 to add new root ref
9030                  * 1 to add new root backref
9031                  */
9032                 trans_num_items += 4;
9033         } else {
9034                 /*
9035                  * 1 to update inode
9036                  * 1 to remove old inode ref
9037                  * 1 to add new inode ref
9038                  */
9039                 trans_num_items += 3;
9040         }
9041         /*
9042          * 1 to remove old dir item
9043          * 1 to remove old dir index
9044          * 1 to add new dir item
9045          * 1 to add new dir index
9046          */
9047         trans_num_items += 4;
9048         /* 1 to update new parent inode if it's not the same as the old parent */
9049         if (new_dir != old_dir)
9050                 trans_num_items++;
9051         if (new_inode) {
9052                 /*
9053                  * 1 to update inode
9054                  * 1 to remove inode ref
9055                  * 1 to remove dir item
9056                  * 1 to remove dir index
9057                  * 1 to possibly add orphan item
9058                  */
9059                 trans_num_items += 5;
9060         }
9061         trans = btrfs_start_transaction(root, trans_num_items);
9062         if (IS_ERR(trans)) {
9063                 ret = PTR_ERR(trans);
9064                 goto out_notrans;
9065         }
9066
9067         if (dest != root) {
9068                 ret = btrfs_record_root_in_trans(trans, dest);
9069                 if (ret)
9070                         goto out_fail;
9071         }
9072
9073         ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
9074         if (ret)
9075                 goto out_fail;
9076
9077         BTRFS_I(old_inode)->dir_index = 0ULL;
9078         if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9079                 /* force full log commit if subvolume involved. */
9080                 btrfs_set_log_full_commit(trans);
9081         } else {
9082                 ret = btrfs_insert_inode_ref(trans, dest, &new_fname.disk_name,
9083                                              old_ino, btrfs_ino(BTRFS_I(new_dir)),
9084                                              index);
9085                 if (ret)
9086                         goto out_fail;
9087         }
9088
9089         inode_inc_iversion(old_dir);
9090         inode_inc_iversion(new_dir);
9091         inode_inc_iversion(old_inode);
9092         old_dir->i_mtime = current_time(old_dir);
9093         old_dir->i_ctime = old_dir->i_mtime;
9094         new_dir->i_mtime = old_dir->i_mtime;
9095         new_dir->i_ctime = old_dir->i_mtime;
9096         old_inode->i_ctime = old_dir->i_mtime;
9097
9098         if (old_dentry->d_parent != new_dentry->d_parent)
9099                 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9100                                         BTRFS_I(old_inode), true);
9101
9102         if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9103                 ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry);
9104         } else {
9105                 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
9106                                            BTRFS_I(d_inode(old_dentry)),
9107                                            &old_fname.disk_name, &rename_ctx);
9108                 if (!ret)
9109                         ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
9110         }
9111         if (ret) {
9112                 btrfs_abort_transaction(trans, ret);
9113                 goto out_fail;
9114         }
9115
9116         if (new_inode) {
9117                 inode_inc_iversion(new_inode);
9118                 new_inode->i_ctime = current_time(new_inode);
9119                 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
9120                              BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9121                         ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry);
9122                         BUG_ON(new_inode->i_nlink == 0);
9123                 } else {
9124                         ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir),
9125                                                  BTRFS_I(d_inode(new_dentry)),
9126                                                  &new_fname.disk_name);
9127                 }
9128                 if (!ret && new_inode->i_nlink == 0)
9129                         ret = btrfs_orphan_add(trans,
9130                                         BTRFS_I(d_inode(new_dentry)));
9131                 if (ret) {
9132                         btrfs_abort_transaction(trans, ret);
9133                         goto out_fail;
9134                 }
9135         }
9136
9137         ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
9138                              &new_fname.disk_name, 0, index);
9139         if (ret) {
9140                 btrfs_abort_transaction(trans, ret);
9141                 goto out_fail;
9142         }
9143
9144         if (old_inode->i_nlink == 1)
9145                 BTRFS_I(old_inode)->dir_index = index;
9146
9147         if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
9148                 btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir),
9149                                    rename_ctx.index, new_dentry->d_parent);
9150
9151         if (flags & RENAME_WHITEOUT) {
9152                 ret = btrfs_create_new_inode(trans, &whiteout_args);
9153                 if (ret) {
9154                         btrfs_abort_transaction(trans, ret);
9155                         goto out_fail;
9156                 } else {
9157                         unlock_new_inode(whiteout_args.inode);
9158                         iput(whiteout_args.inode);
9159                         whiteout_args.inode = NULL;
9160                 }
9161         }
9162 out_fail:
9163         ret2 = btrfs_end_transaction(trans);
9164         ret = ret ? ret : ret2;
9165 out_notrans:
9166         if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9167                 up_read(&fs_info->subvol_sem);
9168         if (flags & RENAME_WHITEOUT)
9169                 btrfs_new_inode_args_destroy(&whiteout_args);
9170 out_whiteout_inode:
9171         if (flags & RENAME_WHITEOUT)
9172                 iput(whiteout_args.inode);
9173 out_fscrypt_names:
9174         fscrypt_free_filename(&old_fname);
9175         fscrypt_free_filename(&new_fname);
9176         return ret;
9177 }
9178
9179 static int btrfs_rename2(struct mnt_idmap *idmap, struct inode *old_dir,
9180                          struct dentry *old_dentry, struct inode *new_dir,
9181                          struct dentry *new_dentry, unsigned int flags)
9182 {
9183         int ret;
9184
9185         if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
9186                 return -EINVAL;
9187
9188         if (flags & RENAME_EXCHANGE)
9189                 ret = btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9190                                             new_dentry);
9191         else
9192                 ret = btrfs_rename(idmap, old_dir, old_dentry, new_dir,
9193                                    new_dentry, flags);
9194
9195         btrfs_btree_balance_dirty(BTRFS_I(new_dir)->root->fs_info);
9196
9197         return ret;
9198 }
9199
9200 struct btrfs_delalloc_work {
9201         struct inode *inode;
9202         struct completion completion;
9203         struct list_head list;
9204         struct btrfs_work work;
9205 };
9206
9207 static void btrfs_run_delalloc_work(struct btrfs_work *work)
9208 {
9209         struct btrfs_delalloc_work *delalloc_work;
9210         struct inode *inode;
9211
9212         delalloc_work = container_of(work, struct btrfs_delalloc_work,
9213                                      work);
9214         inode = delalloc_work->inode;
9215         filemap_flush(inode->i_mapping);
9216         if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9217                                 &BTRFS_I(inode)->runtime_flags))
9218                 filemap_flush(inode->i_mapping);
9219
9220         iput(inode);
9221         complete(&delalloc_work->completion);
9222 }
9223
9224 static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
9225 {
9226         struct btrfs_delalloc_work *work;
9227
9228         work = kmalloc(sizeof(*work), GFP_NOFS);
9229         if (!work)
9230                 return NULL;
9231
9232         init_completion(&work->completion);
9233         INIT_LIST_HEAD(&work->list);
9234         work->inode = inode;
9235         btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
9236
9237         return work;
9238 }
9239
9240 /*
9241  * some fairly slow code that needs optimization. This walks the list
9242  * of all the inodes with pending delalloc and forces them to disk.
9243  */
9244 static int start_delalloc_inodes(struct btrfs_root *root,
9245                                  struct writeback_control *wbc, bool snapshot,
9246                                  bool in_reclaim_context)
9247 {
9248         struct btrfs_inode *binode;
9249         struct inode *inode;
9250         struct btrfs_delalloc_work *work, *next;
9251         struct list_head works;
9252         struct list_head splice;
9253         int ret = 0;
9254         bool full_flush = wbc->nr_to_write == LONG_MAX;
9255
9256         INIT_LIST_HEAD(&works);
9257         INIT_LIST_HEAD(&splice);
9258
9259         mutex_lock(&root->delalloc_mutex);
9260         spin_lock(&root->delalloc_lock);
9261         list_splice_init(&root->delalloc_inodes, &splice);
9262         while (!list_empty(&splice)) {
9263                 binode = list_entry(splice.next, struct btrfs_inode,
9264                                     delalloc_inodes);
9265
9266                 list_move_tail(&binode->delalloc_inodes,
9267                                &root->delalloc_inodes);
9268
9269                 if (in_reclaim_context &&
9270                     test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9271                         continue;
9272
9273                 inode = igrab(&binode->vfs_inode);
9274                 if (!inode) {
9275                         cond_resched_lock(&root->delalloc_lock);
9276                         continue;
9277                 }
9278                 spin_unlock(&root->delalloc_lock);
9279
9280                 if (snapshot)
9281                         set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9282                                 &binode->runtime_flags);
9283                 if (full_flush) {
9284                         work = btrfs_alloc_delalloc_work(inode);
9285                         if (!work) {
9286                                 iput(inode);
9287                                 ret = -ENOMEM;
9288                                 goto out;
9289                         }
9290                         list_add_tail(&work->list, &works);
9291                         btrfs_queue_work(root->fs_info->flush_workers,
9292                                          &work->work);
9293                 } else {
9294                         ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
9295                         btrfs_add_delayed_iput(BTRFS_I(inode));
9296                         if (ret || wbc->nr_to_write <= 0)
9297                                 goto out;
9298                 }
9299                 cond_resched();
9300                 spin_lock(&root->delalloc_lock);
9301         }
9302         spin_unlock(&root->delalloc_lock);
9303
9304 out:
9305         list_for_each_entry_safe(work, next, &works, list) {
9306                 list_del_init(&work->list);
9307                 wait_for_completion(&work->completion);
9308                 kfree(work);
9309         }
9310
9311         if (!list_empty(&splice)) {
9312                 spin_lock(&root->delalloc_lock);
9313                 list_splice_tail(&splice, &root->delalloc_inodes);
9314                 spin_unlock(&root->delalloc_lock);
9315         }
9316         mutex_unlock(&root->delalloc_mutex);
9317         return ret;
9318 }
9319
9320 int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
9321 {
9322         struct writeback_control wbc = {
9323                 .nr_to_write = LONG_MAX,
9324                 .sync_mode = WB_SYNC_NONE,
9325                 .range_start = 0,
9326                 .range_end = LLONG_MAX,
9327         };
9328         struct btrfs_fs_info *fs_info = root->fs_info;
9329
9330         if (BTRFS_FS_ERROR(fs_info))
9331                 return -EROFS;
9332
9333         return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
9334 }
9335
9336 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
9337                                bool in_reclaim_context)
9338 {
9339         struct writeback_control wbc = {
9340                 .nr_to_write = nr,
9341                 .sync_mode = WB_SYNC_NONE,
9342                 .range_start = 0,
9343                 .range_end = LLONG_MAX,
9344         };
9345         struct btrfs_root *root;
9346         struct list_head splice;
9347         int ret;
9348
9349         if (BTRFS_FS_ERROR(fs_info))
9350                 return -EROFS;
9351
9352         INIT_LIST_HEAD(&splice);
9353
9354         mutex_lock(&fs_info->delalloc_root_mutex);
9355         spin_lock(&fs_info->delalloc_root_lock);
9356         list_splice_init(&fs_info->delalloc_roots, &splice);
9357         while (!list_empty(&splice)) {
9358                 /*
9359                  * Reset nr_to_write here so we know that we're doing a full
9360                  * flush.
9361                  */
9362                 if (nr == LONG_MAX)
9363                         wbc.nr_to_write = LONG_MAX;
9364
9365                 root = list_first_entry(&splice, struct btrfs_root,
9366                                         delalloc_root);
9367                 root = btrfs_grab_root(root);
9368                 BUG_ON(!root);
9369                 list_move_tail(&root->delalloc_root,
9370                                &fs_info->delalloc_roots);
9371                 spin_unlock(&fs_info->delalloc_root_lock);
9372
9373                 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
9374                 btrfs_put_root(root);
9375                 if (ret < 0 || wbc.nr_to_write <= 0)
9376                         goto out;
9377                 spin_lock(&fs_info->delalloc_root_lock);
9378         }
9379         spin_unlock(&fs_info->delalloc_root_lock);
9380
9381         ret = 0;
9382 out:
9383         if (!list_empty(&splice)) {
9384                 spin_lock(&fs_info->delalloc_root_lock);
9385                 list_splice_tail(&splice, &fs_info->delalloc_roots);
9386                 spin_unlock(&fs_info->delalloc_root_lock);
9387         }
9388         mutex_unlock(&fs_info->delalloc_root_mutex);
9389         return ret;
9390 }
9391
9392 static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
9393                          struct dentry *dentry, const char *symname)
9394 {
9395         struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
9396         struct btrfs_trans_handle *trans;
9397         struct btrfs_root *root = BTRFS_I(dir)->root;
9398         struct btrfs_path *path;
9399         struct btrfs_key key;
9400         struct inode *inode;
9401         struct btrfs_new_inode_args new_inode_args = {
9402                 .dir = dir,
9403                 .dentry = dentry,
9404         };
9405         unsigned int trans_num_items;
9406         int err;
9407         int name_len;
9408         int datasize;
9409         unsigned long ptr;
9410         struct btrfs_file_extent_item *ei;
9411         struct extent_buffer *leaf;
9412
9413         name_len = strlen(symname);
9414         if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
9415                 return -ENAMETOOLONG;
9416
9417         inode = new_inode(dir->i_sb);
9418         if (!inode)
9419                 return -ENOMEM;
9420         inode_init_owner(idmap, inode, dir, S_IFLNK | S_IRWXUGO);
9421         inode->i_op = &btrfs_symlink_inode_operations;
9422         inode_nohighmem(inode);
9423         inode->i_mapping->a_ops = &btrfs_aops;
9424         btrfs_i_size_write(BTRFS_I(inode), name_len);
9425         inode_set_bytes(inode, name_len);
9426
9427         new_inode_args.inode = inode;
9428         err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
9429         if (err)
9430                 goto out_inode;
9431         /* 1 additional item for the inline extent */
9432         trans_num_items++;
9433
9434         trans = btrfs_start_transaction(root, trans_num_items);
9435         if (IS_ERR(trans)) {
9436                 err = PTR_ERR(trans);
9437                 goto out_new_inode_args;
9438         }
9439
9440         err = btrfs_create_new_inode(trans, &new_inode_args);
9441         if (err)
9442                 goto out;
9443
9444         path = btrfs_alloc_path();
9445         if (!path) {
9446                 err = -ENOMEM;
9447                 btrfs_abort_transaction(trans, err);
9448                 discard_new_inode(inode);
9449                 inode = NULL;
9450                 goto out;
9451         }
9452         key.objectid = btrfs_ino(BTRFS_I(inode));
9453         key.offset = 0;
9454         key.type = BTRFS_EXTENT_DATA_KEY;
9455         datasize = btrfs_file_extent_calc_inline_size(name_len);
9456         err = btrfs_insert_empty_item(trans, root, path, &key,
9457                                       datasize);
9458         if (err) {
9459                 btrfs_abort_transaction(trans, err);
9460                 btrfs_free_path(path);
9461                 discard_new_inode(inode);
9462                 inode = NULL;
9463                 goto out;
9464         }
9465         leaf = path->nodes[0];
9466         ei = btrfs_item_ptr(leaf, path->slots[0],
9467                             struct btrfs_file_extent_item);
9468         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9469         btrfs_set_file_extent_type(leaf, ei,
9470                                    BTRFS_FILE_EXTENT_INLINE);
9471         btrfs_set_file_extent_encryption(leaf, ei, 0);
9472         btrfs_set_file_extent_compression(leaf, ei, 0);
9473         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9474         btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9475
9476         ptr = btrfs_file_extent_inline_start(ei);
9477         write_extent_buffer(leaf, symname, ptr, name_len);
9478         btrfs_mark_buffer_dirty(leaf);
9479         btrfs_free_path(path);
9480
9481         d_instantiate_new(dentry, inode);
9482         err = 0;
9483 out:
9484         btrfs_end_transaction(trans);
9485         btrfs_btree_balance_dirty(fs_info);
9486 out_new_inode_args:
9487         btrfs_new_inode_args_destroy(&new_inode_args);
9488 out_inode:
9489         if (err)
9490                 iput(inode);
9491         return err;
9492 }
9493
9494 static struct btrfs_trans_handle *insert_prealloc_file_extent(
9495                                        struct btrfs_trans_handle *trans_in,
9496                                        struct btrfs_inode *inode,
9497                                        struct btrfs_key *ins,
9498                                        u64 file_offset)
9499 {
9500         struct btrfs_file_extent_item stack_fi;
9501         struct btrfs_replace_extent_info extent_info;
9502         struct btrfs_trans_handle *trans = trans_in;
9503         struct btrfs_path *path;
9504         u64 start = ins->objectid;
9505         u64 len = ins->offset;
9506         int qgroup_released;
9507         int ret;
9508
9509         memset(&stack_fi, 0, sizeof(stack_fi));
9510
9511         btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9512         btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9513         btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9514         btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9515         btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9516         btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9517         /* Encryption and other encoding is reserved and all 0 */
9518
9519         qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9520         if (qgroup_released < 0)
9521                 return ERR_PTR(qgroup_released);
9522
9523         if (trans) {
9524                 ret = insert_reserved_file_extent(trans, inode,
9525                                                   file_offset, &stack_fi,
9526                                                   true, qgroup_released);
9527                 if (ret)
9528                         goto free_qgroup;
9529                 return trans;
9530         }
9531
9532         extent_info.disk_offset = start;
9533         extent_info.disk_len = len;
9534         extent_info.data_offset = 0;
9535         extent_info.data_len = len;
9536         extent_info.file_offset = file_offset;
9537         extent_info.extent_buf = (char *)&stack_fi;
9538         extent_info.is_new_extent = true;
9539         extent_info.update_times = true;
9540         extent_info.qgroup_reserved = qgroup_released;
9541         extent_info.insertions = 0;
9542
9543         path = btrfs_alloc_path();
9544         if (!path) {
9545                 ret = -ENOMEM;
9546                 goto free_qgroup;
9547         }
9548
9549         ret = btrfs_replace_file_extents(inode, path, file_offset,
9550                                      file_offset + len - 1, &extent_info,
9551                                      &trans);
9552         btrfs_free_path(path);
9553         if (ret)
9554                 goto free_qgroup;
9555         return trans;
9556
9557 free_qgroup:
9558         /*
9559          * We have released qgroup data range at the beginning of the function,
9560          * and normally qgroup_released bytes will be freed when committing
9561          * transaction.
9562          * But if we error out early, we have to free what we have released
9563          * or we leak qgroup data reservation.
9564          */
9565         btrfs_qgroup_free_refroot(inode->root->fs_info,
9566                         inode->root->root_key.objectid, qgroup_released,
9567                         BTRFS_QGROUP_RSV_DATA);
9568         return ERR_PTR(ret);
9569 }
9570
9571 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9572                                        u64 start, u64 num_bytes, u64 min_size,
9573                                        loff_t actual_len, u64 *alloc_hint,
9574                                        struct btrfs_trans_handle *trans)
9575 {
9576         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9577         struct extent_map *em;
9578         struct btrfs_root *root = BTRFS_I(inode)->root;
9579         struct btrfs_key ins;
9580         u64 cur_offset = start;
9581         u64 clear_offset = start;
9582         u64 i_size;
9583         u64 cur_bytes;
9584         u64 last_alloc = (u64)-1;
9585         int ret = 0;
9586         bool own_trans = true;
9587         u64 end = start + num_bytes - 1;
9588
9589         if (trans)
9590                 own_trans = false;
9591         while (num_bytes > 0) {
9592                 cur_bytes = min_t(u64, num_bytes, SZ_256M);
9593                 cur_bytes = max(cur_bytes, min_size);
9594                 /*
9595                  * If we are severely fragmented we could end up with really
9596                  * small allocations, so if the allocator is returning small
9597                  * chunks lets make its job easier by only searching for those
9598                  * sized chunks.
9599                  */
9600                 cur_bytes = min(cur_bytes, last_alloc);
9601                 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9602                                 min_size, 0, *alloc_hint, &ins, 1, 0);
9603                 if (ret)
9604                         break;
9605
9606                 /*
9607                  * We've reserved this space, and thus converted it from
9608                  * ->bytes_may_use to ->bytes_reserved.  Any error that happens
9609                  * from here on out we will only need to clear our reservation
9610                  * for the remaining unreserved area, so advance our
9611                  * clear_offset by our extent size.
9612                  */
9613                 clear_offset += ins.offset;
9614
9615                 last_alloc = ins.offset;
9616                 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9617                                                     &ins, cur_offset);
9618                 /*
9619                  * Now that we inserted the prealloc extent we can finally
9620                  * decrement the number of reservations in the block group.
9621                  * If we did it before, we could race with relocation and have
9622                  * relocation miss the reserved extent, making it fail later.
9623                  */
9624                 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
9625                 if (IS_ERR(trans)) {
9626                         ret = PTR_ERR(trans);
9627                         btrfs_free_reserved_extent(fs_info, ins.objectid,
9628                                                    ins.offset, 0);
9629                         break;
9630                 }
9631
9632                 em = alloc_extent_map();
9633                 if (!em) {
9634                         btrfs_drop_extent_map_range(BTRFS_I(inode), cur_offset,
9635                                             cur_offset + ins.offset - 1, false);
9636                         btrfs_set_inode_full_sync(BTRFS_I(inode));
9637                         goto next;
9638                 }
9639
9640                 em->start = cur_offset;
9641                 em->orig_start = cur_offset;
9642                 em->len = ins.offset;
9643                 em->block_start = ins.objectid;
9644                 em->block_len = ins.offset;
9645                 em->orig_block_len = ins.offset;
9646                 em->ram_bytes = ins.offset;
9647                 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9648                 em->generation = trans->transid;
9649
9650                 ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true);
9651                 free_extent_map(em);
9652 next:
9653                 num_bytes -= ins.offset;
9654                 cur_offset += ins.offset;
9655                 *alloc_hint = ins.objectid + ins.offset;
9656
9657                 inode_inc_iversion(inode);
9658                 inode->i_ctime = current_time(inode);
9659                 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
9660                 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
9661                     (actual_len > inode->i_size) &&
9662                     (cur_offset > inode->i_size)) {
9663                         if (cur_offset > actual_len)
9664                                 i_size = actual_len;
9665                         else
9666                                 i_size = cur_offset;
9667                         i_size_write(inode, i_size);
9668                         btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
9669                 }
9670
9671                 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
9672
9673                 if (ret) {
9674                         btrfs_abort_transaction(trans, ret);
9675                         if (own_trans)
9676                                 btrfs_end_transaction(trans);
9677                         break;
9678                 }
9679
9680                 if (own_trans) {
9681                         btrfs_end_transaction(trans);
9682                         trans = NULL;
9683                 }
9684         }
9685         if (clear_offset < end)
9686                 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
9687                         end - clear_offset + 1);
9688         return ret;
9689 }
9690
9691 int btrfs_prealloc_file_range(struct inode *inode, int mode,
9692                               u64 start, u64 num_bytes, u64 min_size,
9693                               loff_t actual_len, u64 *alloc_hint)
9694 {
9695         return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9696                                            min_size, actual_len, alloc_hint,
9697                                            NULL);
9698 }
9699
9700 int btrfs_prealloc_file_range_trans(struct inode *inode,
9701                                     struct btrfs_trans_handle *trans, int mode,
9702                                     u64 start, u64 num_bytes, u64 min_size,
9703                                     loff_t actual_len, u64 *alloc_hint)
9704 {
9705         return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9706                                            min_size, actual_len, alloc_hint, trans);
9707 }
9708
9709 static int btrfs_permission(struct mnt_idmap *idmap,
9710                             struct inode *inode, int mask)
9711 {
9712         struct btrfs_root *root = BTRFS_I(inode)->root;
9713         umode_t mode = inode->i_mode;
9714
9715         if (mask & MAY_WRITE &&
9716             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9717                 if (btrfs_root_readonly(root))
9718                         return -EROFS;
9719                 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9720                         return -EACCES;
9721         }
9722         return generic_permission(idmap, inode, mask);
9723 }
9724
9725 static int btrfs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
9726                          struct file *file, umode_t mode)
9727 {
9728         struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
9729         struct btrfs_trans_handle *trans;
9730         struct btrfs_root *root = BTRFS_I(dir)->root;
9731         struct inode *inode;
9732         struct btrfs_new_inode_args new_inode_args = {
9733                 .dir = dir,
9734                 .dentry = file->f_path.dentry,
9735                 .orphan = true,
9736         };
9737         unsigned int trans_num_items;
9738         int ret;
9739
9740         inode = new_inode(dir->i_sb);
9741         if (!inode)
9742                 return -ENOMEM;
9743         inode_init_owner(idmap, inode, dir, mode);
9744         inode->i_fop = &btrfs_file_operations;
9745         inode->i_op = &btrfs_file_inode_operations;
9746         inode->i_mapping->a_ops = &btrfs_aops;
9747
9748         new_inode_args.inode = inode;
9749         ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
9750         if (ret)
9751                 goto out_inode;
9752
9753         trans = btrfs_start_transaction(root, trans_num_items);
9754         if (IS_ERR(trans)) {
9755                 ret = PTR_ERR(trans);
9756                 goto out_new_inode_args;
9757         }
9758
9759         ret = btrfs_create_new_inode(trans, &new_inode_args);
9760
9761         /*
9762          * We set number of links to 0 in btrfs_create_new_inode(), and here we
9763          * set it to 1 because d_tmpfile() will issue a warning if the count is
9764          * 0, through:
9765          *
9766          *    d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9767          */
9768         set_nlink(inode, 1);
9769
9770         if (!ret) {
9771                 d_tmpfile(file, inode);
9772                 unlock_new_inode(inode);
9773                 mark_inode_dirty(inode);
9774         }
9775
9776         btrfs_end_transaction(trans);
9777         btrfs_btree_balance_dirty(fs_info);
9778 out_new_inode_args:
9779         btrfs_new_inode_args_destroy(&new_inode_args);
9780 out_inode:
9781         if (ret)
9782                 iput(inode);
9783         return finish_open_simple(file, ret);
9784 }
9785
9786 void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
9787 {
9788         struct btrfs_fs_info *fs_info = inode->root->fs_info;
9789         unsigned long index = start >> PAGE_SHIFT;
9790         unsigned long end_index = end >> PAGE_SHIFT;
9791         struct page *page;
9792         u32 len;
9793
9794         ASSERT(end + 1 - start <= U32_MAX);
9795         len = end + 1 - start;
9796         while (index <= end_index) {
9797                 page = find_get_page(inode->vfs_inode.i_mapping, index);
9798                 ASSERT(page); /* Pages should be in the extent_io_tree */
9799
9800                 btrfs_page_set_writeback(fs_info, page, start, len);
9801                 put_page(page);
9802                 index++;
9803         }
9804 }
9805
9806 int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
9807                                              int compress_type)
9808 {
9809         switch (compress_type) {
9810         case BTRFS_COMPRESS_NONE:
9811                 return BTRFS_ENCODED_IO_COMPRESSION_NONE;
9812         case BTRFS_COMPRESS_ZLIB:
9813                 return BTRFS_ENCODED_IO_COMPRESSION_ZLIB;
9814         case BTRFS_COMPRESS_LZO:
9815                 /*
9816                  * The LZO format depends on the sector size. 64K is the maximum
9817                  * sector size that we support.
9818                  */
9819                 if (fs_info->sectorsize < SZ_4K || fs_info->sectorsize > SZ_64K)
9820                         return -EINVAL;
9821                 return BTRFS_ENCODED_IO_COMPRESSION_LZO_4K +
9822                        (fs_info->sectorsize_bits - 12);
9823         case BTRFS_COMPRESS_ZSTD:
9824                 return BTRFS_ENCODED_IO_COMPRESSION_ZSTD;
9825         default:
9826                 return -EUCLEAN;
9827         }
9828 }
9829
9830 static ssize_t btrfs_encoded_read_inline(
9831                                 struct kiocb *iocb,
9832                                 struct iov_iter *iter, u64 start,
9833                                 u64 lockend,
9834                                 struct extent_state **cached_state,
9835                                 u64 extent_start, size_t count,
9836                                 struct btrfs_ioctl_encoded_io_args *encoded,
9837                                 bool *unlocked)
9838 {
9839         struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
9840         struct btrfs_root *root = inode->root;
9841         struct btrfs_fs_info *fs_info = root->fs_info;
9842         struct extent_io_tree *io_tree = &inode->io_tree;
9843         struct btrfs_path *path;
9844         struct extent_buffer *leaf;
9845         struct btrfs_file_extent_item *item;
9846         u64 ram_bytes;
9847         unsigned long ptr;
9848         void *tmp;
9849         ssize_t ret;
9850
9851         path = btrfs_alloc_path();
9852         if (!path) {
9853                 ret = -ENOMEM;
9854                 goto out;
9855         }
9856         ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
9857                                        extent_start, 0);
9858         if (ret) {
9859                 if (ret > 0) {
9860                         /* The extent item disappeared? */
9861                         ret = -EIO;
9862                 }
9863                 goto out;
9864         }
9865         leaf = path->nodes[0];
9866         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
9867
9868         ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
9869         ptr = btrfs_file_extent_inline_start(item);
9870
9871         encoded->len = min_t(u64, extent_start + ram_bytes,
9872                              inode->vfs_inode.i_size) - iocb->ki_pos;
9873         ret = btrfs_encoded_io_compression_from_extent(fs_info,
9874                                  btrfs_file_extent_compression(leaf, item));
9875         if (ret < 0)
9876                 goto out;
9877         encoded->compression = ret;
9878         if (encoded->compression) {
9879                 size_t inline_size;
9880
9881                 inline_size = btrfs_file_extent_inline_item_len(leaf,
9882                                                                 path->slots[0]);
9883                 if (inline_size > count) {
9884                         ret = -ENOBUFS;
9885                         goto out;
9886                 }
9887                 count = inline_size;
9888                 encoded->unencoded_len = ram_bytes;
9889                 encoded->unencoded_offset = iocb->ki_pos - extent_start;
9890         } else {
9891                 count = min_t(u64, count, encoded->len);
9892                 encoded->len = count;
9893                 encoded->unencoded_len = count;
9894                 ptr += iocb->ki_pos - extent_start;
9895         }
9896
9897         tmp = kmalloc(count, GFP_NOFS);
9898         if (!tmp) {
9899                 ret = -ENOMEM;
9900                 goto out;
9901         }
9902         read_extent_buffer(leaf, tmp, ptr, count);
9903         btrfs_release_path(path);
9904         unlock_extent(io_tree, start, lockend, cached_state);
9905         btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
9906         *unlocked = true;
9907
9908         ret = copy_to_iter(tmp, count, iter);
9909         if (ret != count)
9910                 ret = -EFAULT;
9911         kfree(tmp);
9912 out:
9913         btrfs_free_path(path);
9914         return ret;
9915 }
9916
9917 struct btrfs_encoded_read_private {
9918         wait_queue_head_t wait;
9919         atomic_t pending;
9920         blk_status_t status;
9921 };
9922
9923 static void btrfs_encoded_read_endio(struct btrfs_bio *bbio)
9924 {
9925         struct btrfs_encoded_read_private *priv = bbio->private;
9926
9927         if (bbio->bio.bi_status) {
9928                 /*
9929                  * The memory barrier implied by the atomic_dec_return() here
9930                  * pairs with the memory barrier implied by the
9931                  * atomic_dec_return() or io_wait_event() in
9932                  * btrfs_encoded_read_regular_fill_pages() to ensure that this
9933                  * write is observed before the load of status in
9934                  * btrfs_encoded_read_regular_fill_pages().
9935                  */
9936                 WRITE_ONCE(priv->status, bbio->bio.bi_status);
9937         }
9938         if (!atomic_dec_return(&priv->pending))
9939                 wake_up(&priv->wait);
9940         bio_put(&bbio->bio);
9941 }
9942
9943 int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
9944                                           u64 file_offset, u64 disk_bytenr,
9945                                           u64 disk_io_size, struct page **pages)
9946 {
9947         struct btrfs_fs_info *fs_info = inode->root->fs_info;
9948         struct btrfs_encoded_read_private priv = {
9949                 .pending = ATOMIC_INIT(1),
9950         };
9951         unsigned long i = 0;
9952         struct btrfs_bio *bbio;
9953
9954         init_waitqueue_head(&priv.wait);
9955
9956         bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info,
9957                                btrfs_encoded_read_endio, &priv);
9958         bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
9959         bbio->inode = inode;
9960
9961         do {
9962                 size_t bytes = min_t(u64, disk_io_size, PAGE_SIZE);
9963
9964                 if (bio_add_page(&bbio->bio, pages[i], bytes, 0) < bytes) {
9965                         atomic_inc(&priv.pending);
9966                         btrfs_submit_bio(bbio, 0);
9967
9968                         bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info,
9969                                                btrfs_encoded_read_endio, &priv);
9970                         bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
9971                         bbio->inode = inode;
9972                         continue;
9973                 }
9974
9975                 i++;
9976                 disk_bytenr += bytes;
9977                 disk_io_size -= bytes;
9978         } while (disk_io_size);
9979
9980         atomic_inc(&priv.pending);
9981         btrfs_submit_bio(bbio, 0);
9982
9983         if (atomic_dec_return(&priv.pending))
9984                 io_wait_event(priv.wait, !atomic_read(&priv.pending));
9985         /* See btrfs_encoded_read_endio() for ordering. */
9986         return blk_status_to_errno(READ_ONCE(priv.status));
9987 }
9988
9989 static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb,
9990                                           struct iov_iter *iter,
9991                                           u64 start, u64 lockend,
9992                                           struct extent_state **cached_state,
9993                                           u64 disk_bytenr, u64 disk_io_size,
9994                                           size_t count, bool compressed,
9995                                           bool *unlocked)
9996 {
9997         struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
9998         struct extent_io_tree *io_tree = &inode->io_tree;
9999         struct page **pages;
10000         unsigned long nr_pages, i;
10001         u64 cur;
10002         size_t page_offset;
10003         ssize_t ret;
10004
10005         nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
10006         pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
10007         if (!pages)
10008                 return -ENOMEM;
10009         ret = btrfs_alloc_page_array(nr_pages, pages);
10010         if (ret) {
10011                 ret = -ENOMEM;
10012                 goto out;
10013                 }
10014
10015         ret = btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr,
10016                                                     disk_io_size, pages);
10017         if (ret)
10018                 goto out;
10019
10020         unlock_extent(io_tree, start, lockend, cached_state);
10021         btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10022         *unlocked = true;
10023
10024         if (compressed) {
10025                 i = 0;
10026                 page_offset = 0;
10027         } else {
10028                 i = (iocb->ki_pos - start) >> PAGE_SHIFT;
10029                 page_offset = (iocb->ki_pos - start) & (PAGE_SIZE - 1);
10030         }
10031         cur = 0;
10032         while (cur < count) {
10033                 size_t bytes = min_t(size_t, count - cur,
10034                                      PAGE_SIZE - page_offset);
10035
10036                 if (copy_page_to_iter(pages[i], page_offset, bytes,
10037                                       iter) != bytes) {
10038                         ret = -EFAULT;
10039                         goto out;
10040                 }
10041                 i++;
10042                 cur += bytes;
10043                 page_offset = 0;
10044         }
10045         ret = count;
10046 out:
10047         for (i = 0; i < nr_pages; i++) {
10048                 if (pages[i])
10049                         __free_page(pages[i]);
10050         }
10051         kfree(pages);
10052         return ret;
10053 }
10054
10055 ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
10056                            struct btrfs_ioctl_encoded_io_args *encoded)
10057 {
10058         struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10059         struct btrfs_fs_info *fs_info = inode->root->fs_info;
10060         struct extent_io_tree *io_tree = &inode->io_tree;
10061         ssize_t ret;
10062         size_t count = iov_iter_count(iter);
10063         u64 start, lockend, disk_bytenr, disk_io_size;
10064         struct extent_state *cached_state = NULL;
10065         struct extent_map *em;
10066         bool unlocked = false;
10067
10068         file_accessed(iocb->ki_filp);
10069
10070         btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
10071
10072         if (iocb->ki_pos >= inode->vfs_inode.i_size) {
10073                 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10074                 return 0;
10075         }
10076         start = ALIGN_DOWN(iocb->ki_pos, fs_info->sectorsize);
10077         /*
10078          * We don't know how long the extent containing iocb->ki_pos is, but if
10079          * it's compressed we know that it won't be longer than this.
10080          */
10081         lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
10082
10083         for (;;) {
10084                 struct btrfs_ordered_extent *ordered;
10085
10086                 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start,
10087                                                lockend - start + 1);
10088                 if (ret)
10089                         goto out_unlock_inode;
10090                 lock_extent(io_tree, start, lockend, &cached_state);
10091                 ordered = btrfs_lookup_ordered_range(inode, start,
10092                                                      lockend - start + 1);
10093                 if (!ordered)
10094                         break;
10095                 btrfs_put_ordered_extent(ordered);
10096                 unlock_extent(io_tree, start, lockend, &cached_state);
10097                 cond_resched();
10098         }
10099
10100         em = btrfs_get_extent(inode, NULL, 0, start, lockend - start + 1);
10101         if (IS_ERR(em)) {
10102                 ret = PTR_ERR(em);
10103                 goto out_unlock_extent;
10104         }
10105
10106         if (em->block_start == EXTENT_MAP_INLINE) {
10107                 u64 extent_start = em->start;
10108
10109                 /*
10110                  * For inline extents we get everything we need out of the
10111                  * extent item.
10112                  */
10113                 free_extent_map(em);
10114                 em = NULL;
10115                 ret = btrfs_encoded_read_inline(iocb, iter, start, lockend,
10116                                                 &cached_state, extent_start,
10117                                                 count, encoded, &unlocked);
10118                 goto out;
10119         }
10120
10121         /*
10122          * We only want to return up to EOF even if the extent extends beyond
10123          * that.
10124          */
10125         encoded->len = min_t(u64, extent_map_end(em),
10126                              inode->vfs_inode.i_size) - iocb->ki_pos;
10127         if (em->block_start == EXTENT_MAP_HOLE ||
10128             test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
10129                 disk_bytenr = EXTENT_MAP_HOLE;
10130                 count = min_t(u64, count, encoded->len);
10131                 encoded->len = count;
10132                 encoded->unencoded_len = count;
10133         } else if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10134                 disk_bytenr = em->block_start;
10135                 /*
10136                  * Bail if the buffer isn't large enough to return the whole
10137                  * compressed extent.
10138                  */
10139                 if (em->block_len > count) {
10140                         ret = -ENOBUFS;
10141                         goto out_em;
10142                 }
10143                 disk_io_size = em->block_len;
10144                 count = em->block_len;
10145                 encoded->unencoded_len = em->ram_bytes;
10146                 encoded->unencoded_offset = iocb->ki_pos - em->orig_start;
10147                 ret = btrfs_encoded_io_compression_from_extent(fs_info,
10148                                                              em->compress_type);
10149                 if (ret < 0)
10150                         goto out_em;
10151                 encoded->compression = ret;
10152         } else {
10153                 disk_bytenr = em->block_start + (start - em->start);
10154                 if (encoded->len > count)
10155                         encoded->len = count;
10156                 /*
10157                  * Don't read beyond what we locked. This also limits the page
10158                  * allocations that we'll do.
10159                  */
10160                 disk_io_size = min(lockend + 1, iocb->ki_pos + encoded->len) - start;
10161                 count = start + disk_io_size - iocb->ki_pos;
10162                 encoded->len = count;
10163                 encoded->unencoded_len = count;
10164                 disk_io_size = ALIGN(disk_io_size, fs_info->sectorsize);
10165         }
10166         free_extent_map(em);
10167         em = NULL;
10168
10169         if (disk_bytenr == EXTENT_MAP_HOLE) {
10170                 unlock_extent(io_tree, start, lockend, &cached_state);
10171                 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10172                 unlocked = true;
10173                 ret = iov_iter_zero(count, iter);
10174                 if (ret != count)
10175                         ret = -EFAULT;
10176         } else {
10177                 ret = btrfs_encoded_read_regular(iocb, iter, start, lockend,
10178                                                  &cached_state, disk_bytenr,
10179                                                  disk_io_size, count,
10180                                                  encoded->compression,
10181                                                  &unlocked);
10182         }
10183
10184 out:
10185         if (ret >= 0)
10186                 iocb->ki_pos += encoded->len;
10187 out_em:
10188         free_extent_map(em);
10189 out_unlock_extent:
10190         if (!unlocked)
10191                 unlock_extent(io_tree, start, lockend, &cached_state);
10192 out_unlock_inode:
10193         if (!unlocked)
10194                 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10195         return ret;
10196 }
10197
10198 ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
10199                                const struct btrfs_ioctl_encoded_io_args *encoded)
10200 {
10201         struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10202         struct btrfs_root *root = inode->root;
10203         struct btrfs_fs_info *fs_info = root->fs_info;
10204         struct extent_io_tree *io_tree = &inode->io_tree;
10205         struct extent_changeset *data_reserved = NULL;
10206         struct extent_state *cached_state = NULL;
10207         struct btrfs_ordered_extent *ordered;
10208         int compression;
10209         size_t orig_count;
10210         u64 start, end;
10211         u64 num_bytes, ram_bytes, disk_num_bytes;
10212         unsigned long nr_pages, i;
10213         struct page **pages;
10214         struct btrfs_key ins;
10215         bool extent_reserved = false;
10216         struct extent_map *em;
10217         ssize_t ret;
10218
10219         switch (encoded->compression) {
10220         case BTRFS_ENCODED_IO_COMPRESSION_ZLIB:
10221                 compression = BTRFS_COMPRESS_ZLIB;
10222                 break;
10223         case BTRFS_ENCODED_IO_COMPRESSION_ZSTD:
10224                 compression = BTRFS_COMPRESS_ZSTD;
10225                 break;
10226         case BTRFS_ENCODED_IO_COMPRESSION_LZO_4K:
10227         case BTRFS_ENCODED_IO_COMPRESSION_LZO_8K:
10228         case BTRFS_ENCODED_IO_COMPRESSION_LZO_16K:
10229         case BTRFS_ENCODED_IO_COMPRESSION_LZO_32K:
10230         case BTRFS_ENCODED_IO_COMPRESSION_LZO_64K:
10231                 /* The sector size must match for LZO. */
10232                 if (encoded->compression -
10233                     BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + 12 !=
10234                     fs_info->sectorsize_bits)
10235                         return -EINVAL;
10236                 compression = BTRFS_COMPRESS_LZO;
10237                 break;
10238         default:
10239                 return -EINVAL;
10240         }
10241         if (encoded->encryption != BTRFS_ENCODED_IO_ENCRYPTION_NONE)
10242                 return -EINVAL;
10243
10244         orig_count = iov_iter_count(from);
10245
10246         /* The extent size must be sane. */
10247         if (encoded->unencoded_len > BTRFS_MAX_UNCOMPRESSED ||
10248             orig_count > BTRFS_MAX_COMPRESSED || orig_count == 0)
10249                 return -EINVAL;
10250
10251         /*
10252          * The compressed data must be smaller than the decompressed data.
10253          *
10254          * It's of course possible for data to compress to larger or the same
10255          * size, but the buffered I/O path falls back to no compression for such
10256          * data, and we don't want to break any assumptions by creating these
10257          * extents.
10258          *
10259          * Note that this is less strict than the current check we have that the
10260          * compressed data must be at least one sector smaller than the
10261          * decompressed data. We only want to enforce the weaker requirement
10262          * from old kernels that it is at least one byte smaller.
10263          */
10264         if (orig_count >= encoded->unencoded_len)
10265                 return -EINVAL;
10266
10267         /* The extent must start on a sector boundary. */
10268         start = iocb->ki_pos;
10269         if (!IS_ALIGNED(start, fs_info->sectorsize))
10270                 return -EINVAL;
10271
10272         /*
10273          * The extent must end on a sector boundary. However, we allow a write
10274          * which ends at or extends i_size to have an unaligned length; we round
10275          * up the extent size and set i_size to the unaligned end.
10276          */
10277         if (start + encoded->len < inode->vfs_inode.i_size &&
10278             !IS_ALIGNED(start + encoded->len, fs_info->sectorsize))
10279                 return -EINVAL;
10280
10281         /* Finally, the offset in the unencoded data must be sector-aligned. */
10282         if (!IS_ALIGNED(encoded->unencoded_offset, fs_info->sectorsize))
10283                 return -EINVAL;
10284
10285         num_bytes = ALIGN(encoded->len, fs_info->sectorsize);
10286         ram_bytes = ALIGN(encoded->unencoded_len, fs_info->sectorsize);
10287         end = start + num_bytes - 1;
10288
10289         /*
10290          * If the extent cannot be inline, the compressed data on disk must be
10291          * sector-aligned. For convenience, we extend it with zeroes if it
10292          * isn't.
10293          */
10294         disk_num_bytes = ALIGN(orig_count, fs_info->sectorsize);
10295         nr_pages = DIV_ROUND_UP(disk_num_bytes, PAGE_SIZE);
10296         pages = kvcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL_ACCOUNT);
10297         if (!pages)
10298                 return -ENOMEM;
10299         for (i = 0; i < nr_pages; i++) {
10300                 size_t bytes = min_t(size_t, PAGE_SIZE, iov_iter_count(from));
10301                 char *kaddr;
10302
10303                 pages[i] = alloc_page(GFP_KERNEL_ACCOUNT);
10304                 if (!pages[i]) {
10305                         ret = -ENOMEM;
10306                         goto out_pages;
10307                 }
10308                 kaddr = kmap_local_page(pages[i]);
10309                 if (copy_from_iter(kaddr, bytes, from) != bytes) {
10310                         kunmap_local(kaddr);
10311                         ret = -EFAULT;
10312                         goto out_pages;
10313                 }
10314                 if (bytes < PAGE_SIZE)
10315                         memset(kaddr + bytes, 0, PAGE_SIZE - bytes);
10316                 kunmap_local(kaddr);
10317         }
10318
10319         for (;;) {
10320                 struct btrfs_ordered_extent *ordered;
10321
10322                 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, num_bytes);
10323                 if (ret)
10324                         goto out_pages;
10325                 ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping,
10326                                                     start >> PAGE_SHIFT,
10327                                                     end >> PAGE_SHIFT);
10328                 if (ret)
10329                         goto out_pages;
10330                 lock_extent(io_tree, start, end, &cached_state);
10331                 ordered = btrfs_lookup_ordered_range(inode, start, num_bytes);
10332                 if (!ordered &&
10333                     !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end))
10334                         break;
10335                 if (ordered)
10336                         btrfs_put_ordered_extent(ordered);
10337                 unlock_extent(io_tree, start, end, &cached_state);
10338                 cond_resched();
10339         }
10340
10341         /*
10342          * We don't use the higher-level delalloc space functions because our
10343          * num_bytes and disk_num_bytes are different.
10344          */
10345         ret = btrfs_alloc_data_chunk_ondemand(inode, disk_num_bytes);
10346         if (ret)
10347                 goto out_unlock;
10348         ret = btrfs_qgroup_reserve_data(inode, &data_reserved, start, num_bytes);
10349         if (ret)
10350                 goto out_free_data_space;
10351         ret = btrfs_delalloc_reserve_metadata(inode, num_bytes, disk_num_bytes,
10352                                               false);
10353         if (ret)
10354                 goto out_qgroup_free_data;
10355
10356         /* Try an inline extent first. */
10357         if (start == 0 && encoded->unencoded_len == encoded->len &&
10358             encoded->unencoded_offset == 0) {
10359                 ret = cow_file_range_inline(inode, encoded->len, orig_count,
10360                                             compression, pages, true);
10361                 if (ret <= 0) {
10362                         if (ret == 0)
10363                                 ret = orig_count;
10364                         goto out_delalloc_release;
10365                 }
10366         }
10367
10368         ret = btrfs_reserve_extent(root, disk_num_bytes, disk_num_bytes,
10369                                    disk_num_bytes, 0, 0, &ins, 1, 1);
10370         if (ret)
10371                 goto out_delalloc_release;
10372         extent_reserved = true;
10373
10374         em = create_io_em(inode, start, num_bytes,
10375                           start - encoded->unencoded_offset, ins.objectid,
10376                           ins.offset, ins.offset, ram_bytes, compression,
10377                           BTRFS_ORDERED_COMPRESSED);
10378         if (IS_ERR(em)) {
10379                 ret = PTR_ERR(em);
10380                 goto out_free_reserved;
10381         }
10382         free_extent_map(em);
10383
10384         ordered = btrfs_alloc_ordered_extent(inode, start, num_bytes, ram_bytes,
10385                                        ins.objectid, ins.offset,
10386                                        encoded->unencoded_offset,
10387                                        (1 << BTRFS_ORDERED_ENCODED) |
10388                                        (1 << BTRFS_ORDERED_COMPRESSED),
10389                                        compression);
10390         if (IS_ERR(ordered)) {
10391                 btrfs_drop_extent_map_range(inode, start, end, false);
10392                 ret = PTR_ERR(ordered);
10393                 goto out_free_reserved;
10394         }
10395         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10396
10397         if (start + encoded->len > inode->vfs_inode.i_size)
10398                 i_size_write(&inode->vfs_inode, start + encoded->len);
10399
10400         unlock_extent(io_tree, start, end, &cached_state);
10401
10402         btrfs_delalloc_release_extents(inode, num_bytes);
10403
10404         btrfs_submit_compressed_write(ordered, pages, nr_pages, 0, false);
10405         ret = orig_count;
10406         goto out;
10407
10408 out_free_reserved:
10409         btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10410         btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
10411 out_delalloc_release:
10412         btrfs_delalloc_release_extents(inode, num_bytes);
10413         btrfs_delalloc_release_metadata(inode, disk_num_bytes, ret < 0);
10414 out_qgroup_free_data:
10415         if (ret < 0)
10416                 btrfs_qgroup_free_data(inode, data_reserved, start, num_bytes);
10417 out_free_data_space:
10418         /*
10419          * If btrfs_reserve_extent() succeeded, then we already decremented
10420          * bytes_may_use.
10421          */
10422         if (!extent_reserved)
10423                 btrfs_free_reserved_data_space_noquota(fs_info, disk_num_bytes);
10424 out_unlock:
10425         unlock_extent(io_tree, start, end, &cached_state);
10426 out_pages:
10427         for (i = 0; i < nr_pages; i++) {
10428                 if (pages[i])
10429                         __free_page(pages[i]);
10430         }
10431         kvfree(pages);
10432 out:
10433         if (ret >= 0)
10434                 iocb->ki_pos += encoded->len;
10435         return ret;
10436 }
10437
10438 #ifdef CONFIG_SWAP
10439 /*
10440  * Add an entry indicating a block group or device which is pinned by a
10441  * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10442  * negative errno on failure.
10443  */
10444 static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10445                                   bool is_block_group)
10446 {
10447         struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10448         struct btrfs_swapfile_pin *sp, *entry;
10449         struct rb_node **p;
10450         struct rb_node *parent = NULL;
10451
10452         sp = kmalloc(sizeof(*sp), GFP_NOFS);
10453         if (!sp)
10454                 return -ENOMEM;
10455         sp->ptr = ptr;
10456         sp->inode = inode;
10457         sp->is_block_group = is_block_group;
10458         sp->bg_extent_count = 1;
10459
10460         spin_lock(&fs_info->swapfile_pins_lock);
10461         p = &fs_info->swapfile_pins.rb_node;
10462         while (*p) {
10463                 parent = *p;
10464                 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10465                 if (sp->ptr < entry->ptr ||
10466                     (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10467                         p = &(*p)->rb_left;
10468                 } else if (sp->ptr > entry->ptr ||
10469                            (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10470                         p = &(*p)->rb_right;
10471                 } else {
10472                         if (is_block_group)
10473                                 entry->bg_extent_count++;
10474                         spin_unlock(&fs_info->swapfile_pins_lock);
10475                         kfree(sp);
10476                         return 1;
10477                 }
10478         }
10479         rb_link_node(&sp->node, parent, p);
10480         rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10481         spin_unlock(&fs_info->swapfile_pins_lock);
10482         return 0;
10483 }
10484
10485 /* Free all of the entries pinned by this swapfile. */
10486 static void btrfs_free_swapfile_pins(struct inode *inode)
10487 {
10488         struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10489         struct btrfs_swapfile_pin *sp;
10490         struct rb_node *node, *next;
10491
10492         spin_lock(&fs_info->swapfile_pins_lock);
10493         node = rb_first(&fs_info->swapfile_pins);
10494         while (node) {
10495                 next = rb_next(node);
10496                 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10497                 if (sp->inode == inode) {
10498                         rb_erase(&sp->node, &fs_info->swapfile_pins);
10499                         if (sp->is_block_group) {
10500                                 btrfs_dec_block_group_swap_extents(sp->ptr,
10501                                                            sp->bg_extent_count);
10502                                 btrfs_put_block_group(sp->ptr);
10503                         }
10504                         kfree(sp);
10505                 }
10506                 node = next;
10507         }
10508         spin_unlock(&fs_info->swapfile_pins_lock);
10509 }
10510
10511 struct btrfs_swap_info {
10512         u64 start;
10513         u64 block_start;
10514         u64 block_len;
10515         u64 lowest_ppage;
10516         u64 highest_ppage;
10517         unsigned long nr_pages;
10518         int nr_extents;
10519 };
10520
10521 static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10522                                  struct btrfs_swap_info *bsi)
10523 {
10524         unsigned long nr_pages;
10525         unsigned long max_pages;
10526         u64 first_ppage, first_ppage_reported, next_ppage;
10527         int ret;
10528
10529         /*
10530          * Our swapfile may have had its size extended after the swap header was
10531          * written. In that case activating the swapfile should not go beyond
10532          * the max size set in the swap header.
10533          */
10534         if (bsi->nr_pages >= sis->max)
10535                 return 0;
10536
10537         max_pages = sis->max - bsi->nr_pages;
10538         first_ppage = PAGE_ALIGN(bsi->block_start) >> PAGE_SHIFT;
10539         next_ppage = PAGE_ALIGN_DOWN(bsi->block_start + bsi->block_len) >> PAGE_SHIFT;
10540
10541         if (first_ppage >= next_ppage)
10542                 return 0;
10543         nr_pages = next_ppage - first_ppage;
10544         nr_pages = min(nr_pages, max_pages);
10545
10546         first_ppage_reported = first_ppage;
10547         if (bsi->start == 0)
10548                 first_ppage_reported++;
10549         if (bsi->lowest_ppage > first_ppage_reported)
10550                 bsi->lowest_ppage = first_ppage_reported;
10551         if (bsi->highest_ppage < (next_ppage - 1))
10552                 bsi->highest_ppage = next_ppage - 1;
10553
10554         ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10555         if (ret < 0)
10556                 return ret;
10557         bsi->nr_extents += ret;
10558         bsi->nr_pages += nr_pages;
10559         return 0;
10560 }
10561
10562 static void btrfs_swap_deactivate(struct file *file)
10563 {
10564         struct inode *inode = file_inode(file);
10565
10566         btrfs_free_swapfile_pins(inode);
10567         atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10568 }
10569
10570 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10571                                sector_t *span)
10572 {
10573         struct inode *inode = file_inode(file);
10574         struct btrfs_root *root = BTRFS_I(inode)->root;
10575         struct btrfs_fs_info *fs_info = root->fs_info;
10576         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10577         struct extent_state *cached_state = NULL;
10578         struct extent_map *em = NULL;
10579         struct btrfs_device *device = NULL;
10580         struct btrfs_swap_info bsi = {
10581                 .lowest_ppage = (sector_t)-1ULL,
10582         };
10583         int ret = 0;
10584         u64 isize;
10585         u64 start;
10586
10587         /*
10588          * If the swap file was just created, make sure delalloc is done. If the
10589          * file changes again after this, the user is doing something stupid and
10590          * we don't really care.
10591          */
10592         ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10593         if (ret)
10594                 return ret;
10595
10596         /*
10597          * The inode is locked, so these flags won't change after we check them.
10598          */
10599         if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10600                 btrfs_warn(fs_info, "swapfile must not be compressed");
10601                 return -EINVAL;
10602         }
10603         if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10604                 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10605                 return -EINVAL;
10606         }
10607         if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10608                 btrfs_warn(fs_info, "swapfile must not be checksummed");
10609                 return -EINVAL;
10610         }
10611
10612         /*
10613          * Balance or device remove/replace/resize can move stuff around from
10614          * under us. The exclop protection makes sure they aren't running/won't
10615          * run concurrently while we are mapping the swap extents, and
10616          * fs_info->swapfile_pins prevents them from running while the swap
10617          * file is active and moving the extents. Note that this also prevents
10618          * a concurrent device add which isn't actually necessary, but it's not
10619          * really worth the trouble to allow it.
10620          */
10621         if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
10622                 btrfs_warn(fs_info,
10623            "cannot activate swapfile while exclusive operation is running");
10624                 return -EBUSY;
10625         }
10626
10627         /*
10628          * Prevent snapshot creation while we are activating the swap file.
10629          * We do not want to race with snapshot creation. If snapshot creation
10630          * already started before we bumped nr_swapfiles from 0 to 1 and
10631          * completes before the first write into the swap file after it is
10632          * activated, than that write would fallback to COW.
10633          */
10634         if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10635                 btrfs_exclop_finish(fs_info);
10636                 btrfs_warn(fs_info,
10637            "cannot activate swapfile because snapshot creation is in progress");
10638                 return -EINVAL;
10639         }
10640         /*
10641          * Snapshots can create extents which require COW even if NODATACOW is
10642          * set. We use this counter to prevent snapshots. We must increment it
10643          * before walking the extents because we don't want a concurrent
10644          * snapshot to run after we've already checked the extents.
10645          *
10646          * It is possible that subvolume is marked for deletion but still not
10647          * removed yet. To prevent this race, we check the root status before
10648          * activating the swapfile.
10649          */
10650         spin_lock(&root->root_item_lock);
10651         if (btrfs_root_dead(root)) {
10652                 spin_unlock(&root->root_item_lock);
10653
10654                 btrfs_exclop_finish(fs_info);
10655                 btrfs_warn(fs_info,
10656                 "cannot activate swapfile because subvolume %llu is being deleted",
10657                         root->root_key.objectid);
10658                 return -EPERM;
10659         }
10660         atomic_inc(&root->nr_swapfiles);
10661         spin_unlock(&root->root_item_lock);
10662
10663         isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10664
10665         lock_extent(io_tree, 0, isize - 1, &cached_state);
10666         start = 0;
10667         while (start < isize) {
10668                 u64 logical_block_start, physical_block_start;
10669                 struct btrfs_block_group *bg;
10670                 u64 len = isize - start;
10671
10672                 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
10673                 if (IS_ERR(em)) {
10674                         ret = PTR_ERR(em);
10675                         goto out;
10676                 }
10677
10678                 if (em->block_start == EXTENT_MAP_HOLE) {
10679                         btrfs_warn(fs_info, "swapfile must not have holes");
10680                         ret = -EINVAL;
10681                         goto out;
10682                 }
10683                 if (em->block_start == EXTENT_MAP_INLINE) {
10684                         /*
10685                          * It's unlikely we'll ever actually find ourselves
10686                          * here, as a file small enough to fit inline won't be
10687                          * big enough to store more than the swap header, but in
10688                          * case something changes in the future, let's catch it
10689                          * here rather than later.
10690                          */
10691                         btrfs_warn(fs_info, "swapfile must not be inline");
10692                         ret = -EINVAL;
10693                         goto out;
10694                 }
10695                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10696                         btrfs_warn(fs_info, "swapfile must not be compressed");
10697                         ret = -EINVAL;
10698                         goto out;
10699                 }
10700
10701                 logical_block_start = em->block_start + (start - em->start);
10702                 len = min(len, em->len - (start - em->start));
10703                 free_extent_map(em);
10704                 em = NULL;
10705
10706                 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, false, true);
10707                 if (ret < 0) {
10708                         goto out;
10709                 } else if (ret) {
10710                         ret = 0;
10711                 } else {
10712                         btrfs_warn(fs_info,
10713                                    "swapfile must not be copy-on-write");
10714                         ret = -EINVAL;
10715                         goto out;
10716                 }
10717
10718                 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10719                 if (IS_ERR(em)) {
10720                         ret = PTR_ERR(em);
10721                         goto out;
10722                 }
10723
10724                 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10725                         btrfs_warn(fs_info,
10726                                    "swapfile must have single data profile");
10727                         ret = -EINVAL;
10728                         goto out;
10729                 }
10730
10731                 if (device == NULL) {
10732                         device = em->map_lookup->stripes[0].dev;
10733                         ret = btrfs_add_swapfile_pin(inode, device, false);
10734                         if (ret == 1)
10735                                 ret = 0;
10736                         else if (ret)
10737                                 goto out;
10738                 } else if (device != em->map_lookup->stripes[0].dev) {
10739                         btrfs_warn(fs_info, "swapfile must be on one device");
10740                         ret = -EINVAL;
10741                         goto out;
10742                 }
10743
10744                 physical_block_start = (em->map_lookup->stripes[0].physical +
10745                                         (logical_block_start - em->start));
10746                 len = min(len, em->len - (logical_block_start - em->start));
10747                 free_extent_map(em);
10748                 em = NULL;
10749
10750                 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10751                 if (!bg) {
10752                         btrfs_warn(fs_info,
10753                            "could not find block group containing swapfile");
10754                         ret = -EINVAL;
10755                         goto out;
10756                 }
10757
10758                 if (!btrfs_inc_block_group_swap_extents(bg)) {
10759                         btrfs_warn(fs_info,
10760                            "block group for swapfile at %llu is read-only%s",
10761                            bg->start,
10762                            atomic_read(&fs_info->scrubs_running) ?
10763                                        " (scrub running)" : "");
10764                         btrfs_put_block_group(bg);
10765                         ret = -EINVAL;
10766                         goto out;
10767                 }
10768
10769                 ret = btrfs_add_swapfile_pin(inode, bg, true);
10770                 if (ret) {
10771                         btrfs_put_block_group(bg);
10772                         if (ret == 1)
10773                                 ret = 0;
10774                         else
10775                                 goto out;
10776                 }
10777
10778                 if (bsi.block_len &&
10779                     bsi.block_start + bsi.block_len == physical_block_start) {
10780                         bsi.block_len += len;
10781                 } else {
10782                         if (bsi.block_len) {
10783                                 ret = btrfs_add_swap_extent(sis, &bsi);
10784                                 if (ret)
10785                                         goto out;
10786                         }
10787                         bsi.start = start;
10788                         bsi.block_start = physical_block_start;
10789                         bsi.block_len = len;
10790                 }
10791
10792                 start += len;
10793         }
10794
10795         if (bsi.block_len)
10796                 ret = btrfs_add_swap_extent(sis, &bsi);
10797
10798 out:
10799         if (!IS_ERR_OR_NULL(em))
10800                 free_extent_map(em);
10801
10802         unlock_extent(io_tree, 0, isize - 1, &cached_state);
10803
10804         if (ret)
10805                 btrfs_swap_deactivate(file);
10806
10807         btrfs_drew_write_unlock(&root->snapshot_lock);
10808
10809         btrfs_exclop_finish(fs_info);
10810
10811         if (ret)
10812                 return ret;
10813
10814         if (device)
10815                 sis->bdev = device->bdev;
10816         *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10817         sis->max = bsi.nr_pages;
10818         sis->pages = bsi.nr_pages - 1;
10819         sis->highest_bit = bsi.nr_pages - 1;
10820         return bsi.nr_extents;
10821 }
10822 #else
10823 static void btrfs_swap_deactivate(struct file *file)
10824 {
10825 }
10826
10827 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10828                                sector_t *span)
10829 {
10830         return -EOPNOTSUPP;
10831 }
10832 #endif
10833
10834 /*
10835  * Update the number of bytes used in the VFS' inode. When we replace extents in
10836  * a range (clone, dedupe, fallocate's zero range), we must update the number of
10837  * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10838  * always get a correct value.
10839  */
10840 void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10841                               const u64 add_bytes,
10842                               const u64 del_bytes)
10843 {
10844         if (add_bytes == del_bytes)
10845                 return;
10846
10847         spin_lock(&inode->lock);
10848         if (del_bytes > 0)
10849                 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10850         if (add_bytes > 0)
10851                 inode_add_bytes(&inode->vfs_inode, add_bytes);
10852         spin_unlock(&inode->lock);
10853 }
10854
10855 /*
10856  * Verify that there are no ordered extents for a given file range.
10857  *
10858  * @inode:   The target inode.
10859  * @start:   Start offset of the file range, should be sector size aligned.
10860  * @end:     End offset (inclusive) of the file range, its value +1 should be
10861  *           sector size aligned.
10862  *
10863  * This should typically be used for cases where we locked an inode's VFS lock in
10864  * exclusive mode, we have also locked the inode's i_mmap_lock in exclusive mode,
10865  * we have flushed all delalloc in the range, we have waited for all ordered
10866  * extents in the range to complete and finally we have locked the file range in
10867  * the inode's io_tree.
10868  */
10869 void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end)
10870 {
10871         struct btrfs_root *root = inode->root;
10872         struct btrfs_ordered_extent *ordered;
10873
10874         if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))
10875                 return;
10876
10877         ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start);
10878         if (ordered) {
10879                 btrfs_err(root->fs_info,
10880 "found unexpected ordered extent in file range [%llu, %llu] for inode %llu root %llu (ordered range [%llu, %llu])",
10881                           start, end, btrfs_ino(inode), root->root_key.objectid,
10882                           ordered->file_offset,
10883                           ordered->file_offset + ordered->num_bytes - 1);
10884                 btrfs_put_ordered_extent(ordered);
10885         }
10886
10887         ASSERT(ordered == NULL);
10888 }
10889
10890 static const struct inode_operations btrfs_dir_inode_operations = {
10891         .getattr        = btrfs_getattr,
10892         .lookup         = btrfs_lookup,
10893         .create         = btrfs_create,
10894         .unlink         = btrfs_unlink,
10895         .link           = btrfs_link,
10896         .mkdir          = btrfs_mkdir,
10897         .rmdir          = btrfs_rmdir,
10898         .rename         = btrfs_rename2,
10899         .symlink        = btrfs_symlink,
10900         .setattr        = btrfs_setattr,
10901         .mknod          = btrfs_mknod,
10902         .listxattr      = btrfs_listxattr,
10903         .permission     = btrfs_permission,
10904         .get_inode_acl  = btrfs_get_acl,
10905         .set_acl        = btrfs_set_acl,
10906         .update_time    = btrfs_update_time,
10907         .tmpfile        = btrfs_tmpfile,
10908         .fileattr_get   = btrfs_fileattr_get,
10909         .fileattr_set   = btrfs_fileattr_set,
10910 };
10911
10912 static const struct file_operations btrfs_dir_file_operations = {
10913         .llseek         = generic_file_llseek,
10914         .read           = generic_read_dir,
10915         .iterate_shared = btrfs_real_readdir,
10916         .open           = btrfs_opendir,
10917         .unlocked_ioctl = btrfs_ioctl,
10918 #ifdef CONFIG_COMPAT
10919         .compat_ioctl   = btrfs_compat_ioctl,
10920 #endif
10921         .release        = btrfs_release_file,
10922         .fsync          = btrfs_sync_file,
10923 };
10924
10925 /*
10926  * btrfs doesn't support the bmap operation because swapfiles
10927  * use bmap to make a mapping of extents in the file.  They assume
10928  * these extents won't change over the life of the file and they
10929  * use the bmap result to do IO directly to the drive.
10930  *
10931  * the btrfs bmap call would return logical addresses that aren't
10932  * suitable for IO and they also will change frequently as COW
10933  * operations happen.  So, swapfile + btrfs == corruption.
10934  *
10935  * For now we're avoiding this by dropping bmap.
10936  */
10937 static const struct address_space_operations btrfs_aops = {
10938         .read_folio     = btrfs_read_folio,
10939         .writepages     = btrfs_writepages,
10940         .readahead      = btrfs_readahead,
10941         .invalidate_folio = btrfs_invalidate_folio,
10942         .release_folio  = btrfs_release_folio,
10943         .migrate_folio  = btrfs_migrate_folio,
10944         .dirty_folio    = filemap_dirty_folio,
10945         .error_remove_page = generic_error_remove_page,
10946         .swap_activate  = btrfs_swap_activate,
10947         .swap_deactivate = btrfs_swap_deactivate,
10948 };
10949
10950 static const struct inode_operations btrfs_file_inode_operations = {
10951         .getattr        = btrfs_getattr,
10952         .setattr        = btrfs_setattr,
10953         .listxattr      = btrfs_listxattr,
10954         .permission     = btrfs_permission,
10955         .fiemap         = btrfs_fiemap,
10956         .get_inode_acl  = btrfs_get_acl,
10957         .set_acl        = btrfs_set_acl,
10958         .update_time    = btrfs_update_time,
10959         .fileattr_get   = btrfs_fileattr_get,
10960         .fileattr_set   = btrfs_fileattr_set,
10961 };
10962 static const struct inode_operations btrfs_special_inode_operations = {
10963         .getattr        = btrfs_getattr,
10964         .setattr        = btrfs_setattr,
10965         .permission     = btrfs_permission,
10966         .listxattr      = btrfs_listxattr,
10967         .get_inode_acl  = btrfs_get_acl,
10968         .set_acl        = btrfs_set_acl,
10969         .update_time    = btrfs_update_time,
10970 };
10971 static const struct inode_operations btrfs_symlink_inode_operations = {
10972         .get_link       = page_get_link,
10973         .getattr        = btrfs_getattr,
10974         .setattr        = btrfs_setattr,
10975         .permission     = btrfs_permission,
10976         .listxattr      = btrfs_listxattr,
10977         .update_time    = btrfs_update_time,
10978 };
10979
10980 const struct dentry_operations btrfs_dentry_operations = {
10981         .d_delete       = btrfs_dentry_delete,
10982 };