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