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