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