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