LCOV - code coverage report
Current view: top level - fs/f2fs - file.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 0 1360 0.0 %
Date: 2020-09-30 20:25:40 Functions: 0 80 0.0 %
Branches: 0 1122 0.0 %

           Branch data     Line data    Source code
       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                 :            : 
      25                 :            : #include "f2fs.h"
      26                 :            : #include "node.h"
      27                 :            : #include "segment.h"
      28                 :            : #include "xattr.h"
      29                 :            : #include "acl.h"
      30                 :            : #include "gc.h"
      31                 :            : #include "trace.h"
      32                 :            : #include <trace/events/f2fs.h>
      33                 :            : 
      34                 :          0 : static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
      35                 :            : {
      36                 :          0 :         struct inode *inode = file_inode(vmf->vma->vm_file);
      37                 :            :         vm_fault_t ret;
      38                 :            : 
      39                 :          0 :         down_read(&F2FS_I(inode)->i_mmap_sem);
      40                 :          0 :         ret = filemap_fault(vmf);
      41                 :          0 :         up_read(&F2FS_I(inode)->i_mmap_sem);
      42                 :            : 
      43                 :          0 :         trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
      44                 :            : 
      45                 :          0 :         return ret;
      46                 :            : }
      47                 :            : 
      48                 :          0 : static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
      49                 :            : {
      50                 :          0 :         struct page *page = vmf->page;
      51                 :          0 :         struct inode *inode = file_inode(vmf->vma->vm_file);
      52                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
      53                 :            :         struct dnode_of_data dn;
      54                 :            :         int err;
      55                 :            : 
      56         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(sbi))) {
      57                 :            :                 err = -EIO;
      58                 :            :                 goto err;
      59                 :            :         }
      60                 :            : 
      61         [ #  # ]:          0 :         if (!f2fs_is_checkpoint_ready(sbi)) {
      62                 :            :                 err = -ENOSPC;
      63                 :            :                 goto err;
      64                 :            :         }
      65                 :            : 
      66                 :            :         /* should do out of any locked page */
      67                 :          0 :         f2fs_balance_fs(sbi, true);
      68                 :            : 
      69                 :          0 :         sb_start_pagefault(inode->i_sb);
      70                 :            : 
      71         [ #  # ]:          0 :         f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
      72                 :            : 
      73                 :          0 :         file_update_time(vmf->vma->vm_file);
      74                 :          0 :         down_read(&F2FS_I(inode)->i_mmap_sem);
      75                 :          0 :         lock_page(page);
      76   [ #  #  #  #  :          0 :         if (unlikely(page->mapping != inode->i_mapping ||
             #  #  #  # ]
      77                 :            :                         page_offset(page) > i_size_read(inode) ||
      78                 :            :                         !PageUptodate(page))) {
      79                 :          0 :                 unlock_page(page);
      80                 :            :                 err = -EFAULT;
      81                 :          0 :                 goto out_sem;
      82                 :            :         }
      83                 :            : 
      84                 :            :         /* block allocation */
      85                 :          0 :         __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
      86                 :            :         set_new_dnode(&dn, inode, NULL, NULL, 0);
      87                 :          0 :         err = f2fs_get_block(&dn, page->index);
      88                 :          0 :         f2fs_put_dnode(&dn);
      89                 :          0 :         __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
      90         [ #  # ]:          0 :         if (err) {
      91                 :          0 :                 unlock_page(page);
      92                 :          0 :                 goto out_sem;
      93                 :            :         }
      94                 :            : 
      95                 :            :         /* fill the page */
      96                 :          0 :         f2fs_wait_on_page_writeback(page, DATA, false, true);
      97                 :            : 
      98                 :            :         /* wait for GCed page writeback via META_MAPPING */
      99                 :          0 :         f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
     100                 :            : 
     101                 :            :         /*
     102                 :            :          * check to see if the page is mapped already (no holes)
     103                 :            :          */
     104         [ #  # ]:          0 :         if (PageMappedToDisk(page))
     105                 :            :                 goto out_sem;
     106                 :            : 
     107                 :            :         /* page is wholly or partially inside EOF */
     108         [ #  # ]:          0 :         if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
     109                 :            :                                                 i_size_read(inode)) {
     110                 :            :                 loff_t offset;
     111                 :            : 
     112                 :          0 :                 offset = i_size_read(inode) & ~PAGE_MASK;
     113                 :          0 :                 zero_user_segment(page, offset, PAGE_SIZE);
     114                 :            :         }
     115                 :          0 :         set_page_dirty(page);
     116         [ #  # ]:          0 :         if (!PageUptodate(page))
     117                 :            :                 SetPageUptodate(page);
     118                 :            : 
     119                 :          0 :         f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
     120                 :            :         f2fs_update_time(sbi, REQ_TIME);
     121                 :            : 
     122                 :          0 :         trace_f2fs_vm_page_mkwrite(page, DATA);
     123                 :            : out_sem:
     124                 :          0 :         up_read(&F2FS_I(inode)->i_mmap_sem);
     125                 :            : 
     126                 :          0 :         sb_end_pagefault(inode->i_sb);
     127                 :            : err:
     128                 :          0 :         return block_page_mkwrite_return(err);
     129                 :            : }
     130                 :            : 
     131                 :            : static const struct vm_operations_struct f2fs_file_vm_ops = {
     132                 :            :         .fault          = f2fs_filemap_fault,
     133                 :            :         .map_pages      = filemap_map_pages,
     134                 :            :         .page_mkwrite   = f2fs_vm_page_mkwrite,
     135                 :            : };
     136                 :            : 
     137                 :          0 : static int get_parent_ino(struct inode *inode, nid_t *pino)
     138                 :            : {
     139                 :            :         struct dentry *dentry;
     140                 :            : 
     141                 :          0 :         inode = igrab(inode);
     142                 :          0 :         dentry = d_find_any_alias(inode);
     143                 :          0 :         iput(inode);
     144         [ #  # ]:          0 :         if (!dentry)
     145                 :            :                 return 0;
     146                 :            : 
     147                 :          0 :         *pino = parent_ino(dentry);
     148                 :          0 :         dput(dentry);
     149                 :          0 :         return 1;
     150                 :            : }
     151                 :            : 
     152                 :          0 : static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
     153                 :            : {
     154                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
     155                 :            :         enum cp_reason_type cp_reason = CP_NO_NEEDED;
     156                 :            : 
     157         [ #  # ]:          0 :         if (!S_ISREG(inode->i_mode))
     158                 :            :                 cp_reason = CP_NON_REGULAR;
     159         [ #  # ]:          0 :         else if (inode->i_nlink != 1)
     160                 :            :                 cp_reason = CP_HARDLINK;
     161         [ #  # ]:          0 :         else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
     162                 :            :                 cp_reason = CP_SB_NEED_CP;
     163         [ #  # ]:          0 :         else if (file_wrong_pino(inode))
     164                 :            :                 cp_reason = CP_WRONG_PINO;
     165         [ #  # ]:          0 :         else if (!f2fs_space_for_roll_forward(sbi))
     166                 :            :                 cp_reason = CP_NO_SPC_ROLL;
     167         [ #  # ]:          0 :         else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
     168                 :            :                 cp_reason = CP_NODE_NEED_CP;
     169         [ #  # ]:          0 :         else if (test_opt(sbi, FASTBOOT))
     170                 :            :                 cp_reason = CP_FASTBOOT_MODE;
     171         [ #  # ]:          0 :         else if (F2FS_OPTION(sbi).active_logs == 2)
     172                 :            :                 cp_reason = CP_SPEC_LOG_NUM;
     173   [ #  #  #  # ]:          0 :         else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
     174         [ #  # ]:          0 :                 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
     175                 :          0 :                 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
     176                 :            :                                                         TRANS_DIR_INO))
     177                 :            :                 cp_reason = CP_RECOVER_DIR;
     178                 :            : 
     179                 :          0 :         return cp_reason;
     180                 :            : }
     181                 :            : 
     182                 :          0 : static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
     183                 :            : {
     184                 :            :         struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
     185                 :            :         bool ret = false;
     186                 :            :         /* But we need to avoid that there are some inode updates */
     187   [ #  #  #  #  :          0 :         if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
                   #  # ]
     188                 :            :                 ret = true;
     189                 :          0 :         f2fs_put_page(i, 0);
     190                 :          0 :         return ret;
     191                 :            : }
     192                 :            : 
     193                 :          0 : static void try_to_fix_pino(struct inode *inode)
     194                 :            : {
     195                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
     196                 :            :         nid_t pino;
     197                 :            : 
     198                 :          0 :         down_write(&fi->i_sem);
     199   [ #  #  #  #  :          0 :         if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
                   #  # ]
     200                 :          0 :                         get_parent_ino(inode, &pino)) {
     201                 :          0 :                 f2fs_i_pino_write(inode, pino);
     202                 :            :                 file_got_pino(inode);
     203                 :            :         }
     204                 :          0 :         up_write(&fi->i_sem);
     205                 :          0 : }
     206                 :            : 
     207                 :          0 : static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
     208                 :            :                                                 int datasync, bool atomic)
     209                 :            : {
     210                 :          0 :         struct inode *inode = file->f_mapping->host;
     211                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
     212                 :          0 :         nid_t ino = inode->i_ino;
     213                 :            :         int ret = 0;
     214                 :            :         enum cp_reason_type cp_reason = 0;
     215                 :          0 :         struct writeback_control wbc = {
     216                 :            :                 .sync_mode = WB_SYNC_ALL,
     217                 :            :                 .nr_to_write = LONG_MAX,
     218                 :            :                 .for_reclaim = 0,
     219                 :            :         };
     220                 :          0 :         unsigned int seq_id = 0;
     221                 :            : 
     222   [ #  #  #  # ]:          0 :         if (unlikely(f2fs_readonly(inode->i_sb) ||
     223                 :            :                                 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
     224                 :            :                 return 0;
     225                 :            : 
     226                 :          0 :         trace_f2fs_sync_file_enter(inode);
     227                 :            : 
     228         [ #  # ]:          0 :         if (S_ISDIR(inode->i_mode))
     229                 :            :                 goto go_write;
     230                 :            : 
     231                 :            :         /* if fdatasync is triggered, let's do in-place-update */
     232   [ #  #  #  # ]:          0 :         if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
     233                 :          0 :                 set_inode_flag(inode, FI_NEED_IPU);
     234                 :          0 :         ret = file_write_and_wait_range(file, start, end);
     235                 :          0 :         clear_inode_flag(inode, FI_NEED_IPU);
     236                 :            : 
     237         [ #  # ]:          0 :         if (ret) {
     238                 :          0 :                 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
     239                 :          0 :                 return ret;
     240                 :            :         }
     241                 :            : 
     242                 :            :         /* if the inode is dirty, let's recover all the time */
     243         [ #  # ]:          0 :         if (!f2fs_skip_inode_update(inode, datasync)) {
     244                 :          0 :                 f2fs_write_inode(inode, NULL);
     245                 :          0 :                 goto go_write;
     246                 :            :         }
     247                 :            : 
     248                 :            :         /*
     249                 :            :          * if there is no written data, don't waste time to write recovery info.
     250                 :            :          */
     251   [ #  #  #  # ]:          0 :         if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
     252                 :          0 :                         !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
     253                 :            : 
     254                 :            :                 /* it may call write_inode just prior to fsync */
     255         [ #  # ]:          0 :                 if (need_inode_page_update(sbi, ino))
     256                 :            :                         goto go_write;
     257                 :            : 
     258   [ #  #  #  # ]:          0 :                 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
     259                 :          0 :                                 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
     260                 :            :                         goto flush_out;
     261                 :            :                 goto out;
     262                 :            :         }
     263                 :            : go_write:
     264                 :            :         /*
     265                 :            :          * Both of fdatasync() and fsync() are able to be recovered from
     266                 :            :          * sudden-power-off.
     267                 :            :          */
     268                 :          0 :         down_read(&F2FS_I(inode)->i_sem);
     269                 :          0 :         cp_reason = need_do_checkpoint(inode);
     270                 :          0 :         up_read(&F2FS_I(inode)->i_sem);
     271                 :            : 
     272         [ #  # ]:          0 :         if (cp_reason) {
     273                 :            :                 /* all the dirty node pages should be flushed for POR */
     274                 :          0 :                 ret = f2fs_sync_fs(inode->i_sb, 1);
     275                 :            : 
     276                 :            :                 /*
     277                 :            :                  * We've secured consistency through sync_fs. Following pino
     278                 :            :                  * will be used only for fsynced inodes after checkpoint.
     279                 :            :                  */
     280                 :          0 :                 try_to_fix_pino(inode);
     281                 :          0 :                 clear_inode_flag(inode, FI_APPEND_WRITE);
     282                 :          0 :                 clear_inode_flag(inode, FI_UPDATE_WRITE);
     283                 :          0 :                 goto out;
     284                 :            :         }
     285                 :            : sync_nodes:
     286                 :          0 :         atomic_inc(&sbi->wb_sync_req[NODE]);
     287                 :          0 :         ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
     288                 :            :         atomic_dec(&sbi->wb_sync_req[NODE]);
     289         [ #  # ]:          0 :         if (ret)
     290                 :            :                 goto out;
     291                 :            : 
     292                 :            :         /* if cp_error was enabled, we should avoid infinite loop */
     293         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(sbi))) {
     294                 :            :                 ret = -EIO;
     295                 :            :                 goto out;
     296                 :            :         }
     297                 :            : 
     298         [ #  # ]:          0 :         if (f2fs_need_inode_block_update(sbi, ino)) {
     299                 :          0 :                 f2fs_mark_inode_dirty_sync(inode, true);
     300                 :          0 :                 f2fs_write_inode(inode, NULL);
     301                 :          0 :                 goto sync_nodes;
     302                 :            :         }
     303                 :            : 
     304                 :            :         /*
     305                 :            :          * If it's atomic_write, it's just fine to keep write ordering. So
     306                 :            :          * here we don't need to wait for node write completion, since we use
     307                 :            :          * node chain which serializes node blocks. If one of node writes are
     308                 :            :          * reordered, we can see simply broken chain, resulting in stopping
     309                 :            :          * roll-forward recovery. It means we'll recover all or none node blocks
     310                 :            :          * given fsync mark.
     311                 :            :          */
     312         [ #  # ]:          0 :         if (!atomic) {
     313                 :          0 :                 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
     314         [ #  # ]:          0 :                 if (ret)
     315                 :            :                         goto out;
     316                 :            :         }
     317                 :            : 
     318                 :            :         /* once recovery info is written, don't need to tack this */
     319                 :          0 :         f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
     320                 :          0 :         clear_inode_flag(inode, FI_APPEND_WRITE);
     321                 :            : flush_out:
     322   [ #  #  #  # ]:          0 :         if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
     323                 :          0 :                 ret = f2fs_issue_flush(sbi, inode->i_ino);
     324         [ #  # ]:          0 :         if (!ret) {
     325                 :          0 :                 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
     326                 :          0 :                 clear_inode_flag(inode, FI_UPDATE_WRITE);
     327                 :          0 :                 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
     328                 :            :         }
     329                 :            :         f2fs_update_time(sbi, REQ_TIME);
     330                 :            : out:
     331                 :          0 :         trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
     332                 :            :         f2fs_trace_ios(NULL, 1);
     333                 :          0 :         return ret;
     334                 :            : }
     335                 :            : 
     336                 :          0 : int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
     337                 :            : {
     338         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
     339                 :            :                 return -EIO;
     340                 :          0 :         return f2fs_do_sync_file(file, start, end, datasync, false);
     341                 :            : }
     342                 :            : 
     343                 :          0 : static pgoff_t __get_first_dirty_index(struct address_space *mapping,
     344                 :            :                                                 pgoff_t pgofs, int whence)
     345                 :            : {
     346                 :            :         struct page *page;
     347                 :            :         int nr_pages;
     348                 :            : 
     349         [ #  # ]:          0 :         if (whence != SEEK_DATA)
     350                 :            :                 return 0;
     351                 :            : 
     352                 :            :         /* find first dirty page index */
     353                 :            :         nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
     354                 :            :                                       1, &page);
     355         [ #  # ]:          0 :         if (!nr_pages)
     356                 :            :                 return ULONG_MAX;
     357                 :          0 :         pgofs = page->index;
     358                 :          0 :         put_page(page);
     359                 :          0 :         return pgofs;
     360                 :            : }
     361                 :            : 
     362                 :            : static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
     363                 :            :                                 pgoff_t dirty, pgoff_t pgofs, int whence)
     364                 :            : {
     365      [ #  #  # ]:          0 :         switch (whence) {
     366                 :            :         case SEEK_DATA:
     367   [ #  #  #  # ]:          0 :                 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
     368                 :            :                         __is_valid_data_blkaddr(blkaddr))
     369                 :            :                         return true;
     370                 :            :                 break;
     371                 :            :         case SEEK_HOLE:
     372         [ #  # ]:          0 :                 if (blkaddr == NULL_ADDR)
     373                 :            :                         return true;
     374                 :            :                 break;
     375                 :            :         }
     376                 :            :         return false;
     377                 :            : }
     378                 :            : 
     379                 :          0 : static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
     380                 :            : {
     381                 :          0 :         struct inode *inode = file->f_mapping->host;
     382                 :          0 :         loff_t maxbytes = inode->i_sb->s_maxbytes;
     383                 :            :         struct dnode_of_data dn;
     384                 :            :         pgoff_t pgofs, end_offset, dirty;
     385                 :            :         loff_t data_ofs = offset;
     386                 :            :         loff_t isize;
     387                 :            :         int err = 0;
     388                 :            : 
     389                 :            :         inode_lock(inode);
     390                 :            : 
     391                 :            :         isize = i_size_read(inode);
     392         [ #  # ]:          0 :         if (offset >= isize)
     393                 :            :                 goto fail;
     394                 :            : 
     395                 :            :         /* handle inline data case */
     396   [ #  #  #  # ]:          0 :         if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
     397         [ #  # ]:          0 :                 if (whence == SEEK_HOLE)
     398                 :            :                         data_ofs = isize;
     399                 :            :                 goto found;
     400                 :            :         }
     401                 :            : 
     402                 :          0 :         pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
     403                 :            : 
     404                 :          0 :         dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
     405                 :            : 
     406         [ #  # ]:          0 :         for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
     407                 :            :                 set_new_dnode(&dn, inode, NULL, NULL, 0);
     408                 :          0 :                 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
     409         [ #  # ]:          0 :                 if (err && err != -ENOENT) {
     410                 :            :                         goto fail;
     411         [ #  # ]:          0 :                 } else if (err == -ENOENT) {
     412                 :            :                         /* direct node does not exists */
     413         [ #  # ]:          0 :                         if (whence == SEEK_DATA) {
     414                 :          0 :                                 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
     415                 :          0 :                                 continue;
     416                 :            :                         } else {
     417                 :            :                                 goto found;
     418                 :            :                         }
     419                 :            :                 }
     420                 :            : 
     421         [ #  # ]:          0 :                 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
     422                 :            : 
     423                 :            :                 /* find data/hole in dnode block */
     424         [ #  # ]:          0 :                 for (; dn.ofs_in_node < end_offset;
     425                 :          0 :                                 dn.ofs_in_node++, pgofs++,
     426                 :          0 :                                 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
     427                 :            :                         block_t blkaddr;
     428                 :            : 
     429                 :          0 :                         blkaddr = datablock_addr(dn.inode,
     430                 :            :                                         dn.node_page, dn.ofs_in_node);
     431                 :            : 
     432   [ #  #  #  # ]:          0 :                         if (__is_valid_data_blkaddr(blkaddr) &&
     433                 :          0 :                                 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
     434                 :            :                                         blkaddr, DATA_GENERIC_ENHANCE)) {
     435                 :          0 :                                 f2fs_put_dnode(&dn);
     436                 :          0 :                                 goto fail;
     437                 :            :                         }
     438                 :            : 
     439         [ #  # ]:          0 :                         if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
     440                 :            :                                                         pgofs, whence)) {
     441                 :          0 :                                 f2fs_put_dnode(&dn);
     442                 :          0 :                                 goto found;
     443                 :            :                         }
     444                 :            :                 }
     445                 :          0 :                 f2fs_put_dnode(&dn);
     446                 :            :         }
     447                 :            : 
     448         [ #  # ]:          0 :         if (whence == SEEK_DATA)
     449                 :            :                 goto fail;
     450                 :            : found:
     451         [ #  # ]:          0 :         if (whence == SEEK_HOLE && data_ofs > isize)
     452                 :            :                 data_ofs = isize;
     453                 :            :         inode_unlock(inode);
     454                 :          0 :         return vfs_setpos(file, data_ofs, maxbytes);
     455                 :            : fail:
     456                 :            :         inode_unlock(inode);
     457                 :          0 :         return -ENXIO;
     458                 :            : }
     459                 :            : 
     460                 :          0 : static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
     461                 :            : {
     462                 :          0 :         struct inode *inode = file->f_mapping->host;
     463                 :          0 :         loff_t maxbytes = inode->i_sb->s_maxbytes;
     464                 :            : 
     465      [ #  #  # ]:          0 :         switch (whence) {
     466                 :            :         case SEEK_SET:
     467                 :            :         case SEEK_CUR:
     468                 :            :         case SEEK_END:
     469                 :          0 :                 return generic_file_llseek_size(file, offset, whence,
     470                 :            :                                                 maxbytes, i_size_read(inode));
     471                 :            :         case SEEK_DATA:
     472                 :            :         case SEEK_HOLE:
     473         [ #  # ]:          0 :                 if (offset < 0)
     474                 :            :                         return -ENXIO;
     475                 :          0 :                 return f2fs_seek_block(file, offset, whence);
     476                 :            :         }
     477                 :            : 
     478                 :            :         return -EINVAL;
     479                 :            : }
     480                 :            : 
     481                 :          0 : static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
     482                 :            : {
     483                 :            :         struct inode *inode = file_inode(file);
     484                 :            :         int err;
     485                 :            : 
     486         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
     487                 :            :                 return -EIO;
     488                 :            : 
     489                 :            :         /* we don't need to use inline_data strictly */
     490                 :          0 :         err = f2fs_convert_inline_inode(inode);
     491         [ #  # ]:          0 :         if (err)
     492                 :            :                 return err;
     493                 :            : 
     494                 :            :         file_accessed(file);
     495                 :          0 :         vma->vm_ops = &f2fs_file_vm_ops;
     496                 :          0 :         return 0;
     497                 :            : }
     498                 :            : 
     499                 :          0 : static int f2fs_file_open(struct inode *inode, struct file *filp)
     500                 :            : {
     501                 :          0 :         int err = fscrypt_file_open(inode, filp);
     502                 :            : 
     503         [ #  # ]:          0 :         if (err)
     504                 :            :                 return err;
     505                 :            : 
     506                 :            :         err = fsverity_file_open(inode, filp);
     507         [ #  # ]:          0 :         if (err)
     508                 :            :                 return err;
     509                 :            : 
     510                 :          0 :         filp->f_mode |= FMODE_NOWAIT;
     511                 :            : 
     512                 :          0 :         return dquot_file_open(inode, filp);
     513                 :            : }
     514                 :            : 
     515                 :          0 : void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
     516                 :            : {
     517                 :          0 :         struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
     518                 :            :         struct f2fs_node *raw_node;
     519                 :          0 :         int nr_free = 0, ofs = dn->ofs_in_node, len = count;
     520                 :            :         __le32 *addr;
     521                 :            :         int base = 0;
     522                 :            : 
     523   [ #  #  #  # ]:          0 :         if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
     524                 :            :                 base = get_extra_isize(dn->inode);
     525                 :            : 
     526                 :            :         raw_node = F2FS_NODE(dn->node_page);
     527                 :          0 :         addr = blkaddr_in_node(raw_node) + base + ofs;
     528                 :            : 
     529         [ #  # ]:          0 :         for (; count > 0; count--, addr++, dn->ofs_in_node++) {
     530                 :          0 :                 block_t blkaddr = le32_to_cpu(*addr);
     531                 :            : 
     532         [ #  # ]:          0 :                 if (blkaddr == NULL_ADDR)
     533                 :          0 :                         continue;
     534                 :            : 
     535                 :          0 :                 dn->data_blkaddr = NULL_ADDR;
     536                 :          0 :                 f2fs_set_data_blkaddr(dn);
     537                 :            : 
     538   [ #  #  #  # ]:          0 :                 if (__is_valid_data_blkaddr(blkaddr) &&
     539                 :          0 :                         !f2fs_is_valid_blkaddr(sbi, blkaddr,
     540                 :            :                                         DATA_GENERIC_ENHANCE))
     541                 :          0 :                         continue;
     542                 :            : 
     543                 :          0 :                 f2fs_invalidate_blocks(sbi, blkaddr);
     544   [ #  #  #  # ]:          0 :                 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
     545                 :          0 :                         clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
     546                 :          0 :                 nr_free++;
     547                 :            :         }
     548                 :            : 
     549         [ #  # ]:          0 :         if (nr_free) {
     550                 :            :                 pgoff_t fofs;
     551                 :            :                 /*
     552                 :            :                  * once we invalidate valid blkaddr in range [ofs, ofs + count],
     553                 :            :                  * we will invalidate all blkaddr in the whole range.
     554                 :            :                  */
     555                 :          0 :                 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
     556                 :            :                                                         dn->inode) + ofs;
     557                 :          0 :                 f2fs_update_extent_cache_range(dn, fofs, 0, len);
     558                 :          0 :                 dec_valid_block_count(sbi, dn->inode, nr_free);
     559                 :            :         }
     560                 :          0 :         dn->ofs_in_node = ofs;
     561                 :            : 
     562                 :            :         f2fs_update_time(sbi, REQ_TIME);
     563                 :          0 :         trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
     564                 :            :                                          dn->ofs_in_node, nr_free);
     565                 :          0 : }
     566                 :            : 
     567                 :          0 : void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
     568                 :            : {
     569                 :          0 :         f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
     570                 :          0 : }
     571                 :            : 
     572                 :          0 : static int truncate_partial_data_page(struct inode *inode, u64 from,
     573                 :            :                                                                 bool cache_only)
     574                 :            : {
     575                 :          0 :         loff_t offset = from & (PAGE_SIZE - 1);
     576                 :          0 :         pgoff_t index = from >> PAGE_SHIFT;
     577                 :          0 :         struct address_space *mapping = inode->i_mapping;
     578                 :            :         struct page *page;
     579                 :            : 
     580         [ #  # ]:          0 :         if (!offset && !cache_only)
     581                 :            :                 return 0;
     582                 :            : 
     583         [ #  # ]:          0 :         if (cache_only) {
     584                 :            :                 page = find_lock_page(mapping, index);
     585   [ #  #  #  # ]:          0 :                 if (page && PageUptodate(page))
     586                 :            :                         goto truncate_out;
     587                 :          0 :                 f2fs_put_page(page, 1);
     588                 :          0 :                 return 0;
     589                 :            :         }
     590                 :            : 
     591                 :          0 :         page = f2fs_get_lock_data_page(inode, index, true);
     592         [ #  # ]:          0 :         if (IS_ERR(page))
     593         [ #  # ]:          0 :                 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
     594                 :            : truncate_out:
     595                 :          0 :         f2fs_wait_on_page_writeback(page, DATA, true, true);
     596                 :          0 :         zero_user(page, offset, PAGE_SIZE - offset);
     597                 :            : 
     598                 :            :         /* An encrypted inode should have a key and truncate the last page. */
     599   [ #  #  #  # ]:          0 :         f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
     600         [ #  # ]:          0 :         if (!cache_only)
     601                 :          0 :                 set_page_dirty(page);
     602                 :          0 :         f2fs_put_page(page, 1);
     603                 :          0 :         return 0;
     604                 :            : }
     605                 :            : 
     606                 :          0 : int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
     607                 :            : {
     608                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
     609                 :            :         struct dnode_of_data dn;
     610                 :            :         pgoff_t free_from;
     611                 :            :         int count = 0, err = 0;
     612                 :            :         struct page *ipage;
     613                 :            :         bool truncate_page = false;
     614                 :            : 
     615                 :          0 :         trace_f2fs_truncate_blocks_enter(inode, from);
     616                 :            : 
     617                 :          0 :         free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
     618                 :            : 
     619         [ #  # ]:          0 :         if (free_from >= sbi->max_file_blocks)
     620                 :            :                 goto free_partial;
     621                 :            : 
     622         [ #  # ]:          0 :         if (lock)
     623                 :            :                 f2fs_lock_op(sbi);
     624                 :            : 
     625                 :          0 :         ipage = f2fs_get_node_page(sbi, inode->i_ino);
     626         [ #  # ]:          0 :         if (IS_ERR(ipage)) {
     627                 :            :                 err = PTR_ERR(ipage);
     628                 :          0 :                 goto out;
     629                 :            :         }
     630                 :            : 
     631         [ #  # ]:          0 :         if (f2fs_has_inline_data(inode)) {
     632                 :          0 :                 f2fs_truncate_inline_inode(inode, ipage, from);
     633                 :          0 :                 f2fs_put_page(ipage, 1);
     634                 :            :                 truncate_page = true;
     635                 :          0 :                 goto out;
     636                 :            :         }
     637                 :            : 
     638                 :            :         set_new_dnode(&dn, inode, ipage, NULL, 0);
     639                 :          0 :         err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
     640         [ #  # ]:          0 :         if (err) {
     641         [ #  # ]:          0 :                 if (err == -ENOENT)
     642                 :            :                         goto free_next;
     643                 :            :                 goto out;
     644                 :            :         }
     645                 :            : 
     646         [ #  # ]:          0 :         count = ADDRS_PER_PAGE(dn.node_page, inode);
     647                 :            : 
     648                 :          0 :         count -= dn.ofs_in_node;
     649         [ #  # ]:          0 :         f2fs_bug_on(sbi, count < 0);
     650                 :            : 
     651   [ #  #  #  # ]:          0 :         if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
     652                 :          0 :                 f2fs_truncate_data_blocks_range(&dn, count);
     653                 :          0 :                 free_from += count;
     654                 :            :         }
     655                 :            : 
     656                 :          0 :         f2fs_put_dnode(&dn);
     657                 :            : free_next:
     658                 :          0 :         err = f2fs_truncate_inode_blocks(inode, free_from);
     659                 :            : out:
     660         [ #  # ]:          0 :         if (lock)
     661                 :            :                 f2fs_unlock_op(sbi);
     662                 :            : free_partial:
     663                 :            :         /* lastly zero out the first data page */
     664         [ #  # ]:          0 :         if (!err)
     665                 :          0 :                 err = truncate_partial_data_page(inode, from, truncate_page);
     666                 :            : 
     667                 :          0 :         trace_f2fs_truncate_blocks_exit(inode, err);
     668                 :          0 :         return err;
     669                 :            : }
     670                 :            : 
     671                 :          0 : int f2fs_truncate(struct inode *inode)
     672                 :            : {
     673                 :            :         int err;
     674                 :            : 
     675         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
     676                 :            :                 return -EIO;
     677                 :            : 
     678   [ #  #  #  # ]:          0 :         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
     679                 :            :                                 S_ISLNK(inode->i_mode)))
     680                 :            :                 return 0;
     681                 :            : 
     682                 :          0 :         trace_f2fs_truncate(inode);
     683                 :            : 
     684                 :            :         if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
     685                 :            :                 f2fs_show_injection_info(FAULT_TRUNCATE);
     686                 :            :                 return -EIO;
     687                 :            :         }
     688                 :            : 
     689                 :            :         /* we should check inline_data size */
     690         [ #  # ]:          0 :         if (!f2fs_may_inline_data(inode)) {
     691                 :          0 :                 err = f2fs_convert_inline_inode(inode);
     692         [ #  # ]:          0 :                 if (err)
     693                 :            :                         return err;
     694                 :            :         }
     695                 :            : 
     696                 :          0 :         err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
     697         [ #  # ]:          0 :         if (err)
     698                 :            :                 return err;
     699                 :            : 
     700                 :          0 :         inode->i_mtime = inode->i_ctime = current_time(inode);
     701                 :          0 :         f2fs_mark_inode_dirty_sync(inode, false);
     702                 :          0 :         return 0;
     703                 :            : }
     704                 :            : 
     705                 :          0 : int f2fs_getattr(const struct path *path, struct kstat *stat,
     706                 :            :                  u32 request_mask, unsigned int query_flags)
     707                 :            : {
     708                 :          0 :         struct inode *inode = d_inode(path->dentry);
     709                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
     710                 :            :         struct f2fs_inode *ri;
     711                 :            :         unsigned int flags;
     712                 :            : 
     713   [ #  #  #  # ]:          0 :         if (f2fs_has_extra_attr(inode) &&
     714         [ #  # ]:          0 :                         f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
     715                 :          0 :                         F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
     716                 :          0 :                 stat->result_mask |= STATX_BTIME;
     717                 :          0 :                 stat->btime.tv_sec = fi->i_crtime.tv_sec;
     718                 :          0 :                 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
     719                 :            :         }
     720                 :            : 
     721                 :          0 :         flags = fi->i_flags;
     722         [ #  # ]:          0 :         if (flags & F2FS_APPEND_FL)
     723                 :          0 :                 stat->attributes |= STATX_ATTR_APPEND;
     724         [ #  # ]:          0 :         if (IS_ENCRYPTED(inode))
     725                 :          0 :                 stat->attributes |= STATX_ATTR_ENCRYPTED;
     726         [ #  # ]:          0 :         if (flags & F2FS_IMMUTABLE_FL)
     727                 :          0 :                 stat->attributes |= STATX_ATTR_IMMUTABLE;
     728         [ #  # ]:          0 :         if (flags & F2FS_NODUMP_FL)
     729                 :          0 :                 stat->attributes |= STATX_ATTR_NODUMP;
     730                 :            : 
     731                 :          0 :         stat->attributes_mask |= (STATX_ATTR_APPEND |
     732                 :            :                                   STATX_ATTR_ENCRYPTED |
     733                 :            :                                   STATX_ATTR_IMMUTABLE |
     734                 :            :                                   STATX_ATTR_NODUMP);
     735                 :            : 
     736                 :          0 :         generic_fillattr(inode, stat);
     737                 :            : 
     738                 :            :         /* we need to show initial sectors used for inline_data/dentries */
     739   [ #  #  #  #  :          0 :         if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
                   #  # ]
     740                 :            :                                         f2fs_has_inline_dentry(inode))
     741                 :          0 :                 stat->blocks += (stat->size + 511) >> 9;
     742                 :            : 
     743                 :          0 :         return 0;
     744                 :            : }
     745                 :            : 
     746                 :            : #ifdef CONFIG_F2FS_FS_POSIX_ACL
     747                 :          0 : static void __setattr_copy(struct inode *inode, const struct iattr *attr)
     748                 :            : {
     749                 :          0 :         unsigned int ia_valid = attr->ia_valid;
     750                 :            : 
     751         [ #  # ]:          0 :         if (ia_valid & ATTR_UID)
     752                 :          0 :                 inode->i_uid = attr->ia_uid;
     753         [ #  # ]:          0 :         if (ia_valid & ATTR_GID)
     754                 :          0 :                 inode->i_gid = attr->ia_gid;
     755         [ #  # ]:          0 :         if (ia_valid & ATTR_ATIME)
     756                 :          0 :                 inode->i_atime = attr->ia_atime;
     757         [ #  # ]:          0 :         if (ia_valid & ATTR_MTIME)
     758                 :          0 :                 inode->i_mtime = attr->ia_mtime;
     759         [ #  # ]:          0 :         if (ia_valid & ATTR_CTIME)
     760                 :          0 :                 inode->i_ctime = attr->ia_ctime;
     761         [ #  # ]:          0 :         if (ia_valid & ATTR_MODE) {
     762                 :          0 :                 umode_t mode = attr->ia_mode;
     763                 :            : 
     764   [ #  #  #  # ]:          0 :                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
     765                 :          0 :                         mode &= ~S_ISGID;
     766                 :          0 :                 set_acl_inode(inode, mode);
     767                 :            :         }
     768                 :          0 : }
     769                 :            : #else
     770                 :            : #define __setattr_copy setattr_copy
     771                 :            : #endif
     772                 :            : 
     773                 :          0 : int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
     774                 :            : {
     775                 :            :         struct inode *inode = d_inode(dentry);
     776                 :            :         int err;
     777                 :            : 
     778         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
     779                 :            :                 return -EIO;
     780                 :            : 
     781                 :          0 :         err = setattr_prepare(dentry, attr);
     782         [ #  # ]:          0 :         if (err)
     783                 :            :                 return err;
     784                 :            : 
     785                 :            :         err = fscrypt_prepare_setattr(dentry, attr);
     786         [ #  # ]:          0 :         if (err)
     787                 :            :                 return err;
     788                 :            : 
     789                 :            :         err = fsverity_prepare_setattr(dentry, attr);
     790         [ #  # ]:          0 :         if (err)
     791                 :            :                 return err;
     792                 :            : 
     793         [ #  # ]:          0 :         if (is_quota_modification(inode, attr)) {
     794                 :          0 :                 err = dquot_initialize(inode);
     795         [ #  # ]:          0 :                 if (err)
     796                 :            :                         return err;
     797                 :            :         }
     798   [ #  #  #  # ]:          0 :         if ((attr->ia_valid & ATTR_UID &&
     799         [ #  # ]:          0 :                 !uid_eq(attr->ia_uid, inode->i_uid)) ||
     800         [ #  # ]:          0 :                 (attr->ia_valid & ATTR_GID &&
     801                 :            :                 !gid_eq(attr->ia_gid, inode->i_gid))) {
     802                 :            :                 f2fs_lock_op(F2FS_I_SB(inode));
     803                 :          0 :                 err = dquot_transfer(inode, attr);
     804         [ #  # ]:          0 :                 if (err) {
     805                 :            :                         set_sbi_flag(F2FS_I_SB(inode),
     806                 :            :                                         SBI_QUOTA_NEED_REPAIR);
     807                 :            :                         f2fs_unlock_op(F2FS_I_SB(inode));
     808                 :          0 :                         return err;
     809                 :            :                 }
     810                 :            :                 /*
     811                 :            :                  * update uid/gid under lock_op(), so that dquot and inode can
     812                 :            :                  * be updated atomically.
     813                 :            :                  */
     814         [ #  # ]:          0 :                 if (attr->ia_valid & ATTR_UID)
     815                 :          0 :                         inode->i_uid = attr->ia_uid;
     816         [ #  # ]:          0 :                 if (attr->ia_valid & ATTR_GID)
     817                 :          0 :                         inode->i_gid = attr->ia_gid;
     818                 :          0 :                 f2fs_mark_inode_dirty_sync(inode, true);
     819                 :            :                 f2fs_unlock_op(F2FS_I_SB(inode));
     820                 :            :         }
     821                 :            : 
     822         [ #  # ]:          0 :         if (attr->ia_valid & ATTR_SIZE) {
     823                 :            :                 loff_t old_size = i_size_read(inode);
     824                 :            : 
     825         [ #  # ]:          0 :                 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
     826                 :            :                         /*
     827                 :            :                          * should convert inline inode before i_size_write to
     828                 :            :                          * keep smaller than inline_data size with inline flag.
     829                 :            :                          */
     830                 :          0 :                         err = f2fs_convert_inline_inode(inode);
     831         [ #  # ]:          0 :                         if (err)
     832                 :            :                                 return err;
     833                 :            :                 }
     834                 :            : 
     835                 :          0 :                 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
     836                 :          0 :                 down_write(&F2FS_I(inode)->i_mmap_sem);
     837                 :            : 
     838                 :          0 :                 truncate_setsize(inode, attr->ia_size);
     839                 :            : 
     840         [ #  # ]:          0 :                 if (attr->ia_size <= old_size)
     841                 :          0 :                         err = f2fs_truncate(inode);
     842                 :            :                 /*
     843                 :            :                  * do not trim all blocks after i_size if target size is
     844                 :            :                  * larger than i_size.
     845                 :            :                  */
     846                 :          0 :                 up_write(&F2FS_I(inode)->i_mmap_sem);
     847                 :          0 :                 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
     848         [ #  # ]:          0 :                 if (err)
     849                 :            :                         return err;
     850                 :            : 
     851                 :          0 :                 down_write(&F2FS_I(inode)->i_sem);
     852                 :          0 :                 inode->i_mtime = inode->i_ctime = current_time(inode);
     853                 :          0 :                 F2FS_I(inode)->last_disk_size = i_size_read(inode);
     854                 :          0 :                 up_write(&F2FS_I(inode)->i_sem);
     855                 :            :         }
     856                 :            : 
     857                 :          0 :         __setattr_copy(inode, attr);
     858                 :            : 
     859         [ #  # ]:          0 :         if (attr->ia_valid & ATTR_MODE) {
     860         [ #  # ]:          0 :                 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
     861   [ #  #  #  # ]:          0 :                 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
     862                 :          0 :                         inode->i_mode = F2FS_I(inode)->i_acl_mode;
     863                 :          0 :                         clear_inode_flag(inode, FI_ACL_MODE);
     864                 :            :                 }
     865                 :            :         }
     866                 :            : 
     867                 :            :         /* file size may changed here */
     868                 :          0 :         f2fs_mark_inode_dirty_sync(inode, true);
     869                 :            : 
     870                 :            :         /* inode change will produce dirty node pages flushed by checkpoint */
     871                 :          0 :         f2fs_balance_fs(F2FS_I_SB(inode), true);
     872                 :            : 
     873                 :          0 :         return err;
     874                 :            : }
     875                 :            : 
     876                 :            : const struct inode_operations f2fs_file_inode_operations = {
     877                 :            :         .getattr        = f2fs_getattr,
     878                 :            :         .setattr        = f2fs_setattr,
     879                 :            :         .get_acl        = f2fs_get_acl,
     880                 :            :         .set_acl        = f2fs_set_acl,
     881                 :            : #ifdef CONFIG_F2FS_FS_XATTR
     882                 :            :         .listxattr      = f2fs_listxattr,
     883                 :            : #endif
     884                 :            :         .fiemap         = f2fs_fiemap,
     885                 :            : };
     886                 :            : 
     887                 :          0 : static int fill_zero(struct inode *inode, pgoff_t index,
     888                 :            :                                         loff_t start, loff_t len)
     889                 :            : {
     890                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
     891                 :            :         struct page *page;
     892                 :            : 
     893         [ #  # ]:          0 :         if (!len)
     894                 :            :                 return 0;
     895                 :            : 
     896                 :          0 :         f2fs_balance_fs(sbi, true);
     897                 :            : 
     898                 :            :         f2fs_lock_op(sbi);
     899                 :          0 :         page = f2fs_get_new_data_page(inode, NULL, index, false);
     900                 :            :         f2fs_unlock_op(sbi);
     901                 :            : 
     902         [ #  # ]:          0 :         if (IS_ERR(page))
     903                 :          0 :                 return PTR_ERR(page);
     904                 :            : 
     905                 :          0 :         f2fs_wait_on_page_writeback(page, DATA, true, true);
     906                 :          0 :         zero_user(page, start, len);
     907                 :          0 :         set_page_dirty(page);
     908                 :          0 :         f2fs_put_page(page, 1);
     909                 :          0 :         return 0;
     910                 :            : }
     911                 :            : 
     912                 :          0 : int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
     913                 :            : {
     914                 :            :         int err;
     915                 :            : 
     916         [ #  # ]:          0 :         while (pg_start < pg_end) {
     917                 :            :                 struct dnode_of_data dn;
     918                 :            :                 pgoff_t end_offset, count;
     919                 :            : 
     920                 :            :                 set_new_dnode(&dn, inode, NULL, NULL, 0);
     921                 :          0 :                 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
     922         [ #  # ]:          0 :                 if (err) {
     923         [ #  # ]:          0 :                         if (err == -ENOENT) {
     924                 :          0 :                                 pg_start = f2fs_get_next_page_offset(&dn,
     925                 :            :                                                                 pg_start);
     926                 :          0 :                                 continue;
     927                 :            :                         }
     928                 :          0 :                         return err;
     929                 :            :                 }
     930                 :            : 
     931         [ #  # ]:          0 :                 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
     932                 :          0 :                 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
     933                 :            : 
     934         [ #  # ]:          0 :                 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
     935                 :            : 
     936                 :          0 :                 f2fs_truncate_data_blocks_range(&dn, count);
     937                 :          0 :                 f2fs_put_dnode(&dn);
     938                 :            : 
     939                 :          0 :                 pg_start += count;
     940                 :            :         }
     941                 :            :         return 0;
     942                 :            : }
     943                 :            : 
     944                 :          0 : static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
     945                 :            : {
     946                 :            :         pgoff_t pg_start, pg_end;
     947                 :            :         loff_t off_start, off_end;
     948                 :            :         int ret;
     949                 :            : 
     950                 :          0 :         ret = f2fs_convert_inline_inode(inode);
     951         [ #  # ]:          0 :         if (ret)
     952                 :            :                 return ret;
     953                 :            : 
     954                 :          0 :         pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
     955                 :          0 :         pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
     956                 :            : 
     957                 :          0 :         off_start = offset & (PAGE_SIZE - 1);
     958                 :          0 :         off_end = (offset + len) & (PAGE_SIZE - 1);
     959                 :            : 
     960         [ #  # ]:          0 :         if (pg_start == pg_end) {
     961                 :          0 :                 ret = fill_zero(inode, pg_start, off_start,
     962                 :            :                                                 off_end - off_start);
     963         [ #  # ]:          0 :                 if (ret)
     964                 :            :                         return ret;
     965                 :            :         } else {
     966         [ #  # ]:          0 :                 if (off_start) {
     967                 :          0 :                         ret = fill_zero(inode, pg_start++, off_start,
     968                 :            :                                                 PAGE_SIZE - off_start);
     969         [ #  # ]:          0 :                         if (ret)
     970                 :            :                                 return ret;
     971                 :            :                 }
     972         [ #  # ]:          0 :                 if (off_end) {
     973                 :          0 :                         ret = fill_zero(inode, pg_end, 0, off_end);
     974         [ #  # ]:          0 :                         if (ret)
     975                 :            :                                 return ret;
     976                 :            :                 }
     977                 :            : 
     978         [ #  # ]:          0 :                 if (pg_start < pg_end) {
     979                 :          0 :                         struct address_space *mapping = inode->i_mapping;
     980                 :            :                         loff_t blk_start, blk_end;
     981                 :            :                         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
     982                 :            : 
     983                 :          0 :                         f2fs_balance_fs(sbi, true);
     984                 :            : 
     985                 :          0 :                         blk_start = (loff_t)pg_start << PAGE_SHIFT;
     986                 :          0 :                         blk_end = (loff_t)pg_end << PAGE_SHIFT;
     987                 :            : 
     988                 :          0 :                         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
     989                 :          0 :                         down_write(&F2FS_I(inode)->i_mmap_sem);
     990                 :            : 
     991                 :          0 :                         truncate_inode_pages_range(mapping, blk_start,
     992                 :            :                                         blk_end - 1);
     993                 :            : 
     994                 :            :                         f2fs_lock_op(sbi);
     995                 :          0 :                         ret = f2fs_truncate_hole(inode, pg_start, pg_end);
     996                 :            :                         f2fs_unlock_op(sbi);
     997                 :            : 
     998                 :          0 :                         up_write(&F2FS_I(inode)->i_mmap_sem);
     999                 :          0 :                         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1000                 :            :                 }
    1001                 :            :         }
    1002                 :            : 
    1003                 :          0 :         return ret;
    1004                 :            : }
    1005                 :            : 
    1006                 :          0 : static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
    1007                 :            :                                 int *do_replace, pgoff_t off, pgoff_t len)
    1008                 :            : {
    1009                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    1010                 :            :         struct dnode_of_data dn;
    1011                 :            :         int ret, done, i;
    1012                 :            : 
    1013                 :            : next_dnode:
    1014                 :            :         set_new_dnode(&dn, inode, NULL, NULL, 0);
    1015                 :          0 :         ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
    1016         [ #  # ]:          0 :         if (ret && ret != -ENOENT) {
    1017                 :          0 :                 return ret;
    1018         [ #  # ]:          0 :         } else if (ret == -ENOENT) {
    1019         [ #  # ]:          0 :                 if (dn.max_level == 0)
    1020                 :            :                         return -ENOENT;
    1021                 :          0 :                 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) - dn.ofs_in_node,
    1022                 :            :                                                                         len);
    1023                 :          0 :                 blkaddr += done;
    1024                 :          0 :                 do_replace += done;
    1025                 :          0 :                 goto next;
    1026                 :            :         }
    1027                 :            : 
    1028         [ #  # ]:          0 :         done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
    1029                 :            :                                                         dn.ofs_in_node, len);
    1030         [ #  # ]:          0 :         for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
    1031                 :          0 :                 *blkaddr = datablock_addr(dn.inode,
    1032                 :            :                                         dn.node_page, dn.ofs_in_node);
    1033                 :            : 
    1034   [ #  #  #  # ]:          0 :                 if (__is_valid_data_blkaddr(*blkaddr) &&
    1035                 :          0 :                         !f2fs_is_valid_blkaddr(sbi, *blkaddr,
    1036                 :            :                                         DATA_GENERIC_ENHANCE)) {
    1037                 :          0 :                         f2fs_put_dnode(&dn);
    1038                 :          0 :                         return -EFSCORRUPTED;
    1039                 :            :                 }
    1040                 :            : 
    1041         [ #  # ]:          0 :                 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
    1042                 :            : 
    1043         [ #  # ]:          0 :                         if (test_opt(sbi, LFS)) {
    1044                 :          0 :                                 f2fs_put_dnode(&dn);
    1045                 :          0 :                                 return -EOPNOTSUPP;
    1046                 :            :                         }
    1047                 :            : 
    1048                 :            :                         /* do not invalidate this block address */
    1049                 :          0 :                         f2fs_update_data_blkaddr(&dn, NULL_ADDR);
    1050                 :          0 :                         *do_replace = 1;
    1051                 :            :                 }
    1052                 :            :         }
    1053                 :          0 :         f2fs_put_dnode(&dn);
    1054                 :            : next:
    1055                 :          0 :         len -= done;
    1056                 :          0 :         off += done;
    1057         [ #  # ]:          0 :         if (len)
    1058                 :            :                 goto next_dnode;
    1059                 :            :         return 0;
    1060                 :            : }
    1061                 :            : 
    1062                 :          0 : static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
    1063                 :            :                                 int *do_replace, pgoff_t off, int len)
    1064                 :            : {
    1065                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    1066                 :            :         struct dnode_of_data dn;
    1067                 :            :         int ret, i;
    1068                 :            : 
    1069         [ #  # ]:          0 :         for (i = 0; i < len; i++, do_replace++, blkaddr++) {
    1070         [ #  # ]:          0 :                 if (*do_replace == 0)
    1071                 :          0 :                         continue;
    1072                 :            : 
    1073                 :            :                 set_new_dnode(&dn, inode, NULL, NULL, 0);
    1074                 :          0 :                 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
    1075         [ #  # ]:          0 :                 if (ret) {
    1076                 :          0 :                         dec_valid_block_count(sbi, inode, 1);
    1077                 :          0 :                         f2fs_invalidate_blocks(sbi, *blkaddr);
    1078                 :            :                 } else {
    1079                 :          0 :                         f2fs_update_data_blkaddr(&dn, *blkaddr);
    1080                 :            :                 }
    1081                 :          0 :                 f2fs_put_dnode(&dn);
    1082                 :            :         }
    1083                 :          0 :         return 0;
    1084                 :            : }
    1085                 :            : 
    1086                 :          0 : static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
    1087                 :            :                         block_t *blkaddr, int *do_replace,
    1088                 :            :                         pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
    1089                 :            : {
    1090                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
    1091                 :            :         pgoff_t i = 0;
    1092                 :            :         int ret;
    1093                 :            : 
    1094         [ #  # ]:          0 :         while (i < len) {
    1095   [ #  #  #  # ]:          0 :                 if (blkaddr[i] == NULL_ADDR && !full) {
    1096                 :          0 :                         i++;
    1097                 :          0 :                         continue;
    1098                 :            :                 }
    1099                 :            : 
    1100   [ #  #  #  # ]:          0 :                 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
    1101                 :            :                         struct dnode_of_data dn;
    1102                 :            :                         struct node_info ni;
    1103                 :            :                         size_t new_size;
    1104                 :            :                         pgoff_t ilen;
    1105                 :            : 
    1106                 :            :                         set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
    1107                 :          0 :                         ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
    1108         [ #  # ]:          0 :                         if (ret)
    1109                 :          0 :                                 return ret;
    1110                 :            : 
    1111                 :          0 :                         ret = f2fs_get_node_info(sbi, dn.nid, &ni);
    1112         [ #  # ]:          0 :                         if (ret) {
    1113                 :          0 :                                 f2fs_put_dnode(&dn);
    1114                 :          0 :                                 return ret;
    1115                 :            :                         }
    1116                 :            : 
    1117         [ #  # ]:          0 :                         ilen = min((pgoff_t)
    1118                 :            :                                 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
    1119                 :            :                                                 dn.ofs_in_node, len - i);
    1120                 :            :                         do {
    1121                 :          0 :                                 dn.data_blkaddr = datablock_addr(dn.inode,
    1122                 :            :                                                 dn.node_page, dn.ofs_in_node);
    1123                 :          0 :                                 f2fs_truncate_data_blocks_range(&dn, 1);
    1124                 :            : 
    1125         [ #  # ]:          0 :                                 if (do_replace[i]) {
    1126                 :          0 :                                         f2fs_i_blocks_write(src_inode,
    1127                 :            :                                                         1, false, false);
    1128                 :          0 :                                         f2fs_i_blocks_write(dst_inode,
    1129                 :            :                                                         1, true, false);
    1130                 :          0 :                                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
    1131                 :          0 :                                         blkaddr[i], ni.version, true, false);
    1132                 :            : 
    1133                 :          0 :                                         do_replace[i] = 0;
    1134                 :            :                                 }
    1135                 :          0 :                                 dn.ofs_in_node++;
    1136                 :          0 :                                 i++;
    1137                 :          0 :                                 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
    1138         [ #  # ]:          0 :                                 if (dst_inode->i_size < new_size)
    1139                 :          0 :                                         f2fs_i_size_write(dst_inode, new_size);
    1140   [ #  #  #  #  :          0 :                         } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
                   #  # ]
    1141                 :            : 
    1142                 :          0 :                         f2fs_put_dnode(&dn);
    1143                 :            :                 } else {
    1144                 :            :                         struct page *psrc, *pdst;
    1145                 :            : 
    1146                 :          0 :                         psrc = f2fs_get_lock_data_page(src_inode,
    1147                 :            :                                                         src + i, true);
    1148         [ #  # ]:          0 :                         if (IS_ERR(psrc))
    1149                 :          0 :                                 return PTR_ERR(psrc);
    1150                 :          0 :                         pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
    1151                 :            :                                                                 true);
    1152         [ #  # ]:          0 :                         if (IS_ERR(pdst)) {
    1153                 :          0 :                                 f2fs_put_page(psrc, 1);
    1154                 :          0 :                                 return PTR_ERR(pdst);
    1155                 :            :                         }
    1156                 :          0 :                         f2fs_copy_page(psrc, pdst);
    1157                 :          0 :                         set_page_dirty(pdst);
    1158                 :          0 :                         f2fs_put_page(pdst, 1);
    1159                 :          0 :                         f2fs_put_page(psrc, 1);
    1160                 :            : 
    1161                 :          0 :                         ret = f2fs_truncate_hole(src_inode,
    1162                 :            :                                                 src + i, src + i + 1);
    1163         [ #  # ]:          0 :                         if (ret)
    1164                 :          0 :                                 return ret;
    1165                 :          0 :                         i++;
    1166                 :            :                 }
    1167                 :            :         }
    1168                 :            :         return 0;
    1169                 :            : }
    1170                 :            : 
    1171                 :          0 : static int __exchange_data_block(struct inode *src_inode,
    1172                 :            :                         struct inode *dst_inode, pgoff_t src, pgoff_t dst,
    1173                 :            :                         pgoff_t len, bool full)
    1174                 :            : {
    1175                 :            :         block_t *src_blkaddr;
    1176                 :            :         int *do_replace;
    1177                 :            :         pgoff_t olen;
    1178                 :            :         int ret;
    1179                 :            : 
    1180         [ #  # ]:          0 :         while (len) {
    1181                 :          0 :                 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
    1182                 :            : 
    1183                 :            :                 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
    1184                 :            :                                         array_size(olen, sizeof(block_t)),
    1185                 :            :                                         GFP_KERNEL);
    1186         [ #  # ]:          0 :                 if (!src_blkaddr)
    1187                 :            :                         return -ENOMEM;
    1188                 :            : 
    1189                 :            :                 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
    1190                 :            :                                         array_size(olen, sizeof(int)),
    1191                 :            :                                         GFP_KERNEL);
    1192         [ #  # ]:          0 :                 if (!do_replace) {
    1193                 :          0 :                         kvfree(src_blkaddr);
    1194                 :          0 :                         return -ENOMEM;
    1195                 :            :                 }
    1196                 :            : 
    1197                 :          0 :                 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
    1198                 :            :                                         do_replace, src, olen);
    1199         [ #  # ]:          0 :                 if (ret)
    1200                 :            :                         goto roll_back;
    1201                 :            : 
    1202                 :          0 :                 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
    1203                 :            :                                         do_replace, src, dst, olen, full);
    1204         [ #  # ]:          0 :                 if (ret)
    1205                 :            :                         goto roll_back;
    1206                 :            : 
    1207                 :          0 :                 src += olen;
    1208                 :          0 :                 dst += olen;
    1209                 :          0 :                 len -= olen;
    1210                 :            : 
    1211                 :          0 :                 kvfree(src_blkaddr);
    1212                 :          0 :                 kvfree(do_replace);
    1213                 :            :         }
    1214                 :            :         return 0;
    1215                 :            : 
    1216                 :            : roll_back:
    1217                 :          0 :         __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
    1218                 :          0 :         kvfree(src_blkaddr);
    1219                 :          0 :         kvfree(do_replace);
    1220                 :          0 :         return ret;
    1221                 :            : }
    1222                 :            : 
    1223                 :          0 : static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
    1224                 :            : {
    1225                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    1226                 :          0 :         pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
    1227                 :          0 :         pgoff_t start = offset >> PAGE_SHIFT;
    1228                 :          0 :         pgoff_t end = (offset + len) >> PAGE_SHIFT;
    1229                 :            :         int ret;
    1230                 :            : 
    1231                 :          0 :         f2fs_balance_fs(sbi, true);
    1232                 :            : 
    1233                 :            :         /* avoid gc operation during block exchange */
    1234                 :          0 :         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1235                 :          0 :         down_write(&F2FS_I(inode)->i_mmap_sem);
    1236                 :            : 
    1237                 :            :         f2fs_lock_op(sbi);
    1238                 :          0 :         f2fs_drop_extent_tree(inode);
    1239                 :          0 :         truncate_pagecache(inode, offset);
    1240                 :          0 :         ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
    1241                 :            :         f2fs_unlock_op(sbi);
    1242                 :            : 
    1243                 :          0 :         up_write(&F2FS_I(inode)->i_mmap_sem);
    1244                 :          0 :         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1245                 :          0 :         return ret;
    1246                 :            : }
    1247                 :            : 
    1248                 :          0 : static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
    1249                 :            : {
    1250                 :            :         loff_t new_size;
    1251                 :            :         int ret;
    1252                 :            : 
    1253         [ #  # ]:          0 :         if (offset + len >= i_size_read(inode))
    1254                 :            :                 return -EINVAL;
    1255                 :            : 
    1256                 :            :         /* collapse range should be aligned to block size of f2fs. */
    1257   [ #  #  #  # ]:          0 :         if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
    1258                 :            :                 return -EINVAL;
    1259                 :            : 
    1260                 :          0 :         ret = f2fs_convert_inline_inode(inode);
    1261         [ #  # ]:          0 :         if (ret)
    1262                 :            :                 return ret;
    1263                 :            : 
    1264                 :            :         /* write out all dirty pages from offset */
    1265                 :          0 :         ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
    1266         [ #  # ]:          0 :         if (ret)
    1267                 :            :                 return ret;
    1268                 :            : 
    1269                 :          0 :         ret = f2fs_do_collapse(inode, offset, len);
    1270         [ #  # ]:          0 :         if (ret)
    1271                 :            :                 return ret;
    1272                 :            : 
    1273                 :            :         /* write out all moved pages, if possible */
    1274                 :          0 :         down_write(&F2FS_I(inode)->i_mmap_sem);
    1275                 :          0 :         filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
    1276                 :          0 :         truncate_pagecache(inode, offset);
    1277                 :            : 
    1278                 :          0 :         new_size = i_size_read(inode) - len;
    1279                 :          0 :         truncate_pagecache(inode, new_size);
    1280                 :            : 
    1281                 :          0 :         ret = f2fs_truncate_blocks(inode, new_size, true);
    1282                 :          0 :         up_write(&F2FS_I(inode)->i_mmap_sem);
    1283         [ #  # ]:          0 :         if (!ret)
    1284                 :          0 :                 f2fs_i_size_write(inode, new_size);
    1285                 :          0 :         return ret;
    1286                 :            : }
    1287                 :            : 
    1288                 :          0 : static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
    1289                 :            :                                                                 pgoff_t end)
    1290                 :            : {
    1291                 :          0 :         struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
    1292                 :            :         pgoff_t index = start;
    1293                 :          0 :         unsigned int ofs_in_node = dn->ofs_in_node;
    1294                 :            :         blkcnt_t count = 0;
    1295                 :            :         int ret;
    1296                 :            : 
    1297         [ #  # ]:          0 :         for (; index < end; index++, dn->ofs_in_node++) {
    1298         [ #  # ]:          0 :                 if (datablock_addr(dn->inode, dn->node_page,
    1299                 :            :                                         dn->ofs_in_node) == NULL_ADDR)
    1300                 :          0 :                         count++;
    1301                 :            :         }
    1302                 :            : 
    1303                 :          0 :         dn->ofs_in_node = ofs_in_node;
    1304                 :          0 :         ret = f2fs_reserve_new_blocks(dn, count);
    1305         [ #  # ]:          0 :         if (ret)
    1306                 :            :                 return ret;
    1307                 :            : 
    1308                 :          0 :         dn->ofs_in_node = ofs_in_node;
    1309         [ #  # ]:          0 :         for (index = start; index < end; index++, dn->ofs_in_node++) {
    1310                 :          0 :                 dn->data_blkaddr = datablock_addr(dn->inode,
    1311                 :            :                                         dn->node_page, dn->ofs_in_node);
    1312                 :            :                 /*
    1313                 :            :                  * f2fs_reserve_new_blocks will not guarantee entire block
    1314                 :            :                  * allocation.
    1315                 :            :                  */
    1316         [ #  # ]:          0 :                 if (dn->data_blkaddr == NULL_ADDR) {
    1317                 :            :                         ret = -ENOSPC;
    1318                 :            :                         break;
    1319                 :            :                 }
    1320         [ #  # ]:          0 :                 if (dn->data_blkaddr != NEW_ADDR) {
    1321                 :          0 :                         f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
    1322                 :          0 :                         dn->data_blkaddr = NEW_ADDR;
    1323                 :          0 :                         f2fs_set_data_blkaddr(dn);
    1324                 :            :                 }
    1325                 :            :         }
    1326                 :            : 
    1327                 :          0 :         f2fs_update_extent_cache_range(dn, start, 0, index - start);
    1328                 :            : 
    1329                 :          0 :         return ret;
    1330                 :            : }
    1331                 :            : 
    1332                 :          0 : static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
    1333                 :            :                                                                 int mode)
    1334                 :            : {
    1335                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    1336                 :          0 :         struct address_space *mapping = inode->i_mapping;
    1337                 :            :         pgoff_t index, pg_start, pg_end;
    1338                 :            :         loff_t new_size = i_size_read(inode);
    1339                 :            :         loff_t off_start, off_end;
    1340                 :            :         int ret = 0;
    1341                 :            : 
    1342                 :          0 :         ret = inode_newsize_ok(inode, (len + offset));
    1343         [ #  # ]:          0 :         if (ret)
    1344                 :            :                 return ret;
    1345                 :            : 
    1346                 :          0 :         ret = f2fs_convert_inline_inode(inode);
    1347         [ #  # ]:          0 :         if (ret)
    1348                 :            :                 return ret;
    1349                 :            : 
    1350                 :          0 :         ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
    1351         [ #  # ]:          0 :         if (ret)
    1352                 :            :                 return ret;
    1353                 :            : 
    1354                 :          0 :         pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
    1355                 :          0 :         pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
    1356                 :            : 
    1357                 :          0 :         off_start = offset & (PAGE_SIZE - 1);
    1358                 :          0 :         off_end = (offset + len) & (PAGE_SIZE - 1);
    1359                 :            : 
    1360         [ #  # ]:          0 :         if (pg_start == pg_end) {
    1361                 :          0 :                 ret = fill_zero(inode, pg_start, off_start,
    1362                 :            :                                                 off_end - off_start);
    1363         [ #  # ]:          0 :                 if (ret)
    1364                 :            :                         return ret;
    1365                 :            : 
    1366                 :          0 :                 new_size = max_t(loff_t, new_size, offset + len);
    1367                 :            :         } else {
    1368         [ #  # ]:          0 :                 if (off_start) {
    1369                 :          0 :                         ret = fill_zero(inode, pg_start++, off_start,
    1370                 :            :                                                 PAGE_SIZE - off_start);
    1371         [ #  # ]:          0 :                         if (ret)
    1372                 :            :                                 return ret;
    1373                 :            : 
    1374                 :          0 :                         new_size = max_t(loff_t, new_size,
    1375                 :            :                                         (loff_t)pg_start << PAGE_SHIFT);
    1376                 :            :                 }
    1377                 :            : 
    1378         [ #  # ]:          0 :                 for (index = pg_start; index < pg_end;) {
    1379                 :            :                         struct dnode_of_data dn;
    1380                 :            :                         unsigned int end_offset;
    1381                 :            :                         pgoff_t end;
    1382                 :            : 
    1383                 :          0 :                         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1384                 :          0 :                         down_write(&F2FS_I(inode)->i_mmap_sem);
    1385                 :            : 
    1386                 :          0 :                         truncate_pagecache_range(inode,
    1387                 :          0 :                                 (loff_t)index << PAGE_SHIFT,
    1388                 :          0 :                                 ((loff_t)pg_end << PAGE_SHIFT) - 1);
    1389                 :            : 
    1390                 :            :                         f2fs_lock_op(sbi);
    1391                 :            : 
    1392                 :            :                         set_new_dnode(&dn, inode, NULL, NULL, 0);
    1393                 :          0 :                         ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
    1394         [ #  # ]:          0 :                         if (ret) {
    1395                 :            :                                 f2fs_unlock_op(sbi);
    1396                 :          0 :                                 up_write(&F2FS_I(inode)->i_mmap_sem);
    1397                 :          0 :                                 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1398                 :          0 :                                 goto out;
    1399                 :            :                         }
    1400                 :            : 
    1401         [ #  # ]:          0 :                         end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
    1402                 :          0 :                         end = min(pg_end, end_offset - dn.ofs_in_node + index);
    1403                 :            : 
    1404                 :          0 :                         ret = f2fs_do_zero_range(&dn, index, end);
    1405                 :          0 :                         f2fs_put_dnode(&dn);
    1406                 :            : 
    1407                 :            :                         f2fs_unlock_op(sbi);
    1408                 :          0 :                         up_write(&F2FS_I(inode)->i_mmap_sem);
    1409                 :          0 :                         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1410                 :            : 
    1411                 :          0 :                         f2fs_balance_fs(sbi, dn.node_changed);
    1412                 :            : 
    1413         [ #  # ]:          0 :                         if (ret)
    1414                 :            :                                 goto out;
    1415                 :            : 
    1416                 :            :                         index = end;
    1417                 :          0 :                         new_size = max_t(loff_t, new_size,
    1418                 :            :                                         (loff_t)index << PAGE_SHIFT);
    1419                 :            :                 }
    1420                 :            : 
    1421         [ #  # ]:          0 :                 if (off_end) {
    1422                 :          0 :                         ret = fill_zero(inode, pg_end, 0, off_end);
    1423         [ #  # ]:          0 :                         if (ret)
    1424                 :            :                                 goto out;
    1425                 :            : 
    1426                 :          0 :                         new_size = max_t(loff_t, new_size, offset + len);
    1427                 :            :                 }
    1428                 :            :         }
    1429                 :            : 
    1430                 :            : out:
    1431         [ #  # ]:          0 :         if (new_size > i_size_read(inode)) {
    1432         [ #  # ]:          0 :                 if (mode & FALLOC_FL_KEEP_SIZE)
    1433                 :            :                         file_set_keep_isize(inode);
    1434                 :            :                 else
    1435                 :          0 :                         f2fs_i_size_write(inode, new_size);
    1436                 :            :         }
    1437                 :          0 :         return ret;
    1438                 :            : }
    1439                 :            : 
    1440                 :          0 : static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
    1441                 :            : {
    1442                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    1443                 :            :         pgoff_t nr, pg_start, pg_end, delta, idx;
    1444                 :            :         loff_t new_size;
    1445                 :            :         int ret = 0;
    1446                 :            : 
    1447                 :          0 :         new_size = i_size_read(inode) + len;
    1448                 :          0 :         ret = inode_newsize_ok(inode, new_size);
    1449         [ #  # ]:          0 :         if (ret)
    1450                 :            :                 return ret;
    1451                 :            : 
    1452         [ #  # ]:          0 :         if (offset >= i_size_read(inode))
    1453                 :            :                 return -EINVAL;
    1454                 :            : 
    1455                 :            :         /* insert range should be aligned to block size of f2fs. */
    1456   [ #  #  #  # ]:          0 :         if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
    1457                 :            :                 return -EINVAL;
    1458                 :            : 
    1459                 :          0 :         ret = f2fs_convert_inline_inode(inode);
    1460         [ #  # ]:          0 :         if (ret)
    1461                 :            :                 return ret;
    1462                 :            : 
    1463                 :          0 :         f2fs_balance_fs(sbi, true);
    1464                 :            : 
    1465                 :          0 :         down_write(&F2FS_I(inode)->i_mmap_sem);
    1466                 :          0 :         ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
    1467                 :          0 :         up_write(&F2FS_I(inode)->i_mmap_sem);
    1468         [ #  # ]:          0 :         if (ret)
    1469                 :            :                 return ret;
    1470                 :            : 
    1471                 :            :         /* write out all dirty pages from offset */
    1472                 :          0 :         ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
    1473         [ #  # ]:          0 :         if (ret)
    1474                 :            :                 return ret;
    1475                 :            : 
    1476                 :          0 :         pg_start = offset >> PAGE_SHIFT;
    1477                 :          0 :         pg_end = (offset + len) >> PAGE_SHIFT;
    1478                 :          0 :         delta = pg_end - pg_start;
    1479                 :          0 :         idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
    1480                 :            : 
    1481                 :            :         /* avoid gc operation during block exchange */
    1482                 :          0 :         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1483                 :          0 :         down_write(&F2FS_I(inode)->i_mmap_sem);
    1484                 :          0 :         truncate_pagecache(inode, offset);
    1485                 :            : 
    1486         [ #  # ]:          0 :         while (!ret && idx > pg_start) {
    1487                 :          0 :                 nr = idx - pg_start;
    1488         [ #  # ]:          0 :                 if (nr > delta)
    1489                 :            :                         nr = delta;
    1490                 :          0 :                 idx -= nr;
    1491                 :            : 
    1492                 :            :                 f2fs_lock_op(sbi);
    1493                 :          0 :                 f2fs_drop_extent_tree(inode);
    1494                 :            : 
    1495                 :          0 :                 ret = __exchange_data_block(inode, inode, idx,
    1496                 :            :                                         idx + delta, nr, false);
    1497                 :            :                 f2fs_unlock_op(sbi);
    1498                 :            :         }
    1499                 :          0 :         up_write(&F2FS_I(inode)->i_mmap_sem);
    1500                 :          0 :         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1501                 :            : 
    1502                 :            :         /* write out all moved pages, if possible */
    1503                 :          0 :         down_write(&F2FS_I(inode)->i_mmap_sem);
    1504                 :          0 :         filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
    1505                 :          0 :         truncate_pagecache(inode, offset);
    1506                 :          0 :         up_write(&F2FS_I(inode)->i_mmap_sem);
    1507                 :            : 
    1508         [ #  # ]:          0 :         if (!ret)
    1509                 :          0 :                 f2fs_i_size_write(inode, new_size);
    1510                 :          0 :         return ret;
    1511                 :            : }
    1512                 :            : 
    1513                 :          0 : static int expand_inode_data(struct inode *inode, loff_t offset,
    1514                 :            :                                         loff_t len, int mode)
    1515                 :            : {
    1516                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    1517                 :          0 :         struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
    1518                 :            :                         .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
    1519                 :            :                         .m_may_create = true };
    1520                 :            :         pgoff_t pg_end;
    1521                 :            :         loff_t new_size = i_size_read(inode);
    1522                 :            :         loff_t off_end;
    1523                 :            :         int err;
    1524                 :            : 
    1525                 :          0 :         err = inode_newsize_ok(inode, (len + offset));
    1526         [ #  # ]:          0 :         if (err)
    1527                 :            :                 return err;
    1528                 :            : 
    1529                 :          0 :         err = f2fs_convert_inline_inode(inode);
    1530         [ #  # ]:          0 :         if (err)
    1531                 :            :                 return err;
    1532                 :            : 
    1533                 :          0 :         f2fs_balance_fs(sbi, true);
    1534                 :            : 
    1535                 :          0 :         pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
    1536                 :          0 :         off_end = (offset + len) & (PAGE_SIZE - 1);
    1537                 :            : 
    1538                 :          0 :         map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
    1539                 :          0 :         map.m_len = pg_end - map.m_lblk;
    1540         [ #  # ]:          0 :         if (off_end)
    1541                 :          0 :                 map.m_len++;
    1542                 :            : 
    1543         [ #  # ]:          0 :         if (f2fs_is_pinned_file(inode))
    1544                 :          0 :                 map.m_seg_type = CURSEG_COLD_DATA;
    1545                 :            : 
    1546         [ #  # ]:          0 :         err = f2fs_map_blocks(inode, &map, 1, (f2fs_is_pinned_file(inode) ?
    1547                 :            :                                                 F2FS_GET_BLOCK_PRE_DIO :
    1548                 :            :                                                 F2FS_GET_BLOCK_PRE_AIO));
    1549         [ #  # ]:          0 :         if (err) {
    1550                 :            :                 pgoff_t last_off;
    1551                 :            : 
    1552         [ #  # ]:          0 :                 if (!map.m_len)
    1553                 :            :                         return err;
    1554                 :            : 
    1555                 :          0 :                 last_off = map.m_lblk + map.m_len - 1;
    1556                 :            : 
    1557                 :            :                 /* update new size to the failed position */
    1558         [ #  # ]:          0 :                 new_size = (last_off == pg_end) ? offset + len :
    1559                 :          0 :                                         (loff_t)(last_off + 1) << PAGE_SHIFT;
    1560                 :            :         } else {
    1561                 :          0 :                 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
    1562                 :            :         }
    1563                 :            : 
    1564         [ #  # ]:          0 :         if (new_size > i_size_read(inode)) {
    1565         [ #  # ]:          0 :                 if (mode & FALLOC_FL_KEEP_SIZE)
    1566                 :            :                         file_set_keep_isize(inode);
    1567                 :            :                 else
    1568                 :          0 :                         f2fs_i_size_write(inode, new_size);
    1569                 :            :         }
    1570                 :            : 
    1571                 :          0 :         return err;
    1572                 :            : }
    1573                 :            : 
    1574                 :          0 : static long f2fs_fallocate(struct file *file, int mode,
    1575                 :            :                                 loff_t offset, loff_t len)
    1576                 :            : {
    1577                 :            :         struct inode *inode = file_inode(file);
    1578                 :            :         long ret = 0;
    1579                 :            : 
    1580         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
    1581                 :            :                 return -EIO;
    1582         [ #  # ]:          0 :         if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
    1583                 :            :                 return -ENOSPC;
    1584                 :            : 
    1585                 :            :         /* f2fs only support ->fallocate for regular file */
    1586         [ #  # ]:          0 :         if (!S_ISREG(inode->i_mode))
    1587                 :            :                 return -EINVAL;
    1588                 :            : 
    1589   [ #  #  #  # ]:          0 :         if (IS_ENCRYPTED(inode) &&
    1590                 :          0 :                 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
    1591                 :            :                 return -EOPNOTSUPP;
    1592                 :            : 
    1593         [ #  # ]:          0 :         if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
    1594                 :            :                         FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
    1595                 :            :                         FALLOC_FL_INSERT_RANGE))
    1596                 :            :                 return -EOPNOTSUPP;
    1597                 :            : 
    1598                 :            :         inode_lock(inode);
    1599                 :            : 
    1600         [ #  # ]:          0 :         if (mode & FALLOC_FL_PUNCH_HOLE) {
    1601         [ #  # ]:          0 :                 if (offset >= inode->i_size)
    1602                 :            :                         goto out;
    1603                 :            : 
    1604                 :          0 :                 ret = punch_hole(inode, offset, len);
    1605         [ #  # ]:          0 :         } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
    1606                 :          0 :                 ret = f2fs_collapse_range(inode, offset, len);
    1607         [ #  # ]:          0 :         } else if (mode & FALLOC_FL_ZERO_RANGE) {
    1608                 :          0 :                 ret = f2fs_zero_range(inode, offset, len, mode);
    1609         [ #  # ]:          0 :         } else if (mode & FALLOC_FL_INSERT_RANGE) {
    1610                 :          0 :                 ret = f2fs_insert_range(inode, offset, len);
    1611                 :            :         } else {
    1612                 :          0 :                 ret = expand_inode_data(inode, offset, len, mode);
    1613                 :            :         }
    1614                 :            : 
    1615         [ #  # ]:          0 :         if (!ret) {
    1616                 :          0 :                 inode->i_mtime = inode->i_ctime = current_time(inode);
    1617                 :          0 :                 f2fs_mark_inode_dirty_sync(inode, false);
    1618                 :            :                 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    1619                 :            :         }
    1620                 :            : 
    1621                 :            : out:
    1622                 :            :         inode_unlock(inode);
    1623                 :            : 
    1624                 :          0 :         trace_f2fs_fallocate(inode, mode, offset, len, ret);
    1625                 :          0 :         return ret;
    1626                 :            : }
    1627                 :            : 
    1628                 :          0 : static int f2fs_release_file(struct inode *inode, struct file *filp)
    1629                 :            : {
    1630                 :            :         /*
    1631                 :            :          * f2fs_relase_file is called at every close calls. So we should
    1632                 :            :          * not drop any inmemory pages by close called by other process.
    1633                 :            :          */
    1634   [ #  #  #  # ]:          0 :         if (!(filp->f_mode & FMODE_WRITE) ||
    1635                 :          0 :                         atomic_read(&inode->i_writecount) != 1)
    1636                 :            :                 return 0;
    1637                 :            : 
    1638                 :            :         /* some remained atomic pages should discarded */
    1639         [ #  # ]:          0 :         if (f2fs_is_atomic_file(inode))
    1640                 :          0 :                 f2fs_drop_inmem_pages(inode);
    1641         [ #  # ]:          0 :         if (f2fs_is_volatile_file(inode)) {
    1642                 :          0 :                 set_inode_flag(inode, FI_DROP_CACHE);
    1643                 :          0 :                 filemap_fdatawrite(inode->i_mapping);
    1644                 :          0 :                 clear_inode_flag(inode, FI_DROP_CACHE);
    1645                 :          0 :                 clear_inode_flag(inode, FI_VOLATILE_FILE);
    1646                 :          0 :                 stat_dec_volatile_write(inode);
    1647                 :            :         }
    1648                 :            :         return 0;
    1649                 :            : }
    1650                 :            : 
    1651                 :          0 : static int f2fs_file_flush(struct file *file, fl_owner_t id)
    1652                 :            : {
    1653                 :            :         struct inode *inode = file_inode(file);
    1654                 :            : 
    1655                 :            :         /*
    1656                 :            :          * If the process doing a transaction is crashed, we should do
    1657                 :            :          * roll-back. Otherwise, other reader/write can see corrupted database
    1658                 :            :          * until all the writers close its file. Since this should be done
    1659                 :            :          * before dropping file lock, it needs to do in ->flush.
    1660                 :            :          */
    1661   [ #  #  #  # ]:          0 :         if (f2fs_is_atomic_file(inode) &&
    1662                 :          0 :                         F2FS_I(inode)->inmem_task == current)
    1663                 :          0 :                 f2fs_drop_inmem_pages(inode);
    1664                 :          0 :         return 0;
    1665                 :            : }
    1666                 :            : 
    1667                 :          0 : static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
    1668                 :            : {
    1669                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    1670                 :            : 
    1671                 :            :         /* Is it quota file? Do not allow user to mess with it */
    1672         [ #  # ]:          0 :         if (IS_NOQUOTA(inode))
    1673                 :            :                 return -EPERM;
    1674                 :            : 
    1675         [ #  # ]:          0 :         if ((iflags ^ fi->i_flags) & F2FS_CASEFOLD_FL) {
    1676         [ #  # ]:          0 :                 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
    1677                 :            :                         return -EOPNOTSUPP;
    1678         [ #  # ]:          0 :                 if (!f2fs_empty_dir(inode))
    1679                 :            :                         return -ENOTEMPTY;
    1680                 :            :         }
    1681                 :            : 
    1682                 :          0 :         fi->i_flags = iflags | (fi->i_flags & ~mask);
    1683                 :            : 
    1684         [ #  # ]:          0 :         if (fi->i_flags & F2FS_PROJINHERIT_FL)
    1685                 :          0 :                 set_inode_flag(inode, FI_PROJ_INHERIT);
    1686                 :            :         else
    1687                 :          0 :                 clear_inode_flag(inode, FI_PROJ_INHERIT);
    1688                 :            : 
    1689                 :          0 :         inode->i_ctime = current_time(inode);
    1690                 :          0 :         f2fs_set_inode_flags(inode);
    1691                 :          0 :         f2fs_mark_inode_dirty_sync(inode, true);
    1692                 :          0 :         return 0;
    1693                 :            : }
    1694                 :            : 
    1695                 :            : /* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
    1696                 :            : 
    1697                 :            : /*
    1698                 :            :  * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
    1699                 :            :  * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
    1700                 :            :  * F2FS_GETTABLE_FS_FL.  To also make it settable via FS_IOC_SETFLAGS, also add
    1701                 :            :  * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
    1702                 :            :  */
    1703                 :            : 
    1704                 :            : static const struct {
    1705                 :            :         u32 iflag;
    1706                 :            :         u32 fsflag;
    1707                 :            : } f2fs_fsflags_map[] = {
    1708                 :            :         { F2FS_SYNC_FL,         FS_SYNC_FL },
    1709                 :            :         { F2FS_IMMUTABLE_FL,    FS_IMMUTABLE_FL },
    1710                 :            :         { F2FS_APPEND_FL,       FS_APPEND_FL },
    1711                 :            :         { F2FS_NODUMP_FL,       FS_NODUMP_FL },
    1712                 :            :         { F2FS_NOATIME_FL,      FS_NOATIME_FL },
    1713                 :            :         { F2FS_INDEX_FL,        FS_INDEX_FL },
    1714                 :            :         { F2FS_DIRSYNC_FL,      FS_DIRSYNC_FL },
    1715                 :            :         { F2FS_PROJINHERIT_FL,  FS_PROJINHERIT_FL },
    1716                 :            :         { F2FS_CASEFOLD_FL,     FS_CASEFOLD_FL },
    1717                 :            : };
    1718                 :            : 
    1719                 :            : #define F2FS_GETTABLE_FS_FL (           \
    1720                 :            :                 FS_SYNC_FL |            \
    1721                 :            :                 FS_IMMUTABLE_FL |       \
    1722                 :            :                 FS_APPEND_FL |          \
    1723                 :            :                 FS_NODUMP_FL |          \
    1724                 :            :                 FS_NOATIME_FL |         \
    1725                 :            :                 FS_INDEX_FL |           \
    1726                 :            :                 FS_DIRSYNC_FL |         \
    1727                 :            :                 FS_PROJINHERIT_FL |     \
    1728                 :            :                 FS_ENCRYPT_FL |         \
    1729                 :            :                 FS_INLINE_DATA_FL |     \
    1730                 :            :                 FS_NOCOW_FL |           \
    1731                 :            :                 FS_VERITY_FL |          \
    1732                 :            :                 FS_CASEFOLD_FL)
    1733                 :            : 
    1734                 :            : #define F2FS_SETTABLE_FS_FL (           \
    1735                 :            :                 FS_SYNC_FL |            \
    1736                 :            :                 FS_IMMUTABLE_FL |       \
    1737                 :            :                 FS_APPEND_FL |          \
    1738                 :            :                 FS_NODUMP_FL |          \
    1739                 :            :                 FS_NOATIME_FL |         \
    1740                 :            :                 FS_DIRSYNC_FL |         \
    1741                 :            :                 FS_PROJINHERIT_FL |     \
    1742                 :            :                 FS_CASEFOLD_FL)
    1743                 :            : 
    1744                 :            : /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
    1745                 :            : static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
    1746                 :            : {
    1747                 :            :         u32 fsflags = 0;
    1748                 :            :         int i;
    1749                 :            : 
    1750   [ #  #  #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
    1751   [ #  #  #  # ]:          0 :                 if (iflags & f2fs_fsflags_map[i].iflag)
    1752                 :          0 :                         fsflags |= f2fs_fsflags_map[i].fsflag;
    1753                 :            : 
    1754                 :          0 :         return fsflags;
    1755                 :            : }
    1756                 :            : 
    1757                 :            : /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
    1758                 :            : static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
    1759                 :            : {
    1760                 :            :         u32 iflags = 0;
    1761                 :            :         int i;
    1762                 :            : 
    1763   [ #  #  #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
    1764   [ #  #  #  # ]:          0 :                 if (fsflags & f2fs_fsflags_map[i].fsflag)
    1765                 :          0 :                         iflags |= f2fs_fsflags_map[i].iflag;
    1766                 :            : 
    1767                 :          0 :         return iflags;
    1768                 :            : }
    1769                 :            : 
    1770                 :          0 : static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
    1771                 :            : {
    1772                 :            :         struct inode *inode = file_inode(filp);
    1773                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    1774                 :          0 :         u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
    1775                 :            : 
    1776         [ #  # ]:          0 :         if (IS_ENCRYPTED(inode))
    1777                 :          0 :                 fsflags |= FS_ENCRYPT_FL;
    1778         [ #  # ]:          0 :         if (IS_VERITY(inode))
    1779                 :          0 :                 fsflags |= FS_VERITY_FL;
    1780   [ #  #  #  # ]:          0 :         if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
    1781                 :          0 :                 fsflags |= FS_INLINE_DATA_FL;
    1782         [ #  # ]:          0 :         if (is_inode_flag_set(inode, FI_PIN_FILE))
    1783                 :          0 :                 fsflags |= FS_NOCOW_FL;
    1784                 :            : 
    1785                 :          0 :         fsflags &= F2FS_GETTABLE_FS_FL;
    1786                 :            : 
    1787                 :          0 :         return put_user(fsflags, (int __user *)arg);
    1788                 :            : }
    1789                 :            : 
    1790                 :          0 : static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
    1791                 :            : {
    1792                 :            :         struct inode *inode = file_inode(filp);
    1793                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    1794                 :            :         u32 fsflags, old_fsflags;
    1795                 :            :         u32 iflags;
    1796                 :            :         int ret;
    1797                 :            : 
    1798         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    1799                 :            :                 return -EACCES;
    1800                 :            : 
    1801         [ #  # ]:          0 :         if (get_user(fsflags, (int __user *)arg))
    1802                 :            :                 return -EFAULT;
    1803                 :            : 
    1804         [ #  # ]:          0 :         if (fsflags & ~F2FS_GETTABLE_FS_FL)
    1805                 :            :                 return -EOPNOTSUPP;
    1806                 :          0 :         fsflags &= F2FS_SETTABLE_FS_FL;
    1807                 :            : 
    1808                 :            :         iflags = f2fs_fsflags_to_iflags(fsflags);
    1809         [ #  # ]:          0 :         if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
    1810                 :            :                 return -EOPNOTSUPP;
    1811                 :            : 
    1812                 :          0 :         ret = mnt_want_write_file(filp);
    1813         [ #  # ]:          0 :         if (ret)
    1814                 :            :                 return ret;
    1815                 :            : 
    1816                 :            :         inode_lock(inode);
    1817                 :            : 
    1818                 :          0 :         old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
    1819                 :          0 :         ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
    1820         [ #  # ]:          0 :         if (ret)
    1821                 :            :                 goto out;
    1822                 :            : 
    1823                 :          0 :         ret = f2fs_setflags_common(inode, iflags,
    1824                 :            :                         f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
    1825                 :            : out:
    1826                 :            :         inode_unlock(inode);
    1827                 :          0 :         mnt_drop_write_file(filp);
    1828                 :          0 :         return ret;
    1829                 :            : }
    1830                 :            : 
    1831                 :          0 : static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
    1832                 :            : {
    1833                 :            :         struct inode *inode = file_inode(filp);
    1834                 :            : 
    1835                 :          0 :         return put_user(inode->i_generation, (int __user *)arg);
    1836                 :            : }
    1837                 :            : 
    1838                 :          0 : static int f2fs_ioc_start_atomic_write(struct file *filp)
    1839                 :            : {
    1840                 :            :         struct inode *inode = file_inode(filp);
    1841                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    1842                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    1843                 :            :         int ret;
    1844                 :            : 
    1845         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    1846                 :            :                 return -EACCES;
    1847                 :            : 
    1848         [ #  # ]:          0 :         if (!S_ISREG(inode->i_mode))
    1849                 :            :                 return -EINVAL;
    1850                 :            : 
    1851         [ #  # ]:          0 :         if (filp->f_flags & O_DIRECT)
    1852                 :            :                 return -EINVAL;
    1853                 :            : 
    1854                 :          0 :         ret = mnt_want_write_file(filp);
    1855         [ #  # ]:          0 :         if (ret)
    1856                 :            :                 return ret;
    1857                 :            : 
    1858                 :            :         inode_lock(inode);
    1859                 :            : 
    1860         [ #  # ]:          0 :         if (f2fs_is_atomic_file(inode)) {
    1861         [ #  # ]:          0 :                 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
    1862                 :            :                         ret = -EINVAL;
    1863                 :            :                 goto out;
    1864                 :            :         }
    1865                 :            : 
    1866                 :          0 :         ret = f2fs_convert_inline_inode(inode);
    1867         [ #  # ]:          0 :         if (ret)
    1868                 :            :                 goto out;
    1869                 :            : 
    1870                 :          0 :         down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1871                 :            : 
    1872                 :            :         /*
    1873                 :            :          * Should wait end_io to count F2FS_WB_CP_DATA correctly by
    1874                 :            :          * f2fs_is_atomic_file.
    1875                 :            :          */
    1876         [ #  # ]:          0 :         if (get_dirty_pages(inode))
    1877                 :          0 :                 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
    1878                 :            :                           inode->i_ino, get_dirty_pages(inode));
    1879                 :          0 :         ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
    1880         [ #  # ]:          0 :         if (ret) {
    1881                 :          0 :                 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1882                 :          0 :                 goto out;
    1883                 :            :         }
    1884                 :            : 
    1885                 :            :         spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
    1886         [ #  # ]:          0 :         if (list_empty(&fi->inmem_ilist))
    1887                 :          0 :                 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
    1888                 :          0 :         sbi->atomic_files++;
    1889                 :            :         spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
    1890                 :            : 
    1891                 :            :         /* add inode in inmem_list first and set atomic_file */
    1892                 :          0 :         set_inode_flag(inode, FI_ATOMIC_FILE);
    1893                 :          0 :         clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
    1894                 :          0 :         up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
    1895                 :            : 
    1896                 :            :         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    1897                 :          0 :         F2FS_I(inode)->inmem_task = current;
    1898                 :          0 :         stat_inc_atomic_write(inode);
    1899         [ #  # ]:          0 :         stat_update_max_atomic_write(inode);
    1900                 :            : out:
    1901                 :            :         inode_unlock(inode);
    1902                 :          0 :         mnt_drop_write_file(filp);
    1903                 :          0 :         return ret;
    1904                 :            : }
    1905                 :            : 
    1906                 :          0 : static int f2fs_ioc_commit_atomic_write(struct file *filp)
    1907                 :            : {
    1908                 :            :         struct inode *inode = file_inode(filp);
    1909                 :            :         int ret;
    1910                 :            : 
    1911         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    1912                 :            :                 return -EACCES;
    1913                 :            : 
    1914                 :          0 :         ret = mnt_want_write_file(filp);
    1915         [ #  # ]:          0 :         if (ret)
    1916                 :            :                 return ret;
    1917                 :            : 
    1918                 :          0 :         f2fs_balance_fs(F2FS_I_SB(inode), true);
    1919                 :            : 
    1920                 :            :         inode_lock(inode);
    1921                 :            : 
    1922         [ #  # ]:          0 :         if (f2fs_is_volatile_file(inode)) {
    1923                 :            :                 ret = -EINVAL;
    1924                 :            :                 goto err_out;
    1925                 :            :         }
    1926                 :            : 
    1927         [ #  # ]:          0 :         if (f2fs_is_atomic_file(inode)) {
    1928                 :          0 :                 ret = f2fs_commit_inmem_pages(inode);
    1929         [ #  # ]:          0 :                 if (ret)
    1930                 :            :                         goto err_out;
    1931                 :            : 
    1932                 :          0 :                 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
    1933         [ #  # ]:          0 :                 if (!ret)
    1934                 :          0 :                         f2fs_drop_inmem_pages(inode);
    1935                 :            :         } else {
    1936                 :          0 :                 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
    1937                 :            :         }
    1938                 :            : err_out:
    1939         [ #  # ]:          0 :         if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
    1940                 :          0 :                 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
    1941                 :            :                 ret = -EINVAL;
    1942                 :            :         }
    1943                 :            :         inode_unlock(inode);
    1944                 :          0 :         mnt_drop_write_file(filp);
    1945                 :          0 :         return ret;
    1946                 :            : }
    1947                 :            : 
    1948                 :          0 : static int f2fs_ioc_start_volatile_write(struct file *filp)
    1949                 :            : {
    1950                 :            :         struct inode *inode = file_inode(filp);
    1951                 :            :         int ret;
    1952                 :            : 
    1953         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    1954                 :            :                 return -EACCES;
    1955                 :            : 
    1956         [ #  # ]:          0 :         if (!S_ISREG(inode->i_mode))
    1957                 :            :                 return -EINVAL;
    1958                 :            : 
    1959                 :          0 :         ret = mnt_want_write_file(filp);
    1960         [ #  # ]:          0 :         if (ret)
    1961                 :            :                 return ret;
    1962                 :            : 
    1963                 :            :         inode_lock(inode);
    1964                 :            : 
    1965         [ #  # ]:          0 :         if (f2fs_is_volatile_file(inode))
    1966                 :            :                 goto out;
    1967                 :            : 
    1968                 :          0 :         ret = f2fs_convert_inline_inode(inode);
    1969         [ #  # ]:          0 :         if (ret)
    1970                 :            :                 goto out;
    1971                 :            : 
    1972                 :          0 :         stat_inc_volatile_write(inode);
    1973         [ #  # ]:          0 :         stat_update_max_volatile_write(inode);
    1974                 :            : 
    1975                 :          0 :         set_inode_flag(inode, FI_VOLATILE_FILE);
    1976                 :            :         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    1977                 :            : out:
    1978                 :            :         inode_unlock(inode);
    1979                 :          0 :         mnt_drop_write_file(filp);
    1980                 :          0 :         return ret;
    1981                 :            : }
    1982                 :            : 
    1983                 :          0 : static int f2fs_ioc_release_volatile_write(struct file *filp)
    1984                 :            : {
    1985                 :            :         struct inode *inode = file_inode(filp);
    1986                 :            :         int ret;
    1987                 :            : 
    1988         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    1989                 :            :                 return -EACCES;
    1990                 :            : 
    1991                 :          0 :         ret = mnt_want_write_file(filp);
    1992         [ #  # ]:          0 :         if (ret)
    1993                 :            :                 return ret;
    1994                 :            : 
    1995                 :            :         inode_lock(inode);
    1996                 :            : 
    1997         [ #  # ]:          0 :         if (!f2fs_is_volatile_file(inode))
    1998                 :            :                 goto out;
    1999                 :            : 
    2000         [ #  # ]:          0 :         if (!f2fs_is_first_block_written(inode)) {
    2001                 :          0 :                 ret = truncate_partial_data_page(inode, 0, true);
    2002                 :          0 :                 goto out;
    2003                 :            :         }
    2004                 :            : 
    2005                 :          0 :         ret = punch_hole(inode, 0, F2FS_BLKSIZE);
    2006                 :            : out:
    2007                 :            :         inode_unlock(inode);
    2008                 :          0 :         mnt_drop_write_file(filp);
    2009                 :          0 :         return ret;
    2010                 :            : }
    2011                 :            : 
    2012                 :          0 : static int f2fs_ioc_abort_volatile_write(struct file *filp)
    2013                 :            : {
    2014                 :            :         struct inode *inode = file_inode(filp);
    2015                 :            :         int ret;
    2016                 :            : 
    2017         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    2018                 :            :                 return -EACCES;
    2019                 :            : 
    2020                 :          0 :         ret = mnt_want_write_file(filp);
    2021         [ #  # ]:          0 :         if (ret)
    2022                 :            :                 return ret;
    2023                 :            : 
    2024                 :            :         inode_lock(inode);
    2025                 :            : 
    2026         [ #  # ]:          0 :         if (f2fs_is_atomic_file(inode))
    2027                 :          0 :                 f2fs_drop_inmem_pages(inode);
    2028         [ #  # ]:          0 :         if (f2fs_is_volatile_file(inode)) {
    2029                 :          0 :                 clear_inode_flag(inode, FI_VOLATILE_FILE);
    2030                 :          0 :                 stat_dec_volatile_write(inode);
    2031                 :          0 :                 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
    2032                 :            :         }
    2033                 :            : 
    2034                 :          0 :         clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
    2035                 :            : 
    2036                 :            :         inode_unlock(inode);
    2037                 :            : 
    2038                 :          0 :         mnt_drop_write_file(filp);
    2039                 :            :         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    2040                 :          0 :         return ret;
    2041                 :            : }
    2042                 :            : 
    2043                 :          0 : static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
    2044                 :            : {
    2045                 :            :         struct inode *inode = file_inode(filp);
    2046                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2047                 :          0 :         struct super_block *sb = sbi->sb;
    2048                 :            :         __u32 in;
    2049                 :            :         int ret = 0;
    2050                 :            : 
    2051         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2052                 :            :                 return -EPERM;
    2053                 :            : 
    2054         [ #  # ]:          0 :         if (get_user(in, (__u32 __user *)arg))
    2055                 :            :                 return -EFAULT;
    2056                 :            : 
    2057         [ #  # ]:          0 :         if (in != F2FS_GOING_DOWN_FULLSYNC) {
    2058                 :          0 :                 ret = mnt_want_write_file(filp);
    2059         [ #  # ]:          0 :                 if (ret) {
    2060         [ #  # ]:          0 :                         if (ret == -EROFS) {
    2061                 :            :                                 ret = 0;
    2062                 :          0 :                                 f2fs_stop_checkpoint(sbi, false);
    2063                 :            :                                 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
    2064                 :          0 :                                 trace_f2fs_shutdown(sbi, in, ret);
    2065                 :            :                         }
    2066                 :          0 :                         return ret;
    2067                 :            :                 }
    2068                 :            :         }
    2069                 :            : 
    2070   [ #  #  #  #  :          0 :         switch (in) {
                   #  # ]
    2071                 :            :         case F2FS_GOING_DOWN_FULLSYNC:
    2072                 :          0 :                 sb = freeze_bdev(sb->s_bdev);
    2073         [ #  # ]:          0 :                 if (IS_ERR(sb)) {
    2074                 :            :                         ret = PTR_ERR(sb);
    2075                 :          0 :                         goto out;
    2076                 :            :                 }
    2077         [ #  # ]:          0 :                 if (sb) {
    2078                 :          0 :                         f2fs_stop_checkpoint(sbi, false);
    2079                 :            :                         set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
    2080                 :          0 :                         thaw_bdev(sb->s_bdev, sb);
    2081                 :            :                 }
    2082                 :            :                 break;
    2083                 :            :         case F2FS_GOING_DOWN_METASYNC:
    2084                 :            :                 /* do checkpoint only */
    2085                 :          0 :                 ret = f2fs_sync_fs(sb, 1);
    2086         [ #  # ]:          0 :                 if (ret)
    2087                 :            :                         goto out;
    2088                 :          0 :                 f2fs_stop_checkpoint(sbi, false);
    2089                 :            :                 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
    2090                 :            :                 break;
    2091                 :            :         case F2FS_GOING_DOWN_NOSYNC:
    2092                 :          0 :                 f2fs_stop_checkpoint(sbi, false);
    2093                 :            :                 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
    2094                 :            :                 break;
    2095                 :            :         case F2FS_GOING_DOWN_METAFLUSH:
    2096                 :          0 :                 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
    2097                 :          0 :                 f2fs_stop_checkpoint(sbi, false);
    2098                 :            :                 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
    2099                 :            :                 break;
    2100                 :            :         case F2FS_GOING_DOWN_NEED_FSCK:
    2101                 :            :                 set_sbi_flag(sbi, SBI_NEED_FSCK);
    2102                 :            :                 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
    2103                 :            :                 set_sbi_flag(sbi, SBI_IS_DIRTY);
    2104                 :            :                 /* do checkpoint only */
    2105                 :          0 :                 ret = f2fs_sync_fs(sb, 1);
    2106                 :          0 :                 goto out;
    2107                 :            :         default:
    2108                 :            :                 ret = -EINVAL;
    2109                 :            :                 goto out;
    2110                 :            :         }
    2111                 :            : 
    2112                 :          0 :         f2fs_stop_gc_thread(sbi);
    2113                 :          0 :         f2fs_stop_discard_thread(sbi);
    2114                 :            : 
    2115                 :          0 :         f2fs_drop_discard_cmd(sbi);
    2116                 :          0 :         clear_opt(sbi, DISCARD);
    2117                 :            : 
    2118                 :            :         f2fs_update_time(sbi, REQ_TIME);
    2119                 :            : out:
    2120         [ #  # ]:          0 :         if (in != F2FS_GOING_DOWN_FULLSYNC)
    2121                 :          0 :                 mnt_drop_write_file(filp);
    2122                 :            : 
    2123                 :          0 :         trace_f2fs_shutdown(sbi, in, ret);
    2124                 :            : 
    2125                 :          0 :         return ret;
    2126                 :            : }
    2127                 :            : 
    2128                 :          0 : static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
    2129                 :            : {
    2130                 :            :         struct inode *inode = file_inode(filp);
    2131                 :          0 :         struct super_block *sb = inode->i_sb;
    2132                 :          0 :         struct request_queue *q = bdev_get_queue(sb->s_bdev);
    2133                 :            :         struct fstrim_range range;
    2134                 :            :         int ret;
    2135                 :            : 
    2136         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2137                 :            :                 return -EPERM;
    2138                 :            : 
    2139         [ #  # ]:          0 :         if (!f2fs_hw_support_discard(F2FS_SB(sb)))
    2140                 :            :                 return -EOPNOTSUPP;
    2141                 :            : 
    2142         [ #  # ]:          0 :         if (copy_from_user(&range, (struct fstrim_range __user *)arg,
    2143                 :            :                                 sizeof(range)))
    2144                 :            :                 return -EFAULT;
    2145                 :            : 
    2146                 :          0 :         ret = mnt_want_write_file(filp);
    2147         [ #  # ]:          0 :         if (ret)
    2148                 :            :                 return ret;
    2149                 :            : 
    2150                 :          0 :         range.minlen = max((unsigned int)range.minlen,
    2151                 :            :                                 q->limits.discard_granularity);
    2152                 :          0 :         ret = f2fs_trim_fs(F2FS_SB(sb), &range);
    2153                 :          0 :         mnt_drop_write_file(filp);
    2154         [ #  # ]:          0 :         if (ret < 0)
    2155                 :            :                 return ret;
    2156                 :            : 
    2157         [ #  # ]:          0 :         if (copy_to_user((struct fstrim_range __user *)arg, &range,
    2158                 :            :                                 sizeof(range)))
    2159                 :            :                 return -EFAULT;
    2160                 :            :         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    2161                 :          0 :         return 0;
    2162                 :            : }
    2163                 :            : 
    2164                 :            : static bool uuid_is_nonzero(__u8 u[16])
    2165                 :            : {
    2166                 :            :         int i;
    2167                 :            : 
    2168         [ #  # ]:          0 :         for (i = 0; i < 16; i++)
    2169         [ #  # ]:          0 :                 if (u[i])
    2170                 :            :                         return true;
    2171                 :            :         return false;
    2172                 :            : }
    2173                 :            : 
    2174                 :          0 : static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
    2175                 :            : {
    2176                 :            :         struct inode *inode = file_inode(filp);
    2177                 :            : 
    2178         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
    2179                 :            :                 return -EOPNOTSUPP;
    2180                 :            : 
    2181                 :            :         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    2182                 :            : 
    2183                 :          0 :         return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
    2184                 :            : }
    2185                 :            : 
    2186                 :          0 : static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
    2187                 :            : {
    2188         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
    2189                 :            :                 return -EOPNOTSUPP;
    2190                 :          0 :         return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
    2191                 :            : }
    2192                 :            : 
    2193                 :          0 : static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
    2194                 :            : {
    2195                 :            :         struct inode *inode = file_inode(filp);
    2196                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2197                 :            :         int err;
    2198                 :            : 
    2199         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(sbi))
    2200                 :            :                 return -EOPNOTSUPP;
    2201                 :            : 
    2202                 :          0 :         err = mnt_want_write_file(filp);
    2203         [ #  # ]:          0 :         if (err)
    2204                 :            :                 return err;
    2205                 :            : 
    2206                 :          0 :         down_write(&sbi->sb_lock);
    2207                 :            : 
    2208         [ #  # ]:          0 :         if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
    2209                 :            :                 goto got_it;
    2210                 :            : 
    2211                 :            :         /* update superblock with uuid */
    2212                 :          0 :         generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
    2213                 :            : 
    2214                 :          0 :         err = f2fs_commit_super(sbi, false);
    2215         [ #  # ]:          0 :         if (err) {
    2216                 :            :                 /* undo new data */
    2217                 :          0 :                 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
    2218                 :          0 :                 goto out_err;
    2219                 :            :         }
    2220                 :            : got_it:
    2221         [ #  # ]:          0 :         if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
    2222                 :            :                                                                         16))
    2223                 :            :                 err = -EFAULT;
    2224                 :            : out_err:
    2225                 :          0 :         up_write(&sbi->sb_lock);
    2226                 :          0 :         mnt_drop_write_file(filp);
    2227                 :          0 :         return err;
    2228                 :            : }
    2229                 :            : 
    2230                 :          0 : static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
    2231                 :            :                                              unsigned long arg)
    2232                 :            : {
    2233         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
    2234                 :            :                 return -EOPNOTSUPP;
    2235                 :            : 
    2236                 :          0 :         return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
    2237                 :            : }
    2238                 :            : 
    2239                 :          0 : static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
    2240                 :            : {
    2241         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
    2242                 :            :                 return -EOPNOTSUPP;
    2243                 :            : 
    2244                 :          0 :         return fscrypt_ioctl_add_key(filp, (void __user *)arg);
    2245                 :            : }
    2246                 :            : 
    2247                 :          0 : static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
    2248                 :            : {
    2249         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
    2250                 :            :                 return -EOPNOTSUPP;
    2251                 :            : 
    2252                 :          0 :         return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
    2253                 :            : }
    2254                 :            : 
    2255                 :          0 : static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
    2256                 :            :                                                     unsigned long arg)
    2257                 :            : {
    2258         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
    2259                 :            :                 return -EOPNOTSUPP;
    2260                 :            : 
    2261                 :          0 :         return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
    2262                 :            : }
    2263                 :            : 
    2264                 :          0 : static int f2fs_ioc_get_encryption_key_status(struct file *filp,
    2265                 :            :                                               unsigned long arg)
    2266                 :            : {
    2267         [ #  # ]:          0 :         if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
    2268                 :            :                 return -EOPNOTSUPP;
    2269                 :            : 
    2270                 :          0 :         return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
    2271                 :            : }
    2272                 :            : 
    2273                 :          0 : static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
    2274                 :            : {
    2275                 :            :         struct inode *inode = file_inode(filp);
    2276                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2277                 :            :         __u32 sync;
    2278                 :            :         int ret;
    2279                 :            : 
    2280         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2281                 :            :                 return -EPERM;
    2282                 :            : 
    2283         [ #  # ]:          0 :         if (get_user(sync, (__u32 __user *)arg))
    2284                 :            :                 return -EFAULT;
    2285                 :            : 
    2286         [ #  # ]:          0 :         if (f2fs_readonly(sbi->sb))
    2287                 :            :                 return -EROFS;
    2288                 :            : 
    2289                 :          0 :         ret = mnt_want_write_file(filp);
    2290         [ #  # ]:          0 :         if (ret)
    2291                 :            :                 return ret;
    2292                 :            : 
    2293         [ #  # ]:          0 :         if (!sync) {
    2294         [ #  # ]:          0 :                 if (!mutex_trylock(&sbi->gc_mutex)) {
    2295                 :            :                         ret = -EBUSY;
    2296                 :            :                         goto out;
    2297                 :            :                 }
    2298                 :            :         } else {
    2299                 :          0 :                 mutex_lock(&sbi->gc_mutex);
    2300                 :            :         }
    2301                 :            : 
    2302                 :          0 :         ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
    2303                 :            : out:
    2304                 :          0 :         mnt_drop_write_file(filp);
    2305                 :          0 :         return ret;
    2306                 :            : }
    2307                 :            : 
    2308                 :          0 : static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
    2309                 :            : {
    2310                 :            :         struct inode *inode = file_inode(filp);
    2311                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2312                 :            :         struct f2fs_gc_range range;
    2313                 :            :         u64 end;
    2314                 :            :         int ret;
    2315                 :            : 
    2316         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2317                 :            :                 return -EPERM;
    2318                 :            : 
    2319         [ #  # ]:          0 :         if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
    2320                 :            :                                                         sizeof(range)))
    2321                 :            :                 return -EFAULT;
    2322                 :            : 
    2323         [ #  # ]:          0 :         if (f2fs_readonly(sbi->sb))
    2324                 :            :                 return -EROFS;
    2325                 :            : 
    2326                 :          0 :         end = range.start + range.len;
    2327   [ #  #  #  #  :          0 :         if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
             #  #  #  # ]
    2328   [ #  #  #  # ]:          0 :                                         end >= MAX_BLKADDR(sbi))
    2329                 :            :                 return -EINVAL;
    2330                 :            : 
    2331                 :          0 :         ret = mnt_want_write_file(filp);
    2332         [ #  # ]:          0 :         if (ret)
    2333                 :            :                 return ret;
    2334                 :            : 
    2335                 :            : do_more:
    2336         [ #  # ]:          0 :         if (!range.sync) {
    2337         [ #  # ]:          0 :                 if (!mutex_trylock(&sbi->gc_mutex)) {
    2338                 :            :                         ret = -EBUSY;
    2339                 :            :                         goto out;
    2340                 :            :                 }
    2341                 :            :         } else {
    2342                 :          0 :                 mutex_lock(&sbi->gc_mutex);
    2343                 :            :         }
    2344                 :            : 
    2345   [ #  #  #  # ]:          0 :         ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
    2346                 :          0 :         range.start += BLKS_PER_SEC(sbi);
    2347         [ #  # ]:          0 :         if (range.start <= end)
    2348                 :            :                 goto do_more;
    2349                 :            : out:
    2350                 :          0 :         mnt_drop_write_file(filp);
    2351                 :          0 :         return ret;
    2352                 :            : }
    2353                 :            : 
    2354                 :          0 : static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
    2355                 :            : {
    2356                 :            :         struct inode *inode = file_inode(filp);
    2357                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2358                 :            :         int ret;
    2359                 :            : 
    2360         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2361                 :            :                 return -EPERM;
    2362                 :            : 
    2363         [ #  # ]:          0 :         if (f2fs_readonly(sbi->sb))
    2364                 :            :                 return -EROFS;
    2365                 :            : 
    2366         [ #  # ]:          0 :         if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
    2367                 :          0 :                 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
    2368                 :          0 :                 return -EINVAL;
    2369                 :            :         }
    2370                 :            : 
    2371                 :          0 :         ret = mnt_want_write_file(filp);
    2372         [ #  # ]:          0 :         if (ret)
    2373                 :            :                 return ret;
    2374                 :            : 
    2375                 :          0 :         ret = f2fs_sync_fs(sbi->sb, 1);
    2376                 :            : 
    2377                 :          0 :         mnt_drop_write_file(filp);
    2378                 :          0 :         return ret;
    2379                 :            : }
    2380                 :            : 
    2381                 :          0 : static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
    2382                 :            :                                         struct file *filp,
    2383                 :            :                                         struct f2fs_defragment *range)
    2384                 :            : {
    2385                 :            :         struct inode *inode = file_inode(filp);
    2386                 :          0 :         struct f2fs_map_blocks map = { .m_next_extent = NULL,
    2387                 :            :                                         .m_seg_type = NO_CHECK_TYPE ,
    2388                 :            :                                         .m_may_create = false };
    2389                 :          0 :         struct extent_info ei = {0, 0, 0};
    2390                 :            :         pgoff_t pg_start, pg_end, next_pgofs;
    2391                 :          0 :         unsigned int blk_per_seg = sbi->blocks_per_seg;
    2392                 :            :         unsigned int total = 0, sec_num;
    2393                 :            :         block_t blk_end = 0;
    2394                 :            :         bool fragmented = false;
    2395                 :            :         int err;
    2396                 :            : 
    2397                 :            :         /* if in-place-update policy is enabled, don't waste time here */
    2398         [ #  # ]:          0 :         if (f2fs_should_update_inplace(inode, NULL))
    2399                 :            :                 return -EINVAL;
    2400                 :            : 
    2401                 :          0 :         pg_start = range->start >> PAGE_SHIFT;
    2402                 :          0 :         pg_end = (range->start + range->len) >> PAGE_SHIFT;
    2403                 :            : 
    2404                 :          0 :         f2fs_balance_fs(sbi, true);
    2405                 :            : 
    2406                 :            :         inode_lock(inode);
    2407                 :            : 
    2408                 :            :         /* writeback all dirty pages in the range */
    2409                 :          0 :         err = filemap_write_and_wait_range(inode->i_mapping, range->start,
    2410                 :          0 :                                                 range->start + range->len - 1);
    2411         [ #  # ]:          0 :         if (err)
    2412                 :            :                 goto out;
    2413                 :            : 
    2414                 :            :         /*
    2415                 :            :          * lookup mapping info in extent cache, skip defragmenting if physical
    2416                 :            :          * block addresses are continuous.
    2417                 :            :          */
    2418         [ #  # ]:          0 :         if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
    2419         [ #  # ]:          0 :                 if (ei.fofs + ei.len >= pg_end)
    2420                 :            :                         goto out;
    2421                 :            :         }
    2422                 :            : 
    2423                 :          0 :         map.m_lblk = pg_start;
    2424                 :          0 :         map.m_next_pgofs = &next_pgofs;
    2425                 :            : 
    2426                 :            :         /*
    2427                 :            :          * lookup mapping info in dnode page cache, skip defragmenting if all
    2428                 :            :          * physical block addresses are continuous even if there are hole(s)
    2429                 :            :          * in logical blocks.
    2430                 :            :          */
    2431         [ #  # ]:          0 :         while (map.m_lblk < pg_end) {
    2432                 :          0 :                 map.m_len = pg_end - map.m_lblk;
    2433                 :          0 :                 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
    2434         [ #  # ]:          0 :                 if (err)
    2435                 :            :                         goto out;
    2436                 :            : 
    2437         [ #  # ]:          0 :                 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
    2438                 :          0 :                         map.m_lblk = next_pgofs;
    2439                 :          0 :                         continue;
    2440                 :            :                 }
    2441                 :            : 
    2442   [ #  #  #  # ]:          0 :                 if (blk_end && blk_end != map.m_pblk)
    2443                 :            :                         fragmented = true;
    2444                 :            : 
    2445                 :            :                 /* record total count of block that we're going to move */
    2446                 :          0 :                 total += map.m_len;
    2447                 :            : 
    2448                 :          0 :                 blk_end = map.m_pblk + map.m_len;
    2449                 :            : 
    2450                 :          0 :                 map.m_lblk += map.m_len;
    2451                 :            :         }
    2452                 :            : 
    2453         [ #  # ]:          0 :         if (!fragmented) {
    2454                 :            :                 total = 0;
    2455                 :            :                 goto out;
    2456                 :            :         }
    2457                 :            : 
    2458                 :          0 :         sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
    2459                 :            : 
    2460                 :            :         /*
    2461                 :            :          * make sure there are enough free section for LFS allocation, this can
    2462                 :            :          * avoid defragment running in SSR mode when free section are allocated
    2463                 :            :          * intensively
    2464                 :            :          */
    2465         [ #  # ]:          0 :         if (has_not_enough_free_secs(sbi, 0, sec_num)) {
    2466                 :            :                 err = -EAGAIN;
    2467                 :            :                 goto out;
    2468                 :            :         }
    2469                 :            : 
    2470                 :          0 :         map.m_lblk = pg_start;
    2471                 :          0 :         map.m_len = pg_end - pg_start;
    2472                 :            :         total = 0;
    2473                 :            : 
    2474         [ #  # ]:          0 :         while (map.m_lblk < pg_end) {
    2475                 :            :                 pgoff_t idx;
    2476                 :            :                 int cnt = 0;
    2477                 :            : 
    2478                 :            : do_map:
    2479                 :          0 :                 map.m_len = pg_end - map.m_lblk;
    2480                 :          0 :                 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
    2481         [ #  # ]:          0 :                 if (err)
    2482                 :            :                         goto clear_out;
    2483                 :            : 
    2484         [ #  # ]:          0 :                 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
    2485                 :          0 :                         map.m_lblk = next_pgofs;
    2486                 :          0 :                         goto check;
    2487                 :            :                 }
    2488                 :            : 
    2489                 :          0 :                 set_inode_flag(inode, FI_DO_DEFRAG);
    2490                 :            : 
    2491                 :          0 :                 idx = map.m_lblk;
    2492   [ #  #  #  # ]:          0 :                 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
    2493                 :            :                         struct page *page;
    2494                 :            : 
    2495                 :          0 :                         page = f2fs_get_lock_data_page(inode, idx, true);
    2496         [ #  # ]:          0 :                         if (IS_ERR(page)) {
    2497                 :            :                                 err = PTR_ERR(page);
    2498                 :          0 :                                 goto clear_out;
    2499                 :            :                         }
    2500                 :            : 
    2501                 :          0 :                         set_page_dirty(page);
    2502                 :          0 :                         f2fs_put_page(page, 1);
    2503                 :            : 
    2504                 :          0 :                         idx++;
    2505                 :          0 :                         cnt++;
    2506                 :          0 :                         total++;
    2507                 :            :                 }
    2508                 :            : 
    2509                 :          0 :                 map.m_lblk = idx;
    2510                 :            : check:
    2511   [ #  #  #  # ]:          0 :                 if (map.m_lblk < pg_end && cnt < blk_per_seg)
    2512                 :            :                         goto do_map;
    2513                 :            : 
    2514                 :          0 :                 clear_inode_flag(inode, FI_DO_DEFRAG);
    2515                 :            : 
    2516                 :          0 :                 err = filemap_fdatawrite(inode->i_mapping);
    2517         [ #  # ]:          0 :                 if (err)
    2518                 :            :                         goto out;
    2519                 :            :         }
    2520                 :            : clear_out:
    2521                 :          0 :         clear_inode_flag(inode, FI_DO_DEFRAG);
    2522                 :            : out:
    2523                 :            :         inode_unlock(inode);
    2524         [ #  # ]:          0 :         if (!err)
    2525                 :          0 :                 range->len = (u64)total << PAGE_SHIFT;
    2526                 :          0 :         return err;
    2527                 :            : }
    2528                 :            : 
    2529                 :          0 : static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
    2530                 :            : {
    2531                 :            :         struct inode *inode = file_inode(filp);
    2532                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2533                 :            :         struct f2fs_defragment range;
    2534                 :            :         int err;
    2535                 :            : 
    2536         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2537                 :            :                 return -EPERM;
    2538                 :            : 
    2539   [ #  #  #  # ]:          0 :         if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
    2540                 :            :                 return -EINVAL;
    2541                 :            : 
    2542         [ #  # ]:          0 :         if (f2fs_readonly(sbi->sb))
    2543                 :            :                 return -EROFS;
    2544                 :            : 
    2545         [ #  # ]:          0 :         if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
    2546                 :            :                                                         sizeof(range)))
    2547                 :            :                 return -EFAULT;
    2548                 :            : 
    2549                 :            :         /* verify alignment of offset & size */
    2550   [ #  #  #  # ]:          0 :         if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
    2551                 :            :                 return -EINVAL;
    2552                 :            : 
    2553         [ #  # ]:          0 :         if (unlikely((range.start + range.len) >> PAGE_SHIFT >
    2554                 :            :                                         sbi->max_file_blocks))
    2555                 :            :                 return -EINVAL;
    2556                 :            : 
    2557                 :          0 :         err = mnt_want_write_file(filp);
    2558         [ #  # ]:          0 :         if (err)
    2559                 :            :                 return err;
    2560                 :            : 
    2561                 :          0 :         err = f2fs_defragment_range(sbi, filp, &range);
    2562                 :          0 :         mnt_drop_write_file(filp);
    2563                 :            : 
    2564                 :            :         f2fs_update_time(sbi, REQ_TIME);
    2565         [ #  # ]:          0 :         if (err < 0)
    2566                 :            :                 return err;
    2567                 :            : 
    2568         [ #  # ]:          0 :         if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
    2569                 :            :                                                         sizeof(range)))
    2570                 :            :                 return -EFAULT;
    2571                 :            : 
    2572                 :          0 :         return 0;
    2573                 :            : }
    2574                 :            : 
    2575                 :          0 : static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
    2576                 :            :                         struct file *file_out, loff_t pos_out, size_t len)
    2577                 :            : {
    2578                 :            :         struct inode *src = file_inode(file_in);
    2579                 :            :         struct inode *dst = file_inode(file_out);
    2580                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(src);
    2581                 :            :         size_t olen = len, dst_max_i_size = 0;
    2582                 :            :         size_t dst_osize;
    2583                 :            :         int ret;
    2584                 :            : 
    2585   [ #  #  #  # ]:          0 :         if (file_in->f_path.mnt != file_out->f_path.mnt ||
    2586                 :          0 :                                 src->i_sb != dst->i_sb)
    2587                 :            :                 return -EXDEV;
    2588                 :            : 
    2589         [ #  # ]:          0 :         if (unlikely(f2fs_readonly(src->i_sb)))
    2590                 :            :                 return -EROFS;
    2591                 :            : 
    2592   [ #  #  #  # ]:          0 :         if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
    2593                 :            :                 return -EINVAL;
    2594                 :            : 
    2595   [ #  #  #  # ]:          0 :         if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
    2596                 :            :                 return -EOPNOTSUPP;
    2597                 :            : 
    2598         [ #  # ]:          0 :         if (src == dst) {
    2599         [ #  # ]:          0 :                 if (pos_in == pos_out)
    2600                 :            :                         return 0;
    2601   [ #  #  #  # ]:          0 :                 if (pos_out > pos_in && pos_out < pos_in + len)
    2602                 :            :                         return -EINVAL;
    2603                 :            :         }
    2604                 :            : 
    2605                 :            :         inode_lock(src);
    2606         [ #  # ]:          0 :         if (src != dst) {
    2607                 :            :                 ret = -EBUSY;
    2608         [ #  # ]:          0 :                 if (!inode_trylock(dst))
    2609                 :            :                         goto out;
    2610                 :            :         }
    2611                 :            : 
    2612                 :            :         ret = -EINVAL;
    2613   [ #  #  #  # ]:          0 :         if (pos_in + len > src->i_size || pos_in + len < pos_in)
    2614                 :            :                 goto out_unlock;
    2615         [ #  # ]:          0 :         if (len == 0)
    2616                 :          0 :                 olen = len = src->i_size - pos_in;
    2617         [ #  # ]:          0 :         if (pos_in + len == src->i_size)
    2618                 :          0 :                 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
    2619         [ #  # ]:          0 :         if (len == 0) {
    2620                 :            :                 ret = 0;
    2621                 :            :                 goto out_unlock;
    2622                 :            :         }
    2623                 :            : 
    2624                 :          0 :         dst_osize = dst->i_size;
    2625         [ #  # ]:          0 :         if (pos_out + olen > dst->i_size)
    2626                 :          0 :                 dst_max_i_size = pos_out + olen;
    2627                 :            : 
    2628                 :            :         /* verify the end result is block aligned */
    2629   [ #  #  #  # ]:          0 :         if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
    2630         [ #  # ]:          0 :                         !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
    2631                 :          0 :                         !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
    2632                 :            :                 goto out_unlock;
    2633                 :            : 
    2634                 :          0 :         ret = f2fs_convert_inline_inode(src);
    2635         [ #  # ]:          0 :         if (ret)
    2636                 :            :                 goto out_unlock;
    2637                 :            : 
    2638                 :          0 :         ret = f2fs_convert_inline_inode(dst);
    2639         [ #  # ]:          0 :         if (ret)
    2640                 :            :                 goto out_unlock;
    2641                 :            : 
    2642                 :            :         /* write out all dirty pages from offset */
    2643                 :          0 :         ret = filemap_write_and_wait_range(src->i_mapping,
    2644                 :            :                                         pos_in, pos_in + len);
    2645         [ #  # ]:          0 :         if (ret)
    2646                 :            :                 goto out_unlock;
    2647                 :            : 
    2648                 :          0 :         ret = filemap_write_and_wait_range(dst->i_mapping,
    2649                 :            :                                         pos_out, pos_out + len);
    2650         [ #  # ]:          0 :         if (ret)
    2651                 :            :                 goto out_unlock;
    2652                 :            : 
    2653                 :          0 :         f2fs_balance_fs(sbi, true);
    2654                 :            : 
    2655                 :          0 :         down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
    2656         [ #  # ]:          0 :         if (src != dst) {
    2657                 :            :                 ret = -EBUSY;
    2658         [ #  # ]:          0 :                 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
    2659                 :            :                         goto out_src;
    2660                 :            :         }
    2661                 :            : 
    2662                 :            :         f2fs_lock_op(sbi);
    2663                 :          0 :         ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
    2664                 :          0 :                                 pos_out >> F2FS_BLKSIZE_BITS,
    2665                 :          0 :                                 len >> F2FS_BLKSIZE_BITS, false);
    2666                 :            : 
    2667         [ #  # ]:          0 :         if (!ret) {
    2668         [ #  # ]:          0 :                 if (dst_max_i_size)
    2669                 :          0 :                         f2fs_i_size_write(dst, dst_max_i_size);
    2670         [ #  # ]:          0 :                 else if (dst_osize != dst->i_size)
    2671                 :          0 :                         f2fs_i_size_write(dst, dst_osize);
    2672                 :            :         }
    2673                 :            :         f2fs_unlock_op(sbi);
    2674                 :            : 
    2675         [ #  # ]:          0 :         if (src != dst)
    2676                 :          0 :                 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
    2677                 :            : out_src:
    2678                 :          0 :         up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
    2679                 :            : out_unlock:
    2680         [ #  # ]:          0 :         if (src != dst)
    2681                 :            :                 inode_unlock(dst);
    2682                 :            : out:
    2683                 :            :         inode_unlock(src);
    2684                 :          0 :         return ret;
    2685                 :            : }
    2686                 :            : 
    2687                 :          0 : static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
    2688                 :            : {
    2689                 :            :         struct f2fs_move_range range;
    2690                 :            :         struct fd dst;
    2691                 :            :         int err;
    2692                 :            : 
    2693         [ #  # ]:          0 :         if (!(filp->f_mode & FMODE_READ) ||
    2694                 :            :                         !(filp->f_mode & FMODE_WRITE))
    2695                 :            :                 return -EBADF;
    2696                 :            : 
    2697         [ #  # ]:          0 :         if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
    2698                 :            :                                                         sizeof(range)))
    2699                 :            :                 return -EFAULT;
    2700                 :            : 
    2701                 :          0 :         dst = fdget(range.dst_fd);
    2702         [ #  # ]:          0 :         if (!dst.file)
    2703                 :            :                 return -EBADF;
    2704                 :            : 
    2705         [ #  # ]:          0 :         if (!(dst.file->f_mode & FMODE_WRITE)) {
    2706                 :            :                 err = -EBADF;
    2707                 :            :                 goto err_out;
    2708                 :            :         }
    2709                 :            : 
    2710                 :          0 :         err = mnt_want_write_file(filp);
    2711         [ #  # ]:          0 :         if (err)
    2712                 :            :                 goto err_out;
    2713                 :            : 
    2714                 :          0 :         err = f2fs_move_file_range(filp, range.pos_in, dst.file,
    2715                 :          0 :                                         range.pos_out, range.len);
    2716                 :            : 
    2717                 :          0 :         mnt_drop_write_file(filp);
    2718         [ #  # ]:          0 :         if (err)
    2719                 :            :                 goto err_out;
    2720                 :            : 
    2721         [ #  # ]:          0 :         if (copy_to_user((struct f2fs_move_range __user *)arg,
    2722                 :            :                                                 &range, sizeof(range)))
    2723                 :            :                 err = -EFAULT;
    2724                 :            : err_out:
    2725                 :            :         fdput(dst);
    2726                 :          0 :         return err;
    2727                 :            : }
    2728                 :            : 
    2729                 :          0 : static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
    2730                 :            : {
    2731                 :            :         struct inode *inode = file_inode(filp);
    2732                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2733                 :            :         struct sit_info *sm = SIT_I(sbi);
    2734                 :            :         unsigned int start_segno = 0, end_segno = 0;
    2735                 :            :         unsigned int dev_start_segno = 0, dev_end_segno = 0;
    2736                 :            :         struct f2fs_flush_device range;
    2737                 :            :         int ret;
    2738                 :            : 
    2739         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2740                 :            :                 return -EPERM;
    2741                 :            : 
    2742         [ #  # ]:          0 :         if (f2fs_readonly(sbi->sb))
    2743                 :            :                 return -EROFS;
    2744                 :            : 
    2745         [ #  # ]:          0 :         if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
    2746                 :            :                 return -EINVAL;
    2747                 :            : 
    2748         [ #  # ]:          0 :         if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
    2749                 :            :                                                         sizeof(range)))
    2750                 :            :                 return -EFAULT;
    2751                 :            : 
    2752   [ #  #  #  #  :          0 :         if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
                   #  # ]
    2753                 :          0 :                         __is_large_section(sbi)) {
    2754                 :          0 :                 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
    2755                 :            :                           range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
    2756                 :          0 :                 return -EINVAL;
    2757                 :            :         }
    2758                 :            : 
    2759                 :          0 :         ret = mnt_want_write_file(filp);
    2760         [ #  # ]:          0 :         if (ret)
    2761                 :            :                 return ret;
    2762                 :            : 
    2763         [ #  # ]:          0 :         if (range.dev_num != 0)
    2764   [ #  #  #  # ]:          0 :                 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
    2765   [ #  #  #  # ]:          0 :         dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
    2766                 :            : 
    2767                 :          0 :         start_segno = sm->last_victim[FLUSH_DEVICE];
    2768         [ #  # ]:          0 :         if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
    2769                 :            :                 start_segno = dev_start_segno;
    2770                 :          0 :         end_segno = min(start_segno + range.segments, dev_end_segno);
    2771                 :            : 
    2772         [ #  # ]:          0 :         while (start_segno < end_segno) {
    2773         [ #  # ]:          0 :                 if (!mutex_trylock(&sbi->gc_mutex)) {
    2774                 :            :                         ret = -EBUSY;
    2775                 :            :                         goto out;
    2776                 :            :                 }
    2777                 :          0 :                 sm->last_victim[GC_CB] = end_segno + 1;
    2778                 :          0 :                 sm->last_victim[GC_GREEDY] = end_segno + 1;
    2779                 :          0 :                 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
    2780                 :          0 :                 ret = f2fs_gc(sbi, true, true, start_segno);
    2781         [ #  # ]:          0 :                 if (ret == -EAGAIN)
    2782                 :            :                         ret = 0;
    2783         [ #  # ]:          0 :                 else if (ret < 0)
    2784                 :            :                         break;
    2785                 :          0 :                 start_segno++;
    2786                 :            :         }
    2787                 :            : out:
    2788                 :          0 :         mnt_drop_write_file(filp);
    2789                 :          0 :         return ret;
    2790                 :            : }
    2791                 :            : 
    2792                 :          0 : static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
    2793                 :            : {
    2794                 :            :         struct inode *inode = file_inode(filp);
    2795                 :          0 :         u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
    2796                 :            : 
    2797                 :            :         /* Must validate to set it with SQLite behavior in Android. */
    2798                 :          0 :         sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
    2799                 :            : 
    2800                 :          0 :         return put_user(sb_feature, (u32 __user *)arg);
    2801                 :            : }
    2802                 :            : 
    2803                 :            : #ifdef CONFIG_QUOTA
    2804                 :          0 : int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
    2805                 :            : {
    2806                 :          0 :         struct dquot *transfer_to[MAXQUOTAS] = {};
    2807                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2808                 :          0 :         struct super_block *sb = sbi->sb;
    2809                 :            :         int err = 0;
    2810                 :            : 
    2811                 :          0 :         transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
    2812         [ #  # ]:          0 :         if (!IS_ERR(transfer_to[PRJQUOTA])) {
    2813                 :          0 :                 err = __dquot_transfer(inode, transfer_to);
    2814         [ #  # ]:          0 :                 if (err)
    2815                 :            :                         set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
    2816                 :          0 :                 dqput(transfer_to[PRJQUOTA]);
    2817                 :            :         }
    2818                 :          0 :         return err;
    2819                 :            : }
    2820                 :            : 
    2821                 :          0 : static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
    2822                 :            : {
    2823                 :            :         struct inode *inode = file_inode(filp);
    2824                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    2825                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    2826                 :            :         struct page *ipage;
    2827                 :            :         kprojid_t kprojid;
    2828                 :            :         int err;
    2829                 :            : 
    2830         [ #  # ]:          0 :         if (!f2fs_sb_has_project_quota(sbi)) {
    2831         [ #  # ]:          0 :                 if (projid != F2FS_DEF_PROJID)
    2832                 :            :                         return -EOPNOTSUPP;
    2833                 :            :                 else
    2834                 :          0 :                         return 0;
    2835                 :            :         }
    2836                 :            : 
    2837         [ #  # ]:          0 :         if (!f2fs_has_extra_attr(inode))
    2838                 :            :                 return -EOPNOTSUPP;
    2839                 :            : 
    2840                 :          0 :         kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
    2841                 :            : 
    2842         [ #  # ]:          0 :         if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
    2843                 :            :                 return 0;
    2844                 :            : 
    2845                 :            :         err = -EPERM;
    2846                 :            :         /* Is it quota file? Do not allow user to mess with it */
    2847         [ #  # ]:          0 :         if (IS_NOQUOTA(inode))
    2848                 :            :                 return err;
    2849                 :            : 
    2850                 :          0 :         ipage = f2fs_get_node_page(sbi, inode->i_ino);
    2851         [ #  # ]:          0 :         if (IS_ERR(ipage))
    2852                 :          0 :                 return PTR_ERR(ipage);
    2853                 :            : 
    2854         [ #  # ]:          0 :         if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
    2855                 :            :                                                                 i_projid)) {
    2856                 :            :                 err = -EOVERFLOW;
    2857                 :          0 :                 f2fs_put_page(ipage, 1);
    2858                 :          0 :                 return err;
    2859                 :            :         }
    2860                 :          0 :         f2fs_put_page(ipage, 1);
    2861                 :            : 
    2862                 :          0 :         err = dquot_initialize(inode);
    2863         [ #  # ]:          0 :         if (err)
    2864                 :            :                 return err;
    2865                 :            : 
    2866                 :            :         f2fs_lock_op(sbi);
    2867                 :          0 :         err = f2fs_transfer_project_quota(inode, kprojid);
    2868         [ #  # ]:          0 :         if (err)
    2869                 :            :                 goto out_unlock;
    2870                 :            : 
    2871                 :          0 :         F2FS_I(inode)->i_projid = kprojid;
    2872                 :          0 :         inode->i_ctime = current_time(inode);
    2873                 :          0 :         f2fs_mark_inode_dirty_sync(inode, true);
    2874                 :            : out_unlock:
    2875                 :            :         f2fs_unlock_op(sbi);
    2876                 :          0 :         return err;
    2877                 :            : }
    2878                 :            : #else
    2879                 :            : int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
    2880                 :            : {
    2881                 :            :         return 0;
    2882                 :            : }
    2883                 :            : 
    2884                 :            : static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
    2885                 :            : {
    2886                 :            :         if (projid != F2FS_DEF_PROJID)
    2887                 :            :                 return -EOPNOTSUPP;
    2888                 :            :         return 0;
    2889                 :            : }
    2890                 :            : #endif
    2891                 :            : 
    2892                 :            : /* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
    2893                 :            : 
    2894                 :            : /*
    2895                 :            :  * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
    2896                 :            :  * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
    2897                 :            :  * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
    2898                 :            :  */
    2899                 :            : 
    2900                 :            : static const struct {
    2901                 :            :         u32 iflag;
    2902                 :            :         u32 xflag;
    2903                 :            : } f2fs_xflags_map[] = {
    2904                 :            :         { F2FS_SYNC_FL,         FS_XFLAG_SYNC },
    2905                 :            :         { F2FS_IMMUTABLE_FL,    FS_XFLAG_IMMUTABLE },
    2906                 :            :         { F2FS_APPEND_FL,       FS_XFLAG_APPEND },
    2907                 :            :         { F2FS_NODUMP_FL,       FS_XFLAG_NODUMP },
    2908                 :            :         { F2FS_NOATIME_FL,      FS_XFLAG_NOATIME },
    2909                 :            :         { F2FS_PROJINHERIT_FL,  FS_XFLAG_PROJINHERIT },
    2910                 :            : };
    2911                 :            : 
    2912                 :            : #define F2FS_SUPPORTED_XFLAGS (         \
    2913                 :            :                 FS_XFLAG_SYNC |         \
    2914                 :            :                 FS_XFLAG_IMMUTABLE |    \
    2915                 :            :                 FS_XFLAG_APPEND |       \
    2916                 :            :                 FS_XFLAG_NODUMP |       \
    2917                 :            :                 FS_XFLAG_NOATIME |      \
    2918                 :            :                 FS_XFLAG_PROJINHERIT)
    2919                 :            : 
    2920                 :            : /* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
    2921                 :            : static inline u32 f2fs_iflags_to_xflags(u32 iflags)
    2922                 :            : {
    2923                 :            :         u32 xflags = 0;
    2924                 :            :         int i;
    2925                 :            : 
    2926         [ #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
    2927         [ #  # ]:          0 :                 if (iflags & f2fs_xflags_map[i].iflag)
    2928                 :          0 :                         xflags |= f2fs_xflags_map[i].xflag;
    2929                 :            : 
    2930                 :          0 :         return xflags;
    2931                 :            : }
    2932                 :            : 
    2933                 :            : /* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
    2934                 :            : static inline u32 f2fs_xflags_to_iflags(u32 xflags)
    2935                 :            : {
    2936                 :            :         u32 iflags = 0;
    2937                 :            :         int i;
    2938                 :            : 
    2939   [ #  #  #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
    2940   [ #  #  #  # ]:          0 :                 if (xflags & f2fs_xflags_map[i].xflag)
    2941                 :          0 :                         iflags |= f2fs_xflags_map[i].iflag;
    2942                 :            : 
    2943                 :          0 :         return iflags;
    2944                 :            : }
    2945                 :            : 
    2946                 :          0 : static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
    2947                 :            : {
    2948                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    2949                 :            : 
    2950                 :          0 :         simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
    2951                 :            : 
    2952         [ #  # ]:          0 :         if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
    2953                 :          0 :                 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
    2954                 :          0 : }
    2955                 :            : 
    2956                 :          0 : static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
    2957                 :            : {
    2958                 :            :         struct inode *inode = file_inode(filp);
    2959                 :            :         struct fsxattr fa;
    2960                 :            : 
    2961                 :          0 :         f2fs_fill_fsxattr(inode, &fa);
    2962                 :            : 
    2963         [ #  # ]:          0 :         if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
    2964                 :            :                 return -EFAULT;
    2965                 :          0 :         return 0;
    2966                 :            : }
    2967                 :            : 
    2968                 :          0 : static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
    2969                 :            : {
    2970                 :            :         struct inode *inode = file_inode(filp);
    2971                 :            :         struct fsxattr fa, old_fa;
    2972                 :            :         u32 iflags;
    2973                 :            :         int err;
    2974                 :            : 
    2975         [ #  # ]:          0 :         if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
    2976                 :            :                 return -EFAULT;
    2977                 :            : 
    2978                 :            :         /* Make sure caller has proper permission */
    2979         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    2980                 :            :                 return -EACCES;
    2981                 :            : 
    2982         [ #  # ]:          0 :         if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
    2983                 :            :                 return -EOPNOTSUPP;
    2984                 :            : 
    2985                 :            :         iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
    2986         [ #  # ]:          0 :         if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
    2987                 :            :                 return -EOPNOTSUPP;
    2988                 :            : 
    2989                 :          0 :         err = mnt_want_write_file(filp);
    2990         [ #  # ]:          0 :         if (err)
    2991                 :            :                 return err;
    2992                 :            : 
    2993                 :            :         inode_lock(inode);
    2994                 :            : 
    2995                 :          0 :         f2fs_fill_fsxattr(inode, &old_fa);
    2996                 :          0 :         err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
    2997         [ #  # ]:          0 :         if (err)
    2998                 :            :                 goto out;
    2999                 :            : 
    3000                 :          0 :         err = f2fs_setflags_common(inode, iflags,
    3001                 :            :                         f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
    3002         [ #  # ]:          0 :         if (err)
    3003                 :            :                 goto out;
    3004                 :            : 
    3005                 :          0 :         err = f2fs_ioc_setproject(filp, fa.fsx_projid);
    3006                 :            : out:
    3007                 :            :         inode_unlock(inode);
    3008                 :          0 :         mnt_drop_write_file(filp);
    3009                 :          0 :         return err;
    3010                 :            : }
    3011                 :            : 
    3012                 :          0 : int f2fs_pin_file_control(struct inode *inode, bool inc)
    3013                 :            : {
    3014                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    3015                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    3016                 :            : 
    3017                 :            :         /* Use i_gc_failures for normal file as a risk signal. */
    3018         [ #  # ]:          0 :         if (inc)
    3019                 :          0 :                 f2fs_i_gc_failures_write(inode,
    3020                 :          0 :                                 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
    3021                 :            : 
    3022         [ #  # ]:          0 :         if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
    3023                 :          0 :                 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
    3024                 :            :                           __func__, inode->i_ino,
    3025                 :            :                           fi->i_gc_failures[GC_FAILURE_PIN]);
    3026                 :          0 :                 clear_inode_flag(inode, FI_PIN_FILE);
    3027                 :          0 :                 return -EAGAIN;
    3028                 :            :         }
    3029                 :            :         return 0;
    3030                 :            : }
    3031                 :            : 
    3032                 :          0 : static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
    3033                 :            : {
    3034                 :            :         struct inode *inode = file_inode(filp);
    3035                 :            :         __u32 pin;
    3036                 :            :         int ret = 0;
    3037                 :            : 
    3038         [ #  # ]:          0 :         if (get_user(pin, (__u32 __user *)arg))
    3039                 :            :                 return -EFAULT;
    3040                 :            : 
    3041         [ #  # ]:          0 :         if (!S_ISREG(inode->i_mode))
    3042                 :            :                 return -EINVAL;
    3043                 :            : 
    3044         [ #  # ]:          0 :         if (f2fs_readonly(F2FS_I_SB(inode)->sb))
    3045                 :            :                 return -EROFS;
    3046                 :            : 
    3047                 :          0 :         ret = mnt_want_write_file(filp);
    3048         [ #  # ]:          0 :         if (ret)
    3049                 :            :                 return ret;
    3050                 :            : 
    3051                 :            :         inode_lock(inode);
    3052                 :            : 
    3053         [ #  # ]:          0 :         if (f2fs_should_update_outplace(inode, NULL)) {
    3054                 :            :                 ret = -EINVAL;
    3055                 :            :                 goto out;
    3056                 :            :         }
    3057                 :            : 
    3058         [ #  # ]:          0 :         if (!pin) {
    3059                 :          0 :                 clear_inode_flag(inode, FI_PIN_FILE);
    3060                 :            :                 f2fs_i_gc_failures_write(inode, 0);
    3061                 :            :                 goto done;
    3062                 :            :         }
    3063                 :            : 
    3064         [ #  # ]:          0 :         if (f2fs_pin_file_control(inode, false)) {
    3065                 :            :                 ret = -EAGAIN;
    3066                 :            :                 goto out;
    3067                 :            :         }
    3068                 :          0 :         ret = f2fs_convert_inline_inode(inode);
    3069         [ #  # ]:          0 :         if (ret)
    3070                 :            :                 goto out;
    3071                 :            : 
    3072                 :          0 :         set_inode_flag(inode, FI_PIN_FILE);
    3073                 :          0 :         ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
    3074                 :            : done:
    3075                 :            :         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    3076                 :            : out:
    3077                 :            :         inode_unlock(inode);
    3078                 :          0 :         mnt_drop_write_file(filp);
    3079                 :          0 :         return ret;
    3080                 :            : }
    3081                 :            : 
    3082                 :          0 : static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
    3083                 :            : {
    3084                 :            :         struct inode *inode = file_inode(filp);
    3085                 :            :         __u32 pin = 0;
    3086                 :            : 
    3087         [ #  # ]:          0 :         if (is_inode_flag_set(inode, FI_PIN_FILE))
    3088                 :          0 :                 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
    3089                 :          0 :         return put_user(pin, (u32 __user *)arg);
    3090                 :            : }
    3091                 :            : 
    3092                 :          0 : int f2fs_precache_extents(struct inode *inode)
    3093                 :            : {
    3094                 :            :         struct f2fs_inode_info *fi = F2FS_I(inode);
    3095                 :            :         struct f2fs_map_blocks map;
    3096                 :            :         pgoff_t m_next_extent;
    3097                 :            :         loff_t end;
    3098                 :            :         int err;
    3099                 :            : 
    3100         [ #  # ]:          0 :         if (is_inode_flag_set(inode, FI_NO_EXTENT))
    3101                 :            :                 return -EOPNOTSUPP;
    3102                 :            : 
    3103                 :          0 :         map.m_lblk = 0;
    3104                 :          0 :         map.m_next_pgofs = NULL;
    3105                 :          0 :         map.m_next_extent = &m_next_extent;
    3106                 :          0 :         map.m_seg_type = NO_CHECK_TYPE;
    3107                 :          0 :         map.m_may_create = false;
    3108                 :          0 :         end = F2FS_I_SB(inode)->max_file_blocks;
    3109                 :            : 
    3110         [ #  # ]:          0 :         while (map.m_lblk < end) {
    3111                 :          0 :                 map.m_len = end - map.m_lblk;
    3112                 :            : 
    3113                 :          0 :                 down_write(&fi->i_gc_rwsem[WRITE]);
    3114                 :          0 :                 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
    3115                 :          0 :                 up_write(&fi->i_gc_rwsem[WRITE]);
    3116         [ #  # ]:          0 :                 if (err)
    3117                 :          0 :                         return err;
    3118                 :            : 
    3119                 :          0 :                 map.m_lblk = m_next_extent;
    3120                 :            :         }
    3121                 :            : 
    3122                 :          0 :         return err;
    3123                 :            : }
    3124                 :            : 
    3125                 :            : static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
    3126                 :            : {
    3127                 :          0 :         return f2fs_precache_extents(file_inode(filp));
    3128                 :            : }
    3129                 :            : 
    3130                 :          0 : static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
    3131                 :            : {
    3132                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
    3133                 :            :         __u64 block_count;
    3134                 :            :         int ret;
    3135                 :            : 
    3136         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3137                 :            :                 return -EPERM;
    3138                 :            : 
    3139         [ #  # ]:          0 :         if (f2fs_readonly(sbi->sb))
    3140                 :            :                 return -EROFS;
    3141                 :            : 
    3142         [ #  # ]:          0 :         if (copy_from_user(&block_count, (void __user *)arg,
    3143                 :            :                            sizeof(block_count)))
    3144                 :            :                 return -EFAULT;
    3145                 :            : 
    3146                 :          0 :         ret = f2fs_resize_fs(sbi, block_count);
    3147                 :            : 
    3148                 :          0 :         return ret;
    3149                 :            : }
    3150                 :            : 
    3151                 :          0 : static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
    3152                 :            : {
    3153                 :            :         struct inode *inode = file_inode(filp);
    3154                 :            : 
    3155                 :            :         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
    3156                 :            : 
    3157         [ #  # ]:          0 :         if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
    3158                 :          0 :                 f2fs_warn(F2FS_I_SB(inode),
    3159                 :            :                           "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
    3160                 :            :                           inode->i_ino);
    3161                 :          0 :                 return -EOPNOTSUPP;
    3162                 :            :         }
    3163                 :            : 
    3164                 :            :         return fsverity_ioctl_enable(filp, (const void __user *)arg);
    3165                 :            : }
    3166                 :            : 
    3167                 :            : static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
    3168                 :            : {
    3169                 :            :         if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
    3170                 :            :                 return -EOPNOTSUPP;
    3171                 :            : 
    3172                 :            :         return fsverity_ioctl_measure(filp, (void __user *)arg);
    3173                 :            : }
    3174                 :            : 
    3175                 :          0 : static int f2fs_get_volume_name(struct file *filp, unsigned long arg)
    3176                 :            : {
    3177                 :            :         struct inode *inode = file_inode(filp);
    3178                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    3179                 :            :         char *vbuf;
    3180                 :            :         int count;
    3181                 :            :         int err = 0;
    3182                 :            : 
    3183                 :            :         vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
    3184         [ #  # ]:          0 :         if (!vbuf)
    3185                 :            :                 return -ENOMEM;
    3186                 :            : 
    3187                 :          0 :         down_read(&sbi->sb_lock);
    3188                 :          0 :         count = utf16s_to_utf8s(sbi->raw_super->volume_name,
    3189                 :            :                         ARRAY_SIZE(sbi->raw_super->volume_name),
    3190                 :            :                         UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
    3191                 :          0 :         up_read(&sbi->sb_lock);
    3192                 :            : 
    3193         [ #  # ]:          0 :         if (copy_to_user((char __user *)arg, vbuf,
    3194                 :          0 :                                 min(FSLABEL_MAX, count)))
    3195                 :            :                 err = -EFAULT;
    3196                 :            : 
    3197                 :          0 :         kvfree(vbuf);
    3198                 :          0 :         return err;
    3199                 :            : }
    3200                 :            : 
    3201                 :          0 : static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
    3202                 :            : {
    3203                 :            :         struct inode *inode = file_inode(filp);
    3204                 :            :         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
    3205                 :            :         char *vbuf;
    3206                 :            :         int err = 0;
    3207                 :            : 
    3208         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3209                 :            :                 return -EPERM;
    3210                 :            : 
    3211                 :          0 :         vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
    3212         [ #  # ]:          0 :         if (IS_ERR(vbuf))
    3213                 :          0 :                 return PTR_ERR(vbuf);
    3214                 :            : 
    3215                 :          0 :         err = mnt_want_write_file(filp);
    3216         [ #  # ]:          0 :         if (err)
    3217                 :            :                 goto out;
    3218                 :            : 
    3219                 :          0 :         down_write(&sbi->sb_lock);
    3220                 :            : 
    3221                 :          0 :         memset(sbi->raw_super->volume_name, 0,
    3222                 :            :                         sizeof(sbi->raw_super->volume_name));
    3223                 :          0 :         utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
    3224                 :          0 :                         sbi->raw_super->volume_name,
    3225                 :            :                         ARRAY_SIZE(sbi->raw_super->volume_name));
    3226                 :            : 
    3227                 :          0 :         err = f2fs_commit_super(sbi, false);
    3228                 :            : 
    3229                 :          0 :         up_write(&sbi->sb_lock);
    3230                 :            : 
    3231                 :          0 :         mnt_drop_write_file(filp);
    3232                 :            : out:
    3233                 :          0 :         kfree(vbuf);
    3234                 :          0 :         return err;
    3235                 :            : }
    3236                 :            : 
    3237                 :          0 : long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
    3238                 :            : {
    3239         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
    3240                 :            :                 return -EIO;
    3241         [ #  # ]:          0 :         if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
    3242                 :            :                 return -ENOSPC;
    3243                 :            : 
    3244   [ #  #  #  #  :          0 :         switch (cmd) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    3245                 :            :         case F2FS_IOC_GETFLAGS:
    3246                 :          0 :                 return f2fs_ioc_getflags(filp, arg);
    3247                 :            :         case F2FS_IOC_SETFLAGS:
    3248                 :          0 :                 return f2fs_ioc_setflags(filp, arg);
    3249                 :            :         case F2FS_IOC_GETVERSION:
    3250                 :          0 :                 return f2fs_ioc_getversion(filp, arg);
    3251                 :            :         case F2FS_IOC_START_ATOMIC_WRITE:
    3252                 :          0 :                 return f2fs_ioc_start_atomic_write(filp);
    3253                 :            :         case F2FS_IOC_COMMIT_ATOMIC_WRITE:
    3254                 :          0 :                 return f2fs_ioc_commit_atomic_write(filp);
    3255                 :            :         case F2FS_IOC_START_VOLATILE_WRITE:
    3256                 :          0 :                 return f2fs_ioc_start_volatile_write(filp);
    3257                 :            :         case F2FS_IOC_RELEASE_VOLATILE_WRITE:
    3258                 :          0 :                 return f2fs_ioc_release_volatile_write(filp);
    3259                 :            :         case F2FS_IOC_ABORT_VOLATILE_WRITE:
    3260                 :          0 :                 return f2fs_ioc_abort_volatile_write(filp);
    3261                 :            :         case F2FS_IOC_SHUTDOWN:
    3262                 :          0 :                 return f2fs_ioc_shutdown(filp, arg);
    3263                 :            :         case FITRIM:
    3264                 :          0 :                 return f2fs_ioc_fitrim(filp, arg);
    3265                 :            :         case F2FS_IOC_SET_ENCRYPTION_POLICY:
    3266                 :          0 :                 return f2fs_ioc_set_encryption_policy(filp, arg);
    3267                 :            :         case F2FS_IOC_GET_ENCRYPTION_POLICY:
    3268                 :          0 :                 return f2fs_ioc_get_encryption_policy(filp, arg);
    3269                 :            :         case F2FS_IOC_GET_ENCRYPTION_PWSALT:
    3270                 :          0 :                 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
    3271                 :            :         case FS_IOC_GET_ENCRYPTION_POLICY_EX:
    3272                 :          0 :                 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
    3273                 :            :         case FS_IOC_ADD_ENCRYPTION_KEY:
    3274                 :          0 :                 return f2fs_ioc_add_encryption_key(filp, arg);
    3275                 :            :         case FS_IOC_REMOVE_ENCRYPTION_KEY:
    3276                 :          0 :                 return f2fs_ioc_remove_encryption_key(filp, arg);
    3277                 :            :         case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
    3278                 :          0 :                 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
    3279                 :            :         case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
    3280                 :          0 :                 return f2fs_ioc_get_encryption_key_status(filp, arg);
    3281                 :            :         case F2FS_IOC_GARBAGE_COLLECT:
    3282                 :          0 :                 return f2fs_ioc_gc(filp, arg);
    3283                 :            :         case F2FS_IOC_GARBAGE_COLLECT_RANGE:
    3284                 :          0 :                 return f2fs_ioc_gc_range(filp, arg);
    3285                 :            :         case F2FS_IOC_WRITE_CHECKPOINT:
    3286                 :          0 :                 return f2fs_ioc_write_checkpoint(filp, arg);
    3287                 :            :         case F2FS_IOC_DEFRAGMENT:
    3288                 :          0 :                 return f2fs_ioc_defragment(filp, arg);
    3289                 :            :         case F2FS_IOC_MOVE_RANGE:
    3290                 :          0 :                 return f2fs_ioc_move_range(filp, arg);
    3291                 :            :         case F2FS_IOC_FLUSH_DEVICE:
    3292                 :          0 :                 return f2fs_ioc_flush_device(filp, arg);
    3293                 :            :         case F2FS_IOC_GET_FEATURES:
    3294                 :          0 :                 return f2fs_ioc_get_features(filp, arg);
    3295                 :            :         case F2FS_IOC_FSGETXATTR:
    3296                 :          0 :                 return f2fs_ioc_fsgetxattr(filp, arg);
    3297                 :            :         case F2FS_IOC_FSSETXATTR:
    3298                 :          0 :                 return f2fs_ioc_fssetxattr(filp, arg);
    3299                 :            :         case F2FS_IOC_GET_PIN_FILE:
    3300                 :          0 :                 return f2fs_ioc_get_pin_file(filp, arg);
    3301                 :            :         case F2FS_IOC_SET_PIN_FILE:
    3302                 :          0 :                 return f2fs_ioc_set_pin_file(filp, arg);
    3303                 :            :         case F2FS_IOC_PRECACHE_EXTENTS:
    3304                 :          0 :                 return f2fs_ioc_precache_extents(filp, arg);
    3305                 :            :         case F2FS_IOC_RESIZE_FS:
    3306                 :          0 :                 return f2fs_ioc_resize_fs(filp, arg);
    3307                 :            :         case FS_IOC_ENABLE_VERITY:
    3308                 :          0 :                 return f2fs_ioc_enable_verity(filp, arg);
    3309                 :            :         case FS_IOC_MEASURE_VERITY:
    3310                 :            :                 return f2fs_ioc_measure_verity(filp, arg);
    3311                 :            :         case F2FS_IOC_GET_VOLUME_NAME:
    3312                 :          0 :                 return f2fs_get_volume_name(filp, arg);
    3313                 :            :         case F2FS_IOC_SET_VOLUME_NAME:
    3314                 :          0 :                 return f2fs_set_volume_name(filp, arg);
    3315                 :            :         default:
    3316                 :          0 :                 return -ENOTTY;
    3317                 :            :         }
    3318                 :            : }
    3319                 :            : 
    3320                 :          0 : static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
    3321                 :            : {
    3322                 :          0 :         struct file *file = iocb->ki_filp;
    3323                 :            :         struct inode *inode = file_inode(file);
    3324                 :            :         ssize_t ret;
    3325                 :            : 
    3326         [ #  # ]:          0 :         if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
    3327                 :            :                 ret = -EIO;
    3328                 :            :                 goto out;
    3329                 :            :         }
    3330                 :            : 
    3331         [ #  # ]:          0 :         if (iocb->ki_flags & IOCB_NOWAIT) {
    3332         [ #  # ]:          0 :                 if (!inode_trylock(inode)) {
    3333                 :            :                         ret = -EAGAIN;
    3334                 :            :                         goto out;
    3335                 :            :                 }
    3336                 :            :         } else {
    3337                 :            :                 inode_lock(inode);
    3338                 :            :         }
    3339                 :            : 
    3340                 :          0 :         ret = generic_write_checks(iocb, from);
    3341         [ #  # ]:          0 :         if (ret > 0) {
    3342                 :            :                 bool preallocated = false;
    3343                 :            :                 size_t target_size = 0;
    3344                 :            :                 int err;
    3345                 :            : 
    3346         [ #  # ]:          0 :                 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
    3347                 :          0 :                         set_inode_flag(inode, FI_NO_PREALLOC);
    3348                 :            : 
    3349         [ #  # ]:          0 :                 if ((iocb->ki_flags & IOCB_NOWAIT)) {
    3350         [ #  # ]:          0 :                         if (!f2fs_overwrite_io(inode, iocb->ki_pos,
    3351         [ #  # ]:          0 :                                                 iov_iter_count(from)) ||
    3352         [ #  # ]:          0 :                                 f2fs_has_inline_data(inode) ||
    3353                 :          0 :                                 f2fs_force_buffered_io(inode, iocb, from)) {
    3354                 :          0 :                                 clear_inode_flag(inode, FI_NO_PREALLOC);
    3355                 :            :                                 inode_unlock(inode);
    3356                 :            :                                 ret = -EAGAIN;
    3357                 :          0 :                                 goto out;
    3358                 :            :                         }
    3359                 :            :                         goto write;
    3360                 :            :                 }
    3361                 :            : 
    3362         [ #  # ]:          0 :                 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
    3363                 :            :                         goto write;
    3364                 :            : 
    3365         [ #  # ]:          0 :                 if (iocb->ki_flags & IOCB_DIRECT) {
    3366                 :            :                         /*
    3367                 :            :                          * Convert inline data for Direct I/O before entering
    3368                 :            :                          * f2fs_direct_IO().
    3369                 :            :                          */
    3370                 :          0 :                         err = f2fs_convert_inline_inode(inode);
    3371         [ #  # ]:          0 :                         if (err)
    3372                 :            :                                 goto out_err;
    3373                 :            :                         /*
    3374                 :            :                          * If force_buffere_io() is true, we have to allocate
    3375                 :            :                          * blocks all the time, since f2fs_direct_IO will fall
    3376                 :            :                          * back to buffered IO.
    3377                 :            :                          */
    3378   [ #  #  #  # ]:          0 :                         if (!f2fs_force_buffered_io(inode, iocb, from) &&
    3379                 :          0 :                                         allow_outplace_dio(inode, iocb, from))
    3380                 :            :                                 goto write;
    3381                 :            :                 }
    3382                 :            :                 preallocated = true;
    3383                 :          0 :                 target_size = iocb->ki_pos + iov_iter_count(from);
    3384                 :            : 
    3385                 :          0 :                 err = f2fs_preallocate_blocks(iocb, from);
    3386         [ #  # ]:          0 :                 if (err) {
    3387                 :            : out_err:
    3388                 :          0 :                         clear_inode_flag(inode, FI_NO_PREALLOC);
    3389                 :            :                         inode_unlock(inode);
    3390                 :            :                         ret = err;
    3391                 :          0 :                         goto out;
    3392                 :            :                 }
    3393                 :            : write:
    3394                 :          0 :                 ret = __generic_file_write_iter(iocb, from);
    3395                 :          0 :                 clear_inode_flag(inode, FI_NO_PREALLOC);
    3396                 :            : 
    3397                 :            :                 /* if we couldn't write data, we should deallocate blocks. */
    3398   [ #  #  #  # ]:          0 :                 if (preallocated && i_size_read(inode) < target_size)
    3399                 :          0 :                         f2fs_truncate(inode);
    3400                 :            : 
    3401         [ #  # ]:          0 :                 if (ret > 0)
    3402                 :          0 :                         f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
    3403                 :            :         }
    3404                 :            :         inode_unlock(inode);
    3405                 :            : out:
    3406                 :          0 :         trace_f2fs_file_write_iter(inode, iocb->ki_pos,
    3407                 :            :                                         iov_iter_count(from), ret);
    3408         [ #  # ]:          0 :         if (ret > 0)
    3409                 :          0 :                 ret = generic_write_sync(iocb, ret);
    3410                 :          0 :         return ret;
    3411                 :            : }
    3412                 :            : 
    3413                 :            : #ifdef CONFIG_COMPAT
    3414                 :            : long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    3415                 :            : {
    3416                 :            :         switch (cmd) {
    3417                 :            :         case F2FS_IOC32_GETFLAGS:
    3418                 :            :                 cmd = F2FS_IOC_GETFLAGS;
    3419                 :            :                 break;
    3420                 :            :         case F2FS_IOC32_SETFLAGS:
    3421                 :            :                 cmd = F2FS_IOC_SETFLAGS;
    3422                 :            :                 break;
    3423                 :            :         case F2FS_IOC32_GETVERSION:
    3424                 :            :                 cmd = F2FS_IOC_GETVERSION;
    3425                 :            :                 break;
    3426                 :            :         case F2FS_IOC_START_ATOMIC_WRITE:
    3427                 :            :         case F2FS_IOC_COMMIT_ATOMIC_WRITE:
    3428                 :            :         case F2FS_IOC_START_VOLATILE_WRITE:
    3429                 :            :         case F2FS_IOC_RELEASE_VOLATILE_WRITE:
    3430                 :            :         case F2FS_IOC_ABORT_VOLATILE_WRITE:
    3431                 :            :         case F2FS_IOC_SHUTDOWN:
    3432                 :            :         case F2FS_IOC_SET_ENCRYPTION_POLICY:
    3433                 :            :         case F2FS_IOC_GET_ENCRYPTION_PWSALT:
    3434                 :            :         case F2FS_IOC_GET_ENCRYPTION_POLICY:
    3435                 :            :         case FS_IOC_GET_ENCRYPTION_POLICY_EX:
    3436                 :            :         case FS_IOC_ADD_ENCRYPTION_KEY:
    3437                 :            :         case FS_IOC_REMOVE_ENCRYPTION_KEY:
    3438                 :            :         case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
    3439                 :            :         case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
    3440                 :            :         case F2FS_IOC_GARBAGE_COLLECT:
    3441                 :            :         case F2FS_IOC_GARBAGE_COLLECT_RANGE:
    3442                 :            :         case F2FS_IOC_WRITE_CHECKPOINT:
    3443                 :            :         case F2FS_IOC_DEFRAGMENT:
    3444                 :            :         case F2FS_IOC_MOVE_RANGE:
    3445                 :            :         case F2FS_IOC_FLUSH_DEVICE:
    3446                 :            :         case F2FS_IOC_GET_FEATURES:
    3447                 :            :         case F2FS_IOC_FSGETXATTR:
    3448                 :            :         case F2FS_IOC_FSSETXATTR:
    3449                 :            :         case F2FS_IOC_GET_PIN_FILE:
    3450                 :            :         case F2FS_IOC_SET_PIN_FILE:
    3451                 :            :         case F2FS_IOC_PRECACHE_EXTENTS:
    3452                 :            :         case F2FS_IOC_RESIZE_FS:
    3453                 :            :         case FS_IOC_ENABLE_VERITY:
    3454                 :            :         case FS_IOC_MEASURE_VERITY:
    3455                 :            :         case F2FS_IOC_GET_VOLUME_NAME:
    3456                 :            :         case F2FS_IOC_SET_VOLUME_NAME:
    3457                 :            :                 break;
    3458                 :            :         default:
    3459                 :            :                 return -ENOIOCTLCMD;
    3460                 :            :         }
    3461                 :            :         return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
    3462                 :            : }
    3463                 :            : #endif
    3464                 :            : 
    3465                 :            : const struct file_operations f2fs_file_operations = {
    3466                 :            :         .llseek         = f2fs_llseek,
    3467                 :            :         .read_iter      = generic_file_read_iter,
    3468                 :            :         .write_iter     = f2fs_file_write_iter,
    3469                 :            :         .open           = f2fs_file_open,
    3470                 :            :         .release        = f2fs_release_file,
    3471                 :            :         .mmap           = f2fs_file_mmap,
    3472                 :            :         .flush          = f2fs_file_flush,
    3473                 :            :         .fsync          = f2fs_sync_file,
    3474                 :            :         .fallocate      = f2fs_fallocate,
    3475                 :            :         .unlocked_ioctl = f2fs_ioctl,
    3476                 :            : #ifdef CONFIG_COMPAT
    3477                 :            :         .compat_ioctl   = f2fs_compat_ioctl,
    3478                 :            : #endif
    3479                 :            :         .splice_read    = generic_file_splice_read,
    3480                 :            :         .splice_write   = iter_file_splice_write,
    3481                 :            : };

Generated by: LCOV version 1.14