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