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