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