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