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