inode.c revision e93ca491
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 <linux/fsverity.h>
36#include "misc.h"
37#include "ctree.h"
38#include "disk-io.h"
39#include "transaction.h"
40#include "btrfs_inode.h"
41#include "print-tree.h"
42#include "ordered-data.h"
43#include "xattr.h"
44#include "tree-log.h"
45#include "volumes.h"
46#include "compression.h"
47#include "locking.h"
48#include "free-space-cache.h"
49#include "props.h"
50#include "qgroup.h"
51#include "delalloc-space.h"
52#include "block-group.h"
53#include "space-info.h"
54#include "zoned.h"
55#include "subpage.h"
56
57struct btrfs_iget_args {
58	u64 ino;
59	struct btrfs_root *root;
60};
61
62struct btrfs_dio_data {
63	u64 reserve;
64	loff_t length;
65	ssize_t submitted;
66	struct extent_changeset *data_reserved;
67};
68
69static const struct inode_operations btrfs_dir_inode_operations;
70static const struct inode_operations btrfs_symlink_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
73static const struct address_space_operations btrfs_aops;
74static const struct file_operations btrfs_dir_file_operations;
75
76static struct kmem_cache *btrfs_inode_cachep;
77struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_path_cachep;
79struct kmem_cache *btrfs_free_space_cachep;
80struct kmem_cache *btrfs_free_space_bitmap_cachep;
81
82static int btrfs_setsize(struct inode *inode, struct iattr *attr);
83static int btrfs_truncate(struct inode *inode, bool skip_writeback);
84static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
85static noinline int cow_file_range(struct btrfs_inode *inode,
86				   struct page *locked_page,
87				   u64 start, u64 end, int *page_started,
88				   unsigned long *nr_written, int unlock);
89static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
90				       u64 len, u64 orig_start, u64 block_start,
91				       u64 block_len, u64 orig_block_len,
92				       u64 ram_bytes, int compress_type,
93				       int type);
94
95static void __endio_write_update_ordered(struct btrfs_inode *inode,
96					 const u64 offset, const u64 bytes,
97					 const bool uptodate);
98
99/*
100 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
101 *
102 * ilock_flags can have the following bit set:
103 *
104 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
105 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
106 *		     return -EAGAIN
107 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
108 */
109int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
110{
111	if (ilock_flags & BTRFS_ILOCK_SHARED) {
112		if (ilock_flags & BTRFS_ILOCK_TRY) {
113			if (!inode_trylock_shared(inode))
114				return -EAGAIN;
115			else
116				return 0;
117		}
118		inode_lock_shared(inode);
119	} else {
120		if (ilock_flags & BTRFS_ILOCK_TRY) {
121			if (!inode_trylock(inode))
122				return -EAGAIN;
123			else
124				return 0;
125		}
126		inode_lock(inode);
127	}
128	if (ilock_flags & BTRFS_ILOCK_MMAP)
129		down_write(&BTRFS_I(inode)->i_mmap_lock);
130	return 0;
131}
132
133/*
134 * btrfs_inode_unlock - unock inode i_rwsem
135 *
136 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
137 * to decide whether the lock acquired is shared or exclusive.
138 */
139void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
140{
141	if (ilock_flags & BTRFS_ILOCK_MMAP)
142		up_write(&BTRFS_I(inode)->i_mmap_lock);
143	if (ilock_flags & BTRFS_ILOCK_SHARED)
144		inode_unlock_shared(inode);
145	else
146		inode_unlock(inode);
147}
148
149/*
150 * Cleanup all submitted ordered extents in specified range to handle errors
151 * from the btrfs_run_delalloc_range() callback.
152 *
153 * NOTE: caller must ensure that when an error happens, it can not call
154 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
155 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
156 * to be released, which we want to happen only when finishing the ordered
157 * extent (btrfs_finish_ordered_io()).
158 */
159static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
160						 struct page *locked_page,
161						 u64 offset, u64 bytes)
162{
163	unsigned long index = offset >> PAGE_SHIFT;
164	unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
165	u64 page_start = page_offset(locked_page);
166	u64 page_end = page_start + PAGE_SIZE - 1;
167
168	struct page *page;
169
170	while (index <= end_index) {
171		/*
172		 * For locked page, we will call end_extent_writepage() on it
173		 * in run_delalloc_range() for the error handling.  That
174		 * end_extent_writepage() function will call
175		 * btrfs_mark_ordered_io_finished() to clear page Ordered and
176		 * run the ordered extent accounting.
177		 *
178		 * Here we can't just clear the Ordered bit, or
179		 * btrfs_mark_ordered_io_finished() would skip the accounting
180		 * for the page range, and the ordered extent will never finish.
181		 */
182		if (index == (page_offset(locked_page) >> PAGE_SHIFT)) {
183			index++;
184			continue;
185		}
186		page = find_get_page(inode->vfs_inode.i_mapping, index);
187		index++;
188		if (!page)
189			continue;
190
191		/*
192		 * Here we just clear all Ordered bits for every page in the
193		 * range, then __endio_write_update_ordered() will handle
194		 * the ordered extent accounting for the range.
195		 */
196		btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
197					       offset, bytes);
198		put_page(page);
199	}
200
201	/* The locked page covers the full range, nothing needs to be done */
202	if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE)
203		return;
204	/*
205	 * In case this page belongs to the delalloc range being instantiated
206	 * then skip it, since the first page of a range is going to be
207	 * properly cleaned up by the caller of run_delalloc_range
208	 */
209	if (page_start >= offset && page_end <= (offset + bytes - 1)) {
210		bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
211		offset = page_offset(locked_page) + PAGE_SIZE;
212	}
213
214	return __endio_write_update_ordered(inode, offset, bytes, false);
215}
216
217static int btrfs_dirty_inode(struct inode *inode);
218
219static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
220				     struct inode *inode,  struct inode *dir,
221				     const struct qstr *qstr)
222{
223	int err;
224
225	err = btrfs_init_acl(trans, inode, dir);
226	if (!err)
227		err = btrfs_xattr_security_init(trans, inode, dir, qstr);
228	return err;
229}
230
231/*
232 * this does all the hard work for inserting an inline extent into
233 * the btree.  The caller should have done a btrfs_drop_extents so that
234 * no overlapping inline items exist in the btree
235 */
236static int insert_inline_extent(struct btrfs_trans_handle *trans,
237				struct btrfs_path *path, bool extent_inserted,
238				struct btrfs_root *root, struct inode *inode,
239				u64 start, size_t size, size_t compressed_size,
240				int compress_type,
241				struct page **compressed_pages)
242{
243	struct extent_buffer *leaf;
244	struct page *page = NULL;
245	char *kaddr;
246	unsigned long ptr;
247	struct btrfs_file_extent_item *ei;
248	int ret;
249	size_t cur_size = size;
250	unsigned long offset;
251
252	ASSERT((compressed_size > 0 && compressed_pages) ||
253	       (compressed_size == 0 && !compressed_pages));
254
255	if (compressed_size && compressed_pages)
256		cur_size = compressed_size;
257
258	if (!extent_inserted) {
259		struct btrfs_key key;
260		size_t datasize;
261
262		key.objectid = btrfs_ino(BTRFS_I(inode));
263		key.offset = start;
264		key.type = BTRFS_EXTENT_DATA_KEY;
265
266		datasize = btrfs_file_extent_calc_inline_size(cur_size);
267		ret = btrfs_insert_empty_item(trans, root, path, &key,
268					      datasize);
269		if (ret)
270			goto fail;
271	}
272	leaf = path->nodes[0];
273	ei = btrfs_item_ptr(leaf, path->slots[0],
274			    struct btrfs_file_extent_item);
275	btrfs_set_file_extent_generation(leaf, ei, trans->transid);
276	btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
277	btrfs_set_file_extent_encryption(leaf, ei, 0);
278	btrfs_set_file_extent_other_encoding(leaf, ei, 0);
279	btrfs_set_file_extent_ram_bytes(leaf, ei, size);
280	ptr = btrfs_file_extent_inline_start(ei);
281
282	if (compress_type != BTRFS_COMPRESS_NONE) {
283		struct page *cpage;
284		int i = 0;
285		while (compressed_size > 0) {
286			cpage = compressed_pages[i];
287			cur_size = min_t(unsigned long, compressed_size,
288				       PAGE_SIZE);
289
290			kaddr = page_address(cpage);
291			write_extent_buffer(leaf, kaddr, ptr, cur_size);
292
293			i++;
294			ptr += cur_size;
295			compressed_size -= cur_size;
296		}
297		btrfs_set_file_extent_compression(leaf, ei,
298						  compress_type);
299	} else {
300		page = find_get_page(inode->i_mapping,
301				     start >> PAGE_SHIFT);
302		btrfs_set_file_extent_compression(leaf, ei, 0);
303		kaddr = kmap_atomic(page);
304		offset = offset_in_page(start);
305		write_extent_buffer(leaf, kaddr + offset, ptr, size);
306		kunmap_atomic(kaddr);
307		put_page(page);
308	}
309	btrfs_mark_buffer_dirty(leaf);
310	btrfs_release_path(path);
311
312	/*
313	 * We align size to sectorsize for inline extents just for simplicity
314	 * sake.
315	 */
316	size = ALIGN(size, root->fs_info->sectorsize);
317	ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
318	if (ret)
319		goto fail;
320
321	/*
322	 * we're an inline extent, so nobody can
323	 * extend the file past i_size without locking
324	 * a page we already have locked.
325	 *
326	 * We must do any isize and inode updates
327	 * before we unlock the pages.  Otherwise we
328	 * could end up racing with unlink.
329	 */
330	BTRFS_I(inode)->disk_i_size = inode->i_size;
331fail:
332	return ret;
333}
334
335
336/*
337 * conditionally insert an inline extent into the file.  This
338 * does the checks required to make sure the data is small enough
339 * to fit as an inline extent.
340 */
341static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
342					  u64 end, size_t compressed_size,
343					  int compress_type,
344					  struct page **compressed_pages)
345{
346	struct btrfs_drop_extents_args drop_args = { 0 };
347	struct btrfs_root *root = inode->root;
348	struct btrfs_fs_info *fs_info = root->fs_info;
349	struct btrfs_trans_handle *trans;
350	u64 isize = i_size_read(&inode->vfs_inode);
351	u64 actual_end = min(end + 1, isize);
352	u64 inline_len = actual_end - start;
353	u64 aligned_end = ALIGN(end, fs_info->sectorsize);
354	u64 data_len = inline_len;
355	int ret;
356	struct btrfs_path *path;
357
358	if (compressed_size)
359		data_len = compressed_size;
360
361	if (start > 0 ||
362	    actual_end > fs_info->sectorsize ||
363	    data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
364	    (!compressed_size &&
365	    (actual_end & (fs_info->sectorsize - 1)) == 0) ||
366	    end + 1 < isize ||
367	    data_len > fs_info->max_inline) {
368		return 1;
369	}
370
371	path = btrfs_alloc_path();
372	if (!path)
373		return -ENOMEM;
374
375	trans = btrfs_join_transaction(root);
376	if (IS_ERR(trans)) {
377		btrfs_free_path(path);
378		return PTR_ERR(trans);
379	}
380	trans->block_rsv = &inode->block_rsv;
381
382	drop_args.path = path;
383	drop_args.start = start;
384	drop_args.end = aligned_end;
385	drop_args.drop_cache = true;
386	drop_args.replace_extent = true;
387
388	if (compressed_size && compressed_pages)
389		drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
390		   compressed_size);
391	else
392		drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
393		    inline_len);
394
395	ret = btrfs_drop_extents(trans, root, inode, &drop_args);
396	if (ret) {
397		btrfs_abort_transaction(trans, ret);
398		goto out;
399	}
400
401	if (isize > actual_end)
402		inline_len = min_t(u64, isize, actual_end);
403	ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
404				   root, &inode->vfs_inode, start,
405				   inline_len, compressed_size,
406				   compress_type, compressed_pages);
407	if (ret && ret != -ENOSPC) {
408		btrfs_abort_transaction(trans, ret);
409		goto out;
410	} else if (ret == -ENOSPC) {
411		ret = 1;
412		goto out;
413	}
414
415	btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
416	ret = btrfs_update_inode(trans, root, inode);
417	if (ret && ret != -ENOSPC) {
418		btrfs_abort_transaction(trans, ret);
419		goto out;
420	} else if (ret == -ENOSPC) {
421		ret = 1;
422		goto out;
423	}
424
425	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
426out:
427	/*
428	 * Don't forget to free the reserved space, as for inlined extent
429	 * it won't count as data extent, free them directly here.
430	 * And at reserve time, it's always aligned to page size, so
431	 * just free one page here.
432	 */
433	btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
434	btrfs_free_path(path);
435	btrfs_end_transaction(trans);
436	return ret;
437}
438
439struct async_extent {
440	u64 start;
441	u64 ram_size;
442	u64 compressed_size;
443	struct page **pages;
444	unsigned long nr_pages;
445	int compress_type;
446	struct list_head list;
447};
448
449struct async_chunk {
450	struct inode *inode;
451	struct page *locked_page;
452	u64 start;
453	u64 end;
454	unsigned int write_flags;
455	struct list_head extents;
456	struct cgroup_subsys_state *blkcg_css;
457	struct btrfs_work work;
458	atomic_t *pending;
459};
460
461struct async_cow {
462	/* Number of chunks in flight; must be first in the structure */
463	atomic_t num_chunks;
464	struct async_chunk chunks[];
465};
466
467static noinline int add_async_extent(struct async_chunk *cow,
468				     u64 start, u64 ram_size,
469				     u64 compressed_size,
470				     struct page **pages,
471				     unsigned long nr_pages,
472				     int compress_type)
473{
474	struct async_extent *async_extent;
475
476	async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
477	BUG_ON(!async_extent); /* -ENOMEM */
478	async_extent->start = start;
479	async_extent->ram_size = ram_size;
480	async_extent->compressed_size = compressed_size;
481	async_extent->pages = pages;
482	async_extent->nr_pages = nr_pages;
483	async_extent->compress_type = compress_type;
484	list_add_tail(&async_extent->list, &cow->extents);
485	return 0;
486}
487
488/*
489 * Check if the inode has flags compatible with compression
490 */
491static inline bool inode_can_compress(struct btrfs_inode *inode)
492{
493	/* Subpage doesn't support compression yet */
494	if (inode->root->fs_info->sectorsize < PAGE_SIZE)
495		return false;
496	if (inode->flags & BTRFS_INODE_NODATACOW ||
497	    inode->flags & BTRFS_INODE_NODATASUM)
498		return false;
499	return true;
500}
501
502/*
503 * Check if the inode needs to be submitted to compression, based on mount
504 * options, defragmentation, properties or heuristics.
505 */
506static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
507				      u64 end)
508{
509	struct btrfs_fs_info *fs_info = inode->root->fs_info;
510
511	if (!inode_can_compress(inode)) {
512		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
513			KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
514			btrfs_ino(inode));
515		return 0;
516	}
517	/* force compress */
518	if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
519		return 1;
520	/* defrag ioctl */
521	if (inode->defrag_compress)
522		return 1;
523	/* bad compression ratios */
524	if (inode->flags & BTRFS_INODE_NOCOMPRESS)
525		return 0;
526	if (btrfs_test_opt(fs_info, COMPRESS) ||
527	    inode->flags & BTRFS_INODE_COMPRESS ||
528	    inode->prop_compress)
529		return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
530	return 0;
531}
532
533static inline void inode_should_defrag(struct btrfs_inode *inode,
534		u64 start, u64 end, u64 num_bytes, u64 small_write)
535{
536	/* If this is a small write inside eof, kick off a defrag */
537	if (num_bytes < small_write &&
538	    (start > 0 || end + 1 < inode->disk_i_size))
539		btrfs_add_inode_defrag(NULL, inode);
540}
541
542/*
543 * we create compressed extents in two phases.  The first
544 * phase compresses a range of pages that have already been
545 * locked (both pages and state bits are locked).
546 *
547 * This is done inside an ordered work queue, and the compression
548 * is spread across many cpus.  The actual IO submission is step
549 * two, and the ordered work queue takes care of making sure that
550 * happens in the same order things were put onto the queue by
551 * writepages and friends.
552 *
553 * If this code finds it can't get good compression, it puts an
554 * entry onto the work queue to write the uncompressed bytes.  This
555 * makes sure that both compressed inodes and uncompressed inodes
556 * are written in the same order that the flusher thread sent them
557 * down.
558 */
559static noinline int compress_file_range(struct async_chunk *async_chunk)
560{
561	struct inode *inode = async_chunk->inode;
562	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
563	u64 blocksize = fs_info->sectorsize;
564	u64 start = async_chunk->start;
565	u64 end = async_chunk->end;
566	u64 actual_end;
567	u64 i_size;
568	int ret = 0;
569	struct page **pages = NULL;
570	unsigned long nr_pages;
571	unsigned long total_compressed = 0;
572	unsigned long total_in = 0;
573	int i;
574	int will_compress;
575	int compress_type = fs_info->compress_type;
576	int compressed_extents = 0;
577	int redirty = 0;
578
579	inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
580			SZ_16K);
581
582	/*
583	 * We need to save i_size before now because it could change in between
584	 * us evaluating the size and assigning it.  This is because we lock and
585	 * unlock the page in truncate and fallocate, and then modify the i_size
586	 * later on.
587	 *
588	 * The barriers are to emulate READ_ONCE, remove that once i_size_read
589	 * does that for us.
590	 */
591	barrier();
592	i_size = i_size_read(inode);
593	barrier();
594	actual_end = min_t(u64, i_size, end + 1);
595again:
596	will_compress = 0;
597	nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
598	BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
599	nr_pages = min_t(unsigned long, nr_pages,
600			BTRFS_MAX_COMPRESSED / PAGE_SIZE);
601
602	/*
603	 * we don't want to send crud past the end of i_size through
604	 * compression, that's just a waste of CPU time.  So, if the
605	 * end of the file is before the start of our current
606	 * requested range of bytes, we bail out to the uncompressed
607	 * cleanup code that can deal with all of this.
608	 *
609	 * It isn't really the fastest way to fix things, but this is a
610	 * very uncommon corner.
611	 */
612	if (actual_end <= start)
613		goto cleanup_and_bail_uncompressed;
614
615	total_compressed = actual_end - start;
616
617	/*
618	 * skip compression for a small file range(<=blocksize) that
619	 * isn't an inline extent, since it doesn't save disk space at all.
620	 */
621	if (total_compressed <= blocksize &&
622	   (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
623		goto cleanup_and_bail_uncompressed;
624
625	total_compressed = min_t(unsigned long, total_compressed,
626			BTRFS_MAX_UNCOMPRESSED);
627	total_in = 0;
628	ret = 0;
629
630	/*
631	 * we do compression for mount -o compress and when the
632	 * inode has not been flagged as nocompress.  This flag can
633	 * change at any time if we discover bad compression ratios.
634	 */
635	if (nr_pages > 1 && inode_need_compress(BTRFS_I(inode), start, end)) {
636		WARN_ON(pages);
637		pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
638		if (!pages) {
639			/* just bail out to the uncompressed code */
640			nr_pages = 0;
641			goto cont;
642		}
643
644		if (BTRFS_I(inode)->defrag_compress)
645			compress_type = BTRFS_I(inode)->defrag_compress;
646		else if (BTRFS_I(inode)->prop_compress)
647			compress_type = BTRFS_I(inode)->prop_compress;
648
649		/*
650		 * we need to call clear_page_dirty_for_io on each
651		 * page in the range.  Otherwise applications with the file
652		 * mmap'd can wander in and change the page contents while
653		 * we are compressing them.
654		 *
655		 * If the compression fails for any reason, we set the pages
656		 * dirty again later on.
657		 *
658		 * Note that the remaining part is redirtied, the start pointer
659		 * has moved, the end is the original one.
660		 */
661		if (!redirty) {
662			extent_range_clear_dirty_for_io(inode, start, end);
663			redirty = 1;
664		}
665
666		/* Compression level is applied here and only here */
667		ret = btrfs_compress_pages(
668			compress_type | (fs_info->compress_level << 4),
669					   inode->i_mapping, start,
670					   pages,
671					   &nr_pages,
672					   &total_in,
673					   &total_compressed);
674
675		if (!ret) {
676			unsigned long offset = offset_in_page(total_compressed);
677			struct page *page = pages[nr_pages - 1];
678
679			/* zero the tail end of the last page, we might be
680			 * sending it down to disk
681			 */
682			if (offset)
683				memzero_page(page, offset, PAGE_SIZE - offset);
684			will_compress = 1;
685		}
686	}
687cont:
688	/*
689	 * Check cow_file_range() for why we don't even try to create inline
690	 * extent for subpage case.
691	 */
692	if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
693		/* lets try to make an inline extent */
694		if (ret || total_in < actual_end) {
695			/* we didn't compress the entire range, try
696			 * to make an uncompressed inline extent.
697			 */
698			ret = cow_file_range_inline(BTRFS_I(inode), start, end,
699						    0, BTRFS_COMPRESS_NONE,
700						    NULL);
701		} else {
702			/* try making a compressed inline extent */
703			ret = cow_file_range_inline(BTRFS_I(inode), start, end,
704						    total_compressed,
705						    compress_type, pages);
706		}
707		if (ret <= 0) {
708			unsigned long clear_flags = EXTENT_DELALLOC |
709				EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
710				EXTENT_DO_ACCOUNTING;
711			unsigned long page_error_op;
712
713			page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
714
715			/*
716			 * inline extent creation worked or returned error,
717			 * we don't need to create any more async work items.
718			 * Unlock and free up our temp pages.
719			 *
720			 * We use DO_ACCOUNTING here because we need the
721			 * delalloc_release_metadata to be done _after_ we drop
722			 * our outstanding extent for clearing delalloc for this
723			 * range.
724			 */
725			extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
726						     NULL,
727						     clear_flags,
728						     PAGE_UNLOCK |
729						     PAGE_START_WRITEBACK |
730						     page_error_op |
731						     PAGE_END_WRITEBACK);
732
733			/*
734			 * Ensure we only free the compressed pages if we have
735			 * them allocated, as we can still reach here with
736			 * inode_need_compress() == false.
737			 */
738			if (pages) {
739				for (i = 0; i < nr_pages; i++) {
740					WARN_ON(pages[i]->mapping);
741					put_page(pages[i]);
742				}
743				kfree(pages);
744			}
745			return 0;
746		}
747	}
748
749	if (will_compress) {
750		/*
751		 * we aren't doing an inline extent round the compressed size
752		 * up to a block size boundary so the allocator does sane
753		 * things
754		 */
755		total_compressed = ALIGN(total_compressed, blocksize);
756
757		/*
758		 * one last check to make sure the compression is really a
759		 * win, compare the page count read with the blocks on disk,
760		 * compression must free at least one sector size
761		 */
762		total_in = ALIGN(total_in, PAGE_SIZE);
763		if (total_compressed + blocksize <= total_in) {
764			compressed_extents++;
765
766			/*
767			 * The async work queues will take care of doing actual
768			 * allocation on disk for these compressed pages, and
769			 * will submit them to the elevator.
770			 */
771			add_async_extent(async_chunk, start, total_in,
772					total_compressed, pages, nr_pages,
773					compress_type);
774
775			if (start + total_in < end) {
776				start += total_in;
777				pages = NULL;
778				cond_resched();
779				goto again;
780			}
781			return compressed_extents;
782		}
783	}
784	if (pages) {
785		/*
786		 * the compression code ran but failed to make things smaller,
787		 * free any pages it allocated and our page pointer array
788		 */
789		for (i = 0; i < nr_pages; i++) {
790			WARN_ON(pages[i]->mapping);
791			put_page(pages[i]);
792		}
793		kfree(pages);
794		pages = NULL;
795		total_compressed = 0;
796		nr_pages = 0;
797
798		/* flag the file so we don't compress in the future */
799		if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
800		    !(BTRFS_I(inode)->prop_compress)) {
801			BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
802		}
803	}
804cleanup_and_bail_uncompressed:
805	/*
806	 * No compression, but we still need to write the pages in the file
807	 * we've been given so far.  redirty the locked page if it corresponds
808	 * to our extent and set things up for the async work queue to run
809	 * cow_file_range to do the normal delalloc dance.
810	 */
811	if (async_chunk->locked_page &&
812	    (page_offset(async_chunk->locked_page) >= start &&
813	     page_offset(async_chunk->locked_page)) <= end) {
814		__set_page_dirty_nobuffers(async_chunk->locked_page);
815		/* unlocked later on in the async handlers */
816	}
817
818	if (redirty)
819		extent_range_redirty_for_io(inode, start, end);
820	add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
821			 BTRFS_COMPRESS_NONE);
822	compressed_extents++;
823
824	return compressed_extents;
825}
826
827static void free_async_extent_pages(struct async_extent *async_extent)
828{
829	int i;
830
831	if (!async_extent->pages)
832		return;
833
834	for (i = 0; i < async_extent->nr_pages; i++) {
835		WARN_ON(async_extent->pages[i]->mapping);
836		put_page(async_extent->pages[i]);
837	}
838	kfree(async_extent->pages);
839	async_extent->nr_pages = 0;
840	async_extent->pages = NULL;
841}
842
843/*
844 * phase two of compressed writeback.  This is the ordered portion
845 * of the code, which only gets called in the order the work was
846 * queued.  We walk all the async extents created by compress_file_range
847 * and send them down to the disk.
848 */
849static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
850{
851	struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
852	struct btrfs_fs_info *fs_info = inode->root->fs_info;
853	struct async_extent *async_extent;
854	u64 alloc_hint = 0;
855	struct btrfs_key ins;
856	struct extent_map *em;
857	struct btrfs_root *root = inode->root;
858	struct extent_io_tree *io_tree = &inode->io_tree;
859	int ret = 0;
860
861again:
862	while (!list_empty(&async_chunk->extents)) {
863		async_extent = list_entry(async_chunk->extents.next,
864					  struct async_extent, list);
865		list_del(&async_extent->list);
866
867retry:
868		lock_extent(io_tree, async_extent->start,
869			    async_extent->start + async_extent->ram_size - 1);
870		/* did the compression code fall back to uncompressed IO? */
871		if (!async_extent->pages) {
872			int page_started = 0;
873			unsigned long nr_written = 0;
874
875			/* allocate blocks */
876			ret = cow_file_range(inode, async_chunk->locked_page,
877					     async_extent->start,
878					     async_extent->start +
879					     async_extent->ram_size - 1,
880					     &page_started, &nr_written, 0);
881
882			/* JDM XXX */
883
884			/*
885			 * if page_started, cow_file_range inserted an
886			 * inline extent and took care of all the unlocking
887			 * and IO for us.  Otherwise, we need to submit
888			 * all those pages down to the drive.
889			 */
890			if (!page_started && !ret)
891				extent_write_locked_range(&inode->vfs_inode,
892						  async_extent->start,
893						  async_extent->start +
894						  async_extent->ram_size - 1,
895						  WB_SYNC_ALL);
896			else if (ret && async_chunk->locked_page)
897				unlock_page(async_chunk->locked_page);
898			kfree(async_extent);
899			cond_resched();
900			continue;
901		}
902
903		ret = btrfs_reserve_extent(root, async_extent->ram_size,
904					   async_extent->compressed_size,
905					   async_extent->compressed_size,
906					   0, alloc_hint, &ins, 1, 1);
907		if (ret) {
908			free_async_extent_pages(async_extent);
909
910			if (ret == -ENOSPC) {
911				unlock_extent(io_tree, async_extent->start,
912					      async_extent->start +
913					      async_extent->ram_size - 1);
914
915				/*
916				 * we need to redirty the pages if we decide to
917				 * fallback to uncompressed IO, otherwise we
918				 * will not submit these pages down to lower
919				 * layers.
920				 */
921				extent_range_redirty_for_io(&inode->vfs_inode,
922						async_extent->start,
923						async_extent->start +
924						async_extent->ram_size - 1);
925
926				goto retry;
927			}
928			goto out_free;
929		}
930		/*
931		 * here we're doing allocation and writeback of the
932		 * compressed pages
933		 */
934		em = create_io_em(inode, async_extent->start,
935				  async_extent->ram_size, /* len */
936				  async_extent->start, /* orig_start */
937				  ins.objectid, /* block_start */
938				  ins.offset, /* block_len */
939				  ins.offset, /* orig_block_len */
940				  async_extent->ram_size, /* ram_bytes */
941				  async_extent->compress_type,
942				  BTRFS_ORDERED_COMPRESSED);
943		if (IS_ERR(em))
944			/* ret value is not necessary due to void function */
945			goto out_free_reserve;
946		free_extent_map(em);
947
948		ret = btrfs_add_ordered_extent_compress(inode,
949						async_extent->start,
950						ins.objectid,
951						async_extent->ram_size,
952						ins.offset,
953						async_extent->compress_type);
954		if (ret) {
955			btrfs_drop_extent_cache(inode, async_extent->start,
956						async_extent->start +
957						async_extent->ram_size - 1, 0);
958			goto out_free_reserve;
959		}
960		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
961
962		/*
963		 * clear dirty, set writeback and unlock the pages.
964		 */
965		extent_clear_unlock_delalloc(inode, async_extent->start,
966				async_extent->start +
967				async_extent->ram_size - 1,
968				NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
969				PAGE_UNLOCK | PAGE_START_WRITEBACK);
970		if (btrfs_submit_compressed_write(inode, async_extent->start,
971				    async_extent->ram_size,
972				    ins.objectid,
973				    ins.offset, async_extent->pages,
974				    async_extent->nr_pages,
975				    async_chunk->write_flags,
976				    async_chunk->blkcg_css)) {
977			struct page *p = async_extent->pages[0];
978			const u64 start = async_extent->start;
979			const u64 end = start + async_extent->ram_size - 1;
980
981			p->mapping = inode->vfs_inode.i_mapping;
982			btrfs_writepage_endio_finish_ordered(inode, p, start,
983							     end, false);
984
985			p->mapping = NULL;
986			extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
987						     PAGE_END_WRITEBACK |
988						     PAGE_SET_ERROR);
989			free_async_extent_pages(async_extent);
990		}
991		alloc_hint = ins.objectid + ins.offset;
992		kfree(async_extent);
993		cond_resched();
994	}
995	return;
996out_free_reserve:
997	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
998	btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
999out_free:
1000	extent_clear_unlock_delalloc(inode, async_extent->start,
1001				     async_extent->start +
1002				     async_extent->ram_size - 1,
1003				     NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
1004				     EXTENT_DELALLOC_NEW |
1005				     EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
1006				     PAGE_UNLOCK | PAGE_START_WRITEBACK |
1007				     PAGE_END_WRITEBACK | PAGE_SET_ERROR);
1008	free_async_extent_pages(async_extent);
1009	kfree(async_extent);
1010	goto again;
1011}
1012
1013static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
1014				      u64 num_bytes)
1015{
1016	struct extent_map_tree *em_tree = &inode->extent_tree;
1017	struct extent_map *em;
1018	u64 alloc_hint = 0;
1019
1020	read_lock(&em_tree->lock);
1021	em = search_extent_mapping(em_tree, start, num_bytes);
1022	if (em) {
1023		/*
1024		 * if block start isn't an actual block number then find the
1025		 * first block in this inode and use that as a hint.  If that
1026		 * block is also bogus then just don't worry about it.
1027		 */
1028		if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1029			free_extent_map(em);
1030			em = search_extent_mapping(em_tree, 0, 0);
1031			if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1032				alloc_hint = em->block_start;
1033			if (em)
1034				free_extent_map(em);
1035		} else {
1036			alloc_hint = em->block_start;
1037			free_extent_map(em);
1038		}
1039	}
1040	read_unlock(&em_tree->lock);
1041
1042	return alloc_hint;
1043}
1044
1045/*
1046 * when extent_io.c finds a delayed allocation range in the file,
1047 * the call backs end up in this code.  The basic idea is to
1048 * allocate extents on disk for the range, and create ordered data structs
1049 * in ram to track those extents.
1050 *
1051 * locked_page is the page that writepage had locked already.  We use
1052 * it to make sure we don't do extra locks or unlocks.
1053 *
1054 * *page_started is set to one if we unlock locked_page and do everything
1055 * required to start IO on it.  It may be clean and already done with
1056 * IO when we return.
1057 */
1058static noinline int cow_file_range(struct btrfs_inode *inode,
1059				   struct page *locked_page,
1060				   u64 start, u64 end, int *page_started,
1061				   unsigned long *nr_written, int unlock)
1062{
1063	struct btrfs_root *root = inode->root;
1064	struct btrfs_fs_info *fs_info = root->fs_info;
1065	u64 alloc_hint = 0;
1066	u64 num_bytes;
1067	unsigned long ram_size;
1068	u64 cur_alloc_size = 0;
1069	u64 min_alloc_size;
1070	u64 blocksize = fs_info->sectorsize;
1071	struct btrfs_key ins;
1072	struct extent_map *em;
1073	unsigned clear_bits;
1074	unsigned long page_ops;
1075	bool extent_reserved = false;
1076	int ret = 0;
1077
1078	if (btrfs_is_free_space_inode(inode)) {
1079		WARN_ON_ONCE(1);
1080		ret = -EINVAL;
1081		goto out_unlock;
1082	}
1083
1084	num_bytes = ALIGN(end - start + 1, blocksize);
1085	num_bytes = max(blocksize,  num_bytes);
1086	ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
1087
1088	inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
1089
1090	/*
1091	 * Due to the page size limit, for subpage we can only trigger the
1092	 * writeback for the dirty sectors of page, that means data writeback
1093	 * is doing more writeback than what we want.
1094	 *
1095	 * This is especially unexpected for some call sites like fallocate,
1096	 * where we only increase i_size after everything is done.
1097	 * This means we can trigger inline extent even if we didn't want to.
1098	 * So here we skip inline extent creation completely.
1099	 */
1100	if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
1101		/* lets try to make an inline extent */
1102		ret = cow_file_range_inline(inode, start, end, 0,
1103					    BTRFS_COMPRESS_NONE, NULL);
1104		if (ret == 0) {
1105			/*
1106			 * We use DO_ACCOUNTING here because we need the
1107			 * delalloc_release_metadata to be run _after_ we drop
1108			 * our outstanding extent for clearing delalloc for this
1109			 * range.
1110			 */
1111			extent_clear_unlock_delalloc(inode, start, end,
1112				     locked_page,
1113				     EXTENT_LOCKED | EXTENT_DELALLOC |
1114				     EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1115				     EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1116				     PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
1117			*nr_written = *nr_written +
1118			     (end - start + PAGE_SIZE) / PAGE_SIZE;
1119			*page_started = 1;
1120			/*
1121			 * locked_page is locked by the caller of
1122			 * writepage_delalloc(), not locked by
1123			 * __process_pages_contig().
1124			 *
1125			 * We can't let __process_pages_contig() to unlock it,
1126			 * as it doesn't have any subpage::writers recorded.
1127			 *
1128			 * Here we manually unlock the page, since the caller
1129			 * can't use page_started to determine if it's an
1130			 * inline extent or a compressed extent.
1131			 */
1132			unlock_page(locked_page);
1133			goto out;
1134		} else if (ret < 0) {
1135			goto out_unlock;
1136		}
1137	}
1138
1139	alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1140	btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
1141
1142	/*
1143	 * Relocation relies on the relocated extents to have exactly the same
1144	 * size as the original extents. Normally writeback for relocation data
1145	 * extents follows a NOCOW path because relocation preallocates the
1146	 * extents. However, due to an operation such as scrub turning a block
1147	 * group to RO mode, it may fallback to COW mode, so we must make sure
1148	 * an extent allocated during COW has exactly the requested size and can
1149	 * not be split into smaller extents, otherwise relocation breaks and
1150	 * fails during the stage where it updates the bytenr of file extent
1151	 * items.
1152	 */
1153	if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1154		min_alloc_size = num_bytes;
1155	else
1156		min_alloc_size = fs_info->sectorsize;
1157
1158	while (num_bytes > 0) {
1159		cur_alloc_size = num_bytes;
1160		ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1161					   min_alloc_size, 0, alloc_hint,
1162					   &ins, 1, 1);
1163		if (ret < 0)
1164			goto out_unlock;
1165		cur_alloc_size = ins.offset;
1166		extent_reserved = true;
1167
1168		ram_size = ins.offset;
1169		em = create_io_em(inode, start, ins.offset, /* len */
1170				  start, /* orig_start */
1171				  ins.objectid, /* block_start */
1172				  ins.offset, /* block_len */
1173				  ins.offset, /* orig_block_len */
1174				  ram_size, /* ram_bytes */
1175				  BTRFS_COMPRESS_NONE, /* compress_type */
1176				  BTRFS_ORDERED_REGULAR /* type */);
1177		if (IS_ERR(em)) {
1178			ret = PTR_ERR(em);
1179			goto out_reserve;
1180		}
1181		free_extent_map(em);
1182
1183		ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1184					       ram_size, cur_alloc_size,
1185					       BTRFS_ORDERED_REGULAR);
1186		if (ret)
1187			goto out_drop_extent_cache;
1188
1189		if (root->root_key.objectid ==
1190		    BTRFS_DATA_RELOC_TREE_OBJECTID) {
1191			ret = btrfs_reloc_clone_csums(inode, start,
1192						      cur_alloc_size);
1193			/*
1194			 * Only drop cache here, and process as normal.
1195			 *
1196			 * We must not allow extent_clear_unlock_delalloc()
1197			 * at out_unlock label to free meta of this ordered
1198			 * extent, as its meta should be freed by
1199			 * btrfs_finish_ordered_io().
1200			 *
1201			 * So we must continue until @start is increased to
1202			 * skip current ordered extent.
1203			 */
1204			if (ret)
1205				btrfs_drop_extent_cache(inode, start,
1206						start + ram_size - 1, 0);
1207		}
1208
1209		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1210
1211		/*
1212		 * We're not doing compressed IO, don't unlock the first page
1213		 * (which the caller expects to stay locked), don't clear any
1214		 * dirty bits and don't set any writeback bits
1215		 *
1216		 * Do set the Ordered (Private2) bit so we know this page was
1217		 * properly setup for writepage.
1218		 */
1219		page_ops = unlock ? PAGE_UNLOCK : 0;
1220		page_ops |= PAGE_SET_ORDERED;
1221
1222		extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
1223					     locked_page,
1224					     EXTENT_LOCKED | EXTENT_DELALLOC,
1225					     page_ops);
1226		if (num_bytes < cur_alloc_size)
1227			num_bytes = 0;
1228		else
1229			num_bytes -= cur_alloc_size;
1230		alloc_hint = ins.objectid + ins.offset;
1231		start += cur_alloc_size;
1232		extent_reserved = false;
1233
1234		/*
1235		 * btrfs_reloc_clone_csums() error, since start is increased
1236		 * extent_clear_unlock_delalloc() at out_unlock label won't
1237		 * free metadata of current ordered extent, we're OK to exit.
1238		 */
1239		if (ret)
1240			goto out_unlock;
1241	}
1242out:
1243	return ret;
1244
1245out_drop_extent_cache:
1246	btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1247out_reserve:
1248	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1249	btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1250out_unlock:
1251	clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1252		EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1253	page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
1254	/*
1255	 * If we reserved an extent for our delalloc range (or a subrange) and
1256	 * failed to create the respective ordered extent, then it means that
1257	 * when we reserved the extent we decremented the extent's size from
1258	 * the data space_info's bytes_may_use counter and incremented the
1259	 * space_info's bytes_reserved counter by the same amount. We must make
1260	 * sure extent_clear_unlock_delalloc() does not try to decrement again
1261	 * the data space_info's bytes_may_use counter, therefore we do not pass
1262	 * it the flag EXTENT_CLEAR_DATA_RESV.
1263	 */
1264	if (extent_reserved) {
1265		extent_clear_unlock_delalloc(inode, start,
1266					     start + cur_alloc_size - 1,
1267					     locked_page,
1268					     clear_bits,
1269					     page_ops);
1270		start += cur_alloc_size;
1271		if (start >= end)
1272			goto out;
1273	}
1274	extent_clear_unlock_delalloc(inode, start, end, locked_page,
1275				     clear_bits | EXTENT_CLEAR_DATA_RESV,
1276				     page_ops);
1277	goto out;
1278}
1279
1280/*
1281 * work queue call back to started compression on a file and pages
1282 */
1283static noinline void async_cow_start(struct btrfs_work *work)
1284{
1285	struct async_chunk *async_chunk;
1286	int compressed_extents;
1287
1288	async_chunk = container_of(work, struct async_chunk, work);
1289
1290	compressed_extents = compress_file_range(async_chunk);
1291	if (compressed_extents == 0) {
1292		btrfs_add_delayed_iput(async_chunk->inode);
1293		async_chunk->inode = NULL;
1294	}
1295}
1296
1297/*
1298 * work queue call back to submit previously compressed pages
1299 */
1300static noinline void async_cow_submit(struct btrfs_work *work)
1301{
1302	struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1303						     work);
1304	struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
1305	unsigned long nr_pages;
1306
1307	nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
1308		PAGE_SHIFT;
1309
1310	/*
1311	 * ->inode could be NULL if async_chunk_start has failed to compress,
1312	 * in which case we don't have anything to submit, yet we need to
1313	 * always adjust ->async_delalloc_pages as its paired with the init
1314	 * happening in cow_file_range_async
1315	 */
1316	if (async_chunk->inode)
1317		submit_compressed_extents(async_chunk);
1318
1319	/* atomic_sub_return implies a barrier */
1320	if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1321	    5 * SZ_1M)
1322		cond_wake_up_nomb(&fs_info->async_submit_wait);
1323}
1324
1325static noinline void async_cow_free(struct btrfs_work *work)
1326{
1327	struct async_chunk *async_chunk;
1328
1329	async_chunk = container_of(work, struct async_chunk, work);
1330	if (async_chunk->inode)
1331		btrfs_add_delayed_iput(async_chunk->inode);
1332	if (async_chunk->blkcg_css)
1333		css_put(async_chunk->blkcg_css);
1334	/*
1335	 * Since the pointer to 'pending' is at the beginning of the array of
1336	 * async_chunk's, freeing it ensures the whole array has been freed.
1337	 */
1338	if (atomic_dec_and_test(async_chunk->pending))
1339		kvfree(async_chunk->pending);
1340}
1341
1342static int cow_file_range_async(struct btrfs_inode *inode,
1343				struct writeback_control *wbc,
1344				struct page *locked_page,
1345				u64 start, u64 end, int *page_started,
1346				unsigned long *nr_written)
1347{
1348	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1349	struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
1350	struct async_cow *ctx;
1351	struct async_chunk *async_chunk;
1352	unsigned long nr_pages;
1353	u64 cur_end;
1354	u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1355	int i;
1356	bool should_compress;
1357	unsigned nofs_flag;
1358	const unsigned int write_flags = wbc_to_write_flags(wbc);
1359
1360	unlock_extent(&inode->io_tree, start, end);
1361
1362	if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
1363	    !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1364		num_chunks = 1;
1365		should_compress = false;
1366	} else {
1367		should_compress = true;
1368	}
1369
1370	nofs_flag = memalloc_nofs_save();
1371	ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1372	memalloc_nofs_restore(nofs_flag);
1373
1374	if (!ctx) {
1375		unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1376			EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1377			EXTENT_DO_ACCOUNTING;
1378		unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1379					 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
1380
1381		extent_clear_unlock_delalloc(inode, start, end, locked_page,
1382					     clear_bits, page_ops);
1383		return -ENOMEM;
1384	}
1385
1386	async_chunk = ctx->chunks;
1387	atomic_set(&ctx->num_chunks, num_chunks);
1388
1389	for (i = 0; i < num_chunks; i++) {
1390		if (should_compress)
1391			cur_end = min(end, start + SZ_512K - 1);
1392		else
1393			cur_end = end;
1394
1395		/*
1396		 * igrab is called higher up in the call chain, take only the
1397		 * lightweight reference for the callback lifetime
1398		 */
1399		ihold(&inode->vfs_inode);
1400		async_chunk[i].pending = &ctx->num_chunks;
1401		async_chunk[i].inode = &inode->vfs_inode;
1402		async_chunk[i].start = start;
1403		async_chunk[i].end = cur_end;
1404		async_chunk[i].write_flags = write_flags;
1405		INIT_LIST_HEAD(&async_chunk[i].extents);
1406
1407		/*
1408		 * The locked_page comes all the way from writepage and its
1409		 * the original page we were actually given.  As we spread
1410		 * this large delalloc region across multiple async_chunk
1411		 * structs, only the first struct needs a pointer to locked_page
1412		 *
1413		 * This way we don't need racey decisions about who is supposed
1414		 * to unlock it.
1415		 */
1416		if (locked_page) {
1417			/*
1418			 * Depending on the compressibility, the pages might or
1419			 * might not go through async.  We want all of them to
1420			 * be accounted against wbc once.  Let's do it here
1421			 * before the paths diverge.  wbc accounting is used
1422			 * only for foreign writeback detection and doesn't
1423			 * need full accuracy.  Just account the whole thing
1424			 * against the first page.
1425			 */
1426			wbc_account_cgroup_owner(wbc, locked_page,
1427						 cur_end - start);
1428			async_chunk[i].locked_page = locked_page;
1429			locked_page = NULL;
1430		} else {
1431			async_chunk[i].locked_page = NULL;
1432		}
1433
1434		if (blkcg_css != blkcg_root_css) {
1435			css_get(blkcg_css);
1436			async_chunk[i].blkcg_css = blkcg_css;
1437		} else {
1438			async_chunk[i].blkcg_css = NULL;
1439		}
1440
1441		btrfs_init_work(&async_chunk[i].work, async_cow_start,
1442				async_cow_submit, async_cow_free);
1443
1444		nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
1445		atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1446
1447		btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
1448
1449		*nr_written += nr_pages;
1450		start = cur_end + 1;
1451	}
1452	*page_started = 1;
1453	return 0;
1454}
1455
1456static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1457				       struct page *locked_page, u64 start,
1458				       u64 end, int *page_started,
1459				       unsigned long *nr_written)
1460{
1461	int ret;
1462
1463	ret = cow_file_range(inode, locked_page, start, end, page_started,
1464			     nr_written, 0);
1465	if (ret)
1466		return ret;
1467
1468	if (*page_started)
1469		return 0;
1470
1471	__set_page_dirty_nobuffers(locked_page);
1472	account_page_redirty(locked_page);
1473	extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1474	*page_started = 1;
1475
1476	return 0;
1477}
1478
1479static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1480					u64 bytenr, u64 num_bytes)
1481{
1482	int ret;
1483	struct btrfs_ordered_sum *sums;
1484	LIST_HEAD(list);
1485
1486	ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1487				       bytenr + num_bytes - 1, &list, 0);
1488	if (ret == 0 && list_empty(&list))
1489		return 0;
1490
1491	while (!list_empty(&list)) {
1492		sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1493		list_del(&sums->list);
1494		kfree(sums);
1495	}
1496	if (ret < 0)
1497		return ret;
1498	return 1;
1499}
1500
1501static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
1502			   const u64 start, const u64 end,
1503			   int *page_started, unsigned long *nr_written)
1504{
1505	const bool is_space_ino = btrfs_is_free_space_inode(inode);
1506	const bool is_reloc_ino = (inode->root->root_key.objectid ==
1507				   BTRFS_DATA_RELOC_TREE_OBJECTID);
1508	const u64 range_bytes = end + 1 - start;
1509	struct extent_io_tree *io_tree = &inode->io_tree;
1510	u64 range_start = start;
1511	u64 count;
1512
1513	/*
1514	 * If EXTENT_NORESERVE is set it means that when the buffered write was
1515	 * made we had not enough available data space and therefore we did not
1516	 * reserve data space for it, since we though we could do NOCOW for the
1517	 * respective file range (either there is prealloc extent or the inode
1518	 * has the NOCOW bit set).
1519	 *
1520	 * However when we need to fallback to COW mode (because for example the
1521	 * block group for the corresponding extent was turned to RO mode by a
1522	 * scrub or relocation) we need to do the following:
1523	 *
1524	 * 1) We increment the bytes_may_use counter of the data space info.
1525	 *    If COW succeeds, it allocates a new data extent and after doing
1526	 *    that it decrements the space info's bytes_may_use counter and
1527	 *    increments its bytes_reserved counter by the same amount (we do
1528	 *    this at btrfs_add_reserved_bytes()). So we need to increment the
1529	 *    bytes_may_use counter to compensate (when space is reserved at
1530	 *    buffered write time, the bytes_may_use counter is incremented);
1531	 *
1532	 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1533	 *    that if the COW path fails for any reason, it decrements (through
1534	 *    extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1535	 *    data space info, which we incremented in the step above.
1536	 *
1537	 * If we need to fallback to cow and the inode corresponds to a free
1538	 * space cache inode or an inode of the data relocation tree, we must
1539	 * also increment bytes_may_use of the data space_info for the same
1540	 * reason. Space caches and relocated data extents always get a prealloc
1541	 * extent for them, however scrub or balance may have set the block
1542	 * group that contains that extent to RO mode and therefore force COW
1543	 * when starting writeback.
1544	 */
1545	count = count_range_bits(io_tree, &range_start, end, range_bytes,
1546				 EXTENT_NORESERVE, 0);
1547	if (count > 0 || is_space_ino || is_reloc_ino) {
1548		u64 bytes = count;
1549		struct btrfs_fs_info *fs_info = inode->root->fs_info;
1550		struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1551
1552		if (is_space_ino || is_reloc_ino)
1553			bytes = range_bytes;
1554
1555		spin_lock(&sinfo->lock);
1556		btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
1557		spin_unlock(&sinfo->lock);
1558
1559		if (count > 0)
1560			clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1561					 0, 0, NULL);
1562	}
1563
1564	return cow_file_range(inode, locked_page, start, end, page_started,
1565			      nr_written, 1);
1566}
1567
1568/*
1569 * when nowcow writeback call back.  This checks for snapshots or COW copies
1570 * of the extents that exist in the file, and COWs the file as required.
1571 *
1572 * If no cow copies or snapshots exist, we write directly to the existing
1573 * blocks on disk
1574 */
1575static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
1576				       struct page *locked_page,
1577				       const u64 start, const u64 end,
1578				       int *page_started,
1579				       unsigned long *nr_written)
1580{
1581	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1582	struct btrfs_root *root = inode->root;
1583	struct btrfs_path *path;
1584	u64 cow_start = (u64)-1;
1585	u64 cur_offset = start;
1586	int ret;
1587	bool check_prev = true;
1588	const bool freespace_inode = btrfs_is_free_space_inode(inode);
1589	u64 ino = btrfs_ino(inode);
1590	bool nocow = false;
1591	u64 disk_bytenr = 0;
1592	const bool force = inode->flags & BTRFS_INODE_NODATACOW;
1593
1594	path = btrfs_alloc_path();
1595	if (!path) {
1596		extent_clear_unlock_delalloc(inode, start, end, locked_page,
1597					     EXTENT_LOCKED | EXTENT_DELALLOC |
1598					     EXTENT_DO_ACCOUNTING |
1599					     EXTENT_DEFRAG, PAGE_UNLOCK |
1600					     PAGE_START_WRITEBACK |
1601					     PAGE_END_WRITEBACK);
1602		return -ENOMEM;
1603	}
1604
1605	while (1) {
1606		struct btrfs_key found_key;
1607		struct btrfs_file_extent_item *fi;
1608		struct extent_buffer *leaf;
1609		u64 extent_end;
1610		u64 extent_offset;
1611		u64 num_bytes = 0;
1612		u64 disk_num_bytes;
1613		u64 ram_bytes;
1614		int extent_type;
1615
1616		nocow = false;
1617
1618		ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1619					       cur_offset, 0);
1620		if (ret < 0)
1621			goto error;
1622
1623		/*
1624		 * If there is no extent for our range when doing the initial
1625		 * search, then go back to the previous slot as it will be the
1626		 * one containing the search offset
1627		 */
1628		if (ret > 0 && path->slots[0] > 0 && check_prev) {
1629			leaf = path->nodes[0];
1630			btrfs_item_key_to_cpu(leaf, &found_key,
1631					      path->slots[0] - 1);
1632			if (found_key.objectid == ino &&
1633			    found_key.type == BTRFS_EXTENT_DATA_KEY)
1634				path->slots[0]--;
1635		}
1636		check_prev = false;
1637next_slot:
1638		/* Go to next leaf if we have exhausted the current one */
1639		leaf = path->nodes[0];
1640		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1641			ret = btrfs_next_leaf(root, path);
1642			if (ret < 0) {
1643				if (cow_start != (u64)-1)
1644					cur_offset = cow_start;
1645				goto error;
1646			}
1647			if (ret > 0)
1648				break;
1649			leaf = path->nodes[0];
1650		}
1651
1652		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1653
1654		/* Didn't find anything for our INO */
1655		if (found_key.objectid > ino)
1656			break;
1657		/*
1658		 * Keep searching until we find an EXTENT_ITEM or there are no
1659		 * more extents for this inode
1660		 */
1661		if (WARN_ON_ONCE(found_key.objectid < ino) ||
1662		    found_key.type < BTRFS_EXTENT_DATA_KEY) {
1663			path->slots[0]++;
1664			goto next_slot;
1665		}
1666
1667		/* Found key is not EXTENT_DATA_KEY or starts after req range */
1668		if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1669		    found_key.offset > end)
1670			break;
1671
1672		/*
1673		 * If the found extent starts after requested offset, then
1674		 * adjust extent_end to be right before this extent begins
1675		 */
1676		if (found_key.offset > cur_offset) {
1677			extent_end = found_key.offset;
1678			extent_type = 0;
1679			goto out_check;
1680		}
1681
1682		/*
1683		 * Found extent which begins before our range and potentially
1684		 * intersect it
1685		 */
1686		fi = btrfs_item_ptr(leaf, path->slots[0],
1687				    struct btrfs_file_extent_item);
1688		extent_type = btrfs_file_extent_type(leaf, fi);
1689
1690		ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1691		if (extent_type == BTRFS_FILE_EXTENT_REG ||
1692		    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1693			disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1694			extent_offset = btrfs_file_extent_offset(leaf, fi);
1695			extent_end = found_key.offset +
1696				btrfs_file_extent_num_bytes(leaf, fi);
1697			disk_num_bytes =
1698				btrfs_file_extent_disk_num_bytes(leaf, fi);
1699			/*
1700			 * If the extent we got ends before our current offset,
1701			 * skip to the next extent.
1702			 */
1703			if (extent_end <= cur_offset) {
1704				path->slots[0]++;
1705				goto next_slot;
1706			}
1707			/* Skip holes */
1708			if (disk_bytenr == 0)
1709				goto out_check;
1710			/* Skip compressed/encrypted/encoded extents */
1711			if (btrfs_file_extent_compression(leaf, fi) ||
1712			    btrfs_file_extent_encryption(leaf, fi) ||
1713			    btrfs_file_extent_other_encoding(leaf, fi))
1714				goto out_check;
1715			/*
1716			 * If extent is created before the last volume's snapshot
1717			 * this implies the extent is shared, hence we can't do
1718			 * nocow. This is the same check as in
1719			 * btrfs_cross_ref_exist but without calling
1720			 * btrfs_search_slot.
1721			 */
1722			if (!freespace_inode &&
1723			    btrfs_file_extent_generation(leaf, fi) <=
1724			    btrfs_root_last_snapshot(&root->root_item))
1725				goto out_check;
1726			if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1727				goto out_check;
1728
1729			/*
1730			 * The following checks can be expensive, as they need to
1731			 * take other locks and do btree or rbtree searches, so
1732			 * release the path to avoid blocking other tasks for too
1733			 * long.
1734			 */
1735			btrfs_release_path(path);
1736
1737			ret = btrfs_cross_ref_exist(root, ino,
1738						    found_key.offset -
1739						    extent_offset, disk_bytenr, false);
1740			if (ret) {
1741				/*
1742				 * ret could be -EIO if the above fails to read
1743				 * metadata.
1744				 */
1745				if (ret < 0) {
1746					if (cow_start != (u64)-1)
1747						cur_offset = cow_start;
1748					goto error;
1749				}
1750
1751				WARN_ON_ONCE(freespace_inode);
1752				goto out_check;
1753			}
1754			disk_bytenr += extent_offset;
1755			disk_bytenr += cur_offset - found_key.offset;
1756			num_bytes = min(end + 1, extent_end) - cur_offset;
1757			/*
1758			 * If there are pending snapshots for this root, we
1759			 * fall into common COW way
1760			 */
1761			if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
1762				goto out_check;
1763			/*
1764			 * force cow if csum exists in the range.
1765			 * this ensure that csum for a given extent are
1766			 * either valid or do not exist.
1767			 */
1768			ret = csum_exist_in_range(fs_info, disk_bytenr,
1769						  num_bytes);
1770			if (ret) {
1771				/*
1772				 * ret could be -EIO if the above fails to read
1773				 * metadata.
1774				 */
1775				if (ret < 0) {
1776					if (cow_start != (u64)-1)
1777						cur_offset = cow_start;
1778					goto error;
1779				}
1780				WARN_ON_ONCE(freespace_inode);
1781				goto out_check;
1782			}
1783			/* If the extent's block group is RO, we must COW */
1784			if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1785				goto out_check;
1786			nocow = true;
1787		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1788			extent_end = found_key.offset + ram_bytes;
1789			extent_end = ALIGN(extent_end, fs_info->sectorsize);
1790			/* Skip extents outside of our requested range */
1791			if (extent_end <= start) {
1792				path->slots[0]++;
1793				goto next_slot;
1794			}
1795		} else {
1796			/* If this triggers then we have a memory corruption */
1797			BUG();
1798		}
1799out_check:
1800		/*
1801		 * If nocow is false then record the beginning of the range
1802		 * that needs to be COWed
1803		 */
1804		if (!nocow) {
1805			if (cow_start == (u64)-1)
1806				cow_start = cur_offset;
1807			cur_offset = extent_end;
1808			if (cur_offset > end)
1809				break;
1810			if (!path->nodes[0])
1811				continue;
1812			path->slots[0]++;
1813			goto next_slot;
1814		}
1815
1816		/*
1817		 * COW range from cow_start to found_key.offset - 1. As the key
1818		 * will contain the beginning of the first extent that can be
1819		 * NOCOW, following one which needs to be COW'ed
1820		 */
1821		if (cow_start != (u64)-1) {
1822			ret = fallback_to_cow(inode, locked_page,
1823					      cow_start, found_key.offset - 1,
1824					      page_started, nr_written);
1825			if (ret)
1826				goto error;
1827			cow_start = (u64)-1;
1828		}
1829
1830		if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1831			u64 orig_start = found_key.offset - extent_offset;
1832			struct extent_map *em;
1833
1834			em = create_io_em(inode, cur_offset, num_bytes,
1835					  orig_start,
1836					  disk_bytenr, /* block_start */
1837					  num_bytes, /* block_len */
1838					  disk_num_bytes, /* orig_block_len */
1839					  ram_bytes, BTRFS_COMPRESS_NONE,
1840					  BTRFS_ORDERED_PREALLOC);
1841			if (IS_ERR(em)) {
1842				ret = PTR_ERR(em);
1843				goto error;
1844			}
1845			free_extent_map(em);
1846			ret = btrfs_add_ordered_extent(inode, cur_offset,
1847						       disk_bytenr, num_bytes,
1848						       num_bytes,
1849						       BTRFS_ORDERED_PREALLOC);
1850			if (ret) {
1851				btrfs_drop_extent_cache(inode, cur_offset,
1852							cur_offset + num_bytes - 1,
1853							0);
1854				goto error;
1855			}
1856		} else {
1857			ret = btrfs_add_ordered_extent(inode, cur_offset,
1858						       disk_bytenr, num_bytes,
1859						       num_bytes,
1860						       BTRFS_ORDERED_NOCOW);
1861			if (ret)
1862				goto error;
1863		}
1864
1865		if (nocow)
1866			btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1867		nocow = false;
1868
1869		if (root->root_key.objectid ==
1870		    BTRFS_DATA_RELOC_TREE_OBJECTID)
1871			/*
1872			 * Error handled later, as we must prevent
1873			 * extent_clear_unlock_delalloc() in error handler
1874			 * from freeing metadata of created ordered extent.
1875			 */
1876			ret = btrfs_reloc_clone_csums(inode, cur_offset,
1877						      num_bytes);
1878
1879		extent_clear_unlock_delalloc(inode, cur_offset,
1880					     cur_offset + num_bytes - 1,
1881					     locked_page, EXTENT_LOCKED |
1882					     EXTENT_DELALLOC |
1883					     EXTENT_CLEAR_DATA_RESV,
1884					     PAGE_UNLOCK | PAGE_SET_ORDERED);
1885
1886		cur_offset = extent_end;
1887
1888		/*
1889		 * btrfs_reloc_clone_csums() error, now we're OK to call error
1890		 * handler, as metadata for created ordered extent will only
1891		 * be freed by btrfs_finish_ordered_io().
1892		 */
1893		if (ret)
1894			goto error;
1895		if (cur_offset > end)
1896			break;
1897	}
1898	btrfs_release_path(path);
1899
1900	if (cur_offset <= end && cow_start == (u64)-1)
1901		cow_start = cur_offset;
1902
1903	if (cow_start != (u64)-1) {
1904		cur_offset = end;
1905		ret = fallback_to_cow(inode, locked_page, cow_start, end,
1906				      page_started, nr_written);
1907		if (ret)
1908			goto error;
1909	}
1910
1911error:
1912	if (nocow)
1913		btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1914
1915	if (ret && cur_offset < end)
1916		extent_clear_unlock_delalloc(inode, cur_offset, end,
1917					     locked_page, EXTENT_LOCKED |
1918					     EXTENT_DELALLOC | EXTENT_DEFRAG |
1919					     EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1920					     PAGE_START_WRITEBACK |
1921					     PAGE_END_WRITEBACK);
1922	btrfs_free_path(path);
1923	return ret;
1924}
1925
1926static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
1927{
1928	if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1929		if (inode->defrag_bytes &&
1930		    test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1931				   0, NULL))
1932			return false;
1933		return true;
1934	}
1935	return false;
1936}
1937
1938/*
1939 * Function to process delayed allocation (create CoW) for ranges which are
1940 * being touched for the first time.
1941 */
1942int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
1943		u64 start, u64 end, int *page_started, unsigned long *nr_written,
1944		struct writeback_control *wbc)
1945{
1946	int ret;
1947	const bool zoned = btrfs_is_zoned(inode->root->fs_info);
1948
1949	if (should_nocow(inode, start, end)) {
1950		ASSERT(!zoned);
1951		ret = run_delalloc_nocow(inode, locked_page, start, end,
1952					 page_started, nr_written);
1953	} else if (!inode_can_compress(inode) ||
1954		   !inode_need_compress(inode, start, end)) {
1955		if (zoned)
1956			ret = run_delalloc_zoned(inode, locked_page, start, end,
1957						 page_started, nr_written);
1958		else
1959			ret = cow_file_range(inode, locked_page, start, end,
1960					     page_started, nr_written, 1);
1961	} else {
1962		set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1963		ret = cow_file_range_async(inode, wbc, locked_page, start, end,
1964					   page_started, nr_written);
1965	}
1966	ASSERT(ret <= 0);
1967	if (ret)
1968		btrfs_cleanup_ordered_extents(inode, locked_page, start,
1969					      end - start + 1);
1970	return ret;
1971}
1972
1973void btrfs_split_delalloc_extent(struct inode *inode,
1974				 struct extent_state *orig, u64 split)
1975{
1976	u64 size;
1977
1978	/* not delalloc, ignore it */
1979	if (!(orig->state & EXTENT_DELALLOC))
1980		return;
1981
1982	size = orig->end - orig->start + 1;
1983	if (size > BTRFS_MAX_EXTENT_SIZE) {
1984		u32 num_extents;
1985		u64 new_size;
1986
1987		/*
1988		 * See the explanation in btrfs_merge_delalloc_extent, the same
1989		 * applies here, just in reverse.
1990		 */
1991		new_size = orig->end - split + 1;
1992		num_extents = count_max_extents(new_size);
1993		new_size = split - orig->start;
1994		num_extents += count_max_extents(new_size);
1995		if (count_max_extents(size) >= num_extents)
1996			return;
1997	}
1998
1999	spin_lock(&BTRFS_I(inode)->lock);
2000	btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
2001	spin_unlock(&BTRFS_I(inode)->lock);
2002}
2003
2004/*
2005 * Handle merged delayed allocation extents so we can keep track of new extents
2006 * that are just merged onto old extents, such as when we are doing sequential
2007 * writes, so we can properly account for the metadata space we'll need.
2008 */
2009void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2010				 struct extent_state *other)
2011{
2012	u64 new_size, old_size;
2013	u32 num_extents;
2014
2015	/* not delalloc, ignore it */
2016	if (!(other->state & EXTENT_DELALLOC))
2017		return;
2018
2019	if (new->start > other->start)
2020		new_size = new->end - other->start + 1;
2021	else
2022		new_size = other->end - new->start + 1;
2023
2024	/* we're not bigger than the max, unreserve the space and go */
2025	if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2026		spin_lock(&BTRFS_I(inode)->lock);
2027		btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
2028		spin_unlock(&BTRFS_I(inode)->lock);
2029		return;
2030	}
2031
2032	/*
2033	 * We have to add up either side to figure out how many extents were
2034	 * accounted for before we merged into one big extent.  If the number of
2035	 * extents we accounted for is <= the amount we need for the new range
2036	 * then we can return, otherwise drop.  Think of it like this
2037	 *
2038	 * [ 4k][MAX_SIZE]
2039	 *
2040	 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2041	 * need 2 outstanding extents, on one side we have 1 and the other side
2042	 * we have 1 so they are == and we can return.  But in this case
2043	 *
2044	 * [MAX_SIZE+4k][MAX_SIZE+4k]
2045	 *
2046	 * Each range on their own accounts for 2 extents, but merged together
2047	 * they are only 3 extents worth of accounting, so we need to drop in
2048	 * this case.
2049	 */
2050	old_size = other->end - other->start + 1;
2051	num_extents = count_max_extents(old_size);
2052	old_size = new->end - new->start + 1;
2053	num_extents += count_max_extents(old_size);
2054	if (count_max_extents(new_size) >= num_extents)
2055		return;
2056
2057	spin_lock(&BTRFS_I(inode)->lock);
2058	btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
2059	spin_unlock(&BTRFS_I(inode)->lock);
2060}
2061
2062static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2063				      struct inode *inode)
2064{
2065	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2066
2067	spin_lock(&root->delalloc_lock);
2068	if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2069		list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2070			      &root->delalloc_inodes);
2071		set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2072			&BTRFS_I(inode)->runtime_flags);
2073		root->nr_delalloc_inodes++;
2074		if (root->nr_delalloc_inodes == 1) {
2075			spin_lock(&fs_info->delalloc_root_lock);
2076			BUG_ON(!list_empty(&root->delalloc_root));
2077			list_add_tail(&root->delalloc_root,
2078				      &fs_info->delalloc_roots);
2079			spin_unlock(&fs_info->delalloc_root_lock);
2080		}
2081	}
2082	spin_unlock(&root->delalloc_lock);
2083}
2084
2085
2086void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2087				struct btrfs_inode *inode)
2088{
2089	struct btrfs_fs_info *fs_info = root->fs_info;
2090
2091	if (!list_empty(&inode->delalloc_inodes)) {
2092		list_del_init(&inode->delalloc_inodes);
2093		clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2094			  &inode->runtime_flags);
2095		root->nr_delalloc_inodes--;
2096		if (!root->nr_delalloc_inodes) {
2097			ASSERT(list_empty(&root->delalloc_inodes));
2098			spin_lock(&fs_info->delalloc_root_lock);
2099			BUG_ON(list_empty(&root->delalloc_root));
2100			list_del_init(&root->delalloc_root);
2101			spin_unlock(&fs_info->delalloc_root_lock);
2102		}
2103	}
2104}
2105
2106static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2107				     struct btrfs_inode *inode)
2108{
2109	spin_lock(&root->delalloc_lock);
2110	__btrfs_del_delalloc_inode(root, inode);
2111	spin_unlock(&root->delalloc_lock);
2112}
2113
2114/*
2115 * Properly track delayed allocation bytes in the inode and to maintain the
2116 * list of inodes that have pending delalloc work to be done.
2117 */
2118void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2119			       unsigned *bits)
2120{
2121	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2122
2123	if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2124		WARN_ON(1);
2125	/*
2126	 * set_bit and clear bit hooks normally require _irqsave/restore
2127	 * but in this case, we are only testing for the DELALLOC
2128	 * bit, which is only set or cleared with irqs on
2129	 */
2130	if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
2131		struct btrfs_root *root = BTRFS_I(inode)->root;
2132		u64 len = state->end + 1 - state->start;
2133		u32 num_extents = count_max_extents(len);
2134		bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
2135
2136		spin_lock(&BTRFS_I(inode)->lock);
2137		btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2138		spin_unlock(&BTRFS_I(inode)->lock);
2139
2140		/* For sanity tests */
2141		if (btrfs_is_testing(fs_info))
2142			return;
2143
2144		percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2145					 fs_info->delalloc_batch);
2146		spin_lock(&BTRFS_I(inode)->lock);
2147		BTRFS_I(inode)->delalloc_bytes += len;
2148		if (*bits & EXTENT_DEFRAG)
2149			BTRFS_I(inode)->defrag_bytes += len;
2150		if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2151					 &BTRFS_I(inode)->runtime_flags))
2152			btrfs_add_delalloc_inodes(root, inode);
2153		spin_unlock(&BTRFS_I(inode)->lock);
2154	}
2155
2156	if (!(state->state & EXTENT_DELALLOC_NEW) &&
2157	    (*bits & EXTENT_DELALLOC_NEW)) {
2158		spin_lock(&BTRFS_I(inode)->lock);
2159		BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2160			state->start;
2161		spin_unlock(&BTRFS_I(inode)->lock);
2162	}
2163}
2164
2165/*
2166 * Once a range is no longer delalloc this function ensures that proper
2167 * accounting happens.
2168 */
2169void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2170				 struct extent_state *state, unsigned *bits)
2171{
2172	struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2173	struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
2174	u64 len = state->end + 1 - state->start;
2175	u32 num_extents = count_max_extents(len);
2176
2177	if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2178		spin_lock(&inode->lock);
2179		inode->defrag_bytes -= len;
2180		spin_unlock(&inode->lock);
2181	}
2182
2183	/*
2184	 * set_bit and clear bit hooks normally require _irqsave/restore
2185	 * but in this case, we are only testing for the DELALLOC
2186	 * bit, which is only set or cleared with irqs on
2187	 */
2188	if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
2189		struct btrfs_root *root = inode->root;
2190		bool do_list = !btrfs_is_free_space_inode(inode);
2191
2192		spin_lock(&inode->lock);
2193		btrfs_mod_outstanding_extents(inode, -num_extents);
2194		spin_unlock(&inode->lock);
2195
2196		/*
2197		 * We don't reserve metadata space for space cache inodes so we
2198		 * don't need to call delalloc_release_metadata if there is an
2199		 * error.
2200		 */
2201		if (*bits & EXTENT_CLEAR_META_RESV &&
2202		    root != fs_info->tree_root)
2203			btrfs_delalloc_release_metadata(inode, len, false);
2204
2205		/* For sanity tests. */
2206		if (btrfs_is_testing(fs_info))
2207			return;
2208
2209		if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2210		    do_list && !(state->state & EXTENT_NORESERVE) &&
2211		    (*bits & EXTENT_CLEAR_DATA_RESV))
2212			btrfs_free_reserved_data_space_noquota(fs_info, len);
2213
2214		percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2215					 fs_info->delalloc_batch);
2216		spin_lock(&inode->lock);
2217		inode->delalloc_bytes -= len;
2218		if (do_list && inode->delalloc_bytes == 0 &&
2219		    test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2220					&inode->runtime_flags))
2221			btrfs_del_delalloc_inode(root, inode);
2222		spin_unlock(&inode->lock);
2223	}
2224
2225	if ((state->state & EXTENT_DELALLOC_NEW) &&
2226	    (*bits & EXTENT_DELALLOC_NEW)) {
2227		spin_lock(&inode->lock);
2228		ASSERT(inode->new_delalloc_bytes >= len);
2229		inode->new_delalloc_bytes -= len;
2230		if (*bits & EXTENT_ADD_INODE_BYTES)
2231			inode_add_bytes(&inode->vfs_inode, len);
2232		spin_unlock(&inode->lock);
2233	}
2234}
2235
2236/*
2237 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2238 * in a chunk's stripe. This function ensures that bios do not span a
2239 * stripe/chunk
2240 *
2241 * @page - The page we are about to add to the bio
2242 * @size - size we want to add to the bio
2243 * @bio - bio we want to ensure is smaller than a stripe
2244 * @bio_flags - flags of the bio
2245 *
2246 * return 1 if page cannot be added to the bio
2247 * return 0 if page can be added to the bio
2248 * return error otherwise
2249 */
2250int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2251			     unsigned long bio_flags)
2252{
2253	struct inode *inode = page->mapping->host;
2254	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2255	u64 logical = bio->bi_iter.bi_sector << 9;
2256	u32 bio_len = bio->bi_iter.bi_size;
2257	struct extent_map *em;
2258	int ret = 0;
2259	struct btrfs_io_geometry geom;
2260
2261	if (bio_flags & EXTENT_BIO_COMPRESSED)
2262		return 0;
2263
2264	em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
2265	if (IS_ERR(em))
2266		return PTR_ERR(em);
2267	ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
2268	if (ret < 0)
2269		goto out;
2270
2271	if (geom.len < bio_len + size)
2272		ret = 1;
2273out:
2274	free_extent_map(em);
2275	return ret;
2276}
2277
2278/*
2279 * in order to insert checksums into the metadata in large chunks,
2280 * we wait until bio submission time.   All the pages in the bio are
2281 * checksummed and sums are attached onto the ordered extent record.
2282 *
2283 * At IO completion time the cums attached on the ordered extent record
2284 * are inserted into the btree
2285 */
2286static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
2287					   u64 dio_file_offset)
2288{
2289	return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
2290}
2291
2292/*
2293 * Split an extent_map at [start, start + len]
2294 *
2295 * This function is intended to be used only for extract_ordered_extent().
2296 */
2297static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2298			  u64 pre, u64 post)
2299{
2300	struct extent_map_tree *em_tree = &inode->extent_tree;
2301	struct extent_map *em;
2302	struct extent_map *split_pre = NULL;
2303	struct extent_map *split_mid = NULL;
2304	struct extent_map *split_post = NULL;
2305	int ret = 0;
2306	unsigned long flags;
2307
2308	/* Sanity check */
2309	if (pre == 0 && post == 0)
2310		return 0;
2311
2312	split_pre = alloc_extent_map();
2313	if (pre)
2314		split_mid = alloc_extent_map();
2315	if (post)
2316		split_post = alloc_extent_map();
2317	if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2318		ret = -ENOMEM;
2319		goto out;
2320	}
2321
2322	ASSERT(pre + post < len);
2323
2324	lock_extent(&inode->io_tree, start, start + len - 1);
2325	write_lock(&em_tree->lock);
2326	em = lookup_extent_mapping(em_tree, start, len);
2327	if (!em) {
2328		ret = -EIO;
2329		goto out_unlock;
2330	}
2331
2332	ASSERT(em->len == len);
2333	ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2334	ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
2335	ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
2336	ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
2337	ASSERT(!list_empty(&em->list));
2338
2339	flags = em->flags;
2340	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2341
2342	/* First, replace the em with a new extent_map starting from * em->start */
2343	split_pre->start = em->start;
2344	split_pre->len = (pre ? pre : em->len - post);
2345	split_pre->orig_start = split_pre->start;
2346	split_pre->block_start = em->block_start;
2347	split_pre->block_len = split_pre->len;
2348	split_pre->orig_block_len = split_pre->block_len;
2349	split_pre->ram_bytes = split_pre->len;
2350	split_pre->flags = flags;
2351	split_pre->compress_type = em->compress_type;
2352	split_pre->generation = em->generation;
2353
2354	replace_extent_mapping(em_tree, em, split_pre, 1);
2355
2356	/*
2357	 * Now we only have an extent_map at:
2358	 *     [em->start, em->start + pre] if pre != 0
2359	 *     [em->start, em->start + em->len - post] if pre == 0
2360	 */
2361
2362	if (pre) {
2363		/* Insert the middle extent_map */
2364		split_mid->start = em->start + pre;
2365		split_mid->len = em->len - pre - post;
2366		split_mid->orig_start = split_mid->start;
2367		split_mid->block_start = em->block_start + pre;
2368		split_mid->block_len = split_mid->len;
2369		split_mid->orig_block_len = split_mid->block_len;
2370		split_mid->ram_bytes = split_mid->len;
2371		split_mid->flags = flags;
2372		split_mid->compress_type = em->compress_type;
2373		split_mid->generation = em->generation;
2374		add_extent_mapping(em_tree, split_mid, 1);
2375	}
2376
2377	if (post) {
2378		split_post->start = em->start + em->len - post;
2379		split_post->len = post;
2380		split_post->orig_start = split_post->start;
2381		split_post->block_start = em->block_start + em->len - post;
2382		split_post->block_len = split_post->len;
2383		split_post->orig_block_len = split_post->block_len;
2384		split_post->ram_bytes = split_post->len;
2385		split_post->flags = flags;
2386		split_post->compress_type = em->compress_type;
2387		split_post->generation = em->generation;
2388		add_extent_mapping(em_tree, split_post, 1);
2389	}
2390
2391	/* Once for us */
2392	free_extent_map(em);
2393	/* Once for the tree */
2394	free_extent_map(em);
2395
2396out_unlock:
2397	write_unlock(&em_tree->lock);
2398	unlock_extent(&inode->io_tree, start, start + len - 1);
2399out:
2400	free_extent_map(split_pre);
2401	free_extent_map(split_mid);
2402	free_extent_map(split_post);
2403
2404	return ret;
2405}
2406
2407static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2408					   struct bio *bio, loff_t file_offset)
2409{
2410	struct btrfs_ordered_extent *ordered;
2411	u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
2412	u64 file_len;
2413	u64 len = bio->bi_iter.bi_size;
2414	u64 end = start + len;
2415	u64 ordered_end;
2416	u64 pre, post;
2417	int ret = 0;
2418
2419	ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2420	if (WARN_ON_ONCE(!ordered))
2421		return BLK_STS_IOERR;
2422
2423	/* No need to split */
2424	if (ordered->disk_num_bytes == len)
2425		goto out;
2426
2427	/* We cannot split once end_bio'd ordered extent */
2428	if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2429		ret = -EINVAL;
2430		goto out;
2431	}
2432
2433	/* We cannot split a compressed ordered extent */
2434	if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2435		ret = -EINVAL;
2436		goto out;
2437	}
2438
2439	ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2440	/* bio must be in one ordered extent */
2441	if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2442		ret = -EINVAL;
2443		goto out;
2444	}
2445
2446	/* Checksum list should be empty */
2447	if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2448		ret = -EINVAL;
2449		goto out;
2450	}
2451
2452	file_len = ordered->num_bytes;
2453	pre = start - ordered->disk_bytenr;
2454	post = ordered_end - end;
2455
2456	ret = btrfs_split_ordered_extent(ordered, pre, post);
2457	if (ret)
2458		goto out;
2459	ret = split_zoned_em(inode, file_offset, file_len, pre, post);
2460
2461out:
2462	btrfs_put_ordered_extent(ordered);
2463
2464	return errno_to_blk_status(ret);
2465}
2466
2467/*
2468 * extent_io.c submission hook. This does the right thing for csum calculation
2469 * on write, or reading the csums from the tree before a read.
2470 *
2471 * Rules about async/sync submit,
2472 * a) read:				sync submit
2473 *
2474 * b) write without checksum:		sync submit
2475 *
2476 * c) write with checksum:
2477 *    c-1) if bio is issued by fsync:	sync submit
2478 *         (sync_writers != 0)
2479 *
2480 *    c-2) if root is reloc root:	sync submit
2481 *         (only in case of buffered IO)
2482 *
2483 *    c-3) otherwise:			async submit
2484 */
2485blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2486				   int mirror_num, unsigned long bio_flags)
2487
2488{
2489	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2490	struct btrfs_root *root = BTRFS_I(inode)->root;
2491	enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
2492	blk_status_t ret = 0;
2493	int skip_sum;
2494	int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
2495
2496	skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2497		   !fs_info->csum_root;
2498
2499	if (btrfs_is_free_space_inode(BTRFS_I(inode)))
2500		metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
2501
2502	if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2503		struct page *page = bio_first_bvec_all(bio)->bv_page;
2504		loff_t file_offset = page_offset(page);
2505
2506		ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2507		if (ret)
2508			goto out;
2509	}
2510
2511	if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
2512		ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
2513		if (ret)
2514			goto out;
2515
2516		if (bio_flags & EXTENT_BIO_COMPRESSED) {
2517			ret = btrfs_submit_compressed_read(inode, bio,
2518							   mirror_num,
2519							   bio_flags);
2520			goto out;
2521		} else {
2522			/*
2523			 * Lookup bio sums does extra checks around whether we
2524			 * need to csum or not, which is why we ignore skip_sum
2525			 * here.
2526			 */
2527			ret = btrfs_lookup_bio_sums(inode, bio, NULL);
2528			if (ret)
2529				goto out;
2530		}
2531		goto mapit;
2532	} else if (async && !skip_sum) {
2533		/* csum items have already been cloned */
2534		if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2535			goto mapit;
2536		/* we're doing a write, do the async checksumming */
2537		ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2538					  0, btrfs_submit_bio_start);
2539		goto out;
2540	} else if (!skip_sum) {
2541		ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
2542		if (ret)
2543			goto out;
2544	}
2545
2546mapit:
2547	ret = btrfs_map_bio(fs_info, bio, mirror_num);
2548
2549out:
2550	if (ret) {
2551		bio->bi_status = ret;
2552		bio_endio(bio);
2553	}
2554	return ret;
2555}
2556
2557/*
2558 * given a list of ordered sums record them in the inode.  This happens
2559 * at IO completion time based on sums calculated at bio submission time.
2560 */
2561static int add_pending_csums(struct btrfs_trans_handle *trans,
2562			     struct list_head *list)
2563{
2564	struct btrfs_ordered_sum *sum;
2565	int ret;
2566
2567	list_for_each_entry(sum, list, list) {
2568		trans->adding_csums = true;
2569		ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
2570		trans->adding_csums = false;
2571		if (ret)
2572			return ret;
2573	}
2574	return 0;
2575}
2576
2577static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2578					 const u64 start,
2579					 const u64 len,
2580					 struct extent_state **cached_state)
2581{
2582	u64 search_start = start;
2583	const u64 end = start + len - 1;
2584
2585	while (search_start < end) {
2586		const u64 search_len = end - search_start + 1;
2587		struct extent_map *em;
2588		u64 em_len;
2589		int ret = 0;
2590
2591		em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2592		if (IS_ERR(em))
2593			return PTR_ERR(em);
2594
2595		if (em->block_start != EXTENT_MAP_HOLE)
2596			goto next;
2597
2598		em_len = em->len;
2599		if (em->start < search_start)
2600			em_len -= search_start - em->start;
2601		if (em_len > search_len)
2602			em_len = search_len;
2603
2604		ret = set_extent_bit(&inode->io_tree, search_start,
2605				     search_start + em_len - 1,
2606				     EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2607				     GFP_NOFS, NULL);
2608next:
2609		search_start = extent_map_end(em);
2610		free_extent_map(em);
2611		if (ret)
2612			return ret;
2613	}
2614	return 0;
2615}
2616
2617int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2618			      unsigned int extra_bits,
2619			      struct extent_state **cached_state)
2620{
2621	WARN_ON(PAGE_ALIGNED(end));
2622
2623	if (start >= i_size_read(&inode->vfs_inode) &&
2624	    !(inode->flags & BTRFS_INODE_PREALLOC)) {
2625		/*
2626		 * There can't be any extents following eof in this case so just
2627		 * set the delalloc new bit for the range directly.
2628		 */
2629		extra_bits |= EXTENT_DELALLOC_NEW;
2630	} else {
2631		int ret;
2632
2633		ret = btrfs_find_new_delalloc_bytes(inode, start,
2634						    end + 1 - start,
2635						    cached_state);
2636		if (ret)
2637			return ret;
2638	}
2639
2640	return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2641				   cached_state);
2642}
2643
2644/* see btrfs_writepage_start_hook for details on why this is required */
2645struct btrfs_writepage_fixup {
2646	struct page *page;
2647	struct inode *inode;
2648	struct btrfs_work work;
2649};
2650
2651static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2652{
2653	struct btrfs_writepage_fixup *fixup;
2654	struct btrfs_ordered_extent *ordered;
2655	struct extent_state *cached_state = NULL;
2656	struct extent_changeset *data_reserved = NULL;
2657	struct page *page;
2658	struct btrfs_inode *inode;
2659	u64 page_start;
2660	u64 page_end;
2661	int ret = 0;
2662	bool free_delalloc_space = true;
2663
2664	fixup = container_of(work, struct btrfs_writepage_fixup, work);
2665	page = fixup->page;
2666	inode = BTRFS_I(fixup->inode);
2667	page_start = page_offset(page);
2668	page_end = page_offset(page) + PAGE_SIZE - 1;
2669
2670	/*
2671	 * This is similar to page_mkwrite, we need to reserve the space before
2672	 * we take the page lock.
2673	 */
2674	ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2675					   PAGE_SIZE);
2676again:
2677	lock_page(page);
2678
2679	/*
2680	 * Before we queued this fixup, we took a reference on the page.
2681	 * page->mapping may go NULL, but it shouldn't be moved to a different
2682	 * address space.
2683	 */
2684	if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2685		/*
2686		 * Unfortunately this is a little tricky, either
2687		 *
2688		 * 1) We got here and our page had already been dealt with and
2689		 *    we reserved our space, thus ret == 0, so we need to just
2690		 *    drop our space reservation and bail.  This can happen the
2691		 *    first time we come into the fixup worker, or could happen
2692		 *    while waiting for the ordered extent.
2693		 * 2) Our page was already dealt with, but we happened to get an
2694		 *    ENOSPC above from the btrfs_delalloc_reserve_space.  In
2695		 *    this case we obviously don't have anything to release, but
2696		 *    because the page was already dealt with we don't want to
2697		 *    mark the page with an error, so make sure we're resetting
2698		 *    ret to 0.  This is why we have this check _before_ the ret
2699		 *    check, because we do not want to have a surprise ENOSPC
2700		 *    when the page was already properly dealt with.
2701		 */
2702		if (!ret) {
2703			btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2704			btrfs_delalloc_release_space(inode, data_reserved,
2705						     page_start, PAGE_SIZE,
2706						     true);
2707		}
2708		ret = 0;
2709		goto out_page;
2710	}
2711
2712	/*
2713	 * We can't mess with the page state unless it is locked, so now that
2714	 * it is locked bail if we failed to make our space reservation.
2715	 */
2716	if (ret)
2717		goto out_page;
2718
2719	lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
2720
2721	/* already ordered? We're done */
2722	if (PageOrdered(page))
2723		goto out_reserved;
2724
2725	ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
2726	if (ordered) {
2727		unlock_extent_cached(&inode->io_tree, page_start, page_end,
2728				     &cached_state);
2729		unlock_page(page);
2730		btrfs_start_ordered_extent(ordered, 1);
2731		btrfs_put_ordered_extent(ordered);
2732		goto again;
2733	}
2734
2735	ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2736					&cached_state);
2737	if (ret)
2738		goto out_reserved;
2739
2740	/*
2741	 * Everything went as planned, we're now the owner of a dirty page with
2742	 * delayed allocation bits set and space reserved for our COW
2743	 * destination.
2744	 *
2745	 * The page was dirty when we started, nothing should have cleaned it.
2746	 */
2747	BUG_ON(!PageDirty(page));
2748	free_delalloc_space = false;
2749out_reserved:
2750	btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2751	if (free_delalloc_space)
2752		btrfs_delalloc_release_space(inode, data_reserved, page_start,
2753					     PAGE_SIZE, true);
2754	unlock_extent_cached(&inode->io_tree, page_start, page_end,
2755			     &cached_state);
2756out_page:
2757	if (ret) {
2758		/*
2759		 * We hit ENOSPC or other errors.  Update the mapping and page
2760		 * to reflect the errors and clean the page.
2761		 */
2762		mapping_set_error(page->mapping, ret);
2763		end_extent_writepage(page, ret, page_start, page_end);
2764		clear_page_dirty_for_io(page);
2765		SetPageError(page);
2766	}
2767	ClearPageChecked(page);
2768	unlock_page(page);
2769	put_page(page);
2770	kfree(fixup);
2771	extent_changeset_free(data_reserved);
2772	/*
2773	 * As a precaution, do a delayed iput in case it would be the last iput
2774	 * that could need flushing space. Recursing back to fixup worker would
2775	 * deadlock.
2776	 */
2777	btrfs_add_delayed_iput(&inode->vfs_inode);
2778}
2779
2780/*
2781 * There are a few paths in the higher layers of the kernel that directly
2782 * set the page dirty bit without asking the filesystem if it is a
2783 * good idea.  This causes problems because we want to make sure COW
2784 * properly happens and the data=ordered rules are followed.
2785 *
2786 * In our case any range that doesn't have the ORDERED bit set
2787 * hasn't been properly setup for IO.  We kick off an async process
2788 * to fix it up.  The async helper will wait for ordered extents, set
2789 * the delalloc bit and make it safe to write the page.
2790 */
2791int btrfs_writepage_cow_fixup(struct page *page)
2792{
2793	struct inode *inode = page->mapping->host;
2794	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2795	struct btrfs_writepage_fixup *fixup;
2796
2797	/* This page has ordered extent covering it already */
2798	if (PageOrdered(page))
2799		return 0;
2800
2801	/*
2802	 * PageChecked is set below when we create a fixup worker for this page,
2803	 * don't try to create another one if we're already PageChecked()
2804	 *
2805	 * The extent_io writepage code will redirty the page if we send back
2806	 * EAGAIN.
2807	 */
2808	if (PageChecked(page))
2809		return -EAGAIN;
2810
2811	fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2812	if (!fixup)
2813		return -EAGAIN;
2814
2815	/*
2816	 * We are already holding a reference to this inode from
2817	 * write_cache_pages.  We need to hold it because the space reservation
2818	 * takes place outside of the page lock, and we can't trust
2819	 * page->mapping outside of the page lock.
2820	 */
2821	ihold(inode);
2822	SetPageChecked(page);
2823	get_page(page);
2824	btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
2825	fixup->page = page;
2826	fixup->inode = inode;
2827	btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2828
2829	return -EAGAIN;
2830}
2831
2832static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2833				       struct btrfs_inode *inode, u64 file_pos,
2834				       struct btrfs_file_extent_item *stack_fi,
2835				       const bool update_inode_bytes,
2836				       u64 qgroup_reserved)
2837{
2838	struct btrfs_root *root = inode->root;
2839	const u64 sectorsize = root->fs_info->sectorsize;
2840	struct btrfs_path *path;
2841	struct extent_buffer *leaf;
2842	struct btrfs_key ins;
2843	u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2844	u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2845	u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2846	u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
2847	struct btrfs_drop_extents_args drop_args = { 0 };
2848	int ret;
2849
2850	path = btrfs_alloc_path();
2851	if (!path)
2852		return -ENOMEM;
2853
2854	/*
2855	 * we may be replacing one extent in the tree with another.
2856	 * The new extent is pinned in the extent map, and we don't want
2857	 * to drop it from the cache until it is completely in the btree.
2858	 *
2859	 * So, tell btrfs_drop_extents to leave this extent in the cache.
2860	 * the caller is expected to unpin it and allow it to be merged
2861	 * with the others.
2862	 */
2863	drop_args.path = path;
2864	drop_args.start = file_pos;
2865	drop_args.end = file_pos + num_bytes;
2866	drop_args.replace_extent = true;
2867	drop_args.extent_item_size = sizeof(*stack_fi);
2868	ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2869	if (ret)
2870		goto out;
2871
2872	if (!drop_args.extent_inserted) {
2873		ins.objectid = btrfs_ino(inode);
2874		ins.offset = file_pos;
2875		ins.type = BTRFS_EXTENT_DATA_KEY;
2876
2877		ret = btrfs_insert_empty_item(trans, root, path, &ins,
2878					      sizeof(*stack_fi));
2879		if (ret)
2880			goto out;
2881	}
2882	leaf = path->nodes[0];
2883	btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2884	write_extent_buffer(leaf, stack_fi,
2885			btrfs_item_ptr_offset(leaf, path->slots[0]),
2886			sizeof(struct btrfs_file_extent_item));
2887
2888	btrfs_mark_buffer_dirty(leaf);
2889	btrfs_release_path(path);
2890
2891	/*
2892	 * If we dropped an inline extent here, we know the range where it is
2893	 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2894	 * number of bytes only for that range containing the inline extent.
2895	 * The remaining of the range will be processed when clearning the
2896	 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2897	 */
2898	if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2899		u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2900
2901		inline_size = drop_args.bytes_found - inline_size;
2902		btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2903		drop_args.bytes_found -= inline_size;
2904		num_bytes -= sectorsize;
2905	}
2906
2907	if (update_inode_bytes)
2908		btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
2909
2910	ins.objectid = disk_bytenr;
2911	ins.offset = disk_num_bytes;
2912	ins.type = BTRFS_EXTENT_ITEM_KEY;
2913
2914	ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
2915	if (ret)
2916		goto out;
2917
2918	ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
2919					       file_pos, qgroup_reserved, &ins);
2920out:
2921	btrfs_free_path(path);
2922
2923	return ret;
2924}
2925
2926static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2927					 u64 start, u64 len)
2928{
2929	struct btrfs_block_group *cache;
2930
2931	cache = btrfs_lookup_block_group(fs_info, start);
2932	ASSERT(cache);
2933
2934	spin_lock(&cache->lock);
2935	cache->delalloc_bytes -= len;
2936	spin_unlock(&cache->lock);
2937
2938	btrfs_put_block_group(cache);
2939}
2940
2941static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2942					     struct btrfs_ordered_extent *oe)
2943{
2944	struct btrfs_file_extent_item stack_fi;
2945	u64 logical_len;
2946	bool update_inode_bytes;
2947
2948	memset(&stack_fi, 0, sizeof(stack_fi));
2949	btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2950	btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2951	btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2952						   oe->disk_num_bytes);
2953	if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2954		logical_len = oe->truncated_len;
2955	else
2956		logical_len = oe->num_bytes;
2957	btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2958	btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2959	btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2960	/* Encryption and other encoding is reserved and all 0 */
2961
2962	/*
2963	 * For delalloc, when completing an ordered extent we update the inode's
2964	 * bytes when clearing the range in the inode's io tree, so pass false
2965	 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2966	 * except if the ordered extent was truncated.
2967	 */
2968	update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2969			     test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2970
2971	return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2972					   oe->file_offset, &stack_fi,
2973					   update_inode_bytes, oe->qgroup_rsv);
2974}
2975
2976/*
2977 * As ordered data IO finishes, this gets called so we can finish
2978 * an ordered extent if the range of bytes in the file it covers are
2979 * fully written.
2980 */
2981static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2982{
2983	struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2984	struct btrfs_root *root = inode->root;
2985	struct btrfs_fs_info *fs_info = root->fs_info;
2986	struct btrfs_trans_handle *trans = NULL;
2987	struct extent_io_tree *io_tree = &inode->io_tree;
2988	struct extent_state *cached_state = NULL;
2989	u64 start, end;
2990	int compress_type = 0;
2991	int ret = 0;
2992	u64 logical_len = ordered_extent->num_bytes;
2993	bool freespace_inode;
2994	bool truncated = false;
2995	bool clear_reserved_extent = true;
2996	unsigned int clear_bits = EXTENT_DEFRAG;
2997
2998	start = ordered_extent->file_offset;
2999	end = start + ordered_extent->num_bytes - 1;
3000
3001	if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3002	    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3003	    !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3004		clear_bits |= EXTENT_DELALLOC_NEW;
3005
3006	freespace_inode = btrfs_is_free_space_inode(inode);
3007
3008	if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3009		ret = -EIO;
3010		goto out;
3011	}
3012
3013	if (ordered_extent->bdev)
3014		btrfs_rewrite_logical_zoned(ordered_extent);
3015
3016	btrfs_free_io_failure_record(inode, start, end);
3017
3018	if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3019		truncated = true;
3020		logical_len = ordered_extent->truncated_len;
3021		/* Truncated the entire extent, don't bother adding */
3022		if (!logical_len)
3023			goto out;
3024	}
3025
3026	if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
3027		BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
3028
3029		btrfs_inode_safe_disk_i_size_write(inode, 0);
3030		if (freespace_inode)
3031			trans = btrfs_join_transaction_spacecache(root);
3032		else
3033			trans = btrfs_join_transaction(root);
3034		if (IS_ERR(trans)) {
3035			ret = PTR_ERR(trans);
3036			trans = NULL;
3037			goto out;
3038		}
3039		trans->block_rsv = &inode->block_rsv;
3040		ret = btrfs_update_inode_fallback(trans, root, inode);
3041		if (ret) /* -ENOMEM or corruption */
3042			btrfs_abort_transaction(trans, ret);
3043		goto out;
3044	}
3045
3046	clear_bits |= EXTENT_LOCKED;
3047	lock_extent_bits(io_tree, start, end, &cached_state);
3048
3049	if (freespace_inode)
3050		trans = btrfs_join_transaction_spacecache(root);
3051	else
3052		trans = btrfs_join_transaction(root);
3053	if (IS_ERR(trans)) {
3054		ret = PTR_ERR(trans);
3055		trans = NULL;
3056		goto out;
3057	}
3058
3059	trans->block_rsv = &inode->block_rsv;
3060
3061	if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3062		compress_type = ordered_extent->compress_type;
3063	if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3064		BUG_ON(compress_type);
3065		ret = btrfs_mark_extent_written(trans, inode,
3066						ordered_extent->file_offset,
3067						ordered_extent->file_offset +
3068						logical_len);
3069	} else {
3070		BUG_ON(root == fs_info->tree_root);
3071		ret = insert_ordered_extent_file_extent(trans, ordered_extent);
3072		if (!ret) {
3073			clear_reserved_extent = false;
3074			btrfs_release_delalloc_bytes(fs_info,
3075						ordered_extent->disk_bytenr,
3076						ordered_extent->disk_num_bytes);
3077		}
3078	}
3079	unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
3080			   ordered_extent->num_bytes, trans->transid);
3081	if (ret < 0) {
3082		btrfs_abort_transaction(trans, ret);
3083		goto out;
3084	}
3085
3086	ret = add_pending_csums(trans, &ordered_extent->list);
3087	if (ret) {
3088		btrfs_abort_transaction(trans, ret);
3089		goto out;
3090	}
3091
3092	/*
3093	 * If this is a new delalloc range, clear its new delalloc flag to
3094	 * update the inode's number of bytes. This needs to be done first
3095	 * before updating the inode item.
3096	 */
3097	if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3098	    !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
3099		clear_extent_bit(&inode->io_tree, start, end,
3100				 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3101				 0, 0, &cached_state);
3102
3103	btrfs_inode_safe_disk_i_size_write(inode, 0);
3104	ret = btrfs_update_inode_fallback(trans, root, inode);
3105	if (ret) { /* -ENOMEM or corruption */
3106		btrfs_abort_transaction(trans, ret);
3107		goto out;
3108	}
3109	ret = 0;
3110out:
3111	clear_extent_bit(&inode->io_tree, start, end, clear_bits,
3112			 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
3113			 &cached_state);
3114
3115	if (trans)
3116		btrfs_end_transaction(trans);
3117
3118	if (ret || truncated) {
3119		u64 unwritten_start = start;
3120
3121		/*
3122		 * If we failed to finish this ordered extent for any reason we
3123		 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3124		 * extent, and mark the inode with the error if it wasn't
3125		 * already set.  Any error during writeback would have already
3126		 * set the mapping error, so we need to set it if we're the ones
3127		 * marking this ordered extent as failed.
3128		 */
3129		if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3130					     &ordered_extent->flags))
3131			mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3132
3133		if (truncated)
3134			unwritten_start += logical_len;
3135		clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
3136
3137		/* Drop the cache for the part of the extent we didn't write. */
3138		btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
3139
3140		/*
3141		 * If the ordered extent had an IOERR or something else went
3142		 * wrong we need to return the space for this ordered extent
3143		 * back to the allocator.  We only free the extent in the
3144		 * truncated case if we didn't write out the extent at all.
3145		 *
3146		 * If we made it past insert_reserved_file_extent before we
3147		 * errored out then we don't need to do this as the accounting
3148		 * has already been done.
3149		 */
3150		if ((ret || !logical_len) &&
3151		    clear_reserved_extent &&
3152		    !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3153		    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3154			/*
3155			 * Discard the range before returning it back to the
3156			 * free space pool
3157			 */
3158			if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
3159				btrfs_discard_extent(fs_info,
3160						ordered_extent->disk_bytenr,
3161						ordered_extent->disk_num_bytes,
3162						NULL);
3163			btrfs_free_reserved_extent(fs_info,
3164					ordered_extent->disk_bytenr,
3165					ordered_extent->disk_num_bytes, 1);
3166		}
3167	}
3168
3169	/*
3170	 * This needs to be done to make sure anybody waiting knows we are done
3171	 * updating everything for this ordered extent.
3172	 */
3173	btrfs_remove_ordered_extent(inode, ordered_extent);
3174
3175	/* once for us */
3176	btrfs_put_ordered_extent(ordered_extent);
3177	/* once for the tree */
3178	btrfs_put_ordered_extent(ordered_extent);
3179
3180	return ret;
3181}
3182
3183static void finish_ordered_fn(struct btrfs_work *work)
3184{
3185	struct btrfs_ordered_extent *ordered_extent;
3186	ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3187	btrfs_finish_ordered_io(ordered_extent);
3188}
3189
3190void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3191					  struct page *page, u64 start,
3192					  u64 end, bool uptodate)
3193{
3194	trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
3195
3196	btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3197				       finish_ordered_fn, uptodate);
3198}
3199
3200/*
3201 * check_data_csum - verify checksum of one sector of uncompressed data
3202 * @inode:	inode
3203 * @io_bio:	btrfs_io_bio which contains the csum
3204 * @bio_offset:	offset to the beginning of the bio (in bytes)
3205 * @page:	page where is the data to be verified
3206 * @pgoff:	offset inside the page
3207 * @start:	logical offset in the file
3208 *
3209 * The length of such check is always one sector size.
3210 */
3211static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
3212			   u32 bio_offset, struct page *page, u32 pgoff,
3213			   u64 start)
3214{
3215	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3216	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
3217	char *kaddr;
3218	u32 len = fs_info->sectorsize;
3219	const u32 csum_size = fs_info->csum_size;
3220	unsigned int offset_sectors;
3221	u8 *csum_expected;
3222	u8 csum[BTRFS_CSUM_SIZE];
3223
3224	ASSERT(pgoff + len <= PAGE_SIZE);
3225
3226	offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3227	csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
3228
3229	kaddr = kmap_atomic(page);
3230	shash->tfm = fs_info->csum_shash;
3231
3232	crypto_shash_digest(shash, kaddr + pgoff, len, csum);
3233
3234	if (memcmp(csum, csum_expected, csum_size))
3235		goto zeroit;
3236
3237	kunmap_atomic(kaddr);
3238	return 0;
3239zeroit:
3240	btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3241				    io_bio->mirror_num);
3242	if (io_bio->device)
3243		btrfs_dev_stat_inc_and_print(io_bio->device,
3244					     BTRFS_DEV_STAT_CORRUPTION_ERRS);
3245	memset(kaddr + pgoff, 1, len);
3246	flush_dcache_page(page);
3247	kunmap_atomic(kaddr);
3248	return -EIO;
3249}
3250
3251/*
3252 * When reads are done, we need to check csums to verify the data is correct.
3253 * if there's a match, we allow the bio to finish.  If not, the code in
3254 * extent_io.c will try to find good copies for us.
3255 *
3256 * @bio_offset:	offset to the beginning of the bio (in bytes)
3257 * @start:	file offset of the range start
3258 * @end:	file offset of the range end (inclusive)
3259 *
3260 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3261 * csum match.
3262 */
3263unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3264				    struct page *page, u64 start, u64 end)
3265{
3266	struct inode *inode = page->mapping->host;
3267	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3268	struct btrfs_root *root = BTRFS_I(inode)->root;
3269	const u32 sectorsize = root->fs_info->sectorsize;
3270	u32 pg_off;
3271	unsigned int result = 0;
3272
3273	if (PageChecked(page)) {
3274		ClearPageChecked(page);
3275		return 0;
3276	}
3277
3278	/*
3279	 * For subpage case, above PageChecked is not safe as it's not subpage
3280	 * compatible.
3281	 * But for now only cow fixup and compressed read utilize PageChecked
3282	 * flag, while in this context we can easily use io_bio->csum to
3283	 * determine if we really need to do csum verification.
3284	 *
3285	 * So for now, just exit if io_bio->csum is NULL, as it means it's
3286	 * compressed read, and its compressed data csum has already been
3287	 * verified.
3288	 */
3289	if (io_bio->csum == NULL)
3290		return 0;
3291
3292	if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
3293		return 0;
3294
3295	if (!root->fs_info->csum_root)
3296		return 0;
3297
3298	ASSERT(page_offset(page) <= start &&
3299	       end <= page_offset(page) + PAGE_SIZE - 1);
3300	for (pg_off = offset_in_page(start);
3301	     pg_off < offset_in_page(end);
3302	     pg_off += sectorsize, bio_offset += sectorsize) {
3303		u64 file_offset = pg_off + page_offset(page);
3304		int ret;
3305
3306		if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3307		    test_range_bit(io_tree, file_offset,
3308				   file_offset + sectorsize - 1,
3309				   EXTENT_NODATASUM, 1, NULL)) {
3310			/* Skip the range without csum for data reloc inode */
3311			clear_extent_bits(io_tree, file_offset,
3312					  file_offset + sectorsize - 1,
3313					  EXTENT_NODATASUM);
3314			continue;
3315		}
3316		ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3317				      page_offset(page) + pg_off);
3318		if (ret < 0) {
3319			const int nr_bit = (pg_off - offset_in_page(start)) >>
3320				     root->fs_info->sectorsize_bits;
3321
3322			result |= (1U << nr_bit);
3323		}
3324	}
3325	return result;
3326}
3327
3328/*
3329 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3330 *
3331 * @inode: The inode we want to perform iput on
3332 *
3333 * This function uses the generic vfs_inode::i_count to track whether we should
3334 * just decrement it (in case it's > 1) or if this is the last iput then link
3335 * the inode to the delayed iput machinery. Delayed iputs are processed at
3336 * transaction commit time/superblock commit/cleaner kthread.
3337 */
3338void btrfs_add_delayed_iput(struct inode *inode)
3339{
3340	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3341	struct btrfs_inode *binode = BTRFS_I(inode);
3342
3343	if (atomic_add_unless(&inode->i_count, -1, 1))
3344		return;
3345
3346	atomic_inc(&fs_info->nr_delayed_iputs);
3347	spin_lock(&fs_info->delayed_iput_lock);
3348	ASSERT(list_empty(&binode->delayed_iput));
3349	list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3350	spin_unlock(&fs_info->delayed_iput_lock);
3351	if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3352		wake_up_process(fs_info->cleaner_kthread);
3353}
3354
3355static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3356				    struct btrfs_inode *inode)
3357{
3358	list_del_init(&inode->delayed_iput);
3359	spin_unlock(&fs_info->delayed_iput_lock);
3360	iput(&inode->vfs_inode);
3361	if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3362		wake_up(&fs_info->delayed_iputs_wait);
3363	spin_lock(&fs_info->delayed_iput_lock);
3364}
3365
3366static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3367				   struct btrfs_inode *inode)
3368{
3369	if (!list_empty(&inode->delayed_iput)) {
3370		spin_lock(&fs_info->delayed_iput_lock);
3371		if (!list_empty(&inode->delayed_iput))
3372			run_delayed_iput_locked(fs_info, inode);
3373		spin_unlock(&fs_info->delayed_iput_lock);
3374	}
3375}
3376
3377void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3378{
3379
3380	spin_lock(&fs_info->delayed_iput_lock);
3381	while (!list_empty(&fs_info->delayed_iputs)) {
3382		struct btrfs_inode *inode;
3383
3384		inode = list_first_entry(&fs_info->delayed_iputs,
3385				struct btrfs_inode, delayed_iput);
3386		run_delayed_iput_locked(fs_info, inode);
3387		cond_resched_lock(&fs_info->delayed_iput_lock);
3388	}
3389	spin_unlock(&fs_info->delayed_iput_lock);
3390}
3391
3392/**
3393 * Wait for flushing all delayed iputs
3394 *
3395 * @fs_info:  the filesystem
3396 *
3397 * This will wait on any delayed iputs that are currently running with KILLABLE
3398 * set.  Once they are all done running we will return, unless we are killed in
3399 * which case we return EINTR. This helps in user operations like fallocate etc
3400 * that might get blocked on the iputs.
3401 *
3402 * Return EINTR if we were killed, 0 if nothing's pending
3403 */
3404int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3405{
3406	int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3407			atomic_read(&fs_info->nr_delayed_iputs) == 0);
3408	if (ret)
3409		return -EINTR;
3410	return 0;
3411}
3412
3413/*
3414 * This creates an orphan entry for the given inode in case something goes wrong
3415 * in the middle of an unlink.
3416 */
3417int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3418		     struct btrfs_inode *inode)
3419{
3420	int ret;
3421
3422	ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3423	if (ret && ret != -EEXIST) {
3424		btrfs_abort_transaction(trans, ret);
3425		return ret;
3426	}
3427
3428	return 0;
3429}
3430
3431/*
3432 * We have done the delete so we can go ahead and remove the orphan item for
3433 * this particular inode.
3434 */
3435static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3436			    struct btrfs_inode *inode)
3437{
3438	return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
3439}
3440
3441/*
3442 * this cleans up any orphans that may be left on the list from the last use
3443 * of this root.
3444 */
3445int btrfs_orphan_cleanup(struct btrfs_root *root)
3446{
3447	struct btrfs_fs_info *fs_info = root->fs_info;
3448	struct btrfs_path *path;
3449	struct extent_buffer *leaf;
3450	struct btrfs_key key, found_key;
3451	struct btrfs_trans_handle *trans;
3452	struct inode *inode;
3453	u64 last_objectid = 0;
3454	int ret = 0, nr_unlink = 0;
3455
3456	if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3457		return 0;
3458
3459	path = btrfs_alloc_path();
3460	if (!path) {
3461		ret = -ENOMEM;
3462		goto out;
3463	}
3464	path->reada = READA_BACK;
3465
3466	key.objectid = BTRFS_ORPHAN_OBJECTID;
3467	key.type = BTRFS_ORPHAN_ITEM_KEY;
3468	key.offset = (u64)-1;
3469
3470	while (1) {
3471		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3472		if (ret < 0)
3473			goto out;
3474
3475		/*
3476		 * if ret == 0 means we found what we were searching for, which
3477		 * is weird, but possible, so only screw with path if we didn't
3478		 * find the key and see if we have stuff that matches
3479		 */
3480		if (ret > 0) {
3481			ret = 0;
3482			if (path->slots[0] == 0)
3483				break;
3484			path->slots[0]--;
3485		}
3486
3487		/* pull out the item */
3488		leaf = path->nodes[0];
3489		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3490
3491		/* make sure the item matches what we want */
3492		if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3493			break;
3494		if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3495			break;
3496
3497		/* release the path since we're done with it */
3498		btrfs_release_path(path);
3499
3500		/*
3501		 * this is where we are basically btrfs_lookup, without the
3502		 * crossing root thing.  we store the inode number in the
3503		 * offset of the orphan item.
3504		 */
3505
3506		if (found_key.offset == last_objectid) {
3507			btrfs_err(fs_info,
3508				  "Error removing orphan entry, stopping orphan cleanup");
3509			ret = -EINVAL;
3510			goto out;
3511		}
3512
3513		last_objectid = found_key.offset;
3514
3515		found_key.objectid = found_key.offset;
3516		found_key.type = BTRFS_INODE_ITEM_KEY;
3517		found_key.offset = 0;
3518		inode = btrfs_iget(fs_info->sb, last_objectid, root);
3519		ret = PTR_ERR_OR_ZERO(inode);
3520		if (ret && ret != -ENOENT)
3521			goto out;
3522
3523		if (ret == -ENOENT && root == fs_info->tree_root) {
3524			struct btrfs_root *dead_root;
3525			int is_dead_root = 0;
3526
3527			/*
3528			 * This is an orphan in the tree root. Currently these
3529			 * could come from 2 sources:
3530			 *  a) a root (snapshot/subvolume) deletion in progress
3531			 *  b) a free space cache inode
3532			 * We need to distinguish those two, as the orphan item
3533			 * for a root must not get deleted before the deletion
3534			 * of the snapshot/subvolume's tree completes.
3535			 *
3536			 * btrfs_find_orphan_roots() ran before us, which has
3537			 * found all deleted roots and loaded them into
3538			 * fs_info->fs_roots_radix. So here we can find if an
3539			 * orphan item corresponds to a deleted root by looking
3540			 * up the root from that radix tree.
3541			 */
3542
3543			spin_lock(&fs_info->fs_roots_radix_lock);
3544			dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3545							 (unsigned long)found_key.objectid);
3546			if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3547				is_dead_root = 1;
3548			spin_unlock(&fs_info->fs_roots_radix_lock);
3549
3550			if (is_dead_root) {
3551				/* prevent this orphan from being found again */
3552				key.offset = found_key.objectid - 1;
3553				continue;
3554			}
3555
3556		}
3557
3558		/*
3559		 * If we have an inode with links, there are a couple of
3560		 * possibilities:
3561		 *
3562		 * 1. We were halfway through creating fsverity metadata for the
3563		 * file. In that case, the orphan item represents incomplete
3564		 * fsverity metadata which must be cleaned up with
3565		 * btrfs_drop_verity_items and deleting the orphan item.
3566
3567		 * 2. Old kernels (before v3.12) used to create an
3568		 * orphan item for truncate indicating that there were possibly
3569		 * extent items past i_size that needed to be deleted. In v3.12,
3570		 * truncate was changed to update i_size in sync with the extent
3571		 * items, but the (useless) orphan item was still created. Since
3572		 * v4.18, we don't create the orphan item for truncate at all.
3573		 *
3574		 * So, this item could mean that we need to do a truncate, but
3575		 * only if this filesystem was last used on a pre-v3.12 kernel
3576		 * and was not cleanly unmounted. The odds of that are quite
3577		 * slim, and it's a pain to do the truncate now, so just delete
3578		 * the orphan item.
3579		 *
3580		 * It's also possible that this orphan item was supposed to be
3581		 * deleted but wasn't. The inode number may have been reused,
3582		 * but either way, we can delete the orphan item.
3583		 */
3584		if (ret == -ENOENT || inode->i_nlink) {
3585			if (!ret) {
3586				ret = btrfs_drop_verity_items(BTRFS_I(inode));
3587				iput(inode);
3588				if (ret)
3589					goto out;
3590			}
3591			trans = btrfs_start_transaction(root, 1);
3592			if (IS_ERR(trans)) {
3593				ret = PTR_ERR(trans);
3594				goto out;
3595			}
3596			btrfs_debug(fs_info, "auto deleting %Lu",
3597				    found_key.objectid);
3598			ret = btrfs_del_orphan_item(trans, root,
3599						    found_key.objectid);
3600			btrfs_end_transaction(trans);
3601			if (ret)
3602				goto out;
3603			continue;
3604		}
3605
3606		nr_unlink++;
3607
3608		/* this will do delete_inode and everything for us */
3609		iput(inode);
3610	}
3611	/* release the path since we're done with it */
3612	btrfs_release_path(path);
3613
3614	root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3615
3616	if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3617		trans = btrfs_join_transaction(root);
3618		if (!IS_ERR(trans))
3619			btrfs_end_transaction(trans);
3620	}
3621
3622	if (nr_unlink)
3623		btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3624
3625out:
3626	if (ret)
3627		btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3628	btrfs_free_path(path);
3629	return ret;
3630}
3631
3632/*
3633 * very simple check to peek ahead in the leaf looking for xattrs.  If we
3634 * don't find any xattrs, we know there can't be any acls.
3635 *
3636 * slot is the slot the inode is in, objectid is the objectid of the inode
3637 */
3638static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3639					  int slot, u64 objectid,
3640					  int *first_xattr_slot)
3641{
3642	u32 nritems = btrfs_header_nritems(leaf);
3643	struct btrfs_key found_key;
3644	static u64 xattr_access = 0;
3645	static u64 xattr_default = 0;
3646	int scanned = 0;
3647
3648	if (!xattr_access) {
3649		xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3650					strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3651		xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3652					strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3653	}
3654
3655	slot++;
3656	*first_xattr_slot = -1;
3657	while (slot < nritems) {
3658		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3659
3660		/* we found a different objectid, there must not be acls */
3661		if (found_key.objectid != objectid)
3662			return 0;
3663
3664		/* we found an xattr, assume we've got an acl */
3665		if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3666			if (*first_xattr_slot == -1)
3667				*first_xattr_slot = slot;
3668			if (found_key.offset == xattr_access ||
3669			    found_key.offset == xattr_default)
3670				return 1;
3671		}
3672
3673		/*
3674		 * we found a key greater than an xattr key, there can't
3675		 * be any acls later on
3676		 */
3677		if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3678			return 0;
3679
3680		slot++;
3681		scanned++;
3682
3683		/*
3684		 * it goes inode, inode backrefs, xattrs, extents,
3685		 * so if there are a ton of hard links to an inode there can
3686		 * be a lot of backrefs.  Don't waste time searching too hard,
3687		 * this is just an optimization
3688		 */
3689		if (scanned >= 8)
3690			break;
3691	}
3692	/* we hit the end of the leaf before we found an xattr or
3693	 * something larger than an xattr.  We have to assume the inode
3694	 * has acls
3695	 */
3696	if (*first_xattr_slot == -1)
3697		*first_xattr_slot = slot;
3698	return 1;
3699}
3700
3701/*
3702 * read an inode from the btree into the in-memory inode
3703 */
3704static int btrfs_read_locked_inode(struct inode *inode,
3705				   struct btrfs_path *in_path)
3706{
3707	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3708	struct btrfs_path *path = in_path;
3709	struct extent_buffer *leaf;
3710	struct btrfs_inode_item *inode_item;
3711	struct btrfs_root *root = BTRFS_I(inode)->root;
3712	struct btrfs_key location;
3713	unsigned long ptr;
3714	int maybe_acls;
3715	u32 rdev;
3716	int ret;
3717	bool filled = false;
3718	int first_xattr_slot;
3719
3720	ret = btrfs_fill_inode(inode, &rdev);
3721	if (!ret)
3722		filled = true;
3723
3724	if (!path) {
3725		path = btrfs_alloc_path();
3726		if (!path)
3727			return -ENOMEM;
3728	}
3729
3730	memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3731
3732	ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3733	if (ret) {
3734		if (path != in_path)
3735			btrfs_free_path(path);
3736		return ret;
3737	}
3738
3739	leaf = path->nodes[0];
3740
3741	if (filled)
3742		goto cache_index;
3743
3744	inode_item = btrfs_item_ptr(leaf, path->slots[0],
3745				    struct btrfs_inode_item);
3746	inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3747	set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3748	i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3749	i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3750	btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
3751	btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3752			round_up(i_size_read(inode), fs_info->sectorsize));
3753
3754	inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3755	inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3756
3757	inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3758	inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3759
3760	inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3761	inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3762
3763	BTRFS_I(inode)->i_otime.tv_sec =
3764		btrfs_timespec_sec(leaf, &inode_item->otime);
3765	BTRFS_I(inode)->i_otime.tv_nsec =
3766		btrfs_timespec_nsec(leaf, &inode_item->otime);
3767
3768	inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3769	BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3770	BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3771
3772	inode_set_iversion_queried(inode,
3773				   btrfs_inode_sequence(leaf, inode_item));
3774	inode->i_generation = BTRFS_I(inode)->generation;
3775	inode->i_rdev = 0;
3776	rdev = btrfs_inode_rdev(leaf, inode_item);
3777
3778	BTRFS_I(inode)->index_cnt = (u64)-1;
3779	btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3780				&BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
3781
3782cache_index:
3783	/*
3784	 * If we were modified in the current generation and evicted from memory
3785	 * and then re-read we need to do a full sync since we don't have any
3786	 * idea about which extents were modified before we were evicted from
3787	 * cache.
3788	 *
3789	 * This is required for both inode re-read from disk and delayed inode
3790	 * in delayed_nodes_tree.
3791	 */
3792	if (BTRFS_I(inode)->last_trans == fs_info->generation)
3793		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3794			&BTRFS_I(inode)->runtime_flags);
3795
3796	/*
3797	 * We don't persist the id of the transaction where an unlink operation
3798	 * against the inode was last made. So here we assume the inode might
3799	 * have been evicted, and therefore the exact value of last_unlink_trans
3800	 * lost, and set it to last_trans to avoid metadata inconsistencies
3801	 * between the inode and its parent if the inode is fsync'ed and the log
3802	 * replayed. For example, in the scenario:
3803	 *
3804	 * touch mydir/foo
3805	 * ln mydir/foo mydir/bar
3806	 * sync
3807	 * unlink mydir/bar
3808	 * echo 2 > /proc/sys/vm/drop_caches   # evicts inode
3809	 * xfs_io -c fsync mydir/foo
3810	 * <power failure>
3811	 * mount fs, triggers fsync log replay
3812	 *
3813	 * We must make sure that when we fsync our inode foo we also log its
3814	 * parent inode, otherwise after log replay the parent still has the
3815	 * dentry with the "bar" name but our inode foo has a link count of 1
3816	 * and doesn't have an inode ref with the name "bar" anymore.
3817	 *
3818	 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3819	 * but it guarantees correctness at the expense of occasional full
3820	 * transaction commits on fsync if our inode is a directory, or if our
3821	 * inode is not a directory, logging its parent unnecessarily.
3822	 */
3823	BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3824
3825	/*
3826	 * Same logic as for last_unlink_trans. We don't persist the generation
3827	 * of the last transaction where this inode was used for a reflink
3828	 * operation, so after eviction and reloading the inode we must be
3829	 * pessimistic and assume the last transaction that modified the inode.
3830	 */
3831	BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3832
3833	path->slots[0]++;
3834	if (inode->i_nlink != 1 ||
3835	    path->slots[0] >= btrfs_header_nritems(leaf))
3836		goto cache_acl;
3837
3838	btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3839	if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3840		goto cache_acl;
3841
3842	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3843	if (location.type == BTRFS_INODE_REF_KEY) {
3844		struct btrfs_inode_ref *ref;
3845
3846		ref = (struct btrfs_inode_ref *)ptr;
3847		BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3848	} else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3849		struct btrfs_inode_extref *extref;
3850
3851		extref = (struct btrfs_inode_extref *)ptr;
3852		BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3853								     extref);
3854	}
3855cache_acl:
3856	/*
3857	 * try to precache a NULL acl entry for files that don't have
3858	 * any xattrs or acls
3859	 */
3860	maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3861			btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3862	if (first_xattr_slot != -1) {
3863		path->slots[0] = first_xattr_slot;
3864		ret = btrfs_load_inode_props(inode, path);
3865		if (ret)
3866			btrfs_err(fs_info,
3867				  "error loading props for ino %llu (root %llu): %d",
3868				  btrfs_ino(BTRFS_I(inode)),
3869				  root->root_key.objectid, ret);
3870	}
3871	if (path != in_path)
3872		btrfs_free_path(path);
3873
3874	if (!maybe_acls)
3875		cache_no_acl(inode);
3876
3877	switch (inode->i_mode & S_IFMT) {
3878	case S_IFREG:
3879		inode->i_mapping->a_ops = &btrfs_aops;
3880		inode->i_fop = &btrfs_file_operations;
3881		inode->i_op = &btrfs_file_inode_operations;
3882		break;
3883	case S_IFDIR:
3884		inode->i_fop = &btrfs_dir_file_operations;
3885		inode->i_op = &btrfs_dir_inode_operations;
3886		break;
3887	case S_IFLNK:
3888		inode->i_op = &btrfs_symlink_inode_operations;
3889		inode_nohighmem(inode);
3890		inode->i_mapping->a_ops = &btrfs_aops;
3891		break;
3892	default:
3893		inode->i_op = &btrfs_special_inode_operations;
3894		init_special_inode(inode, inode->i_mode, rdev);
3895		break;
3896	}
3897
3898	btrfs_sync_inode_flags_to_i_flags(inode);
3899	return 0;
3900}
3901
3902/*
3903 * given a leaf and an inode, copy the inode fields into the leaf
3904 */
3905static void fill_inode_item(struct btrfs_trans_handle *trans,
3906			    struct extent_buffer *leaf,
3907			    struct btrfs_inode_item *item,
3908			    struct inode *inode)
3909{
3910	struct btrfs_map_token token;
3911	u64 flags;
3912
3913	btrfs_init_map_token(&token, leaf);
3914
3915	btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3916	btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3917	btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3918	btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3919	btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3920
3921	btrfs_set_token_timespec_sec(&token, &item->atime,
3922				     inode->i_atime.tv_sec);
3923	btrfs_set_token_timespec_nsec(&token, &item->atime,
3924				      inode->i_atime.tv_nsec);
3925
3926	btrfs_set_token_timespec_sec(&token, &item->mtime,
3927				     inode->i_mtime.tv_sec);
3928	btrfs_set_token_timespec_nsec(&token, &item->mtime,
3929				      inode->i_mtime.tv_nsec);
3930
3931	btrfs_set_token_timespec_sec(&token, &item->ctime,
3932				     inode->i_ctime.tv_sec);
3933	btrfs_set_token_timespec_nsec(&token, &item->ctime,
3934				      inode->i_ctime.tv_nsec);
3935
3936	btrfs_set_token_timespec_sec(&token, &item->otime,
3937				     BTRFS_I(inode)->i_otime.tv_sec);
3938	btrfs_set_token_timespec_nsec(&token, &item->otime,
3939				      BTRFS_I(inode)->i_otime.tv_nsec);
3940
3941	btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3942	btrfs_set_token_inode_generation(&token, item,
3943					 BTRFS_I(inode)->generation);
3944	btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3945	btrfs_set_token_inode_transid(&token, item, trans->transid);
3946	btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3947	flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3948					  BTRFS_I(inode)->ro_flags);
3949	btrfs_set_token_inode_flags(&token, item, flags);
3950	btrfs_set_token_inode_block_group(&token, item, 0);
3951}
3952
3953/*
3954 * copy everything in the in-memory inode into the btree.
3955 */
3956static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3957				struct btrfs_root *root,
3958				struct btrfs_inode *inode)
3959{
3960	struct btrfs_inode_item *inode_item;
3961	struct btrfs_path *path;
3962	struct extent_buffer *leaf;
3963	int ret;
3964
3965	path = btrfs_alloc_path();
3966	if (!path)
3967		return -ENOMEM;
3968
3969	ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
3970	if (ret) {
3971		if (ret > 0)
3972			ret = -ENOENT;
3973		goto failed;
3974	}
3975
3976	leaf = path->nodes[0];
3977	inode_item = btrfs_item_ptr(leaf, path->slots[0],
3978				    struct btrfs_inode_item);
3979
3980	fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
3981	btrfs_mark_buffer_dirty(leaf);
3982	btrfs_set_inode_last_trans(trans, inode);
3983	ret = 0;
3984failed:
3985	btrfs_free_path(path);
3986	return ret;
3987}
3988
3989/*
3990 * copy everything in the in-memory inode into the btree.
3991 */
3992noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3993				struct btrfs_root *root,
3994				struct btrfs_inode *inode)
3995{
3996	struct btrfs_fs_info *fs_info = root->fs_info;
3997	int ret;
3998
3999	/*
4000	 * If the inode is a free space inode, we can deadlock during commit
4001	 * if we put it into the delayed code.
4002	 *
4003	 * The data relocation inode should also be directly updated
4004	 * without delay
4005	 */
4006	if (!btrfs_is_free_space_inode(inode)
4007	    && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
4008	    && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
4009		btrfs_update_root_times(trans, root);
4010
4011		ret = btrfs_delayed_update_inode(trans, root, inode);
4012		if (!ret)
4013			btrfs_set_inode_last_trans(trans, inode);
4014		return ret;
4015	}
4016
4017	return btrfs_update_inode_item(trans, root, inode);
4018}
4019
4020int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4021				struct btrfs_root *root, struct btrfs_inode *inode)
4022{
4023	int ret;
4024
4025	ret = btrfs_update_inode(trans, root, inode);
4026	if (ret == -ENOSPC)
4027		return btrfs_update_inode_item(trans, root, inode);
4028	return ret;
4029}
4030
4031/*
4032 * unlink helper that gets used here in inode.c and in the tree logging
4033 * recovery code.  It remove a link in a directory with a given name, and
4034 * also drops the back refs in the inode to the directory
4035 */
4036static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4037				struct btrfs_root *root,
4038				struct btrfs_inode *dir,
4039				struct btrfs_inode *inode,
4040				const char *name, int name_len)
4041{
4042	struct btrfs_fs_info *fs_info = root->fs_info;
4043	struct btrfs_path *path;
4044	int ret = 0;
4045	struct btrfs_dir_item *di;
4046	u64 index;
4047	u64 ino = btrfs_ino(inode);
4048	u64 dir_ino = btrfs_ino(dir);
4049
4050	path = btrfs_alloc_path();
4051	if (!path) {
4052		ret = -ENOMEM;
4053		goto out;
4054	}
4055
4056	di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4057				    name, name_len, -1);
4058	if (IS_ERR_OR_NULL(di)) {
4059		ret = di ? PTR_ERR(di) : -ENOENT;
4060		goto err;
4061	}
4062	ret = btrfs_delete_one_dir_name(trans, root, path, di);
4063	if (ret)
4064		goto err;
4065	btrfs_release_path(path);
4066
4067	/*
4068	 * If we don't have dir index, we have to get it by looking up
4069	 * the inode ref, since we get the inode ref, remove it directly,
4070	 * it is unnecessary to do delayed deletion.
4071	 *
4072	 * But if we have dir index, needn't search inode ref to get it.
4073	 * Since the inode ref is close to the inode item, it is better
4074	 * that we delay to delete it, and just do this deletion when
4075	 * we update the inode item.
4076	 */
4077	if (inode->dir_index) {
4078		ret = btrfs_delayed_delete_inode_ref(inode);
4079		if (!ret) {
4080			index = inode->dir_index;
4081			goto skip_backref;
4082		}
4083	}
4084
4085	ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4086				  dir_ino, &index);
4087	if (ret) {
4088		btrfs_info(fs_info,
4089			"failed to delete reference to %.*s, inode %llu parent %llu",
4090			name_len, name, ino, dir_ino);
4091		btrfs_abort_transaction(trans, ret);
4092		goto err;
4093	}
4094skip_backref:
4095	ret = btrfs_delete_delayed_dir_index(trans, dir, index);
4096	if (ret) {
4097		btrfs_abort_transaction(trans, ret);
4098		goto err;
4099	}
4100
4101	ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4102			dir_ino);
4103	if (ret != 0 && ret != -ENOENT) {
4104		btrfs_abort_transaction(trans, ret);
4105		goto err;
4106	}
4107
4108	ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4109			index);
4110	if (ret == -ENOENT)
4111		ret = 0;
4112	else if (ret)
4113		btrfs_abort_transaction(trans, ret);
4114
4115	/*
4116	 * If we have a pending delayed iput we could end up with the final iput
4117	 * being run in btrfs-cleaner context.  If we have enough of these built
4118	 * up we can end up burning a lot of time in btrfs-cleaner without any
4119	 * way to throttle the unlinks.  Since we're currently holding a ref on
4120	 * the inode we can run the delayed iput here without any issues as the
4121	 * final iput won't be done until after we drop the ref we're currently
4122	 * holding.
4123	 */
4124	btrfs_run_delayed_iput(fs_info, inode);
4125err:
4126	btrfs_free_path(path);
4127	if (ret)
4128		goto out;
4129
4130	btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
4131	inode_inc_iversion(&inode->vfs_inode);
4132	inode_inc_iversion(&dir->vfs_inode);
4133	inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4134		dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4135	ret = btrfs_update_inode(trans, root, dir);
4136out:
4137	return ret;
4138}
4139
4140int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4141		       struct btrfs_root *root,
4142		       struct btrfs_inode *dir, struct btrfs_inode *inode,
4143		       const char *name, int name_len)
4144{
4145	int ret;
4146	ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4147	if (!ret) {
4148		drop_nlink(&inode->vfs_inode);
4149		ret = btrfs_update_inode(trans, root, inode);
4150	}
4151	return ret;
4152}
4153
4154/*
4155 * helper to start transaction for unlink and rmdir.
4156 *
4157 * unlink and rmdir are special in btrfs, they do not always free space, so
4158 * if we cannot make our reservations the normal way try and see if there is
4159 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4160 * allow the unlink to occur.
4161 */
4162static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4163{
4164	struct btrfs_root *root = BTRFS_I(dir)->root;
4165
4166	/*
4167	 * 1 for the possible orphan item
4168	 * 1 for the dir item
4169	 * 1 for the dir index
4170	 * 1 for the inode ref
4171	 * 1 for the inode
4172	 */
4173	return btrfs_start_transaction_fallback_global_rsv(root, 5);
4174}
4175
4176static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4177{
4178	struct btrfs_root *root = BTRFS_I(dir)->root;
4179	struct btrfs_trans_handle *trans;
4180	struct inode *inode = d_inode(dentry);
4181	int ret;
4182
4183	trans = __unlink_start_trans(dir);
4184	if (IS_ERR(trans))
4185		return PTR_ERR(trans);
4186
4187	btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4188			0);
4189
4190	ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4191			BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4192			dentry->d_name.len);
4193	if (ret)
4194		goto out;
4195
4196	if (inode->i_nlink == 0) {
4197		ret = btrfs_orphan_add(trans, BTRFS_I(inode));
4198		if (ret)
4199			goto out;
4200	}
4201
4202out:
4203	btrfs_end_transaction(trans);
4204	btrfs_btree_balance_dirty(root->fs_info);
4205	return ret;
4206}
4207
4208static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4209			       struct inode *dir, struct dentry *dentry)
4210{
4211	struct btrfs_root *root = BTRFS_I(dir)->root;
4212	struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
4213	struct btrfs_path *path;
4214	struct extent_buffer *leaf;
4215	struct btrfs_dir_item *di;
4216	struct btrfs_key key;
4217	const char *name = dentry->d_name.name;
4218	int name_len = dentry->d_name.len;
4219	u64 index;
4220	int ret;
4221	u64 objectid;
4222	u64 dir_ino = btrfs_ino(BTRFS_I(dir));
4223
4224	if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4225		objectid = inode->root->root_key.objectid;
4226	} else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4227		objectid = inode->location.objectid;
4228	} else {
4229		WARN_ON(1);
4230		return -EINVAL;
4231	}
4232
4233	path = btrfs_alloc_path();
4234	if (!path)
4235		return -ENOMEM;
4236
4237	di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4238				   name, name_len, -1);
4239	if (IS_ERR_OR_NULL(di)) {
4240		ret = di ? PTR_ERR(di) : -ENOENT;
4241		goto out;
4242	}
4243
4244	leaf = path->nodes[0];
4245	btrfs_dir_item_key_to_cpu(leaf, di, &key);
4246	WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4247	ret = btrfs_delete_one_dir_name(trans, root, path, di);
4248	if (ret) {
4249		btrfs_abort_transaction(trans, ret);
4250		goto out;
4251	}
4252	btrfs_release_path(path);
4253
4254	/*
4255	 * This is a placeholder inode for a subvolume we didn't have a
4256	 * reference to at the time of the snapshot creation.  In the meantime
4257	 * we could have renamed the real subvol link into our snapshot, so
4258	 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
4259	 * Instead simply lookup the dir_index_item for this entry so we can
4260	 * remove it.  Otherwise we know we have a ref to the root and we can
4261	 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4262	 */
4263	if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4264		di = btrfs_search_dir_index_item(root, path, dir_ino,
4265						 name, name_len);
4266		if (IS_ERR_OR_NULL(di)) {
4267			if (!di)
4268				ret = -ENOENT;
4269			else
4270				ret = PTR_ERR(di);
4271			btrfs_abort_transaction(trans, ret);
4272			goto out;
4273		}
4274
4275		leaf = path->nodes[0];
4276		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4277		index = key.offset;
4278		btrfs_release_path(path);
4279	} else {
4280		ret = btrfs_del_root_ref(trans, objectid,
4281					 root->root_key.objectid, dir_ino,
4282					 &index, name, name_len);
4283		if (ret) {
4284			btrfs_abort_transaction(trans, ret);
4285			goto out;
4286		}
4287	}
4288
4289	ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
4290	if (ret) {
4291		btrfs_abort_transaction(trans, ret);
4292		goto out;
4293	}
4294
4295	btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
4296	inode_inc_iversion(dir);
4297	dir->i_mtime = dir->i_ctime = current_time(dir);
4298	ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
4299	if (ret)
4300		btrfs_abort_transaction(trans, ret);
4301out:
4302	btrfs_free_path(path);
4303	return ret;
4304}
4305
4306/*
4307 * Helper to check if the subvolume references other subvolumes or if it's
4308 * default.
4309 */
4310static noinline int may_destroy_subvol(struct btrfs_root *root)
4311{
4312	struct btrfs_fs_info *fs_info = root->fs_info;
4313	struct btrfs_path *path;
4314	struct btrfs_dir_item *di;
4315	struct btrfs_key key;
4316	u64 dir_id;
4317	int ret;
4318
4319	path = btrfs_alloc_path();
4320	if (!path)
4321		return -ENOMEM;
4322
4323	/* Make sure this root isn't set as the default subvol */
4324	dir_id = btrfs_super_root_dir(fs_info->super_copy);
4325	di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4326				   dir_id, "default", 7, 0);
4327	if (di && !IS_ERR(di)) {
4328		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4329		if (key.objectid == root->root_key.objectid) {
4330			ret = -EPERM;
4331			btrfs_err(fs_info,
4332				  "deleting default subvolume %llu is not allowed",
4333				  key.objectid);
4334			goto out;
4335		}
4336		btrfs_release_path(path);
4337	}
4338
4339	key.objectid = root->root_key.objectid;
4340	key.type = BTRFS_ROOT_REF_KEY;
4341	key.offset = (u64)-1;
4342
4343	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4344	if (ret < 0)
4345		goto out;
4346	BUG_ON(ret == 0);
4347
4348	ret = 0;
4349	if (path->slots[0] > 0) {
4350		path->slots[0]--;
4351		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4352		if (key.objectid == root->root_key.objectid &&
4353		    key.type == BTRFS_ROOT_REF_KEY)
4354			ret = -ENOTEMPTY;
4355	}
4356out:
4357	btrfs_free_path(path);
4358	return ret;
4359}
4360
4361/* Delete all dentries for inodes belonging to the root */
4362static void btrfs_prune_dentries(struct btrfs_root *root)
4363{
4364	struct btrfs_fs_info *fs_info = root->fs_info;
4365	struct rb_node *node;
4366	struct rb_node *prev;
4367	struct btrfs_inode *entry;
4368	struct inode *inode;
4369	u64 objectid = 0;
4370
4371	if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4372		WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4373
4374	spin_lock(&root->inode_lock);
4375again:
4376	node = root->inode_tree.rb_node;
4377	prev = NULL;
4378	while (node) {
4379		prev = node;
4380		entry = rb_entry(node, struct btrfs_inode, rb_node);
4381
4382		if (objectid < btrfs_ino(entry))
4383			node = node->rb_left;
4384		else if (objectid > btrfs_ino(entry))
4385			node = node->rb_right;
4386		else
4387			break;
4388	}
4389	if (!node) {
4390		while (prev) {
4391			entry = rb_entry(prev, struct btrfs_inode, rb_node);
4392			if (objectid <= btrfs_ino(entry)) {
4393				node = prev;
4394				break;
4395			}
4396			prev = rb_next(prev);
4397		}
4398	}
4399	while (node) {
4400		entry = rb_entry(node, struct btrfs_inode, rb_node);
4401		objectid = btrfs_ino(entry) + 1;
4402		inode = igrab(&entry->vfs_inode);
4403		if (inode) {
4404			spin_unlock(&root->inode_lock);
4405			if (atomic_read(&inode->i_count) > 1)
4406				d_prune_aliases(inode);
4407			/*
4408			 * btrfs_drop_inode will have it removed from the inode
4409			 * cache when its usage count hits zero.
4410			 */
4411			iput(inode);
4412			cond_resched();
4413			spin_lock(&root->inode_lock);
4414			goto again;
4415		}
4416
4417		if (cond_resched_lock(&root->inode_lock))
4418			goto again;
4419
4420		node = rb_next(node);
4421	}
4422	spin_unlock(&root->inode_lock);
4423}
4424
4425int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4426{
4427	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4428	struct btrfs_root *root = BTRFS_I(dir)->root;
4429	struct inode *inode = d_inode(dentry);
4430	struct btrfs_root *dest = BTRFS_I(inode)->root;
4431	struct btrfs_trans_handle *trans;
4432	struct btrfs_block_rsv block_rsv;
4433	u64 root_flags;
4434	int ret;
4435
4436	/*
4437	 * Don't allow to delete a subvolume with send in progress. This is
4438	 * inside the inode lock so the error handling that has to drop the bit
4439	 * again is not run concurrently.
4440	 */
4441	spin_lock(&dest->root_item_lock);
4442	if (dest->send_in_progress) {
4443		spin_unlock(&dest->root_item_lock);
4444		btrfs_warn(fs_info,
4445			   "attempt to delete subvolume %llu during send",
4446			   dest->root_key.objectid);
4447		return -EPERM;
4448	}
4449	root_flags = btrfs_root_flags(&dest->root_item);
4450	btrfs_set_root_flags(&dest->root_item,
4451			     root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4452	spin_unlock(&dest->root_item_lock);
4453
4454	down_write(&fs_info->subvol_sem);
4455
4456	ret = may_destroy_subvol(dest);
4457	if (ret)
4458		goto out_up_write;
4459
4460	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4461	/*
4462	 * One for dir inode,
4463	 * two for dir entries,
4464	 * two for root ref/backref.
4465	 */
4466	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4467	if (ret)
4468		goto out_up_write;
4469
4470	trans = btrfs_start_transaction(root, 0);
4471	if (IS_ERR(trans)) {
4472		ret = PTR_ERR(trans);
4473		goto out_release;
4474	}
4475	trans->block_rsv = &block_rsv;
4476	trans->bytes_reserved = block_rsv.size;
4477
4478	btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4479
4480	ret = btrfs_unlink_subvol(trans, dir, dentry);
4481	if (ret) {
4482		btrfs_abort_transaction(trans, ret);
4483		goto out_end_trans;
4484	}
4485
4486	ret = btrfs_record_root_in_trans(trans, dest);
4487	if (ret) {
4488		btrfs_abort_transaction(trans, ret);
4489		goto out_end_trans;
4490	}
4491
4492	memset(&dest->root_item.drop_progress, 0,
4493		sizeof(dest->root_item.drop_progress));
4494	btrfs_set_root_drop_level(&dest->root_item, 0);
4495	btrfs_set_root_refs(&dest->root_item, 0);
4496
4497	if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4498		ret = btrfs_insert_orphan_item(trans,
4499					fs_info->tree_root,
4500					dest->root_key.objectid);
4501		if (ret) {
4502			btrfs_abort_transaction(trans, ret);
4503			goto out_end_trans;
4504		}
4505	}
4506
4507	ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
4508				  BTRFS_UUID_KEY_SUBVOL,
4509				  dest->root_key.objectid);
4510	if (ret && ret != -ENOENT) {
4511		btrfs_abort_transaction(trans, ret);
4512		goto out_end_trans;
4513	}
4514	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
4515		ret = btrfs_uuid_tree_remove(trans,
4516					  dest->root_item.received_uuid,
4517					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4518					  dest->root_key.objectid);
4519		if (ret && ret != -ENOENT) {
4520			btrfs_abort_transaction(trans, ret);
4521			goto out_end_trans;
4522		}
4523	}
4524
4525	free_anon_bdev(dest->anon_dev);
4526	dest->anon_dev = 0;
4527out_end_trans:
4528	trans->block_rsv = NULL;
4529	trans->bytes_reserved = 0;
4530	ret = btrfs_end_transaction(trans);
4531	inode->i_flags |= S_DEAD;
4532out_release:
4533	btrfs_subvolume_release_metadata(root, &block_rsv);
4534out_up_write:
4535	up_write(&fs_info->subvol_sem);
4536	if (ret) {
4537		spin_lock(&dest->root_item_lock);
4538		root_flags = btrfs_root_flags(&dest->root_item);
4539		btrfs_set_root_flags(&dest->root_item,
4540				root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4541		spin_unlock(&dest->root_item_lock);
4542	} else {
4543		d_invalidate(dentry);
4544		btrfs_prune_dentries(dest);
4545		ASSERT(dest->send_in_progress == 0);
4546	}
4547
4548	return ret;
4549}
4550
4551static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4552{
4553	struct inode *inode = d_inode(dentry);
4554	int err = 0;
4555	struct btrfs_root *root = BTRFS_I(dir)->root;
4556	struct btrfs_trans_handle *trans;
4557	u64 last_unlink_trans;
4558
4559	if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4560		return -ENOTEMPTY;
4561	if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
4562		return btrfs_delete_subvolume(dir, dentry);
4563
4564	trans = __unlink_start_trans(dir);
4565	if (IS_ERR(trans))
4566		return PTR_ERR(trans);
4567
4568	if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4569		err = btrfs_unlink_subvol(trans, dir, dentry);
4570		goto out;
4571	}
4572
4573	err = btrfs_orphan_add(trans, BTRFS_I(inode));
4574	if (err)
4575		goto out;
4576
4577	last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4578
4579	/* now the directory is empty */
4580	err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4581			BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4582			dentry->d_name.len);
4583	if (!err) {
4584		btrfs_i_size_write(BTRFS_I(inode), 0);
4585		/*
4586		 * Propagate the last_unlink_trans value of the deleted dir to
4587		 * its parent directory. This is to prevent an unrecoverable
4588		 * log tree in the case we do something like this:
4589		 * 1) create dir foo
4590		 * 2) create snapshot under dir foo
4591		 * 3) delete the snapshot
4592		 * 4) rmdir foo
4593		 * 5) mkdir foo
4594		 * 6) fsync foo or some file inside foo
4595		 */
4596		if (last_unlink_trans >= trans->transid)
4597			BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4598	}
4599out:
4600	btrfs_end_transaction(trans);
4601	btrfs_btree_balance_dirty(root->fs_info);
4602
4603	return err;
4604}
4605
4606/*
4607 * Return this if we need to call truncate_block for the last bit of the
4608 * truncate.
4609 */
4610#define NEED_TRUNCATE_BLOCK 1
4611
4612/*
4613 * Remove inode items from a given root.
4614 *
4615 * @trans:		A transaction handle.
4616 * @root:		The root from which to remove items.
4617 * @inode:		The inode whose items we want to remove.
4618 * @new_size:		The new i_size for the inode. This is only applicable when
4619 *			@min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4620 * @min_type:		The minimum key type to remove. All keys with a type
4621 *			greater than this value are removed and all keys with
4622 *			this type are removed only if their offset is >= @new_size.
4623 * @extents_found:	Output parameter that will contain the number of file
4624 *			extent items that were removed or adjusted to the new
4625 *			inode i_size. The caller is responsible for initializing
4626 *			the counter. Also, it can be NULL if the caller does not
4627 *			need this counter.
4628 *
4629 * Remove all keys associated with the inode from the given root that have a key
4630 * with a type greater than or equals to @min_type. When @min_type has a value of
4631 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4632 * greater than or equals to @new_size. If a file extent item that starts before
4633 * @new_size and ends after it is found, its length is adjusted.
4634 *
4635 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4636 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
4637 */
4638int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4639			       struct btrfs_root *root,
4640			       struct btrfs_inode *inode,
4641			       u64 new_size, u32 min_type,
4642			       u64 *extents_found)
4643{
4644	struct btrfs_fs_info *fs_info = root->fs_info;
4645	struct btrfs_path *path;
4646	struct extent_buffer *leaf;
4647	struct btrfs_file_extent_item *fi;
4648	struct btrfs_key key;
4649	struct btrfs_key found_key;
4650	u64 extent_start = 0;
4651	u64 extent_num_bytes = 0;
4652	u64 extent_offset = 0;
4653	u64 item_end = 0;
4654	u64 last_size = new_size;
4655	u32 found_type = (u8)-1;
4656	int found_extent;
4657	int del_item;
4658	int pending_del_nr = 0;
4659	int pending_del_slot = 0;
4660	int extent_type = -1;
4661	int ret;
4662	u64 ino = btrfs_ino(inode);
4663	u64 bytes_deleted = 0;
4664	bool be_nice = false;
4665	bool should_throttle = false;
4666	const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4667	struct extent_state *cached_state = NULL;
4668
4669	BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4670
4671	/*
4672	 * For non-free space inodes and non-shareable roots, we want to back
4673	 * off from time to time.  This means all inodes in subvolume roots,
4674	 * reloc roots, and data reloc roots.
4675	 */
4676	if (!btrfs_is_free_space_inode(inode) &&
4677	    test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
4678		be_nice = true;
4679
4680	path = btrfs_alloc_path();
4681	if (!path)
4682		return -ENOMEM;
4683	path->reada = READA_BACK;
4684
4685	if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4686		lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
4687				 &cached_state);
4688
4689		/*
4690		 * We want to drop from the next block forward in case this
4691		 * new size is not block aligned since we will be keeping the
4692		 * last block of the extent just the way it is.
4693		 */
4694		btrfs_drop_extent_cache(inode, ALIGN(new_size,
4695					fs_info->sectorsize),
4696					(u64)-1, 0);
4697	}
4698
4699	/*
4700	 * This function is also used to drop the items in the log tree before
4701	 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4702	 * it is used to drop the logged items. So we shouldn't kill the delayed
4703	 * items.
4704	 */
4705	if (min_type == 0 && root == inode->root)
4706		btrfs_kill_delayed_inode_items(inode);
4707
4708	key.objectid = ino;
4709	key.offset = (u64)-1;
4710	key.type = (u8)-1;
4711
4712search_again:
4713	/*
4714	 * with a 16K leaf size and 128MB extents, you can actually queue
4715	 * up a huge file in a single leaf.  Most of the time that
4716	 * bytes_deleted is > 0, it will be huge by the time we get here
4717	 */
4718	if (be_nice && bytes_deleted > SZ_32M &&
4719	    btrfs_should_end_transaction(trans)) {
4720		ret = -EAGAIN;
4721		goto out;
4722	}
4723
4724	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4725	if (ret < 0)
4726		goto out;
4727
4728	if (ret > 0) {
4729		ret = 0;
4730		/* there are no items in the tree for us to truncate, we're
4731		 * done
4732		 */
4733		if (path->slots[0] == 0)
4734			goto out;
4735		path->slots[0]--;
4736	}
4737
4738	while (1) {
4739		u64 clear_start = 0, clear_len = 0;
4740
4741		fi = NULL;
4742		leaf = path->nodes[0];
4743		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4744		found_type = found_key.type;
4745
4746		if (found_key.objectid != ino)
4747			break;
4748
4749		if (found_type < min_type)
4750			break;
4751
4752		item_end = found_key.offset;
4753		if (found_type == BTRFS_EXTENT_DATA_KEY) {
4754			fi = btrfs_item_ptr(leaf, path->slots[0],
4755					    struct btrfs_file_extent_item);
4756			extent_type = btrfs_file_extent_type(leaf, fi);
4757			if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4758				item_end +=
4759				    btrfs_file_extent_num_bytes(leaf, fi);
4760
4761				trace_btrfs_truncate_show_fi_regular(
4762					inode, leaf, fi, found_key.offset);
4763			} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4764				item_end += btrfs_file_extent_ram_bytes(leaf,
4765									fi);
4766
4767				trace_btrfs_truncate_show_fi_inline(
4768					inode, leaf, fi, path->slots[0],
4769					found_key.offset);
4770			}
4771			item_end--;
4772		}
4773		if (found_type > min_type) {
4774			del_item = 1;
4775		} else {
4776			if (item_end < new_size)
4777				break;
4778			if (found_key.offset >= new_size)
4779				del_item = 1;
4780			else
4781				del_item = 0;
4782		}
4783		found_extent = 0;
4784		/* FIXME, shrink the extent if the ref count is only 1 */
4785		if (found_type != BTRFS_EXTENT_DATA_KEY)
4786			goto delete;
4787
4788		if (extents_found != NULL)
4789			(*extents_found)++;
4790
4791		if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4792			u64 num_dec;
4793
4794			clear_start = found_key.offset;
4795			extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4796			if (!del_item) {
4797				u64 orig_num_bytes =
4798					btrfs_file_extent_num_bytes(leaf, fi);
4799				extent_num_bytes = ALIGN(new_size -
4800						found_key.offset,
4801						fs_info->sectorsize);
4802				clear_start = ALIGN(new_size, fs_info->sectorsize);
4803				btrfs_set_file_extent_num_bytes(leaf, fi,
4804							 extent_num_bytes);
4805				num_dec = (orig_num_bytes -
4806					   extent_num_bytes);
4807				if (test_bit(BTRFS_ROOT_SHAREABLE,
4808					     &root->state) &&
4809				    extent_start != 0)
4810					inode_sub_bytes(&inode->vfs_inode,
4811							num_dec);
4812				btrfs_mark_buffer_dirty(leaf);
4813			} else {
4814				extent_num_bytes =
4815					btrfs_file_extent_disk_num_bytes(leaf,
4816									 fi);
4817				extent_offset = found_key.offset -
4818					btrfs_file_extent_offset(leaf, fi);
4819
4820				/* FIXME blocksize != 4096 */
4821				num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4822				if (extent_start != 0) {
4823					found_extent = 1;
4824					if (test_bit(BTRFS_ROOT_SHAREABLE,
4825						     &root->state))
4826						inode_sub_bytes(&inode->vfs_inode,
4827								num_dec);
4828				}
4829			}
4830			clear_len = num_dec;
4831		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4832			/*
4833			 * we can't truncate inline items that have had
4834			 * special encodings
4835			 */
4836			if (!del_item &&
4837			    btrfs_file_extent_encryption(leaf, fi) == 0 &&
4838			    btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4839			    btrfs_file_extent_compression(leaf, fi) == 0) {
4840				u32 size = (u32)(new_size - found_key.offset);
4841
4842				btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4843				size = btrfs_file_extent_calc_inline_size(size);
4844				btrfs_truncate_item(path, size, 1);
4845			} else if (!del_item) {
4846				/*
4847				 * We have to bail so the last_size is set to
4848				 * just before this extent.
4849				 */
4850				ret = NEED_TRUNCATE_BLOCK;
4851				break;
4852			} else {
4853				/*
4854				 * Inline extents are special, we just treat
4855				 * them as a full sector worth in the file
4856				 * extent tree just for simplicity sake.
4857				 */
4858				clear_len = fs_info->sectorsize;
4859			}
4860
4861			if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
4862				inode_sub_bytes(&inode->vfs_inode,
4863						item_end + 1 - new_size);
4864		}
4865delete:
4866		/*
4867		 * We use btrfs_truncate_inode_items() to clean up log trees for
4868		 * multiple fsyncs, and in this case we don't want to clear the
4869		 * file extent range because it's just the log.
4870		 */
4871		if (root == inode->root) {
4872			ret = btrfs_inode_clear_file_extent_range(inode,
4873						  clear_start, clear_len);
4874			if (ret) {
4875				btrfs_abort_transaction(trans, ret);
4876				break;
4877			}
4878		}
4879
4880		if (del_item)
4881			last_size = found_key.offset;
4882		else
4883			last_size = new_size;
4884		if (del_item) {
4885			if (!pending_del_nr) {
4886				/* no pending yet, add ourselves */
4887				pending_del_slot = path->slots[0];
4888				pending_del_nr = 1;
4889			} else if (pending_del_nr &&
4890				   path->slots[0] + 1 == pending_del_slot) {
4891				/* hop on the pending chunk */
4892				pending_del_nr++;
4893				pending_del_slot = path->slots[0];
4894			} else {
4895				BUG();
4896			}
4897		} else {
4898			break;
4899		}
4900		should_throttle = false;
4901
4902		if (found_extent &&
4903		    root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4904			struct btrfs_ref ref = { 0 };
4905
4906			bytes_deleted += extent_num_bytes;
4907
4908			btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4909					extent_start, extent_num_bytes, 0);
4910			ref.real_root = root->root_key.objectid;
4911			btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4912					ino, extent_offset);
4913			ret = btrfs_free_extent(trans, &ref);
4914			if (ret) {
4915				btrfs_abort_transaction(trans, ret);
4916				break;
4917			}
4918			if (be_nice) {
4919				if (btrfs_should_throttle_delayed_refs(trans))
4920					should_throttle = true;
4921			}
4922		}
4923
4924		if (found_type == BTRFS_INODE_ITEM_KEY)
4925			break;
4926
4927		if (path->slots[0] == 0 ||
4928		    path->slots[0] != pending_del_slot ||
4929		    should_throttle) {
4930			if (pending_del_nr) {
4931				ret = btrfs_del_items(trans, root, path,
4932						pending_del_slot,
4933						pending_del_nr);
4934				if (ret) {
4935					btrfs_abort_transaction(trans, ret);
4936					break;
4937				}
4938				pending_del_nr = 0;
4939			}
4940			btrfs_release_path(path);
4941
4942			/*
4943			 * We can generate a lot of delayed refs, so we need to
4944			 * throttle every once and a while and make sure we're
4945			 * adding enough space to keep up with the work we are
4946			 * generating.  Since we hold a transaction here we
4947			 * can't flush, and we don't want to FLUSH_LIMIT because
4948			 * we could have generated too many delayed refs to
4949			 * actually allocate, so just bail if we're short and
4950			 * let the normal reservation dance happen higher up.
4951			 */
4952			if (should_throttle) {
4953				ret = btrfs_delayed_refs_rsv_refill(fs_info,
4954							BTRFS_RESERVE_NO_FLUSH);
4955				if (ret) {
4956					ret = -EAGAIN;
4957					break;
4958				}
4959			}
4960			goto search_again;
4961		} else {
4962			path->slots[0]--;
4963		}
4964	}
4965out:
4966	if (ret >= 0 && pending_del_nr) {
4967		int err;
4968
4969		err = btrfs_del_items(trans, root, path, pending_del_slot,
4970				      pending_del_nr);
4971		if (err) {
4972			btrfs_abort_transaction(trans, err);
4973			ret = err;
4974		}
4975	}
4976	if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4977		ASSERT(last_size >= new_size);
4978		if (!ret && last_size > new_size)
4979			last_size = new_size;
4980		btrfs_inode_safe_disk_i_size_write(inode, last_size);
4981		unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4982				     &cached_state);
4983	}
4984
4985	btrfs_free_path(path);
4986	return ret;
4987}
4988
4989/*
4990 * btrfs_truncate_block - read, zero a chunk and write a block
4991 * @inode - inode that we're zeroing
4992 * @from - the offset to start zeroing
4993 * @len - the length to zero, 0 to zero the entire range respective to the
4994 *	offset
4995 * @front - zero up to the offset instead of from the offset on
4996 *
4997 * This will find the block for the "from" offset and cow the block and zero the
4998 * part we want to zero.  This is used with truncate and hole punching.
4999 */
5000int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
5001			 int front)
5002{
5003	struct btrfs_fs_info *fs_info = inode->root->fs_info;
5004	struct address_space *mapping = inode->vfs_inode.i_mapping;
5005	struct extent_io_tree *io_tree = &inode->io_tree;
5006	struct btrfs_ordered_extent *ordered;
5007	struct extent_state *cached_state = NULL;
5008	struct extent_changeset *data_reserved = NULL;
5009	bool only_release_metadata = false;
5010	u32 blocksize = fs_info->sectorsize;
5011	pgoff_t index = from >> PAGE_SHIFT;
5012	unsigned offset = from & (blocksize - 1);
5013	struct page *page;
5014	gfp_t mask = btrfs_alloc_write_mask(mapping);
5015	size_t write_bytes = blocksize;
5016	int ret = 0;
5017	u64 block_start;
5018	u64 block_end;
5019
5020	if (IS_ALIGNED(offset, blocksize) &&
5021	    (!len || IS_ALIGNED(len, blocksize)))
5022		goto out;
5023
5024	block_start = round_down(from, blocksize);
5025	block_end = block_start + blocksize - 1;
5026
5027	ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
5028					  blocksize);
5029	if (ret < 0) {
5030		if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
5031			/* For nocow case, no need to reserve data space */
5032			only_release_metadata = true;
5033		} else {
5034			goto out;
5035		}
5036	}
5037	ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
5038	if (ret < 0) {
5039		if (!only_release_metadata)
5040			btrfs_free_reserved_data_space(inode, data_reserved,
5041						       block_start, blocksize);
5042		goto out;
5043	}
5044again:
5045	page = find_or_create_page(mapping, index, mask);
5046	if (!page) {
5047		btrfs_delalloc_release_space(inode, data_reserved, block_start,
5048					     blocksize, true);
5049		btrfs_delalloc_release_extents(inode, blocksize);
5050		ret = -ENOMEM;
5051		goto out;
5052	}
5053	ret = set_page_extent_mapped(page);
5054	if (ret < 0)
5055		goto out_unlock;
5056
5057	if (!PageUptodate(page)) {
5058		ret = btrfs_readpage(NULL, page);
5059		lock_page(page);
5060		if (page->mapping != mapping) {
5061			unlock_page(page);
5062			put_page(page);
5063			goto again;
5064		}
5065		if (!PageUptodate(page)) {
5066			ret = -EIO;
5067			goto out_unlock;
5068		}
5069	}
5070	wait_on_page_writeback(page);
5071
5072	lock_extent_bits(io_tree, block_start, block_end, &cached_state);
5073
5074	ordered = btrfs_lookup_ordered_extent(inode, block_start);
5075	if (ordered) {
5076		unlock_extent_cached(io_tree, block_start, block_end,
5077				     &cached_state);
5078		unlock_page(page);
5079		put_page(page);
5080		btrfs_start_ordered_extent(ordered, 1);
5081		btrfs_put_ordered_extent(ordered);
5082		goto again;
5083	}
5084
5085	clear_extent_bit(&inode->io_tree, block_start, block_end,
5086			 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5087			 0, 0, &cached_state);
5088
5089	ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
5090					&cached_state);
5091	if (ret) {
5092		unlock_extent_cached(io_tree, block_start, block_end,
5093				     &cached_state);
5094		goto out_unlock;
5095	}
5096
5097	if (offset != blocksize) {
5098		if (!len)
5099			len = blocksize - offset;
5100		if (front)
5101			memzero_page(page, (block_start - page_offset(page)),
5102				     offset);
5103		else
5104			memzero_page(page, (block_start - page_offset(page)) + offset,
5105				     len);
5106		flush_dcache_page(page);
5107	}
5108	ClearPageChecked(page);
5109	btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
5110	unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
5111
5112	if (only_release_metadata)
5113		set_extent_bit(&inode->io_tree, block_start, block_end,
5114			       EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
5115
5116out_unlock:
5117	if (ret) {
5118		if (only_release_metadata)
5119			btrfs_delalloc_release_metadata(inode, blocksize, true);
5120		else
5121			btrfs_delalloc_release_space(inode, data_reserved,
5122					block_start, blocksize, true);
5123	}
5124	btrfs_delalloc_release_extents(inode, blocksize);
5125	unlock_page(page);
5126	put_page(page);
5127out:
5128	if (only_release_metadata)
5129		btrfs_check_nocow_unlock(inode);
5130	extent_changeset_free(data_reserved);
5131	return ret;
5132}
5133
5134static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
5135			     u64 offset, u64 len)
5136{
5137	struct btrfs_fs_info *fs_info = root->fs_info;
5138	struct btrfs_trans_handle *trans;
5139	struct btrfs_drop_extents_args drop_args = { 0 };
5140	int ret;
5141
5142	/*
5143	 * If NO_HOLES is enabled, we don't need to do anything.
5144	 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
5145	 * or btrfs_update_inode() will be called, which guarantee that the next
5146	 * fsync will know this inode was changed and needs to be logged.
5147	 */
5148	if (btrfs_fs_incompat(fs_info, NO_HOLES))
5149		return 0;
5150
5151	/*
5152	 * 1 - for the one we're dropping
5153	 * 1 - for the one we're adding
5154	 * 1 - for updating the inode.
5155	 */
5156	trans = btrfs_start_transaction(root, 3);
5157	if (IS_ERR(trans))
5158		return PTR_ERR(trans);
5159
5160	drop_args.start = offset;
5161	drop_args.end = offset + len;
5162	drop_args.drop_cache = true;
5163
5164	ret = btrfs_drop_extents(trans, root, inode, &drop_args);
5165	if (ret) {
5166		btrfs_abort_transaction(trans, ret);
5167		btrfs_end_transaction(trans);
5168		return ret;
5169	}
5170
5171	ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
5172			offset, 0, 0, len, 0, len, 0, 0, 0);
5173	if (ret) {
5174		btrfs_abort_transaction(trans, ret);
5175	} else {
5176		btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5177		btrfs_update_inode(trans, root, inode);
5178	}
5179	btrfs_end_transaction(trans);
5180	return ret;
5181}
5182
5183/*
5184 * This function puts in dummy file extents for the area we're creating a hole
5185 * for.  So if we are truncating this file to a larger size we need to insert
5186 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5187 * the range between oldsize and size
5188 */
5189int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
5190{
5191	struct btrfs_root *root = inode->root;
5192	struct btrfs_fs_info *fs_info = root->fs_info;
5193	struct extent_io_tree *io_tree = &inode->io_tree;
5194	struct extent_map *em = NULL;
5195	struct extent_state *cached_state = NULL;
5196	struct extent_map_tree *em_tree = &inode->extent_tree;
5197	u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5198	u64 block_end = ALIGN(size, fs_info->sectorsize);
5199	u64 last_byte;
5200	u64 cur_offset;
5201	u64 hole_size;
5202	int err = 0;
5203
5204	/*
5205	 * If our size started in the middle of a block we need to zero out the
5206	 * rest of the block before we expand the i_size, otherwise we could
5207	 * expose stale data.
5208	 */
5209	err = btrfs_truncate_block(inode, oldsize, 0, 0);
5210	if (err)
5211		return err;
5212
5213	if (size <= hole_start)
5214		return 0;
5215
5216	btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5217					   &cached_state);
5218	cur_offset = hole_start;
5219	while (1) {
5220		em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5221				      block_end - cur_offset);
5222		if (IS_ERR(em)) {
5223			err = PTR_ERR(em);
5224			em = NULL;
5225			break;
5226		}
5227		last_byte = min(extent_map_end(em), block_end);
5228		last_byte = ALIGN(last_byte, fs_info->sectorsize);
5229		hole_size = last_byte - cur_offset;
5230
5231		if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
5232			struct extent_map *hole_em;
5233
5234			err = maybe_insert_hole(root, inode, cur_offset,
5235						hole_size);
5236			if (err)
5237				break;
5238
5239			err = btrfs_inode_set_file_extent_range(inode,
5240							cur_offset, hole_size);
5241			if (err)
5242				break;
5243
5244			btrfs_drop_extent_cache(inode, cur_offset,
5245						cur_offset + hole_size - 1, 0);
5246			hole_em = alloc_extent_map();
5247			if (!hole_em) {
5248				set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5249					&inode->runtime_flags);
5250				goto next;
5251			}
5252			hole_em->start = cur_offset;
5253			hole_em->len = hole_size;
5254			hole_em->orig_start = cur_offset;
5255
5256			hole_em->block_start = EXTENT_MAP_HOLE;
5257			hole_em->block_len = 0;
5258			hole_em->orig_block_len = 0;
5259			hole_em->ram_bytes = hole_size;
5260			hole_em->compress_type = BTRFS_COMPRESS_NONE;
5261			hole_em->generation = fs_info->generation;
5262
5263			while (1) {
5264				write_lock(&em_tree->lock);
5265				err = add_extent_mapping(em_tree, hole_em, 1);
5266				write_unlock(&em_tree->lock);
5267				if (err != -EEXIST)
5268					break;
5269				btrfs_drop_extent_cache(inode, cur_offset,
5270							cur_offset +
5271							hole_size - 1, 0);
5272			}
5273			free_extent_map(hole_em);
5274		} else {
5275			err = btrfs_inode_set_file_extent_range(inode,
5276							cur_offset, hole_size);
5277			if (err)
5278				break;
5279		}
5280next:
5281		free_extent_map(em);
5282		em = NULL;
5283		cur_offset = last_byte;
5284		if (cur_offset >= block_end)
5285			break;
5286	}
5287	free_extent_map(em);
5288	unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
5289	return err;
5290}
5291
5292static int btrfs_setsize(struct inode *inode, struct iattr *attr)
5293{
5294	struct btrfs_root *root = BTRFS_I(inode)->root;
5295	struct btrfs_trans_handle *trans;
5296	loff_t oldsize = i_size_read(inode);
5297	loff_t newsize = attr->ia_size;
5298	int mask = attr->ia_valid;
5299	int ret;
5300
5301	/*
5302	 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5303	 * special case where we need to update the times despite not having
5304	 * these flags set.  For all other operations the VFS set these flags
5305	 * explicitly if it wants a timestamp update.
5306	 */
5307	if (newsize != oldsize) {
5308		inode_inc_iversion(inode);
5309		if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5310			inode->i_ctime = inode->i_mtime =
5311				current_time(inode);
5312	}
5313
5314	if (newsize > oldsize) {
5315		/*
5316		 * Don't do an expanding truncate while snapshotting is ongoing.
5317		 * This is to ensure the snapshot captures a fully consistent
5318		 * state of this file - if the snapshot captures this expanding
5319		 * truncation, it must capture all writes that happened before
5320		 * this truncation.
5321		 */
5322		btrfs_drew_write_lock(&root->snapshot_lock);
5323		ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
5324		if (ret) {
5325			btrfs_drew_write_unlock(&root->snapshot_lock);
5326			return ret;
5327		}
5328
5329		trans = btrfs_start_transaction(root, 1);
5330		if (IS_ERR(trans)) {
5331			btrfs_drew_write_unlock(&root->snapshot_lock);
5332			return PTR_ERR(trans);
5333		}
5334
5335		i_size_write(inode, newsize);
5336		btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
5337		pagecache_isize_extended(inode, oldsize, newsize);
5338		ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
5339		btrfs_drew_write_unlock(&root->snapshot_lock);
5340		btrfs_end_transaction(trans);
5341	} else {
5342		struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5343
5344		if (btrfs_is_zoned(fs_info)) {
5345			ret = btrfs_wait_ordered_range(inode,
5346					ALIGN(newsize, fs_info->sectorsize),
5347					(u64)-1);
5348			if (ret)
5349				return ret;
5350		}
5351
5352		/*
5353		 * We're truncating a file that used to have good data down to
5354		 * zero. Make sure any new writes to the file get on disk
5355		 * on close.
5356		 */
5357		if (newsize == 0)
5358			set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
5359				&BTRFS_I(inode)->runtime_flags);
5360
5361		truncate_setsize(inode, newsize);
5362
5363		inode_dio_wait(inode);
5364
5365		ret = btrfs_truncate(inode, newsize == oldsize);
5366		if (ret && inode->i_nlink) {
5367			int err;
5368
5369			/*
5370			 * Truncate failed, so fix up the in-memory size. We
5371			 * adjusted disk_i_size down as we removed extents, so
5372			 * wait for disk_i_size to be stable and then update the
5373			 * in-memory size to match.
5374			 */
5375			err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5376			if (err)
5377				return err;
5378			i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5379		}
5380	}
5381
5382	return ret;
5383}
5384
5385static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5386			 struct iattr *attr)
5387{
5388	struct inode *inode = d_inode(dentry);
5389	struct btrfs_root *root = BTRFS_I(inode)->root;
5390	int err;
5391
5392	if (btrfs_root_readonly(root))
5393		return -EROFS;
5394
5395	err = setattr_prepare(&init_user_ns, dentry, attr);
5396	if (err)
5397		return err;
5398
5399	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5400		err = btrfs_setsize(inode, attr);
5401		if (err)
5402			return err;
5403	}
5404
5405	if (attr->ia_valid) {
5406		setattr_copy(&init_user_ns, inode, attr);
5407		inode_inc_iversion(inode);
5408		err = btrfs_dirty_inode(inode);
5409
5410		if (!err && attr->ia_valid & ATTR_MODE)
5411			err = posix_acl_chmod(&init_user_ns, inode,
5412					      inode->i_mode);
5413	}
5414
5415	return err;
5416}
5417
5418/*
5419 * While truncating the inode pages during eviction, we get the VFS calling
5420 * btrfs_invalidatepage() against each page of the inode. This is slow because
5421 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5422 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5423 * extent_state structures over and over, wasting lots of time.
5424 *
5425 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5426 * those expensive operations on a per page basis and do only the ordered io
5427 * finishing, while we release here the extent_map and extent_state structures,
5428 * without the excessive merging and splitting.
5429 */
5430static void evict_inode_truncate_pages(struct inode *inode)
5431{
5432	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5433	struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5434	struct rb_node *node;
5435
5436	ASSERT(inode->i_state & I_FREEING);
5437	truncate_inode_pages_final(&inode->i_data);
5438
5439	write_lock(&map_tree->lock);
5440	while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
5441		struct extent_map *em;
5442
5443		node = rb_first_cached(&map_tree->map);
5444		em = rb_entry(node, struct extent_map, rb_node);
5445		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5446		clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
5447		remove_extent_mapping(map_tree, em);
5448		free_extent_map(em);
5449		if (need_resched()) {
5450			write_unlock(&map_tree->lock);
5451			cond_resched();
5452			write_lock(&map_tree->lock);
5453		}
5454	}
5455	write_unlock(&map_tree->lock);
5456
5457	/*
5458	 * Keep looping until we have no more ranges in the io tree.
5459	 * We can have ongoing bios started by readahead that have
5460	 * their endio callback (extent_io.c:end_bio_extent_readpage)
5461	 * still in progress (unlocked the pages in the bio but did not yet
5462	 * unlocked the ranges in the io tree). Therefore this means some
5463	 * ranges can still be locked and eviction started because before
5464	 * submitting those bios, which are executed by a separate task (work
5465	 * queue kthread), inode references (inode->i_count) were not taken
5466	 * (which would be dropped in the end io callback of each bio).
5467	 * Therefore here we effectively end up waiting for those bios and
5468	 * anyone else holding locked ranges without having bumped the inode's
5469	 * reference count - if we don't do it, when they access the inode's
5470	 * io_tree to unlock a range it may be too late, leading to an
5471	 * use-after-free issue.
5472	 */
5473	spin_lock(&io_tree->lock);
5474	while (!RB_EMPTY_ROOT(&io_tree->state)) {
5475		struct extent_state *state;
5476		struct extent_state *cached_state = NULL;
5477		u64 start;
5478		u64 end;
5479		unsigned state_flags;
5480
5481		node = rb_first(&io_tree->state);
5482		state = rb_entry(node, struct extent_state, rb_node);
5483		start = state->start;
5484		end = state->end;
5485		state_flags = state->state;
5486		spin_unlock(&io_tree->lock);
5487
5488		lock_extent_bits(io_tree, start, end, &cached_state);
5489
5490		/*
5491		 * If still has DELALLOC flag, the extent didn't reach disk,
5492		 * and its reserved space won't be freed by delayed_ref.
5493		 * So we need to free its reserved space here.
5494		 * (Refer to comment in btrfs_invalidatepage, case 2)
5495		 *
5496		 * Note, end is the bytenr of last byte, so we need + 1 here.
5497		 */
5498		if (state_flags & EXTENT_DELALLOC)
5499			btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5500					       end - start + 1);
5501
5502		clear_extent_bit(io_tree, start, end,
5503				 EXTENT_LOCKED | EXTENT_DELALLOC |
5504				 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5505				 &cached_state);
5506
5507		cond_resched();
5508		spin_lock(&io_tree->lock);
5509	}
5510	spin_unlock(&io_tree->lock);
5511}
5512
5513static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5514							struct btrfs_block_rsv *rsv)
5515{
5516	struct btrfs_fs_info *fs_info = root->fs_info;
5517	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5518	struct btrfs_trans_handle *trans;
5519	u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
5520	int ret;
5521
5522	/*
5523	 * Eviction should be taking place at some place safe because of our
5524	 * delayed iputs.  However the normal flushing code will run delayed
5525	 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5526	 *
5527	 * We reserve the delayed_refs_extra here again because we can't use
5528	 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5529	 * above.  We reserve our extra bit here because we generate a ton of
5530	 * delayed refs activity by truncating.
5531	 *
5532	 * If we cannot make our reservation we'll attempt to steal from the
5533	 * global reserve, because we really want to be able to free up space.
5534	 */
5535	ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5536				     BTRFS_RESERVE_FLUSH_EVICT);
5537	if (ret) {
5538		/*
5539		 * Try to steal from the global reserve if there is space for
5540		 * it.
5541		 */
5542		if (btrfs_check_space_for_delayed_refs(fs_info) ||
5543		    btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5544			btrfs_warn(fs_info,
5545				   "could not allocate space for delete; will truncate on mount");
5546			return ERR_PTR(-ENOSPC);
5547		}
5548		delayed_refs_extra = 0;
5549	}
5550
5551	trans = btrfs_join_transaction(root);
5552	if (IS_ERR(trans))
5553		return trans;
5554
5555	if (delayed_refs_extra) {
5556		trans->block_rsv = &fs_info->trans_block_rsv;
5557		trans->bytes_reserved = delayed_refs_extra;
5558		btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5559					delayed_refs_extra, 1);
5560	}
5561	return trans;
5562}
5563
5564void btrfs_evict_inode(struct inode *inode)
5565{
5566	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5567	struct btrfs_trans_handle *trans;
5568	struct btrfs_root *root = BTRFS_I(inode)->root;
5569	struct btrfs_block_rsv *rsv;
5570	int ret;
5571
5572	trace_btrfs_inode_evict(inode);
5573
5574	if (!root) {
5575		fsverity_cleanup_inode(inode);
5576		clear_inode(inode);
5577		return;
5578	}
5579
5580	evict_inode_truncate_pages(inode);
5581
5582	if (inode->i_nlink &&
5583	    ((btrfs_root_refs(&root->root_item) != 0 &&
5584	      root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5585	     btrfs_is_free_space_inode(BTRFS_I(inode))))
5586		goto no_delete;
5587
5588	if (is_bad_inode(inode))
5589		goto no_delete;
5590
5591	btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
5592
5593	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
5594		goto no_delete;
5595
5596	if (inode->i_nlink > 0) {
5597		BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5598		       root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5599		goto no_delete;
5600	}
5601
5602	ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5603	if (ret)
5604		goto no_delete;
5605
5606	rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5607	if (!rsv)
5608		goto no_delete;
5609	rsv->size = btrfs_calc_metadata_size(fs_info, 1);
5610	rsv->failfast = 1;
5611
5612	btrfs_i_size_write(BTRFS_I(inode), 0);
5613
5614	while (1) {
5615		trans = evict_refill_and_join(root, rsv);
5616		if (IS_ERR(trans))
5617			goto free_rsv;
5618
5619		trans->block_rsv = rsv;
5620
5621		ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
5622						 0, 0, NULL);
5623		trans->block_rsv = &fs_info->trans_block_rsv;
5624		btrfs_end_transaction(trans);
5625		btrfs_btree_balance_dirty(fs_info);
5626		if (ret && ret != -ENOSPC && ret != -EAGAIN)
5627			goto free_rsv;
5628		else if (!ret)
5629			break;
5630	}
5631
5632	/*
5633	 * Errors here aren't a big deal, it just means we leave orphan items in
5634	 * the tree. They will be cleaned up on the next mount. If the inode
5635	 * number gets reused, cleanup deletes the orphan item without doing
5636	 * anything, and unlink reuses the existing orphan item.
5637	 *
5638	 * If it turns out that we are dropping too many of these, we might want
5639	 * to add a mechanism for retrying these after a commit.
5640	 */
5641	trans = evict_refill_and_join(root, rsv);
5642	if (!IS_ERR(trans)) {
5643		trans->block_rsv = rsv;
5644		btrfs_orphan_del(trans, BTRFS_I(inode));
5645		trans->block_rsv = &fs_info->trans_block_rsv;
5646		btrfs_end_transaction(trans);
5647	}
5648
5649free_rsv:
5650	btrfs_free_block_rsv(fs_info, rsv);
5651no_delete:
5652	/*
5653	 * If we didn't successfully delete, the orphan item will still be in
5654	 * the tree and we'll retry on the next mount. Again, we might also want
5655	 * to retry these periodically in the future.
5656	 */
5657	btrfs_remove_delayed_node(BTRFS_I(inode));
5658	fsverity_cleanup_inode(inode);
5659	clear_inode(inode);
5660}
5661
5662/*
5663 * Return the key found in the dir entry in the location pointer, fill @type
5664 * with BTRFS_FT_*, and return 0.
5665 *
5666 * If no dir entries were found, returns -ENOENT.
5667 * If found a corrupted location in dir entry, returns -EUCLEAN.
5668 */
5669static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5670			       struct btrfs_key *location, u8 *type)
5671{
5672	const char *name = dentry->d_name.name;
5673	int namelen = dentry->d_name.len;
5674	struct btrfs_dir_item *di;
5675	struct btrfs_path *path;
5676	struct btrfs_root *root = BTRFS_I(dir)->root;
5677	int ret = 0;
5678
5679	path = btrfs_alloc_path();
5680	if (!path)
5681		return -ENOMEM;
5682
5683	di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5684			name, namelen, 0);
5685	if (IS_ERR_OR_NULL(di)) {
5686		ret = di ? PTR_ERR(di) : -ENOENT;
5687		goto out;
5688	}
5689
5690	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5691	if (location->type != BTRFS_INODE_ITEM_KEY &&
5692	    location->type != BTRFS_ROOT_ITEM_KEY) {
5693		ret = -EUCLEAN;
5694		btrfs_warn(root->fs_info,
5695"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5696			   __func__, name, btrfs_ino(BTRFS_I(dir)),
5697			   location->objectid, location->type, location->offset);
5698	}
5699	if (!ret)
5700		*type = btrfs_dir_type(path->nodes[0], di);
5701out:
5702	btrfs_free_path(path);
5703	return ret;
5704}
5705
5706/*
5707 * when we hit a tree root in a directory, the btrfs part of the inode
5708 * needs to be changed to reflect the root directory of the tree root.  This
5709 * is kind of like crossing a mount point.
5710 */
5711static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5712				    struct inode *dir,
5713				    struct dentry *dentry,
5714				    struct btrfs_key *location,
5715				    struct btrfs_root **sub_root)
5716{
5717	struct btrfs_path *path;
5718	struct btrfs_root *new_root;
5719	struct btrfs_root_ref *ref;
5720	struct extent_buffer *leaf;
5721	struct btrfs_key key;
5722	int ret;
5723	int err = 0;
5724
5725	path = btrfs_alloc_path();
5726	if (!path) {
5727		err = -ENOMEM;
5728		goto out;
5729	}
5730
5731	err = -ENOENT;
5732	key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5733	key.type = BTRFS_ROOT_REF_KEY;
5734	key.offset = location->objectid;
5735
5736	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5737	if (ret) {
5738		if (ret < 0)
5739			err = ret;
5740		goto out;
5741	}
5742
5743	leaf = path->nodes[0];
5744	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5745	if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
5746	    btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5747		goto out;
5748
5749	ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5750				   (unsigned long)(ref + 1),
5751				   dentry->d_name.len);
5752	if (ret)
5753		goto out;
5754
5755	btrfs_release_path(path);
5756
5757	new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
5758	if (IS_ERR(new_root)) {
5759		err = PTR_ERR(new_root);
5760		goto out;
5761	}
5762
5763	*sub_root = new_root;
5764	location->objectid = btrfs_root_dirid(&new_root->root_item);
5765	location->type = BTRFS_INODE_ITEM_KEY;
5766	location->offset = 0;
5767	err = 0;
5768out:
5769	btrfs_free_path(path);
5770	return err;
5771}
5772
5773static void inode_tree_add(struct inode *inode)
5774{
5775	struct btrfs_root *root = BTRFS_I(inode)->root;
5776	struct btrfs_inode *entry;
5777	struct rb_node **p;
5778	struct rb_node *parent;
5779	struct rb_node *new = &BTRFS_I(inode)->rb_node;
5780	u64 ino = btrfs_ino(BTRFS_I(inode));
5781
5782	if (inode_unhashed(inode))
5783		return;
5784	parent = NULL;
5785	spin_lock(&root->inode_lock);
5786	p = &root->inode_tree.rb_node;
5787	while (*p) {
5788		parent = *p;
5789		entry = rb_entry(parent, struct btrfs_inode, rb_node);
5790
5791		if (ino < btrfs_ino(entry))
5792			p = &parent->rb_left;
5793		else if (ino > btrfs_ino(entry))
5794			p = &parent->rb_right;
5795		else {
5796			WARN_ON(!(entry->vfs_inode.i_state &
5797				  (I_WILL_FREE | I_FREEING)));
5798			rb_replace_node(parent, new, &root->inode_tree);
5799			RB_CLEAR_NODE(parent);
5800			spin_unlock(&root->inode_lock);
5801			return;
5802		}
5803	}
5804	rb_link_node(new, parent, p);
5805	rb_insert_color(new, &root->inode_tree);
5806	spin_unlock(&root->inode_lock);
5807}
5808
5809static void inode_tree_del(struct btrfs_inode *inode)
5810{
5811	struct btrfs_root *root = inode->root;
5812	int empty = 0;
5813
5814	spin_lock(&root->inode_lock);
5815	if (!RB_EMPTY_NODE(&inode->rb_node)) {
5816		rb_erase(&inode->rb_node, &root->inode_tree);
5817		RB_CLEAR_NODE(&inode->rb_node);
5818		empty = RB_EMPTY_ROOT(&root->inode_tree);
5819	}
5820	spin_unlock(&root->inode_lock);
5821
5822	if (empty && btrfs_root_refs(&root->root_item) == 0) {
5823		spin_lock(&root->inode_lock);
5824		empty = RB_EMPTY_ROOT(&root->inode_tree);
5825		spin_unlock(&root->inode_lock);
5826		if (empty)
5827			btrfs_add_dead_root(root);
5828	}
5829}
5830
5831
5832static int btrfs_init_locked_inode(struct inode *inode, void *p)
5833{
5834	struct btrfs_iget_args *args = p;
5835
5836	inode->i_ino = args->ino;
5837	BTRFS_I(inode)->location.objectid = args->ino;
5838	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5839	BTRFS_I(inode)->location.offset = 0;
5840	BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5841	BUG_ON(args->root && !BTRFS_I(inode)->root);
5842	return 0;
5843}
5844
5845static int btrfs_find_actor(struct inode *inode, void *opaque)
5846{
5847	struct btrfs_iget_args *args = opaque;
5848
5849	return args->ino == BTRFS_I(inode)->location.objectid &&
5850		args->root == BTRFS_I(inode)->root;
5851}
5852
5853static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5854				       struct btrfs_root *root)
5855{
5856	struct inode *inode;
5857	struct btrfs_iget_args args;
5858	unsigned long hashval = btrfs_inode_hash(ino, root);
5859
5860	args.ino = ino;
5861	args.root = root;
5862
5863	inode = iget5_locked(s, hashval, btrfs_find_actor,
5864			     btrfs_init_locked_inode,
5865			     (void *)&args);
5866	return inode;
5867}
5868
5869/*
5870 * Get an inode object given its inode number and corresponding root.
5871 * Path can be preallocated to prevent recursing back to iget through
5872 * allocator. NULL is also valid but may require an additional allocation
5873 * later.
5874 */
5875struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5876			      struct btrfs_root *root, struct btrfs_path *path)
5877{
5878	struct inode *inode;
5879
5880	inode = btrfs_iget_locked(s, ino, root);
5881	if (!inode)
5882		return ERR_PTR(-ENOMEM);
5883
5884	if (inode->i_state & I_NEW) {
5885		int ret;
5886
5887		ret = btrfs_read_locked_inode(inode, path);
5888		if (!ret) {
5889			inode_tree_add(inode);
5890			unlock_new_inode(inode);
5891		} else {
5892			iget_failed(inode);
5893			/*
5894			 * ret > 0 can come from btrfs_search_slot called by
5895			 * btrfs_read_locked_inode, this means the inode item
5896			 * was not found.
5897			 */
5898			if (ret > 0)
5899				ret = -ENOENT;
5900			inode = ERR_PTR(ret