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