Merge drm/drm-fixes into drm-misc-fixes
[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_put_dnode(&dn);
121                 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
122         }
123
124 #ifdef CONFIG_F2FS_FS_COMPRESSION
125         if (!need_alloc) {
126                 set_new_dnode(&dn, inode, NULL, NULL, 0);
127                 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
128                 f2fs_put_dnode(&dn);
129         }
130 #endif
131         if (err) {
132                 unlock_page(page);
133                 goto out_sem;
134         }
135
136         f2fs_wait_on_page_writeback(page, DATA, false, true);
137
138         /* wait for GCed page writeback via META_MAPPING */
139         f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
140
141         /*
142          * check to see if the page is mapped already (no holes)
143          */
144         if (PageMappedToDisk(page))
145                 goto out_sem;
146
147         /* page is wholly or partially inside EOF */
148         if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
149                                                 i_size_read(inode)) {
150                 loff_t offset;
151
152                 offset = i_size_read(inode) & ~PAGE_MASK;
153                 zero_user_segment(page, offset, PAGE_SIZE);
154         }
155         set_page_dirty(page);
156         if (!PageUptodate(page))
157                 SetPageUptodate(page);
158
159         f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
160         f2fs_update_time(sbi, REQ_TIME);
161
162         trace_f2fs_vm_page_mkwrite(page, DATA);
163 out_sem:
164         up_read(&F2FS_I(inode)->i_mmap_sem);
165
166         sb_end_pagefault(inode->i_sb);
167 err:
168         return block_page_mkwrite_return(err);
169 }
170
171 static const struct vm_operations_struct f2fs_file_vm_ops = {
172         .fault          = f2fs_filemap_fault,
173         .map_pages      = filemap_map_pages,
174         .page_mkwrite   = f2fs_vm_page_mkwrite,
175 };
176
177 static int get_parent_ino(struct inode *inode, nid_t *pino)
178 {
179         struct dentry *dentry;
180
181         /*
182          * Make sure to get the non-deleted alias.  The alias associated with
183          * the open file descriptor being fsync()'ed may be deleted already.
184          */
185         dentry = d_find_alias(inode);
186         if (!dentry)
187                 return 0;
188
189         *pino = parent_ino(dentry);
190         dput(dentry);
191         return 1;
192 }
193
194 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
195 {
196         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
197         enum cp_reason_type cp_reason = CP_NO_NEEDED;
198
199         if (!S_ISREG(inode->i_mode))
200                 cp_reason = CP_NON_REGULAR;
201         else if (f2fs_compressed_file(inode))
202                 cp_reason = CP_COMPRESSED;
203         else if (inode->i_nlink != 1)
204                 cp_reason = CP_HARDLINK;
205         else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
206                 cp_reason = CP_SB_NEED_CP;
207         else if (file_wrong_pino(inode))
208                 cp_reason = CP_WRONG_PINO;
209         else if (!f2fs_space_for_roll_forward(sbi))
210                 cp_reason = CP_NO_SPC_ROLL;
211         else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
212                 cp_reason = CP_NODE_NEED_CP;
213         else if (test_opt(sbi, FASTBOOT))
214                 cp_reason = CP_FASTBOOT_MODE;
215         else if (F2FS_OPTION(sbi).active_logs == 2)
216                 cp_reason = CP_SPEC_LOG_NUM;
217         else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
218                 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
219                 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
220                                                         TRANS_DIR_INO))
221                 cp_reason = CP_RECOVER_DIR;
222
223         return cp_reason;
224 }
225
226 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
227 {
228         struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
229         bool ret = false;
230         /* But we need to avoid that there are some inode updates */
231         if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
232                 ret = true;
233         f2fs_put_page(i, 0);
234         return ret;
235 }
236
237 static void try_to_fix_pino(struct inode *inode)
238 {
239         struct f2fs_inode_info *fi = F2FS_I(inode);
240         nid_t pino;
241
242         down_write(&fi->i_sem);
243         if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
244                         get_parent_ino(inode, &pino)) {
245                 f2fs_i_pino_write(inode, pino);
246                 file_got_pino(inode);
247         }
248         up_write(&fi->i_sem);
249 }
250
251 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
252                                                 int datasync, bool atomic)
253 {
254         struct inode *inode = file->f_mapping->host;
255         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
256         nid_t ino = inode->i_ino;
257         int ret = 0;
258         enum cp_reason_type cp_reason = 0;
259         struct writeback_control wbc = {
260                 .sync_mode = WB_SYNC_ALL,
261                 .nr_to_write = LONG_MAX,
262                 .for_reclaim = 0,
263         };
264         unsigned int seq_id = 0;
265
266         if (unlikely(f2fs_readonly(inode->i_sb) ||
267                                 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
268                 return 0;
269
270         trace_f2fs_sync_file_enter(inode);
271
272         if (S_ISDIR(inode->i_mode))
273                 goto go_write;
274
275         /* if fdatasync is triggered, let's do in-place-update */
276         if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
277                 set_inode_flag(inode, FI_NEED_IPU);
278         ret = file_write_and_wait_range(file, start, end);
279         clear_inode_flag(inode, FI_NEED_IPU);
280
281         if (ret) {
282                 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
283                 return ret;
284         }
285
286         /* if the inode is dirty, let's recover all the time */
287         if (!f2fs_skip_inode_update(inode, datasync)) {
288                 f2fs_write_inode(inode, NULL);
289                 goto go_write;
290         }
291
292         /*
293          * if there is no written data, don't waste time to write recovery info.
294          */
295         if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
296                         !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
297
298                 /* it may call write_inode just prior to fsync */
299                 if (need_inode_page_update(sbi, ino))
300                         goto go_write;
301
302                 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
303                                 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
304                         goto flush_out;
305                 goto out;
306         }
307 go_write:
308         /*
309          * Both of fdatasync() and fsync() are able to be recovered from
310          * sudden-power-off.
311          */
312         down_read(&F2FS_I(inode)->i_sem);
313         cp_reason = need_do_checkpoint(inode);
314         up_read(&F2FS_I(inode)->i_sem);
315
316         if (cp_reason) {
317                 /* all the dirty node pages should be flushed for POR */
318                 ret = f2fs_sync_fs(inode->i_sb, 1);
319
320                 /*
321                  * We've secured consistency through sync_fs. Following pino
322                  * will be used only for fsynced inodes after checkpoint.
323                  */
324                 try_to_fix_pino(inode);
325                 clear_inode_flag(inode, FI_APPEND_WRITE);
326                 clear_inode_flag(inode, FI_UPDATE_WRITE);
327                 goto out;
328         }
329 sync_nodes:
330         atomic_inc(&sbi->wb_sync_req[NODE]);
331         ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
332         atomic_dec(&sbi->wb_sync_req[NODE]);
333         if (ret)
334                 goto out;
335
336         /* if cp_error was enabled, we should avoid infinite loop */
337         if (unlikely(f2fs_cp_error(sbi))) {
338                 ret = -EIO;
339                 goto out;
340         }
341
342         if (f2fs_need_inode_block_update(sbi, ino)) {
343                 f2fs_mark_inode_dirty_sync(inode, true);
344                 f2fs_write_inode(inode, NULL);
345                 goto sync_nodes;
346         }
347
348         /*
349          * If it's atomic_write, it's just fine to keep write ordering. So
350          * here we don't need to wait for node write completion, since we use
351          * node chain which serializes node blocks. If one of node writes are
352          * reordered, we can see simply broken chain, resulting in stopping
353          * roll-forward recovery. It means we'll recover all or none node blocks
354          * given fsync mark.
355          */
356         if (!atomic) {
357                 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
358                 if (ret)
359                         goto out;
360         }
361
362         /* once recovery info is written, don't need to tack this */
363         f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
364         clear_inode_flag(inode, FI_APPEND_WRITE);
365 flush_out:
366         if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
367                 ret = f2fs_issue_flush(sbi, inode->i_ino);
368         if (!ret) {
369                 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
370                 clear_inode_flag(inode, FI_UPDATE_WRITE);
371                 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
372         }
373         f2fs_update_time(sbi, REQ_TIME);
374 out:
375         trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
376         return ret;
377 }
378
379 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
380 {
381         if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
382                 return -EIO;
383         return f2fs_do_sync_file(file, start, end, datasync, false);
384 }
385
386 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
387                                 pgoff_t index, int whence)
388 {
389         switch (whence) {
390         case SEEK_DATA:
391                 if (__is_valid_data_blkaddr(blkaddr))
392                         return true;
393                 if (blkaddr == NEW_ADDR &&
394                     xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
395                         return true;
396                 break;
397         case SEEK_HOLE:
398                 if (blkaddr == NULL_ADDR)
399                         return true;
400                 break;
401         }
402         return false;
403 }
404
405 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
406 {
407         struct inode *inode = file->f_mapping->host;
408         loff_t maxbytes = inode->i_sb->s_maxbytes;
409         struct dnode_of_data dn;
410         pgoff_t pgofs, end_offset;
411         loff_t data_ofs = offset;
412         loff_t isize;
413         int err = 0;
414
415         inode_lock(inode);
416
417         isize = i_size_read(inode);
418         if (offset >= isize)
419                 goto fail;
420
421         /* handle inline data case */
422         if (f2fs_has_inline_data(inode)) {
423                 if (whence == SEEK_HOLE) {
424                         data_ofs = isize;
425                         goto found;
426                 } else if (whence == SEEK_DATA) {
427                         data_ofs = offset;
428                         goto found;
429                 }
430         }
431
432         pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
433
434         for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
435                 set_new_dnode(&dn, inode, NULL, NULL, 0);
436                 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
437                 if (err && err != -ENOENT) {
438                         goto fail;
439                 } else if (err == -ENOENT) {
440                         /* direct node does not exists */
441                         if (whence == SEEK_DATA) {
442                                 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
443                                 continue;
444                         } else {
445                                 goto found;
446                         }
447                 }
448
449                 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
450
451                 /* find data/hole in dnode block */
452                 for (; dn.ofs_in_node < end_offset;
453                                 dn.ofs_in_node++, pgofs++,
454                                 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
455                         block_t blkaddr;
456
457                         blkaddr = f2fs_data_blkaddr(&dn);
458
459                         if (__is_valid_data_blkaddr(blkaddr) &&
460                                 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
461                                         blkaddr, DATA_GENERIC_ENHANCE)) {
462                                 f2fs_put_dnode(&dn);
463                                 goto fail;
464                         }
465
466                         if (__found_offset(file->f_mapping, blkaddr,
467                                                         pgofs, whence)) {
468                                 f2fs_put_dnode(&dn);
469                                 goto found;
470                         }
471                 }
472                 f2fs_put_dnode(&dn);
473         }
474
475         if (whence == SEEK_DATA)
476                 goto fail;
477 found:
478         if (whence == SEEK_HOLE && data_ofs > isize)
479                 data_ofs = isize;
480         inode_unlock(inode);
481         return vfs_setpos(file, data_ofs, maxbytes);
482 fail:
483         inode_unlock(inode);
484         return -ENXIO;
485 }
486
487 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
488 {
489         struct inode *inode = file->f_mapping->host;
490         loff_t maxbytes = inode->i_sb->s_maxbytes;
491
492         if (f2fs_compressed_file(inode))
493                 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
494
495         switch (whence) {
496         case SEEK_SET:
497         case SEEK_CUR:
498         case SEEK_END:
499                 return generic_file_llseek_size(file, offset, whence,
500                                                 maxbytes, i_size_read(inode));
501         case SEEK_DATA:
502         case SEEK_HOLE:
503                 if (offset < 0)
504                         return -ENXIO;
505                 return f2fs_seek_block(file, offset, whence);
506         }
507
508         return -EINVAL;
509 }
510
511 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
512 {
513         struct inode *inode = file_inode(file);
514
515         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
516                 return -EIO;
517
518         if (!f2fs_is_compress_backend_ready(inode))
519                 return -EOPNOTSUPP;
520
521         file_accessed(file);
522         vma->vm_ops = &f2fs_file_vm_ops;
523         set_inode_flag(inode, FI_MMAP_FILE);
524         return 0;
525 }
526
527 static int f2fs_file_open(struct inode *inode, struct file *filp)
528 {
529         int err = fscrypt_file_open(inode, filp);
530
531         if (err)
532                 return err;
533
534         if (!f2fs_is_compress_backend_ready(inode))
535                 return -EOPNOTSUPP;
536
537         err = fsverity_file_open(inode, filp);
538         if (err)
539                 return err;
540
541         filp->f_mode |= FMODE_NOWAIT;
542
543         return dquot_file_open(inode, filp);
544 }
545
546 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
547 {
548         struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
549         struct f2fs_node *raw_node;
550         int nr_free = 0, ofs = dn->ofs_in_node, len = count;
551         __le32 *addr;
552         int base = 0;
553         bool compressed_cluster = false;
554         int cluster_index = 0, valid_blocks = 0;
555         int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
556         bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
557
558         if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
559                 base = get_extra_isize(dn->inode);
560
561         raw_node = F2FS_NODE(dn->node_page);
562         addr = blkaddr_in_node(raw_node) + base + ofs;
563
564         /* Assumption: truncateion starts with cluster */
565         for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
566                 block_t blkaddr = le32_to_cpu(*addr);
567
568                 if (f2fs_compressed_file(dn->inode) &&
569                                         !(cluster_index & (cluster_size - 1))) {
570                         if (compressed_cluster)
571                                 f2fs_i_compr_blocks_update(dn->inode,
572                                                         valid_blocks, false);
573                         compressed_cluster = (blkaddr == COMPRESS_ADDR);
574                         valid_blocks = 0;
575                 }
576
577                 if (blkaddr == NULL_ADDR)
578                         continue;
579
580                 dn->data_blkaddr = NULL_ADDR;
581                 f2fs_set_data_blkaddr(dn);
582
583                 if (__is_valid_data_blkaddr(blkaddr)) {
584                         if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
585                                         DATA_GENERIC_ENHANCE))
586                                 continue;
587                         if (compressed_cluster)
588                                 valid_blocks++;
589                 }
590
591                 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
592                         clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
593
594                 f2fs_invalidate_blocks(sbi, blkaddr);
595
596                 if (!released || blkaddr != COMPRESS_ADDR)
597                         nr_free++;
598         }
599
600         if (compressed_cluster)
601                 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
602
603         if (nr_free) {
604                 pgoff_t fofs;
605                 /*
606                  * once we invalidate valid blkaddr in range [ofs, ofs + count],
607                  * we will invalidate all blkaddr in the whole range.
608                  */
609                 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
610                                                         dn->inode) + ofs;
611                 f2fs_update_extent_cache_range(dn, fofs, 0, len);
612                 dec_valid_block_count(sbi, dn->inode, nr_free);
613         }
614         dn->ofs_in_node = ofs;
615
616         f2fs_update_time(sbi, REQ_TIME);
617         trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
618                                          dn->ofs_in_node, nr_free);
619 }
620
621 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
622 {
623         f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
624 }
625
626 static int truncate_partial_data_page(struct inode *inode, u64 from,
627                                                                 bool cache_only)
628 {
629         loff_t offset = from & (PAGE_SIZE - 1);
630         pgoff_t index = from >> PAGE_SHIFT;
631         struct address_space *mapping = inode->i_mapping;
632         struct page *page;
633
634         if (!offset && !cache_only)
635                 return 0;
636
637         if (cache_only) {
638                 page = find_lock_page(mapping, index);
639                 if (page && PageUptodate(page))
640                         goto truncate_out;
641                 f2fs_put_page(page, 1);
642                 return 0;
643         }
644
645         page = f2fs_get_lock_data_page(inode, index, true);
646         if (IS_ERR(page))
647                 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
648 truncate_out:
649         f2fs_wait_on_page_writeback(page, DATA, true, true);
650         zero_user(page, offset, PAGE_SIZE - offset);
651
652         /* An encrypted inode should have a key and truncate the last page. */
653         f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
654         if (!cache_only)
655                 set_page_dirty(page);
656         f2fs_put_page(page, 1);
657         return 0;
658 }
659
660 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
661 {
662         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
663         struct dnode_of_data dn;
664         pgoff_t free_from;
665         int count = 0, err = 0;
666         struct page *ipage;
667         bool truncate_page = false;
668
669         trace_f2fs_truncate_blocks_enter(inode, from);
670
671         free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
672
673         if (free_from >= max_file_blocks(inode))
674                 goto free_partial;
675
676         if (lock)
677                 f2fs_lock_op(sbi);
678
679         ipage = f2fs_get_node_page(sbi, inode->i_ino);
680         if (IS_ERR(ipage)) {
681                 err = PTR_ERR(ipage);
682                 goto out;
683         }
684
685         if (f2fs_has_inline_data(inode)) {
686                 f2fs_truncate_inline_inode(inode, ipage, from);
687                 f2fs_put_page(ipage, 1);
688                 truncate_page = true;
689                 goto out;
690         }
691
692         set_new_dnode(&dn, inode, ipage, NULL, 0);
693         err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
694         if (err) {
695                 if (err == -ENOENT)
696                         goto free_next;
697                 goto out;
698         }
699
700         count = ADDRS_PER_PAGE(dn.node_page, inode);
701
702         count -= dn.ofs_in_node;
703         f2fs_bug_on(sbi, count < 0);
704
705         if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
706                 f2fs_truncate_data_blocks_range(&dn, count);
707                 free_from += count;
708         }
709
710         f2fs_put_dnode(&dn);
711 free_next:
712         err = f2fs_truncate_inode_blocks(inode, free_from);
713 out:
714         if (lock)
715                 f2fs_unlock_op(sbi);
716 free_partial:
717         /* lastly zero out the first data page */
718         if (!err)
719                 err = truncate_partial_data_page(inode, from, truncate_page);
720
721         trace_f2fs_truncate_blocks_exit(inode, err);
722         return err;
723 }
724
725 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
726 {
727         u64 free_from = from;
728         int err;
729
730 #ifdef CONFIG_F2FS_FS_COMPRESSION
731         /*
732          * for compressed file, only support cluster size
733          * aligned truncation.
734          */
735         if (f2fs_compressed_file(inode))
736                 free_from = round_up(from,
737                                 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
738 #endif
739
740         err = f2fs_do_truncate_blocks(inode, free_from, lock);
741         if (err)
742                 return err;
743
744 #ifdef CONFIG_F2FS_FS_COMPRESSION
745         if (from != free_from) {
746                 err = f2fs_truncate_partial_cluster(inode, from, lock);
747                 if (err)
748                         return err;
749         }
750 #endif
751
752         return 0;
753 }
754
755 int f2fs_truncate(struct inode *inode)
756 {
757         int err;
758
759         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
760                 return -EIO;
761
762         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
763                                 S_ISLNK(inode->i_mode)))
764                 return 0;
765
766         trace_f2fs_truncate(inode);
767
768         if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
769                 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
770                 return -EIO;
771         }
772
773         err = dquot_initialize(inode);
774         if (err)
775                 return err;
776
777         /* we should check inline_data size */
778         if (!f2fs_may_inline_data(inode)) {
779                 err = f2fs_convert_inline_inode(inode);
780                 if (err)
781                         return err;
782         }
783
784         err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
785         if (err)
786                 return err;
787
788         inode->i_mtime = inode->i_ctime = current_time(inode);
789         f2fs_mark_inode_dirty_sync(inode, false);
790         return 0;
791 }
792
793 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
794                  struct kstat *stat, u32 request_mask, unsigned int query_flags)
795 {
796         struct inode *inode = d_inode(path->dentry);
797         struct f2fs_inode_info *fi = F2FS_I(inode);
798         struct f2fs_inode *ri;
799         unsigned int flags;
800
801         if (f2fs_has_extra_attr(inode) &&
802                         f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
803                         F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
804                 stat->result_mask |= STATX_BTIME;
805                 stat->btime.tv_sec = fi->i_crtime.tv_sec;
806                 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
807         }
808
809         flags = fi->i_flags;
810         if (flags & F2FS_COMPR_FL)
811                 stat->attributes |= STATX_ATTR_COMPRESSED;
812         if (flags & F2FS_APPEND_FL)
813                 stat->attributes |= STATX_ATTR_APPEND;
814         if (IS_ENCRYPTED(inode))
815                 stat->attributes |= STATX_ATTR_ENCRYPTED;
816         if (flags & F2FS_IMMUTABLE_FL)
817                 stat->attributes |= STATX_ATTR_IMMUTABLE;
818         if (flags & F2FS_NODUMP_FL)
819                 stat->attributes |= STATX_ATTR_NODUMP;
820         if (IS_VERITY(inode))
821                 stat->attributes |= STATX_ATTR_VERITY;
822
823         stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
824                                   STATX_ATTR_APPEND |
825                                   STATX_ATTR_ENCRYPTED |
826                                   STATX_ATTR_IMMUTABLE |
827                                   STATX_ATTR_NODUMP |
828                                   STATX_ATTR_VERITY);
829
830         generic_fillattr(&init_user_ns, inode, stat);
831
832         /* we need to show initial sectors used for inline_data/dentries */
833         if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
834                                         f2fs_has_inline_dentry(inode))
835                 stat->blocks += (stat->size + 511) >> 9;
836
837         return 0;
838 }
839
840 #ifdef CONFIG_F2FS_FS_POSIX_ACL
841 static void __setattr_copy(struct user_namespace *mnt_userns,
842                            struct inode *inode, const struct iattr *attr)
843 {
844         unsigned int ia_valid = attr->ia_valid;
845
846         if (ia_valid & ATTR_UID)
847                 inode->i_uid = attr->ia_uid;
848         if (ia_valid & ATTR_GID)
849                 inode->i_gid = attr->ia_gid;
850         if (ia_valid & ATTR_ATIME)
851                 inode->i_atime = attr->ia_atime;
852         if (ia_valid & ATTR_MTIME)
853                 inode->i_mtime = attr->ia_mtime;
854         if (ia_valid & ATTR_CTIME)
855                 inode->i_ctime = attr->ia_ctime;
856         if (ia_valid & ATTR_MODE) {
857                 umode_t mode = attr->ia_mode;
858                 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
859
860                 if (!in_group_p(kgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
861                         mode &= ~S_ISGID;
862                 set_acl_inode(inode, mode);
863         }
864 }
865 #else
866 #define __setattr_copy setattr_copy
867 #endif
868
869 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
870                  struct iattr *attr)
871 {
872         struct inode *inode = d_inode(dentry);
873         int err;
874
875         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
876                 return -EIO;
877
878         if (unlikely(IS_IMMUTABLE(inode)))
879                 return -EPERM;
880
881         if (unlikely(IS_APPEND(inode) &&
882                         (attr->ia_valid & (ATTR_MODE | ATTR_UID |
883                                   ATTR_GID | ATTR_TIMES_SET))))
884                 return -EPERM;
885
886         if ((attr->ia_valid & ATTR_SIZE) &&
887                 !f2fs_is_compress_backend_ready(inode))
888                 return -EOPNOTSUPP;
889
890         err = setattr_prepare(&init_user_ns, dentry, attr);
891         if (err)
892                 return err;
893
894         err = fscrypt_prepare_setattr(dentry, attr);
895         if (err)
896                 return err;
897
898         err = fsverity_prepare_setattr(dentry, attr);
899         if (err)
900                 return err;
901
902         if (is_quota_modification(inode, attr)) {
903                 err = dquot_initialize(inode);
904                 if (err)
905                         return err;
906         }
907         if ((attr->ia_valid & ATTR_UID &&
908                 !uid_eq(attr->ia_uid, inode->i_uid)) ||
909                 (attr->ia_valid & ATTR_GID &&
910                 !gid_eq(attr->ia_gid, inode->i_gid))) {
911                 f2fs_lock_op(F2FS_I_SB(inode));
912                 err = dquot_transfer(inode, attr);
913                 if (err) {
914                         set_sbi_flag(F2FS_I_SB(inode),
915                                         SBI_QUOTA_NEED_REPAIR);
916                         f2fs_unlock_op(F2FS_I_SB(inode));
917                         return err;
918                 }
919                 /*
920                  * update uid/gid under lock_op(), so that dquot and inode can
921                  * be updated atomically.
922                  */
923                 if (attr->ia_valid & ATTR_UID)
924                         inode->i_uid = attr->ia_uid;
925                 if (attr->ia_valid & ATTR_GID)
926                         inode->i_gid = attr->ia_gid;
927                 f2fs_mark_inode_dirty_sync(inode, true);
928                 f2fs_unlock_op(F2FS_I_SB(inode));
929         }
930
931         if (attr->ia_valid & ATTR_SIZE) {
932                 loff_t old_size = i_size_read(inode);
933
934                 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
935                         /*
936                          * should convert inline inode before i_size_write to
937                          * keep smaller than inline_data size with inline flag.
938                          */
939                         err = f2fs_convert_inline_inode(inode);
940                         if (err)
941                                 return err;
942                 }
943
944                 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
945                 down_write(&F2FS_I(inode)->i_mmap_sem);
946
947                 truncate_setsize(inode, attr->ia_size);
948
949                 if (attr->ia_size <= old_size)
950                         err = f2fs_truncate(inode);
951                 /*
952                  * do not trim all blocks after i_size if target size is
953                  * larger than i_size.
954                  */
955                 up_write(&F2FS_I(inode)->i_mmap_sem);
956                 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
957                 if (err)
958                         return err;
959
960                 spin_lock(&F2FS_I(inode)->i_size_lock);
961                 inode->i_mtime = inode->i_ctime = current_time(inode);
962                 F2FS_I(inode)->last_disk_size = i_size_read(inode);
963                 spin_unlock(&F2FS_I(inode)->i_size_lock);
964         }
965
966         __setattr_copy(&init_user_ns, inode, attr);
967
968         if (attr->ia_valid & ATTR_MODE) {
969                 err = posix_acl_chmod(&init_user_ns, inode, f2fs_get_inode_mode(inode));
970
971                 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
972                         if (!err)
973                                 inode->i_mode = F2FS_I(inode)->i_acl_mode;
974                         clear_inode_flag(inode, FI_ACL_MODE);
975                 }
976         }
977
978         /* file size may changed here */
979         f2fs_mark_inode_dirty_sync(inode, true);
980
981         /* inode change will produce dirty node pages flushed by checkpoint */
982         f2fs_balance_fs(F2FS_I_SB(inode), true);
983
984         return err;
985 }
986
987 const struct inode_operations f2fs_file_inode_operations = {
988         .getattr        = f2fs_getattr,
989         .setattr        = f2fs_setattr,
990         .get_acl        = f2fs_get_acl,
991         .set_acl        = f2fs_set_acl,
992         .listxattr      = f2fs_listxattr,
993         .fiemap         = f2fs_fiemap,
994         .fileattr_get   = f2fs_fileattr_get,
995         .fileattr_set   = f2fs_fileattr_set,
996 };
997
998 static int fill_zero(struct inode *inode, pgoff_t index,
999                                         loff_t start, loff_t len)
1000 {
1001         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1002         struct page *page;
1003
1004         if (!len)
1005                 return 0;
1006
1007         f2fs_balance_fs(sbi, true);
1008
1009         f2fs_lock_op(sbi);
1010         page = f2fs_get_new_data_page(inode, NULL, index, false);
1011         f2fs_unlock_op(sbi);
1012
1013         if (IS_ERR(page))
1014                 return PTR_ERR(page);
1015
1016         f2fs_wait_on_page_writeback(page, DATA, true, true);
1017         zero_user(page, start, len);
1018         set_page_dirty(page);
1019         f2fs_put_page(page, 1);
1020         return 0;
1021 }
1022
1023 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1024 {
1025         int err;
1026
1027         while (pg_start < pg_end) {
1028                 struct dnode_of_data dn;
1029                 pgoff_t end_offset, count;
1030
1031                 set_new_dnode(&dn, inode, NULL, NULL, 0);
1032                 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1033                 if (err) {
1034                         if (err == -ENOENT) {
1035                                 pg_start = f2fs_get_next_page_offset(&dn,
1036                                                                 pg_start);
1037                                 continue;
1038                         }
1039                         return err;
1040                 }
1041
1042                 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1043                 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1044
1045                 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1046
1047                 f2fs_truncate_data_blocks_range(&dn, count);
1048                 f2fs_put_dnode(&dn);
1049
1050                 pg_start += count;
1051         }
1052         return 0;
1053 }
1054
1055 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1056 {
1057         pgoff_t pg_start, pg_end;
1058         loff_t off_start, off_end;
1059         int ret;
1060
1061         ret = f2fs_convert_inline_inode(inode);
1062         if (ret)
1063                 return ret;
1064
1065         pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1066         pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1067
1068         off_start = offset & (PAGE_SIZE - 1);
1069         off_end = (offset + len) & (PAGE_SIZE - 1);
1070
1071         if (pg_start == pg_end) {
1072                 ret = fill_zero(inode, pg_start, off_start,
1073                                                 off_end - off_start);
1074                 if (ret)
1075                         return ret;
1076         } else {
1077                 if (off_start) {
1078                         ret = fill_zero(inode, pg_start++, off_start,
1079                                                 PAGE_SIZE - off_start);
1080                         if (ret)
1081                                 return ret;
1082                 }
1083                 if (off_end) {
1084                         ret = fill_zero(inode, pg_end, 0, off_end);
1085                         if (ret)
1086                                 return ret;
1087                 }
1088
1089                 if (pg_start < pg_end) {
1090                         struct address_space *mapping = inode->i_mapping;
1091                         loff_t blk_start, blk_end;
1092                         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1093
1094                         f2fs_balance_fs(sbi, true);
1095
1096                         blk_start = (loff_t)pg_start << PAGE_SHIFT;
1097                         blk_end = (loff_t)pg_end << PAGE_SHIFT;
1098
1099                         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1100                         down_write(&F2FS_I(inode)->i_mmap_sem);
1101
1102                         truncate_inode_pages_range(mapping, blk_start,
1103                                         blk_end - 1);
1104
1105                         f2fs_lock_op(sbi);
1106                         ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1107                         f2fs_unlock_op(sbi);
1108
1109                         up_write(&F2FS_I(inode)->i_mmap_sem);
1110                         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1111                 }
1112         }
1113
1114         return ret;
1115 }
1116
1117 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1118                                 int *do_replace, pgoff_t off, pgoff_t len)
1119 {
1120         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1121         struct dnode_of_data dn;
1122         int ret, done, i;
1123
1124 next_dnode:
1125         set_new_dnode(&dn, inode, NULL, NULL, 0);
1126         ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1127         if (ret && ret != -ENOENT) {
1128                 return ret;
1129         } else if (ret == -ENOENT) {
1130                 if (dn.max_level == 0)
1131                         return -ENOENT;
1132                 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1133                                                 dn.ofs_in_node, len);
1134                 blkaddr += done;
1135                 do_replace += done;
1136                 goto next;
1137         }
1138
1139         done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1140                                                         dn.ofs_in_node, len);
1141         for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1142                 *blkaddr = f2fs_data_blkaddr(&dn);
1143
1144                 if (__is_valid_data_blkaddr(*blkaddr) &&
1145                         !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1146                                         DATA_GENERIC_ENHANCE)) {
1147                         f2fs_put_dnode(&dn);
1148                         return -EFSCORRUPTED;
1149                 }
1150
1151                 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1152
1153                         if (f2fs_lfs_mode(sbi)) {
1154                                 f2fs_put_dnode(&dn);
1155                                 return -EOPNOTSUPP;
1156                         }
1157
1158                         /* do not invalidate this block address */
1159                         f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1160                         *do_replace = 1;
1161                 }
1162         }
1163         f2fs_put_dnode(&dn);
1164 next:
1165         len -= done;
1166         off += done;
1167         if (len)
1168                 goto next_dnode;
1169         return 0;
1170 }
1171
1172 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1173                                 int *do_replace, pgoff_t off, int len)
1174 {
1175         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1176         struct dnode_of_data dn;
1177         int ret, i;
1178
1179         for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1180                 if (*do_replace == 0)
1181                         continue;
1182
1183                 set_new_dnode(&dn, inode, NULL, NULL, 0);
1184                 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1185                 if (ret) {
1186                         dec_valid_block_count(sbi, inode, 1);
1187                         f2fs_invalidate_blocks(sbi, *blkaddr);
1188                 } else {
1189                         f2fs_update_data_blkaddr(&dn, *blkaddr);
1190                 }
1191                 f2fs_put_dnode(&dn);
1192         }
1193         return 0;
1194 }
1195
1196 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1197                         block_t *blkaddr, int *do_replace,
1198                         pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1199 {
1200         struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1201         pgoff_t i = 0;
1202         int ret;
1203
1204         while (i < len) {
1205                 if (blkaddr[i] == NULL_ADDR && !full) {
1206                         i++;
1207                         continue;
1208                 }
1209
1210                 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1211                         struct dnode_of_data dn;
1212                         struct node_info ni;
1213                         size_t new_size;
1214                         pgoff_t ilen;
1215
1216                         set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1217                         ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1218                         if (ret)
1219                                 return ret;
1220
1221                         ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1222                         if (ret) {
1223                                 f2fs_put_dnode(&dn);
1224                                 return ret;
1225                         }
1226
1227                         ilen = min((pgoff_t)
1228                                 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1229                                                 dn.ofs_in_node, len - i);
1230                         do {
1231                                 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1232                                 f2fs_truncate_data_blocks_range(&dn, 1);
1233
1234                                 if (do_replace[i]) {
1235                                         f2fs_i_blocks_write(src_inode,
1236                                                         1, false, false);
1237                                         f2fs_i_blocks_write(dst_inode,
1238                                                         1, true, false);
1239                                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1240                                         blkaddr[i], ni.version, true, false);
1241
1242                                         do_replace[i] = 0;
1243                                 }
1244                                 dn.ofs_in_node++;
1245                                 i++;
1246                                 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1247                                 if (dst_inode->i_size < new_size)
1248                                         f2fs_i_size_write(dst_inode, new_size);
1249                         } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1250
1251                         f2fs_put_dnode(&dn);
1252                 } else {
1253                         struct page *psrc, *pdst;
1254
1255                         psrc = f2fs_get_lock_data_page(src_inode,
1256                                                         src + i, true);
1257                         if (IS_ERR(psrc))
1258                                 return PTR_ERR(psrc);
1259                         pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1260                                                                 true);
1261                         if (IS_ERR(pdst)) {
1262                                 f2fs_put_page(psrc, 1);
1263                                 return PTR_ERR(pdst);
1264                         }
1265                         f2fs_copy_page(psrc, pdst);
1266                         set_page_dirty(pdst);
1267                         f2fs_put_page(pdst, 1);
1268                         f2fs_put_page(psrc, 1);
1269
1270                         ret = f2fs_truncate_hole(src_inode,
1271                                                 src + i, src + i + 1);
1272                         if (ret)
1273                                 return ret;
1274                         i++;
1275                 }
1276         }
1277         return 0;
1278 }
1279
1280 static int __exchange_data_block(struct inode *src_inode,
1281                         struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1282                         pgoff_t len, bool full)
1283 {
1284         block_t *src_blkaddr;
1285         int *do_replace;
1286         pgoff_t olen;
1287         int ret;
1288
1289         while (len) {
1290                 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1291
1292                 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1293                                         array_size(olen, sizeof(block_t)),
1294                                         GFP_NOFS);
1295                 if (!src_blkaddr)
1296                         return -ENOMEM;
1297
1298                 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1299                                         array_size(olen, sizeof(int)),
1300                                         GFP_NOFS);
1301                 if (!do_replace) {
1302                         kvfree(src_blkaddr);
1303                         return -ENOMEM;
1304                 }
1305
1306                 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1307                                         do_replace, src, olen);
1308                 if (ret)
1309                         goto roll_back;
1310
1311                 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1312                                         do_replace, src, dst, olen, full);
1313                 if (ret)
1314                         goto roll_back;
1315
1316                 src += olen;
1317                 dst += olen;
1318                 len -= olen;
1319
1320                 kvfree(src_blkaddr);
1321                 kvfree(do_replace);
1322         }
1323         return 0;
1324
1325 roll_back:
1326         __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1327         kvfree(src_blkaddr);
1328         kvfree(do_replace);
1329         return ret;
1330 }
1331
1332 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1333 {
1334         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1335         pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1336         pgoff_t start = offset >> PAGE_SHIFT;
1337         pgoff_t end = (offset + len) >> PAGE_SHIFT;
1338         int ret;
1339
1340         f2fs_balance_fs(sbi, true);
1341
1342         /* avoid gc operation during block exchange */
1343         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1344         down_write(&F2FS_I(inode)->i_mmap_sem);
1345
1346         f2fs_lock_op(sbi);
1347         f2fs_drop_extent_tree(inode);
1348         truncate_pagecache(inode, offset);
1349         ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1350         f2fs_unlock_op(sbi);
1351
1352         up_write(&F2FS_I(inode)->i_mmap_sem);
1353         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1354         return ret;
1355 }
1356
1357 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1358 {
1359         loff_t new_size;
1360         int ret;
1361
1362         if (offset + len >= i_size_read(inode))
1363                 return -EINVAL;
1364
1365         /* collapse range should be aligned to block size of f2fs. */
1366         if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1367                 return -EINVAL;
1368
1369         ret = f2fs_convert_inline_inode(inode);
1370         if (ret)
1371                 return ret;
1372
1373         /* write out all dirty pages from offset */
1374         ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1375         if (ret)
1376                 return ret;
1377
1378         ret = f2fs_do_collapse(inode, offset, len);
1379         if (ret)
1380                 return ret;
1381
1382         /* write out all moved pages, if possible */
1383         down_write(&F2FS_I(inode)->i_mmap_sem);
1384         filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1385         truncate_pagecache(inode, offset);
1386
1387         new_size = i_size_read(inode) - len;
1388         ret = f2fs_truncate_blocks(inode, new_size, true);
1389         up_write(&F2FS_I(inode)->i_mmap_sem);
1390         if (!ret)
1391                 f2fs_i_size_write(inode, new_size);
1392         return ret;
1393 }
1394
1395 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1396                                                                 pgoff_t end)
1397 {
1398         struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1399         pgoff_t index = start;
1400         unsigned int ofs_in_node = dn->ofs_in_node;
1401         blkcnt_t count = 0;
1402         int ret;
1403
1404         for (; index < end; index++, dn->ofs_in_node++) {
1405                 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1406                         count++;
1407         }
1408
1409         dn->ofs_in_node = ofs_in_node;
1410         ret = f2fs_reserve_new_blocks(dn, count);
1411         if (ret)
1412                 return ret;
1413
1414         dn->ofs_in_node = ofs_in_node;
1415         for (index = start; index < end; index++, dn->ofs_in_node++) {
1416                 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1417                 /*
1418                  * f2fs_reserve_new_blocks will not guarantee entire block
1419                  * allocation.
1420                  */
1421                 if (dn->data_blkaddr == NULL_ADDR) {
1422                         ret = -ENOSPC;
1423                         break;
1424                 }
1425                 if (dn->data_blkaddr != NEW_ADDR) {
1426                         f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1427                         dn->data_blkaddr = NEW_ADDR;
1428                         f2fs_set_data_blkaddr(dn);
1429                 }
1430         }
1431
1432         f2fs_update_extent_cache_range(dn, start, 0, index - start);
1433
1434         return ret;
1435 }
1436
1437 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1438                                                                 int mode)
1439 {
1440         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1441         struct address_space *mapping = inode->i_mapping;
1442         pgoff_t index, pg_start, pg_end;
1443         loff_t new_size = i_size_read(inode);
1444         loff_t off_start, off_end;
1445         int ret = 0;
1446
1447         ret = inode_newsize_ok(inode, (len + offset));
1448         if (ret)
1449                 return ret;
1450
1451         ret = f2fs_convert_inline_inode(inode);
1452         if (ret)
1453                 return ret;
1454
1455         ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1456         if (ret)
1457                 return ret;
1458
1459         pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1460         pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1461
1462         off_start = offset & (PAGE_SIZE - 1);
1463         off_end = (offset + len) & (PAGE_SIZE - 1);
1464
1465         if (pg_start == pg_end) {
1466                 ret = fill_zero(inode, pg_start, off_start,
1467                                                 off_end - off_start);
1468                 if (ret)
1469                         return ret;
1470
1471                 new_size = max_t(loff_t, new_size, offset + len);
1472         } else {
1473                 if (off_start) {
1474                         ret = fill_zero(inode, pg_start++, off_start,
1475                                                 PAGE_SIZE - off_start);
1476                         if (ret)
1477                                 return ret;
1478
1479                         new_size = max_t(loff_t, new_size,
1480                                         (loff_t)pg_start << PAGE_SHIFT);
1481                 }
1482
1483                 for (index = pg_start; index < pg_end;) {
1484                         struct dnode_of_data dn;
1485                         unsigned int end_offset;
1486                         pgoff_t end;
1487
1488                         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1489                         down_write(&F2FS_I(inode)->i_mmap_sem);
1490
1491                         truncate_pagecache_range(inode,
1492                                 (loff_t)index << PAGE_SHIFT,
1493                                 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1494
1495                         f2fs_lock_op(sbi);
1496
1497                         set_new_dnode(&dn, inode, NULL, NULL, 0);
1498                         ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1499                         if (ret) {
1500                                 f2fs_unlock_op(sbi);
1501                                 up_write(&F2FS_I(inode)->i_mmap_sem);
1502                                 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1503                                 goto out;
1504                         }
1505
1506                         end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1507                         end = min(pg_end, end_offset - dn.ofs_in_node + index);
1508
1509                         ret = f2fs_do_zero_range(&dn, index, end);
1510                         f2fs_put_dnode(&dn);
1511
1512                         f2fs_unlock_op(sbi);
1513                         up_write(&F2FS_I(inode)->i_mmap_sem);
1514                         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1515
1516                         f2fs_balance_fs(sbi, dn.node_changed);
1517
1518                         if (ret)
1519                                 goto out;
1520
1521                         index = end;
1522                         new_size = max_t(loff_t, new_size,
1523                                         (loff_t)index << PAGE_SHIFT);
1524                 }
1525
1526                 if (off_end) {
1527                         ret = fill_zero(inode, pg_end, 0, off_end);
1528                         if (ret)
1529                                 goto out;
1530
1531                         new_size = max_t(loff_t, new_size, offset + len);
1532                 }
1533         }
1534
1535 out:
1536         if (new_size > i_size_read(inode)) {
1537                 if (mode & FALLOC_FL_KEEP_SIZE)
1538                         file_set_keep_isize(inode);
1539                 else
1540                         f2fs_i_size_write(inode, new_size);
1541         }
1542         return ret;
1543 }
1544
1545 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1546 {
1547         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1548         pgoff_t nr, pg_start, pg_end, delta, idx;
1549         loff_t new_size;
1550         int ret = 0;
1551
1552         new_size = i_size_read(inode) + len;
1553         ret = inode_newsize_ok(inode, new_size);
1554         if (ret)
1555                 return ret;
1556
1557         if (offset >= i_size_read(inode))
1558                 return -EINVAL;
1559
1560         /* insert range should be aligned to block size of f2fs. */
1561         if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1562                 return -EINVAL;
1563
1564         ret = f2fs_convert_inline_inode(inode);
1565         if (ret)
1566                 return ret;
1567
1568         f2fs_balance_fs(sbi, true);
1569
1570         down_write(&F2FS_I(inode)->i_mmap_sem);
1571         ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1572         up_write(&F2FS_I(inode)->i_mmap_sem);
1573         if (ret)
1574                 return ret;
1575
1576         /* write out all dirty pages from offset */
1577         ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1578         if (ret)
1579                 return ret;
1580
1581         pg_start = offset >> PAGE_SHIFT;
1582         pg_end = (offset + len) >> PAGE_SHIFT;
1583         delta = pg_end - pg_start;
1584         idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1585
1586         /* avoid gc operation during block exchange */
1587         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1588         down_write(&F2FS_I(inode)->i_mmap_sem);
1589         truncate_pagecache(inode, offset);
1590
1591         while (!ret && idx > pg_start) {
1592                 nr = idx - pg_start;
1593                 if (nr > delta)
1594                         nr = delta;
1595                 idx -= nr;
1596
1597                 f2fs_lock_op(sbi);
1598                 f2fs_drop_extent_tree(inode);
1599
1600                 ret = __exchange_data_block(inode, inode, idx,
1601                                         idx + delta, nr, false);
1602                 f2fs_unlock_op(sbi);
1603         }
1604         up_write(&F2FS_I(inode)->i_mmap_sem);
1605         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1606
1607         /* write out all moved pages, if possible */
1608         down_write(&F2FS_I(inode)->i_mmap_sem);
1609         filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1610         truncate_pagecache(inode, offset);
1611         up_write(&F2FS_I(inode)->i_mmap_sem);
1612
1613         if (!ret)
1614                 f2fs_i_size_write(inode, new_size);
1615         return ret;
1616 }
1617
1618 static int expand_inode_data(struct inode *inode, loff_t offset,
1619                                         loff_t len, int mode)
1620 {
1621         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1622         struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1623                         .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1624                         .m_may_create = true };
1625         pgoff_t pg_start, pg_end;
1626         loff_t new_size = i_size_read(inode);
1627         loff_t off_end;
1628         block_t expanded = 0;
1629         int err;
1630
1631         err = inode_newsize_ok(inode, (len + offset));
1632         if (err)
1633                 return err;
1634
1635         err = f2fs_convert_inline_inode(inode);
1636         if (err)
1637                 return err;
1638
1639         f2fs_balance_fs(sbi, true);
1640
1641         pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1642         pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1643         off_end = (offset + len) & (PAGE_SIZE - 1);
1644
1645         map.m_lblk = pg_start;
1646         map.m_len = pg_end - pg_start;
1647         if (off_end)
1648                 map.m_len++;
1649
1650         if (!map.m_len)
1651                 return 0;
1652
1653         if (f2fs_is_pinned_file(inode)) {
1654                 block_t sec_blks = BLKS_PER_SEC(sbi);
1655                 block_t sec_len = roundup(map.m_len, sec_blks);
1656
1657                 map.m_len = sec_blks;
1658 next_alloc:
1659                 if (has_not_enough_free_secs(sbi, 0,
1660                         GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1661                         down_write(&sbi->gc_lock);
1662                         err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
1663                         if (err && err != -ENODATA && err != -EAGAIN)
1664                                 goto out_err;
1665                 }
1666
1667                 down_write(&sbi->pin_sem);
1668
1669                 f2fs_lock_op(sbi);
1670                 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1671                 f2fs_unlock_op(sbi);
1672
1673                 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1674                 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1675
1676                 up_write(&sbi->pin_sem);
1677
1678                 expanded += map.m_len;
1679                 sec_len -= map.m_len;
1680                 map.m_lblk += map.m_len;
1681                 if (!err && sec_len)
1682                         goto next_alloc;
1683
1684                 map.m_len = expanded;
1685         } else {
1686                 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1687                 expanded = map.m_len;
1688         }
1689 out_err:
1690         if (err) {
1691                 pgoff_t last_off;
1692
1693                 if (!expanded)
1694                         return err;
1695
1696                 last_off = pg_start + expanded - 1;
1697
1698                 /* update new size to the failed position */
1699                 new_size = (last_off == pg_end) ? offset + len :
1700                                         (loff_t)(last_off + 1) << PAGE_SHIFT;
1701         } else {
1702                 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1703         }
1704
1705         if (new_size > i_size_read(inode)) {
1706                 if (mode & FALLOC_FL_KEEP_SIZE)
1707                         file_set_keep_isize(inode);
1708                 else
1709                         f2fs_i_size_write(inode, new_size);
1710         }
1711
1712         return err;
1713 }
1714
1715 static long f2fs_fallocate(struct file *file, int mode,
1716                                 loff_t offset, loff_t len)
1717 {
1718         struct inode *inode = file_inode(file);
1719         long ret = 0;
1720
1721         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1722                 return -EIO;
1723         if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1724                 return -ENOSPC;
1725         if (!f2fs_is_compress_backend_ready(inode))
1726                 return -EOPNOTSUPP;
1727
1728         /* f2fs only support ->fallocate for regular file */
1729         if (!S_ISREG(inode->i_mode))
1730                 return -EINVAL;
1731
1732         if (IS_ENCRYPTED(inode) &&
1733                 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1734                 return -EOPNOTSUPP;
1735
1736         if (f2fs_compressed_file(inode) &&
1737                 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1738                         FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1739                 return -EOPNOTSUPP;
1740
1741         if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1742                         FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1743                         FALLOC_FL_INSERT_RANGE))
1744                 return -EOPNOTSUPP;
1745
1746         inode_lock(inode);
1747
1748         if (mode & FALLOC_FL_PUNCH_HOLE) {
1749                 if (offset >= inode->i_size)
1750                         goto out;
1751
1752                 ret = punch_hole(inode, offset, len);
1753         } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1754                 ret = f2fs_collapse_range(inode, offset, len);
1755         } else if (mode & FALLOC_FL_ZERO_RANGE) {
1756                 ret = f2fs_zero_range(inode, offset, len, mode);
1757         } else if (mode & FALLOC_FL_INSERT_RANGE) {
1758                 ret = f2fs_insert_range(inode, offset, len);
1759         } else {
1760                 ret = expand_inode_data(inode, offset, len, mode);
1761         }
1762
1763         if (!ret) {
1764                 inode->i_mtime = inode->i_ctime = current_time(inode);
1765                 f2fs_mark_inode_dirty_sync(inode, false);
1766                 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1767         }
1768
1769 out:
1770         inode_unlock(inode);
1771
1772         trace_f2fs_fallocate(inode, mode, offset, len, ret);
1773         return ret;
1774 }
1775
1776 static int f2fs_release_file(struct inode *inode, struct file *filp)
1777 {
1778         /*
1779          * f2fs_relase_file is called at every close calls. So we should
1780          * not drop any inmemory pages by close called by other process.
1781          */
1782         if (!(filp->f_mode & FMODE_WRITE) ||
1783                         atomic_read(&inode->i_writecount) != 1)
1784                 return 0;
1785
1786         /* some remained atomic pages should discarded */
1787         if (f2fs_is_atomic_file(inode))
1788                 f2fs_drop_inmem_pages(inode);
1789         if (f2fs_is_volatile_file(inode)) {
1790                 set_inode_flag(inode, FI_DROP_CACHE);
1791                 filemap_fdatawrite(inode->i_mapping);
1792                 clear_inode_flag(inode, FI_DROP_CACHE);
1793                 clear_inode_flag(inode, FI_VOLATILE_FILE);
1794                 stat_dec_volatile_write(inode);
1795         }
1796         return 0;
1797 }
1798
1799 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1800 {
1801         struct inode *inode = file_inode(file);
1802
1803         /*
1804          * If the process doing a transaction is crashed, we should do
1805          * roll-back. Otherwise, other reader/write can see corrupted database
1806          * until all the writers close its file. Since this should be done
1807          * before dropping file lock, it needs to do in ->flush.
1808          */
1809         if (f2fs_is_atomic_file(inode) &&
1810                         F2FS_I(inode)->inmem_task == current)
1811                 f2fs_drop_inmem_pages(inode);
1812         return 0;
1813 }
1814
1815 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1816 {
1817         struct f2fs_inode_info *fi = F2FS_I(inode);
1818         u32 masked_flags = fi->i_flags & mask;
1819
1820         f2fs_bug_on(F2FS_I_SB(inode), (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 err;
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 };