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