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