xref: /linux/fs/btrfs/file.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/fs.h>
7 #include <linux/pagemap.h>
8 #include <linux/time.h>
9 #include <linux/init.h>
10 #include <linux/string.h>
11 #include <linux/backing-dev.h>
12 #include <linux/falloc.h>
13 #include <linux/writeback.h>
14 #include <linux/compat.h>
15 #include <linux/slab.h>
16 #include <linux/btrfs.h>
17 #include <linux/uio.h>
18 #include <linux/iversion.h>
19 #include <linux/fsverity.h>
20 #include "ctree.h"
21 #include "direct-io.h"
22 #include "disk-io.h"
23 #include "transaction.h"
24 #include "btrfs_inode.h"
25 #include "tree-log.h"
26 #include "locking.h"
27 #include "qgroup.h"
28 #include "compression.h"
29 #include "delalloc-space.h"
30 #include "reflink.h"
31 #include "subpage.h"
32 #include "fs.h"
33 #include "accessors.h"
34 #include "extent-tree.h"
35 #include "file-item.h"
36 #include "ioctl.h"
37 #include "file.h"
38 #include "super.h"
39 
40 /*
41  * Helper to fault in page and copy.  This should go away and be replaced with
42  * calls into generic code.
43  */
44 static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
45 					 struct folio *folio, struct iov_iter *i)
46 {
47 	size_t copied = 0;
48 	size_t total_copied = 0;
49 	int offset = offset_in_page(pos);
50 
51 	while (write_bytes > 0) {
52 		size_t count = min_t(size_t, PAGE_SIZE - offset, write_bytes);
53 		/*
54 		 * Copy data from userspace to the current page
55 		 */
56 		copied = copy_folio_from_iter_atomic(folio, offset, count, i);
57 
58 		/* Flush processor's dcache for this page */
59 		flush_dcache_folio(folio);
60 
61 		/*
62 		 * if we get a partial write, we can end up with
63 		 * partially up to date page.  These add
64 		 * a lot of complexity, so make sure they don't
65 		 * happen by forcing this copy to be retried.
66 		 *
67 		 * The rest of the btrfs_file_write code will fall
68 		 * back to page at a time copies after we return 0.
69 		 */
70 		if (unlikely(copied < count)) {
71 			if (!folio_test_uptodate(folio)) {
72 				iov_iter_revert(i, copied);
73 				copied = 0;
74 			}
75 			if (!copied)
76 				break;
77 		}
78 
79 		write_bytes -= copied;
80 		total_copied += copied;
81 		offset += copied;
82 	}
83 	return total_copied;
84 }
85 
86 /*
87  * Unlock folio after btrfs_file_write() is done with it.
88  */
89 static void btrfs_drop_folio(struct btrfs_fs_info *fs_info, struct folio *folio,
90 			     u64 pos, u64 copied)
91 {
92 	u64 block_start = round_down(pos, fs_info->sectorsize);
93 	u64 block_len = round_up(pos + copied, fs_info->sectorsize) - block_start;
94 
95 	ASSERT(block_len <= U32_MAX);
96 	/*
97 	 * Folio checked is some magic around finding folios that have been
98 	 * modified without going through btrfs_dirty_folio().  Clear it here.
99 	 * There should be no need to mark the pages accessed as
100 	 * prepare_one_folio() should have marked them accessed in
101 	 * prepare_one_folio() via find_or_create_page()
102 	 */
103 	btrfs_folio_clamp_clear_checked(fs_info, folio, block_start, block_len);
104 	folio_unlock(folio);
105 	folio_put(folio);
106 }
107 
108 /*
109  * After btrfs_copy_from_user(), update the following things for delalloc:
110  * - Mark newly dirtied folio as DELALLOC in the io tree.
111  *   Used to advise which range is to be written back.
112  * - Mark modified folio as Uptodate/Dirty and not needing COW fixup
113  * - Update inode size for past EOF write
114  */
115 int btrfs_dirty_folio(struct btrfs_inode *inode, struct folio *folio, loff_t pos,
116 		      size_t write_bytes, struct extent_state **cached, bool noreserve)
117 {
118 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
119 	int ret = 0;
120 	u64 num_bytes;
121 	u64 start_pos;
122 	u64 end_of_last_block;
123 	u64 end_pos = pos + write_bytes;
124 	loff_t isize = i_size_read(&inode->vfs_inode);
125 	unsigned int extra_bits = 0;
126 
127 	if (write_bytes == 0)
128 		return 0;
129 
130 	if (noreserve)
131 		extra_bits |= EXTENT_NORESERVE;
132 
133 	start_pos = round_down(pos, fs_info->sectorsize);
134 	num_bytes = round_up(write_bytes + pos - start_pos,
135 			     fs_info->sectorsize);
136 	ASSERT(num_bytes <= U32_MAX);
137 	ASSERT(folio_pos(folio) <= pos &&
138 	       folio_pos(folio) + folio_size(folio) >= pos + write_bytes);
139 
140 	end_of_last_block = start_pos + num_bytes - 1;
141 
142 	/*
143 	 * The pages may have already been dirty, clear out old accounting so
144 	 * we can set things up properly
145 	 */
146 	clear_extent_bit(&inode->io_tree, start_pos, end_of_last_block,
147 			 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
148 			 cached);
149 
150 	ret = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
151 					extra_bits, cached);
152 	if (ret)
153 		return ret;
154 
155 	btrfs_folio_clamp_set_uptodate(fs_info, folio, start_pos, num_bytes);
156 	btrfs_folio_clamp_clear_checked(fs_info, folio, start_pos, num_bytes);
157 	btrfs_folio_clamp_set_dirty(fs_info, folio, start_pos, num_bytes);
158 
159 	/*
160 	 * we've only changed i_size in ram, and we haven't updated
161 	 * the disk i_size.  There is no need to log the inode
162 	 * at this time.
163 	 */
164 	if (end_pos > isize)
165 		i_size_write(&inode->vfs_inode, end_pos);
166 	return 0;
167 }
168 
169 /*
170  * this is very complex, but the basic idea is to drop all extents
171  * in the range start - end.  hint_block is filled in with a block number
172  * that would be a good hint to the block allocator for this file.
173  *
174  * If an extent intersects the range but is not entirely inside the range
175  * it is either truncated or split.  Anything entirely inside the range
176  * is deleted from the tree.
177  *
178  * Note: the VFS' inode number of bytes is not updated, it's up to the caller
179  * to deal with that. We set the field 'bytes_found' of the arguments structure
180  * with the number of allocated bytes found in the target range, so that the
181  * caller can update the inode's number of bytes in an atomic way when
182  * replacing extents in a range to avoid races with stat(2).
183  */
184 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
185 		       struct btrfs_root *root, struct btrfs_inode *inode,
186 		       struct btrfs_drop_extents_args *args)
187 {
188 	struct btrfs_fs_info *fs_info = root->fs_info;
189 	struct extent_buffer *leaf;
190 	struct btrfs_file_extent_item *fi;
191 	struct btrfs_key key;
192 	struct btrfs_key new_key;
193 	u64 ino = btrfs_ino(inode);
194 	u64 search_start = args->start;
195 	u64 disk_bytenr = 0;
196 	u64 num_bytes = 0;
197 	u64 extent_offset = 0;
198 	u64 extent_end = 0;
199 	u64 last_end = args->start;
200 	int del_nr = 0;
201 	int del_slot = 0;
202 	int extent_type;
203 	int recow;
204 	int ret;
205 	int modify_tree = -1;
206 	int update_refs;
207 	int found = 0;
208 	struct btrfs_path *path = args->path;
209 
210 	args->bytes_found = 0;
211 	args->extent_inserted = false;
212 
213 	/* Must always have a path if ->replace_extent is true */
214 	ASSERT(!(args->replace_extent && !args->path));
215 
216 	if (!path) {
217 		path = btrfs_alloc_path();
218 		if (!path) {
219 			ret = -ENOMEM;
220 			goto out;
221 		}
222 	}
223 
224 	if (args->drop_cache)
225 		btrfs_drop_extent_map_range(inode, args->start, args->end - 1, false);
226 
227 	if (args->start >= inode->disk_i_size && !args->replace_extent)
228 		modify_tree = 0;
229 
230 	update_refs = (btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID);
231 	while (1) {
232 		recow = 0;
233 		ret = btrfs_lookup_file_extent(trans, root, path, ino,
234 					       search_start, modify_tree);
235 		if (ret < 0)
236 			break;
237 		if (ret > 0 && path->slots[0] > 0 && search_start == args->start) {
238 			leaf = path->nodes[0];
239 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
240 			if (key.objectid == ino &&
241 			    key.type == BTRFS_EXTENT_DATA_KEY)
242 				path->slots[0]--;
243 		}
244 		ret = 0;
245 next_slot:
246 		leaf = path->nodes[0];
247 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
248 			BUG_ON(del_nr > 0);
249 			ret = btrfs_next_leaf(root, path);
250 			if (ret < 0)
251 				break;
252 			if (ret > 0) {
253 				ret = 0;
254 				break;
255 			}
256 			leaf = path->nodes[0];
257 			recow = 1;
258 		}
259 
260 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
261 
262 		if (key.objectid > ino)
263 			break;
264 		if (WARN_ON_ONCE(key.objectid < ino) ||
265 		    key.type < BTRFS_EXTENT_DATA_KEY) {
266 			ASSERT(del_nr == 0);
267 			path->slots[0]++;
268 			goto next_slot;
269 		}
270 		if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= args->end)
271 			break;
272 
273 		fi = btrfs_item_ptr(leaf, path->slots[0],
274 				    struct btrfs_file_extent_item);
275 		extent_type = btrfs_file_extent_type(leaf, fi);
276 
277 		if (extent_type == BTRFS_FILE_EXTENT_REG ||
278 		    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
279 			disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
280 			num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
281 			extent_offset = btrfs_file_extent_offset(leaf, fi);
282 			extent_end = key.offset +
283 				btrfs_file_extent_num_bytes(leaf, fi);
284 		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
285 			extent_end = key.offset +
286 				btrfs_file_extent_ram_bytes(leaf, fi);
287 		} else {
288 			/* can't happen */
289 			BUG();
290 		}
291 
292 		/*
293 		 * Don't skip extent items representing 0 byte lengths. They
294 		 * used to be created (bug) if while punching holes we hit
295 		 * -ENOSPC condition. So if we find one here, just ensure we
296 		 * delete it, otherwise we would insert a new file extent item
297 		 * with the same key (offset) as that 0 bytes length file
298 		 * extent item in the call to setup_items_for_insert() later
299 		 * in this function.
300 		 */
301 		if (extent_end == key.offset && extent_end >= search_start) {
302 			last_end = extent_end;
303 			goto delete_extent_item;
304 		}
305 
306 		if (extent_end <= search_start) {
307 			path->slots[0]++;
308 			goto next_slot;
309 		}
310 
311 		found = 1;
312 		search_start = max(key.offset, args->start);
313 		if (recow || !modify_tree) {
314 			modify_tree = -1;
315 			btrfs_release_path(path);
316 			continue;
317 		}
318 
319 		/*
320 		 *     | - range to drop - |
321 		 *  | -------- extent -------- |
322 		 */
323 		if (args->start > key.offset && args->end < extent_end) {
324 			BUG_ON(del_nr > 0);
325 			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
326 				ret = -EOPNOTSUPP;
327 				break;
328 			}
329 
330 			memcpy(&new_key, &key, sizeof(new_key));
331 			new_key.offset = args->start;
332 			ret = btrfs_duplicate_item(trans, root, path,
333 						   &new_key);
334 			if (ret == -EAGAIN) {
335 				btrfs_release_path(path);
336 				continue;
337 			}
338 			if (ret < 0)
339 				break;
340 
341 			leaf = path->nodes[0];
342 			fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
343 					    struct btrfs_file_extent_item);
344 			btrfs_set_file_extent_num_bytes(leaf, fi,
345 							args->start - key.offset);
346 
347 			fi = btrfs_item_ptr(leaf, path->slots[0],
348 					    struct btrfs_file_extent_item);
349 
350 			extent_offset += args->start - key.offset;
351 			btrfs_set_file_extent_offset(leaf, fi, extent_offset);
352 			btrfs_set_file_extent_num_bytes(leaf, fi,
353 							extent_end - args->start);
354 			btrfs_mark_buffer_dirty(trans, leaf);
355 
356 			if (update_refs && disk_bytenr > 0) {
357 				struct btrfs_ref ref = {
358 					.action = BTRFS_ADD_DELAYED_REF,
359 					.bytenr = disk_bytenr,
360 					.num_bytes = num_bytes,
361 					.parent = 0,
362 					.owning_root = btrfs_root_id(root),
363 					.ref_root = btrfs_root_id(root),
364 				};
365 				btrfs_init_data_ref(&ref, new_key.objectid,
366 						    args->start - extent_offset,
367 						    0, false);
368 				ret = btrfs_inc_extent_ref(trans, &ref);
369 				if (ret) {
370 					btrfs_abort_transaction(trans, ret);
371 					break;
372 				}
373 			}
374 			key.offset = args->start;
375 		}
376 		/*
377 		 * From here on out we will have actually dropped something, so
378 		 * last_end can be updated.
379 		 */
380 		last_end = extent_end;
381 
382 		/*
383 		 *  | ---- range to drop ----- |
384 		 *      | -------- extent -------- |
385 		 */
386 		if (args->start <= key.offset && args->end < extent_end) {
387 			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
388 				ret = -EOPNOTSUPP;
389 				break;
390 			}
391 
392 			memcpy(&new_key, &key, sizeof(new_key));
393 			new_key.offset = args->end;
394 			btrfs_set_item_key_safe(trans, path, &new_key);
395 
396 			extent_offset += args->end - key.offset;
397 			btrfs_set_file_extent_offset(leaf, fi, extent_offset);
398 			btrfs_set_file_extent_num_bytes(leaf, fi,
399 							extent_end - args->end);
400 			btrfs_mark_buffer_dirty(trans, leaf);
401 			if (update_refs && disk_bytenr > 0)
402 				args->bytes_found += args->end - key.offset;
403 			break;
404 		}
405 
406 		search_start = extent_end;
407 		/*
408 		 *       | ---- range to drop ----- |
409 		 *  | -------- extent -------- |
410 		 */
411 		if (args->start > key.offset && args->end >= extent_end) {
412 			BUG_ON(del_nr > 0);
413 			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
414 				ret = -EOPNOTSUPP;
415 				break;
416 			}
417 
418 			btrfs_set_file_extent_num_bytes(leaf, fi,
419 							args->start - key.offset);
420 			btrfs_mark_buffer_dirty(trans, leaf);
421 			if (update_refs && disk_bytenr > 0)
422 				args->bytes_found += extent_end - args->start;
423 			if (args->end == extent_end)
424 				break;
425 
426 			path->slots[0]++;
427 			goto next_slot;
428 		}
429 
430 		/*
431 		 *  | ---- range to drop ----- |
432 		 *    | ------ extent ------ |
433 		 */
434 		if (args->start <= key.offset && args->end >= extent_end) {
435 delete_extent_item:
436 			if (del_nr == 0) {
437 				del_slot = path->slots[0];
438 				del_nr = 1;
439 			} else {
440 				BUG_ON(del_slot + del_nr != path->slots[0]);
441 				del_nr++;
442 			}
443 
444 			if (update_refs &&
445 			    extent_type == BTRFS_FILE_EXTENT_INLINE) {
446 				args->bytes_found += extent_end - key.offset;
447 				extent_end = ALIGN(extent_end,
448 						   fs_info->sectorsize);
449 			} else if (update_refs && disk_bytenr > 0) {
450 				struct btrfs_ref ref = {
451 					.action = BTRFS_DROP_DELAYED_REF,
452 					.bytenr = disk_bytenr,
453 					.num_bytes = num_bytes,
454 					.parent = 0,
455 					.owning_root = btrfs_root_id(root),
456 					.ref_root = btrfs_root_id(root),
457 				};
458 				btrfs_init_data_ref(&ref, key.objectid,
459 						    key.offset - extent_offset,
460 						    0, false);
461 				ret = btrfs_free_extent(trans, &ref);
462 				if (ret) {
463 					btrfs_abort_transaction(trans, ret);
464 					break;
465 				}
466 				args->bytes_found += extent_end - key.offset;
467 			}
468 
469 			if (args->end == extent_end)
470 				break;
471 
472 			if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
473 				path->slots[0]++;
474 				goto next_slot;
475 			}
476 
477 			ret = btrfs_del_items(trans, root, path, del_slot,
478 					      del_nr);
479 			if (ret) {
480 				btrfs_abort_transaction(trans, ret);
481 				break;
482 			}
483 
484 			del_nr = 0;
485 			del_slot = 0;
486 
487 			btrfs_release_path(path);
488 			continue;
489 		}
490 
491 		BUG();
492 	}
493 
494 	if (!ret && del_nr > 0) {
495 		/*
496 		 * Set path->slots[0] to first slot, so that after the delete
497 		 * if items are move off from our leaf to its immediate left or
498 		 * right neighbor leafs, we end up with a correct and adjusted
499 		 * path->slots[0] for our insertion (if args->replace_extent).
500 		 */
501 		path->slots[0] = del_slot;
502 		ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
503 		if (ret)
504 			btrfs_abort_transaction(trans, ret);
505 	}
506 
507 	leaf = path->nodes[0];
508 	/*
509 	 * If btrfs_del_items() was called, it might have deleted a leaf, in
510 	 * which case it unlocked our path, so check path->locks[0] matches a
511 	 * write lock.
512 	 */
513 	if (!ret && args->replace_extent &&
514 	    path->locks[0] == BTRFS_WRITE_LOCK &&
515 	    btrfs_leaf_free_space(leaf) >=
516 	    sizeof(struct btrfs_item) + args->extent_item_size) {
517 
518 		key.objectid = ino;
519 		key.type = BTRFS_EXTENT_DATA_KEY;
520 		key.offset = args->start;
521 		if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
522 			struct btrfs_key slot_key;
523 
524 			btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
525 			if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
526 				path->slots[0]++;
527 		}
528 		btrfs_setup_item_for_insert(trans, root, path, &key,
529 					    args->extent_item_size);
530 		args->extent_inserted = true;
531 	}
532 
533 	if (!args->path)
534 		btrfs_free_path(path);
535 	else if (!args->extent_inserted)
536 		btrfs_release_path(path);
537 out:
538 	args->drop_end = found ? min(args->end, last_end) : args->end;
539 
540 	return ret;
541 }
542 
543 static int extent_mergeable(struct extent_buffer *leaf, int slot,
544 			    u64 objectid, u64 bytenr, u64 orig_offset,
545 			    u64 *start, u64 *end)
546 {
547 	struct btrfs_file_extent_item *fi;
548 	struct btrfs_key key;
549 	u64 extent_end;
550 
551 	if (slot < 0 || slot >= btrfs_header_nritems(leaf))
552 		return 0;
553 
554 	btrfs_item_key_to_cpu(leaf, &key, slot);
555 	if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
556 		return 0;
557 
558 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
559 	if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
560 	    btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
561 	    btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
562 	    btrfs_file_extent_compression(leaf, fi) ||
563 	    btrfs_file_extent_encryption(leaf, fi) ||
564 	    btrfs_file_extent_other_encoding(leaf, fi))
565 		return 0;
566 
567 	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
568 	if ((*start && *start != key.offset) || (*end && *end != extent_end))
569 		return 0;
570 
571 	*start = key.offset;
572 	*end = extent_end;
573 	return 1;
574 }
575 
576 /*
577  * Mark extent in the range start - end as written.
578  *
579  * This changes extent type from 'pre-allocated' to 'regular'. If only
580  * part of extent is marked as written, the extent will be split into
581  * two or three.
582  */
583 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
584 			      struct btrfs_inode *inode, u64 start, u64 end)
585 {
586 	struct btrfs_root *root = inode->root;
587 	struct extent_buffer *leaf;
588 	struct btrfs_path *path;
589 	struct btrfs_file_extent_item *fi;
590 	struct btrfs_ref ref = { 0 };
591 	struct btrfs_key key;
592 	struct btrfs_key new_key;
593 	u64 bytenr;
594 	u64 num_bytes;
595 	u64 extent_end;
596 	u64 orig_offset;
597 	u64 other_start;
598 	u64 other_end;
599 	u64 split;
600 	int del_nr = 0;
601 	int del_slot = 0;
602 	int recow;
603 	int ret = 0;
604 	u64 ino = btrfs_ino(inode);
605 
606 	path = btrfs_alloc_path();
607 	if (!path)
608 		return -ENOMEM;
609 again:
610 	recow = 0;
611 	split = start;
612 	key.objectid = ino;
613 	key.type = BTRFS_EXTENT_DATA_KEY;
614 	key.offset = split;
615 
616 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
617 	if (ret < 0)
618 		goto out;
619 	if (ret > 0 && path->slots[0] > 0)
620 		path->slots[0]--;
621 
622 	leaf = path->nodes[0];
623 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
624 	if (key.objectid != ino ||
625 	    key.type != BTRFS_EXTENT_DATA_KEY) {
626 		ret = -EINVAL;
627 		btrfs_abort_transaction(trans, ret);
628 		goto out;
629 	}
630 	fi = btrfs_item_ptr(leaf, path->slots[0],
631 			    struct btrfs_file_extent_item);
632 	if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
633 		ret = -EINVAL;
634 		btrfs_abort_transaction(trans, ret);
635 		goto out;
636 	}
637 	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
638 	if (key.offset > start || extent_end < end) {
639 		ret = -EINVAL;
640 		btrfs_abort_transaction(trans, ret);
641 		goto out;
642 	}
643 
644 	bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
645 	num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
646 	orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
647 	memcpy(&new_key, &key, sizeof(new_key));
648 
649 	if (start == key.offset && end < extent_end) {
650 		other_start = 0;
651 		other_end = start;
652 		if (extent_mergeable(leaf, path->slots[0] - 1,
653 				     ino, bytenr, orig_offset,
654 				     &other_start, &other_end)) {
655 			new_key.offset = end;
656 			btrfs_set_item_key_safe(trans, path, &new_key);
657 			fi = btrfs_item_ptr(leaf, path->slots[0],
658 					    struct btrfs_file_extent_item);
659 			btrfs_set_file_extent_generation(leaf, fi,
660 							 trans->transid);
661 			btrfs_set_file_extent_num_bytes(leaf, fi,
662 							extent_end - end);
663 			btrfs_set_file_extent_offset(leaf, fi,
664 						     end - orig_offset);
665 			fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
666 					    struct btrfs_file_extent_item);
667 			btrfs_set_file_extent_generation(leaf, fi,
668 							 trans->transid);
669 			btrfs_set_file_extent_num_bytes(leaf, fi,
670 							end - other_start);
671 			btrfs_mark_buffer_dirty(trans, leaf);
672 			goto out;
673 		}
674 	}
675 
676 	if (start > key.offset && end == extent_end) {
677 		other_start = end;
678 		other_end = 0;
679 		if (extent_mergeable(leaf, path->slots[0] + 1,
680 				     ino, bytenr, orig_offset,
681 				     &other_start, &other_end)) {
682 			fi = btrfs_item_ptr(leaf, path->slots[0],
683 					    struct btrfs_file_extent_item);
684 			btrfs_set_file_extent_num_bytes(leaf, fi,
685 							start - key.offset);
686 			btrfs_set_file_extent_generation(leaf, fi,
687 							 trans->transid);
688 			path->slots[0]++;
689 			new_key.offset = start;
690 			btrfs_set_item_key_safe(trans, path, &new_key);
691 
692 			fi = btrfs_item_ptr(leaf, path->slots[0],
693 					    struct btrfs_file_extent_item);
694 			btrfs_set_file_extent_generation(leaf, fi,
695 							 trans->transid);
696 			btrfs_set_file_extent_num_bytes(leaf, fi,
697 							other_end - start);
698 			btrfs_set_file_extent_offset(leaf, fi,
699 						     start - orig_offset);
700 			btrfs_mark_buffer_dirty(trans, leaf);
701 			goto out;
702 		}
703 	}
704 
705 	while (start > key.offset || end < extent_end) {
706 		if (key.offset == start)
707 			split = end;
708 
709 		new_key.offset = split;
710 		ret = btrfs_duplicate_item(trans, root, path, &new_key);
711 		if (ret == -EAGAIN) {
712 			btrfs_release_path(path);
713 			goto again;
714 		}
715 		if (ret < 0) {
716 			btrfs_abort_transaction(trans, ret);
717 			goto out;
718 		}
719 
720 		leaf = path->nodes[0];
721 		fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
722 				    struct btrfs_file_extent_item);
723 		btrfs_set_file_extent_generation(leaf, fi, trans->transid);
724 		btrfs_set_file_extent_num_bytes(leaf, fi,
725 						split - key.offset);
726 
727 		fi = btrfs_item_ptr(leaf, path->slots[0],
728 				    struct btrfs_file_extent_item);
729 
730 		btrfs_set_file_extent_generation(leaf, fi, trans->transid);
731 		btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
732 		btrfs_set_file_extent_num_bytes(leaf, fi,
733 						extent_end - split);
734 		btrfs_mark_buffer_dirty(trans, leaf);
735 
736 		ref.action = BTRFS_ADD_DELAYED_REF;
737 		ref.bytenr = bytenr;
738 		ref.num_bytes = num_bytes;
739 		ref.parent = 0;
740 		ref.owning_root = btrfs_root_id(root);
741 		ref.ref_root = btrfs_root_id(root);
742 		btrfs_init_data_ref(&ref, ino, orig_offset, 0, false);
743 		ret = btrfs_inc_extent_ref(trans, &ref);
744 		if (ret) {
745 			btrfs_abort_transaction(trans, ret);
746 			goto out;
747 		}
748 
749 		if (split == start) {
750 			key.offset = start;
751 		} else {
752 			if (start != key.offset) {
753 				ret = -EINVAL;
754 				btrfs_abort_transaction(trans, ret);
755 				goto out;
756 			}
757 			path->slots[0]--;
758 			extent_end = end;
759 		}
760 		recow = 1;
761 	}
762 
763 	other_start = end;
764 	other_end = 0;
765 
766 	ref.action = BTRFS_DROP_DELAYED_REF;
767 	ref.bytenr = bytenr;
768 	ref.num_bytes = num_bytes;
769 	ref.parent = 0;
770 	ref.owning_root = btrfs_root_id(root);
771 	ref.ref_root = btrfs_root_id(root);
772 	btrfs_init_data_ref(&ref, ino, orig_offset, 0, false);
773 	if (extent_mergeable(leaf, path->slots[0] + 1,
774 			     ino, bytenr, orig_offset,
775 			     &other_start, &other_end)) {
776 		if (recow) {
777 			btrfs_release_path(path);
778 			goto again;
779 		}
780 		extent_end = other_end;
781 		del_slot = path->slots[0] + 1;
782 		del_nr++;
783 		ret = btrfs_free_extent(trans, &ref);
784 		if (ret) {
785 			btrfs_abort_transaction(trans, ret);
786 			goto out;
787 		}
788 	}
789 	other_start = 0;
790 	other_end = start;
791 	if (extent_mergeable(leaf, path->slots[0] - 1,
792 			     ino, bytenr, orig_offset,
793 			     &other_start, &other_end)) {
794 		if (recow) {
795 			btrfs_release_path(path);
796 			goto again;
797 		}
798 		key.offset = other_start;
799 		del_slot = path->slots[0];
800 		del_nr++;
801 		ret = btrfs_free_extent(trans, &ref);
802 		if (ret) {
803 			btrfs_abort_transaction(trans, ret);
804 			goto out;
805 		}
806 	}
807 	if (del_nr == 0) {
808 		fi = btrfs_item_ptr(leaf, path->slots[0],
809 			   struct btrfs_file_extent_item);
810 		btrfs_set_file_extent_type(leaf, fi,
811 					   BTRFS_FILE_EXTENT_REG);
812 		btrfs_set_file_extent_generation(leaf, fi, trans->transid);
813 		btrfs_mark_buffer_dirty(trans, leaf);
814 	} else {
815 		fi = btrfs_item_ptr(leaf, del_slot - 1,
816 			   struct btrfs_file_extent_item);
817 		btrfs_set_file_extent_type(leaf, fi,
818 					   BTRFS_FILE_EXTENT_REG);
819 		btrfs_set_file_extent_generation(leaf, fi, trans->transid);
820 		btrfs_set_file_extent_num_bytes(leaf, fi,
821 						extent_end - key.offset);
822 		btrfs_mark_buffer_dirty(trans, leaf);
823 
824 		ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
825 		if (ret < 0) {
826 			btrfs_abort_transaction(trans, ret);
827 			goto out;
828 		}
829 	}
830 out:
831 	btrfs_free_path(path);
832 	return ret;
833 }
834 
835 /*
836  * On error return an unlocked folio and the error value
837  * On success return a locked folio and 0
838  */
839 static int prepare_uptodate_folio(struct inode *inode, struct folio *folio, u64 pos,
840 				  u64 len, bool force_uptodate)
841 {
842 	u64 clamp_start = max_t(u64, pos, folio_pos(folio));
843 	u64 clamp_end = min_t(u64, pos + len, folio_pos(folio) + folio_size(folio));
844 	int ret = 0;
845 
846 	if (folio_test_uptodate(folio))
847 		return 0;
848 
849 	if (!force_uptodate &&
850 	    IS_ALIGNED(clamp_start, PAGE_SIZE) &&
851 	    IS_ALIGNED(clamp_end, PAGE_SIZE))
852 		return 0;
853 
854 	ret = btrfs_read_folio(NULL, folio);
855 	if (ret)
856 		return ret;
857 	folio_lock(folio);
858 	if (!folio_test_uptodate(folio)) {
859 		folio_unlock(folio);
860 		return -EIO;
861 	}
862 
863 	/*
864 	 * Since btrfs_read_folio() will unlock the folio before it returns,
865 	 * there is a window where btrfs_release_folio() can be called to
866 	 * release the page.  Here we check both inode mapping and page
867 	 * private to make sure the page was not released.
868 	 *
869 	 * The private flag check is essential for subpage as we need to store
870 	 * extra bitmap using folio private.
871 	 */
872 	if (folio->mapping != inode->i_mapping || !folio_test_private(folio)) {
873 		folio_unlock(folio);
874 		return -EAGAIN;
875 	}
876 	return 0;
877 }
878 
879 static gfp_t get_prepare_gfp_flags(struct inode *inode, bool nowait)
880 {
881 	gfp_t gfp;
882 
883 	gfp = btrfs_alloc_write_mask(inode->i_mapping);
884 	if (nowait) {
885 		gfp &= ~__GFP_DIRECT_RECLAIM;
886 		gfp |= GFP_NOWAIT;
887 	}
888 
889 	return gfp;
890 }
891 
892 /*
893  * Get folio into the page cache and lock it.
894  */
895 static noinline int prepare_one_folio(struct inode *inode, struct folio **folio_ret,
896 				      loff_t pos, size_t write_bytes,
897 				      bool force_uptodate, bool nowait)
898 {
899 	unsigned long index = pos >> PAGE_SHIFT;
900 	gfp_t mask = get_prepare_gfp_flags(inode, nowait);
901 	fgf_t fgp_flags = (nowait ? FGP_WRITEBEGIN | FGP_NOWAIT : FGP_WRITEBEGIN);
902 	struct folio *folio;
903 	int ret = 0;
904 
905 again:
906 	folio = __filemap_get_folio(inode->i_mapping, index, fgp_flags, mask);
907 	if (IS_ERR(folio)) {
908 		if (nowait)
909 			ret = -EAGAIN;
910 		else
911 			ret = PTR_ERR(folio);
912 		return ret;
913 	}
914 	/* Only support page sized folio yet. */
915 	ASSERT(folio_order(folio) == 0);
916 	ret = set_folio_extent_mapped(folio);
917 	if (ret < 0) {
918 		folio_unlock(folio);
919 		folio_put(folio);
920 		return ret;
921 	}
922 	ret = prepare_uptodate_folio(inode, folio, pos, write_bytes, force_uptodate);
923 	if (ret) {
924 		/* The folio is already unlocked. */
925 		folio_put(folio);
926 		if (!nowait && ret == -EAGAIN) {
927 			ret = 0;
928 			goto again;
929 		}
930 		return ret;
931 	}
932 	*folio_ret = folio;
933 	return 0;
934 }
935 
936 /*
937  * Locks the extent and properly waits for data=ordered extents to finish
938  * before allowing the folios to be modified if need.
939  *
940  * Return:
941  * 1 - the extent is locked
942  * 0 - the extent is not locked, and everything is OK
943  * -EAGAIN - need to prepare the folios again
944  */
945 static noinline int
946 lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct folio *folio,
947 				loff_t pos, size_t write_bytes,
948 				u64 *lockstart, u64 *lockend, bool nowait,
949 				struct extent_state **cached_state)
950 {
951 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
952 	u64 start_pos;
953 	u64 last_pos;
954 	int ret = 0;
955 
956 	start_pos = round_down(pos, fs_info->sectorsize);
957 	last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1;
958 
959 	if (start_pos < inode->vfs_inode.i_size) {
960 		struct btrfs_ordered_extent *ordered;
961 
962 		if (nowait) {
963 			if (!try_lock_extent(&inode->io_tree, start_pos, last_pos,
964 					     cached_state)) {
965 				folio_unlock(folio);
966 				folio_put(folio);
967 				return -EAGAIN;
968 			}
969 		} else {
970 			lock_extent(&inode->io_tree, start_pos, last_pos, cached_state);
971 		}
972 
973 		ordered = btrfs_lookup_ordered_range(inode, start_pos,
974 						     last_pos - start_pos + 1);
975 		if (ordered &&
976 		    ordered->file_offset + ordered->num_bytes > start_pos &&
977 		    ordered->file_offset <= last_pos) {
978 			unlock_extent(&inode->io_tree, start_pos, last_pos,
979 				      cached_state);
980 			folio_unlock(folio);
981 			folio_put(folio);
982 			btrfs_start_ordered_extent(ordered);
983 			btrfs_put_ordered_extent(ordered);
984 			return -EAGAIN;
985 		}
986 		if (ordered)
987 			btrfs_put_ordered_extent(ordered);
988 
989 		*lockstart = start_pos;
990 		*lockend = last_pos;
991 		ret = 1;
992 	}
993 
994 	/*
995 	 * We should be called after prepare_one_folio() which should have locked
996 	 * all pages in the range.
997 	 */
998 	WARN_ON(!folio_test_locked(folio));
999 
1000 	return ret;
1001 }
1002 
1003 /*
1004  * Check if we can do nocow write into the range [@pos, @pos + @write_bytes)
1005  *
1006  * @pos:         File offset.
1007  * @write_bytes: The length to write, will be updated to the nocow writeable
1008  *               range.
1009  *
1010  * This function will flush ordered extents in the range to ensure proper
1011  * nocow checks.
1012  *
1013  * Return:
1014  * > 0          If we can nocow, and updates @write_bytes.
1015  *  0           If we can't do a nocow write.
1016  * -EAGAIN      If we can't do a nocow write because snapshoting of the inode's
1017  *              root is in progress.
1018  * < 0          If an error happened.
1019  *
1020  * NOTE: Callers need to call btrfs_check_nocow_unlock() if we return > 0.
1021  */
1022 int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
1023 			   size_t *write_bytes, bool nowait)
1024 {
1025 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1026 	struct btrfs_root *root = inode->root;
1027 	struct extent_state *cached_state = NULL;
1028 	u64 lockstart, lockend;
1029 	u64 num_bytes;
1030 	int ret;
1031 
1032 	if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
1033 		return 0;
1034 
1035 	if (!btrfs_drew_try_write_lock(&root->snapshot_lock))
1036 		return -EAGAIN;
1037 
1038 	lockstart = round_down(pos, fs_info->sectorsize);
1039 	lockend = round_up(pos + *write_bytes,
1040 			   fs_info->sectorsize) - 1;
1041 	num_bytes = lockend - lockstart + 1;
1042 
1043 	if (nowait) {
1044 		if (!btrfs_try_lock_ordered_range(inode, lockstart, lockend,
1045 						  &cached_state)) {
1046 			btrfs_drew_write_unlock(&root->snapshot_lock);
1047 			return -EAGAIN;
1048 		}
1049 	} else {
1050 		btrfs_lock_and_flush_ordered_range(inode, lockstart, lockend,
1051 						   &cached_state);
1052 	}
1053 	ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes,
1054 			       NULL, nowait, false);
1055 	if (ret <= 0)
1056 		btrfs_drew_write_unlock(&root->snapshot_lock);
1057 	else
1058 		*write_bytes = min_t(size_t, *write_bytes ,
1059 				     num_bytes - pos + lockstart);
1060 	unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state);
1061 
1062 	return ret;
1063 }
1064 
1065 void btrfs_check_nocow_unlock(struct btrfs_inode *inode)
1066 {
1067 	btrfs_drew_write_unlock(&inode->root->snapshot_lock);
1068 }
1069 
1070 int btrfs_write_check(struct kiocb *iocb, size_t count)
1071 {
1072 	struct file *file = iocb->ki_filp;
1073 	struct inode *inode = file_inode(file);
1074 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1075 	loff_t pos = iocb->ki_pos;
1076 	int ret;
1077 	loff_t oldsize;
1078 	loff_t start_pos;
1079 
1080 	/*
1081 	 * Quickly bail out on NOWAIT writes if we don't have the nodatacow or
1082 	 * prealloc flags, as without those flags we always have to COW. We will
1083 	 * later check if we can really COW into the target range (using
1084 	 * can_nocow_extent() at btrfs_get_blocks_direct_write()).
1085 	 */
1086 	if ((iocb->ki_flags & IOCB_NOWAIT) &&
1087 	    !(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
1088 		return -EAGAIN;
1089 
1090 	ret = file_remove_privs(file);
1091 	if (ret)
1092 		return ret;
1093 
1094 	/*
1095 	 * We reserve space for updating the inode when we reserve space for the
1096 	 * extent we are going to write, so we will enospc out there.  We don't
1097 	 * need to start yet another transaction to update the inode as we will
1098 	 * update the inode when we finish writing whatever data we write.
1099 	 */
1100 	if (!IS_NOCMTIME(inode)) {
1101 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1102 		inode_inc_iversion(inode);
1103 	}
1104 
1105 	start_pos = round_down(pos, fs_info->sectorsize);
1106 	oldsize = i_size_read(inode);
1107 	if (start_pos > oldsize) {
1108 		/* Expand hole size to cover write data, preventing empty gap */
1109 		loff_t end_pos = round_up(pos + count, fs_info->sectorsize);
1110 
1111 		ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, end_pos);
1112 		if (ret)
1113 			return ret;
1114 	}
1115 
1116 	return 0;
1117 }
1118 
1119 ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
1120 {
1121 	struct file *file = iocb->ki_filp;
1122 	loff_t pos;
1123 	struct inode *inode = file_inode(file);
1124 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1125 	struct extent_changeset *data_reserved = NULL;
1126 	u64 release_bytes = 0;
1127 	u64 lockstart;
1128 	u64 lockend;
1129 	size_t num_written = 0;
1130 	ssize_t ret;
1131 	loff_t old_isize = i_size_read(inode);
1132 	unsigned int ilock_flags = 0;
1133 	const bool nowait = (iocb->ki_flags & IOCB_NOWAIT);
1134 	unsigned int bdp_flags = (nowait ? BDP_ASYNC : 0);
1135 	bool only_release_metadata = false;
1136 
1137 	if (nowait)
1138 		ilock_flags |= BTRFS_ILOCK_TRY;
1139 
1140 	ret = btrfs_inode_lock(BTRFS_I(inode), ilock_flags);
1141 	if (ret < 0)
1142 		return ret;
1143 
1144 	ret = generic_write_checks(iocb, i);
1145 	if (ret <= 0)
1146 		goto out;
1147 
1148 	ret = btrfs_write_check(iocb, ret);
1149 	if (ret < 0)
1150 		goto out;
1151 
1152 	pos = iocb->ki_pos;
1153 	while (iov_iter_count(i) > 0) {
1154 		struct extent_state *cached_state = NULL;
1155 		size_t offset = offset_in_page(pos);
1156 		size_t sector_offset;
1157 		size_t write_bytes = min(iov_iter_count(i), PAGE_SIZE - offset);
1158 		size_t reserve_bytes;
1159 		size_t copied;
1160 		size_t dirty_sectors;
1161 		size_t num_sectors;
1162 		struct folio *folio = NULL;
1163 		int extents_locked;
1164 		bool force_page_uptodate = false;
1165 
1166 		/*
1167 		 * Fault pages before locking them in prepare_one_folio()
1168 		 * to avoid recursive lock
1169 		 */
1170 		if (unlikely(fault_in_iov_iter_readable(i, write_bytes))) {
1171 			ret = -EFAULT;
1172 			break;
1173 		}
1174 
1175 		only_release_metadata = false;
1176 		sector_offset = pos & (fs_info->sectorsize - 1);
1177 
1178 		extent_changeset_release(data_reserved);
1179 		ret = btrfs_check_data_free_space(BTRFS_I(inode),
1180 						  &data_reserved, pos,
1181 						  write_bytes, nowait);
1182 		if (ret < 0) {
1183 			int can_nocow;
1184 
1185 			if (nowait && (ret == -ENOSPC || ret == -EAGAIN)) {
1186 				ret = -EAGAIN;
1187 				break;
1188 			}
1189 
1190 			/*
1191 			 * If we don't have to COW at the offset, reserve
1192 			 * metadata only. write_bytes may get smaller than
1193 			 * requested here.
1194 			 */
1195 			can_nocow = btrfs_check_nocow_lock(BTRFS_I(inode), pos,
1196 							   &write_bytes, nowait);
1197 			if (can_nocow < 0)
1198 				ret = can_nocow;
1199 			if (can_nocow > 0)
1200 				ret = 0;
1201 			if (ret)
1202 				break;
1203 			only_release_metadata = true;
1204 		}
1205 
1206 		reserve_bytes = round_up(write_bytes + sector_offset,
1207 					 fs_info->sectorsize);
1208 		WARN_ON(reserve_bytes == 0);
1209 		ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
1210 						      reserve_bytes,
1211 						      reserve_bytes, nowait);
1212 		if (ret) {
1213 			if (!only_release_metadata)
1214 				btrfs_free_reserved_data_space(BTRFS_I(inode),
1215 						data_reserved, pos,
1216 						write_bytes);
1217 			else
1218 				btrfs_check_nocow_unlock(BTRFS_I(inode));
1219 
1220 			if (nowait && ret == -ENOSPC)
1221 				ret = -EAGAIN;
1222 			break;
1223 		}
1224 
1225 		release_bytes = reserve_bytes;
1226 again:
1227 		ret = balance_dirty_pages_ratelimited_flags(inode->i_mapping, bdp_flags);
1228 		if (ret) {
1229 			btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
1230 			break;
1231 		}
1232 
1233 		ret = prepare_one_folio(inode, &folio, pos, write_bytes,
1234 					force_page_uptodate, false);
1235 		if (ret) {
1236 			btrfs_delalloc_release_extents(BTRFS_I(inode),
1237 						       reserve_bytes);
1238 			break;
1239 		}
1240 
1241 		extents_locked = lock_and_cleanup_extent_if_need(BTRFS_I(inode),
1242 						folio, pos, write_bytes, &lockstart,
1243 						&lockend, nowait, &cached_state);
1244 		if (extents_locked < 0) {
1245 			if (!nowait && extents_locked == -EAGAIN)
1246 				goto again;
1247 
1248 			btrfs_delalloc_release_extents(BTRFS_I(inode),
1249 						       reserve_bytes);
1250 			ret = extents_locked;
1251 			break;
1252 		}
1253 
1254 		copied = btrfs_copy_from_user(pos, write_bytes, folio, i);
1255 
1256 		num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
1257 		dirty_sectors = round_up(copied + sector_offset,
1258 					fs_info->sectorsize);
1259 		dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
1260 
1261 		if (copied == 0) {
1262 			force_page_uptodate = true;
1263 			dirty_sectors = 0;
1264 		} else {
1265 			force_page_uptodate = false;
1266 		}
1267 
1268 		if (num_sectors > dirty_sectors) {
1269 			/* release everything except the sectors we dirtied */
1270 			release_bytes -= dirty_sectors << fs_info->sectorsize_bits;
1271 			if (only_release_metadata) {
1272 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
1273 							release_bytes, true);
1274 			} else {
1275 				u64 release_start = round_up(pos + copied,
1276 							     fs_info->sectorsize);
1277 				btrfs_delalloc_release_space(BTRFS_I(inode),
1278 						data_reserved, release_start,
1279 						release_bytes, true);
1280 			}
1281 		}
1282 
1283 		release_bytes = round_up(copied + sector_offset,
1284 					fs_info->sectorsize);
1285 
1286 		ret = btrfs_dirty_folio(BTRFS_I(inode), folio, pos, copied,
1287 					&cached_state, only_release_metadata);
1288 
1289 		/*
1290 		 * If we have not locked the extent range, because the range's
1291 		 * start offset is >= i_size, we might still have a non-NULL
1292 		 * cached extent state, acquired while marking the extent range
1293 		 * as delalloc through btrfs_dirty_page(). Therefore free any
1294 		 * possible cached extent state to avoid a memory leak.
1295 		 */
1296 		if (extents_locked)
1297 			unlock_extent(&BTRFS_I(inode)->io_tree, lockstart,
1298 				      lockend, &cached_state);
1299 		else
1300 			free_extent_state(cached_state);
1301 
1302 		btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
1303 		if (ret) {
1304 			btrfs_drop_folio(fs_info, folio, pos, copied);
1305 			break;
1306 		}
1307 
1308 		release_bytes = 0;
1309 		if (only_release_metadata)
1310 			btrfs_check_nocow_unlock(BTRFS_I(inode));
1311 
1312 		btrfs_drop_folio(fs_info, folio, pos, copied);
1313 
1314 		cond_resched();
1315 
1316 		pos += copied;
1317 		num_written += copied;
1318 	}
1319 
1320 	if (release_bytes) {
1321 		if (only_release_metadata) {
1322 			btrfs_check_nocow_unlock(BTRFS_I(inode));
1323 			btrfs_delalloc_release_metadata(BTRFS_I(inode),
1324 					release_bytes, true);
1325 		} else {
1326 			btrfs_delalloc_release_space(BTRFS_I(inode),
1327 					data_reserved,
1328 					round_down(pos, fs_info->sectorsize),
1329 					release_bytes, true);
1330 		}
1331 	}
1332 
1333 	extent_changeset_free(data_reserved);
1334 	if (num_written > 0) {
1335 		pagecache_isize_extended(inode, old_isize, iocb->ki_pos);
1336 		iocb->ki_pos += num_written;
1337 	}
1338 out:
1339 	btrfs_inode_unlock(BTRFS_I(inode), ilock_flags);
1340 	return num_written ? num_written : ret;
1341 }
1342 
1343 static ssize_t btrfs_encoded_write(struct kiocb *iocb, struct iov_iter *from,
1344 			const struct btrfs_ioctl_encoded_io_args *encoded)
1345 {
1346 	struct file *file = iocb->ki_filp;
1347 	struct inode *inode = file_inode(file);
1348 	loff_t count;
1349 	ssize_t ret;
1350 
1351 	btrfs_inode_lock(BTRFS_I(inode), 0);
1352 	count = encoded->len;
1353 	ret = generic_write_checks_count(iocb, &count);
1354 	if (ret == 0 && count != encoded->len) {
1355 		/*
1356 		 * The write got truncated by generic_write_checks_count(). We
1357 		 * can't do a partial encoded write.
1358 		 */
1359 		ret = -EFBIG;
1360 	}
1361 	if (ret || encoded->len == 0)
1362 		goto out;
1363 
1364 	ret = btrfs_write_check(iocb, encoded->len);
1365 	if (ret < 0)
1366 		goto out;
1367 
1368 	ret = btrfs_do_encoded_write(iocb, from, encoded);
1369 out:
1370 	btrfs_inode_unlock(BTRFS_I(inode), 0);
1371 	return ret;
1372 }
1373 
1374 ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from,
1375 			    const struct btrfs_ioctl_encoded_io_args *encoded)
1376 {
1377 	struct file *file = iocb->ki_filp;
1378 	struct btrfs_inode *inode = BTRFS_I(file_inode(file));
1379 	ssize_t num_written, num_sync;
1380 
1381 	/*
1382 	 * If the fs flips readonly due to some impossible error, although we
1383 	 * have opened a file as writable, we have to stop this write operation
1384 	 * to ensure consistency.
1385 	 */
1386 	if (BTRFS_FS_ERROR(inode->root->fs_info))
1387 		return -EROFS;
1388 
1389 	if (encoded && (iocb->ki_flags & IOCB_NOWAIT))
1390 		return -EOPNOTSUPP;
1391 
1392 	if (encoded) {
1393 		num_written = btrfs_encoded_write(iocb, from, encoded);
1394 		num_sync = encoded->len;
1395 	} else if (iocb->ki_flags & IOCB_DIRECT) {
1396 		num_written = btrfs_direct_write(iocb, from);
1397 		num_sync = num_written;
1398 	} else {
1399 		num_written = btrfs_buffered_write(iocb, from);
1400 		num_sync = num_written;
1401 	}
1402 
1403 	btrfs_set_inode_last_sub_trans(inode);
1404 
1405 	if (num_sync > 0) {
1406 		num_sync = generic_write_sync(iocb, num_sync);
1407 		if (num_sync < 0)
1408 			num_written = num_sync;
1409 	}
1410 
1411 	return num_written;
1412 }
1413 
1414 static ssize_t btrfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1415 {
1416 	return btrfs_do_write_iter(iocb, from, NULL);
1417 }
1418 
1419 int btrfs_release_file(struct inode *inode, struct file *filp)
1420 {
1421 	struct btrfs_file_private *private = filp->private_data;
1422 
1423 	if (private) {
1424 		kfree(private->filldir_buf);
1425 		free_extent_state(private->llseek_cached_state);
1426 		kfree(private);
1427 		filp->private_data = NULL;
1428 	}
1429 
1430 	/*
1431 	 * Set by setattr when we are about to truncate a file from a non-zero
1432 	 * size to a zero size.  This tries to flush down new bytes that may
1433 	 * have been written if the application were using truncate to replace
1434 	 * a file in place.
1435 	 */
1436 	if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
1437 			       &BTRFS_I(inode)->runtime_flags))
1438 			filemap_flush(inode->i_mapping);
1439 	return 0;
1440 }
1441 
1442 static int start_ordered_ops(struct btrfs_inode *inode, loff_t start, loff_t end)
1443 {
1444 	int ret;
1445 	struct blk_plug plug;
1446 
1447 	/*
1448 	 * This is only called in fsync, which would do synchronous writes, so
1449 	 * a plug can merge adjacent IOs as much as possible.  Esp. in case of
1450 	 * multiple disks using raid profile, a large IO can be split to
1451 	 * several segments of stripe length (currently 64K).
1452 	 */
1453 	blk_start_plug(&plug);
1454 	ret = btrfs_fdatawrite_range(inode, start, end);
1455 	blk_finish_plug(&plug);
1456 
1457 	return ret;
1458 }
1459 
1460 static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
1461 {
1462 	struct btrfs_inode *inode = ctx->inode;
1463 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1464 
1465 	if (btrfs_inode_in_log(inode, btrfs_get_fs_generation(fs_info)) &&
1466 	    list_empty(&ctx->ordered_extents))
1467 		return true;
1468 
1469 	/*
1470 	 * If we are doing a fast fsync we can not bail out if the inode's
1471 	 * last_trans is <= then the last committed transaction, because we only
1472 	 * update the last_trans of the inode during ordered extent completion,
1473 	 * and for a fast fsync we don't wait for that, we only wait for the
1474 	 * writeback to complete.
1475 	 */
1476 	if (inode->last_trans <= btrfs_get_last_trans_committed(fs_info) &&
1477 	    (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
1478 	     list_empty(&ctx->ordered_extents)))
1479 		return true;
1480 
1481 	return false;
1482 }
1483 
1484 /*
1485  * fsync call for both files and directories.  This logs the inode into
1486  * the tree log instead of forcing full commits whenever possible.
1487  *
1488  * It needs to call filemap_fdatawait so that all ordered extent updates are
1489  * in the metadata btree are up to date for copying to the log.
1490  *
1491  * It drops the inode mutex before doing the tree log commit.  This is an
1492  * important optimization for directories because holding the mutex prevents
1493  * new operations on the dir while we write to disk.
1494  */
1495 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1496 {
1497 	struct dentry *dentry = file_dentry(file);
1498 	struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
1499 	struct btrfs_root *root = inode->root;
1500 	struct btrfs_fs_info *fs_info = root->fs_info;
1501 	struct btrfs_trans_handle *trans;
1502 	struct btrfs_log_ctx ctx;
1503 	int ret = 0, err;
1504 	u64 len;
1505 	bool full_sync;
1506 	bool skip_ilock = false;
1507 
1508 	if (current->journal_info == BTRFS_TRANS_DIO_WRITE_STUB) {
1509 		skip_ilock = true;
1510 		current->journal_info = NULL;
1511 		btrfs_assert_inode_locked(inode);
1512 	}
1513 
1514 	trace_btrfs_sync_file(file, datasync);
1515 
1516 	btrfs_init_log_ctx(&ctx, inode);
1517 
1518 	/*
1519 	 * Always set the range to a full range, otherwise we can get into
1520 	 * several problems, from missing file extent items to represent holes
1521 	 * when not using the NO_HOLES feature, to log tree corruption due to
1522 	 * races between hole detection during logging and completion of ordered
1523 	 * extents outside the range, to missing checksums due to ordered extents
1524 	 * for which we flushed only a subset of their pages.
1525 	 */
1526 	start = 0;
1527 	end = LLONG_MAX;
1528 	len = (u64)LLONG_MAX + 1;
1529 
1530 	/*
1531 	 * We write the dirty pages in the range and wait until they complete
1532 	 * out of the ->i_mutex. If so, we can flush the dirty pages by
1533 	 * multi-task, and make the performance up.  See
1534 	 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
1535 	 */
1536 	ret = start_ordered_ops(inode, start, end);
1537 	if (ret)
1538 		goto out;
1539 
1540 	if (skip_ilock)
1541 		down_write(&inode->i_mmap_lock);
1542 	else
1543 		btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
1544 
1545 	atomic_inc(&root->log_batch);
1546 
1547 	/*
1548 	 * Before we acquired the inode's lock and the mmap lock, someone may
1549 	 * have dirtied more pages in the target range. We need to make sure
1550 	 * that writeback for any such pages does not start while we are logging
1551 	 * the inode, because if it does, any of the following might happen when
1552 	 * we are not doing a full inode sync:
1553 	 *
1554 	 * 1) We log an extent after its writeback finishes but before its
1555 	 *    checksums are added to the csum tree, leading to -EIO errors
1556 	 *    when attempting to read the extent after a log replay.
1557 	 *
1558 	 * 2) We can end up logging an extent before its writeback finishes.
1559 	 *    Therefore after the log replay we will have a file extent item
1560 	 *    pointing to an unwritten extent (and no data checksums as well).
1561 	 *
1562 	 * So trigger writeback for any eventual new dirty pages and then we
1563 	 * wait for all ordered extents to complete below.
1564 	 */
1565 	ret = start_ordered_ops(inode, start, end);
1566 	if (ret) {
1567 		if (skip_ilock)
1568 			up_write(&inode->i_mmap_lock);
1569 		else
1570 			btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1571 		goto out;
1572 	}
1573 
1574 	/*
1575 	 * Always check for the full sync flag while holding the inode's lock,
1576 	 * to avoid races with other tasks. The flag must be either set all the
1577 	 * time during logging or always off all the time while logging.
1578 	 * We check the flag here after starting delalloc above, because when
1579 	 * running delalloc the full sync flag may be set if we need to drop
1580 	 * extra extent map ranges due to temporary memory allocation failures.
1581 	 */
1582 	full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
1583 
1584 	/*
1585 	 * We have to do this here to avoid the priority inversion of waiting on
1586 	 * IO of a lower priority task while holding a transaction open.
1587 	 *
1588 	 * For a full fsync we wait for the ordered extents to complete while
1589 	 * for a fast fsync we wait just for writeback to complete, and then
1590 	 * attach the ordered extents to the transaction so that a transaction
1591 	 * commit waits for their completion, to avoid data loss if we fsync,
1592 	 * the current transaction commits before the ordered extents complete
1593 	 * and a power failure happens right after that.
1594 	 *
1595 	 * For zoned filesystem, if a write IO uses a ZONE_APPEND command, the
1596 	 * logical address recorded in the ordered extent may change. We need
1597 	 * to wait for the IO to stabilize the logical address.
1598 	 */
1599 	if (full_sync || btrfs_is_zoned(fs_info)) {
1600 		ret = btrfs_wait_ordered_range(inode, start, len);
1601 		clear_bit(BTRFS_INODE_COW_WRITE_ERROR, &inode->runtime_flags);
1602 	} else {
1603 		/*
1604 		 * Get our ordered extents as soon as possible to avoid doing
1605 		 * checksum lookups in the csum tree, and use instead the
1606 		 * checksums attached to the ordered extents.
1607 		 */
1608 		btrfs_get_ordered_extents_for_logging(inode, &ctx.ordered_extents);
1609 		ret = filemap_fdatawait_range(inode->vfs_inode.i_mapping, start, end);
1610 		if (ret)
1611 			goto out_release_extents;
1612 
1613 		/*
1614 		 * Check and clear the BTRFS_INODE_COW_WRITE_ERROR now after
1615 		 * starting and waiting for writeback, because for buffered IO
1616 		 * it may have been set during the end IO callback
1617 		 * (end_bbio_data_write() -> btrfs_finish_ordered_extent()) in
1618 		 * case an error happened and we need to wait for ordered
1619 		 * extents to complete so that any extent maps that point to
1620 		 * unwritten locations are dropped and we don't log them.
1621 		 */
1622 		if (test_and_clear_bit(BTRFS_INODE_COW_WRITE_ERROR, &inode->runtime_flags))
1623 			ret = btrfs_wait_ordered_range(inode, start, len);
1624 	}
1625 
1626 	if (ret)
1627 		goto out_release_extents;
1628 
1629 	atomic_inc(&root->log_batch);
1630 
1631 	if (skip_inode_logging(&ctx)) {
1632 		/*
1633 		 * We've had everything committed since the last time we were
1634 		 * modified so clear this flag in case it was set for whatever
1635 		 * reason, it's no longer relevant.
1636 		 */
1637 		clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
1638 		/*
1639 		 * An ordered extent might have started before and completed
1640 		 * already with io errors, in which case the inode was not
1641 		 * updated and we end up here. So check the inode's mapping
1642 		 * for any errors that might have happened since we last
1643 		 * checked called fsync.
1644 		 */
1645 		ret = filemap_check_wb_err(inode->vfs_inode.i_mapping, file->f_wb_err);
1646 		goto out_release_extents;
1647 	}
1648 
1649 	btrfs_init_log_ctx_scratch_eb(&ctx);
1650 
1651 	/*
1652 	 * We use start here because we will need to wait on the IO to complete
1653 	 * in btrfs_sync_log, which could require joining a transaction (for
1654 	 * example checking cross references in the nocow path).  If we use join
1655 	 * here we could get into a situation where we're waiting on IO to
1656 	 * happen that is blocked on a transaction trying to commit.  With start
1657 	 * we inc the extwriter counter, so we wait for all extwriters to exit
1658 	 * before we start blocking joiners.  This comment is to keep somebody
1659 	 * from thinking they are super smart and changing this to
1660 	 * btrfs_join_transaction *cough*Josef*cough*.
1661 	 */
1662 	trans = btrfs_start_transaction(root, 0);
1663 	if (IS_ERR(trans)) {
1664 		ret = PTR_ERR(trans);
1665 		goto out_release_extents;
1666 	}
1667 	trans->in_fsync = true;
1668 
1669 	ret = btrfs_log_dentry_safe(trans, dentry, &ctx);
1670 	/*
1671 	 * Scratch eb no longer needed, release before syncing log or commit
1672 	 * transaction, to avoid holding unnecessary memory during such long
1673 	 * operations.
1674 	 */
1675 	if (ctx.scratch_eb) {
1676 		free_extent_buffer(ctx.scratch_eb);
1677 		ctx.scratch_eb = NULL;
1678 	}
1679 	btrfs_release_log_ctx_extents(&ctx);
1680 	if (ret < 0) {
1681 		/* Fallthrough and commit/free transaction. */
1682 		ret = BTRFS_LOG_FORCE_COMMIT;
1683 	}
1684 
1685 	/* we've logged all the items and now have a consistent
1686 	 * version of the file in the log.  It is possible that
1687 	 * someone will come in and modify the file, but that's
1688 	 * fine because the log is consistent on disk, and we
1689 	 * have references to all of the file's extents
1690 	 *
1691 	 * It is possible that someone will come in and log the
1692 	 * file again, but that will end up using the synchronization
1693 	 * inside btrfs_sync_log to keep things safe.
1694 	 */
1695 	if (skip_ilock)
1696 		up_write(&inode->i_mmap_lock);
1697 	else
1698 		btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1699 
1700 	if (ret == BTRFS_NO_LOG_SYNC) {
1701 		ret = btrfs_end_transaction(trans);
1702 		goto out;
1703 	}
1704 
1705 	/* We successfully logged the inode, attempt to sync the log. */
1706 	if (!ret) {
1707 		ret = btrfs_sync_log(trans, root, &ctx);
1708 		if (!ret) {
1709 			ret = btrfs_end_transaction(trans);
1710 			goto out;
1711 		}
1712 	}
1713 
1714 	/*
1715 	 * At this point we need to commit the transaction because we had
1716 	 * btrfs_need_log_full_commit() or some other error.
1717 	 *
1718 	 * If we didn't do a full sync we have to stop the trans handle, wait on
1719 	 * the ordered extents, start it again and commit the transaction.  If
1720 	 * we attempt to wait on the ordered extents here we could deadlock with
1721 	 * something like fallocate() that is holding the extent lock trying to
1722 	 * start a transaction while some other thread is trying to commit the
1723 	 * transaction while we (fsync) are currently holding the transaction
1724 	 * open.
1725 	 */
1726 	if (!full_sync) {
1727 		ret = btrfs_end_transaction(trans);
1728 		if (ret)
1729 			goto out;
1730 		ret = btrfs_wait_ordered_range(inode, start, len);
1731 		if (ret)
1732 			goto out;
1733 
1734 		/*
1735 		 * This is safe to use here because we're only interested in
1736 		 * making sure the transaction that had the ordered extents is
1737 		 * committed.  We aren't waiting on anything past this point,
1738 		 * we're purely getting the transaction and committing it.
1739 		 */
1740 		trans = btrfs_attach_transaction_barrier(root);
1741 		if (IS_ERR(trans)) {
1742 			ret = PTR_ERR(trans);
1743 
1744 			/*
1745 			 * We committed the transaction and there's no currently
1746 			 * running transaction, this means everything we care
1747 			 * about made it to disk and we are done.
1748 			 */
1749 			if (ret == -ENOENT)
1750 				ret = 0;
1751 			goto out;
1752 		}
1753 	}
1754 
1755 	ret = btrfs_commit_transaction(trans);
1756 out:
1757 	free_extent_buffer(ctx.scratch_eb);
1758 	ASSERT(list_empty(&ctx.list));
1759 	ASSERT(list_empty(&ctx.conflict_inodes));
1760 	err = file_check_and_advance_wb_err(file);
1761 	if (!ret)
1762 		ret = err;
1763 	return ret > 0 ? -EIO : ret;
1764 
1765 out_release_extents:
1766 	btrfs_release_log_ctx_extents(&ctx);
1767 	if (skip_ilock)
1768 		up_write(&inode->i_mmap_lock);
1769 	else
1770 		btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1771 	goto out;
1772 }
1773 
1774 /*
1775  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1776  * called from a page fault handler when a page is first dirtied. Hence we must
1777  * be careful to check for EOF conditions here. We set the page up correctly
1778  * for a written page which means we get ENOSPC checking when writing into
1779  * holes and correct delalloc and unwritten extent mapping on filesystems that
1780  * support these features.
1781  *
1782  * We are not allowed to take the i_mutex here so we have to play games to
1783  * protect against truncate races as the page could now be beyond EOF.  Because
1784  * truncate_setsize() writes the inode size before removing pages, once we have
1785  * the page lock we can determine safely if the page is beyond EOF. If it is not
1786  * beyond EOF, then the page is guaranteed safe against truncation until we
1787  * unlock the page.
1788  */
1789 static vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
1790 {
1791 	struct page *page = vmf->page;
1792 	struct folio *folio = page_folio(page);
1793 	struct inode *inode = file_inode(vmf->vma->vm_file);
1794 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1795 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1796 	struct btrfs_ordered_extent *ordered;
1797 	struct extent_state *cached_state = NULL;
1798 	struct extent_changeset *data_reserved = NULL;
1799 	unsigned long zero_start;
1800 	loff_t size;
1801 	vm_fault_t ret;
1802 	int ret2;
1803 	int reserved = 0;
1804 	u64 reserved_space;
1805 	u64 page_start;
1806 	u64 page_end;
1807 	u64 end;
1808 
1809 	ASSERT(folio_order(folio) == 0);
1810 
1811 	reserved_space = PAGE_SIZE;
1812 
1813 	sb_start_pagefault(inode->i_sb);
1814 	page_start = folio_pos(folio);
1815 	page_end = page_start + folio_size(folio) - 1;
1816 	end = page_end;
1817 
1818 	/*
1819 	 * Reserving delalloc space after obtaining the page lock can lead to
1820 	 * deadlock. For example, if a dirty page is locked by this function
1821 	 * and the call to btrfs_delalloc_reserve_space() ends up triggering
1822 	 * dirty page write out, then the btrfs_writepages() function could
1823 	 * end up waiting indefinitely to get a lock on the page currently
1824 	 * being processed by btrfs_page_mkwrite() function.
1825 	 */
1826 	ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
1827 					    page_start, reserved_space);
1828 	if (!ret2) {
1829 		ret2 = file_update_time(vmf->vma->vm_file);
1830 		reserved = 1;
1831 	}
1832 	if (ret2) {
1833 		ret = vmf_error(ret2);
1834 		if (reserved)
1835 			goto out;
1836 		goto out_noreserve;
1837 	}
1838 
1839 	/* Make the VM retry the fault. */
1840 	ret = VM_FAULT_NOPAGE;
1841 again:
1842 	down_read(&BTRFS_I(inode)->i_mmap_lock);
1843 	folio_lock(folio);
1844 	size = i_size_read(inode);
1845 
1846 	if ((folio->mapping != inode->i_mapping) ||
1847 	    (page_start >= size)) {
1848 		/* Page got truncated out from underneath us. */
1849 		goto out_unlock;
1850 	}
1851 	folio_wait_writeback(folio);
1852 
1853 	lock_extent(io_tree, page_start, page_end, &cached_state);
1854 	ret2 = set_folio_extent_mapped(folio);
1855 	if (ret2 < 0) {
1856 		ret = vmf_error(ret2);
1857 		unlock_extent(io_tree, page_start, page_end, &cached_state);
1858 		goto out_unlock;
1859 	}
1860 
1861 	/*
1862 	 * We can't set the delalloc bits if there are pending ordered
1863 	 * extents.  Drop our locks and wait for them to finish.
1864 	 */
1865 	ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, PAGE_SIZE);
1866 	if (ordered) {
1867 		unlock_extent(io_tree, page_start, page_end, &cached_state);
1868 		folio_unlock(folio);
1869 		up_read(&BTRFS_I(inode)->i_mmap_lock);
1870 		btrfs_start_ordered_extent(ordered);
1871 		btrfs_put_ordered_extent(ordered);
1872 		goto again;
1873 	}
1874 
1875 	if (folio->index == ((size - 1) >> PAGE_SHIFT)) {
1876 		reserved_space = round_up(size - page_start, fs_info->sectorsize);
1877 		if (reserved_space < PAGE_SIZE) {
1878 			end = page_start + reserved_space - 1;
1879 			btrfs_delalloc_release_space(BTRFS_I(inode),
1880 					data_reserved, page_start,
1881 					PAGE_SIZE - reserved_space, true);
1882 		}
1883 	}
1884 
1885 	/*
1886 	 * page_mkwrite gets called when the page is firstly dirtied after it's
1887 	 * faulted in, but write(2) could also dirty a page and set delalloc
1888 	 * bits, thus in this case for space account reason, we still need to
1889 	 * clear any delalloc bits within this page range since we have to
1890 	 * reserve data&meta space before lock_page() (see above comments).
1891 	 */
1892 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
1893 			  EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1894 			  EXTENT_DEFRAG, &cached_state);
1895 
1896 	ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
1897 					&cached_state);
1898 	if (ret2) {
1899 		unlock_extent(io_tree, page_start, page_end, &cached_state);
1900 		ret = VM_FAULT_SIGBUS;
1901 		goto out_unlock;
1902 	}
1903 
1904 	/* Page is wholly or partially inside EOF. */
1905 	if (page_start + folio_size(folio) > size)
1906 		zero_start = offset_in_folio(folio, size);
1907 	else
1908 		zero_start = PAGE_SIZE;
1909 
1910 	if (zero_start != PAGE_SIZE)
1911 		folio_zero_range(folio, zero_start, folio_size(folio) - zero_start);
1912 
1913 	btrfs_folio_clear_checked(fs_info, folio, page_start, PAGE_SIZE);
1914 	btrfs_folio_set_dirty(fs_info, folio, page_start, end + 1 - page_start);
1915 	btrfs_folio_set_uptodate(fs_info, folio, page_start, end + 1 - page_start);
1916 
1917 	btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
1918 
1919 	unlock_extent(io_tree, page_start, page_end, &cached_state);
1920 	up_read(&BTRFS_I(inode)->i_mmap_lock);
1921 
1922 	btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
1923 	sb_end_pagefault(inode->i_sb);
1924 	extent_changeset_free(data_reserved);
1925 	return VM_FAULT_LOCKED;
1926 
1927 out_unlock:
1928 	folio_unlock(folio);
1929 	up_read(&BTRFS_I(inode)->i_mmap_lock);
1930 out:
1931 	btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
1932 	btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
1933 				     reserved_space, (ret != 0));
1934 out_noreserve:
1935 	sb_end_pagefault(inode->i_sb);
1936 	extent_changeset_free(data_reserved);
1937 	return ret;
1938 }
1939 
1940 static const struct vm_operations_struct btrfs_file_vm_ops = {
1941 	.fault		= filemap_fault,
1942 	.map_pages	= filemap_map_pages,
1943 	.page_mkwrite	= btrfs_page_mkwrite,
1944 };
1945 
1946 static int btrfs_file_mmap(struct file	*filp, struct vm_area_struct *vma)
1947 {
1948 	struct address_space *mapping = filp->f_mapping;
1949 
1950 	if (!mapping->a_ops->read_folio)
1951 		return -ENOEXEC;
1952 
1953 	file_accessed(filp);
1954 	vma->vm_ops = &btrfs_file_vm_ops;
1955 
1956 	return 0;
1957 }
1958 
1959 static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf,
1960 			  int slot, u64 start, u64 end)
1961 {
1962 	struct btrfs_file_extent_item *fi;
1963 	struct btrfs_key key;
1964 
1965 	if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1966 		return 0;
1967 
1968 	btrfs_item_key_to_cpu(leaf, &key, slot);
1969 	if (key.objectid != btrfs_ino(inode) ||
1970 	    key.type != BTRFS_EXTENT_DATA_KEY)
1971 		return 0;
1972 
1973 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1974 
1975 	if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1976 		return 0;
1977 
1978 	if (btrfs_file_extent_disk_bytenr(leaf, fi))
1979 		return 0;
1980 
1981 	if (key.offset == end)
1982 		return 1;
1983 	if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
1984 		return 1;
1985 	return 0;
1986 }
1987 
1988 static int fill_holes(struct btrfs_trans_handle *trans,
1989 		struct btrfs_inode *inode,
1990 		struct btrfs_path *path, u64 offset, u64 end)
1991 {
1992 	struct btrfs_fs_info *fs_info = trans->fs_info;
1993 	struct btrfs_root *root = inode->root;
1994 	struct extent_buffer *leaf;
1995 	struct btrfs_file_extent_item *fi;
1996 	struct extent_map *hole_em;
1997 	struct btrfs_key key;
1998 	int ret;
1999 
2000 	if (btrfs_fs_incompat(fs_info, NO_HOLES))
2001 		goto out;
2002 
2003 	key.objectid = btrfs_ino(inode);
2004 	key.type = BTRFS_EXTENT_DATA_KEY;
2005 	key.offset = offset;
2006 
2007 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2008 	if (ret <= 0) {
2009 		/*
2010 		 * We should have dropped this offset, so if we find it then
2011 		 * something has gone horribly wrong.
2012 		 */
2013 		if (ret == 0)
2014 			ret = -EINVAL;
2015 		return ret;
2016 	}
2017 
2018 	leaf = path->nodes[0];
2019 	if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) {
2020 		u64 num_bytes;
2021 
2022 		path->slots[0]--;
2023 		fi = btrfs_item_ptr(leaf, path->slots[0],
2024 				    struct btrfs_file_extent_item);
2025 		num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2026 			end - offset;
2027 		btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2028 		btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2029 		btrfs_set_file_extent_offset(leaf, fi, 0);
2030 		btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2031 		btrfs_mark_buffer_dirty(trans, leaf);
2032 		goto out;
2033 	}
2034 
2035 	if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
2036 		u64 num_bytes;
2037 
2038 		key.offset = offset;
2039 		btrfs_set_item_key_safe(trans, path, &key);
2040 		fi = btrfs_item_ptr(leaf, path->slots[0],
2041 				    struct btrfs_file_extent_item);
2042 		num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2043 			offset;
2044 		btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2045 		btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2046 		btrfs_set_file_extent_offset(leaf, fi, 0);
2047 		btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2048 		btrfs_mark_buffer_dirty(trans, leaf);
2049 		goto out;
2050 	}
2051 	btrfs_release_path(path);
2052 
2053 	ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset,
2054 				       end - offset);
2055 	if (ret)
2056 		return ret;
2057 
2058 out:
2059 	btrfs_release_path(path);
2060 
2061 	hole_em = alloc_extent_map();
2062 	if (!hole_em) {
2063 		btrfs_drop_extent_map_range(inode, offset, end - 1, false);
2064 		btrfs_set_inode_full_sync(inode);
2065 	} else {
2066 		hole_em->start = offset;
2067 		hole_em->len = end - offset;
2068 		hole_em->ram_bytes = hole_em->len;
2069 
2070 		hole_em->disk_bytenr = EXTENT_MAP_HOLE;
2071 		hole_em->disk_num_bytes = 0;
2072 		hole_em->generation = trans->transid;
2073 
2074 		ret = btrfs_replace_extent_map_range(inode, hole_em, true);
2075 		free_extent_map(hole_em);
2076 		if (ret)
2077 			btrfs_set_inode_full_sync(inode);
2078 	}
2079 
2080 	return 0;
2081 }
2082 
2083 /*
2084  * Find a hole extent on given inode and change start/len to the end of hole
2085  * extent.(hole/vacuum extent whose em->start <= start &&
2086  *	   em->start + em->len > start)
2087  * When a hole extent is found, return 1 and modify start/len.
2088  */
2089 static int find_first_non_hole(struct btrfs_inode *inode, u64 *start, u64 *len)
2090 {
2091 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
2092 	struct extent_map *em;
2093 	int ret = 0;
2094 
2095 	em = btrfs_get_extent(inode, NULL,
2096 			      round_down(*start, fs_info->sectorsize),
2097 			      round_up(*len, fs_info->sectorsize));
2098 	if (IS_ERR(em))
2099 		return PTR_ERR(em);
2100 
2101 	/* Hole or vacuum extent(only exists in no-hole mode) */
2102 	if (em->disk_bytenr == EXTENT_MAP_HOLE) {
2103 		ret = 1;
2104 		*len = em->start + em->len > *start + *len ?
2105 		       0 : *start + *len - em->start - em->len;
2106 		*start = em->start + em->len;
2107 	}
2108 	free_extent_map(em);
2109 	return ret;
2110 }
2111 
2112 static void btrfs_punch_hole_lock_range(struct inode *inode,
2113 					const u64 lockstart,
2114 					const u64 lockend,
2115 					struct extent_state **cached_state)
2116 {
2117 	/*
2118 	 * For subpage case, if the range is not at page boundary, we could
2119 	 * have pages at the leading/tailing part of the range.
2120 	 * This could lead to dead loop since filemap_range_has_page()
2121 	 * will always return true.
2122 	 * So here we need to do extra page alignment for
2123 	 * filemap_range_has_page().
2124 	 */
2125 	const u64 page_lockstart = round_up(lockstart, PAGE_SIZE);
2126 	const u64 page_lockend = round_down(lockend + 1, PAGE_SIZE) - 1;
2127 
2128 	while (1) {
2129 		truncate_pagecache_range(inode, lockstart, lockend);
2130 
2131 		lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2132 			    cached_state);
2133 		/*
2134 		 * We can't have ordered extents in the range, nor dirty/writeback
2135 		 * pages, because we have locked the inode's VFS lock in exclusive
2136 		 * mode, we have locked the inode's i_mmap_lock in exclusive mode,
2137 		 * we have flushed all delalloc in the range and we have waited
2138 		 * for any ordered extents in the range to complete.
2139 		 * We can race with anyone reading pages from this range, so after
2140 		 * locking the range check if we have pages in the range, and if
2141 		 * we do, unlock the range and retry.
2142 		 */
2143 		if (!filemap_range_has_page(inode->i_mapping, page_lockstart,
2144 					    page_lockend))
2145 			break;
2146 
2147 		unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2148 			      cached_state);
2149 	}
2150 
2151 	btrfs_assert_inode_range_clean(BTRFS_I(inode), lockstart, lockend);
2152 }
2153 
2154 static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans,
2155 				     struct btrfs_inode *inode,
2156 				     struct btrfs_path *path,
2157 				     struct btrfs_replace_extent_info *extent_info,
2158 				     const u64 replace_len,
2159 				     const u64 bytes_to_drop)
2160 {
2161 	struct btrfs_fs_info *fs_info = trans->fs_info;
2162 	struct btrfs_root *root = inode->root;
2163 	struct btrfs_file_extent_item *extent;
2164 	struct extent_buffer *leaf;
2165 	struct btrfs_key key;
2166 	int slot;
2167 	int ret;
2168 
2169 	if (replace_len == 0)
2170 		return 0;
2171 
2172 	if (extent_info->disk_offset == 0 &&
2173 	    btrfs_fs_incompat(fs_info, NO_HOLES)) {
2174 		btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
2175 		return 0;
2176 	}
2177 
2178 	key.objectid = btrfs_ino(inode);
2179 	key.type = BTRFS_EXTENT_DATA_KEY;
2180 	key.offset = extent_info->file_offset;
2181 	ret = btrfs_insert_empty_item(trans, root, path, &key,
2182 				      sizeof(struct btrfs_file_extent_item));
2183 	if (ret)
2184 		return ret;
2185 	leaf = path->nodes[0];
2186 	slot = path->slots[0];
2187 	write_extent_buffer(leaf, extent_info->extent_buf,
2188 			    btrfs_item_ptr_offset(leaf, slot),
2189 			    sizeof(struct btrfs_file_extent_item));
2190 	extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2191 	ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE);
2192 	btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset);
2193 	btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
2194 	if (extent_info->is_new_extent)
2195 		btrfs_set_file_extent_generation(leaf, extent, trans->transid);
2196 	btrfs_mark_buffer_dirty(trans, leaf);
2197 	btrfs_release_path(path);
2198 
2199 	ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset,
2200 						replace_len);
2201 	if (ret)
2202 		return ret;
2203 
2204 	/* If it's a hole, nothing more needs to be done. */
2205 	if (extent_info->disk_offset == 0) {
2206 		btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
2207 		return 0;
2208 	}
2209 
2210 	btrfs_update_inode_bytes(inode, replace_len, bytes_to_drop);
2211 
2212 	if (extent_info->is_new_extent && extent_info->insertions == 0) {
2213 		key.objectid = extent_info->disk_offset;
2214 		key.type = BTRFS_EXTENT_ITEM_KEY;
2215 		key.offset = extent_info->disk_len;
2216 		ret = btrfs_alloc_reserved_file_extent(trans, root,
2217 						       btrfs_ino(inode),
2218 						       extent_info->file_offset,
2219 						       extent_info->qgroup_reserved,
2220 						       &key);
2221 	} else {
2222 		struct btrfs_ref ref = {
2223 			.action = BTRFS_ADD_DELAYED_REF,
2224 			.bytenr = extent_info->disk_offset,
2225 			.num_bytes = extent_info->disk_len,
2226 			.owning_root = btrfs_root_id(root),
2227 			.ref_root = btrfs_root_id(root),
2228 		};
2229 		u64 ref_offset;
2230 
2231 		ref_offset = extent_info->file_offset - extent_info->data_offset;
2232 		btrfs_init_data_ref(&ref, btrfs_ino(inode), ref_offset, 0, false);
2233 		ret = btrfs_inc_extent_ref(trans, &ref);
2234 	}
2235 
2236 	extent_info->insertions++;
2237 
2238 	return ret;
2239 }
2240 
2241 /*
2242  * The respective range must have been previously locked, as well as the inode.
2243  * The end offset is inclusive (last byte of the range).
2244  * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing
2245  * the file range with an extent.
2246  * When not punching a hole, we don't want to end up in a state where we dropped
2247  * extents without inserting a new one, so we must abort the transaction to avoid
2248  * a corruption.
2249  */
2250 int btrfs_replace_file_extents(struct btrfs_inode *inode,
2251 			       struct btrfs_path *path, const u64 start,
2252 			       const u64 end,
2253 			       struct btrfs_replace_extent_info *extent_info,
2254 			       struct btrfs_trans_handle **trans_out)
2255 {
2256 	struct btrfs_drop_extents_args drop_args = { 0 };
2257 	struct btrfs_root *root = inode->root;
2258 	struct btrfs_fs_info *fs_info = root->fs_info;
2259 	u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1);
2260 	u64 ino_size = round_up(inode->vfs_inode.i_size, fs_info->sectorsize);
2261 	struct btrfs_trans_handle *trans = NULL;
2262 	struct btrfs_block_rsv *rsv;
2263 	unsigned int rsv_count;
2264 	u64 cur_offset;
2265 	u64 len = end - start;
2266 	int ret = 0;
2267 
2268 	if (end <= start)
2269 		return -EINVAL;
2270 
2271 	rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
2272 	if (!rsv) {
2273 		ret = -ENOMEM;
2274 		goto out;
2275 	}
2276 	rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1);
2277 	rsv->failfast = true;
2278 
2279 	/*
2280 	 * 1 - update the inode
2281 	 * 1 - removing the extents in the range
2282 	 * 1 - adding the hole extent if no_holes isn't set or if we are
2283 	 *     replacing the range with a new extent
2284 	 */
2285 	if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info)
2286 		rsv_count = 3;
2287 	else
2288 		rsv_count = 2;
2289 
2290 	trans = btrfs_start_transaction(root, rsv_count);
2291 	if (IS_ERR(trans)) {
2292 		ret = PTR_ERR(trans);
2293 		trans = NULL;
2294 		goto out_free;
2295 	}
2296 
2297 	ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
2298 				      min_size, false);
2299 	if (WARN_ON(ret))
2300 		goto out_trans;
2301 	trans->block_rsv = rsv;
2302 
2303 	cur_offset = start;
2304 	drop_args.path = path;
2305 	drop_args.end = end + 1;
2306 	drop_args.drop_cache = true;
2307 	while (cur_offset < end) {
2308 		drop_args.start = cur_offset;
2309 		ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2310 		/* If we are punching a hole decrement the inode's byte count */
2311 		if (!extent_info)
2312 			btrfs_update_inode_bytes(inode, 0,
2313 						 drop_args.bytes_found);
2314 		if (ret != -ENOSPC) {
2315 			/*
2316 			 * The only time we don't want to abort is if we are
2317 			 * attempting to clone a partial inline extent, in which
2318 			 * case we'll get EOPNOTSUPP.  However if we aren't
2319 			 * clone we need to abort no matter what, because if we
2320 			 * got EOPNOTSUPP via prealloc then we messed up and
2321 			 * need to abort.
2322 			 */
2323 			if (ret &&
2324 			    (ret != -EOPNOTSUPP ||
2325 			     (extent_info && extent_info->is_new_extent)))
2326 				btrfs_abort_transaction(trans, ret);
2327 			break;
2328 		}
2329 
2330 		trans->block_rsv = &fs_info->trans_block_rsv;
2331 
2332 		if (!extent_info && cur_offset < drop_args.drop_end &&
2333 		    cur_offset < ino_size) {
2334 			ret = fill_holes(trans, inode, path, cur_offset,
2335 					 drop_args.drop_end);
2336 			if (ret) {
2337 				/*
2338 				 * If we failed then we didn't insert our hole
2339 				 * entries for the area we dropped, so now the
2340 				 * fs is corrupted, so we must abort the
2341 				 * transaction.
2342 				 */
2343 				btrfs_abort_transaction(trans, ret);
2344 				break;
2345 			}
2346 		} else if (!extent_info && cur_offset < drop_args.drop_end) {
2347 			/*
2348 			 * We are past the i_size here, but since we didn't
2349 			 * insert holes we need to clear the mapped area so we
2350 			 * know to not set disk_i_size in this area until a new
2351 			 * file extent is inserted here.
2352 			 */
2353 			ret = btrfs_inode_clear_file_extent_range(inode,
2354 					cur_offset,
2355 					drop_args.drop_end - cur_offset);
2356 			if (ret) {
2357 				/*
2358 				 * We couldn't clear our area, so we could
2359 				 * presumably adjust up and corrupt the fs, so
2360 				 * we need to abort.
2361 				 */
2362 				btrfs_abort_transaction(trans, ret);
2363 				break;
2364 			}
2365 		}
2366 
2367 		if (extent_info &&
2368 		    drop_args.drop_end > extent_info->file_offset) {
2369 			u64 replace_len = drop_args.drop_end -
2370 					  extent_info->file_offset;
2371 
2372 			ret = btrfs_insert_replace_extent(trans, inode,	path,
2373 					extent_info, replace_len,
2374 					drop_args.bytes_found);
2375 			if (ret) {
2376 				btrfs_abort_transaction(trans, ret);
2377 				break;
2378 			}
2379 			extent_info->data_len -= replace_len;
2380 			extent_info->data_offset += replace_len;
2381 			extent_info->file_offset += replace_len;
2382 		}
2383 
2384 		/*
2385 		 * We are releasing our handle on the transaction, balance the
2386 		 * dirty pages of the btree inode and flush delayed items, and
2387 		 * then get a new transaction handle, which may now point to a
2388 		 * new transaction in case someone else may have committed the
2389 		 * transaction we used to replace/drop file extent items. So
2390 		 * bump the inode's iversion and update mtime and ctime except
2391 		 * if we are called from a dedupe context. This is because a
2392 		 * power failure/crash may happen after the transaction is
2393 		 * committed and before we finish replacing/dropping all the
2394 		 * file extent items we need.
2395 		 */
2396 		inode_inc_iversion(&inode->vfs_inode);
2397 
2398 		if (!extent_info || extent_info->update_times)
2399 			inode_set_mtime_to_ts(&inode->vfs_inode,
2400 					      inode_set_ctime_current(&inode->vfs_inode));
2401 
2402 		ret = btrfs_update_inode(trans, inode);
2403 		if (ret)
2404 			break;
2405 
2406 		btrfs_end_transaction(trans);
2407 		btrfs_btree_balance_dirty(fs_info);
2408 
2409 		trans = btrfs_start_transaction(root, rsv_count);
2410 		if (IS_ERR(trans)) {
2411 			ret = PTR_ERR(trans);
2412 			trans = NULL;
2413 			break;
2414 		}
2415 
2416 		ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
2417 					      rsv, min_size, false);
2418 		if (WARN_ON(ret))
2419 			break;
2420 		trans->block_rsv = rsv;
2421 
2422 		cur_offset = drop_args.drop_end;
2423 		len = end - cur_offset;
2424 		if (!extent_info && len) {
2425 			ret = find_first_non_hole(inode, &cur_offset, &len);
2426 			if (unlikely(ret < 0))
2427 				break;
2428 			if (ret && !len) {
2429 				ret = 0;
2430 				break;
2431 			}
2432 		}
2433 	}
2434 
2435 	/*
2436 	 * If we were cloning, force the next fsync to be a full one since we
2437 	 * we replaced (or just dropped in the case of cloning holes when
2438 	 * NO_HOLES is enabled) file extent items and did not setup new extent
2439 	 * maps for the replacement extents (or holes).
2440 	 */
2441 	if (extent_info && !extent_info->is_new_extent)
2442 		btrfs_set_inode_full_sync(inode);
2443 
2444 	if (ret)
2445 		goto out_trans;
2446 
2447 	trans->block_rsv = &fs_info->trans_block_rsv;
2448 	/*
2449 	 * If we are using the NO_HOLES feature we might have had already an
2450 	 * hole that overlaps a part of the region [lockstart, lockend] and
2451 	 * ends at (or beyond) lockend. Since we have no file extent items to
2452 	 * represent holes, drop_end can be less than lockend and so we must
2453 	 * make sure we have an extent map representing the existing hole (the
2454 	 * call to __btrfs_drop_extents() might have dropped the existing extent
2455 	 * map representing the existing hole), otherwise the fast fsync path
2456 	 * will not record the existence of the hole region
2457 	 * [existing_hole_start, lockend].
2458 	 */
2459 	if (drop_args.drop_end <= end)
2460 		drop_args.drop_end = end + 1;
2461 	/*
2462 	 * Don't insert file hole extent item if it's for a range beyond eof
2463 	 * (because it's useless) or if it represents a 0 bytes range (when
2464 	 * cur_offset == drop_end).
2465 	 */
2466 	if (!extent_info && cur_offset < ino_size &&
2467 	    cur_offset < drop_args.drop_end) {
2468 		ret = fill_holes(trans, inode, path, cur_offset,
2469 				 drop_args.drop_end);
2470 		if (ret) {
2471 			/* Same comment as above. */
2472 			btrfs_abort_transaction(trans, ret);
2473 			goto out_trans;
2474 		}
2475 	} else if (!extent_info && cur_offset < drop_args.drop_end) {
2476 		/* See the comment in the loop above for the reasoning here. */
2477 		ret = btrfs_inode_clear_file_extent_range(inode, cur_offset,
2478 					drop_args.drop_end - cur_offset);
2479 		if (ret) {
2480 			btrfs_abort_transaction(trans, ret);
2481 			goto out_trans;
2482 		}
2483 
2484 	}
2485 	if (extent_info) {
2486 		ret = btrfs_insert_replace_extent(trans, inode, path,
2487 				extent_info, extent_info->data_len,
2488 				drop_args.bytes_found);
2489 		if (ret) {
2490 			btrfs_abort_transaction(trans, ret);
2491 			goto out_trans;
2492 		}
2493 	}
2494 
2495 out_trans:
2496 	if (!trans)
2497 		goto out_free;
2498 
2499 	trans->block_rsv = &fs_info->trans_block_rsv;
2500 	if (ret)
2501 		btrfs_end_transaction(trans);
2502 	else
2503 		*trans_out = trans;
2504 out_free:
2505 	btrfs_free_block_rsv(fs_info, rsv);
2506 out:
2507 	return ret;
2508 }
2509 
2510 static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len)
2511 {
2512 	struct inode *inode = file_inode(file);
2513 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2514 	struct btrfs_root *root = BTRFS_I(inode)->root;
2515 	struct extent_state *cached_state = NULL;
2516 	struct btrfs_path *path;
2517 	struct btrfs_trans_handle *trans = NULL;
2518 	u64 lockstart;
2519 	u64 lockend;
2520 	u64 tail_start;
2521 	u64 tail_len;
2522 	u64 orig_start = offset;
2523 	int ret = 0;
2524 	bool same_block;
2525 	u64 ino_size;
2526 	bool truncated_block = false;
2527 	bool updated_inode = false;
2528 
2529 	btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2530 
2531 	ret = btrfs_wait_ordered_range(BTRFS_I(inode), offset, len);
2532 	if (ret)
2533 		goto out_only_mutex;
2534 
2535 	ino_size = round_up(inode->i_size, fs_info->sectorsize);
2536 	ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
2537 	if (ret < 0)
2538 		goto out_only_mutex;
2539 	if (ret && !len) {
2540 		/* Already in a large hole */
2541 		ret = 0;
2542 		goto out_only_mutex;
2543 	}
2544 
2545 	ret = file_modified(file);
2546 	if (ret)
2547 		goto out_only_mutex;
2548 
2549 	lockstart = round_up(offset, fs_info->sectorsize);
2550 	lockend = round_down(offset + len, fs_info->sectorsize) - 1;
2551 	same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
2552 		== (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
2553 	/*
2554 	 * We needn't truncate any block which is beyond the end of the file
2555 	 * because we are sure there is no data there.
2556 	 */
2557 	/*
2558 	 * Only do this if we are in the same block and we aren't doing the
2559 	 * entire block.
2560 	 */
2561 	if (same_block && len < fs_info->sectorsize) {
2562 		if (offset < ino_size) {
2563 			truncated_block = true;
2564 			ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
2565 						   0);
2566 		} else {
2567 			ret = 0;
2568 		}
2569 		goto out_only_mutex;
2570 	}
2571 
2572 	/* zero back part of the first block */
2573 	if (offset < ino_size) {
2574 		truncated_block = true;
2575 		ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
2576 		if (ret) {
2577 			btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2578 			return ret;
2579 		}
2580 	}
2581 
2582 	/* Check the aligned pages after the first unaligned page,
2583 	 * if offset != orig_start, which means the first unaligned page
2584 	 * including several following pages are already in holes,
2585 	 * the extra check can be skipped */
2586 	if (offset == orig_start) {
2587 		/* after truncate page, check hole again */
2588 		len = offset + len - lockstart;
2589 		offset = lockstart;
2590 		ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
2591 		if (ret < 0)
2592 			goto out_only_mutex;
2593 		if (ret && !len) {
2594 			ret = 0;
2595 			goto out_only_mutex;
2596 		}
2597 		lockstart = offset;
2598 	}
2599 
2600 	/* Check the tail unaligned part is in a hole */
2601 	tail_start = lockend + 1;
2602 	tail_len = offset + len - tail_start;
2603 	if (tail_len) {
2604 		ret = find_first_non_hole(BTRFS_I(inode), &tail_start, &tail_len);
2605 		if (unlikely(ret < 0))
2606 			goto out_only_mutex;
2607 		if (!ret) {
2608 			/* zero the front end of the last page */
2609 			if (tail_start + tail_len < ino_size) {
2610 				truncated_block = true;
2611 				ret = btrfs_truncate_block(BTRFS_I(inode),
2612 							tail_start + tail_len,
2613 							0, 1);
2614 				if (ret)
2615 					goto out_only_mutex;
2616 			}
2617 		}
2618 	}
2619 
2620 	if (lockend < lockstart) {
2621 		ret = 0;
2622 		goto out_only_mutex;
2623 	}
2624 
2625 	btrfs_punch_hole_lock_range(inode, lockstart, lockend, &cached_state);
2626 
2627 	path = btrfs_alloc_path();
2628 	if (!path) {
2629 		ret = -ENOMEM;
2630 		goto out;
2631 	}
2632 
2633 	ret = btrfs_replace_file_extents(BTRFS_I(inode), path, lockstart,
2634 					 lockend, NULL, &trans);
2635 	btrfs_free_path(path);
2636 	if (ret)
2637 		goto out;
2638 
2639 	ASSERT(trans != NULL);
2640 	inode_inc_iversion(inode);
2641 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2642 	ret = btrfs_update_inode(trans, BTRFS_I(inode));
2643 	updated_inode = true;
2644 	btrfs_end_transaction(trans);
2645 	btrfs_btree_balance_dirty(fs_info);
2646 out:
2647 	unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2648 		      &cached_state);
2649 out_only_mutex:
2650 	if (!updated_inode && truncated_block && !ret) {
2651 		/*
2652 		 * If we only end up zeroing part of a page, we still need to
2653 		 * update the inode item, so that all the time fields are
2654 		 * updated as well as the necessary btrfs inode in memory fields
2655 		 * for detecting, at fsync time, if the inode isn't yet in the
2656 		 * log tree or it's there but not up to date.
2657 		 */
2658 		struct timespec64 now = inode_set_ctime_current(inode);
2659 
2660 		inode_inc_iversion(inode);
2661 		inode_set_mtime_to_ts(inode, now);
2662 		trans = btrfs_start_transaction(root, 1);
2663 		if (IS_ERR(trans)) {
2664 			ret = PTR_ERR(trans);
2665 		} else {
2666 			int ret2;
2667 
2668 			ret = btrfs_update_inode(trans, BTRFS_I(inode));
2669 			ret2 = btrfs_end_transaction(trans);
2670 			if (!ret)
2671 				ret = ret2;
2672 		}
2673 	}
2674 	btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2675 	return ret;
2676 }
2677 
2678 /* Helper structure to record which range is already reserved */
2679 struct falloc_range {
2680 	struct list_head list;
2681 	u64 start;
2682 	u64 len;
2683 };
2684 
2685 /*
2686  * Helper function to add falloc range
2687  *
2688  * Caller should have locked the larger range of extent containing
2689  * [start, len)
2690  */
2691 static int add_falloc_range(struct list_head *head, u64 start, u64 len)
2692 {
2693 	struct falloc_range *range = NULL;
2694 
2695 	if (!list_empty(head)) {
2696 		/*
2697 		 * As fallocate iterates by bytenr order, we only need to check
2698 		 * the last range.
2699 		 */
2700 		range = list_last_entry(head, struct falloc_range, list);
2701 		if (range->start + range->len == start) {
2702 			range->len += len;
2703 			return 0;
2704 		}
2705 	}
2706 
2707 	range = kmalloc(sizeof(*range), GFP_KERNEL);
2708 	if (!range)
2709 		return -ENOMEM;
2710 	range->start = start;
2711 	range->len = len;
2712 	list_add_tail(&range->list, head);
2713 	return 0;
2714 }
2715 
2716 static int btrfs_fallocate_update_isize(struct inode *inode,
2717 					const u64 end,
2718 					const int mode)
2719 {
2720 	struct btrfs_trans_handle *trans;
2721 	struct btrfs_root *root = BTRFS_I(inode)->root;
2722 	int ret;
2723 	int ret2;
2724 
2725 	if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode))
2726 		return 0;
2727 
2728 	trans = btrfs_start_transaction(root, 1);
2729 	if (IS_ERR(trans))
2730 		return PTR_ERR(trans);
2731 
2732 	inode_set_ctime_current(inode);
2733 	i_size_write(inode, end);
2734 	btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
2735 	ret = btrfs_update_inode(trans, BTRFS_I(inode));
2736 	ret2 = btrfs_end_transaction(trans);
2737 
2738 	return ret ? ret : ret2;
2739 }
2740 
2741 enum {
2742 	RANGE_BOUNDARY_WRITTEN_EXTENT,
2743 	RANGE_BOUNDARY_PREALLOC_EXTENT,
2744 	RANGE_BOUNDARY_HOLE,
2745 };
2746 
2747 static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,
2748 						 u64 offset)
2749 {
2750 	const u64 sectorsize = inode->root->fs_info->sectorsize;
2751 	struct extent_map *em;
2752 	int ret;
2753 
2754 	offset = round_down(offset, sectorsize);
2755 	em = btrfs_get_extent(inode, NULL, offset, sectorsize);
2756 	if (IS_ERR(em))
2757 		return PTR_ERR(em);
2758 
2759 	if (em->disk_bytenr == EXTENT_MAP_HOLE)
2760 		ret = RANGE_BOUNDARY_HOLE;
2761 	else if (em->flags & EXTENT_FLAG_PREALLOC)
2762 		ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
2763 	else
2764 		ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
2765 
2766 	free_extent_map(em);
2767 	return ret;
2768 }
2769 
2770 static int btrfs_zero_range(struct inode *inode,
2771 			    loff_t offset,
2772 			    loff_t len,
2773 			    const int mode)
2774 {
2775 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2776 	struct extent_map *em;
2777 	struct extent_changeset *data_reserved = NULL;
2778 	int ret;
2779 	u64 alloc_hint = 0;
2780 	const u64 sectorsize = fs_info->sectorsize;
2781 	u64 alloc_start = round_down(offset, sectorsize);
2782 	u64 alloc_end = round_up(offset + len, sectorsize);
2783 	u64 bytes_to_reserve = 0;
2784 	bool space_reserved = false;
2785 
2786 	em = btrfs_get_extent(BTRFS_I(inode), NULL, alloc_start,
2787 			      alloc_end - alloc_start);
2788 	if (IS_ERR(em)) {
2789 		ret = PTR_ERR(em);
2790 		goto out;
2791 	}
2792 
2793 	/*
2794 	 * Avoid hole punching and extent allocation for some cases. More cases
2795 	 * could be considered, but these are unlikely common and we keep things
2796 	 * as simple as possible for now. Also, intentionally, if the target
2797 	 * range contains one or more prealloc extents together with regular
2798 	 * extents and holes, we drop all the existing extents and allocate a
2799 	 * new prealloc extent, so that we get a larger contiguous disk extent.
2800 	 */
2801 	if (em->start <= alloc_start && (em->flags & EXTENT_FLAG_PREALLOC)) {
2802 		const u64 em_end = em->start + em->len;
2803 
2804 		if (em_end >= offset + len) {
2805 			/*
2806 			 * The whole range is already a prealloc extent,
2807 			 * do nothing except updating the inode's i_size if
2808 			 * needed.
2809 			 */
2810 			free_extent_map(em);
2811 			ret = btrfs_fallocate_update_isize(inode, offset + len,
2812 							   mode);
2813 			goto out;
2814 		}
2815 		/*
2816 		 * Part of the range is already a prealloc extent, so operate
2817 		 * only on the remaining part of the range.
2818 		 */
2819 		alloc_start = em_end;
2820 		ASSERT(IS_ALIGNED(alloc_start, sectorsize));
2821 		len = offset + len - alloc_start;
2822 		offset = alloc_start;
2823 		alloc_hint = extent_map_block_start(em) + em->len;
2824 	}
2825 	free_extent_map(em);
2826 
2827 	if (BTRFS_BYTES_TO_BLKS(fs_info, offset) ==
2828 	    BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) {
2829 		em = btrfs_get_extent(BTRFS_I(inode), NULL, alloc_start, sectorsize);
2830 		if (IS_ERR(em)) {
2831 			ret = PTR_ERR(em);
2832 			goto out;
2833 		}
2834 
2835 		if (em->flags & EXTENT_FLAG_PREALLOC) {
2836 			free_extent_map(em);
2837 			ret = btrfs_fallocate_update_isize(inode, offset + len,
2838 							   mode);
2839 			goto out;
2840 		}
2841 		if (len < sectorsize && em->disk_bytenr != EXTENT_MAP_HOLE) {
2842 			free_extent_map(em);
2843 			ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
2844 						   0);
2845 			if (!ret)
2846 				ret = btrfs_fallocate_update_isize(inode,
2847 								   offset + len,
2848 								   mode);
2849 			return ret;
2850 		}
2851 		free_extent_map(em);
2852 		alloc_start = round_down(offset, sectorsize);
2853 		alloc_end = alloc_start + sectorsize;
2854 		goto reserve_space;
2855 	}
2856 
2857 	alloc_start = round_up(offset, sectorsize);
2858 	alloc_end = round_down(offset + len, sectorsize);
2859 
2860 	/*
2861 	 * For unaligned ranges, check the pages at the boundaries, they might
2862 	 * map to an extent, in which case we need to partially zero them, or
2863 	 * they might map to a hole, in which case we need our allocation range
2864 	 * to cover them.
2865 	 */
2866 	if (!IS_ALIGNED(offset, sectorsize)) {
2867 		ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
2868 							    offset);
2869 		if (ret < 0)
2870 			goto out;
2871 		if (ret == RANGE_BOUNDARY_HOLE) {
2872 			alloc_start = round_down(offset, sectorsize);
2873 			ret = 0;
2874 		} else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
2875 			ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
2876 			if (ret)
2877 				goto out;
2878 		} else {
2879 			ret = 0;
2880 		}
2881 	}
2882 
2883 	if (!IS_ALIGNED(offset + len, sectorsize)) {
2884 		ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
2885 							    offset + len);
2886 		if (ret < 0)
2887 			goto out;
2888 		if (ret == RANGE_BOUNDARY_HOLE) {
2889 			alloc_end = round_up(offset + len, sectorsize);
2890 			ret = 0;
2891 		} else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
2892 			ret = btrfs_truncate_block(BTRFS_I(inode), offset + len,
2893 						   0, 1);
2894 			if (ret)
2895 				goto out;
2896 		} else {
2897 			ret = 0;
2898 		}
2899 	}
2900 
2901 reserve_space:
2902 	if (alloc_start < alloc_end) {
2903 		struct extent_state *cached_state = NULL;
2904 		const u64 lockstart = alloc_start;
2905 		const u64 lockend = alloc_end - 1;
2906 
2907 		bytes_to_reserve = alloc_end - alloc_start;
2908 		ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
2909 						      bytes_to_reserve);
2910 		if (ret < 0)
2911 			goto out;
2912 		space_reserved = true;
2913 		btrfs_punch_hole_lock_range(inode, lockstart, lockend,
2914 					    &cached_state);
2915 		ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved,
2916 						alloc_start, bytes_to_reserve);
2917 		if (ret) {
2918 			unlock_extent(&BTRFS_I(inode)->io_tree, lockstart,
2919 				      lockend, &cached_state);
2920 			goto out;
2921 		}
2922 		ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
2923 						alloc_end - alloc_start,
2924 						fs_info->sectorsize,
2925 						offset + len, &alloc_hint);
2926 		unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2927 			      &cached_state);
2928 		/* btrfs_prealloc_file_range releases reserved space on error */
2929 		if (ret) {
2930 			space_reserved = false;
2931 			goto out;
2932 		}
2933 	}
2934 	ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
2935  out:
2936 	if (ret && space_reserved)
2937 		btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
2938 					       alloc_start, bytes_to_reserve);
2939 	extent_changeset_free(data_reserved);
2940 
2941 	return ret;
2942 }
2943 
2944 static long btrfs_fallocate(struct file *file, int mode,
2945 			    loff_t offset, loff_t len)
2946 {
2947 	struct inode *inode = file_inode(file);
2948 	struct extent_state *cached_state = NULL;
2949 	struct extent_changeset *data_reserved = NULL;
2950 	struct falloc_range *range;
2951 	struct falloc_range *tmp;
2952 	LIST_HEAD(reserve_list);
2953 	u64 cur_offset;
2954 	u64 last_byte;
2955 	u64 alloc_start;
2956 	u64 alloc_end;
2957 	u64 alloc_hint = 0;
2958 	u64 locked_end;
2959 	u64 actual_end = 0;
2960 	u64 data_space_needed = 0;
2961 	u64 data_space_reserved = 0;
2962 	u64 qgroup_reserved = 0;
2963 	struct extent_map *em;
2964 	int blocksize = BTRFS_I(inode)->root->fs_info->sectorsize;
2965 	int ret;
2966 
2967 	/* Do not allow fallocate in ZONED mode */
2968 	if (btrfs_is_zoned(inode_to_fs_info(inode)))
2969 		return -EOPNOTSUPP;
2970 
2971 	alloc_start = round_down(offset, blocksize);
2972 	alloc_end = round_up(offset + len, blocksize);
2973 	cur_offset = alloc_start;
2974 
2975 	/* Make sure we aren't being give some crap mode */
2976 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
2977 		     FALLOC_FL_ZERO_RANGE))
2978 		return -EOPNOTSUPP;
2979 
2980 	if (mode & FALLOC_FL_PUNCH_HOLE)
2981 		return btrfs_punch_hole(file, offset, len);
2982 
2983 	btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2984 
2985 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
2986 		ret = inode_newsize_ok(inode, offset + len);
2987 		if (ret)
2988 			goto out;
2989 	}
2990 
2991 	ret = file_modified(file);
2992 	if (ret)
2993 		goto out;
2994 
2995 	/*
2996 	 * TODO: Move these two operations after we have checked
2997 	 * accurate reserved space, or fallocate can still fail but
2998 	 * with page truncated or size expanded.
2999 	 *
3000 	 * But that's a minor problem and won't do much harm BTW.
3001 	 */
3002 	if (alloc_start > inode->i_size) {
3003 		ret = btrfs_cont_expand(BTRFS_I(inode), i_size_read(inode),
3004 					alloc_start);
3005 		if (ret)
3006 			goto out;
3007 	} else if (offset + len > inode->i_size) {
3008 		/*
3009 		 * If we are fallocating from the end of the file onward we
3010 		 * need to zero out the end of the block if i_size lands in the
3011 		 * middle of a block.
3012 		 */
3013 		ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
3014 		if (ret)
3015 			goto out;
3016 	}
3017 
3018 	/*
3019 	 * We have locked the inode at the VFS level (in exclusive mode) and we
3020 	 * have locked the i_mmap_lock lock (in exclusive mode). Now before
3021 	 * locking the file range, flush all dealloc in the range and wait for
3022 	 * all ordered extents in the range to complete. After this we can lock
3023 	 * the file range and, due to the previous locking we did, we know there
3024 	 * can't be more delalloc or ordered extents in the range.
3025 	 */
3026 	ret = btrfs_wait_ordered_range(BTRFS_I(inode), alloc_start,
3027 				       alloc_end - alloc_start);
3028 	if (ret)
3029 		goto out;
3030 
3031 	if (mode & FALLOC_FL_ZERO_RANGE) {
3032 		ret = btrfs_zero_range(inode, offset, len, mode);
3033 		btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3034 		return ret;
3035 	}
3036 
3037 	locked_end = alloc_end - 1;
3038 	lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3039 		    &cached_state);
3040 
3041 	btrfs_assert_inode_range_clean(BTRFS_I(inode), alloc_start, locked_end);
3042 
3043 	/* First, check if we exceed the qgroup limit */
3044 	while (cur_offset < alloc_end) {
3045 		em = btrfs_get_extent(BTRFS_I(inode), NULL, cur_offset,
3046 				      alloc_end - cur_offset);
3047 		if (IS_ERR(em)) {
3048 			ret = PTR_ERR(em);
3049 			break;
3050 		}
3051 		last_byte = min(extent_map_end(em), alloc_end);
3052 		actual_end = min_t(u64, extent_map_end(em), offset + len);
3053 		last_byte = ALIGN(last_byte, blocksize);
3054 		if (em->disk_bytenr == EXTENT_MAP_HOLE ||
3055 		    (cur_offset >= inode->i_size &&
3056 		     !(em->flags & EXTENT_FLAG_PREALLOC))) {
3057 			const u64 range_len = last_byte - cur_offset;
3058 
3059 			ret = add_falloc_range(&reserve_list, cur_offset, range_len);
3060 			if (ret < 0) {
3061 				free_extent_map(em);
3062 				break;
3063 			}
3064 			ret = btrfs_qgroup_reserve_data(BTRFS_I(inode),
3065 					&data_reserved, cur_offset, range_len);
3066 			if (ret < 0) {
3067 				free_extent_map(em);
3068 				break;
3069 			}
3070 			qgroup_reserved += range_len;
3071 			data_space_needed += range_len;
3072 		}
3073 		free_extent_map(em);
3074 		cur_offset = last_byte;
3075 	}
3076 
3077 	if (!ret && data_space_needed > 0) {
3078 		/*
3079 		 * We are safe to reserve space here as we can't have delalloc
3080 		 * in the range, see above.
3081 		 */
3082 		ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3083 						      data_space_needed);
3084 		if (!ret)
3085 			data_space_reserved = data_space_needed;
3086 	}
3087 
3088 	/*
3089 	 * If ret is still 0, means we're OK to fallocate.
3090 	 * Or just cleanup the list and exit.
3091 	 */
3092 	list_for_each_entry_safe(range, tmp, &reserve_list, list) {
3093 		if (!ret) {
3094 			ret = btrfs_prealloc_file_range(inode, mode,
3095 					range->start,
3096 					range->len, blocksize,
3097 					offset + len, &alloc_hint);
3098 			/*
3099 			 * btrfs_prealloc_file_range() releases space even
3100 			 * if it returns an error.
3101 			 */
3102 			data_space_reserved -= range->len;
3103 			qgroup_reserved -= range->len;
3104 		} else if (data_space_reserved > 0) {
3105 			btrfs_free_reserved_data_space(BTRFS_I(inode),
3106 					       data_reserved, range->start,
3107 					       range->len);
3108 			data_space_reserved -= range->len;
3109 			qgroup_reserved -= range->len;
3110 		} else if (qgroup_reserved > 0) {
3111 			btrfs_qgroup_free_data(BTRFS_I(inode), data_reserved,
3112 					       range->start, range->len, NULL);
3113 			qgroup_reserved -= range->len;
3114 		}
3115 		list_del(&range->list);
3116 		kfree(range);
3117 	}
3118 	if (ret < 0)
3119 		goto out_unlock;
3120 
3121 	/*
3122 	 * We didn't need to allocate any more space, but we still extended the
3123 	 * size of the file so we need to update i_size and the inode item.
3124 	 */
3125 	ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
3126 out_unlock:
3127 	unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3128 		      &cached_state);
3129 out:
3130 	btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3131 	extent_changeset_free(data_reserved);
3132 	return ret;
3133 }
3134 
3135 /*
3136  * Helper for btrfs_find_delalloc_in_range(). Find a subrange in a given range
3137  * that has unflushed and/or flushing delalloc. There might be other adjacent
3138  * subranges after the one it found, so btrfs_find_delalloc_in_range() keeps
3139  * looping while it gets adjacent subranges, and merging them together.
3140  */
3141 static bool find_delalloc_subrange(struct btrfs_inode *inode, u64 start, u64 end,
3142 				   struct extent_state **cached_state,
3143 				   bool *search_io_tree,
3144 				   u64 *delalloc_start_ret, u64 *delalloc_end_ret)
3145 {
3146 	u64 len = end + 1 - start;
3147 	u64 delalloc_len = 0;
3148 	struct btrfs_ordered_extent *oe;
3149 	u64 oe_start;
3150 	u64 oe_end;
3151 
3152 	/*
3153 	 * Search the io tree first for EXTENT_DELALLOC. If we find any, it
3154 	 * means we have delalloc (dirty pages) for which writeback has not
3155 	 * started yet.
3156 	 */
3157 	if (*search_io_tree) {
3158 		spin_lock(&inode->lock);
3159 		if (inode->delalloc_bytes > 0) {
3160 			spin_unlock(&inode->lock);
3161 			*delalloc_start_ret = start;
3162 			delalloc_len = count_range_bits(&inode->io_tree,
3163 							delalloc_start_ret, end,
3164 							len, EXTENT_DELALLOC, 1,
3165 							cached_state);
3166 		} else {
3167 			spin_unlock(&inode->lock);
3168 		}
3169 	}
3170 
3171 	if (delalloc_len > 0) {
3172 		/*
3173 		 * If delalloc was found then *delalloc_start_ret has a sector size
3174 		 * aligned value (rounded down).
3175 		 */
3176 		*delalloc_end_ret = *delalloc_start_ret + delalloc_len - 1;
3177 
3178 		if (*delalloc_start_ret == start) {
3179 			/* Delalloc for the whole range, nothing more to do. */
3180 			if (*delalloc_end_ret == end)
3181 				return true;
3182 			/* Else trim our search range for ordered extents. */
3183 			start = *delalloc_end_ret + 1;
3184 			len = end + 1 - start;
3185 		}
3186 	} else {
3187 		/* No delalloc, future calls don't need to search again. */
3188 		*search_io_tree = false;
3189 	}
3190 
3191 	/*
3192 	 * Now also check if there's any ordered extent in the range.
3193 	 * We do this because:
3194 	 *
3195 	 * 1) When delalloc is flushed, the file range is locked, we clear the
3196 	 *    EXTENT_DELALLOC bit from the io tree and create an extent map and
3197 	 *    an ordered extent for the write. So we might just have been called
3198 	 *    after delalloc is flushed and before the ordered extent completes
3199 	 *    and inserts the new file extent item in the subvolume's btree;
3200 	 *
3201 	 * 2) We may have an ordered extent created by flushing delalloc for a
3202 	 *    subrange that starts before the subrange we found marked with
3203 	 *    EXTENT_DELALLOC in the io tree.
3204 	 *
3205 	 * We could also use the extent map tree to find such delalloc that is
3206 	 * being flushed, but using the ordered extents tree is more efficient
3207 	 * because it's usually much smaller as ordered extents are removed from
3208 	 * the tree once they complete. With the extent maps, we mau have them
3209 	 * in the extent map tree for a very long time, and they were either
3210 	 * created by previous writes or loaded by read operations.
3211 	 */
3212 	oe = btrfs_lookup_first_ordered_range(inode, start, len);
3213 	if (!oe)
3214 		return (delalloc_len > 0);
3215 
3216 	/* The ordered extent may span beyond our search range. */
3217 	oe_start = max(oe->file_offset, start);
3218 	oe_end = min(oe->file_offset + oe->num_bytes - 1, end);
3219 
3220 	btrfs_put_ordered_extent(oe);
3221 
3222 	/* Don't have unflushed delalloc, return the ordered extent range. */
3223 	if (delalloc_len == 0) {
3224 		*delalloc_start_ret = oe_start;
3225 		*delalloc_end_ret = oe_end;
3226 		return true;
3227 	}
3228 
3229 	/*
3230 	 * We have both unflushed delalloc (io_tree) and an ordered extent.
3231 	 * If the ranges are adjacent returned a combined range, otherwise
3232 	 * return the leftmost range.
3233 	 */
3234 	if (oe_start < *delalloc_start_ret) {
3235 		if (oe_end < *delalloc_start_ret)
3236 			*delalloc_end_ret = oe_end;
3237 		*delalloc_start_ret = oe_start;
3238 	} else if (*delalloc_end_ret + 1 == oe_start) {
3239 		*delalloc_end_ret = oe_end;
3240 	}
3241 
3242 	return true;
3243 }
3244 
3245 /*
3246  * Check if there's delalloc in a given range.
3247  *
3248  * @inode:               The inode.
3249  * @start:               The start offset of the range. It does not need to be
3250  *                       sector size aligned.
3251  * @end:                 The end offset (inclusive value) of the search range.
3252  *                       It does not need to be sector size aligned.
3253  * @cached_state:        Extent state record used for speeding up delalloc
3254  *                       searches in the inode's io_tree. Can be NULL.
3255  * @delalloc_start_ret:  Output argument, set to the start offset of the
3256  *                       subrange found with delalloc (may not be sector size
3257  *                       aligned).
3258  * @delalloc_end_ret:    Output argument, set to he end offset (inclusive value)
3259  *                       of the subrange found with delalloc.
3260  *
3261  * Returns true if a subrange with delalloc is found within the given range, and
3262  * if so it sets @delalloc_start_ret and @delalloc_end_ret with the start and
3263  * end offsets of the subrange.
3264  */
3265 bool btrfs_find_delalloc_in_range(struct btrfs_inode *inode, u64 start, u64 end,
3266 				  struct extent_state **cached_state,
3267 				  u64 *delalloc_start_ret, u64 *delalloc_end_ret)
3268 {
3269 	u64 cur_offset = round_down(start, inode->root->fs_info->sectorsize);
3270 	u64 prev_delalloc_end = 0;
3271 	bool search_io_tree = true;
3272 	bool ret = false;
3273 
3274 	while (cur_offset <= end) {
3275 		u64 delalloc_start;
3276 		u64 delalloc_end;
3277 		bool delalloc;
3278 
3279 		delalloc = find_delalloc_subrange(inode, cur_offset, end,
3280 						  cached_state, &search_io_tree,
3281 						  &delalloc_start,
3282 						  &delalloc_end);
3283 		if (!delalloc)
3284 			break;
3285 
3286 		if (prev_delalloc_end == 0) {
3287 			/* First subrange found. */
3288 			*delalloc_start_ret = max(delalloc_start, start);
3289 			*delalloc_end_ret = delalloc_end;
3290 			ret = true;
3291 		} else if (delalloc_start == prev_delalloc_end + 1) {
3292 			/* Subrange adjacent to the previous one, merge them. */
3293 			*delalloc_end_ret = delalloc_end;
3294 		} else {
3295 			/* Subrange not adjacent to the previous one, exit. */
3296 			break;
3297 		}
3298 
3299 		prev_delalloc_end = delalloc_end;
3300 		cur_offset = delalloc_end + 1;
3301 		cond_resched();
3302 	}
3303 
3304 	return ret;
3305 }
3306 
3307 /*
3308  * Check if there's a hole or delalloc range in a range representing a hole (or
3309  * prealloc extent) found in the inode's subvolume btree.
3310  *
3311  * @inode:      The inode.
3312  * @whence:     Seek mode (SEEK_DATA or SEEK_HOLE).
3313  * @start:      Start offset of the hole region. It does not need to be sector
3314  *              size aligned.
3315  * @end:        End offset (inclusive value) of the hole region. It does not
3316  *              need to be sector size aligned.
3317  * @start_ret:  Return parameter, used to set the start of the subrange in the
3318  *              hole that matches the search criteria (seek mode), if such
3319  *              subrange is found (return value of the function is true).
3320  *              The value returned here may not be sector size aligned.
3321  *
3322  * Returns true if a subrange matching the given seek mode is found, and if one
3323  * is found, it updates @start_ret with the start of the subrange.
3324  */
3325 static bool find_desired_extent_in_hole(struct btrfs_inode *inode, int whence,
3326 					struct extent_state **cached_state,
3327 					u64 start, u64 end, u64 *start_ret)
3328 {
3329 	u64 delalloc_start;
3330 	u64 delalloc_end;
3331 	bool delalloc;
3332 
3333 	delalloc = btrfs_find_delalloc_in_range(inode, start, end, cached_state,
3334 						&delalloc_start, &delalloc_end);
3335 	if (delalloc && whence == SEEK_DATA) {
3336 		*start_ret = delalloc_start;
3337 		return true;
3338 	}
3339 
3340 	if (delalloc && whence == SEEK_HOLE) {
3341 		/*
3342 		 * We found delalloc but it starts after out start offset. So we
3343 		 * have a hole between our start offset and the delalloc start.
3344 		 */
3345 		if (start < delalloc_start) {
3346 			*start_ret = start;
3347 			return true;
3348 		}
3349 		/*
3350 		 * Delalloc range starts at our start offset.
3351 		 * If the delalloc range's length is smaller than our range,
3352 		 * then it means we have a hole that starts where the delalloc
3353 		 * subrange ends.
3354 		 */
3355 		if (delalloc_end < end) {
3356 			*start_ret = delalloc_end + 1;
3357 			return true;
3358 		}
3359 
3360 		/* There's delalloc for the whole range. */
3361 		return false;
3362 	}
3363 
3364 	if (!delalloc && whence == SEEK_HOLE) {
3365 		*start_ret = start;
3366 		return true;
3367 	}
3368 
3369 	/*
3370 	 * No delalloc in the range and we are seeking for data. The caller has
3371 	 * to iterate to the next extent item in the subvolume btree.
3372 	 */
3373 	return false;
3374 }
3375 
3376 static loff_t find_desired_extent(struct file *file, loff_t offset, int whence)
3377 {
3378 	struct btrfs_inode *inode = BTRFS_I(file->f_mapping->host);
3379 	struct btrfs_file_private *private;
3380 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
3381 	struct extent_state *cached_state = NULL;
3382 	struct extent_state **delalloc_cached_state;
3383 	const loff_t i_size = i_size_read(&inode->vfs_inode);
3384 	const u64 ino = btrfs_ino(inode);
3385 	struct btrfs_root *root = inode->root;
3386 	struct btrfs_path *path;
3387 	struct btrfs_key key;
3388 	u64 last_extent_end;
3389 	u64 lockstart;
3390 	u64 lockend;
3391 	u64 start;
3392 	int ret;
3393 	bool found = false;
3394 
3395 	if (i_size == 0 || offset >= i_size)
3396 		return -ENXIO;
3397 
3398 	/*
3399 	 * Quick path. If the inode has no prealloc extents and its number of
3400 	 * bytes used matches its i_size, then it can not have holes.
3401 	 */
3402 	if (whence == SEEK_HOLE &&
3403 	    !(inode->flags & BTRFS_INODE_PREALLOC) &&
3404 	    inode_get_bytes(&inode->vfs_inode) == i_size)
3405 		return i_size;
3406 
3407 	spin_lock(&inode->lock);
3408 	private = file->private_data;
3409 	spin_unlock(&inode->lock);
3410 
3411 	if (private && private->owner_task != current) {
3412 		/*
3413 		 * Not allocated by us, don't use it as its cached state is used
3414 		 * by the task that allocated it and we don't want neither to
3415 		 * mess with it nor get incorrect results because it reflects an
3416 		 * invalid state for the current task.
3417 		 */
3418 		private = NULL;
3419 	} else if (!private) {
3420 		private = kzalloc(sizeof(*private), GFP_KERNEL);
3421 		/*
3422 		 * No worries if memory allocation failed.
3423 		 * The private structure is used only for speeding up multiple
3424 		 * lseek SEEK_HOLE/DATA calls to a file when there's delalloc,
3425 		 * so everything will still be correct.
3426 		 */
3427 		if (private) {
3428 			bool free = false;
3429 
3430 			private->owner_task = current;
3431 
3432 			spin_lock(&inode->lock);
3433 			if (file->private_data)
3434 				free = true;
3435 			else
3436 				file->private_data = private;
3437 			spin_unlock(&inode->lock);
3438 
3439 			if (free) {
3440 				kfree(private);
3441 				private = NULL;
3442 			}
3443 		}
3444 	}
3445 
3446 	if (private)
3447 		delalloc_cached_state = &private->llseek_cached_state;
3448 	else
3449 		delalloc_cached_state = NULL;
3450 
3451 	/*
3452 	 * offset can be negative, in this case we start finding DATA/HOLE from
3453 	 * the very start of the file.
3454 	 */
3455 	start = max_t(loff_t, 0, offset);
3456 
3457 	lockstart = round_down(start, fs_info->sectorsize);
3458 	lockend = round_up(i_size, fs_info->sectorsize);
3459 	if (lockend <= lockstart)
3460 		lockend = lockstart + fs_info->sectorsize;
3461 	lockend--;
3462 
3463 	path = btrfs_alloc_path();
3464 	if (!path)
3465 		return -ENOMEM;
3466 	path->reada = READA_FORWARD;
3467 
3468 	key.objectid = ino;
3469 	key.type = BTRFS_EXTENT_DATA_KEY;
3470 	key.offset = start;
3471 
3472 	last_extent_end = lockstart;
3473 
3474 	lock_extent(&inode->io_tree, lockstart, lockend, &cached_state);
3475 
3476 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3477 	if (ret < 0) {
3478 		goto out;
3479 	} else if (ret > 0 && path->slots[0] > 0) {
3480 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
3481 		if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY)
3482 			path->slots[0]--;
3483 	}
3484 
3485 	while (start < i_size) {
3486 		struct extent_buffer *leaf = path->nodes[0];
3487 		struct btrfs_file_extent_item *extent;
3488 		u64 extent_end;
3489 		u8 type;
3490 
3491 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3492 			ret = btrfs_next_leaf(root, path);
3493 			if (ret < 0)
3494 				goto out;
3495 			else if (ret > 0)
3496 				break;
3497 
3498 			leaf = path->nodes[0];
3499 		}
3500 
3501 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3502 		if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
3503 			break;
3504 
3505 		extent_end = btrfs_file_extent_end(path);
3506 
3507 		/*
3508 		 * In the first iteration we may have a slot that points to an
3509 		 * extent that ends before our start offset, so skip it.
3510 		 */
3511 		if (extent_end <= start) {
3512 			path->slots[0]++;
3513 			continue;
3514 		}
3515 
3516 		/* We have an implicit hole, NO_HOLES feature is likely set. */
3517 		if (last_extent_end < key.offset) {
3518 			u64 search_start = last_extent_end;
3519 			u64 found_start;
3520 
3521 			/*
3522 			 * First iteration, @start matches @offset and it's
3523 			 * within the hole.
3524 			 */
3525 			if (start == offset)
3526 				search_start = offset;
3527 
3528 			found = find_desired_extent_in_hole(inode, whence,
3529 							    delalloc_cached_state,
3530 							    search_start,
3531 							    key.offset - 1,
3532 							    &found_start);
3533 			if (found) {
3534 				start = found_start;
3535 				break;
3536 			}
3537 			/*
3538 			 * Didn't find data or a hole (due to delalloc) in the
3539 			 * implicit hole range, so need to analyze the extent.
3540 			 */
3541 		}
3542 
3543 		extent = btrfs_item_ptr(leaf, path->slots[0],
3544 					struct btrfs_file_extent_item);
3545 		type = btrfs_file_extent_type(leaf, extent);
3546 
3547 		/*
3548 		 * Can't access the extent's disk_bytenr field if this is an
3549 		 * inline extent, since at that offset, it's where the extent
3550 		 * data starts.
3551 		 */
3552 		if (type == BTRFS_FILE_EXTENT_PREALLOC ||
3553 		    (type == BTRFS_FILE_EXTENT_REG &&
3554 		     btrfs_file_extent_disk_bytenr(leaf, extent) == 0)) {
3555 			/*
3556 			 * Explicit hole or prealloc extent, search for delalloc.
3557 			 * A prealloc extent is treated like a hole.
3558 			 */
3559 			u64 search_start = key.offset;
3560 			u64 found_start;
3561 
3562 			/*
3563 			 * First iteration, @start matches @offset and it's
3564 			 * within the hole.
3565 			 */
3566 			if (start == offset)
3567 				search_start = offset;
3568 
3569 			found = find_desired_extent_in_hole(inode, whence,
3570 							    delalloc_cached_state,
3571 							    search_start,
3572 							    extent_end - 1,
3573 							    &found_start);
3574 			if (found) {
3575 				start = found_start;
3576 				break;
3577 			}
3578 			/*
3579 			 * Didn't find data or a hole (due to delalloc) in the
3580 			 * implicit hole range, so need to analyze the next
3581 			 * extent item.
3582 			 */
3583 		} else {
3584 			/*
3585 			 * Found a regular or inline extent.
3586 			 * If we are seeking for data, adjust the start offset
3587 			 * and stop, we're done.
3588 			 */
3589 			if (whence == SEEK_DATA) {
3590 				start = max_t(u64, key.offset, offset);
3591 				found = true;
3592 				break;
3593 			}
3594 			/*
3595 			 * Else, we are seeking for a hole, check the next file
3596 			 * extent item.
3597 			 */
3598 		}
3599 
3600 		start = extent_end;
3601 		last_extent_end = extent_end;
3602 		path->slots[0]++;
3603 		if (fatal_signal_pending(current)) {
3604 			ret = -EINTR;
3605 			goto out;
3606 		}
3607 		cond_resched();
3608 	}
3609 
3610 	/* We have an implicit hole from the last extent found up to i_size. */
3611 	if (!found && start < i_size) {
3612 		found = find_desired_extent_in_hole(inode, whence,
3613 						    delalloc_cached_state, start,
3614 						    i_size - 1, &start);
3615 		if (!found)
3616 			start = i_size;
3617 	}
3618 
3619 out:
3620 	unlock_extent(&inode->io_tree, lockstart, lockend, &cached_state);
3621 	btrfs_free_path(path);
3622 
3623 	if (ret < 0)
3624 		return ret;
3625 
3626 	if (whence == SEEK_DATA && start >= i_size)
3627 		return -ENXIO;
3628 
3629 	return min_t(loff_t, start, i_size);
3630 }
3631 
3632 static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
3633 {
3634 	struct inode *inode = file->f_mapping->host;
3635 
3636 	switch (whence) {
3637 	default:
3638 		return generic_file_llseek(file, offset, whence);
3639 	case SEEK_DATA:
3640 	case SEEK_HOLE:
3641 		btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_SHARED);
3642 		offset = find_desired_extent(file, offset, whence);
3643 		btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_SHARED);
3644 		break;
3645 	}
3646 
3647 	if (offset < 0)
3648 		return offset;
3649 
3650 	return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3651 }
3652 
3653 static int btrfs_file_open(struct inode *inode, struct file *filp)
3654 {
3655 	int ret;
3656 
3657 	filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
3658 
3659 	ret = fsverity_file_open(inode, filp);
3660 	if (ret)
3661 		return ret;
3662 	return generic_file_open(inode, filp);
3663 }
3664 
3665 static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
3666 {
3667 	ssize_t ret = 0;
3668 
3669 	if (iocb->ki_flags & IOCB_DIRECT) {
3670 		ret = btrfs_direct_read(iocb, to);
3671 		if (ret < 0 || !iov_iter_count(to) ||
3672 		    iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp)))
3673 			return ret;
3674 	}
3675 
3676 	return filemap_read(iocb, to, ret);
3677 }
3678 
3679 const struct file_operations btrfs_file_operations = {
3680 	.llseek		= btrfs_file_llseek,
3681 	.read_iter      = btrfs_file_read_iter,
3682 	.splice_read	= filemap_splice_read,
3683 	.write_iter	= btrfs_file_write_iter,
3684 	.splice_write	= iter_file_splice_write,
3685 	.mmap		= btrfs_file_mmap,
3686 	.open		= btrfs_file_open,
3687 	.release	= btrfs_release_file,
3688 	.get_unmapped_area = thp_get_unmapped_area,
3689 	.fsync		= btrfs_sync_file,
3690 	.fallocate	= btrfs_fallocate,
3691 	.unlocked_ioctl	= btrfs_ioctl,
3692 #ifdef CONFIG_COMPAT
3693 	.compat_ioctl	= btrfs_compat_ioctl,
3694 #endif
3695 	.remap_file_range = btrfs_remap_file_range,
3696 	.uring_cmd	= btrfs_uring_cmd,
3697 	.fop_flags	= FOP_BUFFER_RASYNC | FOP_BUFFER_WASYNC,
3698 };
3699 
3700 int btrfs_fdatawrite_range(struct btrfs_inode *inode, loff_t start, loff_t end)
3701 {
3702 	struct address_space *mapping = inode->vfs_inode.i_mapping;
3703 	int ret;
3704 
3705 	/*
3706 	 * So with compression we will find and lock a dirty page and clear the
3707 	 * first one as dirty, setup an async extent, and immediately return
3708 	 * with the entire range locked but with nobody actually marked with
3709 	 * writeback.  So we can't just filemap_write_and_wait_range() and
3710 	 * expect it to work since it will just kick off a thread to do the
3711 	 * actual work.  So we need to call filemap_fdatawrite_range _again_
3712 	 * since it will wait on the page lock, which won't be unlocked until
3713 	 * after the pages have been marked as writeback and so we're good to go
3714 	 * from there.  We have to do this otherwise we'll miss the ordered
3715 	 * extents and that results in badness.  Please Josef, do not think you
3716 	 * know better and pull this out at some point in the future, it is
3717 	 * right and you are wrong.
3718 	 */
3719 	ret = filemap_fdatawrite_range(mapping, start, end);
3720 	if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags))
3721 		ret = filemap_fdatawrite_range(mapping, start, end);
3722 
3723 	return ret;
3724 }
3725