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