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