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