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