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