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