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