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