blob: f585545277d775b36824bba05c7de46ea694e07f [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09007 */
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>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090013#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090014#include <linux/falloc.h>
15#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090016#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/uaccess.h>
18#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090019#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080020#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070021#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070022#include <linux/file.h>
Chao Yu4507847c2019-07-17 17:06:11 +080023#include <linux/nls.h>
Daeho Jeong9af84642020-07-21 12:21:11 +090024#include <linux/sched/signal.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090025
26#include "f2fs.h"
27#include "node.h"
28#include "segment.h"
29#include "xattr.h"
30#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080031#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080032#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090033#include <trace/events/f2fs.h>
Chao Yufa4320c2020-11-02 14:21:31 +080034#include <uapi/linux/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035
Souptick Joarderea4d4792018-04-15 01:40:02 +053036static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080037{
38 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053039 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080040
41 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053042 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080043 up_read(&F2FS_I(inode)->i_mmap_sem);
44
Chao Yu8b83ac82020-04-16 18:16:56 +080045 if (!ret)
46 f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
47 F2FS_BLKSIZE);
48
Chao Yud7648342019-04-15 15:22:19 +080049 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
50
Souptick Joarderea4d4792018-04-15 01:40:02 +053051 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080052}
53
Souptick Joarderea4d4792018-04-15 01:40:02 +053054static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090055{
56 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080057 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070058 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080059 struct dnode_of_data dn;
Chao Yu4c8ff702019-11-01 18:07:14 +080060 bool need_alloc = true;
61 int err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090062
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020063 if (unlikely(f2fs_cp_error(sbi))) {
64 err = -EIO;
65 goto err;
66 }
67
Chao Yu00e09c02019-08-23 17:58:36 +080068 if (!f2fs_is_checkpoint_ready(sbi)) {
69 err = -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +080070 goto err;
Chao Yu00e09c02019-08-23 17:58:36 +080071 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090072
Chao Yu4c8ff702019-11-01 18:07:14 +080073#ifdef CONFIG_F2FS_FS_COMPRESSION
74 if (f2fs_compressed_file(inode)) {
75 int ret = f2fs_is_compressed_cluster(inode, page->index);
76
77 if (ret < 0) {
78 err = ret;
79 goto err;
80 } else if (ret) {
81 if (ret < F2FS_I(inode)->i_cluster_size) {
82 err = -EAGAIN;
83 goto err;
84 }
85 need_alloc = false;
86 }
87 }
88#endif
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080089 /* should do out of any locked page */
Chao Yu4c8ff702019-11-01 18:07:14 +080090 if (need_alloc)
91 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080092
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090093 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070094
95 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070096
Dave Jiang11bac802017-02-24 14:56:41 -080097 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080098 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090099 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900100 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900101 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900102 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900103 unlock_page(page);
104 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800105 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900106 }
107
Chao Yu4c8ff702019-11-01 18:07:14 +0800108 if (need_alloc) {
109 /* block allocation */
Chao Yu0ef81832020-06-18 14:36:22 +0800110 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +0800111 set_new_dnode(&dn, inode, NULL, NULL, 0);
112 err = f2fs_get_block(&dn, page->index);
113 f2fs_put_dnode(&dn);
Chao Yu0ef81832020-06-18 14:36:22 +0800114 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu39a86952018-09-27 18:33:18 +0800115 }
116
Chao Yu06c75402020-03-02 17:34:27 +0800117#ifdef CONFIG_F2FS_FS_COMPRESSION
118 if (!need_alloc) {
119 set_new_dnode(&dn, inode, NULL, NULL, 0);
120 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
121 f2fs_put_dnode(&dn);
122 }
123#endif
124 if (err) {
125 unlock_page(page);
126 goto out_sem;
Chao Yu39a86952018-09-27 18:33:18 +0800127 }
128
Chao Yubae0ee72018-12-25 17:43:42 +0800129 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +0800130
131 /* wait for GCed page writeback via META_MAPPING */
132 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
133
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900134 /*
135 * check to see if the page is mapped already (no holes)
136 */
137 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800138 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900139
140 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300141 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800142 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900143 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900144
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300145 offset = i_size_read(inode) & ~PAGE_MASK;
146 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900147 }
148 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700149 if (!PageUptodate(page))
150 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900151
Chao Yub0af6d42017-08-02 23:21:48 +0800152 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530153 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800154
Jaegeuk Kime943a102013-10-25 14:26:31 +0900155 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800156out_sem:
157 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800158
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900159 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200160err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900161 return block_page_mkwrite_return(err);
162}
163
164static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800165 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700166 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900167 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900168};
169
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900170static int get_parent_ino(struct inode *inode, nid_t *pino)
171{
172 struct dentry *dentry;
173
Eric Biggers84c9c2d2020-05-05 11:41:11 -0700174 /*
175 * Make sure to get the non-deleted alias. The alias associated with
176 * the open file descriptor being fsync()'ed may be deleted already.
177 */
178 dentry = d_find_alias(inode);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900179 if (!dentry)
180 return 0;
181
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900182 *pino = parent_ino(dentry);
183 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900184 return 1;
185}
186
Chao Yua5fd5052017-11-06 22:51:45 +0800187static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800188{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700189 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800190 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800191
Chao Yua5fd5052017-11-06 22:51:45 +0800192 if (!S_ISREG(inode->i_mode))
193 cp_reason = CP_NON_REGULAR;
Chao Yu4c8ff702019-11-01 18:07:14 +0800194 else if (f2fs_compressed_file(inode))
195 cp_reason = CP_COMPRESSED;
Chao Yua5fd5052017-11-06 22:51:45 +0800196 else if (inode->i_nlink != 1)
197 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700198 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800199 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800200 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800201 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800202 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800203 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800204 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800205 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700206 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800207 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800208 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800209 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800210 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800211 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
212 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
213 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800214 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800215
Chao Yua5fd5052017-11-06 22:51:45 +0800216 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800217}
218
Changman Lee9c7bb702014-12-08 15:29:40 +0900219static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
220{
221 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
222 bool ret = false;
223 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800224 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900225 ret = true;
226 f2fs_put_page(i, 0);
227 return ret;
228}
229
Changman Lee51455b12014-12-08 15:29:41 +0900230static void try_to_fix_pino(struct inode *inode)
231{
232 struct f2fs_inode_info *fi = F2FS_I(inode);
233 nid_t pino;
234
235 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900236 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
237 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700238 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900239 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900240 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700241 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900242}
243
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700244static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
245 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900246{
247 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700249 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900250 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800251 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900252 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900253 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900254 .nr_to_write = LONG_MAX,
255 .for_reclaim = 0,
256 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800257 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900258
Daniel Rosenberg43549942018-08-20 19:21:43 -0700259 if (unlikely(f2fs_readonly(inode->i_sb) ||
260 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900261 return 0;
262
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900263 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700264
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800265 if (S_ISDIR(inode->i_mode))
266 goto go_write;
267
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700268 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800269 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700270 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400271 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700272 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700273
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900274 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800275 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900276 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900277 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900278
Changman Lee9c7bb702014-12-08 15:29:40 +0900279 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800280 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700281 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900282 goto go_write;
283 }
284
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700285 /*
286 * if there is no written data, don't waste time to write recovery info.
287 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700288 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800289 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700290
Changman Lee9c7bb702014-12-08 15:29:40 +0900291 /* it may call write_inode just prior to fsync */
292 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700293 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700294
Jaegeuk Kim91942322016-05-20 10:13:22 -0700295 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800296 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700297 goto flush_out;
298 goto out;
299 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700300go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900301 /*
302 * Both of fdatasync() and fsync() are able to be recovered from
303 * sudden-power-off.
304 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700305 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800306 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700307 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900308
Chao Yua5fd5052017-11-06 22:51:45 +0800309 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900310 /* all the dirty node pages should be flushed for POR */
311 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900312
Changman Lee51455b12014-12-08 15:29:41 +0900313 /*
314 * We've secured consistency through sync_fs. Following pino
315 * will be used only for fsynced inodes after checkpoint.
316 */
317 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700318 clear_inode_flag(inode, FI_APPEND_WRITE);
319 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900320 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900321 }
Changman Lee51455b12014-12-08 15:29:41 +0900322sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800323 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800324 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800325 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700326 if (ret)
327 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900328
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800329 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800330 if (unlikely(f2fs_cp_error(sbi))) {
331 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800332 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800333 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800334
Chao Yu4d57b862018-05-30 00:20:41 +0800335 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700336 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900337 f2fs_write_inode(inode, NULL);
338 goto sync_nodes;
339 }
340
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700341 /*
342 * If it's atomic_write, it's just fine to keep write ordering. So
343 * here we don't need to wait for node write completion, since we use
344 * node chain which serializes node blocks. If one of node writes are
345 * reordered, we can see simply broken chain, resulting in stopping
346 * roll-forward recovery. It means we'll recover all or none node blocks
347 * given fsync mark.
348 */
349 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800350 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700351 if (ret)
352 goto out;
353 }
Changman Lee51455b12014-12-08 15:29:41 +0900354
355 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800356 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700357 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900358flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700359 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800360 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800361 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800362 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800363 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800364 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800365 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800366 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900367out:
Chao Yua5fd5052017-11-06 22:51:45 +0800368 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700369 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900370 return ret;
371}
372
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700373int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
374{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200375 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
376 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700377 return f2fs_do_sync_file(file, start, end, datasync, false);
378}
379
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100380static bool __found_offset(struct address_space *mapping, block_t blkaddr,
381 pgoff_t index, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900382{
383 switch (whence) {
384 case SEEK_DATA:
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100385 if (__is_valid_data_blkaddr(blkaddr))
386 return true;
387 if (blkaddr == NEW_ADDR &&
388 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900389 return true;
390 break;
391 case SEEK_HOLE:
392 if (blkaddr == NULL_ADDR)
393 return true;
394 break;
395 }
396 return false;
397}
398
Chao Yu267378d2014-04-23 14:10:24 +0800399static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
400{
401 struct inode *inode = file->f_mapping->host;
402 loff_t maxbytes = inode->i_sb->s_maxbytes;
403 struct dnode_of_data dn;
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100404 pgoff_t pgofs, end_offset;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900405 loff_t data_ofs = offset;
406 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800407 int err = 0;
408
Al Viro59551022016-01-22 15:40:57 -0500409 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800410
411 isize = i_size_read(inode);
412 if (offset >= isize)
413 goto fail;
414
415 /* handle inline data case */
Chao Yu7a6e59d2020-11-02 17:36:58 +0800416 if (f2fs_has_inline_data(inode)) {
417 if (whence == SEEK_HOLE) {
418 data_ofs = isize;
419 goto found;
420 } else if (whence == SEEK_DATA) {
421 data_ofs = offset;
422 goto found;
423 }
Chao Yu267378d2014-04-23 14:10:24 +0800424 }
425
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300426 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800427
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300428 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800429 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800430 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800431 if (err && err != -ENOENT) {
432 goto fail;
433 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900434 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800435 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800436 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800437 continue;
438 } else {
439 goto found;
440 }
441 }
442
Chao Yu81ca7352016-01-26 15:39:35 +0800443 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800444
445 /* find data/hole in dnode block */
446 for (; dn.ofs_in_node < end_offset;
447 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300448 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800449 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900450
Chao Yua2ced1c2020-02-14 17:44:10 +0800451 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu267378d2014-04-23 14:10:24 +0800452
Chao Yuc9b60782018-08-01 19:13:44 +0800453 if (__is_valid_data_blkaddr(blkaddr) &&
454 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800455 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800456 f2fs_put_dnode(&dn);
457 goto fail;
458 }
459
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100460 if (__found_offset(file->f_mapping, blkaddr,
Chao Yue1da7872018-06-05 17:44:11 +0800461 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800462 f2fs_put_dnode(&dn);
463 goto found;
464 }
465 }
466 f2fs_put_dnode(&dn);
467 }
468
469 if (whence == SEEK_DATA)
470 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800471found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900472 if (whence == SEEK_HOLE && data_ofs > isize)
473 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500474 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800475 return vfs_setpos(file, data_ofs, maxbytes);
476fail:
Al Viro59551022016-01-22 15:40:57 -0500477 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800478 return -ENXIO;
479}
480
481static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
482{
483 struct inode *inode = file->f_mapping->host;
484 loff_t maxbytes = inode->i_sb->s_maxbytes;
485
486 switch (whence) {
487 case SEEK_SET:
488 case SEEK_CUR:
489 case SEEK_END:
490 return generic_file_llseek_size(file, offset, whence,
491 maxbytes, i_size_read(inode));
492 case SEEK_DATA:
493 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700494 if (offset < 0)
495 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800496 return f2fs_seek_block(file, offset, whence);
497 }
498
499 return -EINVAL;
500}
501
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900502static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
503{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700504 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800505 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700506
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200507 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
508 return -EIO;
509
Chao Yu4c8ff702019-11-01 18:07:14 +0800510 if (!f2fs_is_compress_backend_ready(inode))
511 return -EOPNOTSUPP;
512
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700513 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800514 err = f2fs_convert_inline_inode(inode);
515 if (err)
516 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700517
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900518 file_accessed(file);
519 vma->vm_ops = &f2fs_file_vm_ops;
Chao Yu4c8ff702019-11-01 18:07:14 +0800520 set_inode_flag(inode, FI_MMAP_FILE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900521 return 0;
522}
523
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700524static int f2fs_file_open(struct inode *inode, struct file *filp)
525{
Eric Biggers2e168c82017-11-29 12:35:28 -0800526 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700527
Eric Biggers2e168c82017-11-29 12:35:28 -0800528 if (err)
529 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900530
Chao Yu4c8ff702019-11-01 18:07:14 +0800531 if (!f2fs_is_compress_backend_ready(inode))
532 return -EOPNOTSUPP;
533
Eric Biggers95ae2512019-07-22 09:26:24 -0700534 err = fsverity_file_open(inode, filp);
535 if (err)
536 return err;
537
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900538 filp->f_mode |= FMODE_NOWAIT;
539
Chao Yu0abd6752017-07-09 00:13:07 +0800540 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700541}
542
Chao Yu4d57b862018-05-30 00:20:41 +0800543void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900544{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700545 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900546 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800547 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900548 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800549 int base = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800550 bool compressed_cluster = false;
551 int cluster_index = 0, valid_blocks = 0;
552 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
Daeho Jeongc2759eb2020-09-08 11:44:10 +0900553 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
Chao Yu7a2af762017-07-19 00:19:06 +0800554
555 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
556 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900557
Gu Zheng45590712013-07-15 17:57:38 +0800558 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800559 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900560
Chao Yu4c8ff702019-11-01 18:07:14 +0800561 /* Assumption: truncateion starts with cluster */
562 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900563 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900564
Chao Yu4c8ff702019-11-01 18:07:14 +0800565 if (f2fs_compressed_file(dn->inode) &&
566 !(cluster_index & (cluster_size - 1))) {
567 if (compressed_cluster)
568 f2fs_i_compr_blocks_update(dn->inode,
569 valid_blocks, false);
570 compressed_cluster = (blkaddr == COMPRESS_ADDR);
571 valid_blocks = 0;
572 }
573
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900574 if (blkaddr == NULL_ADDR)
575 continue;
576
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800577 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800578 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800579
Chao Yu4c8ff702019-11-01 18:07:14 +0800580 if (__is_valid_data_blkaddr(blkaddr)) {
581 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800582 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800583 continue;
584 if (compressed_cluster)
585 valid_blocks++;
586 }
Chao Yuc9b60782018-08-01 19:13:44 +0800587
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700588 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700589 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800590
591 f2fs_invalidate_blocks(sbi, blkaddr);
Chao Yuef8d5632020-03-06 15:36:09 +0800592
593 if (!released || blkaddr != COMPRESS_ADDR)
594 nr_free++;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900595 }
Chao Yu19b2c302015-08-26 20:34:48 +0800596
Chao Yu4c8ff702019-11-01 18:07:14 +0800597 if (compressed_cluster)
598 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
599
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900600 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800601 pgoff_t fofs;
602 /*
603 * once we invalidate valid blkaddr in range [ofs, ofs + count],
604 * we will invalidate all blkaddr in the whole range.
605 */
Chao Yu4d57b862018-05-30 00:20:41 +0800606 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800607 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800608 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900609 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900610 }
611 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900612
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800613 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900614 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
615 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900616}
617
Chao Yu4d57b862018-05-30 00:20:41 +0800618void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900619{
Chao Yud02a6e62019-03-25 21:08:19 +0800620 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900621}
622
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800623static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700624 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900625{
youngjun yoo193bea12018-05-30 04:21:14 +0900626 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700628 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900629 struct page *page;
630
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700631 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700632 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900633
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700634 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700635 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700636 if (page && PageUptodate(page))
637 goto truncate_out;
638 f2fs_put_page(page, 1);
639 return 0;
640 }
641
Chao Yu4d57b862018-05-30 00:20:41 +0800642 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900643 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800644 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700645truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800646 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300647 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700648
649 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530650 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700651 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800652 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900653 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700654 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900655}
656
Chao Yu3265d3d2020-03-18 16:22:59 +0800657int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900658{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700659 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900660 struct dnode_of_data dn;
661 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800662 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700663 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800664 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665
Namjae Jeon51dd6242013-04-20 01:28:52 +0900666 trace_f2fs_truncate_blocks_enter(inode, from);
667
Chao Yudf033ca2018-03-20 23:08:29 +0800668 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900669
Chao Yu09210c92016-05-05 19:13:03 +0800670 if (free_from >= sbi->max_file_blocks)
671 goto free_partial;
672
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700673 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800674 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800675
Chao Yu4d57b862018-05-30 00:20:41 +0800676 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700677 if (IS_ERR(ipage)) {
678 err = PTR_ERR(ipage);
679 goto out;
680 }
681
682 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800683 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700684 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800685 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700686 goto out;
687 }
688
689 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800690 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900691 if (err) {
692 if (err == -ENOENT)
693 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700694 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700695 }
696
Chao Yu81ca7352016-01-26 15:39:35 +0800697 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900698
699 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700700 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900701
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900702 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800703 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900704 free_from += count;
705 }
706
707 f2fs_put_dnode(&dn);
708free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800709 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800710out:
711 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800712 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800713free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700714 /* lastly zero out the first data page */
715 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800716 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900717
Namjae Jeon51dd6242013-04-20 01:28:52 +0900718 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719 return err;
720}
721
Chao Yu4c8ff702019-11-01 18:07:14 +0800722int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
723{
724 u64 free_from = from;
Chao Yu3265d3d2020-03-18 16:22:59 +0800725 int err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800726
Chao Yu3265d3d2020-03-18 16:22:59 +0800727#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800728 /*
729 * for compressed file, only support cluster size
730 * aligned truncation.
731 */
Chao Yu4fec3fc02020-04-08 19:55:17 +0800732 if (f2fs_compressed_file(inode))
733 free_from = round_up(from,
734 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
Chao Yu3265d3d2020-03-18 16:22:59 +0800735#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800736
Chao Yu3265d3d2020-03-18 16:22:59 +0800737 err = f2fs_do_truncate_blocks(inode, free_from, lock);
738 if (err)
739 return err;
740
741#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu17d76482020-08-10 18:38:45 +0800742 if (from != free_from) {
Chao Yu3265d3d2020-03-18 16:22:59 +0800743 err = f2fs_truncate_partial_cluster(inode, from, lock);
Chao Yu17d76482020-08-10 18:38:45 +0800744 if (err)
745 return err;
746 }
Chao Yu3265d3d2020-03-18 16:22:59 +0800747#endif
748
Chao Yu17d76482020-08-10 18:38:45 +0800749 return 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800750}
751
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700752int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900753{
Chao Yub0154892015-08-24 17:39:42 +0800754 int err;
755
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200756 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
757 return -EIO;
758
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900759 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
760 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800761 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900762
Namjae Jeon51dd6242013-04-20 01:28:52 +0900763 trace_f2fs_truncate(inode);
764
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800765 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800766 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800767 return -EIO;
768 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200769
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800770 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800771 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800772 err = f2fs_convert_inline_inode(inode);
773 if (err)
774 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800775 }
776
Chao Yuc42d28c2019-02-02 17:33:01 +0800777 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800778 if (err)
779 return err;
780
Deepa Dinamani078cd822016-09-14 07:48:04 -0700781 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700782 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800783 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900784}
785
David Howellsa528d352017-01-31 16:46:22 +0000786int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800787 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900788{
David Howellsa528d352017-01-31 16:46:22 +0000789 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800790 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800791 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800792 unsigned int flags;
793
Chao Yu1c1d35d2018-01-25 14:54:42 +0800794 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800795 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800796 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
797 stat->result_mask |= STATX_BTIME;
798 stat->btime.tv_sec = fi->i_crtime.tv_sec;
799 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
800 }
801
Eric Biggers36098552019-06-04 22:59:04 -0700802 flags = fi->i_flags;
Chao Yufd267252020-03-25 10:22:09 +0800803 if (flags & F2FS_COMPR_FL)
804 stat->attributes |= STATX_ATTR_COMPRESSED;
Chao Yu59c84402018-04-03 15:08:17 +0800805 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800806 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530807 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800808 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800809 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800810 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800811 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800812 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700813 if (IS_VERITY(inode))
814 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800815
Chao Yufd267252020-03-25 10:22:09 +0800816 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
817 STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800818 STATX_ATTR_ENCRYPTED |
819 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700820 STATX_ATTR_NODUMP |
821 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800822
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900823 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700824
825 /* we need to show initial sectors used for inline_data/dentries */
826 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
827 f2fs_has_inline_dentry(inode))
828 stat->blocks += (stat->size + 511) >> 9;
829
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900830 return 0;
831}
832
833#ifdef CONFIG_F2FS_FS_POSIX_ACL
834static void __setattr_copy(struct inode *inode, const struct iattr *attr)
835{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900836 unsigned int ia_valid = attr->ia_valid;
837
838 if (ia_valid & ATTR_UID)
839 inode->i_uid = attr->ia_uid;
840 if (ia_valid & ATTR_GID)
841 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200842 if (ia_valid & ATTR_ATIME)
843 inode->i_atime = attr->ia_atime;
844 if (ia_valid & ATTR_MTIME)
845 inode->i_mtime = attr->ia_mtime;
846 if (ia_valid & ATTR_CTIME)
847 inode->i_ctime = attr->ia_ctime;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900848 if (ia_valid & ATTR_MODE) {
849 umode_t mode = attr->ia_mode;
850
851 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
852 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700853 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900854 }
855}
856#else
857#define __setattr_copy setattr_copy
858#endif
859
860int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
861{
David Howells2b0143b2015-03-17 22:25:59 +0000862 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900863 int err;
864
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200865 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
866 return -EIO;
867
Chao Yu4c8ff702019-11-01 18:07:14 +0800868 if ((attr->ia_valid & ATTR_SIZE) &&
869 !f2fs_is_compress_backend_ready(inode))
870 return -EOPNOTSUPP;
871
Jan Kara31051c82016-05-26 16:55:18 +0200872 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900873 if (err)
874 return err;
875
Eric Biggers20bb2472017-11-29 12:35:32 -0800876 err = fscrypt_prepare_setattr(dentry, attr);
877 if (err)
878 return err;
879
Eric Biggers95ae2512019-07-22 09:26:24 -0700880 err = fsverity_prepare_setattr(dentry, attr);
881 if (err)
882 return err;
883
Chao Yu0abd6752017-07-09 00:13:07 +0800884 if (is_quota_modification(inode, attr)) {
885 err = dquot_initialize(inode);
886 if (err)
887 return err;
888 }
889 if ((attr->ia_valid & ATTR_UID &&
890 !uid_eq(attr->ia_uid, inode->i_uid)) ||
891 (attr->ia_valid & ATTR_GID &&
892 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800893 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800894 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800895 if (err) {
896 set_sbi_flag(F2FS_I_SB(inode),
897 SBI_QUOTA_NEED_REPAIR);
898 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800899 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800900 }
901 /*
902 * update uid/gid under lock_op(), so that dquot and inode can
903 * be updated atomically.
904 */
905 if (attr->ia_valid & ATTR_UID)
906 inode->i_uid = attr->ia_uid;
907 if (attr->ia_valid & ATTR_GID)
908 inode->i_gid = attr->ia_gid;
909 f2fs_mark_inode_dirty_sync(inode, true);
910 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800911 }
912
Chao Yu09db6a22014-09-15 18:02:09 +0800913 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800914 loff_t old_size = i_size_read(inode);
915
916 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
917 /*
918 * should convert inline inode before i_size_write to
919 * keep smaller than inline_data size with inline flag.
920 */
921 err = f2fs_convert_inline_inode(inode);
922 if (err)
923 return err;
924 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800925
Chao Yua33c1502018-08-05 23:04:25 +0800926 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900927 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800928
929 truncate_setsize(inode, attr->ia_size);
930
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800931 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800932 err = f2fs_truncate(inode);
933 /*
934 * do not trim all blocks after i_size if target size is
935 * larger than i_size.
936 */
Chao Yua33c1502018-08-05 23:04:25 +0800937 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900938 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800939 if (err)
940 return err;
941
Chao Yuc10c9822020-02-27 19:30:03 +0800942 spin_lock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800943 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800944 F2FS_I(inode)->last_disk_size = i_size_read(inode);
Chao Yuc10c9822020-02-27 19:30:03 +0800945 spin_unlock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900946 }
947
948 __setattr_copy(inode, attr);
949
950 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800951 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700952 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
953 inode->i_mode = F2FS_I(inode)->i_acl_mode;
954 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900955 }
956 }
957
Yunlei Hec0ed4402016-12-11 15:35:15 +0800958 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800959 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700960
961 /* inode change will produce dirty node pages flushed by checkpoint */
962 f2fs_balance_fs(F2FS_I_SB(inode), true);
963
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900964 return err;
965}
966
967const struct inode_operations f2fs_file_inode_operations = {
968 .getattr = f2fs_getattr,
969 .setattr = f2fs_setattr,
970 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800971 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900972 .listxattr = f2fs_listxattr,
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900973 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900974};
975
Chao Yu63943282015-08-07 18:36:06 +0800976static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900977 loff_t start, loff_t len)
978{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700979 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900980 struct page *page;
981
982 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800983 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900984
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800985 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900986
Gu Zhenge4795562013-09-27 18:08:30 +0800987 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800988 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800989 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900990
Chao Yu63943282015-08-07 18:36:06 +0800991 if (IS_ERR(page))
992 return PTR_ERR(page);
993
Chao Yubae0ee72018-12-25 17:43:42 +0800994 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +0800995 zero_user(page, start, len);
996 set_page_dirty(page);
997 f2fs_put_page(page, 1);
998 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900999}
1000
Chao Yu4d57b862018-05-30 00:20:41 +08001001int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001002{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001003 int err;
1004
Chao Yuea587112015-09-17 20:22:44 +08001005 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001006 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001007 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001008
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001009 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001010 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001011 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001012 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001013 pg_start = f2fs_get_next_page_offset(&dn,
1014 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001015 continue;
Chao Yuea587112015-09-17 20:22:44 +08001016 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001017 return err;
1018 }
1019
Chao Yu81ca7352016-01-26 15:39:35 +08001020 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001021 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1022
1023 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1024
Chao Yu4d57b862018-05-30 00:20:41 +08001025 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001026 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001027
1028 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001029 }
1030 return 0;
1031}
1032
Chao Yua66c7b22013-11-22 16:52:50 +08001033static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001034{
1035 pgoff_t pg_start, pg_end;
1036 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001037 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001038
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001039 ret = f2fs_convert_inline_inode(inode);
1040 if (ret)
1041 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001042
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001043 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1044 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001045
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001046 off_start = offset & (PAGE_SIZE - 1);
1047 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001048
1049 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001050 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001051 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001052 if (ret)
1053 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001054 } else {
Chao Yu63943282015-08-07 18:36:06 +08001055 if (off_start) {
1056 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001057 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001058 if (ret)
1059 return ret;
1060 }
1061 if (off_end) {
1062 ret = fill_zero(inode, pg_end, 0, off_end);
1063 if (ret)
1064 return ret;
1065 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001066
1067 if (pg_start < pg_end) {
1068 struct address_space *mapping = inode->i_mapping;
1069 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001070 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001071
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001072 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001073
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001074 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1075 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001076
Chao Yua33c1502018-08-05 23:04:25 +08001077 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001078 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001079
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001080 truncate_inode_pages_range(mapping, blk_start,
1081 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001082
Gu Zhenge4795562013-09-27 18:08:30 +08001083 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001084 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001085 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001086
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001087 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001088 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001089 }
1090 }
1091
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001092 return ret;
1093}
1094
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001095static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1096 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001097{
1098 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1099 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001100 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001101
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001102next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001103 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001104 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001105 if (ret && ret != -ENOENT) {
1106 return ret;
1107 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001108 if (dn.max_level == 0)
1109 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001110 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1111 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001112 blkaddr += done;
1113 do_replace += done;
1114 goto next;
1115 }
1116
1117 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1118 dn.ofs_in_node, len);
1119 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001120 *blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu93770ab2019-04-15 15:26:32 +08001121
1122 if (__is_valid_data_blkaddr(*blkaddr) &&
1123 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1124 DATA_GENERIC_ENHANCE)) {
1125 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001126 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001127 }
1128
Chao Yu4d57b862018-05-30 00:20:41 +08001129 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001130
Chao Yub0332a02020-02-14 17:44:12 +08001131 if (f2fs_lfs_mode(sbi)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001132 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001133 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001134 }
1135
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001136 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001137 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001138 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001139 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001140 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001141 f2fs_put_dnode(&dn);
1142next:
1143 len -= done;
1144 off += done;
1145 if (len)
1146 goto next_dnode;
1147 return 0;
1148}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001149
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001150static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1151 int *do_replace, pgoff_t off, int len)
1152{
1153 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1154 struct dnode_of_data dn;
1155 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001156
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001157 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1158 if (*do_replace == 0)
1159 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001160
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001161 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001162 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001163 if (ret) {
1164 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001165 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001166 } else {
1167 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001168 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001169 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001170 }
1171 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001172}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001173
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001174static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1175 block_t *blkaddr, int *do_replace,
1176 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1177{
1178 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1179 pgoff_t i = 0;
1180 int ret;
1181
1182 while (i < len) {
1183 if (blkaddr[i] == NULL_ADDR && !full) {
1184 i++;
1185 continue;
1186 }
1187
1188 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1189 struct dnode_of_data dn;
1190 struct node_info ni;
1191 size_t new_size;
1192 pgoff_t ilen;
1193
1194 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001195 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001196 if (ret)
1197 return ret;
1198
Chao Yu77357302018-07-17 00:02:17 +08001199 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1200 if (ret) {
1201 f2fs_put_dnode(&dn);
1202 return ret;
1203 }
1204
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001205 ilen = min((pgoff_t)
1206 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1207 dn.ofs_in_node, len - i);
1208 do {
Chao Yua2ced1c2020-02-14 17:44:10 +08001209 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu4d57b862018-05-30 00:20:41 +08001210 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001211
1212 if (do_replace[i]) {
1213 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001214 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001215 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001216 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001217 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1218 blkaddr[i], ni.version, true, false);
1219
1220 do_replace[i] = 0;
1221 }
1222 dn.ofs_in_node++;
1223 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001224 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001225 if (dst_inode->i_size < new_size)
1226 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001227 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001228
1229 f2fs_put_dnode(&dn);
1230 } else {
1231 struct page *psrc, *pdst;
1232
Chao Yu4d57b862018-05-30 00:20:41 +08001233 psrc = f2fs_get_lock_data_page(src_inode,
1234 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001235 if (IS_ERR(psrc))
1236 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001237 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001238 true);
1239 if (IS_ERR(pdst)) {
1240 f2fs_put_page(psrc, 1);
1241 return PTR_ERR(pdst);
1242 }
1243 f2fs_copy_page(psrc, pdst);
1244 set_page_dirty(pdst);
1245 f2fs_put_page(pdst, 1);
1246 f2fs_put_page(psrc, 1);
1247
Chao Yu4d57b862018-05-30 00:20:41 +08001248 ret = f2fs_truncate_hole(src_inode,
1249 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001250 if (ret)
1251 return ret;
1252 i++;
1253 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001254 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001255 return 0;
1256}
1257
1258static int __exchange_data_block(struct inode *src_inode,
1259 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001260 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001261{
1262 block_t *src_blkaddr;
1263 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001264 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001265 int ret;
1266
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001267 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001268 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001269
Chao Yu628b3d12017-11-30 19:28:18 +08001270 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001271 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001272 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001273 if (!src_blkaddr)
1274 return -ENOMEM;
1275
Chao Yu628b3d12017-11-30 19:28:18 +08001276 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001277 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001278 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001279 if (!do_replace) {
1280 kvfree(src_blkaddr);
1281 return -ENOMEM;
1282 }
1283
1284 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1285 do_replace, src, olen);
1286 if (ret)
1287 goto roll_back;
1288
1289 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1290 do_replace, src, dst, olen, full);
1291 if (ret)
1292 goto roll_back;
1293
1294 src += olen;
1295 dst += olen;
1296 len -= olen;
1297
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001298 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001299 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001300 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001301 return 0;
1302
1303roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001304 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001305 kvfree(src_blkaddr);
1306 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001307 return ret;
1308}
1309
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001310static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001311{
1312 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001313 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001314 pgoff_t start = offset >> PAGE_SHIFT;
1315 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001316 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001317
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001318 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001319
1320 /* avoid gc operation during block exchange */
1321 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1322 down_write(&F2FS_I(inode)->i_mmap_sem);
1323
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001324 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001325 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001326 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001327 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1328 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001329
1330 up_write(&F2FS_I(inode)->i_mmap_sem);
1331 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001332 return ret;
1333}
1334
1335static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1336{
Chao Yub4ace332015-05-06 13:09:46 +08001337 loff_t new_size;
1338 int ret;
1339
Chao Yub4ace332015-05-06 13:09:46 +08001340 if (offset + len >= i_size_read(inode))
1341 return -EINVAL;
1342
1343 /* collapse range should be aligned to block size of f2fs. */
1344 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1345 return -EINVAL;
1346
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001347 ret = f2fs_convert_inline_inode(inode);
1348 if (ret)
1349 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001350
Chao Yub4ace332015-05-06 13:09:46 +08001351 /* write out all dirty pages from offset */
1352 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1353 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001354 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001355
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001356 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001357 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001358 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001359
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001360 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001361 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001362 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1363 truncate_pagecache(inode, offset);
1364
Chao Yub4ace332015-05-06 13:09:46 +08001365 new_size = i_size_read(inode) - len;
Chao Yuc42d28c2019-02-02 17:33:01 +08001366 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001367 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001368 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001369 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001370 return ret;
1371}
1372
Chao Yu6e961942016-05-09 19:56:31 +08001373static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1374 pgoff_t end)
1375{
1376 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1377 pgoff_t index = start;
1378 unsigned int ofs_in_node = dn->ofs_in_node;
1379 blkcnt_t count = 0;
1380 int ret;
1381
1382 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001383 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001384 count++;
1385 }
1386
1387 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001388 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001389 if (ret)
1390 return ret;
1391
1392 dn->ofs_in_node = ofs_in_node;
1393 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001394 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001395 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001396 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001397 * allocation.
1398 */
1399 if (dn->data_blkaddr == NULL_ADDR) {
1400 ret = -ENOSPC;
1401 break;
1402 }
1403 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001404 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001405 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001406 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001407 }
1408 }
1409
1410 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1411
1412 return ret;
1413}
1414
Chao Yu75cd4e02015-05-06 13:11:13 +08001415static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1416 int mode)
1417{
1418 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1419 struct address_space *mapping = inode->i_mapping;
1420 pgoff_t index, pg_start, pg_end;
1421 loff_t new_size = i_size_read(inode);
1422 loff_t off_start, off_end;
1423 int ret = 0;
1424
Chao Yu75cd4e02015-05-06 13:11:13 +08001425 ret = inode_newsize_ok(inode, (len + offset));
1426 if (ret)
1427 return ret;
1428
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001429 ret = f2fs_convert_inline_inode(inode);
1430 if (ret)
1431 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001432
1433 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1434 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001435 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001436
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001437 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1438 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001439
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001440 off_start = offset & (PAGE_SIZE - 1);
1441 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001442
1443 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001444 ret = fill_zero(inode, pg_start, off_start,
1445 off_end - off_start);
1446 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001447 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001448
Chao Yu75cd4e02015-05-06 13:11:13 +08001449 new_size = max_t(loff_t, new_size, offset + len);
1450 } else {
1451 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001452 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001453 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001454 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001455 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001456
Chao Yu75cd4e02015-05-06 13:11:13 +08001457 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001459 }
1460
Chao Yu6e961942016-05-09 19:56:31 +08001461 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001462 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001463 unsigned int end_offset;
1464 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001465
Chao Yuc70798532018-08-05 23:02:22 +08001466 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001467 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001468
1469 truncate_pagecache_range(inode,
1470 (loff_t)index << PAGE_SHIFT,
1471 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1472
Chao Yu75cd4e02015-05-06 13:11:13 +08001473 f2fs_lock_op(sbi);
1474
Chao Yu6e961942016-05-09 19:56:31 +08001475 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001476 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001477 if (ret) {
1478 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001479 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001480 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001481 goto out;
1482 }
1483
Chao Yu6e961942016-05-09 19:56:31 +08001484 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1485 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1486
1487 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001488 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001489
Chao Yu75cd4e02015-05-06 13:11:13 +08001490 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001491 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001492 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001493
1494 f2fs_balance_fs(sbi, dn.node_changed);
1495
Chao Yu6e961942016-05-09 19:56:31 +08001496 if (ret)
1497 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001498
Chao Yu6e961942016-05-09 19:56:31 +08001499 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001500 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001501 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001502 }
1503
1504 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001505 ret = fill_zero(inode, pg_end, 0, off_end);
1506 if (ret)
1507 goto out;
1508
Chao Yu75cd4e02015-05-06 13:11:13 +08001509 new_size = max_t(loff_t, new_size, offset + len);
1510 }
1511 }
1512
1513out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001514 if (new_size > i_size_read(inode)) {
1515 if (mode & FALLOC_FL_KEEP_SIZE)
1516 file_set_keep_isize(inode);
1517 else
1518 f2fs_i_size_write(inode, new_size);
1519 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001520 return ret;
1521}
1522
Chao Yuf62185d2015-05-28 19:16:57 +08001523static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1524{
1525 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001526 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001527 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001528 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001529
Chao Yuf62185d2015-05-28 19:16:57 +08001530 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001531 ret = inode_newsize_ok(inode, new_size);
1532 if (ret)
1533 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001534
1535 if (offset >= i_size_read(inode))
1536 return -EINVAL;
1537
1538 /* insert range should be aligned to block size of f2fs. */
1539 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1540 return -EINVAL;
1541
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001542 ret = f2fs_convert_inline_inode(inode);
1543 if (ret)
1544 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001545
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001546 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001547
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001548 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001549 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001550 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001551 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001552 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001553
1554 /* write out all dirty pages from offset */
1555 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1556 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001557 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001559 pg_start = offset >> PAGE_SHIFT;
1560 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001561 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001562 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001563
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001564 /* avoid gc operation during block exchange */
1565 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1566 down_write(&F2FS_I(inode)->i_mmap_sem);
1567 truncate_pagecache(inode, offset);
1568
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001569 while (!ret && idx > pg_start) {
1570 nr = idx - pg_start;
1571 if (nr > delta)
1572 nr = delta;
1573 idx -= nr;
1574
Chao Yuf62185d2015-05-28 19:16:57 +08001575 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001576 f2fs_drop_extent_tree(inode);
1577
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001578 ret = __exchange_data_block(inode, inode, idx,
1579 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001580 f2fs_unlock_op(sbi);
1581 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001582 up_write(&F2FS_I(inode)->i_mmap_sem);
1583 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001584
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001585 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001586 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001587 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1588 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001589 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001590
1591 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001592 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001593 return ret;
1594}
1595
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001596static int expand_inode_data(struct inode *inode, loff_t offset,
1597 loff_t len, int mode)
1598{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001599 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001600 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001601 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1602 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001603 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001604 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001605 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001606 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001607
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001608 err = inode_newsize_ok(inode, (len + offset));
1609 if (err)
1610 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001611
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001612 err = f2fs_convert_inline_inode(inode);
1613 if (err)
1614 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001615
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001616 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001617
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001618 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001619 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001620
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001621 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1622 map.m_len = pg_end - map.m_lblk;
1623 if (off_end)
1624 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001625
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001626 if (!map.m_len)
1627 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001628
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001629 if (f2fs_is_pinned_file(inode)) {
1630 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1631 sbi->log_blocks_per_seg;
1632 block_t done = 0;
1633
1634 if (map.m_len % sbi->blocks_per_seg)
1635 len += sbi->blocks_per_seg;
1636
1637 map.m_len = sbi->blocks_per_seg;
1638next_alloc:
1639 if (has_not_enough_free_secs(sbi, 0,
1640 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
Chao Yufb24fea2020-01-14 19:36:50 +08001641 down_write(&sbi->gc_lock);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001642 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1643 if (err && err != -ENODATA && err != -EAGAIN)
1644 goto out_err;
1645 }
1646
1647 down_write(&sbi->pin_sem);
Daeho Jeongfd612642020-05-27 13:02:31 +09001648
1649 f2fs_lock_op(sbi);
Chao Yud0b9e422020-08-04 21:14:45 +08001650 f2fs_allocate_new_segment(sbi, CURSEG_COLD_DATA_PINNED);
Daeho Jeongfd612642020-05-27 13:02:31 +09001651 f2fs_unlock_op(sbi);
1652
Chao Yud0b9e422020-08-04 21:14:45 +08001653 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001654 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Chao Yud0b9e422020-08-04 21:14:45 +08001655
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001656 up_write(&sbi->pin_sem);
1657
1658 done += map.m_len;
1659 len -= map.m_len;
1660 map.m_lblk += map.m_len;
1661 if (!err && len)
1662 goto next_alloc;
1663
1664 map.m_len = done;
1665 } else {
1666 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1667 }
1668out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001669 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001670 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001671
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001672 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001673 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001674
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001675 last_off = map.m_lblk + map.m_len - 1;
1676
1677 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001678 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001679 (loff_t)(last_off + 1) << PAGE_SHIFT;
1680 } else {
1681 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001682 }
1683
Chao Yue8ed90a2017-11-05 21:53:30 +08001684 if (new_size > i_size_read(inode)) {
1685 if (mode & FALLOC_FL_KEEP_SIZE)
1686 file_set_keep_isize(inode);
1687 else
1688 f2fs_i_size_write(inode, new_size);
1689 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001690
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001691 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001692}
1693
1694static long f2fs_fallocate(struct file *file, int mode,
1695 loff_t offset, loff_t len)
1696{
Al Viro6131ffa2013-02-27 16:59:05 -05001697 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001698 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001699
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001700 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1701 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001702 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1703 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001704 if (!f2fs_is_compress_backend_ready(inode))
1705 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001706
Chao Yuc9980122015-09-11 14:39:02 +08001707 /* f2fs only support ->fallocate for regular file */
1708 if (!S_ISREG(inode->i_mode))
1709 return -EINVAL;
1710
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301711 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001712 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001713 return -EOPNOTSUPP;
1714
Chao Yu4c8ff702019-11-01 18:07:14 +08001715 if (f2fs_compressed_file(inode) &&
1716 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1717 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1718 return -EOPNOTSUPP;
1719
Chao Yub4ace332015-05-06 13:09:46 +08001720 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001721 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1722 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001723 return -EOPNOTSUPP;
1724
Al Viro59551022016-01-22 15:40:57 -05001725 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001726
Taehee Yoo587c0a42015-04-21 15:59:12 +09001727 if (mode & FALLOC_FL_PUNCH_HOLE) {
1728 if (offset >= inode->i_size)
1729 goto out;
1730
Chao Yua66c7b22013-11-22 16:52:50 +08001731 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001732 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1733 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001734 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1735 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001736 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1737 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001738 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001739 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001740 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001741
Namjae Jeon3af60a42012-12-30 14:52:37 +09001742 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001743 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001744 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001745 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001746 }
Chao Yu3375f692014-01-28 10:29:26 +08001747
Taehee Yoo587c0a42015-04-21 15:59:12 +09001748out:
Al Viro59551022016-01-22 15:40:57 -05001749 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001750
Namjae Jeonc01e2852013-04-23 17:00:52 +09001751 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001752 return ret;
1753}
1754
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001755static int f2fs_release_file(struct inode *inode, struct file *filp)
1756{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001757 /*
1758 * f2fs_relase_file is called at every close calls. So we should
1759 * not drop any inmemory pages by close called by other process.
1760 */
1761 if (!(filp->f_mode & FMODE_WRITE) ||
1762 atomic_read(&inode->i_writecount) != 1)
1763 return 0;
1764
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001765 /* some remained atomic pages should discarded */
1766 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001767 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001768 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001769 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001770 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001771 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001772 clear_inode_flag(inode, FI_VOLATILE_FILE);
1773 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001774 }
1775 return 0;
1776}
1777
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001778static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001779{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001780 struct inode *inode = file_inode(file);
1781
1782 /*
1783 * If the process doing a transaction is crashed, we should do
1784 * roll-back. Otherwise, other reader/write can see corrupted database
1785 * until all the writers close its file. Since this should be done
1786 * before dropping file lock, it needs to do in ->flush.
1787 */
1788 if (f2fs_is_atomic_file(inode) &&
1789 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001790 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001791 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001792}
1793
Eric Biggers36098552019-06-04 22:59:04 -07001794static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001795{
1796 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001797 u32 masked_flags = fi->i_flags & mask;
1798
1799 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
Chao Yu2c1d0302017-07-29 00:32:52 +08001800
1801 /* Is it quota file? Do not allow user to mess with it */
1802 if (IS_NOQUOTA(inode))
1803 return -EPERM;
1804
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001805 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001806 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1807 return -EOPNOTSUPP;
1808 if (!f2fs_empty_dir(inode))
1809 return -ENOTEMPTY;
1810 }
1811
Chao Yu4c8ff702019-11-01 18:07:14 +08001812 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1813 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1814 return -EOPNOTSUPP;
1815 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1816 return -EINVAL;
1817 }
1818
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001819 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
Chao Yuaa576972020-03-27 18:29:51 +08001820 if (masked_flags & F2FS_COMPR_FL) {
Daeho Jeong78134d02020-09-08 11:44:11 +09001821 if (!f2fs_disable_compressed_file(inode))
Chao Yu2536ac62020-03-10 20:50:09 +08001822 return -EINVAL;
1823 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001824 if (iflags & F2FS_NOCOMP_FL)
1825 return -EINVAL;
1826 if (iflags & F2FS_COMPR_FL) {
Chao Yu4c8ff702019-11-01 18:07:14 +08001827 if (!f2fs_may_compress(inode))
1828 return -EINVAL;
Chao Yu519a5a22020-09-18 11:03:49 +08001829 if (S_ISREG(inode->i_mode) && inode->i_size)
1830 return -EINVAL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001831
1832 set_compress_context(inode);
1833 }
1834 }
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001835 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1836 if (masked_flags & F2FS_COMPR_FL)
Chao Yu4c8ff702019-11-01 18:07:14 +08001837 return -EINVAL;
1838 }
1839
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001840 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001841 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1842 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001843
Chao Yu59c84402018-04-03 15:08:17 +08001844 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001845 set_inode_flag(inode, FI_PROJ_INHERIT);
1846 else
1847 clear_inode_flag(inode, FI_PROJ_INHERIT);
1848
1849 inode->i_ctime = current_time(inode);
1850 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001851 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001852 return 0;
1853}
1854
Eric Biggers36098552019-06-04 22:59:04 -07001855/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1856
1857/*
1858 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1859 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1860 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1861 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1862 */
1863
1864static const struct {
1865 u32 iflag;
1866 u32 fsflag;
1867} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001868 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001869 { F2FS_SYNC_FL, FS_SYNC_FL },
1870 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1871 { F2FS_APPEND_FL, FS_APPEND_FL },
1872 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1873 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001874 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001875 { F2FS_INDEX_FL, FS_INDEX_FL },
1876 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1877 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001878 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001879};
1880
1881#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001882 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001883 FS_SYNC_FL | \
1884 FS_IMMUTABLE_FL | \
1885 FS_APPEND_FL | \
1886 FS_NODUMP_FL | \
1887 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001888 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001889 FS_INDEX_FL | \
1890 FS_DIRSYNC_FL | \
1891 FS_PROJINHERIT_FL | \
1892 FS_ENCRYPT_FL | \
1893 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001894 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001895 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001896 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001897
1898#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001899 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001900 FS_SYNC_FL | \
1901 FS_IMMUTABLE_FL | \
1902 FS_APPEND_FL | \
1903 FS_NODUMP_FL | \
1904 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001905 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001906 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001907 FS_PROJINHERIT_FL | \
1908 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001909
1910/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1911static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1912{
1913 u32 fsflags = 0;
1914 int i;
1915
1916 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1917 if (iflags & f2fs_fsflags_map[i].iflag)
1918 fsflags |= f2fs_fsflags_map[i].fsflag;
1919
1920 return fsflags;
1921}
1922
1923/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1924static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1925{
1926 u32 iflags = 0;
1927 int i;
1928
1929 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1930 if (fsflags & f2fs_fsflags_map[i].fsflag)
1931 iflags |= f2fs_fsflags_map[i].iflag;
1932
1933 return iflags;
1934}
1935
1936static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1937{
1938 struct inode *inode = file_inode(filp);
1939 struct f2fs_inode_info *fi = F2FS_I(inode);
1940 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1941
1942 if (IS_ENCRYPTED(inode))
1943 fsflags |= FS_ENCRYPT_FL;
Eric Biggers95ae2512019-07-22 09:26:24 -07001944 if (IS_VERITY(inode))
1945 fsflags |= FS_VERITY_FL;
Eric Biggers36098552019-06-04 22:59:04 -07001946 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1947 fsflags |= FS_INLINE_DATA_FL;
1948 if (is_inode_flag_set(inode, FI_PIN_FILE))
1949 fsflags |= FS_NOCOW_FL;
1950
1951 fsflags &= F2FS_GETTABLE_FS_FL;
1952
1953 return put_user(fsflags, (int __user *)arg);
1954}
1955
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001956static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1957{
1958 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001959 struct f2fs_inode_info *fi = F2FS_I(inode);
1960 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001961 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001962 int ret;
1963
Chao Yu7fb17fe2016-05-09 19:56:32 +08001964 if (!inode_owner_or_capable(inode))
1965 return -EACCES;
1966
Eric Biggers36098552019-06-04 22:59:04 -07001967 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001968 return -EFAULT;
1969
Eric Biggers36098552019-06-04 22:59:04 -07001970 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1971 return -EOPNOTSUPP;
1972 fsflags &= F2FS_SETTABLE_FS_FL;
1973
1974 iflags = f2fs_fsflags_to_iflags(fsflags);
1975 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1976 return -EOPNOTSUPP;
1977
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001978 ret = mnt_want_write_file(filp);
1979 if (ret)
1980 return ret;
1981
Al Viro59551022016-01-22 15:40:57 -05001982 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001983
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001984 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1985 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1986 if (ret)
1987 goto out;
1988
Eric Biggers36098552019-06-04 22:59:04 -07001989 ret = f2fs_setflags_common(inode, iflags,
1990 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001991out:
Chao Yua72d4b92017-05-03 23:59:13 +08001992 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001993 mnt_drop_write_file(filp);
1994 return ret;
1995}
1996
Chao Yud49f3e82015-01-23 20:36:04 +08001997static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1998{
1999 struct inode *inode = file_inode(filp);
2000
2001 return put_user(inode->i_generation, (int __user *)arg);
2002}
2003
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002004static int f2fs_ioc_start_atomic_write(struct file *filp)
2005{
2006 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002007 struct f2fs_inode_info *fi = F2FS_I(inode);
2008 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08002009 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002010
2011 if (!inode_owner_or_capable(inode))
2012 return -EACCES;
2013
Jaegeuk Kime8118982017-03-17 10:04:15 +08002014 if (!S_ISREG(inode->i_mode))
2015 return -EINVAL;
2016
Chao Yu038d0692019-07-25 22:39:11 +08002017 if (filp->f_flags & O_DIRECT)
2018 return -EINVAL;
2019
Chao Yu7fb17fe2016-05-09 19:56:32 +08002020 ret = mnt_want_write_file(filp);
2021 if (ret)
2022 return ret;
2023
Chao Yu0fac5582016-05-09 19:56:33 +08002024 inode_lock(inode);
2025
Chao Yu4c8ff702019-11-01 18:07:14 +08002026 f2fs_disable_compressed_file(inode);
2027
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002028 if (f2fs_is_atomic_file(inode)) {
2029 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2030 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002031 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002032 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002033
Chao Yuf4c9c742015-07-17 18:06:35 +08002034 ret = f2fs_convert_inline_inode(inode);
2035 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002036 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002037
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002038 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2039
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002040 /*
2041 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2042 * f2fs_is_atomic_file.
2043 */
2044 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002045 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2046 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002047 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002048 if (ret) {
2049 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002050 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002051 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002052
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002053 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2054 if (list_empty(&fi->inmem_ilist))
2055 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302056 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002057 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2058
2059 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002060 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002061 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002062 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002063
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002064 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002065 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002066 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002067out:
Chao Yu0fac5582016-05-09 19:56:33 +08002068 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002069 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002070 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002071}
2072
2073static int f2fs_ioc_commit_atomic_write(struct file *filp)
2074{
2075 struct inode *inode = file_inode(filp);
2076 int ret;
2077
2078 if (!inode_owner_or_capable(inode))
2079 return -EACCES;
2080
2081 ret = mnt_want_write_file(filp);
2082 if (ret)
2083 return ret;
2084
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002085 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002086
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002087 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002088
Chao Yub169c3c2018-04-18 17:45:02 +08002089 if (f2fs_is_volatile_file(inode)) {
2090 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002091 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002092 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002093
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002094 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002095 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002096 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002097 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002098
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002099 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002100 if (!ret)
2101 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002102 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002103 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002104 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002105err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002106 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2107 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2108 ret = -EINVAL;
2109 }
Chao Yu0fac5582016-05-09 19:56:33 +08002110 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002111 mnt_drop_write_file(filp);
2112 return ret;
2113}
2114
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002115static int f2fs_ioc_start_volatile_write(struct file *filp)
2116{
2117 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002118 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002119
2120 if (!inode_owner_or_capable(inode))
2121 return -EACCES;
2122
Chao Yu8ff09712017-03-17 15:43:57 +08002123 if (!S_ISREG(inode->i_mode))
2124 return -EINVAL;
2125
Chao Yu7fb17fe2016-05-09 19:56:32 +08002126 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002127 if (ret)
2128 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002129
Chao Yu0fac5582016-05-09 19:56:33 +08002130 inode_lock(inode);
2131
Chao Yu7fb17fe2016-05-09 19:56:32 +08002132 if (f2fs_is_volatile_file(inode))
2133 goto out;
2134
2135 ret = f2fs_convert_inline_inode(inode);
2136 if (ret)
2137 goto out;
2138
Chao Yu648d50b2017-03-22 17:23:45 +08002139 stat_inc_volatile_write(inode);
2140 stat_update_max_volatile_write(inode);
2141
Jaegeuk Kim91942322016-05-20 10:13:22 -07002142 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002143 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002144out:
Chao Yu0fac5582016-05-09 19:56:33 +08002145 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002146 mnt_drop_write_file(filp);
2147 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002148}
2149
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002150static int f2fs_ioc_release_volatile_write(struct file *filp)
2151{
2152 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002153 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002154
2155 if (!inode_owner_or_capable(inode))
2156 return -EACCES;
2157
Chao Yu7fb17fe2016-05-09 19:56:32 +08002158 ret = mnt_want_write_file(filp);
2159 if (ret)
2160 return ret;
2161
Chao Yu0fac5582016-05-09 19:56:33 +08002162 inode_lock(inode);
2163
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002164 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002165 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002166
Chao Yu7fb17fe2016-05-09 19:56:32 +08002167 if (!f2fs_is_first_block_written(inode)) {
2168 ret = truncate_partial_data_page(inode, 0, true);
2169 goto out;
2170 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002171
Chao Yu7fb17fe2016-05-09 19:56:32 +08002172 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2173out:
Chao Yu0fac5582016-05-09 19:56:33 +08002174 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002175 mnt_drop_write_file(filp);
2176 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002177}
2178
2179static int f2fs_ioc_abort_volatile_write(struct file *filp)
2180{
2181 struct inode *inode = file_inode(filp);
2182 int ret;
2183
2184 if (!inode_owner_or_capable(inode))
2185 return -EACCES;
2186
2187 ret = mnt_want_write_file(filp);
2188 if (ret)
2189 return ret;
2190
Chao Yu0fac5582016-05-09 19:56:33 +08002191 inode_lock(inode);
2192
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002193 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002194 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002195 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002196 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002197 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002198 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002199 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002200
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002201 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2202
Chao Yu0fac5582016-05-09 19:56:33 +08002203 inode_unlock(inode);
2204
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002205 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002206 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002207 return ret;
2208}
2209
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002210static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2211{
2212 struct inode *inode = file_inode(filp);
2213 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2214 struct super_block *sb = sbi->sb;
2215 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002216 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002217
2218 if (!capable(CAP_SYS_ADMIN))
2219 return -EPERM;
2220
2221 if (get_user(in, (__u32 __user *)arg))
2222 return -EFAULT;
2223
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302224 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2225 ret = mnt_want_write_file(filp);
Chao Yu86264412020-06-08 20:03:16 +08002226 if (ret) {
2227 if (ret == -EROFS) {
2228 ret = 0;
2229 f2fs_stop_checkpoint(sbi, false);
2230 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2231 trace_f2fs_shutdown(sbi, in, ret);
2232 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302233 return ret;
Chao Yu86264412020-06-08 20:03:16 +08002234 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302235 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002236
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002237 switch (in) {
2238 case F2FS_GOING_DOWN_FULLSYNC:
Christoph Hellwig040f04b2020-11-24 11:54:06 +01002239 ret = freeze_bdev(sb->s_bdev);
2240 if (ret)
Chao Yud027c482018-01-17 22:28:52 +08002241 goto out;
Christoph Hellwig040f04b2020-11-24 11:54:06 +01002242 f2fs_stop_checkpoint(sbi, false);
2243 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2244 thaw_bdev(sb->s_bdev);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002245 break;
2246 case F2FS_GOING_DOWN_METASYNC:
2247 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002248 ret = f2fs_sync_fs(sb, 1);
2249 if (ret)
2250 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002251 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002252 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002253 break;
2254 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002255 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002256 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002257 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002258 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002259 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002260 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002261 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002262 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002263 case F2FS_GOING_DOWN_NEED_FSCK:
2264 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002265 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2266 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002267 /* do checkpoint only */
2268 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002269 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002270 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002271 ret = -EINVAL;
2272 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002273 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002274
Chao Yu4d57b862018-05-30 00:20:41 +08002275 f2fs_stop_gc_thread(sbi);
2276 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002277
Chao Yu4d57b862018-05-30 00:20:41 +08002278 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002279 clear_opt(sbi, DISCARD);
2280
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002281 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002282out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302283 if (in != F2FS_GOING_DOWN_FULLSYNC)
2284 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002285
2286 trace_f2fs_shutdown(sbi, in, ret);
2287
Chao Yu7fb17fe2016-05-09 19:56:32 +08002288 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002289}
2290
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002291static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2292{
2293 struct inode *inode = file_inode(filp);
2294 struct super_block *sb = inode->i_sb;
2295 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2296 struct fstrim_range range;
2297 int ret;
2298
2299 if (!capable(CAP_SYS_ADMIN))
2300 return -EPERM;
2301
Chao Yu7d20c8a2018-09-04 03:52:17 +08002302 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002303 return -EOPNOTSUPP;
2304
2305 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2306 sizeof(range)))
2307 return -EFAULT;
2308
Chao Yu7fb17fe2016-05-09 19:56:32 +08002309 ret = mnt_want_write_file(filp);
2310 if (ret)
2311 return ret;
2312
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002313 range.minlen = max((unsigned int)range.minlen,
2314 q->limits.discard_granularity);
2315 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002316 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002317 if (ret < 0)
2318 return ret;
2319
2320 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2321 sizeof(range)))
2322 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002323 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002324 return 0;
2325}
2326
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002327static bool uuid_is_nonzero(__u8 u[16])
2328{
2329 int i;
2330
2331 for (i = 0; i < 16; i++)
2332 if (u[i])
2333 return true;
2334 return false;
2335}
2336
2337static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2338{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002339 struct inode *inode = file_inode(filp);
2340
Chao Yu7beb01f2018-10-24 18:34:26 +08002341 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002342 return -EOPNOTSUPP;
2343
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002344 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002345
Eric Biggersdb717d82016-11-26 19:07:49 -05002346 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002347}
2348
2349static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2350{
Chao Yu7beb01f2018-10-24 18:34:26 +08002351 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002352 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002353 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002354}
2355
2356static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2357{
2358 struct inode *inode = file_inode(filp);
2359 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2360 int err;
2361
Chao Yu7beb01f2018-10-24 18:34:26 +08002362 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002363 return -EOPNOTSUPP;
2364
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002365 err = mnt_want_write_file(filp);
2366 if (err)
2367 return err;
2368
Chao Yu846ae672018-02-26 22:04:13 +08002369 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002370
2371 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2372 goto got_it;
2373
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002374 /* update superblock with uuid */
2375 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2376
Chao Yuc5bda1c2015-06-08 13:28:03 +08002377 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002378 if (err) {
2379 /* undo new data */
2380 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002381 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002382 }
2383got_it:
2384 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2385 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002386 err = -EFAULT;
2387out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002388 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002389 mnt_drop_write_file(filp);
2390 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002391}
2392
Eric Biggers8ce589c2019-08-04 19:35:48 -07002393static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2394 unsigned long arg)
2395{
2396 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2397 return -EOPNOTSUPP;
2398
2399 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2400}
2401
2402static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2403{
2404 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2405 return -EOPNOTSUPP;
2406
2407 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2408}
2409
2410static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2411{
2412 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2413 return -EOPNOTSUPP;
2414
2415 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2416}
2417
2418static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2419 unsigned long arg)
2420{
2421 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2422 return -EOPNOTSUPP;
2423
2424 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2425}
2426
2427static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2428 unsigned long arg)
2429{
2430 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2431 return -EOPNOTSUPP;
2432
2433 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2434}
2435
Eric Biggersee446e12020-03-14 13:50:51 -07002436static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2437{
2438 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2439 return -EOPNOTSUPP;
2440
2441 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2442}
2443
Chao Yuc1c1b582015-07-10 18:08:10 +08002444static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2445{
2446 struct inode *inode = file_inode(filp);
2447 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002448 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002449 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002450
2451 if (!capable(CAP_SYS_ADMIN))
2452 return -EPERM;
2453
Chao Yud530d4d2015-10-05 22:22:44 +08002454 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002455 return -EFAULT;
2456
Chao Yud530d4d2015-10-05 22:22:44 +08002457 if (f2fs_readonly(sbi->sb))
2458 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002459
Chao Yu7fb17fe2016-05-09 19:56:32 +08002460 ret = mnt_want_write_file(filp);
2461 if (ret)
2462 return ret;
2463
Chao Yud530d4d2015-10-05 22:22:44 +08002464 if (!sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002465 if (!down_write_trylock(&sbi->gc_lock)) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002466 ret = -EBUSY;
2467 goto out;
2468 }
Chao Yud530d4d2015-10-05 22:22:44 +08002469 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002470 down_write(&sbi->gc_lock);
Chao Yuc1c1b582015-07-10 18:08:10 +08002471 }
2472
Jaegeuk Kime066b832017-04-13 15:17:00 -07002473 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002474out:
2475 mnt_drop_write_file(filp);
2476 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002477}
2478
Chao Yu34178b1b2020-11-10 09:24:37 +08002479static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002480{
Chao Yu34178b1b2020-11-10 09:24:37 +08002481 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002482 u64 end;
2483 int ret;
2484
2485 if (!capable(CAP_SYS_ADMIN))
2486 return -EPERM;
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002487 if (f2fs_readonly(sbi->sb))
2488 return -EROFS;
2489
Chao Yu34178b1b2020-11-10 09:24:37 +08002490 end = range->start + range->len;
2491 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302492 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002493 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002494
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002495 ret = mnt_want_write_file(filp);
2496 if (ret)
2497 return ret;
2498
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002499do_more:
Chao Yu34178b1b2020-11-10 09:24:37 +08002500 if (!range->sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002501 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002502 ret = -EBUSY;
2503 goto out;
2504 }
2505 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002506 down_write(&sbi->gc_lock);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002507 }
2508
Chao Yu34178b1b2020-11-10 09:24:37 +08002509 ret = f2fs_gc(sbi, range->sync, true, GET_SEGNO(sbi, range->start));
Qilong Zhang97767502020-06-28 19:23:03 +08002510 if (ret) {
2511 if (ret == -EBUSY)
2512 ret = -EAGAIN;
2513 goto out;
2514 }
Chao Yu34178b1b2020-11-10 09:24:37 +08002515 range->start += BLKS_PER_SEC(sbi);
2516 if (range->start <= end)
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002517 goto do_more;
2518out:
2519 mnt_drop_write_file(filp);
2520 return ret;
2521}
2522
Chao Yu34178b1b2020-11-10 09:24:37 +08002523static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2524{
2525 struct f2fs_gc_range range;
2526
2527 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2528 sizeof(range)))
2529 return -EFAULT;
2530 return __f2fs_ioc_gc_range(filp, &range);
2531}
2532
Chao Yu059c0642018-07-17 20:41:49 +08002533static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002534{
2535 struct inode *inode = file_inode(filp);
2536 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002537 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002538
2539 if (!capable(CAP_SYS_ADMIN))
2540 return -EPERM;
2541
2542 if (f2fs_readonly(sbi->sb))
2543 return -EROFS;
2544
Daniel Rosenberg43549942018-08-20 19:21:43 -07002545 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002546 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002547 return -EINVAL;
2548 }
2549
Chao Yu7fb17fe2016-05-09 19:56:32 +08002550 ret = mnt_want_write_file(filp);
2551 if (ret)
2552 return ret;
2553
2554 ret = f2fs_sync_fs(sbi->sb, 1);
2555
2556 mnt_drop_write_file(filp);
2557 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002558}
2559
Chao Yud323d002015-10-27 09:53:45 +08002560static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2561 struct file *filp,
2562 struct f2fs_defragment *range)
2563{
2564 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002565 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002566 .m_seg_type = NO_CHECK_TYPE ,
2567 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002568 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002569 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002570 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002571 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002572 block_t blk_end = 0;
2573 bool fragmented = false;
2574 int err;
2575
2576 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002577 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002578 return -EINVAL;
2579
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002580 pg_start = range->start >> PAGE_SHIFT;
2581 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002582
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002583 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002584
Al Viro59551022016-01-22 15:40:57 -05002585 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002586
2587 /* writeback all dirty pages in the range */
2588 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002589 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002590 if (err)
2591 goto out;
2592
2593 /*
2594 * lookup mapping info in extent cache, skip defragmenting if physical
2595 * block addresses are continuous.
2596 */
2597 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2598 if (ei.fofs + ei.len >= pg_end)
2599 goto out;
2600 }
2601
2602 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002603 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002604
2605 /*
2606 * lookup mapping info in dnode page cache, skip defragmenting if all
2607 * physical block addresses are continuous even if there are hole(s)
2608 * in logical blocks.
2609 */
2610 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002611 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002612 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002613 if (err)
2614 goto out;
2615
2616 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002617 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002618 continue;
2619 }
2620
Chao Yu25a912e2018-01-10 18:18:51 +08002621 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002622 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002623
2624 /* record total count of block that we're going to move */
2625 total += map.m_len;
2626
Chao Yud323d002015-10-27 09:53:45 +08002627 blk_end = map.m_pblk + map.m_len;
2628
2629 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002630 }
2631
Chao Yud3a1a0e2019-07-29 23:02:29 +08002632 if (!fragmented) {
2633 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002634 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002635 }
Chao Yud323d002015-10-27 09:53:45 +08002636
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002637 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002638
2639 /*
2640 * make sure there are enough free section for LFS allocation, this can
2641 * avoid defragment running in SSR mode when free section are allocated
2642 * intensively
2643 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002644 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002645 err = -EAGAIN;
2646 goto out;
2647 }
2648
Chao Yu25a912e2018-01-10 18:18:51 +08002649 map.m_lblk = pg_start;
2650 map.m_len = pg_end - pg_start;
2651 total = 0;
2652
Chao Yud323d002015-10-27 09:53:45 +08002653 while (map.m_lblk < pg_end) {
2654 pgoff_t idx;
2655 int cnt = 0;
2656
2657do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002658 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002659 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002660 if (err)
2661 goto clear_out;
2662
2663 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002664 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002665 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002666 }
2667
Jaegeuk Kim91942322016-05-20 10:13:22 -07002668 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002669
2670 idx = map.m_lblk;
2671 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2672 struct page *page;
2673
Chao Yu4d57b862018-05-30 00:20:41 +08002674 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002675 if (IS_ERR(page)) {
2676 err = PTR_ERR(page);
2677 goto clear_out;
2678 }
2679
2680 set_page_dirty(page);
2681 f2fs_put_page(page, 1);
2682
2683 idx++;
2684 cnt++;
2685 total++;
2686 }
2687
2688 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002689check:
2690 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002691 goto do_map;
2692
Jaegeuk Kim91942322016-05-20 10:13:22 -07002693 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002694
2695 err = filemap_fdatawrite(inode->i_mapping);
2696 if (err)
2697 goto out;
2698 }
2699clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002700 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002701out:
Al Viro59551022016-01-22 15:40:57 -05002702 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002703 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002704 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002705 return err;
2706}
2707
2708static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2709{
2710 struct inode *inode = file_inode(filp);
2711 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2712 struct f2fs_defragment range;
2713 int err;
2714
2715 if (!capable(CAP_SYS_ADMIN))
2716 return -EPERM;
2717
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002718 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002719 return -EINVAL;
2720
Kinglong Meed7563862017-03-10 17:55:07 +08002721 if (f2fs_readonly(sbi->sb))
2722 return -EROFS;
2723
2724 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2725 sizeof(range)))
2726 return -EFAULT;
2727
2728 /* verify alignment of offset & size */
2729 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2730 return -EINVAL;
2731
2732 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2733 sbi->max_file_blocks))
2734 return -EINVAL;
2735
Chao Yud323d002015-10-27 09:53:45 +08002736 err = mnt_want_write_file(filp);
2737 if (err)
2738 return err;
2739
Chao Yud323d002015-10-27 09:53:45 +08002740 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002741 mnt_drop_write_file(filp);
2742
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002743 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002744 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002745 return err;
Chao Yud323d002015-10-27 09:53:45 +08002746
2747 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2748 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002749 return -EFAULT;
2750
2751 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002752}
2753
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002754static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2755 struct file *file_out, loff_t pos_out, size_t len)
2756{
2757 struct inode *src = file_inode(file_in);
2758 struct inode *dst = file_inode(file_out);
2759 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2760 size_t olen = len, dst_max_i_size = 0;
2761 size_t dst_osize;
2762 int ret;
2763
2764 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2765 src->i_sb != dst->i_sb)
2766 return -EXDEV;
2767
2768 if (unlikely(f2fs_readonly(src->i_sb)))
2769 return -EROFS;
2770
Chao Yufe8494b2016-08-04 20:13:02 +08002771 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2772 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002773
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302774 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002775 return -EOPNOTSUPP;
2776
Dan Robertsonaad13832020-08-30 21:45:23 +00002777 if (pos_out < 0 || pos_in < 0)
2778 return -EINVAL;
2779
Fan Lid95fd912016-09-13 11:35:42 +08002780 if (src == dst) {
2781 if (pos_in == pos_out)
2782 return 0;
2783 if (pos_out > pos_in && pos_out < pos_in + len)
2784 return -EINVAL;
2785 }
2786
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002787 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002788 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002789 ret = -EBUSY;
2790 if (!inode_trylock(dst))
2791 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002792 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002793
2794 ret = -EINVAL;
2795 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2796 goto out_unlock;
2797 if (len == 0)
2798 olen = len = src->i_size - pos_in;
2799 if (pos_in + len == src->i_size)
2800 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2801 if (len == 0) {
2802 ret = 0;
2803 goto out_unlock;
2804 }
2805
2806 dst_osize = dst->i_size;
2807 if (pos_out + olen > dst->i_size)
2808 dst_max_i_size = pos_out + olen;
2809
2810 /* verify the end result is block aligned */
2811 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2812 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2813 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2814 goto out_unlock;
2815
2816 ret = f2fs_convert_inline_inode(src);
2817 if (ret)
2818 goto out_unlock;
2819
2820 ret = f2fs_convert_inline_inode(dst);
2821 if (ret)
2822 goto out_unlock;
2823
2824 /* write out all dirty pages from offset */
2825 ret = filemap_write_and_wait_range(src->i_mapping,
2826 pos_in, pos_in + len);
2827 if (ret)
2828 goto out_unlock;
2829
2830 ret = filemap_write_and_wait_range(dst->i_mapping,
2831 pos_out, pos_out + len);
2832 if (ret)
2833 goto out_unlock;
2834
2835 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002836
2837 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2838 if (src != dst) {
2839 ret = -EBUSY;
2840 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2841 goto out_src;
2842 }
2843
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002844 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002845 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2846 pos_out >> F2FS_BLKSIZE_BITS,
2847 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002848
2849 if (!ret) {
2850 if (dst_max_i_size)
2851 f2fs_i_size_write(dst, dst_max_i_size);
2852 else if (dst_osize != dst->i_size)
2853 f2fs_i_size_write(dst, dst_osize);
2854 }
2855 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002856
2857 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002858 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002859out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002860 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002861out_unlock:
2862 if (src != dst)
2863 inode_unlock(dst);
2864out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002865 inode_unlock(src);
2866 return ret;
2867}
2868
Chao Yu34178b1b2020-11-10 09:24:37 +08002869static int __f2fs_ioc_move_range(struct file *filp,
2870 struct f2fs_move_range *range)
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002871{
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002872 struct fd dst;
2873 int err;
2874
2875 if (!(filp->f_mode & FMODE_READ) ||
2876 !(filp->f_mode & FMODE_WRITE))
2877 return -EBADF;
2878
Chao Yu34178b1b2020-11-10 09:24:37 +08002879 dst = fdget(range->dst_fd);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002880 if (!dst.file)
2881 return -EBADF;
2882
2883 if (!(dst.file->f_mode & FMODE_WRITE)) {
2884 err = -EBADF;
2885 goto err_out;
2886 }
2887
2888 err = mnt_want_write_file(filp);
2889 if (err)
2890 goto err_out;
2891
Chao Yu34178b1b2020-11-10 09:24:37 +08002892 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2893 range->pos_out, range->len);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002894
2895 mnt_drop_write_file(filp);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002896err_out:
2897 fdput(dst);
2898 return err;
2899}
2900
Chao Yu34178b1b2020-11-10 09:24:37 +08002901static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2902{
2903 struct f2fs_move_range range;
2904
2905 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2906 sizeof(range)))
2907 return -EFAULT;
2908 return __f2fs_ioc_move_range(filp, &range);
2909}
2910
Jaegeuk Kime066b832017-04-13 15:17:00 -07002911static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2912{
2913 struct inode *inode = file_inode(filp);
2914 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2915 struct sit_info *sm = SIT_I(sbi);
2916 unsigned int start_segno = 0, end_segno = 0;
2917 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2918 struct f2fs_flush_device range;
2919 int ret;
2920
2921 if (!capable(CAP_SYS_ADMIN))
2922 return -EPERM;
2923
2924 if (f2fs_readonly(sbi->sb))
2925 return -EROFS;
2926
Daniel Rosenberg43549942018-08-20 19:21:43 -07002927 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2928 return -EINVAL;
2929
Jaegeuk Kime066b832017-04-13 15:17:00 -07002930 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2931 sizeof(range)))
2932 return -EFAULT;
2933
Damien Le Moal09168782019-03-16 09:13:06 +09002934 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002935 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002936 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2937 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002938 return -EINVAL;
2939 }
2940
2941 ret = mnt_want_write_file(filp);
2942 if (ret)
2943 return ret;
2944
2945 if (range.dev_num != 0)
2946 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2947 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2948
2949 start_segno = sm->last_victim[FLUSH_DEVICE];
2950 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2951 start_segno = dev_start_segno;
2952 end_segno = min(start_segno + range.segments, dev_end_segno);
2953
2954 while (start_segno < end_segno) {
Chao Yufb24fea2020-01-14 19:36:50 +08002955 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002956 ret = -EBUSY;
2957 goto out;
2958 }
2959 sm->last_victim[GC_CB] = end_segno + 1;
2960 sm->last_victim[GC_GREEDY] = end_segno + 1;
2961 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2962 ret = f2fs_gc(sbi, true, true, start_segno);
2963 if (ret == -EAGAIN)
2964 ret = 0;
2965 else if (ret < 0)
2966 break;
2967 start_segno++;
2968 }
2969out:
2970 mnt_drop_write_file(filp);
2971 return ret;
2972}
2973
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002974static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2975{
2976 struct inode *inode = file_inode(filp);
2977 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2978
2979 /* Must validate to set it with SQLite behavior in Android. */
2980 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2981
2982 return put_user(sb_feature, (u32 __user *)arg);
2983}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002984
Chao Yu2c1d0302017-07-29 00:32:52 +08002985#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002986int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2987{
2988 struct dquot *transfer_to[MAXQUOTAS] = {};
2989 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2990 struct super_block *sb = sbi->sb;
2991 int err = 0;
2992
2993 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2994 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2995 err = __dquot_transfer(inode, transfer_to);
2996 if (err)
2997 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2998 dqput(transfer_to[PRJQUOTA]);
2999 }
3000 return err;
3001}
3002
Chao Yu2c1d0302017-07-29 00:32:52 +08003003static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3004{
3005 struct inode *inode = file_inode(filp);
3006 struct f2fs_inode_info *fi = F2FS_I(inode);
3007 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003008 struct page *ipage;
3009 kprojid_t kprojid;
3010 int err;
3011
Chao Yu7beb01f2018-10-24 18:34:26 +08003012 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08003013 if (projid != F2FS_DEF_PROJID)
3014 return -EOPNOTSUPP;
3015 else
3016 return 0;
3017 }
3018
3019 if (!f2fs_has_extra_attr(inode))
3020 return -EOPNOTSUPP;
3021
3022 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3023
3024 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3025 return 0;
3026
Chao Yu2c1d0302017-07-29 00:32:52 +08003027 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003028 /* Is it quota file? Do not allow user to mess with it */
3029 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003030 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003031
Chao Yu4d57b862018-05-30 00:20:41 +08003032 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003033 if (IS_ERR(ipage))
3034 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003035
3036 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3037 i_projid)) {
3038 err = -EOVERFLOW;
3039 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003040 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003041 }
3042 f2fs_put_page(ipage, 1);
3043
Chao Yuc22aecd2018-04-21 17:53:52 +08003044 err = dquot_initialize(inode);
3045 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003046 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003047
Chao Yu78130812018-09-25 15:36:02 +08003048 f2fs_lock_op(sbi);
3049 err = f2fs_transfer_project_quota(inode, kprojid);
3050 if (err)
3051 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003052
3053 F2FS_I(inode)->i_projid = kprojid;
3054 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003055 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003056out_unlock:
3057 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003058 return err;
3059}
3060#else
Chao Yu78130812018-09-25 15:36:02 +08003061int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3062{
3063 return 0;
3064}
3065
Chao Yu2c1d0302017-07-29 00:32:52 +08003066static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3067{
3068 if (projid != F2FS_DEF_PROJID)
3069 return -EOPNOTSUPP;
3070 return 0;
3071}
3072#endif
3073
Eric Biggers36098552019-06-04 22:59:04 -07003074/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08003075
Eric Biggers36098552019-06-04 22:59:04 -07003076/*
3077 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3078 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3079 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3080 */
3081
3082static const struct {
3083 u32 iflag;
3084 u32 xflag;
3085} f2fs_xflags_map[] = {
3086 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3087 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3088 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3089 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3090 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3091 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3092};
3093
3094#define F2FS_SUPPORTED_XFLAGS ( \
3095 FS_XFLAG_SYNC | \
3096 FS_XFLAG_IMMUTABLE | \
3097 FS_XFLAG_APPEND | \
3098 FS_XFLAG_NODUMP | \
3099 FS_XFLAG_NOATIME | \
3100 FS_XFLAG_PROJINHERIT)
3101
3102/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3103static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3104{
3105 u32 xflags = 0;
3106 int i;
3107
3108 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3109 if (iflags & f2fs_xflags_map[i].iflag)
3110 xflags |= f2fs_xflags_map[i].xflag;
3111
Chao Yu2c1d0302017-07-29 00:32:52 +08003112 return xflags;
3113}
3114
Eric Biggers36098552019-06-04 22:59:04 -07003115/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3116static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003117{
Eric Biggers36098552019-06-04 22:59:04 -07003118 u32 iflags = 0;
3119 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08003120
Eric Biggers36098552019-06-04 22:59:04 -07003121 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3122 if (xflags & f2fs_xflags_map[i].xflag)
3123 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08003124
3125 return iflags;
3126}
3127
Eric Biggers6fc93c42019-07-01 13:26:29 -07003128static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3129{
3130 struct f2fs_inode_info *fi = F2FS_I(inode);
3131
3132 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3133
3134 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3135 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3136}
3137
Chao Yu2c1d0302017-07-29 00:32:52 +08003138static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3139{
3140 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08003141 struct fsxattr fa;
3142
Eric Biggers6fc93c42019-07-01 13:26:29 -07003143 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08003144
3145 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3146 return -EFAULT;
3147 return 0;
3148}
3149
3150static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3151{
3152 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003153 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07003154 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003155 int err;
3156
3157 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3158 return -EFAULT;
3159
3160 /* Make sure caller has proper permission */
3161 if (!inode_owner_or_capable(inode))
3162 return -EACCES;
3163
Eric Biggers36098552019-06-04 22:59:04 -07003164 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08003165 return -EOPNOTSUPP;
3166
Eric Biggers36098552019-06-04 22:59:04 -07003167 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3168 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003169 return -EOPNOTSUPP;
3170
3171 err = mnt_want_write_file(filp);
3172 if (err)
3173 return err;
3174
3175 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003176
3177 f2fs_fill_fsxattr(inode, &old_fa);
3178 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09003179 if (err)
3180 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07003181
Eric Biggers36098552019-06-04 22:59:04 -07003182 err = f2fs_setflags_common(inode, iflags,
3183 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08003184 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003185 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08003186
3187 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09003188out:
3189 inode_unlock(inode);
3190 mnt_drop_write_file(filp);
3191 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003192}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003193
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003194int f2fs_pin_file_control(struct inode *inode, bool inc)
3195{
3196 struct f2fs_inode_info *fi = F2FS_I(inode);
3197 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3198
3199 /* Use i_gc_failures for normal file as a risk signal. */
3200 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003201 f2fs_i_gc_failures_write(inode,
3202 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003203
Chao Yu2ef79ec2018-05-07 20:28:54 +08003204 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003205 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3206 __func__, inode->i_ino,
3207 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003208 clear_inode_flag(inode, FI_PIN_FILE);
3209 return -EAGAIN;
3210 }
3211 return 0;
3212}
3213
3214static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3215{
3216 struct inode *inode = file_inode(filp);
3217 __u32 pin;
3218 int ret = 0;
3219
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003220 if (get_user(pin, (__u32 __user *)arg))
3221 return -EFAULT;
3222
3223 if (!S_ISREG(inode->i_mode))
3224 return -EINVAL;
3225
3226 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3227 return -EROFS;
3228
3229 ret = mnt_want_write_file(filp);
3230 if (ret)
3231 return ret;
3232
3233 inode_lock(inode);
3234
Chao Yu4d57b862018-05-30 00:20:41 +08003235 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08003236 ret = -EINVAL;
3237 goto out;
3238 }
3239
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003240 if (!pin) {
3241 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003242 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003243 goto done;
3244 }
3245
3246 if (f2fs_pin_file_control(inode, false)) {
3247 ret = -EAGAIN;
3248 goto out;
3249 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003250
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003251 ret = f2fs_convert_inline_inode(inode);
3252 if (ret)
3253 goto out;
3254
Daeho Jeong78134d02020-09-08 11:44:11 +09003255 if (!f2fs_disable_compressed_file(inode)) {
Chao Yu4c8ff702019-11-01 18:07:14 +08003256 ret = -EOPNOTSUPP;
3257 goto out;
3258 }
3259
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003260 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003261 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003262done:
3263 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3264out:
3265 inode_unlock(inode);
3266 mnt_drop_write_file(filp);
3267 return ret;
3268}
3269
3270static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3271{
3272 struct inode *inode = file_inode(filp);
3273 __u32 pin = 0;
3274
3275 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003276 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003277 return put_user(pin, (u32 __user *)arg);
3278}
3279
Chao Yuc4020b22018-01-11 14:42:30 +08003280int f2fs_precache_extents(struct inode *inode)
3281{
3282 struct f2fs_inode_info *fi = F2FS_I(inode);
3283 struct f2fs_map_blocks map;
3284 pgoff_t m_next_extent;
3285 loff_t end;
3286 int err;
3287
3288 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3289 return -EOPNOTSUPP;
3290
3291 map.m_lblk = 0;
3292 map.m_next_pgofs = NULL;
3293 map.m_next_extent = &m_next_extent;
3294 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003295 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003296 end = F2FS_I_SB(inode)->max_file_blocks;
3297
3298 while (map.m_lblk < end) {
3299 map.m_len = end - map.m_lblk;
3300
Chao Yub2532c62018-04-24 10:55:28 +08003301 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003302 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003303 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003304 if (err)
3305 return err;
3306
3307 map.m_lblk = m_next_extent;
3308 }
3309
3310 return err;
3311}
3312
3313static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3314{
3315 return f2fs_precache_extents(file_inode(filp));
3316}
3317
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003318static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3319{
3320 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3321 __u64 block_count;
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003322
3323 if (!capable(CAP_SYS_ADMIN))
3324 return -EPERM;
3325
3326 if (f2fs_readonly(sbi->sb))
3327 return -EROFS;
3328
3329 if (copy_from_user(&block_count, (void __user *)arg,
3330 sizeof(block_count)))
3331 return -EFAULT;
3332
Jaegeuk Kimb4b10062020-03-31 11:43:07 -07003333 return f2fs_resize_fs(sbi, block_count);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003334}
3335
Eric Biggers95ae2512019-07-22 09:26:24 -07003336static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3337{
3338 struct inode *inode = file_inode(filp);
3339
3340 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3341
3342 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3343 f2fs_warn(F2FS_I_SB(inode),
3344 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3345 inode->i_ino);
3346 return -EOPNOTSUPP;
3347 }
3348
3349 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3350}
3351
3352static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3353{
3354 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3355 return -EOPNOTSUPP;
3356
3357 return fsverity_ioctl_measure(filp, (void __user *)arg);
3358}
3359
Eric Biggers3357af82020-07-14 15:18:12 -07003360static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003361{
3362 struct inode *inode = file_inode(filp);
3363 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3364 char *vbuf;
3365 int count;
3366 int err = 0;
3367
3368 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3369 if (!vbuf)
3370 return -ENOMEM;
3371
3372 down_read(&sbi->sb_lock);
3373 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3374 ARRAY_SIZE(sbi->raw_super->volume_name),
3375 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3376 up_read(&sbi->sb_lock);
3377
3378 if (copy_to_user((char __user *)arg, vbuf,
3379 min(FSLABEL_MAX, count)))
3380 err = -EFAULT;
3381
Chao Yuc8eb7022020-09-14 16:47:00 +08003382 kfree(vbuf);
Chao Yu4507847c2019-07-17 17:06:11 +08003383 return err;
3384}
3385
Eric Biggers3357af82020-07-14 15:18:12 -07003386static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003387{
3388 struct inode *inode = file_inode(filp);
3389 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3390 char *vbuf;
3391 int err = 0;
3392
3393 if (!capable(CAP_SYS_ADMIN))
3394 return -EPERM;
3395
3396 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3397 if (IS_ERR(vbuf))
3398 return PTR_ERR(vbuf);
3399
3400 err = mnt_want_write_file(filp);
3401 if (err)
3402 goto out;
3403
3404 down_write(&sbi->sb_lock);
3405
3406 memset(sbi->raw_super->volume_name, 0,
3407 sizeof(sbi->raw_super->volume_name));
3408 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3409 sbi->raw_super->volume_name,
3410 ARRAY_SIZE(sbi->raw_super->volume_name));
3411
3412 err = f2fs_commit_super(sbi, false);
3413
3414 up_write(&sbi->sb_lock);
3415
3416 mnt_drop_write_file(filp);
3417out:
3418 kfree(vbuf);
3419 return err;
3420}
3421
Chao Yu439dfb12020-02-21 18:09:21 +08003422static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3423{
3424 struct inode *inode = file_inode(filp);
3425 __u64 blocks;
3426
3427 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3428 return -EOPNOTSUPP;
3429
3430 if (!f2fs_compressed_file(inode))
3431 return -EINVAL;
3432
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003433 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
Chao Yu439dfb12020-02-21 18:09:21 +08003434 return put_user(blocks, (u64 __user *)arg);
3435}
3436
Chao Yuef8d5632020-03-06 15:36:09 +08003437static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3438{
3439 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3440 unsigned int released_blocks = 0;
3441 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3442 block_t blkaddr;
3443 int i;
3444
3445 for (i = 0; i < count; i++) {
3446 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3447 dn->ofs_in_node + i);
3448
3449 if (!__is_valid_data_blkaddr(blkaddr))
3450 continue;
3451 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3452 DATA_GENERIC_ENHANCE)))
3453 return -EFSCORRUPTED;
3454 }
3455
3456 while (count) {
3457 int compr_blocks = 0;
3458
3459 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3460 blkaddr = f2fs_data_blkaddr(dn);
3461
3462 if (i == 0) {
3463 if (blkaddr == COMPRESS_ADDR)
3464 continue;
3465 dn->ofs_in_node += cluster_size;
3466 goto next;
3467 }
3468
3469 if (__is_valid_data_blkaddr(blkaddr))
3470 compr_blocks++;
3471
3472 if (blkaddr != NEW_ADDR)
3473 continue;
3474
3475 dn->data_blkaddr = NULL_ADDR;
3476 f2fs_set_data_blkaddr(dn);
3477 }
3478
3479 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3480 dec_valid_block_count(sbi, dn->inode,
3481 cluster_size - compr_blocks);
3482
3483 released_blocks += cluster_size - compr_blocks;
3484next:
3485 count -= cluster_size;
3486 }
3487
3488 return released_blocks;
3489}
3490
3491static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3492{
3493 struct inode *inode = file_inode(filp);
3494 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3495 pgoff_t page_idx = 0, last_idx;
3496 unsigned int released_blocks = 0;
3497 int ret;
3498 int writecount;
3499
3500 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3501 return -EOPNOTSUPP;
3502
3503 if (!f2fs_compressed_file(inode))
3504 return -EINVAL;
3505
3506 if (f2fs_readonly(sbi->sb))
3507 return -EROFS;
3508
3509 ret = mnt_want_write_file(filp);
3510 if (ret)
3511 return ret;
3512
3513 f2fs_balance_fs(F2FS_I_SB(inode), true);
3514
3515 inode_lock(inode);
3516
3517 writecount = atomic_read(&inode->i_writecount);
Daeho Jeong8c8cf262020-10-12 13:59:47 +09003518 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3519 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
Chao Yuef8d5632020-03-06 15:36:09 +08003520 ret = -EBUSY;
3521 goto out;
3522 }
3523
3524 if (IS_IMMUTABLE(inode)) {
3525 ret = -EINVAL;
3526 goto out;
3527 }
3528
3529 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3530 if (ret)
3531 goto out;
3532
Chao Yuef8d5632020-03-06 15:36:09 +08003533 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3534 f2fs_set_inode_flags(inode);
3535 inode->i_ctime = current_time(inode);
3536 f2fs_mark_inode_dirty_sync(inode, true);
3537
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003538 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
Daeho Jeong567c4bf2020-07-30 14:09:28 +09003539 goto out;
3540
Chao Yuef8d5632020-03-06 15:36:09 +08003541 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3542 down_write(&F2FS_I(inode)->i_mmap_sem);
3543
3544 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3545
3546 while (page_idx < last_idx) {
3547 struct dnode_of_data dn;
3548 pgoff_t end_offset, count;
3549
3550 set_new_dnode(&dn, inode, NULL, NULL, 0);
3551 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3552 if (ret) {
3553 if (ret == -ENOENT) {
3554 page_idx = f2fs_get_next_page_offset(&dn,
3555 page_idx);
3556 ret = 0;
3557 continue;
3558 }
3559 break;
3560 }
3561
3562 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3563 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003564 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuef8d5632020-03-06 15:36:09 +08003565
3566 ret = release_compress_blocks(&dn, count);
3567
3568 f2fs_put_dnode(&dn);
3569
3570 if (ret < 0)
3571 break;
3572
3573 page_idx += count;
3574 released_blocks += ret;
3575 }
3576
3577 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3578 up_write(&F2FS_I(inode)->i_mmap_sem);
3579out:
3580 inode_unlock(inode);
3581
3582 mnt_drop_write_file(filp);
3583
3584 if (ret >= 0) {
3585 ret = put_user(released_blocks, (u64 __user *)arg);
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003586 } else if (released_blocks &&
3587 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
Chao Yuef8d5632020-03-06 15:36:09 +08003588 set_sbi_flag(sbi, SBI_NEED_FSCK);
3589 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003590 "iblocks=%llu, released=%u, compr_blocks=%u, "
Chao Yuef8d5632020-03-06 15:36:09 +08003591 "run fsck to fix.",
3592 __func__, inode->i_ino, inode->i_blocks,
3593 released_blocks,
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003594 atomic_read(&F2FS_I(inode)->i_compr_blocks));
Chao Yuef8d5632020-03-06 15:36:09 +08003595 }
3596
3597 return ret;
3598}
3599
Chao Yuc75488f2020-03-06 14:35:33 +08003600static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3601{
3602 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3603 unsigned int reserved_blocks = 0;
3604 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3605 block_t blkaddr;
3606 int i;
3607
3608 for (i = 0; i < count; i++) {
3609 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3610 dn->ofs_in_node + i);
3611
3612 if (!__is_valid_data_blkaddr(blkaddr))
3613 continue;
3614 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3615 DATA_GENERIC_ENHANCE)))
3616 return -EFSCORRUPTED;
3617 }
3618
3619 while (count) {
3620 int compr_blocks = 0;
3621 blkcnt_t reserved;
3622 int ret;
3623
3624 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3625 blkaddr = f2fs_data_blkaddr(dn);
3626
3627 if (i == 0) {
3628 if (blkaddr == COMPRESS_ADDR)
3629 continue;
3630 dn->ofs_in_node += cluster_size;
3631 goto next;
3632 }
3633
3634 if (__is_valid_data_blkaddr(blkaddr)) {
3635 compr_blocks++;
3636 continue;
3637 }
3638
3639 dn->data_blkaddr = NEW_ADDR;
3640 f2fs_set_data_blkaddr(dn);
3641 }
3642
3643 reserved = cluster_size - compr_blocks;
3644 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3645 if (ret)
3646 return ret;
3647
3648 if (reserved != cluster_size - compr_blocks)
3649 return -ENOSPC;
3650
3651 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3652
3653 reserved_blocks += reserved;
3654next:
3655 count -= cluster_size;
3656 }
3657
3658 return reserved_blocks;
3659}
3660
3661static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3662{
3663 struct inode *inode = file_inode(filp);
3664 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3665 pgoff_t page_idx = 0, last_idx;
3666 unsigned int reserved_blocks = 0;
3667 int ret;
3668
3669 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3670 return -EOPNOTSUPP;
3671
3672 if (!f2fs_compressed_file(inode))
3673 return -EINVAL;
3674
3675 if (f2fs_readonly(sbi->sb))
3676 return -EROFS;
3677
3678 ret = mnt_want_write_file(filp);
3679 if (ret)
3680 return ret;
3681
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003682 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
Chao Yuc75488f2020-03-06 14:35:33 +08003683 goto out;
3684
3685 f2fs_balance_fs(F2FS_I_SB(inode), true);
3686
3687 inode_lock(inode);
3688
3689 if (!IS_IMMUTABLE(inode)) {
3690 ret = -EINVAL;
3691 goto unlock_inode;
3692 }
3693
3694 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3695 down_write(&F2FS_I(inode)->i_mmap_sem);
3696
3697 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3698
3699 while (page_idx < last_idx) {
3700 struct dnode_of_data dn;
3701 pgoff_t end_offset, count;
3702
3703 set_new_dnode(&dn, inode, NULL, NULL, 0);
3704 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3705 if (ret) {
3706 if (ret == -ENOENT) {
3707 page_idx = f2fs_get_next_page_offset(&dn,
3708 page_idx);
3709 ret = 0;
3710 continue;
3711 }
3712 break;
3713 }
3714
3715 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3716 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003717 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuc75488f2020-03-06 14:35:33 +08003718
3719 ret = reserve_compress_blocks(&dn, count);
3720
3721 f2fs_put_dnode(&dn);
3722
3723 if (ret < 0)
3724 break;
3725
3726 page_idx += count;
3727 reserved_blocks += ret;
3728 }
3729
3730 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3731 up_write(&F2FS_I(inode)->i_mmap_sem);
3732
3733 if (ret >= 0) {
3734 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3735 f2fs_set_inode_flags(inode);
3736 inode->i_ctime = current_time(inode);
3737 f2fs_mark_inode_dirty_sync(inode, true);
3738 }
3739unlock_inode:
3740 inode_unlock(inode);
3741out:
3742 mnt_drop_write_file(filp);
3743
3744 if (ret >= 0) {
3745 ret = put_user(reserved_blocks, (u64 __user *)arg);
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003746 } else if (reserved_blocks &&
3747 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
Chao Yuc75488f2020-03-06 14:35:33 +08003748 set_sbi_flag(sbi, SBI_NEED_FSCK);
3749 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003750 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
Chao Yuc75488f2020-03-06 14:35:33 +08003751 "run fsck to fix.",
3752 __func__, inode->i_ino, inode->i_blocks,
3753 reserved_blocks,
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003754 atomic_read(&F2FS_I(inode)->i_compr_blocks));
Chao Yuc75488f2020-03-06 14:35:33 +08003755 }
3756
3757 return ret;
3758}
3759
Daeho Jeong9af84642020-07-21 12:21:11 +09003760static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3761 pgoff_t off, block_t block, block_t len, u32 flags)
3762{
3763 struct request_queue *q = bdev_get_queue(bdev);
3764 sector_t sector = SECTOR_FROM_BLOCK(block);
3765 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3766 int ret = 0;
3767
3768 if (!q)
3769 return -ENXIO;
3770
3771 if (flags & F2FS_TRIM_FILE_DISCARD)
3772 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3773 blk_queue_secure_erase(q) ?
3774 BLKDEV_DISCARD_SECURE : 0);
3775
3776 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3777 if (IS_ENCRYPTED(inode))
3778 ret = fscrypt_zeroout_range(inode, off, block, len);
3779 else
3780 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3781 GFP_NOFS, 0);
3782 }
3783
3784 return ret;
3785}
3786
3787static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3788{
3789 struct inode *inode = file_inode(filp);
3790 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3791 struct address_space *mapping = inode->i_mapping;
3792 struct block_device *prev_bdev = NULL;
3793 struct f2fs_sectrim_range range;
3794 pgoff_t index, pg_end, prev_index = 0;
3795 block_t prev_block = 0, len = 0;
3796 loff_t end_addr;
3797 bool to_end = false;
3798 int ret = 0;
3799
3800 if (!(filp->f_mode & FMODE_WRITE))
3801 return -EBADF;
3802
3803 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3804 sizeof(range)))
3805 return -EFAULT;
3806
3807 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3808 !S_ISREG(inode->i_mode))
3809 return -EINVAL;
3810
3811 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3812 !f2fs_hw_support_discard(sbi)) ||
3813 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3814 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3815 return -EOPNOTSUPP;
3816
3817 file_start_write(filp);
3818 inode_lock(inode);
3819
3820 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3821 range.start >= inode->i_size) {
3822 ret = -EINVAL;
3823 goto err;
3824 }
3825
3826 if (range.len == 0)
3827 goto err;
3828
3829 if (inode->i_size - range.start > range.len) {
3830 end_addr = range.start + range.len;
3831 } else {
3832 end_addr = range.len == (u64)-1 ?
3833 sbi->sb->s_maxbytes : inode->i_size;
3834 to_end = true;
3835 }
3836
3837 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3838 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3839 ret = -EINVAL;
3840 goto err;
3841 }
3842
3843 index = F2FS_BYTES_TO_BLK(range.start);
3844 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3845
3846 ret = f2fs_convert_inline_inode(inode);
3847 if (ret)
3848 goto err;
3849
3850 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3851 down_write(&F2FS_I(inode)->i_mmap_sem);
3852
3853 ret = filemap_write_and_wait_range(mapping, range.start,
3854 to_end ? LLONG_MAX : end_addr - 1);
3855 if (ret)
3856 goto out;
3857
3858 truncate_inode_pages_range(mapping, range.start,
3859 to_end ? -1 : end_addr - 1);
3860
3861 while (index < pg_end) {
3862 struct dnode_of_data dn;
3863 pgoff_t end_offset, count;
3864 int i;
3865
3866 set_new_dnode(&dn, inode, NULL, NULL, 0);
3867 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3868 if (ret) {
3869 if (ret == -ENOENT) {
3870 index = f2fs_get_next_page_offset(&dn, index);
3871 continue;
3872 }
3873 goto out;
3874 }
3875
3876 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3877 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3878 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3879 struct block_device *cur_bdev;
3880 block_t blkaddr = f2fs_data_blkaddr(&dn);
3881
3882 if (!__is_valid_data_blkaddr(blkaddr))
3883 continue;
3884
3885 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3886 DATA_GENERIC_ENHANCE)) {
3887 ret = -EFSCORRUPTED;
3888 f2fs_put_dnode(&dn);
3889 goto out;
3890 }
3891
3892 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3893 if (f2fs_is_multi_device(sbi)) {
3894 int di = f2fs_target_device_index(sbi, blkaddr);
3895
3896 blkaddr -= FDEV(di).start_blk;
3897 }
3898
3899 if (len) {
3900 if (prev_bdev == cur_bdev &&
3901 index == prev_index + len &&
3902 blkaddr == prev_block + len) {
3903 len++;
3904 } else {
3905 ret = f2fs_secure_erase(prev_bdev,
3906 inode, prev_index, prev_block,
3907 len, range.flags);
3908 if (ret) {
3909 f2fs_put_dnode(&dn);
3910 goto out;
3911 }
3912
3913 len = 0;
3914 }
3915 }
3916
3917 if (!len) {
3918 prev_bdev = cur_bdev;
3919 prev_index = index;
3920 prev_block = blkaddr;
3921 len = 1;
3922 }
3923 }
3924
3925 f2fs_put_dnode(&dn);
3926
3927 if (fatal_signal_pending(current)) {
3928 ret = -EINTR;
3929 goto out;
3930 }
3931 cond_resched();
3932 }
3933
3934 if (len)
3935 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3936 prev_block, len, range.flags);
3937out:
3938 up_write(&F2FS_I(inode)->i_mmap_sem);
3939 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3940err:
3941 inode_unlock(inode);
3942 file_end_write(filp);
3943
3944 return ret;
3945}
3946
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09003947static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003948{
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09003949 struct inode *inode = file_inode(filp);
3950 struct f2fs_comp_option option;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003951
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09003952 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3953 return -EOPNOTSUPP;
3954
3955 inode_lock_shared(inode);
3956
3957 if (!f2fs_compressed_file(inode)) {
3958 inode_unlock_shared(inode);
3959 return -ENODATA;
3960 }
3961
3962 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3963 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3964
3965 inode_unlock_shared(inode);
3966
3967 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3968 sizeof(option)))
3969 return -EFAULT;
3970
3971 return 0;
3972}
3973
Daeho Jeonge1e8deb2020-10-30 13:10:35 +09003974static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3975{
3976 struct inode *inode = file_inode(filp);
3977 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3978 struct f2fs_comp_option option;
3979 int ret = 0;
3980
3981 if (!f2fs_sb_has_compression(sbi))
3982 return -EOPNOTSUPP;
3983
3984 if (!(filp->f_mode & FMODE_WRITE))
3985 return -EBADF;
3986
3987 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3988 sizeof(option)))
3989 return -EFAULT;
3990
3991 if (!f2fs_compressed_file(inode) ||
3992 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3993 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3994 option.algorithm >= COMPRESS_MAX)
3995 return -EINVAL;
3996
3997 file_start_write(filp);
3998 inode_lock(inode);
3999
4000 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4001 ret = -EBUSY;
4002 goto out;
4003 }
4004
4005 if (inode->i_size != 0) {
4006 ret = -EFBIG;
4007 goto out;
4008 }
4009
4010 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
4011 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
4012 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
4013 f2fs_mark_inode_dirty_sync(inode, true);
4014
4015 if (!f2fs_is_compress_backend_ready(inode))
4016 f2fs_warn(sbi, "compression algorithm is successfully set, "
4017 "but current kernel doesn't support this algorithm.");
4018out:
4019 inode_unlock(inode);
4020 file_end_write(filp);
4021
4022 return ret;
4023}
4024
Daeho Jeong5fdb3222020-12-03 15:56:15 +09004025static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4026{
4027 DEFINE_READAHEAD(ractl, NULL, inode->i_mapping, page_idx);
4028 struct address_space *mapping = inode->i_mapping;
4029 struct page *page;
4030 pgoff_t redirty_idx = page_idx;
4031 int i, page_len = 0, ret = 0;
4032
4033 page_cache_ra_unbounded(&ractl, len, 0);
4034
4035 for (i = 0; i < len; i++, page_idx++) {
4036 page = read_cache_page(mapping, page_idx, NULL, NULL);
4037 if (IS_ERR(page)) {
4038 ret = PTR_ERR(page);
4039 break;
4040 }
4041 page_len++;
4042 }
4043
4044 for (i = 0; i < page_len; i++, redirty_idx++) {
4045 page = find_lock_page(mapping, redirty_idx);
4046 if (!page)
4047 ret = -ENOENT;
4048 set_page_dirty(page);
4049 f2fs_put_page(page, 1);
4050 f2fs_put_page(page, 0);
4051 }
4052
4053 return ret;
4054}
4055
4056static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
4057{
4058 struct inode *inode = file_inode(filp);
4059 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4060 struct f2fs_inode_info *fi = F2FS_I(inode);
4061 pgoff_t page_idx = 0, last_idx;
4062 unsigned int blk_per_seg = sbi->blocks_per_seg;
4063 int cluster_size = F2FS_I(inode)->i_cluster_size;
4064 int count, ret;
4065
4066 if (!f2fs_sb_has_compression(sbi) ||
4067 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4068 return -EOPNOTSUPP;
4069
4070 if (!(filp->f_mode & FMODE_WRITE))
4071 return -EBADF;
4072
4073 if (!f2fs_compressed_file(inode))
4074 return -EINVAL;
4075
4076 f2fs_balance_fs(F2FS_I_SB(inode), true);
4077
4078 file_start_write(filp);
4079 inode_lock(inode);
4080
4081 if (!f2fs_is_compress_backend_ready(inode)) {
4082 ret = -EOPNOTSUPP;
4083 goto out;
4084 }
4085
4086 if (f2fs_is_mmap_file(inode)) {
4087 ret = -EBUSY;
4088 goto out;
4089 }
4090
4091 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4092 if (ret)
4093 goto out;
4094
4095 if (!atomic_read(&fi->i_compr_blocks))
4096 goto out;
4097
4098 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4099
4100 count = last_idx - page_idx;
4101 while (count) {
4102 int len = min(cluster_size, count);
4103
4104 ret = redirty_blocks(inode, page_idx, len);
4105 if (ret < 0)
4106 break;
4107
4108 if (get_dirty_pages(inode) >= blk_per_seg)
4109 filemap_fdatawrite(inode->i_mapping);
4110
4111 count -= len;
4112 page_idx += len;
4113 }
4114
4115 if (!ret)
4116 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4117 LLONG_MAX);
4118
4119 if (ret)
4120 f2fs_warn(sbi, "%s: The file might be partially decompressed "
4121 "(errno=%d). Please delete the file.\n",
4122 __func__, ret);
4123out:
4124 inode_unlock(inode);
4125 file_end_write(filp);
4126
4127 return ret;
4128}
4129
4130static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4131{
4132 struct inode *inode = file_inode(filp);
4133 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4134 pgoff_t page_idx = 0, last_idx;
4135 unsigned int blk_per_seg = sbi->blocks_per_seg;
4136 int cluster_size = F2FS_I(inode)->i_cluster_size;
4137 int count, ret;
4138
4139 if (!f2fs_sb_has_compression(sbi) ||
4140 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4141 return -EOPNOTSUPP;
4142
4143 if (!(filp->f_mode & FMODE_WRITE))
4144 return -EBADF;
4145
4146 if (!f2fs_compressed_file(inode))
4147 return -EINVAL;
4148
4149 f2fs_balance_fs(F2FS_I_SB(inode), true);
4150
4151 file_start_write(filp);
4152 inode_lock(inode);
4153
4154 if (!f2fs_is_compress_backend_ready(inode)) {
4155 ret = -EOPNOTSUPP;
4156 goto out;
4157 }
4158
4159 if (f2fs_is_mmap_file(inode)) {
4160 ret = -EBUSY;
4161 goto out;
4162 }
4163
4164 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4165 if (ret)
4166 goto out;
4167
4168 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4169
4170 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4171
4172 count = last_idx - page_idx;
4173 while (count) {
4174 int len = min(cluster_size, count);
4175
4176 ret = redirty_blocks(inode, page_idx, len);
4177 if (ret < 0)
4178 break;
4179
4180 if (get_dirty_pages(inode) >= blk_per_seg)
4181 filemap_fdatawrite(inode->i_mapping);
4182
4183 count -= len;
4184 page_idx += len;
4185 }
4186
4187 if (!ret)
4188 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4189 LLONG_MAX);
4190
4191 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4192
4193 if (ret)
4194 f2fs_warn(sbi, "%s: The file might be partially compressed "
4195 "(errno=%d). Please delete the file.\n",
4196 __func__, ret);
4197out:
4198 inode_unlock(inode);
4199 file_end_write(filp);
4200
4201 return ret;
4202}
4203
Chao Yu34178b1b2020-11-10 09:24:37 +08004204static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004205{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004206 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07004207 case FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07004208 return f2fs_ioc_getflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004209 case FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07004210 return f2fs_ioc_setflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004211 case FS_IOC_GETVERSION:
Chao Yud49f3e82015-01-23 20:36:04 +08004212 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004213 case F2FS_IOC_START_ATOMIC_WRITE:
4214 return f2fs_ioc_start_atomic_write(filp);
4215 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4216 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07004217 case F2FS_IOC_START_VOLATILE_WRITE:
4218 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08004219 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4220 return f2fs_ioc_release_volatile_write(filp);
4221 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4222 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08004223 case F2FS_IOC_SHUTDOWN:
4224 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004225 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07004226 return f2fs_ioc_fitrim(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004227 case FS_IOC_SET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07004228 return f2fs_ioc_set_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004229 case FS_IOC_GET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07004230 return f2fs_ioc_get_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004231 case FS_IOC_GET_ENCRYPTION_PWSALT:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07004232 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07004233 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4234 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4235 case FS_IOC_ADD_ENCRYPTION_KEY:
4236 return f2fs_ioc_add_encryption_key(filp, arg);
4237 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4238 return f2fs_ioc_remove_encryption_key(filp, arg);
4239 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4240 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4241 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4242 return f2fs_ioc_get_encryption_key_status(filp, arg);
Eric Biggersee446e12020-03-14 13:50:51 -07004243 case FS_IOC_GET_ENCRYPTION_NONCE:
4244 return f2fs_ioc_get_encryption_nonce(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08004245 case F2FS_IOC_GARBAGE_COLLECT:
4246 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07004247 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4248 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08004249 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08004250 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08004251 case F2FS_IOC_DEFRAGMENT:
4252 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004253 case F2FS_IOC_MOVE_RANGE:
4254 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07004255 case F2FS_IOC_FLUSH_DEVICE:
4256 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004257 case F2FS_IOC_GET_FEATURES:
4258 return f2fs_ioc_get_features(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004259 case FS_IOC_FSGETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08004260 return f2fs_ioc_fsgetxattr(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004261 case FS_IOC_FSSETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08004262 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004263 case F2FS_IOC_GET_PIN_FILE:
4264 return f2fs_ioc_get_pin_file(filp, arg);
4265 case F2FS_IOC_SET_PIN_FILE:
4266 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08004267 case F2FS_IOC_PRECACHE_EXTENTS:
4268 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004269 case F2FS_IOC_RESIZE_FS:
4270 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07004271 case FS_IOC_ENABLE_VERITY:
4272 return f2fs_ioc_enable_verity(filp, arg);
4273 case FS_IOC_MEASURE_VERITY:
4274 return f2fs_ioc_measure_verity(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004275 case FS_IOC_GETFSLABEL:
4276 return f2fs_ioc_getfslabel(filp, arg);
4277 case FS_IOC_SETFSLABEL:
4278 return f2fs_ioc_setfslabel(filp, arg);
Chao Yu439dfb12020-02-21 18:09:21 +08004279 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4280 return f2fs_get_compress_blocks(filp, arg);
Chao Yuef8d5632020-03-06 15:36:09 +08004281 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4282 return f2fs_release_compress_blocks(filp, arg);
Chao Yuc75488f2020-03-06 14:35:33 +08004283 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4284 return f2fs_reserve_compress_blocks(filp, arg);
Daeho Jeong9af84642020-07-21 12:21:11 +09004285 case F2FS_IOC_SEC_TRIM_FILE:
4286 return f2fs_sec_trim_file(filp, arg);
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09004287 case F2FS_IOC_GET_COMPRESS_OPTION:
4288 return f2fs_ioc_get_compress_option(filp, arg);
Daeho Jeonge1e8deb2020-10-30 13:10:35 +09004289 case F2FS_IOC_SET_COMPRESS_OPTION:
4290 return f2fs_ioc_set_compress_option(filp, arg);
Daeho Jeong5fdb3222020-12-03 15:56:15 +09004291 case F2FS_IOC_DECOMPRESS_FILE:
4292 return f2fs_ioc_decompress_file(filp, arg);
4293 case F2FS_IOC_COMPRESS_FILE:
4294 return f2fs_ioc_compress_file(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004295 default:
4296 return -ENOTTY;
4297 }
4298}
4299
Chao Yu34178b1b2020-11-10 09:24:37 +08004300long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4301{
4302 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4303 return -EIO;
4304 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4305 return -ENOSPC;
4306
4307 return __f2fs_ioctl(filp, cmd, arg);
4308}
4309
Chao Yu4c8ff702019-11-01 18:07:14 +08004310static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4311{
4312 struct file *file = iocb->ki_filp;
4313 struct inode *inode = file_inode(file);
Chao Yu8b83ac82020-04-16 18:16:56 +08004314 int ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004315
4316 if (!f2fs_is_compress_backend_ready(inode))
4317 return -EOPNOTSUPP;
4318
Chao Yu8b83ac82020-04-16 18:16:56 +08004319 ret = generic_file_read_iter(iocb, iter);
4320
4321 if (ret > 0)
4322 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4323
4324 return ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004325}
4326
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004327static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4328{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004329 struct file *file = iocb->ki_filp;
4330 struct inode *inode = file_inode(file);
4331 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004332
Chao Yu126ce722019-04-02 18:52:22 +08004333 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4334 ret = -EIO;
4335 goto out;
4336 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02004337
Chao Yu7bd29352020-02-24 19:20:15 +08004338 if (!f2fs_is_compress_backend_ready(inode)) {
4339 ret = -EOPNOTSUPP;
4340 goto out;
4341 }
Chao Yu4c8ff702019-11-01 18:07:14 +08004342
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004343 if (iocb->ki_flags & IOCB_NOWAIT) {
4344 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08004345 ret = -EAGAIN;
4346 goto out;
4347 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004348 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004349 inode_lock(inode);
4350 }
4351
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004352 ret = generic_write_checks(iocb, from);
4353 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004354 bool preallocated = false;
4355 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004356 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004357
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004358 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4359 set_inode_flag(inode, FI_NO_PREALLOC);
4360
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004361 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4362 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004363 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004364 f2fs_has_inline_data(inode) ||
4365 f2fs_force_buffered_io(inode, iocb, from)) {
4366 clear_inode_flag(inode, FI_NO_PREALLOC);
4367 inode_unlock(inode);
4368 ret = -EAGAIN;
4369 goto out;
4370 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004371 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07004372 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004373
4374 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4375 goto write;
4376
4377 if (iocb->ki_flags & IOCB_DIRECT) {
4378 /*
4379 * Convert inline data for Direct I/O before entering
4380 * f2fs_direct_IO().
4381 */
4382 err = f2fs_convert_inline_inode(inode);
4383 if (err)
4384 goto out_err;
4385 /*
4386 * If force_buffere_io() is true, we have to allocate
4387 * blocks all the time, since f2fs_direct_IO will fall
4388 * back to buffered IO.
4389 */
4390 if (!f2fs_force_buffered_io(inode, iocb, from) &&
4391 allow_outplace_dio(inode, iocb, from))
4392 goto write;
4393 }
4394 preallocated = true;
4395 target_size = iocb->ki_pos + iov_iter_count(from);
4396
4397 err = f2fs_preallocate_blocks(iocb, from);
4398 if (err) {
4399out_err:
4400 clear_inode_flag(inode, FI_NO_PREALLOC);
4401 inode_unlock(inode);
4402 ret = err;
4403 goto out;
4404 }
4405write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004406 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004407 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08004408
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004409 /* if we couldn't write data, we should deallocate blocks. */
4410 if (preallocated && i_size_read(inode) < target_size)
4411 f2fs_truncate(inode);
4412
Chao Yub0af6d42017-08-02 23:21:48 +08004413 if (ret > 0)
4414 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004415 }
4416 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08004417out:
4418 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4419 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07004420 if (ret > 0)
4421 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004422 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004423}
4424
Namjae Jeone9750822013-02-04 23:41:41 +09004425#ifdef CONFIG_COMPAT
Chao Yu34178b1b2020-11-10 09:24:37 +08004426struct compat_f2fs_gc_range {
4427 u32 sync;
4428 compat_u64 start;
4429 compat_u64 len;
4430};
4431#define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4432 struct compat_f2fs_gc_range)
4433
4434static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4435{
4436 struct compat_f2fs_gc_range __user *urange;
4437 struct f2fs_gc_range range;
4438 int err;
4439
4440 urange = compat_ptr(arg);
4441 err = get_user(range.sync, &urange->sync);
4442 err |= get_user(range.start, &urange->start);
4443 err |= get_user(range.len, &urange->len);
4444 if (err)
4445 return -EFAULT;
4446
4447 return __f2fs_ioc_gc_range(file, &range);
4448}
4449
4450struct compat_f2fs_move_range {
4451 u32 dst_fd;
4452 compat_u64 pos_in;
4453 compat_u64 pos_out;
4454 compat_u64 len;
4455};
4456#define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4457 struct compat_f2fs_move_range)
4458
4459static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4460{
4461 struct compat_f2fs_move_range __user *urange;
4462 struct f2fs_move_range range;
4463 int err;
4464
4465 urange = compat_ptr(arg);
4466 err = get_user(range.dst_fd, &urange->dst_fd);
4467 err |= get_user(range.pos_in, &urange->pos_in);
4468 err |= get_user(range.pos_out, &urange->pos_out);
4469 err |= get_user(range.len, &urange->len);
4470 if (err)
4471 return -EFAULT;
4472
4473 return __f2fs_ioc_move_range(file, &range);
4474}
4475
Namjae Jeone9750822013-02-04 23:41:41 +09004476long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4477{
Chao Yu34178b1b2020-11-10 09:24:37 +08004478 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4479 return -EIO;
4480 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4481 return -ENOSPC;
4482
Namjae Jeone9750822013-02-04 23:41:41 +09004483 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07004484 case FS_IOC32_GETFLAGS:
4485 cmd = FS_IOC_GETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09004486 break;
Eric Biggers3357af82020-07-14 15:18:12 -07004487 case FS_IOC32_SETFLAGS:
4488 cmd = FS_IOC_SETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09004489 break;
Eric Biggers3357af82020-07-14 15:18:12 -07004490 case FS_IOC32_GETVERSION:
4491 cmd = FS_IOC_GETVERSION;
Chao Yu04ef4b62015-11-10 18:44:20 +08004492 break;
Chao Yu34178b1b2020-11-10 09:24:37 +08004493 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4494 return f2fs_compat_ioc_gc_range(file, arg);
4495 case F2FS_IOC32_MOVE_RANGE:
4496 return f2fs_compat_ioc_move_range(file, arg);
Chao Yu04ef4b62015-11-10 18:44:20 +08004497 case F2FS_IOC_START_ATOMIC_WRITE:
4498 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4499 case F2FS_IOC_START_VOLATILE_WRITE:
4500 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4501 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4502 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02004503 case FITRIM:
Eric Biggers3357af82020-07-14 15:18:12 -07004504 case FS_IOC_SET_ENCRYPTION_POLICY:
4505 case FS_IOC_GET_ENCRYPTION_PWSALT:
4506 case FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07004507 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4508 case FS_IOC_ADD_ENCRYPTION_KEY:
4509 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4510 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4511 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Eric Biggersee446e12020-03-14 13:50:51 -07004512 case FS_IOC_GET_ENCRYPTION_NONCE:
Chao Yu04ef4b62015-11-10 18:44:20 +08004513 case F2FS_IOC_GARBAGE_COLLECT:
4514 case F2FS_IOC_WRITE_CHECKPOINT:
4515 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kime066b832017-04-13 15:17:00 -07004516 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004517 case F2FS_IOC_GET_FEATURES:
Eric Biggers3357af82020-07-14 15:18:12 -07004518 case FS_IOC_FSGETXATTR:
4519 case FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004520 case F2FS_IOC_GET_PIN_FILE:
4521 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08004522 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004523 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07004524 case FS_IOC_ENABLE_VERITY:
4525 case FS_IOC_MEASURE_VERITY:
Eric Biggers3357af82020-07-14 15:18:12 -07004526 case FS_IOC_GETFSLABEL:
4527 case FS_IOC_SETFSLABEL:
Chao Yu439dfb12020-02-21 18:09:21 +08004528 case F2FS_IOC_GET_COMPRESS_BLOCKS:
Chao Yuef8d5632020-03-06 15:36:09 +08004529 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
Chao Yuc75488f2020-03-06 14:35:33 +08004530 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
Daeho Jeong9af84642020-07-21 12:21:11 +09004531 case F2FS_IOC_SEC_TRIM_FILE:
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09004532 case F2FS_IOC_GET_COMPRESS_OPTION:
Daeho Jeonge1e8deb2020-10-30 13:10:35 +09004533 case F2FS_IOC_SET_COMPRESS_OPTION:
Daeho Jeong5fdb3222020-12-03 15:56:15 +09004534 case F2FS_IOC_DECOMPRESS_FILE:
4535 case F2FS_IOC_COMPRESS_FILE:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004536 break;
Namjae Jeone9750822013-02-04 23:41:41 +09004537 default:
4538 return -ENOIOCTLCMD;
4539 }
Chao Yu34178b1b2020-11-10 09:24:37 +08004540 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Namjae Jeone9750822013-02-04 23:41:41 +09004541}
4542#endif
4543
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004544const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08004545 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08004546 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004547 .write_iter = f2fs_file_write_iter,
4548 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08004549 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004550 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07004551 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004552 .fsync = f2fs_sync_file,
4553 .fallocate = f2fs_fallocate,
4554 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09004555#ifdef CONFIG_COMPAT
4556 .compat_ioctl = f2fs_compat_ioctl,
4557#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004558 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04004559 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004560};