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