xref: /linux/fs/f2fs/file.c (revision 92fcf151766d7406a36f2ba363dc811024146212)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/file.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/blk-crypto.h>
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/stat.h>
12 #include <linux/writeback.h>
13 #include <linux/blkdev.h>
14 #include <linux/falloc.h>
15 #include <linux/filelock.h>
16 #include <linux/types.h>
17 #include <linux/compat.h>
18 #include <linux/uaccess.h>
19 #include <linux/mount.h>
20 #include <linux/uio.h>
21 #include <linux/uuid.h>
22 #include <linux/file.h>
23 #include <linux/nls.h>
24 #include <linux/sched/signal.h>
25 #include <linux/fileattr.h>
26 #include <linux/fadvise.h>
27 #include <linux/iomap.h>
28 
29 #include "f2fs.h"
30 #include "node.h"
31 #include "segment.h"
32 #include "xattr.h"
33 #include "acl.h"
34 #include "gc.h"
35 #include "iostat.h"
36 #include <trace/events/f2fs.h>
37 #include <uapi/linux/f2fs.h>
38 
39 static int fill_zero(struct inode *inode, pgoff_t index,
40 					loff_t start, loff_t len);
41 
42 static int do_zero_post_eof_page(struct inode *inode, loff_t new_size)
43 {
44 	loff_t old_size = i_size_read(inode);
45 	unsigned int offset, len;
46 	pgoff_t index;
47 	int err;
48 
49 	offset = old_size & (PAGE_SIZE - 1);
50 
51 	if (!offset)
52 		return 0;
53 
54 	len = min_t(loff_t, PAGE_SIZE - offset, new_size - old_size);
55 	index = old_size >> PAGE_SHIFT;
56 
57 	if (f2fs_has_inline_data(inode)) {
58 		/* data post eof should be always zero */
59 		if (new_size <= MAX_INLINE_DATA(inode))
60 			return 0;
61 		err = f2fs_convert_inline_inode(inode);
62 		if (err)
63 			return err;
64 	}
65 
66 	err = fill_zero(inode, index, offset, len);
67 	if (err)
68 		return err;
69 	return filemap_write_and_wait_range(inode->i_mapping,
70 				old_size, old_size + len - 1);
71 }
72 
73 static int f2fs_zero_post_eof_page(struct inode *inode,
74 					loff_t new_size, bool lock, bool writeback)
75 {
76 	loff_t old_size = i_size_read(inode);
77 	bool strict =
78 		F2FS_OPTION(F2FS_I_SB(inode)).fsync_mode == FSYNC_MODE_STRICT;
79 
80 	if (old_size >= new_size)
81 		return 0;
82 
83 	if (!strict && mapping_empty(inode->i_mapping))
84 		return 0;
85 
86 	if (lock)
87 		filemap_invalidate_lock(inode->i_mapping);
88 	/* zero or drop pages only in range of [old_size, new_size] */
89 	truncate_inode_pages_range(inode->i_mapping, old_size, new_size - 1);
90 	if (lock)
91 		filemap_invalidate_unlock(inode->i_mapping);
92 
93 	if (!writeback || !strict)
94 		return 0;
95 	/*
96 	 * In fsync_mode=strict, when we expand an unaligned EOF size, we
97 	 * should zero post EOF data and writeback the data immediately,
98 	 * so that it can avoid exposing stale data after metadata flush
99 	 * and POR.
100 	 */
101 	return do_zero_post_eof_page(inode, new_size);
102 }
103 
104 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
105 {
106 	struct inode *inode = file_inode(vmf->vma->vm_file);
107 	vm_flags_t flags = vmf->vma->vm_flags;
108 	vm_fault_t ret;
109 
110 	ret = filemap_fault(vmf);
111 	if (ret & VM_FAULT_LOCKED)
112 		f2fs_update_iostat(F2FS_I_SB(inode), inode,
113 					APP_MAPPED_READ_IO,
114 					F2FS_BLKSIZE(F2FS_I_SB(inode)));
115 
116 	trace_f2fs_filemap_fault(inode, vmf->pgoff, flags, ret);
117 
118 	return ret;
119 }
120 
121 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
122 {
123 	struct folio *folio = page_folio(vmf->page);
124 	struct inode *inode = file_inode(vmf->vma->vm_file);
125 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
126 	struct dnode_of_data dn;
127 	bool need_alloc = !f2fs_is_pinned_file(inode);
128 	loff_t isize;
129 	int err = 0;
130 	vm_fault_t ret;
131 
132 	/*
133 	 * We only support large folio on the read case.
134 	 * Don't make any dirty pages.
135 	 */
136 	if (unlikely(IS_IMMUTABLE(inode)) ||
137 	    mapping_large_folio_support(inode->i_mapping)) {
138 		f2fs_err(sbi, "Not expected: immutable: %d large_folio: %d",
139 				IS_IMMUTABLE(inode),
140 				mapping_large_folio_support(inode->i_mapping));
141 		return VM_FAULT_SIGBUS;
142 	}
143 
144 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
145 		err = -EIO;
146 		goto out;
147 	}
148 
149 	if (unlikely(f2fs_cp_error(sbi))) {
150 		err = -EIO;
151 		goto out;
152 	}
153 
154 	if (!f2fs_is_checkpoint_ready(sbi)) {
155 		err = -ENOSPC;
156 		goto out;
157 	}
158 
159 	err = f2fs_convert_inline_inode(inode);
160 	if (err)
161 		goto out;
162 
163 #ifdef CONFIG_F2FS_FS_COMPRESSION
164 	if (f2fs_compressed_file(inode)) {
165 		int ret = f2fs_is_compressed_cluster(inode, folio->index);
166 
167 		if (ret < 0) {
168 			err = ret;
169 			goto out;
170 		} else if (ret) {
171 			need_alloc = false;
172 		}
173 	}
174 #endif
175 	/* should do out of any locked page */
176 	if (need_alloc)
177 		f2fs_balance_fs(sbi, true);
178 
179 	sb_start_pagefault(inode->i_sb);
180 
181 	f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
182 
183 	err = f2fs_zero_post_eof_page(inode,
184 		(folio->index + 1) << PAGE_SHIFT, true, false);
185 	if (err)
186 		goto out_pagefault;
187 
188 	file_update_time(vmf->vma->vm_file);
189 	filemap_invalidate_lock_shared(inode->i_mapping);
190 
191 	/*
192 	 * mmap stores bypass the COW inode and would break atomicity. Check
193 	 * under invalidate_lock: f2fs_ioc_start_atomic_write() holds it while
194 	 * flushing dirty folios and setting FI_ATOMIC_FILE, so either it sees
195 	 * our dirty folio and writes it back, or we see the flag.
196 	 */
197 	if (unlikely(f2fs_is_atomic_file(inode))) {
198 		err = -EOPNOTSUPP;
199 		goto out_sem;
200 	}
201 
202 	folio_lock(folio);
203 	/*
204 	 * A concurrent buffered write can extend isize, so we store isize in a
205 	 * local variable to let the zeroing offset match the EOF check below.
206 	 */
207 	isize = i_size_read(inode);
208 
209 	if (unlikely(folio->mapping != inode->i_mapping ||
210 			folio_pos(folio) > isize ||
211 			!folio_test_uptodate(folio))) {
212 		folio_unlock(folio);
213 		err = -EFAULT;
214 		goto out_sem;
215 	}
216 
217 	set_new_dnode(&dn, inode, NULL, NULL, 0);
218 	if (need_alloc) {
219 		/* block allocation */
220 		err = f2fs_get_block_locked(&dn, folio->index);
221 	} else {
222 		err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
223 		f2fs_put_dnode(&dn);
224 		if (f2fs_is_pinned_file(inode) &&
225 		    !__is_valid_data_blkaddr(dn.data_blkaddr))
226 			err = -EIO;
227 	}
228 
229 	if (err) {
230 		folio_unlock(folio);
231 		goto out_sem;
232 	}
233 
234 	f2fs_folio_wait_writeback(folio, false, true);
235 
236 	/* wait for GCed page writeback via generic cache */
237 	f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
238 
239 	/* page is wholly or partially inside EOF */
240 	if (((loff_t)(folio->index + 1) << PAGE_SHIFT) > isize) {
241 		loff_t offset;
242 
243 		offset = isize & ~PAGE_MASK;
244 		folio_zero_segment(folio, offset, folio_size(folio));
245 	}
246 	folio_mark_dirty(folio);
247 
248 	f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE(sbi));
249 	f2fs_update_time(sbi, REQ_TIME);
250 
251 out_sem:
252 	filemap_invalidate_unlock_shared(inode->i_mapping);
253 out_pagefault:
254 	sb_end_pagefault(inode->i_sb);
255 out:
256 	ret = vmf_fs_error(err);
257 
258 	trace_f2fs_vm_page_mkwrite(inode, folio->index, vmf->vma->vm_flags, ret);
259 	return ret;
260 }
261 
262 static const struct vm_operations_struct f2fs_file_vm_ops = {
263 	.fault		= f2fs_filemap_fault,
264 	.map_pages	= filemap_map_pages,
265 	.page_mkwrite	= f2fs_vm_page_mkwrite,
266 };
267 
268 static int get_parent_ino(struct inode *inode, nid_t *pino)
269 {
270 	struct dentry *dentry;
271 
272 	/*
273 	 * Make sure to get the non-deleted alias.  The alias associated with
274 	 * the open file descriptor being fsync()'ed may be deleted already.
275 	 */
276 	dentry = d_find_alias(inode);
277 	if (!dentry)
278 		return 0;
279 
280 	*pino = d_parent_ino(dentry);
281 	dput(dentry);
282 	return 1;
283 }
284 
285 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
286 {
287 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
288 	enum cp_reason_type cp_reason = CP_NO_NEEDED;
289 
290 	if (!S_ISREG(inode->i_mode))
291 		cp_reason = CP_NON_REGULAR;
292 	else if (f2fs_compressed_file(inode))
293 		cp_reason = CP_COMPRESSED;
294 	else if (inode->i_nlink != 1)
295 		cp_reason = CP_HARDLINK;
296 	else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
297 		cp_reason = CP_SB_NEED_CP;
298 	else if (file_wrong_pino(inode))
299 		cp_reason = CP_WRONG_PINO;
300 	else if (!f2fs_space_for_roll_forward(sbi))
301 		cp_reason = CP_NO_SPC_ROLL;
302 	else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
303 		cp_reason = CP_NODE_NEED_CP;
304 	else if (test_opt(sbi, FASTBOOT))
305 		cp_reason = CP_FASTBOOT_MODE;
306 	else if (F2FS_OPTION(sbi).active_logs == 2)
307 		cp_reason = CP_SPEC_LOG_NUM;
308 	else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
309 		f2fs_need_dentry_mark(sbi, inode->i_ino) &&
310 		f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
311 							TRANS_DIR_INO))
312 		cp_reason = CP_RECOVER_DIR;
313 	else if (f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
314 							XATTR_DIR_INO))
315 		cp_reason = CP_XATTR_DIR;
316 
317 	return cp_reason;
318 }
319 
320 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
321 {
322 	struct f2fs_cached_block *entry = f2fs_find_node_cache(sbi, ino);
323 	bool ret = false;
324 	/* But we need to avoid that there are some inode updates */
325 	if ((!IS_ERR(entry) && f2fs_cache_test_dirty(entry)) ||
326 	    f2fs_need_inode_block_update(sbi, ino))
327 		ret = true;
328 	f2fs_put_cache(entry, false);
329 	return ret;
330 }
331 
332 static void try_to_fix_pino(struct inode *inode)
333 {
334 	struct f2fs_inode_info *fi = F2FS_I(inode);
335 	nid_t pino;
336 
337 	f2fs_down_write(&fi->i_sem);
338 	if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
339 			get_parent_ino(inode, &pino)) {
340 		f2fs_i_pino_write(inode, pino);
341 		file_got_pino(inode);
342 	}
343 	f2fs_up_write(&fi->i_sem);
344 }
345 
346 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
347 						int datasync, bool atomic)
348 {
349 	struct inode *inode = file->f_mapping->host;
350 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
351 	nid_t ino = inode->i_ino;
352 	int ret = 0;
353 	enum cp_reason_type cp_reason = 0;
354 	unsigned int seq_id = 0;
355 
356 	if (unlikely(f2fs_readonly(inode->i_sb)))
357 		return 0;
358 
359 	trace_f2fs_sync_file_enter(inode);
360 
361 	if (S_ISDIR(inode->i_mode))
362 		goto go_write;
363 
364 	/* if fdatasync is triggered, let's do in-place-update */
365 	if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
366 		set_inode_flag(inode, FI_NEED_IPU);
367 	ret = file_write_and_wait_range(file, start, end);
368 	clear_inode_flag(inode, FI_NEED_IPU);
369 
370 	if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
371 		trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
372 		return ret;
373 	}
374 
375 	/* if the inode is dirty, let's recover all the time */
376 	if (!f2fs_skip_inode_update(inode, datasync)) {
377 		f2fs_write_inode(inode, NULL);
378 		goto go_write;
379 	}
380 
381 	/*
382 	 * if there is no written data, don't waste time to write recovery info.
383 	 */
384 	if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
385 			!f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
386 
387 		/* it may call write_inode just prior to fsync */
388 		if (need_inode_page_update(sbi, ino))
389 			goto go_write;
390 
391 		if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
392 				f2fs_exist_written_data(sbi, ino, UPDATE_INO))
393 			goto flush_out;
394 		goto out;
395 	} else {
396 		/*
397 		 * for OPU case, during fsync(), node can be persisted before
398 		 * data when lower device doesn't support write barrier, result
399 		 * in data corruption after SPO.
400 		 * So for strict fsync mode, force to use atomic write semantics
401 		 * to keep write order in between data/node and last node to
402 		 * avoid potential data corruption.
403 		 */
404 		if (F2FS_OPTION(sbi).fsync_mode ==
405 				FSYNC_MODE_STRICT && !atomic)
406 			atomic = true;
407 	}
408 go_write:
409 	/*
410 	 * Both of fdatasync() and fsync() are able to be recovered from
411 	 * sudden-power-off.
412 	 */
413 	f2fs_down_read(&F2FS_I(inode)->i_sem);
414 	cp_reason = need_do_checkpoint(inode);
415 	f2fs_up_read(&F2FS_I(inode)->i_sem);
416 
417 	if (cp_reason) {
418 		/* all the dirty node pages should be flushed for POR */
419 		ret = f2fs_sync_fs(inode->i_sb, 1);
420 
421 		/*
422 		 * We've secured consistency through sync_fs. Following pino
423 		 * will be used only for fsynced inodes after checkpoint.
424 		 */
425 		try_to_fix_pino(inode);
426 		clear_inode_flag(inode, FI_APPEND_WRITE);
427 		clear_inode_flag(inode, FI_UPDATE_WRITE);
428 		goto out;
429 	}
430 sync_nodes:
431 	atomic_inc(&sbi->wb_sync_req[NODE]);
432 	ret = f2fs_fsync_node_caches(sbi, inode, atomic, &seq_id);
433 	atomic_dec(&sbi->wb_sync_req[NODE]);
434 	if (ret)
435 		goto out;
436 
437 	/* if cp_error was enabled, we should avoid infinite loop */
438 	if (unlikely(f2fs_cp_error(sbi))) {
439 		ret = -EIO;
440 		goto out;
441 	}
442 
443 	if (f2fs_need_inode_block_update(sbi, ino)) {
444 		f2fs_mark_inode_dirty_sync(inode, true);
445 		f2fs_write_inode(inode, NULL);
446 		goto sync_nodes;
447 	}
448 
449 	/*
450 	 * If it's atomic_write, it's just fine to keep write ordering. So
451 	 * here we don't need to wait for node write completion, since we use
452 	 * node chain which serializes node blocks. If one of node writes are
453 	 * reordered, we can see simply broken chain, resulting in stopping
454 	 * roll-forward recovery. It means we'll recover all or none node blocks
455 	 * given fsync mark.
456 	 */
457 	if (!atomic) {
458 		ret = f2fs_wait_on_node_caches_writeback(sbi, seq_id);
459 		if (ret)
460 			goto out;
461 	}
462 
463 	/* once recovery info is written, don't need to tack this */
464 	f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
465 	clear_inode_flag(inode, FI_APPEND_WRITE);
466 flush_out:
467 	if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
468 		ret = f2fs_issue_flush(sbi, inode->i_ino);
469 	if (!ret) {
470 		f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
471 		clear_inode_flag(inode, FI_UPDATE_WRITE);
472 		f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
473 	}
474 	f2fs_update_time(sbi, REQ_TIME);
475 out:
476 	trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
477 	return ret;
478 }
479 
480 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
481 {
482 	if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
483 		return -EIO;
484 	return f2fs_do_sync_file(file, start, end, datasync, false);
485 }
486 
487 static bool __found_offset(struct address_space *mapping,
488 		struct dnode_of_data *dn, pgoff_t index, int whence)
489 {
490 	block_t blkaddr = f2fs_data_blkaddr(dn);
491 	struct inode *inode = mapping->host;
492 	bool compressed_cluster = false;
493 
494 	if (f2fs_compressed_file(inode)) {
495 		block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_entry,
496 		    ALIGN_DOWN(dn->ofs_in_node, F2FS_I(inode)->i_cluster_size));
497 
498 		compressed_cluster = first_blkaddr == COMPRESS_ADDR;
499 	}
500 
501 	switch (whence) {
502 	case SEEK_DATA:
503 		if (__is_valid_data_blkaddr(blkaddr))
504 			return true;
505 		if (blkaddr == NEW_ADDR &&
506 		    xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
507 			return true;
508 		if (compressed_cluster)
509 			return true;
510 		break;
511 	case SEEK_HOLE:
512 		if (compressed_cluster)
513 			return false;
514 		if (blkaddr == NULL_ADDR)
515 			return true;
516 		break;
517 	}
518 	return false;
519 }
520 
521 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
522 {
523 	struct inode *inode = file->f_mapping->host;
524 	loff_t maxbytes = F2FS_BLK_TO_BYTES(F2FS_I_SB(inode),
525 			max_file_blocks(F2FS_I_SB(inode), inode));
526 	struct dnode_of_data dn;
527 	pgoff_t pgofs, end_offset;
528 	loff_t data_ofs = offset;
529 	loff_t isize;
530 	int err = 0;
531 
532 	inode_lock_shared(inode);
533 
534 	isize = i_size_read(inode);
535 	if (offset >= isize)
536 		goto fail;
537 
538 	/* handle inline data case */
539 	if (f2fs_has_inline_data(inode)) {
540 		if (whence == SEEK_HOLE) {
541 			data_ofs = isize;
542 			goto found;
543 		} else if (whence == SEEK_DATA) {
544 			data_ofs = offset;
545 			goto found;
546 		}
547 	}
548 
549 	pgofs = F2FS_BYTES_TO_BLK(F2FS_I_SB(inode), offset);
550 
551 	for (; data_ofs < isize;
552 			data_ofs = F2FS_BLK_TO_BYTES(F2FS_I_SB(inode), pgofs)) {
553 		set_new_dnode(&dn, inode, NULL, NULL, 0);
554 		err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
555 		if (err && err != -ENOENT) {
556 			goto fail;
557 		} else if (err == -ENOENT) {
558 			/* direct node does not exists */
559 			if (whence == SEEK_DATA) {
560 				pgofs = f2fs_get_next_page_offset(&dn, pgofs);
561 				continue;
562 			} else {
563 				goto found;
564 			}
565 		}
566 
567 		end_offset = ADDRS_PER_PAGE(dn.node_entry, inode);
568 
569 		/* find data/hole in dnode block */
570 		for (; dn.ofs_in_node < end_offset;
571 				dn.ofs_in_node++, pgofs++,
572 				data_ofs = F2FS_BLK_TO_BYTES(F2FS_I_SB(inode), pgofs)) {
573 			block_t blkaddr;
574 
575 			blkaddr = f2fs_data_blkaddr(&dn);
576 
577 			if (__is_valid_data_blkaddr(blkaddr) &&
578 				!f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
579 					blkaddr, DATA_GENERIC_ENHANCE)) {
580 				f2fs_put_dnode(&dn);
581 				goto fail;
582 			}
583 
584 			if (__found_offset(file->f_mapping, &dn,
585 							pgofs, whence)) {
586 				f2fs_put_dnode(&dn);
587 				goto found;
588 			}
589 		}
590 		f2fs_put_dnode(&dn);
591 	}
592 
593 	if (whence == SEEK_DATA)
594 		goto fail;
595 found:
596 	if (whence == SEEK_HOLE && data_ofs > isize)
597 		data_ofs = isize;
598 	inode_unlock_shared(inode);
599 	return vfs_setpos(file, data_ofs, maxbytes);
600 fail:
601 	inode_unlock_shared(inode);
602 	return -ENXIO;
603 }
604 
605 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
606 {
607 	struct inode *inode = file->f_mapping->host;
608 	loff_t maxbytes = F2FS_BLK_TO_BYTES(F2FS_I_SB(inode),
609 			max_file_blocks(F2FS_I_SB(inode), inode));
610 
611 	switch (whence) {
612 	case SEEK_SET:
613 	case SEEK_CUR:
614 	case SEEK_END:
615 		return generic_file_llseek_size(file, offset, whence,
616 						maxbytes, i_size_read(inode));
617 	case SEEK_DATA:
618 	case SEEK_HOLE:
619 		if (offset < 0)
620 			return -ENXIO;
621 		return f2fs_seek_block(file, offset, whence);
622 	}
623 
624 	return -EINVAL;
625 }
626 
627 static int f2fs_file_mmap_prepare(struct vm_area_desc *desc)
628 {
629 	struct file *file = desc->file;
630 	struct inode *inode = file_inode(file);
631 
632 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
633 		return -EIO;
634 
635 	if (!f2fs_is_compress_backend_ready(inode))
636 		return -EOPNOTSUPP;
637 
638 	file_accessed(file);
639 	desc->vm_ops = &f2fs_file_vm_ops;
640 
641 	f2fs_down_read(&F2FS_I(inode)->i_sem);
642 	set_inode_flag(inode, FI_MMAP_FILE);
643 	f2fs_up_read(&F2FS_I(inode)->i_sem);
644 
645 	return 0;
646 }
647 
648 static int finish_preallocate_blocks(struct inode *inode)
649 {
650 	int ret = 0;
651 	bool opened;
652 
653 	f2fs_down_read(&F2FS_I(inode)->i_sem);
654 	opened = is_inode_flag_set(inode, FI_OPENED_FILE);
655 	f2fs_up_read(&F2FS_I(inode)->i_sem);
656 	if (opened)
657 		return 0;
658 
659 	inode_lock(inode);
660 	if (is_inode_flag_set(inode, FI_OPENED_FILE))
661 		goto out_unlock;
662 
663 	if (!file_should_truncate(inode))
664 		goto out_update;
665 
666 	f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
667 	filemap_invalidate_lock(inode->i_mapping);
668 
669 	truncate_setsize(inode, i_size_read(inode));
670 	ret = f2fs_truncate(inode);
671 
672 	filemap_invalidate_unlock(inode->i_mapping);
673 	f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
674 	if (ret)
675 		goto out_unlock;
676 
677 	file_dont_truncate(inode);
678 out_update:
679 	f2fs_down_write(&F2FS_I(inode)->i_sem);
680 	set_inode_flag(inode, FI_OPENED_FILE);
681 	f2fs_up_write(&F2FS_I(inode)->i_sem);
682 out_unlock:
683 	inode_unlock(inode);
684 	return ret;
685 }
686 
687 static int f2fs_file_open(struct inode *inode, struct file *filp)
688 {
689 	int err = fscrypt_file_open(inode, filp);
690 
691 	if (err)
692 		return err;
693 
694 	if (!f2fs_is_compress_backend_ready(inode))
695 		return -EOPNOTSUPP;
696 
697 	if (mapping_large_folio_support(inode->i_mapping) &&
698 	    filp->f_mode & FMODE_WRITE)
699 		return -EOPNOTSUPP;
700 
701 	err = fsverity_file_open(inode, filp);
702 	if (err)
703 		return err;
704 
705 	filp->f_mode |= FMODE_NOWAIT;
706 	filp->f_mode |= FMODE_CAN_ODIRECT;
707 
708 	err = dquot_file_open(inode, filp);
709 	if (err)
710 		return err;
711 
712 	err = finish_preallocate_blocks(inode);
713 	if (!err)
714 		atomic_inc(&F2FS_I(inode)->open_count);
715 	return err;
716 }
717 
718 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
719 {
720 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
721 	int nr_free = 0, ofs = dn->ofs_in_node, len = count;
722 	__le32 *addr;
723 	bool compressed_cluster = false;
724 	int cluster_index = 0, valid_blocks = 0;
725 	int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
726 	bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
727 	block_t blkstart;
728 	int blklen = 0;
729 
730 	addr = get_dnode_addr(dn->inode, dn->node_entry) + ofs;
731 	blkstart = le32_to_cpu(*addr);
732 
733 	/* Assumption: truncation starts with cluster */
734 	for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
735 		block_t blkaddr = le32_to_cpu(*addr);
736 
737 		if (f2fs_compressed_file(dn->inode) &&
738 					!(cluster_index & (cluster_size - 1))) {
739 			if (compressed_cluster)
740 				f2fs_i_compr_blocks_update(dn->inode,
741 							valid_blocks, false);
742 			compressed_cluster = (blkaddr == COMPRESS_ADDR);
743 			valid_blocks = 0;
744 		}
745 
746 		if (blkaddr == NULL_ADDR)
747 			goto next;
748 
749 		f2fs_set_data_blkaddr(dn, NULL_ADDR);
750 
751 		if (__is_valid_data_blkaddr(blkaddr)) {
752 			if (time_to_inject(sbi, FAULT_BLKADDR_CONSISTENCE))
753 				goto next;
754 			if (!f2fs_is_valid_blkaddr_raw(sbi, blkaddr,
755 						DATA_GENERIC_ENHANCE))
756 				goto next;
757 			if (compressed_cluster)
758 				valid_blocks++;
759 		}
760 
761 		if (blkstart + blklen == blkaddr) {
762 			blklen++;
763 		} else {
764 			f2fs_invalidate_blocks(sbi, blkstart, blklen);
765 			blkstart = blkaddr;
766 			blklen = 1;
767 		}
768 
769 		if (!released || blkaddr != COMPRESS_ADDR)
770 			nr_free++;
771 
772 		continue;
773 
774 next:
775 		if (blklen)
776 			f2fs_invalidate_blocks(sbi, blkstart, blklen);
777 
778 		blkstart = le32_to_cpu(*(addr + 1));
779 		blklen = 0;
780 	}
781 
782 	if (blklen)
783 		f2fs_invalidate_blocks(sbi, blkstart, blklen);
784 
785 	if (compressed_cluster)
786 		f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
787 
788 	if (nr_free) {
789 		pgoff_t fofs;
790 		/*
791 		 * once we invalidate valid blkaddr in range [ofs, ofs + count],
792 		 * we will invalidate all blkaddr in the whole range.
793 		 */
794 		fofs = f2fs_start_bidx_of_node(ofs_of_node(sbi, dn->node_entry),
795 							dn->inode) + ofs;
796 		f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
797 		f2fs_update_age_extent_cache_range(dn, fofs, len);
798 		dec_valid_block_count(sbi, dn->inode, nr_free);
799 	}
800 	dn->ofs_in_node = ofs;
801 
802 	f2fs_update_time(sbi, REQ_TIME);
803 	trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
804 					 dn->ofs_in_node, nr_free);
805 }
806 
807 static int truncate_partial_data_page(struct inode *inode, u64 from,
808 								bool cache_only)
809 {
810 	loff_t offset = from & (PAGE_SIZE - 1);
811 	pgoff_t index = from >> PAGE_SHIFT;
812 	struct address_space *mapping = inode->i_mapping;
813 	struct folio *folio;
814 
815 	if (!offset && !cache_only)
816 		return 0;
817 
818 	if (cache_only) {
819 		folio = filemap_lock_folio(mapping, index);
820 		if (IS_ERR(folio))
821 		       return 0;
822 		if (folio_test_uptodate(folio))
823 			goto truncate_out;
824 		f2fs_folio_put(folio, true);
825 		return 0;
826 	}
827 
828 	folio = f2fs_get_lock_data_folio(inode, index, true);
829 	if (IS_ERR(folio))
830 		return PTR_ERR(folio) == -ENOENT ? 0 : PTR_ERR(folio);
831 truncate_out:
832 	f2fs_folio_wait_writeback(folio, true, true);
833 	folio_zero_segment(folio, offset, folio_size(folio));
834 
835 	/* An encrypted inode should have a key and truncate the last page. */
836 	f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
837 	if (!cache_only)
838 		folio_mark_dirty(folio);
839 	f2fs_folio_put(folio, true);
840 	return 0;
841 }
842 
843 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
844 {
845 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
846 	struct dnode_of_data dn;
847 	struct f2fs_lock_context lc;
848 	pgoff_t free_from;
849 	int count = 0, err = 0;
850 	struct f2fs_cached_block *ientry;
851 	bool truncate_page = false;
852 
853 	trace_f2fs_truncate_blocks_enter(inode, from);
854 
855 	if (IS_DEVICE_ALIASING(inode) && from) {
856 		err = -EINVAL;
857 		goto out_err;
858 	}
859 
860 	free_from = (pgoff_t)F2FS_BLK_ALIGN(sbi, from);
861 
862 	if (free_from >= max_file_blocks(sbi, inode))
863 		goto free_partial;
864 
865 	if (lock)
866 		f2fs_lock_op(sbi, &lc);
867 
868 	ientry = f2fs_get_inode_cache(sbi, inode->i_ino);
869 	if (IS_ERR(ientry)) {
870 		err = PTR_ERR(ientry);
871 		goto out;
872 	}
873 
874 	if (IS_DEVICE_ALIASING(inode)) {
875 		struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
876 		struct extent_info ei;
877 
878 		read_lock(&et->lock);
879 		ei = et->largest;
880 		read_unlock(&et->lock);
881 
882 		f2fs_invalidate_blocks(sbi, ei.blk, ei.len);
883 
884 		dec_valid_block_count(sbi, inode, ei.len);
885 		f2fs_update_time(sbi, REQ_TIME);
886 
887 		f2fs_drop_extent_tree(inode);
888 
889 		f2fs_put_cache(ientry, true);
890 		goto out;
891 	}
892 
893 	if (f2fs_has_inline_data(inode)) {
894 		f2fs_truncate_inline_inode(inode, ientry, from);
895 		f2fs_put_cache(ientry, true);
896 		truncate_page = true;
897 		goto out;
898 	}
899 
900 	set_new_dnode(&dn, inode, ientry, NULL, 0);
901 	err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
902 	if (err) {
903 		if (err == -ENOENT)
904 			goto free_next;
905 		goto out;
906 	}
907 
908 	count = ADDRS_PER_PAGE(dn.node_entry, inode);
909 
910 	count -= dn.ofs_in_node;
911 	f2fs_bug_on(sbi, count < 0);
912 
913 	if (dn.ofs_in_node || IS_INODE(sbi, dn.node_entry)) {
914 		f2fs_truncate_data_blocks_range(&dn, count);
915 		free_from += count;
916 	}
917 
918 	f2fs_put_dnode(&dn);
919 free_next:
920 	err = f2fs_truncate_inode_blocks(inode, free_from);
921 out:
922 	if (lock)
923 		f2fs_unlock_op(sbi, &lc);
924 free_partial:
925 	/* lastly zero out the first data page */
926 	if (!err)
927 		err = truncate_partial_data_page(inode, from, truncate_page);
928 out_err:
929 	trace_f2fs_truncate_blocks_exit(inode, err);
930 	return err;
931 }
932 
933 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
934 {
935 	u64 free_from = from;
936 	int err;
937 
938 #ifdef CONFIG_F2FS_FS_COMPRESSION
939 	/*
940 	 * for compressed file, only support cluster size
941 	 * aligned truncation.
942 	 */
943 	if (f2fs_compressed_file(inode))
944 		free_from = round_up(from,
945 				F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
946 #endif
947 
948 	err = f2fs_do_truncate_blocks(inode, free_from, lock);
949 	if (err)
950 		return err;
951 
952 #ifdef CONFIG_F2FS_FS_COMPRESSION
953 	/*
954 	 * For compressed file, after release compress blocks, don't allow write
955 	 * direct, but we should allow write direct after truncate to zero.
956 	 */
957 	if (f2fs_compressed_file(inode) && !free_from
958 			&& is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
959 		clear_inode_flag(inode, FI_COMPRESS_RELEASED);
960 
961 	if (from != free_from) {
962 		err = f2fs_truncate_partial_cluster(inode, from, lock);
963 		if (err)
964 			return err;
965 	}
966 #endif
967 
968 	return 0;
969 }
970 
971 int f2fs_truncate(struct inode *inode)
972 {
973 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
974 	int err;
975 
976 	if (unlikely(f2fs_cp_error(sbi)))
977 		return -EIO;
978 
979 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
980 				S_ISLNK(inode->i_mode)))
981 		return 0;
982 
983 	trace_f2fs_truncate(inode);
984 
985 	if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
986 		return -EIO;
987 
988 	err = f2fs_dquot_initialize(inode);
989 	if (err)
990 		return err;
991 
992 	/* we should check inline_data size */
993 	if (!f2fs_may_inline_data(inode)) {
994 		err = f2fs_convert_inline_inode(inode);
995 		if (err) {
996 			/*
997 			 * Always truncate page #0 to avoid page cache
998 			 * leak in evict() path.
999 			 */
1000 			truncate_inode_pages_range(inode->i_mapping,
1001 					F2FS_BLK_TO_BYTES(sbi, 0),
1002 					F2FS_BLK_END_BYTES(sbi, 0));
1003 			return err;
1004 		}
1005 	}
1006 
1007 	err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1008 	if (err)
1009 		return err;
1010 
1011 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1012 	f2fs_mark_inode_dirty_sync(inode, false);
1013 	return 0;
1014 }
1015 
1016 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
1017 {
1018 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1019 
1020 	if (fsverity_active(inode))
1021 		return true;
1022 	if (f2fs_compressed_file(inode))
1023 		return true;
1024 	/*
1025 	 * only force direct read to use buffered IO, for direct write,
1026 	 * it expects inline data conversion before committing IO.
1027 	 */
1028 	if (f2fs_has_inline_data(inode) && rw == READ)
1029 		return true;
1030 
1031 	/* disallow direct IO if any of devices has unaligned blksize */
1032 	if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
1033 		return true;
1034 	/*
1035 	 * for blkzoned device, fallback direct IO to buffered IO, so
1036 	 * all IOs can be serialized by log-structured write.
1037 	 */
1038 	if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE) &&
1039 	    !f2fs_is_pinned_file(inode))
1040 		return true;
1041 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1042 		return true;
1043 
1044 	return false;
1045 }
1046 
1047 int f2fs_getattr(const struct mnt_idmap *idmap, const struct path *path,
1048 		 struct kstat *stat, u32 request_mask, unsigned int query_flags)
1049 {
1050 	struct inode *inode = d_inode(path->dentry);
1051 	struct f2fs_inode_info *fi = F2FS_I(inode);
1052 	struct f2fs_inode *ri = NULL;
1053 	unsigned int flags;
1054 
1055 	if (f2fs_has_extra_attr(inode) &&
1056 			f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
1057 			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
1058 		stat->result_mask |= STATX_BTIME;
1059 		stat->btime.tv_sec = fi->i_crtime.tv_sec;
1060 		stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
1061 	}
1062 
1063 	/*
1064 	 * Return the DIO alignment restrictions if requested.
1065 	 *
1066 	 * f2fs sometimes supports DIO reads but not DIO writes.  STATX_DIOALIGN
1067 	 * cannot represent that, so in that case we report no DIO support.
1068 	 */
1069 	if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
1070 		unsigned int bsize = i_blocksize(inode);
1071 
1072 		stat->result_mask |= STATX_DIOALIGN;
1073 		if (!f2fs_force_buffered_io(inode, WRITE)) {
1074 			stat->dio_mem_align = bsize;
1075 			stat->dio_offset_align = bsize;
1076 		}
1077 	}
1078 
1079 	flags = fi->i_flags;
1080 	if (flags & F2FS_COMPR_FL)
1081 		stat->attributes |= STATX_ATTR_COMPRESSED;
1082 	if (flags & F2FS_APPEND_FL)
1083 		stat->attributes |= STATX_ATTR_APPEND;
1084 	if (IS_ENCRYPTED(inode))
1085 		stat->attributes |= STATX_ATTR_ENCRYPTED;
1086 	if (flags & F2FS_IMMUTABLE_FL)
1087 		stat->attributes |= STATX_ATTR_IMMUTABLE;
1088 	if (flags & F2FS_NODUMP_FL)
1089 		stat->attributes |= STATX_ATTR_NODUMP;
1090 
1091 	stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
1092 				  STATX_ATTR_APPEND |
1093 				  STATX_ATTR_ENCRYPTED |
1094 				  STATX_ATTR_IMMUTABLE |
1095 				  STATX_ATTR_NODUMP);
1096 
1097 	generic_fillattr(idmap, request_mask, inode, stat);
1098 
1099 	/* we need to show initial sectors used for inline_data/dentries */
1100 	if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
1101 					f2fs_has_inline_dentry(inode))
1102 		stat->blocks += (stat->size + 511) >> 9;
1103 
1104 	return 0;
1105 }
1106 
1107 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1108 static void __setattr_copy(const struct mnt_idmap *idmap,
1109 			   struct inode *inode, const struct iattr *attr)
1110 {
1111 	unsigned int ia_valid = attr->ia_valid;
1112 	struct iattr sattr = *attr;
1113 
1114 	sattr.ia_valid &= ~ATTR_MODE;
1115 
1116 	setattr_copy(idmap, inode, &sattr);
1117 	if (ia_valid & ATTR_MODE) {
1118 		umode_t mode = sattr.ia_mode;
1119 
1120 		if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))
1121 			mode &= ~S_ISGID;
1122 		set_acl_inode(inode, mode);
1123 	}
1124 }
1125 #else
1126 #define __setattr_copy setattr_copy
1127 #endif
1128 
1129 int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
1130 		 struct iattr *attr)
1131 {
1132 	struct inode *inode = d_inode(dentry);
1133 	struct f2fs_inode_info *fi = F2FS_I(inode);
1134 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1135 	int err;
1136 
1137 	if (unlikely(f2fs_cp_error(sbi)))
1138 		return -EIO;
1139 
1140 	err = setattr_prepare(idmap, dentry, attr);
1141 	if (err)
1142 		return err;
1143 
1144 	err = fscrypt_prepare_setattr(dentry, attr);
1145 	if (err)
1146 		return err;
1147 
1148 	if (unlikely(IS_IMMUTABLE(inode)))
1149 		return -EPERM;
1150 
1151 	if (unlikely(IS_APPEND(inode) &&
1152 			(attr->ia_valid & (ATTR_MODE | ATTR_UID |
1153 				  ATTR_GID | ATTR_TIMES_SET))))
1154 		return -EPERM;
1155 
1156 	if ((attr->ia_valid & ATTR_SIZE)) {
1157 		if (mapping_large_folio_support(inode->i_mapping))
1158 			return -EOPNOTSUPP;
1159 		if (IS_DEVICE_ALIASING(inode))
1160 			return -EPERM;
1161 		if (!f2fs_is_compress_backend_ready(inode))
1162 			return -EOPNOTSUPP;
1163 		if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) &&
1164 			!IS_ALIGNED(attr->ia_size,
1165 			F2FS_BLK_TO_BYTES(sbi, fi->i_cluster_size)))
1166 			return -EINVAL;
1167 
1168 		if (f2fs_is_pinned_file(inode)) {
1169 			/*
1170 			 * It may break section-aligned fallocate recovery
1171 			 * mechanism, so do not allow larger size truncation.
1172 			 */
1173 			if (attr->ia_size > i_size_read(inode))
1174 				return -EINVAL;
1175 			/*
1176 			 * To prevent scattered pin block generation, we don't
1177 			 * allow smaller/equal size unaligned truncation for
1178 			 * pinned file.
1179 			 */
1180 			else if (!IS_ALIGNED(attr->ia_size,
1181 				F2FS_BLK_TO_BYTES(sbi, CAP_BLKS_PER_SEC(sbi))))
1182 				return -EINVAL;
1183 		}
1184 	}
1185 
1186 	if (is_quota_modification(idmap, inode, attr)) {
1187 		err = f2fs_dquot_initialize(inode);
1188 		if (err)
1189 			return err;
1190 	}
1191 	if (i_uid_needs_update(idmap, attr, inode) ||
1192 	    i_gid_needs_update(idmap, attr, inode)) {
1193 		struct f2fs_lock_context lc;
1194 
1195 		f2fs_lock_op(sbi, &lc);
1196 		err = dquot_transfer(idmap, inode, attr);
1197 		if (err) {
1198 			set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
1199 			f2fs_unlock_op(sbi, &lc);
1200 			return err;
1201 		}
1202 		/*
1203 		 * update uid/gid under lock_op(), so that dquot and inode can
1204 		 * be updated atomically.
1205 		 */
1206 		i_uid_update(idmap, attr, inode);
1207 		i_gid_update(idmap, attr, inode);
1208 		f2fs_mark_inode_dirty_sync(inode, true);
1209 		f2fs_unlock_op(sbi, &lc);
1210 	}
1211 
1212 	if (attr->ia_valid & ATTR_SIZE) {
1213 		loff_t old_size = i_size_read(inode);
1214 
1215 		if (attr->ia_size > MAX_INLINE_DATA(inode)) {
1216 			/*
1217 			 * should convert inline inode before i_size_write to
1218 			 * keep smaller than inline_data size with inline flag.
1219 			 */
1220 			err = f2fs_convert_inline_inode(inode);
1221 			if (err)
1222 				return err;
1223 		}
1224 
1225 		/*
1226 		 * wait for inflight dio, blocks should be removed after
1227 		 * IO completion.
1228 		 */
1229 		if (attr->ia_size < old_size)
1230 			inode_dio_wait(inode);
1231 
1232 		f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
1233 		filemap_invalidate_lock(inode->i_mapping);
1234 
1235 		if (attr->ia_size > old_size) {
1236 			err = f2fs_zero_post_eof_page(inode,
1237 				attr->ia_size, false, true);
1238 			if (err)
1239 				goto err_out;
1240 		}
1241 		truncate_setsize(inode, attr->ia_size);
1242 
1243 		if (attr->ia_size <= old_size)
1244 			err = f2fs_truncate(inode);
1245 		/*
1246 		 * do not trim all blocks after i_size if target size is
1247 		 * larger than i_size.
1248 		 */
1249 err_out:
1250 		filemap_invalidate_unlock(inode->i_mapping);
1251 		f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
1252 		if (err)
1253 			return err;
1254 
1255 		spin_lock(&fi->i_size_lock);
1256 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1257 		fi->last_disk_size = i_size_read(inode);
1258 		spin_unlock(&fi->i_size_lock);
1259 	}
1260 
1261 	__setattr_copy(idmap, inode, attr);
1262 
1263 	if (attr->ia_valid & ATTR_MODE) {
1264 		err = posix_acl_chmod(idmap, dentry, f2fs_get_inode_mode(inode));
1265 
1266 		if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1267 			if (!err)
1268 				inode->i_mode = fi->i_acl_mode;
1269 			clear_inode_flag(inode, FI_ACL_MODE);
1270 		}
1271 	}
1272 
1273 	/* file size may changed here */
1274 	f2fs_mark_inode_dirty_sync(inode, true);
1275 
1276 	/* inode change will produce dirty node pages flushed by checkpoint */
1277 	f2fs_balance_fs(sbi, true);
1278 
1279 	return err;
1280 }
1281 
1282 const struct inode_operations f2fs_file_inode_operations = {
1283 	.getattr	= f2fs_getattr,
1284 	.setattr	= f2fs_setattr,
1285 	.get_inode_acl	= f2fs_get_acl,
1286 	.set_acl	= f2fs_set_acl,
1287 	.listxattr	= f2fs_listxattr,
1288 	.fiemap		= f2fs_fiemap,
1289 	.fileattr_get	= f2fs_fileattr_get,
1290 	.fileattr_set	= f2fs_fileattr_set,
1291 };
1292 
1293 static int fill_zero(struct inode *inode, pgoff_t index,
1294 					loff_t start, loff_t len)
1295 {
1296 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1297 	struct folio *folio;
1298 	struct f2fs_lock_context lc;
1299 
1300 	if (!len)
1301 		return 0;
1302 
1303 	f2fs_balance_fs(sbi, true);
1304 
1305 	f2fs_lock_op(sbi, &lc);
1306 	folio = f2fs_get_new_data_folio(inode, NULL, index, false);
1307 	f2fs_unlock_op(sbi, &lc);
1308 
1309 	if (IS_ERR(folio))
1310 		return PTR_ERR(folio);
1311 
1312 	f2fs_folio_wait_writeback(folio, true, true);
1313 	folio_zero_range(folio, start, len);
1314 	folio_mark_dirty(folio);
1315 	f2fs_folio_put(folio, true);
1316 	return 0;
1317 }
1318 
1319 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1320 {
1321 	int err;
1322 
1323 	while (pg_start < pg_end) {
1324 		struct dnode_of_data dn;
1325 		pgoff_t end_offset, count;
1326 
1327 		set_new_dnode(&dn, inode, NULL, NULL, 0);
1328 		err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1329 		if (err) {
1330 			if (err == -ENOENT) {
1331 				pg_start = f2fs_get_next_page_offset(&dn,
1332 								pg_start);
1333 				continue;
1334 			}
1335 			return err;
1336 		}
1337 
1338 		end_offset = ADDRS_PER_PAGE(dn.node_entry, inode);
1339 		count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1340 
1341 		f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1342 
1343 		f2fs_truncate_data_blocks_range(&dn, count);
1344 		f2fs_put_dnode(&dn);
1345 
1346 		pg_start += count;
1347 	}
1348 	return 0;
1349 }
1350 
1351 static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1352 {
1353 	pgoff_t pg_start, pg_end;
1354 	loff_t off_start, off_end;
1355 	int ret;
1356 
1357 	ret = f2fs_convert_inline_inode(inode);
1358 	if (ret)
1359 		return ret;
1360 
1361 	ret = f2fs_zero_post_eof_page(inode, offset + len, true, false);
1362 	if (ret)
1363 		return ret;
1364 
1365 	pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1366 	pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1367 
1368 	off_start = offset & (PAGE_SIZE - 1);
1369 	off_end = (offset + len) & (PAGE_SIZE - 1);
1370 
1371 	if (pg_start == pg_end) {
1372 		ret = fill_zero(inode, pg_start, off_start,
1373 						off_end - off_start);
1374 		if (ret)
1375 			return ret;
1376 	} else {
1377 		if (off_start) {
1378 			ret = fill_zero(inode, pg_start++, off_start,
1379 						PAGE_SIZE - off_start);
1380 			if (ret)
1381 				return ret;
1382 		}
1383 		if (off_end) {
1384 			ret = fill_zero(inode, pg_end, 0, off_end);
1385 			if (ret)
1386 				return ret;
1387 		}
1388 
1389 		if (pg_start < pg_end) {
1390 			loff_t blk_start, blk_end;
1391 			struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1392 			struct f2fs_lock_context lc;
1393 
1394 			f2fs_balance_fs(sbi, true);
1395 
1396 			blk_start = (loff_t)pg_start << PAGE_SHIFT;
1397 			blk_end = (loff_t)pg_end << PAGE_SHIFT;
1398 
1399 			f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1400 			filemap_invalidate_lock(inode->i_mapping);
1401 
1402 			truncate_pagecache_range(inode, blk_start, blk_end - 1);
1403 
1404 			f2fs_lock_op(sbi, &lc);
1405 			ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1406 			f2fs_unlock_op(sbi, &lc);
1407 
1408 			filemap_invalidate_unlock(inode->i_mapping);
1409 			f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1410 		}
1411 	}
1412 
1413 	return ret;
1414 }
1415 
1416 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1417 				int *do_replace, pgoff_t off, pgoff_t len)
1418 {
1419 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1420 	struct dnode_of_data dn;
1421 	int ret, done, i;
1422 
1423 next_dnode:
1424 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1425 	ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1426 	if (ret && ret != -ENOENT) {
1427 		return ret;
1428 	} else if (ret == -ENOENT) {
1429 		if (dn.max_level == 0)
1430 			return -ENOENT;
1431 		done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1432 						dn.ofs_in_node, len);
1433 		blkaddr += done;
1434 		do_replace += done;
1435 		goto next;
1436 	}
1437 
1438 	done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_entry, inode) -
1439 							dn.ofs_in_node, len);
1440 	for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1441 		*blkaddr = f2fs_data_blkaddr(&dn);
1442 
1443 		if (__is_valid_data_blkaddr(*blkaddr) &&
1444 			!f2fs_is_valid_blkaddr(sbi, *blkaddr,
1445 					DATA_GENERIC_ENHANCE)) {
1446 			f2fs_put_dnode(&dn);
1447 			return -EFSCORRUPTED;
1448 		}
1449 
1450 		if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1451 
1452 			if (f2fs_lfs_mode(sbi)) {
1453 				f2fs_put_dnode(&dn);
1454 				return -EOPNOTSUPP;
1455 			}
1456 
1457 			/* do not invalidate this block address */
1458 			f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1459 			*do_replace = 1;
1460 		}
1461 	}
1462 	f2fs_put_dnode(&dn);
1463 next:
1464 	len -= done;
1465 	off += done;
1466 	if (len)
1467 		goto next_dnode;
1468 	return 0;
1469 }
1470 
1471 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1472 				int *do_replace, pgoff_t off, int len)
1473 {
1474 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1475 	struct dnode_of_data dn;
1476 	int ret, i;
1477 
1478 	for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1479 		if (*do_replace == 0)
1480 			continue;
1481 
1482 		set_new_dnode(&dn, inode, NULL, NULL, 0);
1483 		ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1484 		if (ret) {
1485 			dec_valid_block_count(sbi, inode, 1);
1486 			f2fs_invalidate_blocks(sbi, *blkaddr, 1);
1487 		} else {
1488 			f2fs_update_data_blkaddr(&dn, *blkaddr);
1489 		}
1490 		f2fs_put_dnode(&dn);
1491 	}
1492 	return 0;
1493 }
1494 
1495 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1496 			block_t *blkaddr, int *do_replace,
1497 			pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1498 {
1499 	struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1500 	pgoff_t i = 0;
1501 	int ret;
1502 
1503 	while (i < len) {
1504 		if (blkaddr[i] == NULL_ADDR && !full) {
1505 			i++;
1506 			continue;
1507 		}
1508 
1509 		if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1510 			struct dnode_of_data dn;
1511 			struct node_info ni;
1512 			size_t new_size;
1513 			pgoff_t ilen;
1514 
1515 			set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1516 			ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1517 			if (ret)
1518 				return ret;
1519 
1520 			ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1521 			if (ret) {
1522 				f2fs_put_dnode(&dn);
1523 				return ret;
1524 			}
1525 
1526 			ilen = min((pgoff_t)
1527 				ADDRS_PER_PAGE(dn.node_entry, dst_inode) -
1528 						dn.ofs_in_node, len - i);
1529 			do {
1530 				dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1531 				f2fs_truncate_data_blocks_range(&dn, 1);
1532 
1533 				if (do_replace[i]) {
1534 					f2fs_i_blocks_write(src_inode,
1535 							1, false, false);
1536 					f2fs_i_blocks_write(dst_inode,
1537 							1, true, false);
1538 					f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1539 					blkaddr[i], ni.version, true, false);
1540 
1541 					do_replace[i] = 0;
1542 				}
1543 				dn.ofs_in_node++;
1544 				i++;
1545 				new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1546 				if (dst_inode->i_size < new_size)
1547 					f2fs_i_size_write(dst_inode, new_size);
1548 			} while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1549 
1550 			f2fs_put_dnode(&dn);
1551 		} else {
1552 			struct folio *fsrc, *fdst;
1553 
1554 			fsrc = f2fs_get_lock_data_folio(src_inode,
1555 							src + i, true);
1556 			if (IS_ERR(fsrc))
1557 				return PTR_ERR(fsrc);
1558 			fdst = f2fs_get_new_data_folio(dst_inode, NULL, dst + i,
1559 								true);
1560 			if (IS_ERR(fdst)) {
1561 				f2fs_folio_put(fsrc, true);
1562 				return PTR_ERR(fdst);
1563 			}
1564 
1565 			f2fs_folio_wait_writeback(fdst, true, true);
1566 
1567 			memcpy_folio(fdst, 0, fsrc, 0, PAGE_SIZE);
1568 			folio_mark_dirty(fdst);
1569 			folio_set_f2fs_gcing(fdst);
1570 			f2fs_folio_put(fdst, true);
1571 			f2fs_folio_put(fsrc, true);
1572 
1573 			ret = f2fs_truncate_hole(src_inode,
1574 						src + i, src + i + 1);
1575 			if (ret)
1576 				return ret;
1577 			i++;
1578 		}
1579 	}
1580 	return 0;
1581 }
1582 
1583 static int __exchange_data_block(struct inode *src_inode,
1584 			struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1585 			pgoff_t len, bool full)
1586 {
1587 	block_t *src_blkaddr;
1588 	int *do_replace;
1589 	pgoff_t olen;
1590 	int ret;
1591 
1592 	while (len) {
1593 		olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1594 
1595 		src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1596 					array_size(olen, sizeof(block_t)),
1597 					GFP_NOFS);
1598 		if (!src_blkaddr)
1599 			return -ENOMEM;
1600 
1601 		do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1602 					array_size(olen, sizeof(int)),
1603 					GFP_NOFS);
1604 		if (!do_replace) {
1605 			kvfree(src_blkaddr);
1606 			return -ENOMEM;
1607 		}
1608 
1609 		ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1610 					do_replace, src, olen);
1611 		if (ret)
1612 			goto roll_back;
1613 
1614 		ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1615 					do_replace, src, dst, olen, full);
1616 		if (ret)
1617 			goto roll_back;
1618 
1619 		src += olen;
1620 		dst += olen;
1621 		len -= olen;
1622 
1623 		kvfree(src_blkaddr);
1624 		kvfree(do_replace);
1625 	}
1626 	return 0;
1627 
1628 roll_back:
1629 	__roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1630 	kvfree(src_blkaddr);
1631 	kvfree(do_replace);
1632 	return ret;
1633 }
1634 
1635 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1636 {
1637 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1638 	struct f2fs_lock_context lc;
1639 	pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1640 	pgoff_t start = offset >> PAGE_SHIFT;
1641 	pgoff_t end = (offset + len) >> PAGE_SHIFT;
1642 	int ret;
1643 
1644 	f2fs_balance_fs(sbi, true);
1645 
1646 	/* avoid gc operation during block exchange */
1647 	f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1648 	filemap_invalidate_lock(inode->i_mapping);
1649 
1650 	ret = f2fs_zero_post_eof_page(inode, offset + len, false, false);
1651 	if (ret)
1652 		goto out_unlock;
1653 
1654 	f2fs_lock_op(sbi, &lc);
1655 	f2fs_drop_extent_tree(inode);
1656 	truncate_pagecache(inode, offset);
1657 	ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1658 	f2fs_unlock_op(sbi, &lc);
1659 
1660 out_unlock:
1661 	filemap_invalidate_unlock(inode->i_mapping);
1662 	f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1663 	return ret;
1664 }
1665 
1666 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1667 {
1668 	loff_t new_size;
1669 	int ret;
1670 
1671 	if (offset + len >= i_size_read(inode))
1672 		return -EINVAL;
1673 
1674 	/* collapse range should be aligned to block size of f2fs. */
1675 	if (offset & F2FS_BLKSIZE_MASK(F2FS_I_SB(inode)) ||
1676 	    len & F2FS_BLKSIZE_MASK(F2FS_I_SB(inode)))
1677 		return -EINVAL;
1678 
1679 	ret = f2fs_convert_inline_inode(inode);
1680 	if (ret)
1681 		return ret;
1682 
1683 	/* write out all dirty pages from offset */
1684 	ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1685 	if (ret)
1686 		return ret;
1687 
1688 	ret = f2fs_do_collapse(inode, offset, len);
1689 	if (ret)
1690 		return ret;
1691 
1692 	/* write out all moved pages, if possible */
1693 	filemap_invalidate_lock(inode->i_mapping);
1694 	ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1695 	if (ret)
1696 		goto out_unlock;
1697 	truncate_pagecache(inode, offset);
1698 
1699 	new_size = i_size_read(inode) - len;
1700 	ret = f2fs_truncate_blocks(inode, new_size, true);
1701 out_unlock:
1702 	filemap_invalidate_unlock(inode->i_mapping);
1703 	if (!ret)
1704 		f2fs_i_size_write(inode, new_size);
1705 	return ret;
1706 }
1707 
1708 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1709 								pgoff_t end)
1710 {
1711 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1712 	pgoff_t index = start;
1713 	unsigned int ofs_in_node = dn->ofs_in_node;
1714 	blkcnt_t count = 0;
1715 	int ret;
1716 
1717 	for (; index < end; index++, dn->ofs_in_node++) {
1718 		if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1719 			count++;
1720 	}
1721 
1722 	dn->ofs_in_node = ofs_in_node;
1723 	ret = f2fs_reserve_new_blocks(dn, count);
1724 	if (ret)
1725 		return ret;
1726 
1727 	dn->ofs_in_node = ofs_in_node;
1728 	for (index = start; index < end; index++, dn->ofs_in_node++) {
1729 		dn->data_blkaddr = f2fs_data_blkaddr(dn);
1730 		/*
1731 		 * f2fs_reserve_new_blocks will not guarantee entire block
1732 		 * allocation.
1733 		 */
1734 		if (dn->data_blkaddr == NULL_ADDR) {
1735 			ret = -ENOSPC;
1736 			break;
1737 		}
1738 
1739 		if (dn->data_blkaddr == NEW_ADDR)
1740 			continue;
1741 
1742 		if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1743 					DATA_GENERIC_ENHANCE)) {
1744 			ret = -EFSCORRUPTED;
1745 			break;
1746 		}
1747 
1748 		f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
1749 		f2fs_set_data_blkaddr(dn, NEW_ADDR);
1750 	}
1751 
1752 	if (index > start) {
1753 		f2fs_update_read_extent_cache_range(dn, start, 0,
1754 							index - start);
1755 		f2fs_update_age_extent_cache_range(dn, start, index - start);
1756 	}
1757 
1758 	return ret;
1759 }
1760 
1761 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1762 								int mode)
1763 {
1764 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1765 	struct address_space *mapping = inode->i_mapping;
1766 	pgoff_t index, pg_start, pg_end;
1767 	loff_t new_size = i_size_read(inode);
1768 	loff_t off_start, off_end;
1769 	int ret = 0;
1770 
1771 	ret = inode_newsize_ok(inode, (len + offset));
1772 	if (ret)
1773 		return ret;
1774 
1775 	ret = f2fs_convert_inline_inode(inode);
1776 	if (ret)
1777 		return ret;
1778 
1779 	ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1780 	if (ret)
1781 		return ret;
1782 
1783 	ret = f2fs_zero_post_eof_page(inode, offset + len, true, false);
1784 	if (ret)
1785 		return ret;
1786 
1787 	pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1788 	pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1789 
1790 	off_start = offset & (PAGE_SIZE - 1);
1791 	off_end = (offset + len) & (PAGE_SIZE - 1);
1792 
1793 	if (pg_start == pg_end) {
1794 		ret = fill_zero(inode, pg_start, off_start,
1795 						off_end - off_start);
1796 		if (ret)
1797 			return ret;
1798 
1799 		new_size = max_t(loff_t, new_size, offset + len);
1800 	} else {
1801 		if (off_start) {
1802 			ret = fill_zero(inode, pg_start++, off_start,
1803 						PAGE_SIZE - off_start);
1804 			if (ret)
1805 				return ret;
1806 
1807 			new_size = max_t(loff_t, new_size,
1808 					(loff_t)pg_start << PAGE_SHIFT);
1809 		}
1810 
1811 		for (index = pg_start; index < pg_end;) {
1812 			struct dnode_of_data dn;
1813 			struct f2fs_lock_context lc;
1814 			unsigned int end_offset;
1815 			pgoff_t end;
1816 
1817 			f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1818 			filemap_invalidate_lock(mapping);
1819 
1820 			truncate_pagecache_range(inode,
1821 				(loff_t)index << PAGE_SHIFT,
1822 				((loff_t)pg_end << PAGE_SHIFT) - 1);
1823 
1824 			f2fs_lock_op(sbi, &lc);
1825 
1826 			set_new_dnode(&dn, inode, NULL, NULL, 0);
1827 			ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1828 			if (ret) {
1829 				f2fs_unlock_op(sbi, &lc);
1830 				filemap_invalidate_unlock(mapping);
1831 				f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1832 				goto out;
1833 			}
1834 
1835 			end_offset = ADDRS_PER_PAGE(dn.node_entry, inode);
1836 			end = min(pg_end, end_offset - dn.ofs_in_node + index);
1837 
1838 			ret = f2fs_do_zero_range(&dn, index, end);
1839 			f2fs_put_dnode(&dn);
1840 
1841 			f2fs_unlock_op(sbi, &lc);
1842 			filemap_invalidate_unlock(mapping);
1843 			f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1844 
1845 			f2fs_balance_fs(sbi, dn.node_changed);
1846 
1847 			if (ret)
1848 				goto out;
1849 
1850 			index = end;
1851 			new_size = max_t(loff_t, new_size,
1852 					(loff_t)index << PAGE_SHIFT);
1853 		}
1854 
1855 		if (off_end) {
1856 			ret = fill_zero(inode, pg_end, 0, off_end);
1857 			if (ret)
1858 				goto out;
1859 
1860 			new_size = max_t(loff_t, new_size, offset + len);
1861 		}
1862 	}
1863 
1864 out:
1865 	if (new_size > i_size_read(inode)) {
1866 		if (mode & FALLOC_FL_KEEP_SIZE)
1867 			file_set_keep_isize(inode);
1868 		else
1869 			f2fs_i_size_write(inode, new_size);
1870 	}
1871 	return ret;
1872 }
1873 
1874 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1875 {
1876 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1877 	struct address_space *mapping = inode->i_mapping;
1878 	pgoff_t nr, pg_start, pg_end, delta, idx;
1879 	loff_t new_size;
1880 	int ret = 0;
1881 
1882 	new_size = i_size_read(inode) + len;
1883 	ret = inode_newsize_ok(inode, new_size);
1884 	if (ret)
1885 		return ret;
1886 
1887 	if (offset >= i_size_read(inode))
1888 		return -EINVAL;
1889 
1890 	/* insert range should be aligned to block size of f2fs. */
1891 	if (offset & F2FS_BLKSIZE_MASK(F2FS_I_SB(inode)) ||
1892 	    len & F2FS_BLKSIZE_MASK(F2FS_I_SB(inode)))
1893 		return -EINVAL;
1894 
1895 	ret = f2fs_convert_inline_inode(inode);
1896 	if (ret)
1897 		return ret;
1898 
1899 	f2fs_balance_fs(sbi, true);
1900 
1901 	filemap_invalidate_lock(mapping);
1902 	ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1903 	filemap_invalidate_unlock(mapping);
1904 	if (ret)
1905 		return ret;
1906 
1907 	/* write out all dirty pages from offset */
1908 	ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1909 	if (ret)
1910 		return ret;
1911 
1912 	pg_start = offset >> PAGE_SHIFT;
1913 	pg_end = (offset + len) >> PAGE_SHIFT;
1914 	delta = pg_end - pg_start;
1915 	idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1916 
1917 	/* avoid gc operation during block exchange */
1918 	f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1919 	filemap_invalidate_lock(mapping);
1920 
1921 	ret = f2fs_zero_post_eof_page(inode, offset + len, false, false);
1922 	if (ret)
1923 		goto out_unlock;
1924 	truncate_pagecache(inode, offset);
1925 
1926 	while (!ret && idx > pg_start) {
1927 		struct f2fs_lock_context lc;
1928 
1929 		nr = idx - pg_start;
1930 		if (nr > delta)
1931 			nr = delta;
1932 		idx -= nr;
1933 
1934 		f2fs_lock_op(sbi, &lc);
1935 		f2fs_drop_extent_tree(inode);
1936 
1937 		ret = __exchange_data_block(inode, inode, idx,
1938 					idx + delta, nr, false);
1939 		f2fs_unlock_op(sbi, &lc);
1940 	}
1941 out_unlock:
1942 	filemap_invalidate_unlock(mapping);
1943 	f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1944 	if (ret)
1945 		return ret;
1946 
1947 	/* write out all moved pages, if possible */
1948 	filemap_invalidate_lock(mapping);
1949 	ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1950 	truncate_pagecache(inode, offset);
1951 	filemap_invalidate_unlock(mapping);
1952 
1953 	if (!ret)
1954 		f2fs_i_size_write(inode, new_size);
1955 	return ret;
1956 }
1957 
1958 static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
1959 					loff_t len, int mode)
1960 {
1961 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1962 	struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1963 			.m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1964 			.m_may_create = true };
1965 	struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1966 			.init_gc_type = FG_GC,
1967 			.should_migrate_blocks = false,
1968 			.err_gc_skipped = true,
1969 			.nr_free_secs = 0 };
1970 	pgoff_t pg_start, pg_end;
1971 	loff_t new_size;
1972 	loff_t off_end;
1973 	block_t expanded = 0;
1974 	int err;
1975 
1976 	err = inode_newsize_ok(inode, (len + offset));
1977 	if (err)
1978 		return err;
1979 
1980 	err = f2fs_convert_inline_inode(inode);
1981 	if (err)
1982 		return err;
1983 
1984 	err = f2fs_zero_post_eof_page(inode, offset + len, true, true);
1985 	if (err)
1986 		return err;
1987 
1988 	f2fs_balance_fs(sbi, true);
1989 
1990 	pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1991 	pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1992 	off_end = (offset + len) & (PAGE_SIZE - 1);
1993 
1994 	map.m_lblk = pg_start;
1995 	map.m_len = pg_end - pg_start;
1996 	if (off_end)
1997 		map.m_len++;
1998 
1999 	if (!map.m_len)
2000 		return 0;
2001 
2002 	if (f2fs_is_pinned_file(inode)) {
2003 		block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
2004 		block_t sec_len;
2005 
2006 		if (map.m_lblk % sec_blks) {
2007 			pg_start = rounddown(map.m_lblk, sec_blks);
2008 			map.m_lblk = pg_start;
2009 			map.m_len = pg_end - pg_start;
2010 			if (off_end)
2011 				map.m_len++;
2012 		}
2013 		sec_len = roundup(map.m_len, sec_blks);
2014 		map.m_len = sec_blks;
2015 next_alloc:
2016 		f2fs_down_write(&sbi->pin_sem);
2017 
2018 		if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2019 			if (has_not_enough_free_secs(sbi, 0, 0)) {
2020 				f2fs_up_write(&sbi->pin_sem);
2021 				err = -ENOSPC;
2022 				f2fs_warn_ratelimited(sbi,
2023 					"ino:%llu, start:%lu, end:%lu, need to trigger GC to "
2024 					"reclaim enough free segment when checkpoint is enabled",
2025 					inode->i_ino, pg_start, pg_end);
2026 				goto out_err;
2027 			}
2028 		}
2029 
2030 		if (has_not_enough_free_secs(sbi, 0,
2031 				sbi->reserved_pin_section)) {
2032 			f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
2033 			stat_inc_gc_call_count(sbi, FOREGROUND);
2034 			err = f2fs_gc(sbi, &gc_control);
2035 			if (err && err != -ENODATA) {
2036 				f2fs_up_write(&sbi->pin_sem);
2037 				goto out_err;
2038 			}
2039 		}
2040 
2041 		err = f2fs_allocate_pinning_section(sbi);
2042 		if (err) {
2043 			f2fs_up_write(&sbi->pin_sem);
2044 			goto out_err;
2045 		}
2046 
2047 		map.m_seg_type = CURSEG_COLD_DATA_PINNED;
2048 		err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
2049 		file_dont_truncate(inode);
2050 
2051 		f2fs_up_write(&sbi->pin_sem);
2052 
2053 		expanded += map.m_len;
2054 		sec_len -= map.m_len;
2055 		map.m_lblk += map.m_len;
2056 		if (!err && sec_len)
2057 			goto next_alloc;
2058 
2059 		map.m_len = expanded;
2060 	} else {
2061 		err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
2062 		expanded = map.m_len;
2063 	}
2064 out_err:
2065 	if (err) {
2066 		pgoff_t last_off;
2067 
2068 		if (!expanded)
2069 			return err;
2070 
2071 		last_off = pg_start + expanded - 1;
2072 
2073 		/* update new size to the failed position */
2074 		new_size = (last_off == pg_end) ? offset + len :
2075 					(loff_t)(last_off + 1) << PAGE_SHIFT;
2076 	} else {
2077 		new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
2078 	}
2079 
2080 	if (new_size > i_size_read(inode)) {
2081 		if (mode & FALLOC_FL_KEEP_SIZE)
2082 			file_set_keep_isize(inode);
2083 		else
2084 			f2fs_i_size_write(inode, new_size);
2085 	}
2086 
2087 	return err;
2088 }
2089 
2090 static long f2fs_fallocate(struct file *file, int mode,
2091 				loff_t offset, loff_t len)
2092 {
2093 	struct inode *inode = file_inode(file);
2094 	long ret = 0;
2095 
2096 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2097 		return -EIO;
2098 	if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
2099 		return -ENOSPC;
2100 	if (!f2fs_is_compress_backend_ready(inode) || IS_DEVICE_ALIASING(inode))
2101 		return -EOPNOTSUPP;
2102 
2103 	/* f2fs only support ->fallocate for regular file */
2104 	if (!S_ISREG(inode->i_mode))
2105 		return -EINVAL;
2106 
2107 	if (IS_ENCRYPTED(inode) &&
2108 		(mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
2109 		return -EOPNOTSUPP;
2110 
2111 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
2112 			FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
2113 			FALLOC_FL_INSERT_RANGE))
2114 		return -EOPNOTSUPP;
2115 
2116 	inode_lock(inode);
2117 
2118 	/*
2119 	 * Pinned file should not support partial truncation since the block
2120 	 * can be used by applications. Atomic files should not either, since
2121 	 * these modify the original inode directly and break atomicity.
2122 	 */
2123 	if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode) ||
2124 	     f2fs_is_atomic_file(inode)) &&
2125 		(mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
2126 			FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
2127 		ret = -EOPNOTSUPP;
2128 		goto out;
2129 	}
2130 
2131 	ret = file_modified(file);
2132 	if (ret)
2133 		goto out;
2134 
2135 	/*
2136 	 * wait for inflight dio, blocks should be removed after IO
2137 	 * completion.
2138 	 */
2139 	inode_dio_wait(inode);
2140 
2141 	if (mode & FALLOC_FL_PUNCH_HOLE) {
2142 		if (offset >= inode->i_size)
2143 			goto out;
2144 
2145 		ret = f2fs_punch_hole(inode, offset, len);
2146 	} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
2147 		ret = f2fs_collapse_range(inode, offset, len);
2148 	} else if (mode & FALLOC_FL_ZERO_RANGE) {
2149 		ret = f2fs_zero_range(inode, offset, len, mode);
2150 	} else if (mode & FALLOC_FL_INSERT_RANGE) {
2151 		ret = f2fs_insert_range(inode, offset, len);
2152 	} else {
2153 		ret = f2fs_expand_inode_data(inode, offset, len, mode);
2154 	}
2155 
2156 	if (!ret) {
2157 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2158 		f2fs_mark_inode_dirty_sync(inode, false);
2159 		f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2160 	}
2161 
2162 out:
2163 	inode_unlock(inode);
2164 
2165 	trace_f2fs_fallocate(inode, mode, offset, len, ret);
2166 	return ret;
2167 }
2168 
2169 static int f2fs_release_file(struct inode *inode, struct file *filp)
2170 {
2171 	if (atomic_dec_and_test(&F2FS_I(inode)->open_count))
2172 		f2fs_remove_donate_inode(inode);
2173 
2174 	/*
2175 	 * f2fs_release_file is called at every close calls. So we should
2176 	 * not drop any inmemory pages by close called by other process.
2177 	 */
2178 	if (!(filp->f_mode & FMODE_WRITE) ||
2179 			atomic_read(&inode->i_writecount) != 1)
2180 		return 0;
2181 
2182 	inode_lock(inode);
2183 	f2fs_abort_atomic_write(inode, true);
2184 	inode_unlock(inode);
2185 
2186 	return 0;
2187 }
2188 
2189 static int f2fs_file_flush(struct file *file, fl_owner_t id)
2190 {
2191 	struct inode *inode = file_inode(file);
2192 
2193 	/*
2194 	 * If the process doing a transaction is crashed, we should do
2195 	 * roll-back. Otherwise, other reader/write can see corrupted database
2196 	 * until all the writers close its file. Since this should be done
2197 	 * before dropping file lock, it needs to do in ->flush.
2198 	 */
2199 	if (F2FS_I(inode)->atomic_write_task == current &&
2200 				(current->flags & PF_EXITING)) {
2201 		inode_lock(inode);
2202 		f2fs_abort_atomic_write(inode, true);
2203 		inode_unlock(inode);
2204 	}
2205 
2206 	return 0;
2207 }
2208 
2209 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
2210 {
2211 	struct f2fs_inode_info *fi = F2FS_I(inode);
2212 	u32 masked_flags = fi->i_flags & mask;
2213 
2214 	/* mask can be shrunk by flags_valid selector */
2215 	iflags &= mask;
2216 
2217 	/* Is it quota file? Do not allow user to mess with it */
2218 	if (IS_NOQUOTA(inode))
2219 		return -EPERM;
2220 
2221 	if (IS_DEVICE_ALIASING(inode))
2222 		return -EPERM;
2223 
2224 	if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
2225 		if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
2226 			return -EOPNOTSUPP;
2227 		if (!f2fs_empty_dir(inode))
2228 			return -ENOTEMPTY;
2229 	}
2230 
2231 	if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
2232 		if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
2233 			return -EOPNOTSUPP;
2234 		if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
2235 			return -EINVAL;
2236 	}
2237 
2238 	if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
2239 		if (masked_flags & F2FS_COMPR_FL) {
2240 			if (!f2fs_disable_compressed_file(inode))
2241 				return -EINVAL;
2242 		} else {
2243 			/* try to convert inline_data to support compression */
2244 			int err = f2fs_convert_inline_inode(inode);
2245 			if (err)
2246 				return err;
2247 
2248 			f2fs_down_write(&fi->i_sem);
2249 			if (!f2fs_may_compress(inode) ||
2250 				atomic_read(&fi->writeback) ||
2251 				(S_ISREG(inode->i_mode) &&
2252 				F2FS_HAS_BLOCKS(inode))) {
2253 				f2fs_up_write(&fi->i_sem);
2254 				return -EINVAL;
2255 			}
2256 			err = set_compress_context(inode);
2257 			f2fs_up_write(&fi->i_sem);
2258 
2259 			if (err)
2260 				return err;
2261 		}
2262 	}
2263 
2264 	fi->i_flags = iflags | (fi->i_flags & ~mask);
2265 	f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
2266 					(fi->i_flags & F2FS_NOCOMP_FL));
2267 
2268 	if (fi->i_flags & F2FS_PROJINHERIT_FL)
2269 		set_inode_flag(inode, FI_PROJ_INHERIT);
2270 	else
2271 		clear_inode_flag(inode, FI_PROJ_INHERIT);
2272 
2273 	inode_set_ctime_current(inode);
2274 	f2fs_set_inode_flags(inode);
2275 	f2fs_mark_inode_dirty_sync(inode, true);
2276 	return 0;
2277 }
2278 
2279 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
2280 
2281 /*
2282  * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
2283  * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
2284  * F2FS_GETTABLE_FS_FL.  To also make it settable via FS_IOC_SETFLAGS, also add
2285  * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
2286  *
2287  * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
2288  * FS_IOC_FSSETXATTR is done by the VFS.
2289  */
2290 
2291 static const struct {
2292 	u32 iflag;
2293 	u32 fsflag;
2294 } f2fs_fsflags_map[] = {
2295 	{ F2FS_COMPR_FL,	FS_COMPR_FL },
2296 	{ F2FS_SYNC_FL,		FS_SYNC_FL },
2297 	{ F2FS_IMMUTABLE_FL,	FS_IMMUTABLE_FL },
2298 	{ F2FS_APPEND_FL,	FS_APPEND_FL },
2299 	{ F2FS_NODUMP_FL,	FS_NODUMP_FL },
2300 	{ F2FS_NOATIME_FL,	FS_NOATIME_FL },
2301 	{ F2FS_NOCOMP_FL,	FS_NOCOMP_FL },
2302 	{ F2FS_INDEX_FL,	FS_INDEX_FL },
2303 	{ F2FS_DIRSYNC_FL,	FS_DIRSYNC_FL },
2304 	{ F2FS_PROJINHERIT_FL,	FS_PROJINHERIT_FL },
2305 	{ F2FS_CASEFOLD_FL,	FS_CASEFOLD_FL },
2306 };
2307 
2308 #define F2FS_GETTABLE_FS_FL (		\
2309 		FS_COMPR_FL |		\
2310 		FS_SYNC_FL |		\
2311 		FS_IMMUTABLE_FL |	\
2312 		FS_APPEND_FL |		\
2313 		FS_NODUMP_FL |		\
2314 		FS_NOATIME_FL |		\
2315 		FS_NOCOMP_FL |		\
2316 		FS_INDEX_FL |		\
2317 		FS_DIRSYNC_FL |		\
2318 		FS_PROJINHERIT_FL |	\
2319 		FS_ENCRYPT_FL |		\
2320 		FS_INLINE_DATA_FL |	\
2321 		FS_NOCOW_FL |		\
2322 		FS_VERITY_FL |		\
2323 		FS_CASEFOLD_FL)
2324 
2325 #define F2FS_SETTABLE_FS_FL (		\
2326 		FS_COMPR_FL |		\
2327 		FS_SYNC_FL |		\
2328 		FS_IMMUTABLE_FL |	\
2329 		FS_APPEND_FL |		\
2330 		FS_NODUMP_FL |		\
2331 		FS_NOATIME_FL |		\
2332 		FS_NOCOMP_FL |		\
2333 		FS_DIRSYNC_FL |		\
2334 		FS_PROJINHERIT_FL |	\
2335 		FS_CASEFOLD_FL)
2336 
2337 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2338 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2339 {
2340 	u32 fsflags = 0;
2341 	int i;
2342 
2343 	for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2344 		if (iflags & f2fs_fsflags_map[i].iflag)
2345 			fsflags |= f2fs_fsflags_map[i].fsflag;
2346 
2347 	return fsflags;
2348 }
2349 
2350 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2351 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2352 {
2353 	u32 iflags = 0;
2354 	int i;
2355 
2356 	for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2357 		if (fsflags & f2fs_fsflags_map[i].fsflag)
2358 			iflags |= f2fs_fsflags_map[i].iflag;
2359 
2360 	return iflags;
2361 }
2362 
2363 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2364 {
2365 	struct inode *inode = file_inode(filp);
2366 
2367 	return put_user(inode->i_generation, (int __user *)arg);
2368 }
2369 
2370 static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
2371 {
2372 	struct inode *inode = file_inode(filp);
2373 	const struct mnt_idmap *idmap = file_mnt_idmap(filp);
2374 	struct f2fs_inode_info *fi = F2FS_I(inode);
2375 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2376 	loff_t isize;
2377 	int ret;
2378 
2379 	if (!(filp->f_mode & FMODE_WRITE))
2380 		return -EBADF;
2381 
2382 	if (!inode_owner_or_capable(idmap, inode))
2383 		return -EACCES;
2384 
2385 	if (!S_ISREG(inode->i_mode))
2386 		return -EINVAL;
2387 
2388 	if (filp->f_flags & O_DIRECT)
2389 		return -EINVAL;
2390 
2391 	ret = mnt_want_write_file(filp);
2392 	if (ret)
2393 		return ret;
2394 
2395 	inode_lock(inode);
2396 
2397 	if (!f2fs_disable_compressed_file(inode) ||
2398 			f2fs_is_pinned_file(inode)) {
2399 		ret = -EINVAL;
2400 		goto out;
2401 	}
2402 
2403 	if (f2fs_is_atomic_file(inode))
2404 		goto out;
2405 
2406 	ret = f2fs_convert_inline_inode(inode);
2407 	if (ret)
2408 		goto out;
2409 
2410 	f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2411 	f2fs_down_write(&fi->i_gc_rwsem[READ]);
2412 	/* serialize with f2fs_vm_page_mkwrite(), see the comment there */
2413 	filemap_invalidate_lock(inode->i_mapping);
2414 
2415 	/*
2416 	 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2417 	 * f2fs_is_atomic_file.
2418 	 */
2419 	if (get_dirty_pages(inode))
2420 		f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%llu, npages=%u",
2421 			  inode->i_ino, get_dirty_pages(inode));
2422 	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2423 	if (ret)
2424 		goto out_unlock;
2425 
2426 	/* Check if the inode already has a COW inode */
2427 	if (fi->cow_inode == NULL) {
2428 		/* Create a COW inode for atomic write */
2429 		struct dentry *dentry = file_dentry(filp);
2430 		struct inode *dir = d_inode(dentry->d_parent);
2431 
2432 		ret = f2fs_get_tmpfile(idmap, dir, &fi->cow_inode);
2433 		if (ret)
2434 			goto out_unlock;
2435 
2436 		set_inode_flag(fi->cow_inode, FI_COW_FILE);
2437 		clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2438 
2439 		/* Set the COW inode's atomic_inode to the atomic inode */
2440 		F2FS_I(fi->cow_inode)->atomic_inode = inode;
2441 	} else {
2442 		/* Reuse the already created COW inode */
2443 		f2fs_bug_on(sbi, get_dirty_pages(fi->cow_inode));
2444 
2445 		invalidate_mapping_pages(fi->cow_inode->i_mapping, 0, -1);
2446 
2447 		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2448 		if (ret)
2449 			goto out_unlock;
2450 	}
2451 
2452 	f2fs_write_inode(inode, NULL);
2453 
2454 	stat_inc_atomic_inode(inode);
2455 
2456 	set_inode_flag(inode, FI_ATOMIC_FILE);
2457 
2458 	isize = i_size_read(inode);
2459 	fi->original_i_size = isize;
2460 	if (truncate) {
2461 		set_inode_flag(inode, FI_ATOMIC_REPLACE);
2462 		truncate_inode_pages(inode->i_mapping, 0);
2463 		f2fs_i_size_write(inode, 0);
2464 		isize = 0;
2465 	}
2466 	f2fs_i_size_write(fi->cow_inode, isize);
2467 
2468 out_unlock:
2469 	filemap_invalidate_unlock(inode->i_mapping);
2470 	f2fs_up_write(&fi->i_gc_rwsem[READ]);
2471 	f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2472 	if (ret)
2473 		goto out;
2474 
2475 	f2fs_update_time(sbi, REQ_TIME);
2476 	fi->atomic_write_task = current;
2477 	stat_update_max_atomic_write(inode);
2478 	fi->atomic_write_cnt = 0;
2479 out:
2480 	inode_unlock(inode);
2481 	mnt_drop_write_file(filp);
2482 	return ret;
2483 }
2484 
2485 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2486 {
2487 	struct inode *inode = file_inode(filp);
2488 	const struct mnt_idmap *idmap = file_mnt_idmap(filp);
2489 	int ret;
2490 
2491 	if (!(filp->f_mode & FMODE_WRITE))
2492 		return -EBADF;
2493 
2494 	if (!inode_owner_or_capable(idmap, inode))
2495 		return -EACCES;
2496 
2497 	ret = mnt_want_write_file(filp);
2498 	if (ret)
2499 		return ret;
2500 
2501 	f2fs_balance_fs(F2FS_I_SB(inode), true);
2502 
2503 	inode_lock(inode);
2504 
2505 	if (f2fs_is_atomic_file(inode)) {
2506 		ret = f2fs_commit_atomic_write(inode);
2507 		if (!ret)
2508 			ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2509 
2510 		f2fs_abort_atomic_write(inode, ret);
2511 	} else {
2512 		ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2513 	}
2514 
2515 	inode_unlock(inode);
2516 	mnt_drop_write_file(filp);
2517 	return ret;
2518 }
2519 
2520 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2521 {
2522 	struct inode *inode = file_inode(filp);
2523 	const struct mnt_idmap *idmap = file_mnt_idmap(filp);
2524 	int ret;
2525 
2526 	if (!(filp->f_mode & FMODE_WRITE))
2527 		return -EBADF;
2528 
2529 	if (!inode_owner_or_capable(idmap, inode))
2530 		return -EACCES;
2531 
2532 	ret = mnt_want_write_file(filp);
2533 	if (ret)
2534 		return ret;
2535 
2536 	inode_lock(inode);
2537 
2538 	f2fs_abort_atomic_write(inode, true);
2539 
2540 	inode_unlock(inode);
2541 
2542 	mnt_drop_write_file(filp);
2543 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2544 	return ret;
2545 }
2546 
2547 int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
2548 						bool readonly, bool need_lock)
2549 {
2550 	struct super_block *sb = sbi->sb;
2551 	int ret = 0;
2552 
2553 	switch (flag) {
2554 	case F2FS_GOING_DOWN_FULLSYNC:
2555 		ret = bdev_freeze(sb->s_bdev);
2556 		if (ret)
2557 			goto out;
2558 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2559 		bdev_thaw(sb->s_bdev);
2560 		break;
2561 	case F2FS_GOING_DOWN_METASYNC:
2562 		/* do checkpoint only */
2563 		ret = f2fs_sync_fs(sb, 1);
2564 		if (ret) {
2565 			if (ret == -EIO)
2566 				ret = 0;
2567 			goto out;
2568 		}
2569 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2570 		break;
2571 	case F2FS_GOING_DOWN_NOSYNC:
2572 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2573 		break;
2574 	case F2FS_GOING_DOWN_METAFLUSH:
2575 		f2fs_sync_meta_caches(sbi, LONG_MAX, true, FS_META_IO);
2576 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2577 		break;
2578 	case F2FS_GOING_DOWN_NEED_FSCK:
2579 		set_sbi_flag(sbi, SBI_NEED_FSCK);
2580 		set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2581 		set_sbi_flag(sbi, SBI_IS_DIRTY);
2582 		/* do checkpoint only */
2583 		ret = f2fs_sync_fs(sb, 1);
2584 		if (ret == -EIO)
2585 			ret = 0;
2586 		goto out;
2587 	default:
2588 		ret = -EINVAL;
2589 		goto out;
2590 	}
2591 
2592 	if (readonly)
2593 		goto out;
2594 
2595 	/*
2596 	 * grab sb->s_umount to avoid racing w/ remount() and other shutdown
2597 	 * paths.
2598 	 */
2599 	if (need_lock)
2600 		down_write(&sbi->sb->s_umount);
2601 
2602 	f2fs_stop_gc_thread(sbi);
2603 	f2fs_stop_discard_thread(sbi);
2604 
2605 	f2fs_drop_discard_cmd(sbi);
2606 	clear_opt(sbi, DISCARD);
2607 
2608 	if (need_lock)
2609 		up_write(&sbi->sb->s_umount);
2610 
2611 	f2fs_update_time(sbi, REQ_TIME);
2612 out:
2613 
2614 	trace_f2fs_shutdown(sbi, flag, ret);
2615 
2616 	return ret;
2617 }
2618 
2619 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2620 {
2621 	struct inode *inode = file_inode(filp);
2622 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2623 	__u32 in;
2624 	int ret;
2625 	bool need_drop = false, readonly = false;
2626 
2627 	if (!capable(CAP_SYS_ADMIN))
2628 		return -EPERM;
2629 
2630 	if (get_user(in, (__u32 __user *)arg))
2631 		return -EFAULT;
2632 
2633 	if (in != F2FS_GOING_DOWN_FULLSYNC) {
2634 		ret = mnt_want_write_file(filp);
2635 		if (ret) {
2636 			if (ret != -EROFS)
2637 				return ret;
2638 
2639 			/* fallback to nosync shutdown for readonly fs */
2640 			in = F2FS_GOING_DOWN_NOSYNC;
2641 			readonly = true;
2642 		} else {
2643 			need_drop = true;
2644 		}
2645 	}
2646 
2647 	ret = f2fs_do_shutdown(sbi, in, readonly, true);
2648 
2649 	if (need_drop)
2650 		mnt_drop_write_file(filp);
2651 
2652 	return ret;
2653 }
2654 
2655 static int f2fs_keep_noreuse_range(struct inode *inode,
2656 				loff_t offset, loff_t len)
2657 {
2658 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2659 	u64 max_bytes = F2FS_BLK_TO_BYTES(sbi,
2660 					max_file_blocks(sbi, inode));
2661 	u64 start, end;
2662 	int ret = 0;
2663 
2664 	if (!S_ISREG(inode->i_mode))
2665 		return 0;
2666 
2667 	if (offset >= max_bytes || len > max_bytes ||
2668 	    (offset + len) > max_bytes)
2669 		return 0;
2670 
2671 	start = offset >> PAGE_SHIFT;
2672 	end = DIV_ROUND_UP(offset + len, PAGE_SIZE);
2673 
2674 	inode_lock(inode);
2675 	if (f2fs_is_atomic_file(inode)) {
2676 		inode_unlock(inode);
2677 		return 0;
2678 	}
2679 
2680 	spin_lock(&sbi->inode_lock[DONATE_INODE]);
2681 	/* let's remove the range, if len = 0 */
2682 	if (!len) {
2683 		if (!list_empty(&F2FS_I(inode)->gdonate_list)) {
2684 			list_del_init(&F2FS_I(inode)->gdonate_list);
2685 			sbi->donate_files--;
2686 			if (is_inode_flag_set(inode, FI_DONATE_FINISHED))
2687 				ret = -EALREADY;
2688 			else
2689 				set_inode_flag(inode, FI_DONATE_FINISHED);
2690 		} else
2691 			ret = -ENOENT;
2692 	} else {
2693 		if (list_empty(&F2FS_I(inode)->gdonate_list)) {
2694 			list_add_tail(&F2FS_I(inode)->gdonate_list,
2695 					&sbi->inode_list[DONATE_INODE]);
2696 			sbi->donate_files++;
2697 		} else {
2698 			list_move_tail(&F2FS_I(inode)->gdonate_list,
2699 					&sbi->inode_list[DONATE_INODE]);
2700 		}
2701 		F2FS_I(inode)->donate_start = start;
2702 		F2FS_I(inode)->donate_end = end - 1;
2703 		clear_inode_flag(inode, FI_DONATE_FINISHED);
2704 	}
2705 	spin_unlock(&sbi->inode_lock[DONATE_INODE]);
2706 	inode_unlock(inode);
2707 
2708 	return ret;
2709 }
2710 
2711 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2712 {
2713 	struct inode *inode = file_inode(filp);
2714 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2715 	struct fstrim_range range;
2716 	int ret;
2717 
2718 	if (!capable(CAP_SYS_ADMIN))
2719 		return -EPERM;
2720 
2721 	if (!f2fs_hw_support_discard(sbi))
2722 		return -EOPNOTSUPP;
2723 
2724 	if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2725 				sizeof(range)))
2726 		return -EFAULT;
2727 
2728 	ret = mnt_want_write_file(filp);
2729 	if (ret)
2730 		return ret;
2731 
2732 	range.minlen = max_t(unsigned int, range.minlen,
2733 			f2fs_hw_discard_granularity(sbi));
2734 	ret = f2fs_trim_fs(sbi, &range);
2735 	mnt_drop_write_file(filp);
2736 	if (ret < 0)
2737 		return ret;
2738 
2739 	if (copy_to_user((struct fstrim_range __user *)arg, &range,
2740 				sizeof(range)))
2741 		return -EFAULT;
2742 	f2fs_update_time(sbi, REQ_TIME);
2743 	return 0;
2744 }
2745 
2746 static bool uuid_is_nonzero(__u8 u[16])
2747 {
2748 	int i;
2749 
2750 	for (i = 0; i < 16; i++)
2751 		if (u[i])
2752 			return true;
2753 	return false;
2754 }
2755 
2756 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2757 {
2758 	struct inode *inode = file_inode(filp);
2759 	int ret;
2760 
2761 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2762 		return -EOPNOTSUPP;
2763 
2764 	ret = fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2765 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2766 	return ret;
2767 }
2768 
2769 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2770 {
2771 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2772 		return -EOPNOTSUPP;
2773 	return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2774 }
2775 
2776 static int f2fs_ioc_get_dev_alias_status(struct file *filp, unsigned long arg)
2777 {
2778 	struct inode *inode = file_inode(filp);
2779 
2780 	if (!IS_DEVICE_ALIASING(inode))
2781 		return -EINVAL;
2782 
2783 	return put_user(F2FS_HAS_BLOCKS(inode) ? F2FS_DEV_ALIAS_STATUS_RESERVED :
2784 				F2FS_DEV_ALIAS_STATUS_RELEASED, (u32 __user *)arg);
2785 }
2786 
2787 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2788 {
2789 	struct inode *inode = file_inode(filp);
2790 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2791 	u8 encrypt_pw_salt[16];
2792 	int err;
2793 
2794 	if (!f2fs_sb_has_encrypt(sbi))
2795 		return -EOPNOTSUPP;
2796 
2797 	err = mnt_want_write_file(filp);
2798 	if (err)
2799 		return err;
2800 
2801 	f2fs_down_write(&sbi->sb_lock);
2802 
2803 	if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2804 		goto got_it;
2805 
2806 	/* update superblock with uuid */
2807 	generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2808 
2809 	err = f2fs_commit_super(sbi, false);
2810 	if (err) {
2811 		/* undo new data */
2812 		memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2813 		goto out_err;
2814 	}
2815 got_it:
2816 	memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2817 out_err:
2818 	f2fs_up_write(&sbi->sb_lock);
2819 	mnt_drop_write_file(filp);
2820 
2821 	if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2822 		err = -EFAULT;
2823 
2824 	return err;
2825 }
2826 
2827 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2828 					     unsigned long arg)
2829 {
2830 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2831 		return -EOPNOTSUPP;
2832 
2833 	return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2834 }
2835 
2836 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2837 {
2838 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2839 		return -EOPNOTSUPP;
2840 
2841 	return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2842 }
2843 
2844 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2845 {
2846 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2847 		return -EOPNOTSUPP;
2848 
2849 	return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2850 }
2851 
2852 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2853 						    unsigned long arg)
2854 {
2855 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2856 		return -EOPNOTSUPP;
2857 
2858 	return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2859 }
2860 
2861 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2862 					      unsigned long arg)
2863 {
2864 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2865 		return -EOPNOTSUPP;
2866 
2867 	return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2868 }
2869 
2870 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2871 {
2872 	if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2873 		return -EOPNOTSUPP;
2874 
2875 	return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2876 }
2877 
2878 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2879 {
2880 	struct inode *inode = file_inode(filp);
2881 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2882 	struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2883 			.no_bg_gc = false,
2884 			.should_migrate_blocks = false,
2885 			.nr_free_secs = 0 };
2886 	__u32 sync;
2887 	int ret;
2888 
2889 	if (!capable(CAP_SYS_ADMIN))
2890 		return -EPERM;
2891 
2892 	if (get_user(sync, (__u32 __user *)arg))
2893 		return -EFAULT;
2894 
2895 	if (f2fs_readonly(sbi->sb))
2896 		return -EROFS;
2897 
2898 	ret = mnt_want_write_file(filp);
2899 	if (ret)
2900 		return ret;
2901 
2902 	if (!sync) {
2903 		if (!f2fs_down_write_trylock_trace(&sbi->gc_lock,
2904 						&gc_control.lc)) {
2905 			ret = -EBUSY;
2906 			goto out;
2907 		}
2908 	} else {
2909 		f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
2910 	}
2911 
2912 	gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2913 	gc_control.err_gc_skipped = sync;
2914 	stat_inc_gc_call_count(sbi, FOREGROUND);
2915 	ret = f2fs_gc(sbi, &gc_control);
2916 out:
2917 	mnt_drop_write_file(filp);
2918 	return ret;
2919 }
2920 
2921 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2922 {
2923 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2924 	struct f2fs_gc_control gc_control = {
2925 			.init_gc_type = range->sync ? FG_GC : BG_GC,
2926 			.no_bg_gc = false,
2927 			.should_migrate_blocks = false,
2928 			.err_gc_skipped = range->sync,
2929 			.nr_free_secs = 0 };
2930 	u64 end;
2931 	int ret;
2932 
2933 	if (!capable(CAP_SYS_ADMIN))
2934 		return -EPERM;
2935 	if (f2fs_readonly(sbi->sb))
2936 		return -EROFS;
2937 
2938 	end = range->start + range->len;
2939 	if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2940 					end >= MAX_BLKADDR(sbi))
2941 		return -EINVAL;
2942 
2943 	ret = mnt_want_write_file(filp);
2944 	if (ret)
2945 		return ret;
2946 
2947 do_more:
2948 	if (!range->sync) {
2949 		if (!f2fs_down_write_trylock_trace(&sbi->gc_lock, &gc_control.lc)) {
2950 			ret = -EBUSY;
2951 			goto out;
2952 		}
2953 	} else {
2954 		f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
2955 	}
2956 
2957 	gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2958 	stat_inc_gc_call_count(sbi, FOREGROUND);
2959 	ret = f2fs_gc(sbi, &gc_control);
2960 	if (ret) {
2961 		if (ret == -EBUSY)
2962 			ret = -EAGAIN;
2963 		goto out;
2964 	}
2965 	range->start += CAP_BLKS_PER_SEC(sbi);
2966 	if (range->start <= end)
2967 		goto do_more;
2968 out:
2969 	mnt_drop_write_file(filp);
2970 	return ret;
2971 }
2972 
2973 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2974 {
2975 	struct f2fs_gc_range range;
2976 
2977 	if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2978 							sizeof(range)))
2979 		return -EFAULT;
2980 	return __f2fs_ioc_gc_range(filp, &range);
2981 }
2982 
2983 static int f2fs_ioc_write_checkpoint(struct file *filp)
2984 {
2985 	struct inode *inode = file_inode(filp);
2986 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2987 	int ret;
2988 
2989 	if (!capable(CAP_SYS_ADMIN))
2990 		return -EPERM;
2991 
2992 	if (f2fs_readonly(sbi->sb))
2993 		return -EROFS;
2994 
2995 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2996 		f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2997 		return -EINVAL;
2998 	}
2999 
3000 	ret = mnt_want_write_file(filp);
3001 	if (ret)
3002 		return ret;
3003 
3004 	ret = f2fs_sync_fs(sbi->sb, 1);
3005 
3006 	mnt_drop_write_file(filp);
3007 	return ret;
3008 }
3009 
3010 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
3011 					struct file *filp,
3012 					struct f2fs_defragment *range)
3013 {
3014 	struct inode *inode = file_inode(filp);
3015 	struct f2fs_map_blocks map = { .m_next_extent = NULL,
3016 					.m_seg_type = NO_CHECK_TYPE,
3017 					.m_may_create = false };
3018 	struct extent_info ei = {};
3019 	pgoff_t pg_start, pg_end, next_pgofs;
3020 	unsigned int total = 0, sec_num;
3021 	block_t blk_end = 0;
3022 	bool fragmented = false;
3023 	int err;
3024 
3025 	f2fs_balance_fs(sbi, true);
3026 
3027 	inode_lock(inode);
3028 	pg_start = range->start >> PAGE_SHIFT;
3029 	pg_end = min_t(pgoff_t,
3030 				(range->start + range->len) >> PAGE_SHIFT,
3031 				DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE));
3032 
3033 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) ||
3034 		f2fs_is_atomic_file(inode)) {
3035 		err = -EINVAL;
3036 		goto unlock_out;
3037 	}
3038 
3039 	/* if in-place-update policy is enabled, don't waste time here */
3040 	set_inode_flag(inode, FI_OPU_WRITE);
3041 	if (f2fs_should_update_inplace(inode, NULL)) {
3042 		err = -EINVAL;
3043 		goto out;
3044 	}
3045 
3046 	/* writeback all dirty pages in the range */
3047 	err = filemap_write_and_wait_range(inode->i_mapping,
3048 						(loff_t)pg_start << PAGE_SHIFT,
3049 						((loff_t)pg_end << PAGE_SHIFT) - 1);
3050 	if (err)
3051 		goto out;
3052 
3053 	/*
3054 	 * lookup mapping info in extent cache, skip defragmenting if physical
3055 	 * block addresses are continuous.
3056 	 */
3057 	if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
3058 		if ((pgoff_t)ei.fofs + ei.len >= pg_end)
3059 			goto out;
3060 	}
3061 
3062 	map.m_lblk = pg_start;
3063 	map.m_next_pgofs = &next_pgofs;
3064 
3065 	/*
3066 	 * lookup mapping info in dnode page cache, skip defragmenting if all
3067 	 * physical block addresses are continuous even if there are hole(s)
3068 	 * in logical blocks.
3069 	 */
3070 	while (map.m_lblk < pg_end) {
3071 		map.m_len = pg_end - map.m_lblk;
3072 		err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
3073 		if (err)
3074 			goto out;
3075 
3076 		if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3077 			map.m_lblk = next_pgofs;
3078 			continue;
3079 		}
3080 
3081 		if (blk_end && blk_end != map.m_pblk)
3082 			fragmented = true;
3083 
3084 		/* record total count of block that we're going to move */
3085 		total += map.m_len;
3086 
3087 		blk_end = map.m_pblk + map.m_len;
3088 
3089 		map.m_lblk += map.m_len;
3090 	}
3091 
3092 	if (!fragmented) {
3093 		total = 0;
3094 		goto out;
3095 	}
3096 
3097 	sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
3098 
3099 	/*
3100 	 * make sure there are enough free section for LFS allocation, this can
3101 	 * avoid defragment running in SSR mode when free section are allocated
3102 	 * intensively
3103 	 */
3104 	if (has_not_enough_free_secs(sbi, 0, sec_num)) {
3105 		err = -EAGAIN;
3106 		goto out;
3107 	}
3108 
3109 	map.m_lblk = pg_start;
3110 	map.m_len = pg_end - pg_start;
3111 	total = 0;
3112 
3113 	while (map.m_lblk < pg_end) {
3114 		pgoff_t idx;
3115 		int cnt = 0;
3116 
3117 do_map:
3118 		map.m_len = pg_end - map.m_lblk;
3119 		err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
3120 		if (err)
3121 			goto clear_out;
3122 
3123 		if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3124 			map.m_lblk = next_pgofs;
3125 			goto check;
3126 		}
3127 
3128 		set_inode_flag(inode, FI_SKIP_WRITES);
3129 
3130 		idx = map.m_lblk;
3131 		while (idx < map.m_lblk + map.m_len &&
3132 						cnt < BLKS_PER_SEG(sbi)) {
3133 			struct folio *folio;
3134 
3135 			folio = f2fs_get_lock_data_folio(inode, idx, true);
3136 			if (IS_ERR(folio)) {
3137 				err = PTR_ERR(folio);
3138 				goto clear_out;
3139 			}
3140 
3141 			f2fs_folio_wait_writeback(folio, true, true);
3142 
3143 			folio_mark_dirty(folio);
3144 			folio_set_f2fs_gcing(folio);
3145 			f2fs_folio_put(folio, true);
3146 
3147 			idx++;
3148 			cnt++;
3149 			total++;
3150 		}
3151 
3152 		map.m_lblk = idx;
3153 check:
3154 		if (map.m_lblk < pg_end && cnt < BLKS_PER_SEG(sbi))
3155 			goto do_map;
3156 
3157 		clear_inode_flag(inode, FI_SKIP_WRITES);
3158 
3159 		err = filemap_fdatawrite(inode->i_mapping);
3160 		if (err)
3161 			goto out;
3162 	}
3163 clear_out:
3164 	clear_inode_flag(inode, FI_SKIP_WRITES);
3165 out:
3166 	clear_inode_flag(inode, FI_OPU_WRITE);
3167 unlock_out:
3168 	inode_unlock(inode);
3169 	if (!err) {
3170 		range->len = (u64)total << PAGE_SHIFT;
3171 		stat_inc_defrag_blk_count(sbi, total);
3172 	}
3173 	return err;
3174 }
3175 
3176 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
3177 {
3178 	struct inode *inode = file_inode(filp);
3179 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3180 	struct f2fs_defragment range;
3181 	int err;
3182 
3183 	if (!capable(CAP_SYS_ADMIN))
3184 		return -EPERM;
3185 
3186 	if (!S_ISREG(inode->i_mode))
3187 		return -EINVAL;
3188 
3189 	if (f2fs_readonly(sbi->sb))
3190 		return -EROFS;
3191 
3192 	if (IS_DEVICE_ALIASING(inode))
3193 		return -EOPNOTSUPP;
3194 
3195 	if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
3196 							sizeof(range)))
3197 		return -EFAULT;
3198 
3199 	/* verify alignment of offset & size */
3200 	if (range.start & F2FS_BLKSIZE_MASK(sbi) ||
3201 	    range.len & F2FS_BLKSIZE_MASK(sbi))
3202 		return -EINVAL;
3203 
3204 	if (unlikely(F2FS_BYTES_TO_BLK(sbi, range.start + range.len) >
3205 					max_file_blocks(sbi, inode)))
3206 		return -EINVAL;
3207 
3208 	err = mnt_want_write_file(filp);
3209 	if (err)
3210 		return err;
3211 
3212 	err = f2fs_defragment_range(sbi, filp, &range);
3213 	mnt_drop_write_file(filp);
3214 
3215 	if (range.len)
3216 		f2fs_update_time(sbi, REQ_TIME);
3217 	if (err < 0)
3218 		return err;
3219 
3220 	if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
3221 							sizeof(range)))
3222 		return -EFAULT;
3223 
3224 	return 0;
3225 }
3226 
3227 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
3228 			struct file *file_out, loff_t pos_out, size_t len)
3229 {
3230 	struct inode *src = file_inode(file_in);
3231 	struct inode *dst = file_inode(file_out);
3232 	struct f2fs_sb_info *sbi = F2FS_I_SB(src);
3233 	struct f2fs_lock_context lc;
3234 	size_t olen = len;
3235 	loff_t dst_max_i_size = 0;
3236 	loff_t dst_osize, dst_end;
3237 	int ret;
3238 
3239 	if (file_in->f_path.mnt != file_out->f_path.mnt ||
3240 				src->i_sb != dst->i_sb)
3241 		return -EXDEV;
3242 
3243 	if (unlikely(f2fs_readonly(src->i_sb)))
3244 		return -EROFS;
3245 
3246 	if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
3247 		return -EINVAL;
3248 
3249 	if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst) ||
3250 		IS_DEVICE_ALIASING(src) || IS_DEVICE_ALIASING(dst))
3251 		return -EOPNOTSUPP;
3252 
3253 	if (pos_out < 0 || pos_in < 0)
3254 		return -EINVAL;
3255 
3256 	if (src == dst) {
3257 		if (pos_in == pos_out)
3258 			return 0;
3259 	}
3260 
3261 	inode_lock(src);
3262 	if (src != dst) {
3263 		ret = -EBUSY;
3264 		if (!inode_trylock(dst))
3265 			goto out;
3266 	}
3267 
3268 	if (f2fs_compressed_file(src) || f2fs_compressed_file(dst) ||
3269 		f2fs_is_pinned_file(src) || f2fs_is_pinned_file(dst)) {
3270 		ret = -EOPNOTSUPP;
3271 		goto out_unlock;
3272 	}
3273 
3274 	if (f2fs_is_atomic_file(src) || f2fs_is_atomic_file(dst)) {
3275 		ret = -EINVAL;
3276 		goto out_unlock;
3277 	}
3278 
3279 	ret = -EINVAL;
3280 	if (pos_in + len > src->i_size || pos_in + len < pos_in)
3281 		goto out_unlock;
3282 	if (len == 0)
3283 		olen = len = src->i_size - pos_in;
3284 	if (src == dst && pos_out > pos_in && pos_out < pos_in + len)
3285 		goto out_unlock;
3286 	if (pos_in + len == src->i_size)
3287 		len = ALIGN(src->i_size, F2FS_BLKSIZE(sbi)) - pos_in;
3288 	if (len == 0) {
3289 		ret = 0;
3290 		goto out_unlock;
3291 	}
3292 
3293 	dst_osize = dst->i_size;
3294 	if (olen > LLONG_MAX - pos_out)
3295 		goto out_unlock;
3296 	dst_end = pos_out + olen;
3297 	if (dst_end > dst->i_size) {
3298 		ret = inode_newsize_ok(dst, dst_end);
3299 		if (ret)
3300 			goto out_unlock;
3301 		dst_max_i_size = dst_end;
3302 	}
3303 
3304 	/* verify the end result is block aligned */
3305 	if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE(sbi)) ||
3306 			!IS_ALIGNED(pos_in + len, F2FS_BLKSIZE(sbi)) ||
3307 			!IS_ALIGNED(pos_out, F2FS_BLKSIZE(sbi)))
3308 		goto out_unlock;
3309 
3310 	ret = f2fs_convert_inline_inode(src);
3311 	if (ret)
3312 		goto out_unlock;
3313 
3314 	ret = f2fs_convert_inline_inode(dst);
3315 	if (ret)
3316 		goto out_unlock;
3317 
3318 	/* write out all dirty pages from offset */
3319 	ret = filemap_write_and_wait_range(src->i_mapping,
3320 					pos_in, pos_in + len);
3321 	if (ret)
3322 		goto out_unlock;
3323 
3324 	ret = filemap_write_and_wait_range(dst->i_mapping,
3325 					pos_out, pos_out + len);
3326 	if (ret)
3327 		goto out_unlock;
3328 
3329 	f2fs_balance_fs(sbi, true);
3330 
3331 	f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
3332 	if (src != dst) {
3333 		ret = -EBUSY;
3334 		if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
3335 			goto out_src;
3336 	}
3337 
3338 	f2fs_lock_op(sbi, &lc);
3339 	ret = __exchange_data_block(src, dst, F2FS_BYTES_TO_BLK(sbi, pos_in),
3340 				F2FS_BYTES_TO_BLK(sbi, pos_out),
3341 				F2FS_BYTES_TO_BLK(sbi, len), false);
3342 
3343 	if (!ret) {
3344 		if (dst_max_i_size)
3345 			f2fs_i_size_write(dst, dst_max_i_size);
3346 		else if (dst_osize != dst->i_size)
3347 			f2fs_i_size_write(dst, dst_osize);
3348 	}
3349 	f2fs_unlock_op(sbi, &lc);
3350 
3351 	if (src != dst)
3352 		f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
3353 out_src:
3354 	f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
3355 	if (ret)
3356 		goto out_unlock;
3357 
3358 	inode_set_mtime_to_ts(src, inode_set_ctime_current(src));
3359 	f2fs_mark_inode_dirty_sync(src, false);
3360 	if (src != dst) {
3361 		inode_set_mtime_to_ts(dst, inode_set_ctime_current(dst));
3362 		f2fs_mark_inode_dirty_sync(dst, false);
3363 	}
3364 	f2fs_update_time(sbi, REQ_TIME);
3365 
3366 out_unlock:
3367 	if (src != dst)
3368 		inode_unlock(dst);
3369 out:
3370 	inode_unlock(src);
3371 	return ret;
3372 }
3373 
3374 static int __f2fs_ioc_move_range(struct file *filp,
3375 				struct f2fs_move_range *range)
3376 {
3377 	int err;
3378 
3379 	if (!(filp->f_mode & FMODE_READ) ||
3380 			!(filp->f_mode & FMODE_WRITE))
3381 		return -EBADF;
3382 
3383 	CLASS(fd, dst)(range->dst_fd);
3384 	if (fd_empty(dst))
3385 		return -EBADF;
3386 
3387 	if (!(fd_file(dst)->f_mode & FMODE_WRITE))
3388 		return -EBADF;
3389 
3390 	err = mnt_want_write_file(filp);
3391 	if (err)
3392 		return err;
3393 
3394 	err = f2fs_move_file_range(filp, range->pos_in, fd_file(dst),
3395 					range->pos_out, range->len);
3396 
3397 	mnt_drop_write_file(filp);
3398 	return err;
3399 }
3400 
3401 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
3402 {
3403 	struct f2fs_move_range range;
3404 
3405 	if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
3406 							sizeof(range)))
3407 		return -EFAULT;
3408 	return __f2fs_ioc_move_range(filp, &range);
3409 }
3410 
3411 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
3412 {
3413 	struct inode *inode = file_inode(filp);
3414 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3415 	struct sit_info *sm = SIT_I(sbi);
3416 	unsigned int start_segno = 0, end_segno = 0;
3417 	unsigned int dev_start_segno = 0, dev_end_segno = 0;
3418 	struct f2fs_flush_device range;
3419 	struct f2fs_gc_control gc_control = {
3420 			.init_gc_type = FG_GC,
3421 			.should_migrate_blocks = true,
3422 			.err_gc_skipped = true,
3423 			.nr_free_secs = 0 };
3424 	int ret;
3425 
3426 	if (!capable(CAP_SYS_ADMIN))
3427 		return -EPERM;
3428 
3429 	if (f2fs_readonly(sbi->sb))
3430 		return -EROFS;
3431 
3432 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
3433 		return -EINVAL;
3434 
3435 	if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
3436 							sizeof(range)))
3437 		return -EFAULT;
3438 
3439 	if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
3440 			__is_large_section(sbi)) {
3441 		f2fs_warn(sbi, "Can't flush %u in %d for SEGS_PER_SEC %u != 1",
3442 			  range.dev_num, sbi->s_ndevs, SEGS_PER_SEC(sbi));
3443 		return -EINVAL;
3444 	}
3445 
3446 	ret = mnt_want_write_file(filp);
3447 	if (ret)
3448 		return ret;
3449 
3450 	if (range.dev_num != 0)
3451 		dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
3452 	dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
3453 
3454 	start_segno = sm->last_victim[FLUSH_DEVICE];
3455 	if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
3456 		start_segno = dev_start_segno;
3457 	end_segno = min(start_segno + range.segments, dev_end_segno);
3458 
3459 	while (start_segno < end_segno) {
3460 		if (!f2fs_down_write_trylock_trace(&sbi->gc_lock, &gc_control.lc)) {
3461 			ret = -EBUSY;
3462 			goto out;
3463 		}
3464 		sm->last_victim[GC_CB] = end_segno + 1;
3465 		sm->last_victim[GC_GREEDY] = end_segno + 1;
3466 		sm->last_victim[ALLOC_NEXT] = end_segno + 1;
3467 
3468 		gc_control.victim_segno = start_segno;
3469 		stat_inc_gc_call_count(sbi, FOREGROUND);
3470 		ret = f2fs_gc(sbi, &gc_control);
3471 		if (ret == -EAGAIN)
3472 			ret = 0;
3473 		else if (ret < 0)
3474 			break;
3475 		start_segno++;
3476 	}
3477 out:
3478 	mnt_drop_write_file(filp);
3479 	return ret;
3480 }
3481 
3482 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
3483 {
3484 	struct inode *inode = file_inode(filp);
3485 	u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3486 
3487 	/* Must validate to set it with SQLite behavior in Android. */
3488 	sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3489 
3490 	return put_user(sb_feature, (u32 __user *)arg);
3491 }
3492 
3493 #ifdef CONFIG_QUOTA
3494 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3495 {
3496 	struct dquot *transfer_to[MAXQUOTAS] = {};
3497 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3498 	struct super_block *sb = sbi->sb;
3499 	int err;
3500 
3501 	transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3502 	if (IS_ERR(transfer_to[PRJQUOTA]))
3503 		return PTR_ERR(transfer_to[PRJQUOTA]);
3504 
3505 	err = __dquot_transfer(inode, transfer_to);
3506 	if (err)
3507 		set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3508 	dqput(transfer_to[PRJQUOTA]);
3509 	return err;
3510 }
3511 
3512 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3513 {
3514 	struct f2fs_inode_info *fi = F2FS_I(inode);
3515 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3516 	struct f2fs_inode *ri = NULL;
3517 	struct f2fs_lock_context lc;
3518 	kprojid_t kprojid;
3519 	int err;
3520 
3521 	if (!f2fs_sb_has_project_quota(sbi)) {
3522 		if (projid != F2FS_DEF_PROJID)
3523 			return -EOPNOTSUPP;
3524 		else
3525 			return 0;
3526 	}
3527 
3528 	if (!f2fs_has_extra_attr(inode))
3529 		return -EOPNOTSUPP;
3530 
3531 	kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3532 
3533 	if (projid_eq(kprojid, fi->i_projid))
3534 		return 0;
3535 
3536 	err = -EPERM;
3537 	/* Is it quota file? Do not allow user to mess with it */
3538 	if (IS_NOQUOTA(inode))
3539 		return err;
3540 
3541 	if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3542 		return -EOVERFLOW;
3543 
3544 	err = f2fs_dquot_initialize(inode);
3545 	if (err)
3546 		return err;
3547 
3548 	f2fs_lock_op(sbi, &lc);
3549 	err = f2fs_transfer_project_quota(inode, kprojid);
3550 	if (err)
3551 		goto out_unlock;
3552 
3553 	fi->i_projid = kprojid;
3554 	inode_set_ctime_current(inode);
3555 	f2fs_mark_inode_dirty_sync(inode, true);
3556 out_unlock:
3557 	f2fs_unlock_op(sbi, &lc);
3558 	return err;
3559 }
3560 #else
3561 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3562 {
3563 	return 0;
3564 }
3565 
3566 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3567 {
3568 	if (projid != F2FS_DEF_PROJID)
3569 		return -EOPNOTSUPP;
3570 	return 0;
3571 }
3572 #endif
3573 
3574 int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
3575 {
3576 	struct inode *inode = d_inode(dentry);
3577 	struct f2fs_inode_info *fi = F2FS_I(inode);
3578 	u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3579 
3580 	if (IS_ENCRYPTED(inode))
3581 		fsflags |= FS_ENCRYPT_FL;
3582 	if (IS_VERITY(inode))
3583 		fsflags |= FS_VERITY_FL;
3584 	if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3585 		fsflags |= FS_INLINE_DATA_FL;
3586 	if (is_inode_flag_set(inode, FI_PIN_FILE))
3587 		fsflags |= FS_NOCOW_FL;
3588 
3589 	fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3590 
3591 	if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3592 		fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3593 
3594 	return 0;
3595 }
3596 
3597 int f2fs_fileattr_set(const struct mnt_idmap *idmap,
3598 		      struct dentry *dentry, struct file_kattr *fa)
3599 {
3600 	struct inode *inode = d_inode(dentry);
3601 	u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3602 	u32 iflags;
3603 	int err;
3604 
3605 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3606 		return -EIO;
3607 	if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3608 		return -ENOSPC;
3609 	if (fsflags & ~F2FS_GETTABLE_FS_FL)
3610 		return -EOPNOTSUPP;
3611 	fsflags &= F2FS_SETTABLE_FS_FL;
3612 	if (!fa->flags_valid)
3613 		mask &= FS_COMMON_FL;
3614 
3615 	iflags = f2fs_fsflags_to_iflags(fsflags);
3616 	if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3617 		return -EOPNOTSUPP;
3618 
3619 	err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3620 	if (!err)
3621 		err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3622 
3623 	return err;
3624 }
3625 
3626 int f2fs_pin_file_control(struct inode *inode, bool inc)
3627 {
3628 	struct f2fs_inode_info *fi = F2FS_I(inode);
3629 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3630 
3631 	if (IS_DEVICE_ALIASING(inode))
3632 		return -EINVAL;
3633 
3634 	if (fi->i_gc_failures >= sbi->gc_pin_file_threshold) {
3635 		f2fs_warn(sbi, "%s: Enable GC = ino %llx after %x GC trials",
3636 			  __func__, inode->i_ino, fi->i_gc_failures);
3637 		clear_inode_flag(inode, FI_PIN_FILE);
3638 		return -EAGAIN;
3639 	}
3640 
3641 	/* Use i_gc_failures for normal file as a risk signal. */
3642 	if (inc)
3643 		f2fs_i_gc_failures_write(inode, fi->i_gc_failures + 1);
3644 
3645 	return 0;
3646 }
3647 
3648 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3649 {
3650 	struct inode *inode = file_inode(filp);
3651 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3652 	__u32 pin;
3653 	int ret = 0;
3654 
3655 	if (get_user(pin, (__u32 __user *)arg))
3656 		return -EFAULT;
3657 
3658 	if (!S_ISREG(inode->i_mode))
3659 		return -EINVAL;
3660 
3661 	if (f2fs_readonly(sbi->sb))
3662 		return -EROFS;
3663 
3664 	if (!pin && IS_DEVICE_ALIASING(inode))
3665 		return -EOPNOTSUPP;
3666 
3667 	ret = mnt_want_write_file(filp);
3668 	if (ret)
3669 		return ret;
3670 
3671 	inode_lock(inode);
3672 
3673 	if (f2fs_is_atomic_file(inode)) {
3674 		ret = -EINVAL;
3675 		goto out;
3676 	}
3677 
3678 	if (!pin) {
3679 		clear_inode_flag(inode, FI_PIN_FILE);
3680 		f2fs_i_gc_failures_write(inode, 0);
3681 		goto done;
3682 	} else if (f2fs_is_pinned_file(inode)) {
3683 		goto done;
3684 	}
3685 
3686 	if (F2FS_HAS_BLOCKS(inode)) {
3687 		ret = -EFBIG;
3688 		goto out;
3689 	}
3690 
3691 	/* Let's allow file pinning on zoned device. */
3692 	if (!f2fs_sb_has_blkzoned(sbi) &&
3693 	    f2fs_should_update_outplace(inode, NULL)) {
3694 		ret = -EINVAL;
3695 		goto out;
3696 	}
3697 
3698 	if (f2fs_pin_file_control(inode, false)) {
3699 		ret = -EAGAIN;
3700 		goto out;
3701 	}
3702 
3703 	ret = f2fs_convert_inline_inode(inode);
3704 	if (ret)
3705 		goto out;
3706 
3707 	if (!f2fs_disable_compressed_file(inode)) {
3708 		ret = -EOPNOTSUPP;
3709 		goto out;
3710 	}
3711 
3712 	set_inode_flag(inode, FI_PIN_FILE);
3713 	ret = F2FS_I(inode)->i_gc_failures;
3714 done:
3715 	f2fs_update_time(sbi, REQ_TIME);
3716 out:
3717 	inode_unlock(inode);
3718 	mnt_drop_write_file(filp);
3719 	return ret;
3720 }
3721 
3722 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3723 {
3724 	struct inode *inode = file_inode(filp);
3725 	__u32 pin = 0;
3726 
3727 	if (is_inode_flag_set(inode, FI_PIN_FILE))
3728 		pin = F2FS_I(inode)->i_gc_failures;
3729 	return put_user(pin, (u32 __user *)arg);
3730 }
3731 
3732 static int f2fs_ioc_get_dev_alias_file(struct file *filp, unsigned long arg)
3733 {
3734 	return put_user(IS_DEVICE_ALIASING(file_inode(filp)) ? 1 : 0,
3735 			(u32 __user *)arg);
3736 }
3737 
3738 static bool f2fs_get_dev_alias_extent(struct f2fs_sb_info *sbi,
3739 				       struct dentry *dentry,
3740 				       struct extent_info *ei)
3741 {
3742 	int i;
3743 
3744 	for (i = 1; i < sbi->s_ndevs; i++) {
3745 		char *name = strrchr(FDEV(i).path, '/');
3746 
3747 		name = name ? name + 1 : FDEV(i).path;
3748 		if (strcmp(name, dentry->d_name.name))
3749 			continue;
3750 
3751 		ei->blk = FDEV(i).start_blk;
3752 		ei->len = FDEV(i).total_segments << sbi->log_blocks_per_seg;
3753 		ei->fofs = 0;
3754 		return true;
3755 	}
3756 	return false;
3757 }
3758 
3759 static int f2fs_ioc_reserve_dev_alias(struct file *filp)
3760 {
3761 	struct inode *inode = file_inode(filp);
3762 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3763 	struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
3764 	struct extent_info ei;
3765 	struct cp_control cpc = { CP_SYNC, 0, 0, 0 };
3766 	struct f2fs_lock_context lc, glc;
3767 	blkcnt_t count;
3768 	unsigned int start, end;
3769 	int type, err;
3770 
3771 	if (!capable(CAP_SYS_ADMIN))
3772 		return -EPERM;
3773 
3774 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
3775 		return -EINVAL;
3776 
3777 	err = mnt_want_write_file(filp);
3778 	if (err)
3779 		return err;
3780 
3781 	inode_lock(inode);
3782 
3783 	if (!IS_DEVICE_ALIASING(inode)) {
3784 		err = -EINVAL;
3785 		goto out_inode_unlock;
3786 	}
3787 
3788 	if (F2FS_HAS_BLOCKS(inode)) {
3789 		err = 0;
3790 		goto out_inode_unlock;
3791 	}
3792 
3793 	if (!f2fs_get_dev_alias_extent(sbi, filp->f_path.dentry, &ei)) {
3794 		f2fs_warn(sbi, "device alias file (%s, ino=%llu) has no matching device",
3795 			  filp->f_path.dentry->d_name.name,
3796 			  (unsigned long long)inode->i_ino);
3797 		set_sbi_flag(sbi, SBI_NEED_FSCK);
3798 		f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
3799 		err = -EFSCORRUPTED;
3800 		goto out_inode_unlock;
3801 	}
3802 
3803 	spin_lock(&sbi->stat_lock);
3804 	if (sbi->total_valid_block_count + ei.len >
3805 			get_available_block_count(sbi, inode, true)) {
3806 		spin_unlock(&sbi->stat_lock);
3807 		err = -ENOSPC;
3808 		goto out_inode_unlock;
3809 	}
3810 	sbi->alias_reserved_blocks += ei.len;
3811 	spin_unlock(&sbi->stat_lock);
3812 
3813 	spin_lock(&FREE_I(sbi)->segmap_lock);
3814 	FDEV(f2fs_target_device_index(sbi, ei.blk)).is_reserving = true;
3815 	spin_unlock(&FREE_I(sbi)->segmap_lock);
3816 
3817 	start = GET_SEGNO(sbi, ei.blk);
3818 	end = GET_SEGNO(sbi, ei.blk + ei.len - 1);
3819 
3820 	/* Acquire gc_lock for victim reset, curseg resize, and range GC */
3821 	f2fs_down_write_trace(&sbi->gc_lock, &glc);
3822 
3823 	/* Reset the victim information to prevent GC from targeting the range */
3824 	f2fs_reset_gc_victim_resource(sbi, start, end);
3825 
3826 	/* Move out cursegs from the target range */
3827 	for (type = CURSEG_HOT_DATA; type < NR_CURSEG_PERSIST_TYPE; type++) {
3828 		err = f2fs_allocate_segment_for_resize(sbi, type, start, end);
3829 		if (err)
3830 			goto out_gc_unlock;
3831 	}
3832 
3833 	f2fs_lock_op(sbi, &lc);
3834 
3835 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
3836 		err = -EINVAL;
3837 		f2fs_unlock_op(sbi, &lc);
3838 		goto out_gc_unlock;
3839 	}
3840 
3841 	/* do GC to move out valid blocks in the range all at once! */
3842 	err = f2fs_gc_range(sbi, start, end, false, 0, false);
3843 	if (err) {
3844 		f2fs_unlock_op(sbi, &lc);
3845 		goto out_gc_unlock;
3846 	}
3847 
3848 	count = ei.len;
3849 	err = inc_valid_block_count(sbi, inode, &count, false, true);
3850 	if (err) {
3851 		f2fs_unlock_op(sbi, &lc);
3852 		goto out_gc_unlock;
3853 	}
3854 
3855 	write_lock(&et->lock);
3856 	et->largest = ei;
3857 	write_unlock(&et->lock);
3858 	clear_inode_flag(inode, FI_NO_EXTENT);
3859 
3860 	f2fs_drop_discard_cmd_range(sbi, ei.blk, ei.len);
3861 	f2fs_reserve_device_alias(sbi, ei.blk, ei.len);
3862 
3863 	i_size_write(inode, (loff_t)ei.len << sbi->log_blocksize);
3864 	f2fs_update_inode_cache(inode);
3865 
3866 	spin_lock(&FREE_I(sbi)->segmap_lock);
3867 	FDEV(f2fs_target_device_index(sbi, ei.blk)).is_reserving = false;
3868 	spin_unlock(&FREE_I(sbi)->segmap_lock);
3869 
3870 	f2fs_unlock_op(sbi, &lc);
3871 	f2fs_up_write_trace(&sbi->gc_lock, &glc);
3872 
3873 	inode_unlock(inode);
3874 	mnt_drop_write_file(filp);
3875 
3876 	return f2fs_write_checkpoint(sbi, &cpc);
3877 
3878 out_gc_unlock:
3879 	spin_lock(&sbi->stat_lock);
3880 	sbi->alias_reserved_blocks -= ei.len;
3881 	spin_unlock(&sbi->stat_lock);
3882 
3883 	spin_lock(&FREE_I(sbi)->segmap_lock);
3884 	FDEV(f2fs_target_device_index(sbi, ei.blk)).is_reserving = false;
3885 	spin_unlock(&FREE_I(sbi)->segmap_lock);
3886 	f2fs_up_write_trace(&sbi->gc_lock, &glc);
3887 
3888 out_inode_unlock:
3889 	inode_unlock(inode);
3890 	mnt_drop_write_file(filp);
3891 	return err;
3892 }
3893 
3894 static int f2fs_ioc_release_dev_alias(struct file *filp)
3895 {
3896 	struct inode *inode = file_inode(filp);
3897 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3898 	struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
3899 	struct extent_info ei = {0, };
3900 	struct cp_control cpc = { CP_SYNC, 0, 0, 0 };
3901 	struct f2fs_lock_context lc, glc;
3902 	int err;
3903 
3904 	if (!capable(CAP_SYS_ADMIN))
3905 		return -EPERM;
3906 
3907 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
3908 		return -EINVAL;
3909 
3910 	err = mnt_want_write_file(filp);
3911 	if (err)
3912 		return err;
3913 
3914 	inode_lock(inode);
3915 
3916 	if (!IS_DEVICE_ALIASING(inode)) {
3917 		err = -EINVAL;
3918 		goto out_inode_unlock;
3919 	}
3920 
3921 	if (!F2FS_HAS_BLOCKS(inode)) {
3922 		err = 0;
3923 		goto out_inode_unlock;
3924 	}
3925 
3926 	err = filemap_write_and_wait(inode->i_mapping);
3927 	if (err)
3928 		goto out_inode_unlock;
3929 
3930 	read_lock(&et->lock);
3931 	ei = et->largest;
3932 	read_unlock(&et->lock);
3933 
3934 	f2fs_down_write_trace(&sbi->gc_lock, &glc);
3935 	f2fs_lock_op(sbi, &lc);
3936 
3937 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
3938 		err = -EINVAL;
3939 		f2fs_unlock_op(sbi, &lc);
3940 		f2fs_up_write_trace(&sbi->gc_lock, &glc);
3941 		goto out_inode_unlock;
3942 	}
3943 
3944 	filemap_invalidate_lock(inode->i_mapping);
3945 	truncate_setsize(inode, 0);
3946 
3947 	err = f2fs_truncate_blocks(inode, 0, false);
3948 	if (err)
3949 		i_size_write(inode, (loff_t)ei.len << sbi->log_blocksize);
3950 	filemap_invalidate_unlock(inode->i_mapping);
3951 
3952 	if (err) {
3953 		f2fs_unlock_op(sbi, &lc);
3954 		f2fs_up_write_trace(&sbi->gc_lock, &glc);
3955 		goto out_inode_unlock;
3956 	}
3957 
3958 	f2fs_update_inode_cache(inode);
3959 
3960 	f2fs_unlock_op(sbi, &lc);
3961 	f2fs_up_write_trace(&sbi->gc_lock, &glc);
3962 
3963 	inode_unlock(inode);
3964 	mnt_drop_write_file(filp);
3965 
3966 	return f2fs_write_checkpoint(sbi, &cpc);
3967 
3968 out_inode_unlock:
3969 	inode_unlock(inode);
3970 	mnt_drop_write_file(filp);
3971 	return err;
3972 }
3973 
3974 static int f2fs_ioc_io_prio(struct file *filp, unsigned long arg)
3975 {
3976 	struct inode *inode = file_inode(filp);
3977 	__u32 level;
3978 
3979 	if (get_user(level, (__u32 __user *)arg))
3980 		return -EFAULT;
3981 
3982 	if (!S_ISREG(inode->i_mode) || level >= F2FS_IOPRIO_MAX)
3983 		return -EINVAL;
3984 
3985 	inode_lock(inode);
3986 	F2FS_I(inode)->ioprio_hint = level;
3987 	inode_unlock(inode);
3988 	return 0;
3989 }
3990 
3991 int f2fs_precache_extents(struct inode *inode)
3992 {
3993 	struct f2fs_inode_info *fi = F2FS_I(inode);
3994 	struct f2fs_map_blocks map;
3995 	pgoff_t m_next_extent;
3996 	loff_t end;
3997 	int err;
3998 
3999 	if (is_inode_flag_set(inode, FI_NO_EXTENT))
4000 		return -EOPNOTSUPP;
4001 
4002 	map.m_lblk = 0;
4003 	map.m_pblk = 0;
4004 	map.m_next_pgofs = NULL;
4005 	map.m_next_extent = &m_next_extent;
4006 	map.m_seg_type = NO_CHECK_TYPE;
4007 	map.m_may_create = false;
4008 	end = F2FS_BLK_ALIGN(F2FS_I_SB(inode), i_size_read(inode));
4009 
4010 	while (map.m_lblk < end) {
4011 		map.m_len = end - map.m_lblk;
4012 
4013 		f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
4014 		err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
4015 		f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
4016 		if (err || !map.m_len)
4017 			return err;
4018 
4019 		map.m_lblk = m_next_extent;
4020 	}
4021 
4022 	return 0;
4023 }
4024 
4025 static int f2fs_ioc_precache_extents(struct file *filp)
4026 {
4027 	return f2fs_precache_extents(file_inode(filp));
4028 }
4029 
4030 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
4031 {
4032 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
4033 	__u64 block_count;
4034 
4035 	if (!capable(CAP_SYS_ADMIN))
4036 		return -EPERM;
4037 
4038 	if (f2fs_readonly(sbi->sb))
4039 		return -EROFS;
4040 
4041 	if (copy_from_user(&block_count, (void __user *)arg,
4042 			   sizeof(block_count)))
4043 		return -EFAULT;
4044 
4045 	return f2fs_resize_fs(filp, block_count);
4046 }
4047 
4048 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
4049 {
4050 	struct inode *inode = file_inode(filp);
4051 
4052 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4053 
4054 	if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
4055 		f2fs_warn(F2FS_I_SB(inode),
4056 			  "Can't enable fs-verity on inode %llu: the verity feature is not enabled on this filesystem",
4057 			  inode->i_ino);
4058 		return -EOPNOTSUPP;
4059 	}
4060 
4061 	return fsverity_ioctl_enable(filp, (const void __user *)arg);
4062 }
4063 
4064 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
4065 {
4066 	if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
4067 		return -EOPNOTSUPP;
4068 
4069 	return fsverity_ioctl_measure(filp, (void __user *)arg);
4070 }
4071 
4072 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
4073 {
4074 	if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
4075 		return -EOPNOTSUPP;
4076 
4077 	return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
4078 }
4079 
4080 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
4081 {
4082 	struct inode *inode = file_inode(filp);
4083 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4084 	char *vbuf;
4085 	int count;
4086 	int err = 0;
4087 
4088 	vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
4089 	if (!vbuf)
4090 		return -ENOMEM;
4091 
4092 	f2fs_down_read(&sbi->sb_lock);
4093 	count = utf16s_to_utf8s(sbi->raw_super->volume_name,
4094 			ARRAY_SIZE(sbi->raw_super->volume_name),
4095 			UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
4096 	f2fs_up_read(&sbi->sb_lock);
4097 
4098 	if (copy_to_user((char __user *)arg, vbuf,
4099 				min(FSLABEL_MAX, count)))
4100 		err = -EFAULT;
4101 
4102 	kfree(vbuf);
4103 	return err;
4104 }
4105 
4106 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
4107 {
4108 	struct inode *inode = file_inode(filp);
4109 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4110 	char *vbuf;
4111 	int err = 0;
4112 
4113 	if (!capable(CAP_SYS_ADMIN))
4114 		return -EPERM;
4115 
4116 	vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
4117 	if (IS_ERR(vbuf))
4118 		return PTR_ERR(vbuf);
4119 
4120 	err = mnt_want_write_file(filp);
4121 	if (err)
4122 		goto out;
4123 
4124 	f2fs_down_write(&sbi->sb_lock);
4125 
4126 	memset(sbi->raw_super->volume_name, 0,
4127 			sizeof(sbi->raw_super->volume_name));
4128 	utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
4129 			sbi->raw_super->volume_name,
4130 			ARRAY_SIZE(sbi->raw_super->volume_name));
4131 
4132 	err = f2fs_commit_super(sbi, false);
4133 
4134 	f2fs_up_write(&sbi->sb_lock);
4135 
4136 	mnt_drop_write_file(filp);
4137 out:
4138 	kfree(vbuf);
4139 	return err;
4140 }
4141 
4142 static int f2fs_get_compress_blocks(struct inode *inode, __u64 *blocks)
4143 {
4144 	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
4145 		return -EOPNOTSUPP;
4146 
4147 	if (!f2fs_compressed_file(inode))
4148 		return -EINVAL;
4149 
4150 	*blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
4151 
4152 	return 0;
4153 }
4154 
4155 static int f2fs_ioc_get_compress_blocks(struct file *filp, unsigned long arg)
4156 {
4157 	struct inode *inode = file_inode(filp);
4158 	__u64 blocks;
4159 	int ret;
4160 
4161 	ret = f2fs_get_compress_blocks(inode, &blocks);
4162 	if (ret < 0)
4163 		return ret;
4164 
4165 	return put_user(blocks, (u64 __user *)arg);
4166 }
4167 
4168 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
4169 {
4170 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
4171 	unsigned int released_blocks = 0;
4172 	int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
4173 	block_t blkaddr;
4174 	int i;
4175 
4176 	for (i = 0; i < count; i++) {
4177 		blkaddr = data_blkaddr(dn->inode, dn->node_entry,
4178 						dn->ofs_in_node + i);
4179 
4180 		if (!__is_valid_data_blkaddr(blkaddr))
4181 			continue;
4182 		if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
4183 					DATA_GENERIC_ENHANCE)))
4184 			return -EFSCORRUPTED;
4185 	}
4186 
4187 	while (count) {
4188 		int compr_blocks = 0;
4189 
4190 		for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
4191 			blkaddr = f2fs_data_blkaddr(dn);
4192 
4193 			if (i == 0) {
4194 				if (blkaddr == COMPRESS_ADDR)
4195 					continue;
4196 				dn->ofs_in_node += cluster_size;
4197 				goto next;
4198 			}
4199 
4200 			if (__is_valid_data_blkaddr(blkaddr))
4201 				compr_blocks++;
4202 
4203 			if (blkaddr != NEW_ADDR)
4204 				continue;
4205 
4206 			f2fs_set_data_blkaddr(dn, NULL_ADDR);
4207 		}
4208 
4209 		f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
4210 		dec_valid_block_count(sbi, dn->inode,
4211 					cluster_size - compr_blocks);
4212 
4213 		released_blocks += cluster_size - compr_blocks;
4214 next:
4215 		count -= cluster_size;
4216 	}
4217 
4218 	return released_blocks;
4219 }
4220 
4221 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
4222 {
4223 	struct inode *inode = file_inode(filp);
4224 	struct f2fs_inode_info *fi = F2FS_I(inode);
4225 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4226 	struct f2fs_lock_context lc;
4227 	pgoff_t page_idx = 0, last_idx;
4228 	unsigned int released_blocks = 0;
4229 	int ret;
4230 	int writecount;
4231 
4232 	if (!f2fs_sb_has_compression(sbi))
4233 		return -EOPNOTSUPP;
4234 
4235 	if (f2fs_readonly(sbi->sb))
4236 		return -EROFS;
4237 
4238 	ret = mnt_want_write_file(filp);
4239 	if (ret)
4240 		return ret;
4241 
4242 	f2fs_balance_fs(sbi, true);
4243 
4244 	inode_lock(inode);
4245 
4246 	writecount = atomic_read(&inode->i_writecount);
4247 	if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
4248 			(!(filp->f_mode & FMODE_WRITE) && writecount)) {
4249 		ret = -EBUSY;
4250 		goto out;
4251 	}
4252 
4253 	if (!f2fs_compressed_file(inode) ||
4254 		is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4255 		ret = -EINVAL;
4256 		goto out;
4257 	}
4258 
4259 	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4260 	if (ret)
4261 		goto out;
4262 
4263 	if (!atomic_read(&fi->i_compr_blocks)) {
4264 		ret = -EPERM;
4265 		goto out;
4266 	}
4267 
4268 	set_inode_flag(inode, FI_COMPRESS_RELEASED);
4269 	inode_set_ctime_current(inode);
4270 	f2fs_mark_inode_dirty_sync(inode, true);
4271 
4272 	f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
4273 	filemap_invalidate_lock(inode->i_mapping);
4274 
4275 	last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4276 
4277 	while (page_idx < last_idx) {
4278 		struct dnode_of_data dn;
4279 		pgoff_t end_offset, count;
4280 
4281 		f2fs_lock_op(sbi, &lc);
4282 
4283 		set_new_dnode(&dn, inode, NULL, NULL, 0);
4284 		ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
4285 		if (ret) {
4286 			f2fs_unlock_op(sbi, &lc);
4287 			if (ret == -ENOENT) {
4288 				page_idx = f2fs_get_next_page_offset(&dn,
4289 								page_idx);
4290 				ret = 0;
4291 				continue;
4292 			}
4293 			break;
4294 		}
4295 
4296 		end_offset = ADDRS_PER_PAGE(dn.node_entry, inode);
4297 		count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
4298 		count = round_up(count, fi->i_cluster_size);
4299 
4300 		ret = release_compress_blocks(&dn, count);
4301 
4302 		f2fs_put_dnode(&dn);
4303 
4304 		f2fs_unlock_op(sbi, &lc);
4305 
4306 		if (ret < 0)
4307 			break;
4308 
4309 		page_idx += count;
4310 		released_blocks += ret;
4311 	}
4312 
4313 	filemap_invalidate_unlock(inode->i_mapping);
4314 	f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
4315 out:
4316 	if (released_blocks)
4317 		f2fs_update_time(sbi, REQ_TIME);
4318 	inode_unlock(inode);
4319 
4320 	mnt_drop_write_file(filp);
4321 
4322 	if (ret >= 0) {
4323 		ret = put_user(released_blocks, (u64 __user *)arg);
4324 	} else if (released_blocks &&
4325 			atomic_read(&fi->i_compr_blocks)) {
4326 		set_sbi_flag(sbi, SBI_NEED_FSCK);
4327 		f2fs_warn(sbi, "%s: partial blocks were released i_ino=%llx "
4328 			"iblocks=%llu, released=%u, compr_blocks=%u, "
4329 			"run fsck to fix.",
4330 			__func__, inode->i_ino, inode->i_blocks,
4331 			released_blocks,
4332 			atomic_read(&fi->i_compr_blocks));
4333 	}
4334 
4335 	return ret;
4336 }
4337 
4338 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,
4339 		unsigned int *reserved_blocks)
4340 {
4341 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
4342 	int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
4343 	block_t blkaddr;
4344 	int i;
4345 
4346 	for (i = 0; i < count; i++) {
4347 		blkaddr = data_blkaddr(dn->inode, dn->node_entry,
4348 						dn->ofs_in_node + i);
4349 
4350 		if (!__is_valid_data_blkaddr(blkaddr))
4351 			continue;
4352 		if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
4353 					DATA_GENERIC_ENHANCE)))
4354 			return -EFSCORRUPTED;
4355 	}
4356 
4357 	while (count) {
4358 		int compr_blocks = 0;
4359 		blkcnt_t reserved = 0;
4360 		blkcnt_t to_reserved;
4361 		int ret;
4362 
4363 		for (i = 0; i < cluster_size; i++) {
4364 			blkaddr = data_blkaddr(dn->inode, dn->node_entry,
4365 						dn->ofs_in_node + i);
4366 
4367 			if (i == 0) {
4368 				if (blkaddr != COMPRESS_ADDR) {
4369 					dn->ofs_in_node += cluster_size;
4370 					goto next;
4371 				}
4372 				continue;
4373 			}
4374 
4375 			/*
4376 			 * compressed cluster was not released due to it
4377 			 * fails in release_compress_blocks(), so NEW_ADDR
4378 			 * is a possible case.
4379 			 */
4380 			if (blkaddr == NEW_ADDR) {
4381 				reserved++;
4382 				continue;
4383 			}
4384 			if (__is_valid_data_blkaddr(blkaddr)) {
4385 				compr_blocks++;
4386 				continue;
4387 			}
4388 		}
4389 
4390 		to_reserved = cluster_size - compr_blocks - reserved;
4391 
4392 		/* for the case all blocks in cluster were reserved */
4393 		if (reserved && to_reserved == 1) {
4394 			dn->ofs_in_node += cluster_size;
4395 			goto next;
4396 		}
4397 
4398 		ret = inc_valid_block_count(sbi, dn->inode,
4399 						&to_reserved, false, false);
4400 		if (unlikely(ret))
4401 			return ret;
4402 
4403 		for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
4404 			if (f2fs_data_blkaddr(dn) == NULL_ADDR)
4405 				f2fs_set_data_blkaddr(dn, NEW_ADDR);
4406 		}
4407 
4408 		f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
4409 
4410 		*reserved_blocks += to_reserved;
4411 next:
4412 		count -= cluster_size;
4413 	}
4414 
4415 	return 0;
4416 }
4417 
4418 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
4419 {
4420 	struct inode *inode = file_inode(filp);
4421 	struct f2fs_inode_info *fi = F2FS_I(inode);
4422 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4423 	pgoff_t page_idx = 0, last_idx;
4424 	unsigned int reserved_blocks = 0;
4425 	int ret;
4426 
4427 	if (!f2fs_sb_has_compression(sbi))
4428 		return -EOPNOTSUPP;
4429 
4430 	if (f2fs_readonly(sbi->sb))
4431 		return -EROFS;
4432 
4433 	ret = mnt_want_write_file(filp);
4434 	if (ret)
4435 		return ret;
4436 
4437 	f2fs_balance_fs(sbi, true);
4438 
4439 	inode_lock(inode);
4440 
4441 	if (!f2fs_compressed_file(inode) ||
4442 		!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4443 		ret = -EINVAL;
4444 		goto unlock_inode;
4445 	}
4446 
4447 	if (atomic_read(&fi->i_compr_blocks))
4448 		goto unlock_inode;
4449 
4450 	f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
4451 	filemap_invalidate_lock(inode->i_mapping);
4452 
4453 	last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4454 
4455 	while (page_idx < last_idx) {
4456 		struct dnode_of_data dn;
4457 		struct f2fs_lock_context lc;
4458 		pgoff_t end_offset, count;
4459 
4460 		f2fs_lock_op(sbi, &lc);
4461 
4462 		set_new_dnode(&dn, inode, NULL, NULL, 0);
4463 		ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
4464 		if (ret) {
4465 			f2fs_unlock_op(sbi, &lc);
4466 			if (ret == -ENOENT) {
4467 				page_idx = f2fs_get_next_page_offset(&dn,
4468 								page_idx);
4469 				ret = 0;
4470 				continue;
4471 			}
4472 			break;
4473 		}
4474 
4475 		end_offset = ADDRS_PER_PAGE(dn.node_entry, inode);
4476 		count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
4477 		count = round_up(count, fi->i_cluster_size);
4478 
4479 		ret = reserve_compress_blocks(&dn, count, &reserved_blocks);
4480 
4481 		f2fs_put_dnode(&dn);
4482 
4483 		f2fs_unlock_op(sbi, &lc);
4484 
4485 		if (ret < 0)
4486 			break;
4487 
4488 		page_idx += count;
4489 	}
4490 
4491 	filemap_invalidate_unlock(inode->i_mapping);
4492 	f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
4493 
4494 	if (!ret) {
4495 		clear_inode_flag(inode, FI_COMPRESS_RELEASED);
4496 		inode_set_ctime_current(inode);
4497 		f2fs_mark_inode_dirty_sync(inode, true);
4498 	}
4499 unlock_inode:
4500 	if (reserved_blocks)
4501 		f2fs_update_time(sbi, REQ_TIME);
4502 	inode_unlock(inode);
4503 	mnt_drop_write_file(filp);
4504 
4505 	if (!ret) {
4506 		ret = put_user(reserved_blocks, (u64 __user *)arg);
4507 	} else if (reserved_blocks &&
4508 			atomic_read(&fi->i_compr_blocks)) {
4509 		set_sbi_flag(sbi, SBI_NEED_FSCK);
4510 		f2fs_warn(sbi, "%s: partial blocks were reserved i_ino=%llx "
4511 			"iblocks=%llu, reserved=%u, compr_blocks=%u, "
4512 			"run fsck to fix.",
4513 			__func__, inode->i_ino, inode->i_blocks,
4514 			reserved_blocks,
4515 			atomic_read(&fi->i_compr_blocks));
4516 	}
4517 
4518 	return ret;
4519 }
4520 
4521 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
4522 		pgoff_t off, block_t block, block_t len, u32 flags)
4523 {
4524 	sector_t sector = SECTOR_FROM_BLOCK(F2FS_I_SB(inode), block);
4525 	sector_t nr_sects = SECTOR_FROM_BLOCK(F2FS_I_SB(inode), len);
4526 	int ret = 0;
4527 
4528 	if (flags & F2FS_TRIM_FILE_DISCARD) {
4529 		if (bdev_max_secure_erase_sectors(bdev))
4530 			ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
4531 					GFP_NOFS);
4532 		else
4533 			ret = blkdev_issue_discard(bdev, sector, nr_sects,
4534 					GFP_NOFS);
4535 	}
4536 
4537 	if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
4538 		if (IS_ENCRYPTED(inode))
4539 			ret = fscrypt_zeroout_range(inode,
4540 					(loff_t)off << inode->i_blkbits, sector,
4541 					(u64)len << inode->i_blkbits);
4542 		else
4543 			ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
4544 					GFP_NOFS, 0);
4545 	}
4546 
4547 	return ret;
4548 }
4549 
4550 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
4551 {
4552 	struct inode *inode = file_inode(filp);
4553 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4554 	struct address_space *mapping = inode->i_mapping;
4555 	struct block_device *prev_bdev = NULL;
4556 	struct f2fs_sectrim_range range;
4557 	pgoff_t index, pg_end, prev_index = 0;
4558 	block_t prev_block = 0, len = 0;
4559 	loff_t end_addr;
4560 	bool to_end = false;
4561 	int ret = 0;
4562 
4563 	if (!(filp->f_mode & FMODE_WRITE))
4564 		return -EBADF;
4565 
4566 	if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
4567 				sizeof(range)))
4568 		return -EFAULT;
4569 
4570 	if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
4571 			!S_ISREG(inode->i_mode))
4572 		return -EINVAL;
4573 
4574 	if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
4575 			!f2fs_hw_support_discard(sbi)) ||
4576 			((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
4577 			 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
4578 		return -EOPNOTSUPP;
4579 
4580 	ret = mnt_want_write_file(filp);
4581 	if (ret)
4582 		return ret;
4583 	inode_lock(inode);
4584 
4585 	if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
4586 			range.start >= inode->i_size) {
4587 		ret = -EINVAL;
4588 		goto err;
4589 	}
4590 
4591 	if (range.len == 0)
4592 		goto err;
4593 
4594 	if (inode->i_size - range.start > range.len) {
4595 		end_addr = range.start + range.len;
4596 	} else {
4597 		end_addr = range.len == (u64)-1 ?
4598 			sbi->sb->s_maxbytes : inode->i_size;
4599 		to_end = true;
4600 	}
4601 
4602 	if (!IS_ALIGNED(range.start, F2FS_BLKSIZE(sbi)) ||
4603 	    (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE(sbi)))) {
4604 		ret = -EINVAL;
4605 		goto err;
4606 	}
4607 
4608 	index = F2FS_BYTES_TO_BLK(sbi, range.start);
4609 	pg_end = F2FS_BLK_ALIGN(sbi, end_addr);
4610 
4611 	ret = f2fs_convert_inline_inode(inode);
4612 	if (ret)
4613 		goto err;
4614 
4615 	f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4616 	filemap_invalidate_lock(mapping);
4617 
4618 	ret = filemap_write_and_wait_range(mapping, range.start,
4619 			to_end ? LLONG_MAX : end_addr - 1);
4620 	if (ret)
4621 		goto out;
4622 
4623 	truncate_inode_pages_range(mapping, range.start,
4624 			to_end ? -1 : end_addr - 1);
4625 
4626 	while (index < pg_end) {
4627 		struct dnode_of_data dn;
4628 		pgoff_t end_offset, count;
4629 		int i;
4630 
4631 		set_new_dnode(&dn, inode, NULL, NULL, 0);
4632 		ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
4633 		if (ret) {
4634 			if (ret == -ENOENT) {
4635 				index = f2fs_get_next_page_offset(&dn, index);
4636 				continue;
4637 			}
4638 			goto out;
4639 		}
4640 
4641 		end_offset = ADDRS_PER_PAGE(dn.node_entry, inode);
4642 		count = min(end_offset - dn.ofs_in_node, pg_end - index);
4643 		for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
4644 			struct block_device *cur_bdev;
4645 			block_t blkaddr = f2fs_data_blkaddr(&dn);
4646 
4647 			if (!__is_valid_data_blkaddr(blkaddr))
4648 				continue;
4649 
4650 			if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
4651 						DATA_GENERIC_ENHANCE)) {
4652 				ret = -EFSCORRUPTED;
4653 				f2fs_put_dnode(&dn);
4654 				goto out;
4655 			}
4656 
4657 			cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
4658 			if (f2fs_is_multi_device(sbi)) {
4659 				int di = f2fs_target_device_index(sbi, blkaddr);
4660 
4661 				blkaddr -= FDEV(di).start_blk;
4662 			}
4663 
4664 			if (len) {
4665 				if (prev_bdev == cur_bdev &&
4666 						index == prev_index + len &&
4667 						blkaddr == prev_block + len) {
4668 					len++;
4669 				} else {
4670 					ret = f2fs_secure_erase(prev_bdev,
4671 						inode, prev_index, prev_block,
4672 						len, range.flags);
4673 					if (ret) {
4674 						f2fs_put_dnode(&dn);
4675 						goto out;
4676 					}
4677 
4678 					len = 0;
4679 				}
4680 			}
4681 
4682 			if (!len) {
4683 				prev_bdev = cur_bdev;
4684 				prev_index = index;
4685 				prev_block = blkaddr;
4686 				len = 1;
4687 			}
4688 		}
4689 
4690 		f2fs_put_dnode(&dn);
4691 
4692 		if (fatal_signal_pending(current)) {
4693 			ret = -EINTR;
4694 			goto out;
4695 		}
4696 		cond_resched();
4697 	}
4698 
4699 	if (len)
4700 		ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
4701 				prev_block, len, range.flags);
4702 	f2fs_update_time(sbi, REQ_TIME);
4703 out:
4704 	filemap_invalidate_unlock(mapping);
4705 	f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4706 err:
4707 	inode_unlock(inode);
4708 	mnt_drop_write_file(filp);
4709 
4710 	return ret;
4711 }
4712 
4713 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
4714 {
4715 	struct inode *inode = file_inode(filp);
4716 	struct f2fs_comp_option option;
4717 
4718 	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
4719 		return -EOPNOTSUPP;
4720 
4721 	inode_lock_shared(inode);
4722 
4723 	if (!f2fs_compressed_file(inode)) {
4724 		inode_unlock_shared(inode);
4725 		return -ENODATA;
4726 	}
4727 
4728 	option.algorithm = F2FS_I(inode)->i_compress_algorithm;
4729 	option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
4730 
4731 	inode_unlock_shared(inode);
4732 
4733 	if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
4734 				sizeof(option)))
4735 		return -EFAULT;
4736 
4737 	return 0;
4738 }
4739 
4740 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
4741 {
4742 	struct inode *inode = file_inode(filp);
4743 	struct f2fs_inode_info *fi = F2FS_I(inode);
4744 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4745 	struct f2fs_comp_option option;
4746 	int ret = 0;
4747 
4748 	if (!f2fs_sb_has_compression(sbi))
4749 		return -EOPNOTSUPP;
4750 
4751 	if (!(filp->f_mode & FMODE_WRITE))
4752 		return -EBADF;
4753 
4754 	if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
4755 				sizeof(option)))
4756 		return -EFAULT;
4757 
4758 	if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
4759 		option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
4760 		option.algorithm >= COMPRESS_MAX)
4761 		return -EINVAL;
4762 
4763 	ret = mnt_want_write_file(filp);
4764 	if (ret)
4765 		return ret;
4766 	inode_lock(inode);
4767 
4768 	f2fs_down_write(&F2FS_I(inode)->i_sem);
4769 	if (!f2fs_compressed_file(inode)) {
4770 		ret = -EINVAL;
4771 		goto out;
4772 	}
4773 
4774 	if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4775 		ret = -EBUSY;
4776 		goto out;
4777 	}
4778 
4779 	if (F2FS_HAS_BLOCKS(inode)) {
4780 		ret = -EFBIG;
4781 		goto out;
4782 	}
4783 
4784 	fi->i_compress_algorithm = option.algorithm;
4785 	fi->i_log_cluster_size = option.log_cluster_size;
4786 	fi->i_cluster_size = BIT(option.log_cluster_size);
4787 	/* Set default level */
4788 	if (fi->i_compress_algorithm == COMPRESS_ZSTD)
4789 		fi->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
4790 	else
4791 		fi->i_compress_level = 0;
4792 	/* Adjust mount option level */
4793 	if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
4794 	    F2FS_OPTION(sbi).compress_level)
4795 		fi->i_compress_level = F2FS_OPTION(sbi).compress_level;
4796 	f2fs_mark_inode_dirty_sync(inode, true);
4797 
4798 	if (!f2fs_is_compress_backend_ready(inode))
4799 		f2fs_warn(sbi, "compression algorithm is successfully set, "
4800 			"but current kernel doesn't support this algorithm.");
4801 out:
4802 	f2fs_up_write(&fi->i_sem);
4803 	inode_unlock(inode);
4804 	mnt_drop_write_file(filp);
4805 
4806 	return ret;
4807 }
4808 
4809 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4810 {
4811 	DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
4812 	struct address_space *mapping = inode->i_mapping;
4813 	struct folio *folio;
4814 	pgoff_t redirty_idx = page_idx;
4815 	int page_len = 0, ret = 0;
4816 
4817 	filemap_invalidate_lock_shared(mapping);
4818 	page_cache_ra_unbounded(&ractl, len, 0);
4819 	filemap_invalidate_unlock_shared(mapping);
4820 
4821 	do {
4822 		folio = read_cache_folio(mapping, page_idx, NULL, NULL);
4823 		if (IS_ERR(folio)) {
4824 			ret = PTR_ERR(folio);
4825 			break;
4826 		}
4827 		page_len += folio_nr_pages(folio) - (page_idx - folio->index);
4828 		page_idx = folio_next_index(folio);
4829 	} while (page_len < len);
4830 
4831 	while (redirty_idx < page_idx) {
4832 		folio = filemap_lock_folio(mapping, redirty_idx);
4833 
4834 		/* It will never fail, when folio has pinned above */
4835 		f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(folio));
4836 
4837 		f2fs_folio_wait_writeback(folio, true, true);
4838 
4839 		folio_mark_dirty(folio);
4840 		folio_set_f2fs_gcing(folio);
4841 		redirty_idx = folio_next_index(folio);
4842 		folio_unlock(folio);
4843 		folio_put_refs(folio, 2);
4844 	}
4845 
4846 	return ret;
4847 }
4848 
4849 static int f2fs_ioc_decompress_file(struct file *filp)
4850 {
4851 	struct inode *inode = file_inode(filp);
4852 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4853 	struct f2fs_inode_info *fi = F2FS_I(inode);
4854 	pgoff_t page_idx = 0, last_idx, cluster_idx;
4855 	int ret;
4856 
4857 	if (!f2fs_sb_has_compression(sbi) ||
4858 			F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4859 		return -EOPNOTSUPP;
4860 
4861 	if (!(filp->f_mode & FMODE_WRITE))
4862 		return -EBADF;
4863 
4864 	f2fs_balance_fs(sbi, true);
4865 
4866 	ret = mnt_want_write_file(filp);
4867 	if (ret)
4868 		return ret;
4869 	inode_lock(inode);
4870 
4871 	if (!f2fs_is_compress_backend_ready(inode)) {
4872 		ret = -EOPNOTSUPP;
4873 		goto out;
4874 	}
4875 
4876 	if (!f2fs_compressed_file(inode) ||
4877 		is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4878 		ret = -EINVAL;
4879 		goto out;
4880 	}
4881 
4882 	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4883 	if (ret)
4884 		goto out;
4885 
4886 	if (!atomic_read(&fi->i_compr_blocks))
4887 		goto out;
4888 
4889 	last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4890 	last_idx >>= fi->i_log_cluster_size;
4891 
4892 	for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
4893 		page_idx = cluster_idx << fi->i_log_cluster_size;
4894 
4895 		if (!f2fs_is_compressed_cluster(inode, page_idx))
4896 			continue;
4897 
4898 		ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
4899 		if (ret < 0)
4900 			break;
4901 
4902 		if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4903 			ret = filemap_fdatawrite(inode->i_mapping);
4904 			if (ret < 0)
4905 				break;
4906 		}
4907 
4908 		cond_resched();
4909 		if (fatal_signal_pending(current)) {
4910 			ret = -EINTR;
4911 			break;
4912 		}
4913 	}
4914 
4915 	if (!ret)
4916 		ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4917 							LLONG_MAX);
4918 
4919 	if (ret)
4920 		f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4921 			  __func__, ret);
4922 	f2fs_update_time(sbi, REQ_TIME);
4923 out:
4924 	inode_unlock(inode);
4925 	mnt_drop_write_file(filp);
4926 
4927 	return ret;
4928 }
4929 
4930 static int f2fs_ioc_compress_file(struct file *filp)
4931 {
4932 	struct inode *inode = file_inode(filp);
4933 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4934 	struct f2fs_inode_info *fi = F2FS_I(inode);
4935 	pgoff_t page_idx = 0, last_idx, cluster_idx;
4936 	int ret;
4937 
4938 	if (!f2fs_sb_has_compression(sbi) ||
4939 			F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4940 		return -EOPNOTSUPP;
4941 
4942 	if (!(filp->f_mode & FMODE_WRITE))
4943 		return -EBADF;
4944 
4945 	f2fs_balance_fs(sbi, true);
4946 
4947 	ret = mnt_want_write_file(filp);
4948 	if (ret)
4949 		return ret;
4950 	inode_lock(inode);
4951 
4952 	if (!f2fs_is_compress_backend_ready(inode)) {
4953 		ret = -EOPNOTSUPP;
4954 		goto out;
4955 	}
4956 
4957 	if (!f2fs_compressed_file(inode) ||
4958 		is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4959 		ret = -EINVAL;
4960 		goto out;
4961 	}
4962 
4963 	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4964 	if (ret)
4965 		goto out;
4966 
4967 	set_inode_flag(inode, FI_ENABLE_COMPRESS);
4968 
4969 	last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4970 	last_idx >>= fi->i_log_cluster_size;
4971 
4972 	for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
4973 		page_idx = cluster_idx << fi->i_log_cluster_size;
4974 
4975 		if (f2fs_is_sparse_cluster(inode, page_idx))
4976 			continue;
4977 
4978 		ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
4979 		if (ret < 0)
4980 			break;
4981 
4982 		if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4983 			ret = filemap_fdatawrite(inode->i_mapping);
4984 			if (ret < 0)
4985 				break;
4986 		}
4987 
4988 		cond_resched();
4989 		if (fatal_signal_pending(current)) {
4990 			ret = -EINTR;
4991 			break;
4992 		}
4993 	}
4994 
4995 	if (!ret)
4996 		ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4997 							LLONG_MAX);
4998 
4999 	clear_inode_flag(inode, FI_ENABLE_COMPRESS);
5000 
5001 	if (ret)
5002 		f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
5003 			  __func__, ret);
5004 	f2fs_update_time(sbi, REQ_TIME);
5005 out:
5006 	inode_unlock(inode);
5007 	mnt_drop_write_file(filp);
5008 
5009 	return ret;
5010 }
5011 
5012 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5013 {
5014 	switch (cmd) {
5015 	case FS_IOC_GETVERSION:
5016 		return f2fs_ioc_getversion(filp, arg);
5017 	case F2FS_IOC_START_ATOMIC_WRITE:
5018 		return f2fs_ioc_start_atomic_write(filp, false);
5019 	case F2FS_IOC_START_ATOMIC_REPLACE:
5020 		return f2fs_ioc_start_atomic_write(filp, true);
5021 	case F2FS_IOC_COMMIT_ATOMIC_WRITE:
5022 		return f2fs_ioc_commit_atomic_write(filp);
5023 	case F2FS_IOC_ABORT_ATOMIC_WRITE:
5024 		return f2fs_ioc_abort_atomic_write(filp);
5025 	case F2FS_IOC_START_VOLATILE_WRITE:
5026 	case F2FS_IOC_RELEASE_VOLATILE_WRITE:
5027 		return -EOPNOTSUPP;
5028 	case F2FS_IOC_SHUTDOWN:
5029 		return f2fs_ioc_shutdown(filp, arg);
5030 	case FITRIM:
5031 		return f2fs_ioc_fitrim(filp, arg);
5032 	case FS_IOC_SET_ENCRYPTION_POLICY:
5033 		return f2fs_ioc_set_encryption_policy(filp, arg);
5034 	case FS_IOC_GET_ENCRYPTION_POLICY:
5035 		return f2fs_ioc_get_encryption_policy(filp, arg);
5036 	case FS_IOC_GET_ENCRYPTION_PWSALT:
5037 		return f2fs_ioc_get_encryption_pwsalt(filp, arg);
5038 	case FS_IOC_GET_ENCRYPTION_POLICY_EX:
5039 		return f2fs_ioc_get_encryption_policy_ex(filp, arg);
5040 	case FS_IOC_ADD_ENCRYPTION_KEY:
5041 		return f2fs_ioc_add_encryption_key(filp, arg);
5042 	case FS_IOC_REMOVE_ENCRYPTION_KEY:
5043 		return f2fs_ioc_remove_encryption_key(filp, arg);
5044 	case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
5045 		return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
5046 	case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
5047 		return f2fs_ioc_get_encryption_key_status(filp, arg);
5048 	case FS_IOC_GET_ENCRYPTION_NONCE:
5049 		return f2fs_ioc_get_encryption_nonce(filp, arg);
5050 	case F2FS_IOC_GARBAGE_COLLECT:
5051 		return f2fs_ioc_gc(filp, arg);
5052 	case F2FS_IOC_GARBAGE_COLLECT_RANGE:
5053 		return f2fs_ioc_gc_range(filp, arg);
5054 	case F2FS_IOC_WRITE_CHECKPOINT:
5055 		return f2fs_ioc_write_checkpoint(filp);
5056 	case F2FS_IOC_DEFRAGMENT:
5057 		return f2fs_ioc_defragment(filp, arg);
5058 	case F2FS_IOC_MOVE_RANGE:
5059 		return f2fs_ioc_move_range(filp, arg);
5060 	case F2FS_IOC_FLUSH_DEVICE:
5061 		return f2fs_ioc_flush_device(filp, arg);
5062 	case F2FS_IOC_GET_FEATURES:
5063 		return f2fs_ioc_get_features(filp, arg);
5064 	case F2FS_IOC_GET_PIN_FILE:
5065 		return f2fs_ioc_get_pin_file(filp, arg);
5066 	case F2FS_IOC_SET_PIN_FILE:
5067 		return f2fs_ioc_set_pin_file(filp, arg);
5068 	case F2FS_IOC_PRECACHE_EXTENTS:
5069 		return f2fs_ioc_precache_extents(filp);
5070 	case F2FS_IOC_RESIZE_FS:
5071 		return f2fs_ioc_resize_fs(filp, arg);
5072 	case FS_IOC_ENABLE_VERITY:
5073 		return f2fs_ioc_enable_verity(filp, arg);
5074 	case FS_IOC_MEASURE_VERITY:
5075 		return f2fs_ioc_measure_verity(filp, arg);
5076 	case FS_IOC_READ_VERITY_METADATA:
5077 		return f2fs_ioc_read_verity_metadata(filp, arg);
5078 	case FS_IOC_GETFSLABEL:
5079 		return f2fs_ioc_getfslabel(filp, arg);
5080 	case FS_IOC_SETFSLABEL:
5081 		return f2fs_ioc_setfslabel(filp, arg);
5082 	case F2FS_IOC_GET_COMPRESS_BLOCKS:
5083 		return f2fs_ioc_get_compress_blocks(filp, arg);
5084 	case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
5085 		return f2fs_release_compress_blocks(filp, arg);
5086 	case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
5087 		return f2fs_reserve_compress_blocks(filp, arg);
5088 	case F2FS_IOC_SEC_TRIM_FILE:
5089 		return f2fs_sec_trim_file(filp, arg);
5090 	case F2FS_IOC_GET_COMPRESS_OPTION:
5091 		return f2fs_ioc_get_compress_option(filp, arg);
5092 	case F2FS_IOC_SET_COMPRESS_OPTION:
5093 		return f2fs_ioc_set_compress_option(filp, arg);
5094 	case F2FS_IOC_DECOMPRESS_FILE:
5095 		return f2fs_ioc_decompress_file(filp);
5096 	case F2FS_IOC_COMPRESS_FILE:
5097 		return f2fs_ioc_compress_file(filp);
5098 	case F2FS_IOC_GET_DEV_ALIAS_FILE:
5099 		return f2fs_ioc_get_dev_alias_file(filp, arg);
5100 	case F2FS_IOC_GET_DEV_ALIAS_STATUS:
5101 		return f2fs_ioc_get_dev_alias_status(filp, arg);
5102 	case F2FS_IOC_IO_PRIO:
5103 		return f2fs_ioc_io_prio(filp, arg);
5104 	case F2FS_IOC_RESERVE_DEV_ALIAS:
5105 		return f2fs_ioc_reserve_dev_alias(filp);
5106 	case F2FS_IOC_RELEASE_DEV_ALIAS:
5107 		return f2fs_ioc_release_dev_alias(filp);
5108 	default:
5109 		return -ENOTTY;
5110 	}
5111 }
5112 
5113 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5114 {
5115 	if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
5116 		return -EIO;
5117 	if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
5118 		return -ENOSPC;
5119 
5120 	return __f2fs_ioctl(filp, cmd, arg);
5121 }
5122 
5123 /*
5124  * Return %true if the given read or write request should use direct I/O, or
5125  * %false if it should use buffered I/O.
5126  */
5127 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
5128 				struct iov_iter *iter)
5129 {
5130 	unsigned int align;
5131 
5132 	if (!(iocb->ki_flags & IOCB_DIRECT))
5133 		return false;
5134 
5135 	if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
5136 		return false;
5137 
5138 	/*
5139 	 * Direct I/O not aligned to the disk's logical_block_size will be
5140 	 * attempted, but will fail with -EINVAL.
5141 	 *
5142 	 * f2fs additionally requires that direct I/O be aligned to the
5143 	 * filesystem block size, which is often a stricter requirement.
5144 	 * However, f2fs traditionally falls back to buffered I/O on requests
5145 	 * that are logical_block_size-aligned but not fs-block aligned.
5146 	 *
5147 	 * The below logic implements this behavior.
5148 	 */
5149 	align = iocb->ki_pos | iov_iter_alignment(iter);
5150 	if (!IS_ALIGNED(align, i_blocksize(inode)) &&
5151 	    IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
5152 		return false;
5153 
5154 	return true;
5155 }
5156 
5157 #ifdef CONFIG_F2FS_IOSTAT
5158 static void f2fs_dio_end_bio(struct bio *bio)
5159 {
5160 	struct bio_iostat_ctx *iostat_ctx = bio->bi_private;
5161 	void *orig_bi_private = iostat_ctx->post_read_ctx;
5162 
5163 	iostat_update_and_unbind_ctx(bio);
5164 	bio->bi_private = orig_bi_private;
5165 	iomap_dio_bio_end_io(bio);
5166 }
5167 
5168 static void f2fs_dio_iostat_start(struct f2fs_sb_info *sbi, struct bio *bio)
5169 {
5170 	void *bi_private = bio->bi_private;
5171 
5172 	if (!sbi->iostat_enable)
5173 		return;
5174 
5175 	iostat_alloc_and_bind_ctx(sbi, bio, bi_private);
5176 	iostat_update_submit_ctx(bio, DATA);
5177 	bio->bi_end_io = f2fs_dio_end_bio;
5178 }
5179 #else
5180 static inline void f2fs_dio_iostat_start(struct f2fs_sb_info *sbi,
5181 					 struct bio *bio) {}
5182 #endif
5183 
5184 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
5185 				unsigned int flags)
5186 {
5187 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
5188 
5189 	dec_cache_count(sbi, F2FS_DIO_READ);
5190 	if (error)
5191 		return error;
5192 	f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
5193 	return 0;
5194 }
5195 
5196 static void f2fs_dio_read_submit_io(const struct iomap_iter *iter,
5197 					struct bio *bio, loff_t file_offset)
5198 {
5199 	struct f2fs_sb_info *sbi = F2FS_I_SB(iter->inode);
5200 
5201 	f2fs_dio_iostat_start(sbi, bio);
5202 	blk_crypto_submit_bio(bio);
5203 }
5204 
5205 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
5206 	.end_io = f2fs_dio_read_end_io,
5207 	.submit_io = f2fs_dio_read_submit_io,
5208 };
5209 
5210 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
5211 {
5212 	struct file *file = iocb->ki_filp;
5213 	struct inode *inode = file_inode(file);
5214 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
5215 	struct f2fs_inode_info *fi = F2FS_I(inode);
5216 	const loff_t pos = iocb->ki_pos;
5217 	const size_t count = iov_iter_count(to);
5218 	struct iomap_dio *dio;
5219 	ssize_t ret;
5220 
5221 	if (count == 0)
5222 		return 0; /* skip atime update */
5223 
5224 	trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
5225 
5226 	if (iocb->ki_flags & IOCB_NOWAIT) {
5227 		if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
5228 			ret = -EAGAIN;
5229 			goto out;
5230 		}
5231 	} else {
5232 		f2fs_down_read(&fi->i_gc_rwsem[READ]);
5233 	}
5234 
5235 	/* dio is not compatible w/ atomic file */
5236 	if (f2fs_is_atomic_file(inode)) {
5237 		f2fs_up_read(&fi->i_gc_rwsem[READ]);
5238 		ret = -EOPNOTSUPP;
5239 		goto out;
5240 	}
5241 
5242 	/*
5243 	 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
5244 	 * the higher-level function iomap_dio_rw() in order to ensure that the
5245 	 * F2FS_DIO_READ counter will be decremented correctly in all cases.
5246 	 */
5247 	inc_cache_count(sbi, F2FS_DIO_READ);
5248 	dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
5249 			     &f2fs_iomap_dio_read_ops, 0, NULL, 0);
5250 	if (IS_ERR_OR_NULL(dio)) {
5251 		ret = PTR_ERR_OR_ZERO(dio);
5252 		if (ret != -EIOCBQUEUED)
5253 			dec_cache_count(sbi, F2FS_DIO_READ);
5254 	} else {
5255 		ret = iomap_dio_complete(dio);
5256 	}
5257 
5258 	f2fs_up_read(&fi->i_gc_rwsem[READ]);
5259 
5260 	file_accessed(file);
5261 out:
5262 	trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
5263 	return ret;
5264 }
5265 
5266 static void f2fs_trace_rw_file_path(struct file *file, loff_t pos, size_t count,
5267 				    int rw)
5268 {
5269 	struct inode *inode = file_inode(file);
5270 	char *buf, *path;
5271 
5272 	buf = f2fs_getname(F2FS_I_SB(inode));
5273 	if (!buf)
5274 		return;
5275 	path = dentry_path_raw(file_dentry(file), buf, PATH_MAX);
5276 	if (IS_ERR(path))
5277 		goto free_buf;
5278 	if (rw == WRITE)
5279 		trace_f2fs_datawrite_start(inode, pos, count,
5280 				current->pid, path, current->comm);
5281 	else
5282 		trace_f2fs_dataread_start(inode, pos, count,
5283 				current->pid, path, current->comm);
5284 free_buf:
5285 	f2fs_putname(buf);
5286 }
5287 
5288 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
5289 {
5290 	struct inode *inode = file_inode(iocb->ki_filp);
5291 	const loff_t pos = iocb->ki_pos;
5292 	ssize_t ret;
5293 	bool dio;
5294 
5295 	if (!f2fs_is_compress_backend_ready(inode))
5296 		return -EOPNOTSUPP;
5297 
5298 	if (trace_f2fs_dataread_start_enabled())
5299 		f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
5300 					iov_iter_count(to), READ);
5301 
5302 	dio = f2fs_should_use_dio(inode, iocb, to);
5303 
5304 	/* In LFS mode, if there is inflight dio, wait for its completion */
5305 	if (f2fs_lfs_mode(F2FS_I_SB(inode)) &&
5306 	    get_nr_caches(F2FS_I_SB(inode), F2FS_DIO_WRITE) &&
5307 		(!f2fs_is_pinned_file(inode) || !dio))
5308 		inode_dio_wait(inode);
5309 
5310 	if (dio) {
5311 		ret = f2fs_dio_read_iter(iocb, to);
5312 	} else {
5313 		ret = filemap_read(iocb, to, 0);
5314 		if (ret > 0)
5315 			f2fs_update_iostat(F2FS_I_SB(inode), inode,
5316 						APP_BUFFERED_READ_IO, ret);
5317 	}
5318 	trace_f2fs_dataread_end(inode, pos, ret);
5319 	return ret;
5320 }
5321 
5322 static ssize_t f2fs_file_splice_read(struct file *in, loff_t *ppos,
5323 				     struct pipe_inode_info *pipe,
5324 				     size_t len, unsigned int flags)
5325 {
5326 	struct inode *inode = file_inode(in);
5327 	const loff_t pos = *ppos;
5328 	ssize_t ret;
5329 
5330 	if (!f2fs_is_compress_backend_ready(inode))
5331 		return -EOPNOTSUPP;
5332 
5333 	if (trace_f2fs_dataread_start_enabled())
5334 		f2fs_trace_rw_file_path(in, pos, len, READ);
5335 
5336 	ret = filemap_splice_read(in, ppos, pipe, len, flags);
5337 	if (ret > 0)
5338 		f2fs_update_iostat(F2FS_I_SB(inode), inode,
5339 				   APP_BUFFERED_READ_IO, ret);
5340 
5341 	trace_f2fs_dataread_end(inode, pos, ret);
5342 	return ret;
5343 }
5344 
5345 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
5346 {
5347 	struct file *file = iocb->ki_filp;
5348 	struct inode *inode = file_inode(file);
5349 	ssize_t count;
5350 	int err;
5351 
5352 	if (IS_IMMUTABLE(inode))
5353 		return -EPERM;
5354 
5355 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
5356 		return -EPERM;
5357 
5358 	count = generic_write_checks(iocb, from);
5359 	if (count <= 0)
5360 		return count;
5361 
5362 	err = file_modified(file);
5363 	if (err)
5364 		return err;
5365 
5366 	err = f2fs_zero_post_eof_page(inode,
5367 		iocb->ki_pos + iov_iter_count(from), true, true);
5368 	if (err)
5369 		return err;
5370 	return count;
5371 }
5372 
5373 /*
5374  * Preallocate blocks for a write request, if it is possible and helpful to do
5375  * so.  Returns a positive number if blocks may have been preallocated, 0 if no
5376  * blocks were preallocated, or a negative errno value if something went
5377  * seriously wrong.  Also sets FI_PREALLOCATED_ALL on the inode if *all* the
5378  * requested blocks (not just some of them) have been allocated.
5379  */
5380 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
5381 				   bool dio)
5382 {
5383 	struct inode *inode = file_inode(iocb->ki_filp);
5384 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
5385 	const loff_t pos = iocb->ki_pos;
5386 	const size_t count = iov_iter_count(iter);
5387 	struct f2fs_map_blocks map = {};
5388 	int flag;
5389 	int ret;
5390 
5391 	/* If it will be an out-of-place direct write, don't bother. */
5392 	if (dio && f2fs_lfs_mode(sbi))
5393 		return 0;
5394 	/*
5395 	 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
5396 	 * buffered IO, if DIO meets any holes.
5397 	 */
5398 	if (dio && i_size_read(inode) &&
5399 		(F2FS_BYTES_TO_BLK(sbi, pos) <
5400 		 F2FS_BLK_ALIGN(sbi, i_size_read(inode))))
5401 		return 0;
5402 
5403 	/* No-wait I/O can't allocate blocks. */
5404 	if (iocb->ki_flags & IOCB_NOWAIT)
5405 		return 0;
5406 
5407 	/* If it will be a short write, don't bother. */
5408 	if (fault_in_iov_iter_readable(iter, count))
5409 		return 0;
5410 
5411 	if (f2fs_has_inline_data(inode)) {
5412 		/* If the data will fit inline, don't bother. */
5413 		if (pos + count <= MAX_INLINE_DATA(inode))
5414 			return 0;
5415 		ret = f2fs_convert_inline_inode(inode);
5416 		if (ret)
5417 			return ret;
5418 	}
5419 
5420 	/* Do not preallocate blocks that will be written partially in 4KB. */
5421 	map.m_lblk = F2FS_BLK_ALIGN(sbi, pos);
5422 	map.m_len = F2FS_BYTES_TO_BLK(sbi, pos + count);
5423 	if (map.m_len > map.m_lblk)
5424 		map.m_len -= map.m_lblk;
5425 	else
5426 		return 0;
5427 
5428 	if (!IS_DEVICE_ALIASING(inode))
5429 		map.m_may_create = true;
5430 	if (dio) {
5431 		map.m_seg_type = f2fs_rw_hint_to_seg_type(sbi,
5432 						inode->i_write_hint);
5433 		flag = F2FS_GET_BLOCK_PRE_DIO;
5434 	} else {
5435 		map.m_seg_type = NO_CHECK_TYPE;
5436 		flag = F2FS_GET_BLOCK_PRE_AIO;
5437 	}
5438 
5439 	ret = f2fs_map_blocks(inode, &map, flag);
5440 	/* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
5441 	if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
5442 		return ret;
5443 	if (ret == 0)
5444 		set_inode_flag(inode, FI_PREALLOCATED_ALL);
5445 	return map.m_len;
5446 }
5447 
5448 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
5449 					struct iov_iter *from)
5450 {
5451 	struct file *file = iocb->ki_filp;
5452 	struct inode *inode = file_inode(file);
5453 	ssize_t ret;
5454 
5455 	if (iocb->ki_flags & IOCB_NOWAIT)
5456 		return -EOPNOTSUPP;
5457 
5458 	ret = generic_perform_write(iocb, from);
5459 
5460 	if (ret > 0) {
5461 		f2fs_update_iostat(F2FS_I_SB(inode), inode,
5462 						APP_BUFFERED_IO, ret);
5463 	}
5464 	return ret;
5465 }
5466 
5467 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
5468 				 unsigned int flags)
5469 {
5470 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
5471 
5472 	dec_cache_count(sbi, F2FS_DIO_WRITE);
5473 	if (error)
5474 		return error;
5475 	f2fs_update_time(sbi, REQ_TIME);
5476 	f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
5477 	return 0;
5478 }
5479 
5480 static bool f2fs_valid_write_stream(struct f2fs_sb_info *sbi, u8 write_stream)
5481 {
5482 	int i;
5483 
5484 	if (!write_stream)
5485 		return true;
5486 	if (!f2fs_is_multi_device(sbi))
5487 		return write_stream <= bdev_max_write_streams(sbi->sb->s_bdev);
5488 
5489 	for (i = 0; i < sbi->s_ndevs; i++)
5490 		if (write_stream > bdev_max_write_streams(FDEV(i).bdev))
5491 			return false;
5492 	return true;
5493 }
5494 
5495 static void f2fs_dio_write_submit_io(const struct iomap_iter *iter,
5496 					struct bio *bio, loff_t file_offset)
5497 {
5498 	struct inode *inode = iter->inode;
5499 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
5500 	struct kiocb *iocb = iter->private;
5501 	enum log_type type = f2fs_rw_hint_to_seg_type(sbi, inode->i_write_hint);
5502 	enum temp_type temp = f2fs_get_segment_temp(sbi, type);
5503 
5504 	bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, DATA, temp);
5505 	bio->bi_write_stream =
5506 		iocb->ki_write_stream ? iocb->ki_write_stream :
5507 		f2fs_io_type_to_write_stream(bio->bi_bdev, DATA, temp);
5508 	f2fs_dio_iostat_start(sbi, bio);
5509 	blk_crypto_submit_bio(bio);
5510 }
5511 
5512 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
5513 	.end_io		= f2fs_dio_write_end_io,
5514 	.submit_io	= f2fs_dio_write_submit_io,
5515 };
5516 
5517 static void f2fs_flush_buffered_write(struct address_space *mapping,
5518 				      loff_t start_pos, loff_t end_pos)
5519 {
5520 	int ret;
5521 
5522 	ret = filemap_write_and_wait_range(mapping, start_pos, end_pos);
5523 	if (ret < 0)
5524 		return;
5525 	invalidate_mapping_pages(mapping,
5526 				 start_pos >> PAGE_SHIFT,
5527 				 end_pos >> PAGE_SHIFT);
5528 }
5529 
5530 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
5531 				   bool *may_need_sync)
5532 {
5533 	struct file *file = iocb->ki_filp;
5534 	struct inode *inode = file_inode(file);
5535 	struct f2fs_inode_info *fi = F2FS_I(inode);
5536 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
5537 	const bool do_opu = f2fs_lfs_mode(sbi);
5538 	const loff_t pos = iocb->ki_pos;
5539 	const ssize_t count = iov_iter_count(from);
5540 	unsigned int dio_flags;
5541 	struct iomap_dio *dio;
5542 	ssize_t ret;
5543 
5544 	trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
5545 
5546 	if (!f2fs_valid_write_stream(sbi, iocb->ki_write_stream)) {
5547 		ret = -EINVAL;
5548 		goto out;
5549 	}
5550 
5551 	if (iocb->ki_flags & IOCB_NOWAIT) {
5552 		/* f2fs_convert_inline_inode() and block allocation can block */
5553 		if (f2fs_has_inline_data(inode) ||
5554 		    !f2fs_overwrite_io(inode, pos, count)) {
5555 			ret = -EAGAIN;
5556 			goto out;
5557 		}
5558 
5559 		if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
5560 			ret = -EAGAIN;
5561 			goto out;
5562 		}
5563 		if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
5564 			f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
5565 			ret = -EAGAIN;
5566 			goto out;
5567 		}
5568 	} else {
5569 		ret = f2fs_convert_inline_inode(inode);
5570 		if (ret)
5571 			goto out;
5572 
5573 		f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
5574 		if (do_opu)
5575 			f2fs_down_read(&fi->i_gc_rwsem[READ]);
5576 	}
5577 
5578 	/*
5579 	 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
5580 	 * the higher-level function iomap_dio_rw() in order to ensure that the
5581 	 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
5582 	 */
5583 	inc_cache_count(sbi, F2FS_DIO_WRITE);
5584 	dio_flags = 0;
5585 	if (pos + count > inode->i_size)
5586 		dio_flags |= IOMAP_DIO_FORCE_WAIT;
5587 	dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
5588 			     &f2fs_iomap_dio_write_ops, dio_flags, iocb, 0);
5589 	if (IS_ERR_OR_NULL(dio)) {
5590 		ret = PTR_ERR_OR_ZERO(dio);
5591 		if (ret == -ENOTBLK)
5592 			ret = 0;
5593 		if (ret != -EIOCBQUEUED)
5594 			dec_cache_count(sbi, F2FS_DIO_WRITE);
5595 	} else {
5596 		ret = iomap_dio_complete(dio);
5597 	}
5598 
5599 	if (do_opu)
5600 		f2fs_up_read(&fi->i_gc_rwsem[READ]);
5601 	f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
5602 
5603 	if (ret < 0)
5604 		goto out;
5605 	if (pos + ret > inode->i_size)
5606 		f2fs_i_size_write(inode, pos + ret);
5607 	if (!do_opu)
5608 		set_inode_flag(inode, FI_UPDATE_WRITE);
5609 
5610 	if (iov_iter_count(from)) {
5611 		ssize_t ret2;
5612 		loff_t bufio_start_pos = iocb->ki_pos;
5613 
5614 		/*
5615 		 * The direct write was partial, so we need to fall back to a
5616 		 * buffered write for the remainder.
5617 		 */
5618 
5619 		ret2 = f2fs_buffered_write_iter(iocb, from);
5620 		if (iov_iter_count(from))
5621 			f2fs_write_failed(inode, iocb->ki_pos);
5622 		if (ret2 < 0)
5623 			goto out;
5624 
5625 		/*
5626 		 * Ensure that the pagecache pages are written to disk and
5627 		 * invalidated to preserve the expected O_DIRECT semantics.
5628 		 */
5629 		if (ret2 > 0) {
5630 			loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
5631 
5632 			ret += ret2;
5633 
5634 			f2fs_flush_buffered_write(file->f_mapping,
5635 						  bufio_start_pos,
5636 						  bufio_end_pos);
5637 		}
5638 	} else {
5639 		/* iomap_dio_rw() already handled the generic_write_sync(). */
5640 		*may_need_sync = false;
5641 	}
5642 out:
5643 	trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
5644 	return ret;
5645 }
5646 
5647 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
5648 {
5649 	struct inode *inode = file_inode(iocb->ki_filp);
5650 	const loff_t orig_pos = iocb->ki_pos;
5651 	const size_t orig_count = iov_iter_count(from);
5652 	loff_t target_size;
5653 	bool dio;
5654 	bool may_need_sync = true;
5655 	int preallocated;
5656 	ssize_t ret;
5657 	loff_t bufio_start_pos;
5658 
5659 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
5660 		ret = -EIO;
5661 		goto out;
5662 	}
5663 
5664 	if (!f2fs_is_compress_backend_ready(inode)) {
5665 		ret = -EOPNOTSUPP;
5666 		goto out;
5667 	}
5668 
5669 	if (iocb->ki_flags & IOCB_NOWAIT) {
5670 		if (!inode_trylock(inode)) {
5671 			ret = -EAGAIN;
5672 			goto out;
5673 		}
5674 	} else {
5675 		inode_lock(inode);
5676 	}
5677 
5678 	ret = f2fs_write_checks(iocb, from);
5679 	if (ret <= 0)
5680 		goto out_unlock;
5681 
5682 	if (f2fs_is_pinned_file(inode) &&
5683 	    !f2fs_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from))) {
5684 		ret = -EIO;
5685 		goto out_unlock;
5686 	}
5687 
5688 	bufio_start_pos = iocb->ki_pos;
5689 
5690 	/* Determine whether we will do a direct write or a buffered write. */
5691 	dio = f2fs_should_use_dio(inode, iocb, from);
5692 
5693 	/* dio is not compatible w/ atomic write */
5694 	if (dio && f2fs_is_atomic_file(inode)) {
5695 		ret = -EOPNOTSUPP;
5696 		goto out_unlock;
5697 	}
5698 
5699 	/* Possibly preallocate the blocks for the write. */
5700 	target_size = iocb->ki_pos + iov_iter_count(from);
5701 	preallocated = f2fs_preallocate_blocks(iocb, from, dio);
5702 	if (preallocated < 0) {
5703 		ret = preallocated;
5704 	} else {
5705 		if (trace_f2fs_datawrite_start_enabled())
5706 			f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
5707 						orig_count, WRITE);
5708 
5709 		/* Do the actual write. */
5710 		ret = dio ?
5711 			f2fs_dio_write_iter(iocb, from, &may_need_sync) :
5712 			f2fs_buffered_write_iter(iocb, from);
5713 
5714 		trace_f2fs_datawrite_end(inode, orig_pos, ret);
5715 	}
5716 
5717 	/* Don't leave any preallocated blocks around past i_size. */
5718 	if (preallocated && i_size_read(inode) < target_size) {
5719 		f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
5720 		filemap_invalidate_lock(inode->i_mapping);
5721 		if (!f2fs_truncate(inode))
5722 			file_dont_truncate(inode);
5723 		filemap_invalidate_unlock(inode->i_mapping);
5724 		f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
5725 	} else {
5726 		file_dont_truncate(inode);
5727 	}
5728 
5729 	clear_inode_flag(inode, FI_PREALLOCATED_ALL);
5730 out_unlock:
5731 	inode_unlock(inode);
5732 out:
5733 	trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
5734 
5735 	if (ret > 0 && may_need_sync)
5736 		ret = generic_write_sync(iocb, ret);
5737 
5738 	/* If buffered IO was forced, flush and drop the data from
5739 	 * the page cache to preserve O_DIRECT semantics
5740 	 */
5741 	if (ret > 0 && !dio && (iocb->ki_flags & IOCB_DIRECT))
5742 		f2fs_flush_buffered_write(iocb->ki_filp->f_mapping,
5743 					  bufio_start_pos,
5744 					  bufio_start_pos + ret - 1);
5745 
5746 	return ret;
5747 }
5748 
5749 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
5750 		int advice)
5751 {
5752 	struct address_space *mapping;
5753 	struct backing_dev_info *bdi;
5754 	struct inode *inode = file_inode(filp);
5755 	int err;
5756 
5757 	trace_f2fs_fadvise(inode, offset, len, advice);
5758 
5759 	if (advice == POSIX_FADV_SEQUENTIAL) {
5760 		if (S_ISFIFO(inode->i_mode))
5761 			return -ESPIPE;
5762 
5763 		mapping = filp->f_mapping;
5764 		if (!mapping || len < 0)
5765 			return -EINVAL;
5766 
5767 		bdi = inode_to_bdi(mapping->host);
5768 		filp->f_ra.ra_pages = bdi->ra_pages *
5769 			F2FS_I_SB(inode)->seq_file_ra_mul;
5770 		spin_lock(&filp->f_lock);
5771 		filp->f_mode &= ~FMODE_RANDOM;
5772 		spin_unlock(&filp->f_lock);
5773 		return 0;
5774 	} else if (advice == POSIX_FADV_WILLNEED && offset == 0) {
5775 		/* Load extent cache at the first readahead. */
5776 		f2fs_precache_extents(inode);
5777 	}
5778 
5779 	err = generic_fadvise(filp, offset, len, advice);
5780 	if (err)
5781 		return err;
5782 
5783 	if (advice == POSIX_FADV_DONTNEED &&
5784 	    (test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
5785 	     f2fs_compressed_file(inode)))
5786 		f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
5787 	else if (advice == POSIX_FADV_NOREUSE)
5788 		err = f2fs_keep_noreuse_range(inode, offset, len);
5789 	return err;
5790 }
5791 
5792 #ifdef CONFIG_COMPAT
5793 struct compat_f2fs_gc_range {
5794 	u32 sync;
5795 	compat_u64 start;
5796 	compat_u64 len;
5797 };
5798 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,\
5799 						struct compat_f2fs_gc_range)
5800 
5801 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
5802 {
5803 	struct compat_f2fs_gc_range __user *urange;
5804 	struct f2fs_gc_range range;
5805 	int err;
5806 
5807 	urange = compat_ptr(arg);
5808 	err = get_user(range.sync, &urange->sync);
5809 	err |= get_user(range.start, &urange->start);
5810 	err |= get_user(range.len, &urange->len);
5811 	if (err)
5812 		return -EFAULT;
5813 
5814 	return __f2fs_ioc_gc_range(file, &range);
5815 }
5816 
5817 struct compat_f2fs_move_range {
5818 	u32 dst_fd;
5819 	compat_u64 pos_in;
5820 	compat_u64 pos_out;
5821 	compat_u64 len;
5822 };
5823 #define F2FS_IOC32_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
5824 					struct compat_f2fs_move_range)
5825 
5826 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
5827 {
5828 	struct compat_f2fs_move_range __user *urange;
5829 	struct f2fs_move_range range;
5830 	int err;
5831 
5832 	urange = compat_ptr(arg);
5833 	err = get_user(range.dst_fd, &urange->dst_fd);
5834 	err |= get_user(range.pos_in, &urange->pos_in);
5835 	err |= get_user(range.pos_out, &urange->pos_out);
5836 	err |= get_user(range.len, &urange->len);
5837 	if (err)
5838 		return -EFAULT;
5839 
5840 	return __f2fs_ioc_move_range(file, &range);
5841 }
5842 
5843 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5844 {
5845 	if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
5846 		return -EIO;
5847 	if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
5848 		return -ENOSPC;
5849 
5850 	switch (cmd) {
5851 	case FS_IOC32_GETVERSION:
5852 		cmd = FS_IOC_GETVERSION;
5853 		break;
5854 	case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
5855 		return f2fs_compat_ioc_gc_range(file, arg);
5856 	case F2FS_IOC32_MOVE_RANGE:
5857 		return f2fs_compat_ioc_move_range(file, arg);
5858 	case F2FS_IOC_START_ATOMIC_WRITE:
5859 	case F2FS_IOC_START_ATOMIC_REPLACE:
5860 	case F2FS_IOC_COMMIT_ATOMIC_WRITE:
5861 	case F2FS_IOC_START_VOLATILE_WRITE:
5862 	case F2FS_IOC_RELEASE_VOLATILE_WRITE:
5863 	case F2FS_IOC_ABORT_ATOMIC_WRITE:
5864 	case F2FS_IOC_SHUTDOWN:
5865 	case FITRIM:
5866 	case FS_IOC_SET_ENCRYPTION_POLICY:
5867 	case FS_IOC_GET_ENCRYPTION_PWSALT:
5868 	case FS_IOC_GET_ENCRYPTION_POLICY:
5869 	case FS_IOC_GET_ENCRYPTION_POLICY_EX:
5870 	case FS_IOC_ADD_ENCRYPTION_KEY:
5871 	case FS_IOC_REMOVE_ENCRYPTION_KEY:
5872 	case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
5873 	case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
5874 	case FS_IOC_GET_ENCRYPTION_NONCE:
5875 	case F2FS_IOC_GARBAGE_COLLECT:
5876 	case F2FS_IOC_WRITE_CHECKPOINT:
5877 	case F2FS_IOC_DEFRAGMENT:
5878 	case F2FS_IOC_FLUSH_DEVICE:
5879 	case F2FS_IOC_GET_FEATURES:
5880 	case F2FS_IOC_GET_PIN_FILE:
5881 	case F2FS_IOC_SET_PIN_FILE:
5882 	case F2FS_IOC_PRECACHE_EXTENTS:
5883 	case F2FS_IOC_RESIZE_FS:
5884 	case FS_IOC_ENABLE_VERITY:
5885 	case FS_IOC_MEASURE_VERITY:
5886 	case FS_IOC_READ_VERITY_METADATA:
5887 	case FS_IOC_GETFSLABEL:
5888 	case FS_IOC_SETFSLABEL:
5889 	case F2FS_IOC_GET_COMPRESS_BLOCKS:
5890 	case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
5891 	case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
5892 	case F2FS_IOC_SEC_TRIM_FILE:
5893 	case F2FS_IOC_GET_COMPRESS_OPTION:
5894 	case F2FS_IOC_SET_COMPRESS_OPTION:
5895 	case F2FS_IOC_DECOMPRESS_FILE:
5896 	case F2FS_IOC_COMPRESS_FILE:
5897 	case F2FS_IOC_GET_DEV_ALIAS_FILE:
5898 	case F2FS_IOC_GET_DEV_ALIAS_STATUS:
5899 	case F2FS_IOC_IO_PRIO:
5900 	case F2FS_IOC_RESERVE_DEV_ALIAS:
5901 	case F2FS_IOC_RELEASE_DEV_ALIAS:
5902 		break;
5903 	default:
5904 		return -ENOIOCTLCMD;
5905 	}
5906 	return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5907 }
5908 #endif
5909 
5910 const struct file_operations f2fs_file_operations = {
5911 	.llseek		= f2fs_llseek,
5912 	.read_iter	= f2fs_file_read_iter,
5913 	.write_iter	= f2fs_file_write_iter,
5914 	.iopoll		= iocb_bio_iopoll,
5915 	.open		= f2fs_file_open,
5916 	.release	= f2fs_release_file,
5917 	.mmap_prepare	= f2fs_file_mmap_prepare,
5918 	.flush		= f2fs_file_flush,
5919 	.fsync		= f2fs_sync_file,
5920 	.fallocate	= f2fs_fallocate,
5921 	.unlocked_ioctl	= f2fs_ioctl,
5922 #ifdef CONFIG_COMPAT
5923 	.compat_ioctl	= f2fs_compat_ioctl,
5924 #endif
5925 	.splice_read	= f2fs_file_splice_read,
5926 	.splice_write	= iter_file_splice_write,
5927 	.fadvise	= f2fs_file_fadvise,
5928 	.fop_flags	= FOP_BUFFER_RASYNC,
5929 	.setlease	= generic_setlease,
5930 };
5931