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

source code of linux/fs/f2fs/file.c