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