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