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