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