blob: 792f9059d897c3b21cb387c7a229216196cd9fae [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 Yub7ff0882020-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
Chao Yu04a49572020-12-26 18:07:01 +080063 if (unlikely(IS_IMMUTABLE(inode)))
64 return VM_FAULT_SIGBUS;
65
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020066 if (unlikely(f2fs_cp_error(sbi))) {
67 err = -EIO;
68 goto err;
69 }
70
Chao Yu00e09c02019-08-23 17:58:36 +080071 if (!f2fs_is_checkpoint_ready(sbi)) {
72 err = -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +080073 goto err;
Chao Yu00e09c02019-08-23 17:58:36 +080074 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090075
Chao Yu4c8ff702019-11-01 18:07:14 +080076#ifdef CONFIG_F2FS_FS_COMPRESSION
77 if (f2fs_compressed_file(inode)) {
78 int ret = f2fs_is_compressed_cluster(inode, page->index);
79
80 if (ret < 0) {
81 err = ret;
82 goto err;
83 } else if (ret) {
84 if (ret < F2FS_I(inode)->i_cluster_size) {
85 err = -EAGAIN;
86 goto err;
87 }
88 need_alloc = false;
89 }
90 }
91#endif
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080092 /* should do out of any locked page */
Chao Yu4c8ff702019-11-01 18:07:14 +080093 if (need_alloc)
94 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080095
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090096 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070097
98 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070099
Dave Jiang11bac802017-02-24 14:56:41 -0800100 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800101 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900102 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900103 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900104 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900105 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900106 unlock_page(page);
107 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800108 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900109 }
110
Chao Yu4c8ff702019-11-01 18:07:14 +0800111 if (need_alloc) {
112 /* block allocation */
Chao Yu0ef81832020-06-18 14:36:22 +0800113 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +0800114 set_new_dnode(&dn, inode, NULL, NULL, 0);
115 err = f2fs_get_block(&dn, page->index);
116 f2fs_put_dnode(&dn);
Chao Yu0ef81832020-06-18 14:36:22 +0800117 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu39a86952018-09-27 18:33:18 +0800118 }
119
Chao Yu06c75402020-03-02 17:34:27 +0800120#ifdef CONFIG_F2FS_FS_COMPRESSION
121 if (!need_alloc) {
122 set_new_dnode(&dn, inode, NULL, NULL, 0);
123 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
124 f2fs_put_dnode(&dn);
125 }
126#endif
127 if (err) {
128 unlock_page(page);
129 goto out_sem;
Chao Yu39a86952018-09-27 18:33:18 +0800130 }
131
Chao Yubae0ee72018-12-25 17:43:42 +0800132 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +0800133
134 /* wait for GCed page writeback via META_MAPPING */
135 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
136
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900137 /*
138 * check to see if the page is mapped already (no holes)
139 */
140 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800141 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900142
143 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300144 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800145 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900146 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900147
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300148 offset = i_size_read(inode) & ~PAGE_MASK;
149 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900150 }
151 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700152 if (!PageUptodate(page))
153 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900154
Chao Yub0af6d42017-08-02 23:21:48 +0800155 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530156 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800157
Jaegeuk Kime943a102013-10-25 14:26:31 +0900158 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800159out_sem:
160 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800161
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900162 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200163err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900164 return block_page_mkwrite_return(err);
165}
166
167static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800168 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700169 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900170 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900171};
172
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900173static int get_parent_ino(struct inode *inode, nid_t *pino)
174{
175 struct dentry *dentry;
176
Eric Biggers84c9c2d2020-05-05 11:41:11 -0700177 /*
178 * Make sure to get the non-deleted alias. The alias associated with
179 * the open file descriptor being fsync()'ed may be deleted already.
180 */
181 dentry = d_find_alias(inode);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900182 if (!dentry)
183 return 0;
184
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900185 *pino = parent_ino(dentry);
186 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900187 return 1;
188}
189
Chao Yua5fd5052017-11-06 22:51:45 +0800190static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800191{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700192 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800193 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800194
Chao Yua5fd5052017-11-06 22:51:45 +0800195 if (!S_ISREG(inode->i_mode))
196 cp_reason = CP_NON_REGULAR;
Chao Yu4c8ff702019-11-01 18:07:14 +0800197 else if (f2fs_compressed_file(inode))
198 cp_reason = CP_COMPRESSED;
Chao Yua5fd5052017-11-06 22:51:45 +0800199 else if (inode->i_nlink != 1)
200 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700201 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800202 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800203 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800204 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800205 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800206 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800207 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800208 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700209 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800210 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800211 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800212 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800213 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800214 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
215 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
216 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800217 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800218
Chao Yua5fd5052017-11-06 22:51:45 +0800219 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800220}
221
Changman Lee9c7bb702014-12-08 15:29:40 +0900222static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
223{
224 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
225 bool ret = false;
226 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800227 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900228 ret = true;
229 f2fs_put_page(i, 0);
230 return ret;
231}
232
Changman Lee51455b12014-12-08 15:29:41 +0900233static void try_to_fix_pino(struct inode *inode)
234{
235 struct f2fs_inode_info *fi = F2FS_I(inode);
236 nid_t pino;
237
238 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900239 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
240 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700241 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900242 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900243 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700244 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900245}
246
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700247static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
248 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900249{
250 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700251 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700252 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900253 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800254 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900255 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900256 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900257 .nr_to_write = LONG_MAX,
258 .for_reclaim = 0,
259 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800260 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900261
Jaegeuk Kim611b7f9dc2021-08-19 14:00:57 -0700262 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900263 return 0;
264
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900265 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700266
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800267 if (S_ISDIR(inode->i_mode))
268 goto go_write;
269
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700270 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800271 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700272 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400273 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700274 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700275
Jaegeuk Kim611b7f9dc2021-08-19 14:00:57 -0700276 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
Chao Yua5fd5052017-11-06 22:51:45 +0800277 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900278 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900279 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900280
Changman Lee9c7bb702014-12-08 15:29:40 +0900281 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800282 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700283 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900284 goto go_write;
285 }
286
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700287 /*
288 * if there is no written data, don't waste time to write recovery info.
289 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700290 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800291 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700292
Changman Lee9c7bb702014-12-08 15:29:40 +0900293 /* it may call write_inode just prior to fsync */
294 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700295 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700296
Jaegeuk Kim91942322016-05-20 10:13:22 -0700297 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800298 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700299 goto flush_out;
300 goto out;
301 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700302go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900303 /*
304 * Both of fdatasync() and fsync() are able to be recovered from
305 * sudden-power-off.
306 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700307 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800308 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700309 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900310
Chao Yua5fd5052017-11-06 22:51:45 +0800311 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900312 /* all the dirty node pages should be flushed for POR */
313 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900314
Changman Lee51455b12014-12-08 15:29:41 +0900315 /*
316 * We've secured consistency through sync_fs. Following pino
317 * will be used only for fsynced inodes after checkpoint.
318 */
319 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700320 clear_inode_flag(inode, FI_APPEND_WRITE);
321 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900322 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900323 }
Changman Lee51455b12014-12-08 15:29:41 +0900324sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800325 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800326 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800327 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700328 if (ret)
329 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900330
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800331 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800332 if (unlikely(f2fs_cp_error(sbi))) {
333 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800334 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800335 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800336
Chao Yu4d57b862018-05-30 00:20:41 +0800337 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700338 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900339 f2fs_write_inode(inode, NULL);
340 goto sync_nodes;
341 }
342
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700343 /*
344 * If it's atomic_write, it's just fine to keep write ordering. So
345 * here we don't need to wait for node write completion, since we use
346 * node chain which serializes node blocks. If one of node writes are
347 * reordered, we can see simply broken chain, resulting in stopping
348 * roll-forward recovery. It means we'll recover all or none node blocks
349 * given fsync mark.
350 */
351 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800352 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700353 if (ret)
354 goto out;
355 }
Changman Lee51455b12014-12-08 15:29:41 +0900356
357 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800358 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700359 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900360flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700361 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800362 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800363 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800364 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800365 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800366 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800367 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800368 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900369out:
Chao Yua5fd5052017-11-06 22:51:45 +0800370 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700371 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900372 return ret;
373}
374
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700375int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
376{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200377 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
378 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700379 return f2fs_do_sync_file(file, start, end, datasync, false);
380}
381
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100382static bool __found_offset(struct address_space *mapping, block_t blkaddr,
383 pgoff_t index, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900384{
385 switch (whence) {
386 case SEEK_DATA:
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100387 if (__is_valid_data_blkaddr(blkaddr))
388 return true;
389 if (blkaddr == NEW_ADDR &&
390 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900391 return true;
392 break;
393 case SEEK_HOLE:
394 if (blkaddr == NULL_ADDR)
395 return true;
396 break;
397 }
398 return false;
399}
400
Chao Yu267378d2014-04-23 14:10:24 +0800401static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
402{
403 struct inode *inode = file->f_mapping->host;
404 loff_t maxbytes = inode->i_sb->s_maxbytes;
405 struct dnode_of_data dn;
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100406 pgoff_t pgofs, end_offset;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900407 loff_t data_ofs = offset;
408 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800409 int err = 0;
410
Al Viro59551022016-01-22 15:40:57 -0500411 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800412
413 isize = i_size_read(inode);
414 if (offset >= isize)
415 goto fail;
416
417 /* handle inline data case */
Chao Yu4cad0052020-11-02 17:36:58 +0800418 if (f2fs_has_inline_data(inode)) {
419 if (whence == SEEK_HOLE) {
420 data_ofs = isize;
421 goto found;
422 } else if (whence == SEEK_DATA) {
423 data_ofs = offset;
424 goto found;
425 }
Chao Yu267378d2014-04-23 14:10:24 +0800426 }
427
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300428 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800429
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300430 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800431 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800432 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800433 if (err && err != -ENOENT) {
434 goto fail;
435 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900436 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800437 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800438 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800439 continue;
440 } else {
441 goto found;
442 }
443 }
444
Chao Yu81ca7352016-01-26 15:39:35 +0800445 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800446
447 /* find data/hole in dnode block */
448 for (; dn.ofs_in_node < end_offset;
449 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300450 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800451 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900452
Chao Yua2ced1c2020-02-14 17:44:10 +0800453 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu267378d2014-04-23 14:10:24 +0800454
Chao Yuc9b60782018-08-01 19:13:44 +0800455 if (__is_valid_data_blkaddr(blkaddr) &&
456 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800457 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800458 f2fs_put_dnode(&dn);
459 goto fail;
460 }
461
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100462 if (__found_offset(file->f_mapping, blkaddr,
Chao Yue1da7872018-06-05 17:44:11 +0800463 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800464 f2fs_put_dnode(&dn);
465 goto found;
466 }
467 }
468 f2fs_put_dnode(&dn);
469 }
470
471 if (whence == SEEK_DATA)
472 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800473found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900474 if (whence == SEEK_HOLE && data_ofs > isize)
475 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500476 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800477 return vfs_setpos(file, data_ofs, maxbytes);
478fail:
Al Viro59551022016-01-22 15:40:57 -0500479 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800480 return -ENXIO;
481}
482
483static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
484{
485 struct inode *inode = file->f_mapping->host;
486 loff_t maxbytes = inode->i_sb->s_maxbytes;
487
488 switch (whence) {
489 case SEEK_SET:
490 case SEEK_CUR:
491 case SEEK_END:
492 return generic_file_llseek_size(file, offset, whence,
493 maxbytes, i_size_read(inode));
494 case SEEK_DATA:
495 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700496 if (offset < 0)
497 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800498 return f2fs_seek_block(file, offset, whence);
499 }
500
501 return -EINVAL;
502}
503
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900504static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
505{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700506 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800507 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700508
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200509 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
510 return -EIO;
511
Chao Yu4c8ff702019-11-01 18:07:14 +0800512 if (!f2fs_is_compress_backend_ready(inode))
513 return -EOPNOTSUPP;
514
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700515 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800516 err = f2fs_convert_inline_inode(inode);
517 if (err)
518 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700519
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900520 file_accessed(file);
521 vma->vm_ops = &f2fs_file_vm_ops;
Chao Yu4c8ff702019-11-01 18:07:14 +0800522 set_inode_flag(inode, FI_MMAP_FILE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900523 return 0;
524}
525
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700526static int f2fs_file_open(struct inode *inode, struct file *filp)
527{
Eric Biggers2e168c82017-11-29 12:35:28 -0800528 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700529
Eric Biggers2e168c82017-11-29 12:35:28 -0800530 if (err)
531 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900532
Chao Yu4c8ff702019-11-01 18:07:14 +0800533 if (!f2fs_is_compress_backend_ready(inode))
534 return -EOPNOTSUPP;
535
Eric Biggers95ae2512019-07-22 09:26:24 -0700536 err = fsverity_file_open(inode, filp);
537 if (err)
538 return err;
539
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900540 filp->f_mode |= FMODE_NOWAIT;
541
Chao Yu0abd6752017-07-09 00:13:07 +0800542 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700543}
544
Chao Yu4d57b862018-05-30 00:20:41 +0800545void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900546{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700547 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900548 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800549 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900550 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800551 int base = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800552 bool compressed_cluster = false;
553 int cluster_index = 0, valid_blocks = 0;
554 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
Daeho Jeongc2759eb2020-09-08 11:44:10 +0900555 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
Chao Yu7a2af762017-07-19 00:19:06 +0800556
557 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
558 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900559
Gu Zheng45590712013-07-15 17:57:38 +0800560 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800561 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900562
Chao Yu4c8ff702019-11-01 18:07:14 +0800563 /* Assumption: truncateion starts with cluster */
564 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900565 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900566
Chao Yu4c8ff702019-11-01 18:07:14 +0800567 if (f2fs_compressed_file(dn->inode) &&
568 !(cluster_index & (cluster_size - 1))) {
569 if (compressed_cluster)
570 f2fs_i_compr_blocks_update(dn->inode,
571 valid_blocks, false);
572 compressed_cluster = (blkaddr == COMPRESS_ADDR);
573 valid_blocks = 0;
574 }
575
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900576 if (blkaddr == NULL_ADDR)
577 continue;
578
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800579 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800580 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800581
Chao Yu4c8ff702019-11-01 18:07:14 +0800582 if (__is_valid_data_blkaddr(blkaddr)) {
583 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800584 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800585 continue;
586 if (compressed_cluster)
587 valid_blocks++;
588 }
Chao Yuc9b60782018-08-01 19:13:44 +0800589
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700590 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700591 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800592
593 f2fs_invalidate_blocks(sbi, blkaddr);
Chao Yuef8d5632020-03-06 15:36:09 +0800594
595 if (!released || blkaddr != COMPRESS_ADDR)
596 nr_free++;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900597 }
Chao Yu19b2c302015-08-26 20:34:48 +0800598
Chao Yu4c8ff702019-11-01 18:07:14 +0800599 if (compressed_cluster)
600 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
601
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900602 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800603 pgoff_t fofs;
604 /*
605 * once we invalidate valid blkaddr in range [ofs, ofs + count],
606 * we will invalidate all blkaddr in the whole range.
607 */
Chao Yu4d57b862018-05-30 00:20:41 +0800608 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800609 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800610 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900611 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900612 }
613 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900614
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800615 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900616 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
617 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900618}
619
Chao Yu4d57b862018-05-30 00:20:41 +0800620void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900621{
Chao Yud02a6e62019-03-25 21:08:19 +0800622 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900623}
624
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800625static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700626 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900627{
youngjun yoo193bea12018-05-30 04:21:14 +0900628 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300629 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700630 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900631 struct page *page;
632
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700633 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700634 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900635
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700636 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700637 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700638 if (page && PageUptodate(page))
639 goto truncate_out;
640 f2fs_put_page(page, 1);
641 return 0;
642 }
643
Chao Yu4d57b862018-05-30 00:20:41 +0800644 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900645 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800646 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700647truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800648 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300649 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700650
651 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530652 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700653 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800654 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900655 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700656 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900657}
658
Chao Yu3265d3d2020-03-18 16:22:59 +0800659int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900660{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700661 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900662 struct dnode_of_data dn;
663 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800664 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700665 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800666 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900667
Namjae Jeon51dd6242013-04-20 01:28:52 +0900668 trace_f2fs_truncate_blocks_enter(inode, from);
669
Chao Yudf033ca2018-03-20 23:08:29 +0800670 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900671
Chao Yu09210c92016-05-05 19:13:03 +0800672 if (free_from >= sbi->max_file_blocks)
673 goto free_partial;
674
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700675 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800676 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800677
Chao Yu4d57b862018-05-30 00:20:41 +0800678 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700679 if (IS_ERR(ipage)) {
680 err = PTR_ERR(ipage);
681 goto out;
682 }
683
684 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800685 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700686 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800687 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700688 goto out;
689 }
690
691 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800692 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900693 if (err) {
694 if (err == -ENOENT)
695 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700696 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700697 }
698
Chao Yu81ca7352016-01-26 15:39:35 +0800699 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900700
701 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700702 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900703
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900704 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800705 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900706 free_from += count;
707 }
708
709 f2fs_put_dnode(&dn);
710free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800711 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800712out:
713 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800714 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800715free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700716 /* lastly zero out the first data page */
717 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800718 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719
Namjae Jeon51dd6242013-04-20 01:28:52 +0900720 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900721 return err;
722}
723
Chao Yu4c8ff702019-11-01 18:07:14 +0800724int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
725{
726 u64 free_from = from;
Chao Yu3265d3d2020-03-18 16:22:59 +0800727 int err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800728
Chao Yu3265d3d2020-03-18 16:22:59 +0800729#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800730 /*
731 * for compressed file, only support cluster size
732 * aligned truncation.
733 */
Chao Yu4fec3fc02020-04-08 19:55:17 +0800734 if (f2fs_compressed_file(inode))
735 free_from = round_up(from,
736 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
Chao Yu3265d3d2020-03-18 16:22:59 +0800737#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800738
Chao Yu3265d3d2020-03-18 16:22:59 +0800739 err = f2fs_do_truncate_blocks(inode, free_from, lock);
740 if (err)
741 return err;
742
743#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu17d76482020-08-10 18:38:45 +0800744 if (from != free_from) {
Chao Yu3265d3d2020-03-18 16:22:59 +0800745 err = f2fs_truncate_partial_cluster(inode, from, lock);
Chao Yu17d76482020-08-10 18:38:45 +0800746 if (err)
747 return err;
748 }
Chao Yu3265d3d2020-03-18 16:22:59 +0800749#endif
750
Chao Yu17d76482020-08-10 18:38:45 +0800751 return 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800752}
753
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700754int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900755{
Chao Yub0154892015-08-24 17:39:42 +0800756 int err;
757
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200758 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
759 return -EIO;
760
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900761 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
762 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800763 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900764
Namjae Jeon51dd6242013-04-20 01:28:52 +0900765 trace_f2fs_truncate(inode);
766
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800767 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800768 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800769 return -EIO;
770 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200771
Yi Chena9553ae2021-01-28 17:02:56 +0800772 err = dquot_initialize(inode);
773 if (err)
774 return err;
775
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800776 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800777 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800778 err = f2fs_convert_inline_inode(inode);
779 if (err)
780 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800781 }
782
Chao Yuc42d28c2019-02-02 17:33:01 +0800783 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800784 if (err)
785 return err;
786
Deepa Dinamani078cd822016-09-14 07:48:04 -0700787 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700788 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800789 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900790}
791
David Howellsa528d352017-01-31 16:46:22 +0000792int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800793 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900794{
David Howellsa528d352017-01-31 16:46:22 +0000795 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800796 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800797 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800798 unsigned int flags;
799
Chao Yu1c1d35d2018-01-25 14:54:42 +0800800 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800801 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800802 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
803 stat->result_mask |= STATX_BTIME;
804 stat->btime.tv_sec = fi->i_crtime.tv_sec;
805 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
806 }
807
Eric Biggers36098552019-06-04 22:59:04 -0700808 flags = fi->i_flags;
Chao Yufd267252020-03-25 10:22:09 +0800809 if (flags & F2FS_COMPR_FL)
810 stat->attributes |= STATX_ATTR_COMPRESSED;
Chao Yu59c84402018-04-03 15:08:17 +0800811 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800812 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530813 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800814 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800815 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800816 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800817 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800818 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700819 if (IS_VERITY(inode))
820 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800821
Chao Yufd267252020-03-25 10:22:09 +0800822 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
823 STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800824 STATX_ATTR_ENCRYPTED |
825 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700826 STATX_ATTR_NODUMP |
827 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800828
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900829 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700830
831 /* we need to show initial sectors used for inline_data/dentries */
832 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
833 f2fs_has_inline_dentry(inode))
834 stat->blocks += (stat->size + 511) >> 9;
835
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900836 return 0;
837}
838
839#ifdef CONFIG_F2FS_FS_POSIX_ACL
840static void __setattr_copy(struct inode *inode, const struct iattr *attr)
841{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900842 unsigned int ia_valid = attr->ia_valid;
843
844 if (ia_valid & ATTR_UID)
845 inode->i_uid = attr->ia_uid;
846 if (ia_valid & ATTR_GID)
847 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200848 if (ia_valid & ATTR_ATIME)
849 inode->i_atime = attr->ia_atime;
850 if (ia_valid & ATTR_MTIME)
851 inode->i_mtime = attr->ia_mtime;
852 if (ia_valid & ATTR_CTIME)
853 inode->i_ctime = attr->ia_ctime;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900854 if (ia_valid & ATTR_MODE) {
855 umode_t mode = attr->ia_mode;
856
Chao Yue3912392020-12-16 17:15:23 +0800857 if (!in_group_p(inode->i_gid) &&
858 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900859 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700860 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900861 }
862}
863#else
864#define __setattr_copy setattr_copy
865#endif
866
867int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
868{
David Howells2b0143b2015-03-17 22:25:59 +0000869 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900870 int err;
871
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200872 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
873 return -EIO;
874
Chao Yu04a49572020-12-26 18:07:01 +0800875 if (unlikely(IS_IMMUTABLE(inode)))
876 return -EPERM;
877
878 if (unlikely(IS_APPEND(inode) &&
879 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
880 ATTR_GID | ATTR_TIMES_SET))))
881 return -EPERM;
882
Chao Yu4c8ff702019-11-01 18:07:14 +0800883 if ((attr->ia_valid & ATTR_SIZE) &&
884 !f2fs_is_compress_backend_ready(inode))
885 return -EOPNOTSUPP;
886
Jan Kara31051c82016-05-26 16:55:18 +0200887 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900888 if (err)
889 return err;
890
Eric Biggers20bb2472017-11-29 12:35:32 -0800891 err = fscrypt_prepare_setattr(dentry, attr);
892 if (err)
893 return err;
894
Eric Biggers95ae2512019-07-22 09:26:24 -0700895 err = fsverity_prepare_setattr(dentry, attr);
896 if (err)
897 return err;
898
Chao Yu0abd6752017-07-09 00:13:07 +0800899 if (is_quota_modification(inode, attr)) {
900 err = dquot_initialize(inode);
901 if (err)
902 return err;
903 }
904 if ((attr->ia_valid & ATTR_UID &&
905 !uid_eq(attr->ia_uid, inode->i_uid)) ||
906 (attr->ia_valid & ATTR_GID &&
907 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800908 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800909 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800910 if (err) {
911 set_sbi_flag(F2FS_I_SB(inode),
912 SBI_QUOTA_NEED_REPAIR);
913 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800914 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800915 }
916 /*
917 * update uid/gid under lock_op(), so that dquot and inode can
918 * be updated atomically.
919 */
920 if (attr->ia_valid & ATTR_UID)
921 inode->i_uid = attr->ia_uid;
922 if (attr->ia_valid & ATTR_GID)
923 inode->i_gid = attr->ia_gid;
924 f2fs_mark_inode_dirty_sync(inode, true);
925 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800926 }
927
Chao Yu09db6a22014-09-15 18:02:09 +0800928 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800929 loff_t old_size = i_size_read(inode);
930
931 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
932 /*
933 * should convert inline inode before i_size_write to
934 * keep smaller than inline_data size with inline flag.
935 */
936 err = f2fs_convert_inline_inode(inode);
937 if (err)
938 return err;
939 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800940
Chao Yua33c1502018-08-05 23:04:25 +0800941 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900942 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800943
944 truncate_setsize(inode, attr->ia_size);
945
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800946 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800947 err = f2fs_truncate(inode);
948 /*
949 * do not trim all blocks after i_size if target size is
950 * larger than i_size.
951 */
Chao Yua33c1502018-08-05 23:04:25 +0800952 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900953 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800954 if (err)
955 return err;
956
Chao Yuc10c9822020-02-27 19:30:03 +0800957 spin_lock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800958 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800959 F2FS_I(inode)->last_disk_size = i_size_read(inode);
Chao Yuc10c9822020-02-27 19:30:03 +0800960 spin_unlock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900961 }
962
963 __setattr_copy(inode, attr);
964
965 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800966 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700967 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
968 inode->i_mode = F2FS_I(inode)->i_acl_mode;
969 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900970 }
971 }
972
Yunlei Hec0ed4402016-12-11 15:35:15 +0800973 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800974 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700975
976 /* inode change will produce dirty node pages flushed by checkpoint */
977 f2fs_balance_fs(F2FS_I_SB(inode), true);
978
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900979 return err;
980}
981
982const struct inode_operations f2fs_file_inode_operations = {
983 .getattr = f2fs_getattr,
984 .setattr = f2fs_setattr,
985 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800986 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900987 .listxattr = f2fs_listxattr,
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900988 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900989};
990
Chao Yu63943282015-08-07 18:36:06 +0800991static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900992 loff_t start, loff_t len)
993{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700994 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900995 struct page *page;
996
997 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800998 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900999
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001000 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +09001001
Gu Zhenge4795562013-09-27 18:08:30 +08001002 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001003 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +08001004 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001005
Chao Yu63943282015-08-07 18:36:06 +08001006 if (IS_ERR(page))
1007 return PTR_ERR(page);
1008
Chao Yubae0ee72018-12-25 17:43:42 +08001009 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +08001010 zero_user(page, start, len);
1011 set_page_dirty(page);
1012 f2fs_put_page(page, 1);
1013 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001014}
1015
Chao Yu4d57b862018-05-30 00:20:41 +08001016int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001017{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001018 int err;
1019
Chao Yuea587112015-09-17 20:22:44 +08001020 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001021 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001022 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001023
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001024 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001025 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001026 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001027 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001028 pg_start = f2fs_get_next_page_offset(&dn,
1029 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001030 continue;
Chao Yuea587112015-09-17 20:22:44 +08001031 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001032 return err;
1033 }
1034
Chao Yu81ca7352016-01-26 15:39:35 +08001035 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001036 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1037
1038 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1039
Chao Yu4d57b862018-05-30 00:20:41 +08001040 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001041 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001042
1043 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001044 }
1045 return 0;
1046}
1047
Chao Yua66c7b22013-11-22 16:52:50 +08001048static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001049{
1050 pgoff_t pg_start, pg_end;
1051 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001052 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001053
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001054 ret = f2fs_convert_inline_inode(inode);
1055 if (ret)
1056 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001057
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001058 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1059 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001060
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001061 off_start = offset & (PAGE_SIZE - 1);
1062 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001063
1064 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001065 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001066 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001067 if (ret)
1068 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001069 } else {
Chao Yu63943282015-08-07 18:36:06 +08001070 if (off_start) {
1071 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001072 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001073 if (ret)
1074 return ret;
1075 }
1076 if (off_end) {
1077 ret = fill_zero(inode, pg_end, 0, off_end);
1078 if (ret)
1079 return ret;
1080 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001081
1082 if (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001083 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001084 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001085
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001086 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001087
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001088 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1089 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001090
Chao Yua33c1502018-08-05 23:04:25 +08001091 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001092 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001093
Chao Yu4b719282021-08-25 19:34:19 +08001094 truncate_pagecache_range(inode, blk_start, blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001095
Gu Zhenge4795562013-09-27 18:08:30 +08001096 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001097 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001098 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001099
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001100 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001101 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001102 }
1103 }
1104
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001105 return ret;
1106}
1107
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001108static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1109 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001110{
1111 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1112 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001113 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001114
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001115next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001116 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001117 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001118 if (ret && ret != -ENOENT) {
1119 return ret;
1120 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001121 if (dn.max_level == 0)
1122 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001123 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1124 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001125 blkaddr += done;
1126 do_replace += done;
1127 goto next;
1128 }
1129
1130 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1131 dn.ofs_in_node, len);
1132 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001133 *blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu93770ab2019-04-15 15:26:32 +08001134
1135 if (__is_valid_data_blkaddr(*blkaddr) &&
1136 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1137 DATA_GENERIC_ENHANCE)) {
1138 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001139 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001140 }
1141
Chao Yu4d57b862018-05-30 00:20:41 +08001142 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001143
Chao Yub0332a02020-02-14 17:44:12 +08001144 if (f2fs_lfs_mode(sbi)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001145 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001146 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001147 }
1148
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001149 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001150 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001151 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001152 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001153 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001154 f2fs_put_dnode(&dn);
1155next:
1156 len -= done;
1157 off += done;
1158 if (len)
1159 goto next_dnode;
1160 return 0;
1161}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001162
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001163static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1164 int *do_replace, pgoff_t off, int len)
1165{
1166 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1167 struct dnode_of_data dn;
1168 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001169
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001170 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1171 if (*do_replace == 0)
1172 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001173
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001174 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001175 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001176 if (ret) {
1177 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001178 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001179 } else {
1180 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001181 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001182 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001183 }
1184 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001185}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001186
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001187static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1188 block_t *blkaddr, int *do_replace,
1189 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1190{
1191 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1192 pgoff_t i = 0;
1193 int ret;
1194
1195 while (i < len) {
1196 if (blkaddr[i] == NULL_ADDR && !full) {
1197 i++;
1198 continue;
1199 }
1200
1201 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1202 struct dnode_of_data dn;
1203 struct node_info ni;
1204 size_t new_size;
1205 pgoff_t ilen;
1206
1207 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001208 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001209 if (ret)
1210 return ret;
1211
Chao Yu77357302018-07-17 00:02:17 +08001212 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1213 if (ret) {
1214 f2fs_put_dnode(&dn);
1215 return ret;
1216 }
1217
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001218 ilen = min((pgoff_t)
1219 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1220 dn.ofs_in_node, len - i);
1221 do {
Chao Yua2ced1c2020-02-14 17:44:10 +08001222 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu4d57b862018-05-30 00:20:41 +08001223 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001224
1225 if (do_replace[i]) {
1226 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001227 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001228 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001229 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001230 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1231 blkaddr[i], ni.version, true, false);
1232
1233 do_replace[i] = 0;
1234 }
1235 dn.ofs_in_node++;
1236 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001237 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001238 if (dst_inode->i_size < new_size)
1239 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001240 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001241
1242 f2fs_put_dnode(&dn);
1243 } else {
1244 struct page *psrc, *pdst;
1245
Chao Yu4d57b862018-05-30 00:20:41 +08001246 psrc = f2fs_get_lock_data_page(src_inode,
1247 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001248 if (IS_ERR(psrc))
1249 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001250 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001251 true);
1252 if (IS_ERR(pdst)) {
1253 f2fs_put_page(psrc, 1);
1254 return PTR_ERR(pdst);
1255 }
1256 f2fs_copy_page(psrc, pdst);
1257 set_page_dirty(pdst);
1258 f2fs_put_page(pdst, 1);
1259 f2fs_put_page(psrc, 1);
1260
Chao Yu4d57b862018-05-30 00:20:41 +08001261 ret = f2fs_truncate_hole(src_inode,
1262 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001263 if (ret)
1264 return ret;
1265 i++;
1266 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001267 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001268 return 0;
1269}
1270
1271static int __exchange_data_block(struct inode *src_inode,
1272 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001273 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001274{
1275 block_t *src_blkaddr;
1276 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001277 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001278 int ret;
1279
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001280 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001281 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001282
Chao Yu628b3d12017-11-30 19:28:18 +08001283 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001284 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001285 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001286 if (!src_blkaddr)
1287 return -ENOMEM;
1288
Chao Yu628b3d12017-11-30 19:28:18 +08001289 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001290 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001291 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001292 if (!do_replace) {
1293 kvfree(src_blkaddr);
1294 return -ENOMEM;
1295 }
1296
1297 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1298 do_replace, src, olen);
1299 if (ret)
1300 goto roll_back;
1301
1302 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1303 do_replace, src, dst, olen, full);
1304 if (ret)
1305 goto roll_back;
1306
1307 src += olen;
1308 dst += olen;
1309 len -= olen;
1310
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001311 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001312 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001313 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001314 return 0;
1315
1316roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001317 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001318 kvfree(src_blkaddr);
1319 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001320 return ret;
1321}
1322
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001323static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001324{
1325 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001326 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001327 pgoff_t start = offset >> PAGE_SHIFT;
1328 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001329 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001330
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001331 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001332
1333 /* avoid gc operation during block exchange */
1334 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1335 down_write(&F2FS_I(inode)->i_mmap_sem);
1336
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001337 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001338 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001339 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001340 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1341 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001342
1343 up_write(&F2FS_I(inode)->i_mmap_sem);
1344 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001345 return ret;
1346}
1347
1348static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1349{
Chao Yub4ace332015-05-06 13:09:46 +08001350 loff_t new_size;
1351 int ret;
1352
Chao Yub4ace332015-05-06 13:09:46 +08001353 if (offset + len >= i_size_read(inode))
1354 return -EINVAL;
1355
1356 /* collapse range should be aligned to block size of f2fs. */
1357 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1358 return -EINVAL;
1359
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001360 ret = f2fs_convert_inline_inode(inode);
1361 if (ret)
1362 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001363
Chao Yub4ace332015-05-06 13:09:46 +08001364 /* write out all dirty pages from offset */
1365 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1366 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001367 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001368
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001369 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001370 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001371 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001372
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001373 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001374 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001375 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1376 truncate_pagecache(inode, offset);
1377
Chao Yub4ace332015-05-06 13:09:46 +08001378 new_size = i_size_read(inode) - len;
Chao Yuc42d28c2019-02-02 17:33:01 +08001379 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001380 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001381 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001382 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001383 return ret;
1384}
1385
Chao Yu6e961942016-05-09 19:56:31 +08001386static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1387 pgoff_t end)
1388{
1389 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1390 pgoff_t index = start;
1391 unsigned int ofs_in_node = dn->ofs_in_node;
1392 blkcnt_t count = 0;
1393 int ret;
1394
1395 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001396 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001397 count++;
1398 }
1399
1400 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001401 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001402 if (ret)
1403 return ret;
1404
1405 dn->ofs_in_node = ofs_in_node;
1406 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001407 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001408 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001409 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001410 * allocation.
1411 */
1412 if (dn->data_blkaddr == NULL_ADDR) {
1413 ret = -ENOSPC;
1414 break;
1415 }
1416 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001417 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001418 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001419 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001420 }
1421 }
1422
1423 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1424
1425 return ret;
1426}
1427
Chao Yu75cd4e02015-05-06 13:11:13 +08001428static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1429 int mode)
1430{
1431 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1432 struct address_space *mapping = inode->i_mapping;
1433 pgoff_t index, pg_start, pg_end;
1434 loff_t new_size = i_size_read(inode);
1435 loff_t off_start, off_end;
1436 int ret = 0;
1437
Chao Yu75cd4e02015-05-06 13:11:13 +08001438 ret = inode_newsize_ok(inode, (len + offset));
1439 if (ret)
1440 return ret;
1441
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001442 ret = f2fs_convert_inline_inode(inode);
1443 if (ret)
1444 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001445
1446 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1447 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001448 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001449
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1451 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001452
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001453 off_start = offset & (PAGE_SIZE - 1);
1454 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001455
1456 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001457 ret = fill_zero(inode, pg_start, off_start,
1458 off_end - off_start);
1459 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001460 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001461
Chao Yu75cd4e02015-05-06 13:11:13 +08001462 new_size = max_t(loff_t, new_size, offset + len);
1463 } else {
1464 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001465 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001466 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001467 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001468 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001469
Chao Yu75cd4e02015-05-06 13:11:13 +08001470 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001471 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001472 }
1473
Chao Yu6e961942016-05-09 19:56:31 +08001474 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001475 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001476 unsigned int end_offset;
1477 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001478
Chao Yuc70798532018-08-05 23:02:22 +08001479 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001480 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001481
1482 truncate_pagecache_range(inode,
1483 (loff_t)index << PAGE_SHIFT,
1484 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1485
Chao Yu75cd4e02015-05-06 13:11:13 +08001486 f2fs_lock_op(sbi);
1487
Chao Yu6e961942016-05-09 19:56:31 +08001488 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001489 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001490 if (ret) {
1491 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001492 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001493 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001494 goto out;
1495 }
1496
Chao Yu6e961942016-05-09 19:56:31 +08001497 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1498 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1499
1500 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001501 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001502
Chao Yu75cd4e02015-05-06 13:11:13 +08001503 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001504 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001505 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001506
1507 f2fs_balance_fs(sbi, dn.node_changed);
1508
Chao Yu6e961942016-05-09 19:56:31 +08001509 if (ret)
1510 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001511
Chao Yu6e961942016-05-09 19:56:31 +08001512 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001513 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001514 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001515 }
1516
1517 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001518 ret = fill_zero(inode, pg_end, 0, off_end);
1519 if (ret)
1520 goto out;
1521
Chao Yu75cd4e02015-05-06 13:11:13 +08001522 new_size = max_t(loff_t, new_size, offset + len);
1523 }
1524 }
1525
1526out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001527 if (new_size > i_size_read(inode)) {
1528 if (mode & FALLOC_FL_KEEP_SIZE)
1529 file_set_keep_isize(inode);
1530 else
1531 f2fs_i_size_write(inode, new_size);
1532 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001533 return ret;
1534}
1535
Chao Yuf62185d2015-05-28 19:16:57 +08001536static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1537{
1538 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001539 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001540 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001541 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001542
Chao Yuf62185d2015-05-28 19:16:57 +08001543 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001544 ret = inode_newsize_ok(inode, new_size);
1545 if (ret)
1546 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001547
1548 if (offset >= i_size_read(inode))
1549 return -EINVAL;
1550
1551 /* insert range should be aligned to block size of f2fs. */
1552 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1553 return -EINVAL;
1554
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001555 ret = f2fs_convert_inline_inode(inode);
1556 if (ret)
1557 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001558
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001559 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001560
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001561 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001562 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001563 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001564 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001565 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001566
1567 /* write out all dirty pages from offset */
1568 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1569 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001570 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001571
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001572 pg_start = offset >> PAGE_SHIFT;
1573 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001574 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001575 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001576
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001577 /* avoid gc operation during block exchange */
1578 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1579 down_write(&F2FS_I(inode)->i_mmap_sem);
1580 truncate_pagecache(inode, offset);
1581
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001582 while (!ret && idx > pg_start) {
1583 nr = idx - pg_start;
1584 if (nr > delta)
1585 nr = delta;
1586 idx -= nr;
1587
Chao Yuf62185d2015-05-28 19:16:57 +08001588 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001589 f2fs_drop_extent_tree(inode);
1590
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001591 ret = __exchange_data_block(inode, inode, idx,
1592 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001593 f2fs_unlock_op(sbi);
1594 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001595 up_write(&F2FS_I(inode)->i_mmap_sem);
1596 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001597
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001598 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001599 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001600 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1601 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001602 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001603
1604 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001605 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001606 return ret;
1607}
1608
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001609static int expand_inode_data(struct inode *inode, loff_t offset,
1610 loff_t len, int mode)
1611{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001612 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001613 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001614 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1615 .m_may_create = true };
Chao Yu8c8f7c42021-03-24 11:24:33 +08001616 pgoff_t pg_start, pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001617 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001618 loff_t off_end;
Chao Yu8c8f7c42021-03-24 11:24:33 +08001619 block_t expanded = 0;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001620 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001621
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001622 err = inode_newsize_ok(inode, (len + offset));
1623 if (err)
1624 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001625
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001626 err = f2fs_convert_inline_inode(inode);
1627 if (err)
1628 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001629
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001630 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001631
Chao Yu8c8f7c42021-03-24 11:24:33 +08001632 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001633 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001634 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001635
Chao Yu8c8f7c42021-03-24 11:24:33 +08001636 map.m_lblk = pg_start;
1637 map.m_len = pg_end - pg_start;
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001638 if (off_end)
1639 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001640
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001641 if (!map.m_len)
1642 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001643
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001644 if (f2fs_is_pinned_file(inode)) {
Chao Yu74d2b0e2021-03-05 17:56:01 +08001645 block_t sec_blks = BLKS_PER_SEC(sbi);
1646 block_t sec_len = roundup(map.m_len, sec_blks);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001647
Chao Yu74d2b0e2021-03-05 17:56:01 +08001648 map.m_len = sec_blks;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001649next_alloc:
1650 if (has_not_enough_free_secs(sbi, 0,
1651 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
Chao Yufb24fea2020-01-14 19:36:50 +08001652 down_write(&sbi->gc_lock);
Chao Yu81ba1632021-02-20 17:35:40 +08001653 err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
Chao Yu8c8f7c42021-03-24 11:24:33 +08001654 if (err && err != -ENODATA && err != -EAGAIN)
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001655 goto out_err;
1656 }
1657
1658 down_write(&sbi->pin_sem);
Daeho Jeongfd612642020-05-27 13:02:31 +09001659
1660 f2fs_lock_op(sbi);
Chao Yu74d2b0e2021-03-05 17:56:01 +08001661 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED);
Daeho Jeongfd612642020-05-27 13:02:31 +09001662 f2fs_unlock_op(sbi);
1663
Chao Yud0b9e422020-08-04 21:14:45 +08001664 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001665 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Chao Yud0b9e422020-08-04 21:14:45 +08001666
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001667 up_write(&sbi->pin_sem);
1668
Chao Yu8c8f7c42021-03-24 11:24:33 +08001669 expanded += map.m_len;
Chao Yu74d2b0e2021-03-05 17:56:01 +08001670 sec_len -= map.m_len;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001671 map.m_lblk += map.m_len;
Chao Yu74d2b0e2021-03-05 17:56:01 +08001672 if (!err && sec_len)
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001673 goto next_alloc;
1674
Chao Yu8c8f7c42021-03-24 11:24:33 +08001675 map.m_len = expanded;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001676 } else {
1677 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Chao Yu8c8f7c42021-03-24 11:24:33 +08001678 expanded = map.m_len;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001679 }
1680out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001681 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001682 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001683
Chao Yu8c8f7c42021-03-24 11:24:33 +08001684 if (!expanded)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001685 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001686
Chao Yu8c8f7c42021-03-24 11:24:33 +08001687 last_off = pg_start + expanded - 1;
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001688
1689 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001690 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001691 (loff_t)(last_off + 1) << PAGE_SHIFT;
1692 } else {
1693 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001694 }
1695
Chao Yue8ed90a2017-11-05 21:53:30 +08001696 if (new_size > i_size_read(inode)) {
1697 if (mode & FALLOC_FL_KEEP_SIZE)
1698 file_set_keep_isize(inode);
1699 else
1700 f2fs_i_size_write(inode, new_size);
1701 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001702
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001703 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001704}
1705
1706static long f2fs_fallocate(struct file *file, int mode,
1707 loff_t offset, loff_t len)
1708{
Al Viro6131ffa2013-02-27 16:59:05 -05001709 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001710 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001711
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001712 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1713 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001714 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1715 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001716 if (!f2fs_is_compress_backend_ready(inode))
1717 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001718
Chao Yuc9980122015-09-11 14:39:02 +08001719 /* f2fs only support ->fallocate for regular file */
1720 if (!S_ISREG(inode->i_mode))
1721 return -EINVAL;
1722
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301723 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001724 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001725 return -EOPNOTSUPP;
1726
Chao Yu4c8ff702019-11-01 18:07:14 +08001727 if (f2fs_compressed_file(inode) &&
1728 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1729 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1730 return -EOPNOTSUPP;
1731
Chao Yub4ace332015-05-06 13:09:46 +08001732 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001733 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1734 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001735 return -EOPNOTSUPP;
1736
Al Viro59551022016-01-22 15:40:57 -05001737 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001738
Taehee Yoo587c0a42015-04-21 15:59:12 +09001739 if (mode & FALLOC_FL_PUNCH_HOLE) {
1740 if (offset >= inode->i_size)
1741 goto out;
1742
Chao Yua66c7b22013-11-22 16:52:50 +08001743 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001744 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1745 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001746 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1747 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001748 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1749 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001750 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001751 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001752 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001753
Namjae Jeon3af60a42012-12-30 14:52:37 +09001754 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001755 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001756 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001757 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001758 }
Chao Yu3375f692014-01-28 10:29:26 +08001759
Taehee Yoo587c0a42015-04-21 15:59:12 +09001760out:
Al Viro59551022016-01-22 15:40:57 -05001761 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001762
Namjae Jeonc01e2852013-04-23 17:00:52 +09001763 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001764 return ret;
1765}
1766
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001767static int f2fs_release_file(struct inode *inode, struct file *filp)
1768{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001769 /*
1770 * f2fs_relase_file is called at every close calls. So we should
1771 * not drop any inmemory pages by close called by other process.
1772 */
1773 if (!(filp->f_mode & FMODE_WRITE) ||
1774 atomic_read(&inode->i_writecount) != 1)
1775 return 0;
1776
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001777 /* some remained atomic pages should discarded */
1778 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001779 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001780 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001781 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001782 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001783 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001784 clear_inode_flag(inode, FI_VOLATILE_FILE);
1785 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001786 }
1787 return 0;
1788}
1789
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001790static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001791{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001792 struct inode *inode = file_inode(file);
1793
1794 /*
1795 * If the process doing a transaction is crashed, we should do
1796 * roll-back. Otherwise, other reader/write can see corrupted database
1797 * until all the writers close its file. Since this should be done
1798 * before dropping file lock, it needs to do in ->flush.
1799 */
1800 if (f2fs_is_atomic_file(inode) &&
1801 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001802 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001803 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001804}
1805
Eric Biggers36098552019-06-04 22:59:04 -07001806static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001807{
1808 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001809 u32 masked_flags = fi->i_flags & mask;
1810
1811 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
Chao Yu2c1d0302017-07-29 00:32:52 +08001812
1813 /* Is it quota file? Do not allow user to mess with it */
1814 if (IS_NOQUOTA(inode))
1815 return -EPERM;
1816
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001817 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001818 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1819 return -EOPNOTSUPP;
1820 if (!f2fs_empty_dir(inode))
1821 return -ENOTEMPTY;
1822 }
1823
Chao Yu4c8ff702019-11-01 18:07:14 +08001824 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1825 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1826 return -EOPNOTSUPP;
1827 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1828 return -EINVAL;
1829 }
1830
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001831 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
Chao Yuaa576972020-03-27 18:29:51 +08001832 if (masked_flags & F2FS_COMPR_FL) {
Daeho Jeong78134d02020-09-08 11:44:11 +09001833 if (!f2fs_disable_compressed_file(inode))
Chao Yu2536ac62020-03-10 20:50:09 +08001834 return -EINVAL;
1835 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001836 if (iflags & F2FS_NOCOMP_FL)
1837 return -EINVAL;
1838 if (iflags & F2FS_COMPR_FL) {
Chao Yu4c8ff702019-11-01 18:07:14 +08001839 if (!f2fs_may_compress(inode))
1840 return -EINVAL;
Chao Yu519a5a22020-09-18 11:03:49 +08001841 if (S_ISREG(inode->i_mode) && inode->i_size)
1842 return -EINVAL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001843
1844 set_compress_context(inode);
1845 }
1846 }
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001847 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1848 if (masked_flags & F2FS_COMPR_FL)
Chao Yu4c8ff702019-11-01 18:07:14 +08001849 return -EINVAL;
1850 }
1851
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001852 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001853 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1854 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001855
Chao Yu59c84402018-04-03 15:08:17 +08001856 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001857 set_inode_flag(inode, FI_PROJ_INHERIT);
1858 else
1859 clear_inode_flag(inode, FI_PROJ_INHERIT);
1860
1861 inode->i_ctime = current_time(inode);
1862 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001863 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001864 return 0;
1865}
1866
Eric Biggers36098552019-06-04 22:59:04 -07001867/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1868
1869/*
1870 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1871 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1872 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1873 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1874 */
1875
1876static const struct {
1877 u32 iflag;
1878 u32 fsflag;
1879} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001880 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001881 { F2FS_SYNC_FL, FS_SYNC_FL },
1882 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1883 { F2FS_APPEND_FL, FS_APPEND_FL },
1884 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1885 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001886 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001887 { F2FS_INDEX_FL, FS_INDEX_FL },
1888 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1889 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001890 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001891};
1892
1893#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001894 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001895 FS_SYNC_FL | \
1896 FS_IMMUTABLE_FL | \
1897 FS_APPEND_FL | \
1898 FS_NODUMP_FL | \
1899 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001900 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001901 FS_INDEX_FL | \
1902 FS_DIRSYNC_FL | \
1903 FS_PROJINHERIT_FL | \
1904 FS_ENCRYPT_FL | \
1905 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001906 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001907 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001908 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001909
1910#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001911 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001912 FS_SYNC_FL | \
1913 FS_IMMUTABLE_FL | \
1914 FS_APPEND_FL | \
1915 FS_NODUMP_FL | \
1916 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001917 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001918 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001919 FS_PROJINHERIT_FL | \
1920 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001921
1922/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1923static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1924{
1925 u32 fsflags = 0;
1926 int i;
1927
1928 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1929 if (iflags & f2fs_fsflags_map[i].iflag)
1930 fsflags |= f2fs_fsflags_map[i].fsflag;
1931
1932 return fsflags;
1933}
1934
1935/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1936static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1937{
1938 u32 iflags = 0;
1939 int i;
1940
1941 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1942 if (fsflags & f2fs_fsflags_map[i].fsflag)
1943 iflags |= f2fs_fsflags_map[i].iflag;
1944
1945 return iflags;
1946}
1947
1948static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1949{
1950 struct inode *inode = file_inode(filp);
1951 struct f2fs_inode_info *fi = F2FS_I(inode);
1952 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1953
1954 if (IS_ENCRYPTED(inode))
1955 fsflags |= FS_ENCRYPT_FL;
Eric Biggers95ae2512019-07-22 09:26:24 -07001956 if (IS_VERITY(inode))
1957 fsflags |= FS_VERITY_FL;
Eric Biggers36098552019-06-04 22:59:04 -07001958 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1959 fsflags |= FS_INLINE_DATA_FL;
1960 if (is_inode_flag_set(inode, FI_PIN_FILE))
1961 fsflags |= FS_NOCOW_FL;
1962
1963 fsflags &= F2FS_GETTABLE_FS_FL;
1964
1965 return put_user(fsflags, (int __user *)arg);
1966}
1967
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001968static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1969{
1970 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001971 struct f2fs_inode_info *fi = F2FS_I(inode);
1972 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001973 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001974 int ret;
1975
Chao Yu7fb17fe2016-05-09 19:56:32 +08001976 if (!inode_owner_or_capable(inode))
1977 return -EACCES;
1978
Eric Biggers36098552019-06-04 22:59:04 -07001979 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001980 return -EFAULT;
1981
Eric Biggers36098552019-06-04 22:59:04 -07001982 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1983 return -EOPNOTSUPP;
1984 fsflags &= F2FS_SETTABLE_FS_FL;
1985
1986 iflags = f2fs_fsflags_to_iflags(fsflags);
1987 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1988 return -EOPNOTSUPP;
1989
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001990 ret = mnt_want_write_file(filp);
1991 if (ret)
1992 return ret;
1993
Al Viro59551022016-01-22 15:40:57 -05001994 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001995
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001996 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1997 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1998 if (ret)
1999 goto out;
2000
Eric Biggers36098552019-06-04 22:59:04 -07002001 ret = f2fs_setflags_common(inode, iflags,
2002 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07002003out:
Chao Yua72d4b92017-05-03 23:59:13 +08002004 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002005 mnt_drop_write_file(filp);
2006 return ret;
2007}
2008
Chao Yud49f3e82015-01-23 20:36:04 +08002009static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2010{
2011 struct inode *inode = file_inode(filp);
2012
2013 return put_user(inode->i_generation, (int __user *)arg);
2014}
2015
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002016static int f2fs_ioc_start_atomic_write(struct file *filp)
2017{
2018 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002019 struct f2fs_inode_info *fi = F2FS_I(inode);
2020 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08002021 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002022
2023 if (!inode_owner_or_capable(inode))
2024 return -EACCES;
2025
Jaegeuk Kime8118982017-03-17 10:04:15 +08002026 if (!S_ISREG(inode->i_mode))
2027 return -EINVAL;
2028
Chao Yu038d0692019-07-25 22:39:11 +08002029 if (filp->f_flags & O_DIRECT)
2030 return -EINVAL;
2031
Chao Yu7fb17fe2016-05-09 19:56:32 +08002032 ret = mnt_want_write_file(filp);
2033 if (ret)
2034 return ret;
2035
Chao Yu0fac5582016-05-09 19:56:33 +08002036 inode_lock(inode);
2037
Fengnan Changd2c53e72022-03-18 09:23:04 +08002038 if (!f2fs_disable_compressed_file(inode)) {
2039 ret = -EINVAL;
2040 goto out;
2041 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002042
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002043 if (f2fs_is_atomic_file(inode)) {
2044 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2045 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002046 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002047 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002048
Chao Yuf4c9c742015-07-17 18:06:35 +08002049 ret = f2fs_convert_inline_inode(inode);
2050 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002051 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002052
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002053 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2054
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002055 /*
2056 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2057 * f2fs_is_atomic_file.
2058 */
2059 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002060 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2061 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002062 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002063 if (ret) {
2064 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002065 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002066 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002067
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002068 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2069 if (list_empty(&fi->inmem_ilist))
2070 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302071 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002072 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2073
2074 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002075 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002076 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002077 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002078
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002079 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002080 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002081 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002082out:
Chao Yu0fac5582016-05-09 19:56:33 +08002083 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002084 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002085 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002086}
2087
2088static int f2fs_ioc_commit_atomic_write(struct file *filp)
2089{
2090 struct inode *inode = file_inode(filp);
2091 int ret;
2092
2093 if (!inode_owner_or_capable(inode))
2094 return -EACCES;
2095
2096 ret = mnt_want_write_file(filp);
2097 if (ret)
2098 return ret;
2099
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002100 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002101
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002102 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002103
Chao Yub169c3c2018-04-18 17:45:02 +08002104 if (f2fs_is_volatile_file(inode)) {
2105 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002106 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002107 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002108
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002109 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002110 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002111 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002112 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002113
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002114 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002115 if (!ret)
2116 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002117 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002118 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002119 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002120err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002121 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2122 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2123 ret = -EINVAL;
2124 }
Chao Yu0fac5582016-05-09 19:56:33 +08002125 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002126 mnt_drop_write_file(filp);
2127 return ret;
2128}
2129
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002130static int f2fs_ioc_start_volatile_write(struct file *filp)
2131{
2132 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002133 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002134
2135 if (!inode_owner_or_capable(inode))
2136 return -EACCES;
2137
Chao Yu8ff09712017-03-17 15:43:57 +08002138 if (!S_ISREG(inode->i_mode))
2139 return -EINVAL;
2140
Chao Yu7fb17fe2016-05-09 19:56:32 +08002141 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002142 if (ret)
2143 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002144
Chao Yu0fac5582016-05-09 19:56:33 +08002145 inode_lock(inode);
2146
Chao Yu7fb17fe2016-05-09 19:56:32 +08002147 if (f2fs_is_volatile_file(inode))
2148 goto out;
2149
2150 ret = f2fs_convert_inline_inode(inode);
2151 if (ret)
2152 goto out;
2153
Chao Yu648d50b2017-03-22 17:23:45 +08002154 stat_inc_volatile_write(inode);
2155 stat_update_max_volatile_write(inode);
2156
Jaegeuk Kim91942322016-05-20 10:13:22 -07002157 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002158 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002159out:
Chao Yu0fac5582016-05-09 19:56:33 +08002160 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002161 mnt_drop_write_file(filp);
2162 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002163}
2164
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002165static int f2fs_ioc_release_volatile_write(struct file *filp)
2166{
2167 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002168 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002169
2170 if (!inode_owner_or_capable(inode))
2171 return -EACCES;
2172
Chao Yu7fb17fe2016-05-09 19:56:32 +08002173 ret = mnt_want_write_file(filp);
2174 if (ret)
2175 return ret;
2176
Chao Yu0fac5582016-05-09 19:56:33 +08002177 inode_lock(inode);
2178
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002179 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002180 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002181
Chao Yu7fb17fe2016-05-09 19:56:32 +08002182 if (!f2fs_is_first_block_written(inode)) {
2183 ret = truncate_partial_data_page(inode, 0, true);
2184 goto out;
2185 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002186
Chao Yu7fb17fe2016-05-09 19:56:32 +08002187 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2188out:
Chao Yu0fac5582016-05-09 19:56:33 +08002189 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002190 mnt_drop_write_file(filp);
2191 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002192}
2193
2194static int f2fs_ioc_abort_volatile_write(struct file *filp)
2195{
2196 struct inode *inode = file_inode(filp);
2197 int ret;
2198
2199 if (!inode_owner_or_capable(inode))
2200 return -EACCES;
2201
2202 ret = mnt_want_write_file(filp);
2203 if (ret)
2204 return ret;
2205
Chao Yu0fac5582016-05-09 19:56:33 +08002206 inode_lock(inode);
2207
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002208 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002209 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002210 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002211 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002212 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002213 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002214 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002215
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002216 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2217
Chao Yu0fac5582016-05-09 19:56:33 +08002218 inode_unlock(inode);
2219
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002220 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002221 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002222 return ret;
2223}
2224
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002225static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2226{
2227 struct inode *inode = file_inode(filp);
2228 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2229 struct super_block *sb = sbi->sb;
2230 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002231 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002232
2233 if (!capable(CAP_SYS_ADMIN))
2234 return -EPERM;
2235
2236 if (get_user(in, (__u32 __user *)arg))
2237 return -EFAULT;
2238
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302239 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2240 ret = mnt_want_write_file(filp);
Chao Yu86264412020-06-08 20:03:16 +08002241 if (ret) {
2242 if (ret == -EROFS) {
2243 ret = 0;
2244 f2fs_stop_checkpoint(sbi, false);
2245 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2246 trace_f2fs_shutdown(sbi, in, ret);
2247 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302248 return ret;
Chao Yu86264412020-06-08 20:03:16 +08002249 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302250 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002251
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002252 switch (in) {
2253 case F2FS_GOING_DOWN_FULLSYNC:
2254 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08002255 if (IS_ERR(sb)) {
2256 ret = PTR_ERR(sb);
2257 goto out;
2258 }
2259 if (sb) {
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 Kim1abff932015-01-08 19:15:53 -08002262 thaw_bdev(sb->s_bdev, sb);
2263 }
2264 break;
2265 case F2FS_GOING_DOWN_METASYNC:
2266 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002267 ret = f2fs_sync_fs(sb, 1);
2268 if (ret)
2269 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002270 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002271 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002272 break;
2273 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002274 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002275 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002276 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002277 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002278 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002279 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002280 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002281 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002282 case F2FS_GOING_DOWN_NEED_FSCK:
2283 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002284 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2285 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002286 /* do checkpoint only */
2287 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002288 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002289 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002290 ret = -EINVAL;
2291 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002292 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002293
Chao Yu4d57b862018-05-30 00:20:41 +08002294 f2fs_stop_gc_thread(sbi);
2295 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002296
Chao Yu4d57b862018-05-30 00:20:41 +08002297 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002298 clear_opt(sbi, DISCARD);
2299
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002300 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002301out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302302 if (in != F2FS_GOING_DOWN_FULLSYNC)
2303 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002304
2305 trace_f2fs_shutdown(sbi, in, ret);
2306
Chao Yu7fb17fe2016-05-09 19:56:32 +08002307 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002308}
2309
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002310static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2311{
2312 struct inode *inode = file_inode(filp);
2313 struct super_block *sb = inode->i_sb;
2314 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2315 struct fstrim_range range;
2316 int ret;
2317
2318 if (!capable(CAP_SYS_ADMIN))
2319 return -EPERM;
2320
Chao Yu7d20c8a2018-09-04 03:52:17 +08002321 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002322 return -EOPNOTSUPP;
2323
2324 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2325 sizeof(range)))
2326 return -EFAULT;
2327
Chao Yu7fb17fe2016-05-09 19:56:32 +08002328 ret = mnt_want_write_file(filp);
2329 if (ret)
2330 return ret;
2331
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002332 range.minlen = max((unsigned int)range.minlen,
2333 q->limits.discard_granularity);
2334 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002335 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002336 if (ret < 0)
2337 return ret;
2338
2339 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2340 sizeof(range)))
2341 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002342 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002343 return 0;
2344}
2345
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002346static bool uuid_is_nonzero(__u8 u[16])
2347{
2348 int i;
2349
2350 for (i = 0; i < 16; i++)
2351 if (u[i])
2352 return true;
2353 return false;
2354}
2355
2356static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2357{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002358 struct inode *inode = file_inode(filp);
2359
Chao Yu7beb01f2018-10-24 18:34:26 +08002360 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002361 return -EOPNOTSUPP;
2362
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002363 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002364
Eric Biggersdb717d82016-11-26 19:07:49 -05002365 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002366}
2367
2368static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2369{
Chao Yu7beb01f2018-10-24 18:34:26 +08002370 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002371 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002372 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002373}
2374
2375static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2376{
2377 struct inode *inode = file_inode(filp);
2378 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2379 int err;
2380
Chao Yu7beb01f2018-10-24 18:34:26 +08002381 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002382 return -EOPNOTSUPP;
2383
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002384 err = mnt_want_write_file(filp);
2385 if (err)
2386 return err;
2387
Chao Yu846ae672018-02-26 22:04:13 +08002388 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002389
2390 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2391 goto got_it;
2392
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002393 /* update superblock with uuid */
2394 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2395
Chao Yuc5bda1c2015-06-08 13:28:03 +08002396 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002397 if (err) {
2398 /* undo new data */
2399 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002400 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002401 }
2402got_it:
2403 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2404 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002405 err = -EFAULT;
2406out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002407 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002408 mnt_drop_write_file(filp);
2409 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002410}
2411
Eric Biggers8ce589c2019-08-04 19:35:48 -07002412static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2413 unsigned long arg)
2414{
2415 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2416 return -EOPNOTSUPP;
2417
2418 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2419}
2420
2421static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2422{
2423 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2424 return -EOPNOTSUPP;
2425
2426 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2427}
2428
2429static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2430{
2431 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2432 return -EOPNOTSUPP;
2433
2434 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2435}
2436
2437static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2438 unsigned long arg)
2439{
2440 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2441 return -EOPNOTSUPP;
2442
2443 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2444}
2445
2446static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2447 unsigned long arg)
2448{
2449 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2450 return -EOPNOTSUPP;
2451
2452 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2453}
2454
Eric Biggersee446e12020-03-14 13:50:51 -07002455static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2456{
2457 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2458 return -EOPNOTSUPP;
2459
2460 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2461}
2462
Chao Yuc1c1b582015-07-10 18:08:10 +08002463static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2464{
2465 struct inode *inode = file_inode(filp);
2466 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002467 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002468 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002469
2470 if (!capable(CAP_SYS_ADMIN))
2471 return -EPERM;
2472
Chao Yud530d4d2015-10-05 22:22:44 +08002473 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002474 return -EFAULT;
2475
Chao Yud530d4d2015-10-05 22:22:44 +08002476 if (f2fs_readonly(sbi->sb))
2477 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002478
Chao Yu7fb17fe2016-05-09 19:56:32 +08002479 ret = mnt_want_write_file(filp);
2480 if (ret)
2481 return ret;
2482
Chao Yud530d4d2015-10-05 22:22:44 +08002483 if (!sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002484 if (!down_write_trylock(&sbi->gc_lock)) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002485 ret = -EBUSY;
2486 goto out;
2487 }
Chao Yud530d4d2015-10-05 22:22:44 +08002488 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002489 down_write(&sbi->gc_lock);
Chao Yuc1c1b582015-07-10 18:08:10 +08002490 }
2491
Chao Yu81ba1632021-02-20 17:35:40 +08002492 ret = f2fs_gc(sbi, sync, true, false, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002493out:
2494 mnt_drop_write_file(filp);
2495 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002496}
2497
Chao Yude2041d2020-11-10 09:24:37 +08002498static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002499{
Chao Yude2041d2020-11-10 09:24:37 +08002500 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002501 u64 end;
2502 int ret;
2503
2504 if (!capable(CAP_SYS_ADMIN))
2505 return -EPERM;
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002506 if (f2fs_readonly(sbi->sb))
2507 return -EROFS;
2508
Chao Yude2041d2020-11-10 09:24:37 +08002509 end = range->start + range->len;
2510 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302511 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002512 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002513
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002514 ret = mnt_want_write_file(filp);
2515 if (ret)
2516 return ret;
2517
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002518do_more:
Chao Yude2041d2020-11-10 09:24:37 +08002519 if (!range->sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002520 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002521 ret = -EBUSY;
2522 goto out;
2523 }
2524 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002525 down_write(&sbi->gc_lock);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002526 }
2527
Chao Yu81ba1632021-02-20 17:35:40 +08002528 ret = f2fs_gc(sbi, range->sync, true, false,
2529 GET_SEGNO(sbi, range->start));
Qilong Zhang97767502020-06-28 19:23:03 +08002530 if (ret) {
2531 if (ret == -EBUSY)
2532 ret = -EAGAIN;
2533 goto out;
2534 }
Chao Yude2041d2020-11-10 09:24:37 +08002535 range->start += BLKS_PER_SEC(sbi);
2536 if (range->start <= end)
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002537 goto do_more;
2538out:
2539 mnt_drop_write_file(filp);
2540 return ret;
2541}
2542
Chao Yude2041d2020-11-10 09:24:37 +08002543static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2544{
2545 struct f2fs_gc_range range;
2546
2547 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2548 sizeof(range)))
2549 return -EFAULT;
2550 return __f2fs_ioc_gc_range(filp, &range);
2551}
2552
Chao Yu059c0642018-07-17 20:41:49 +08002553static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002554{
2555 struct inode *inode = file_inode(filp);
2556 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002557 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002558
2559 if (!capable(CAP_SYS_ADMIN))
2560 return -EPERM;
2561
2562 if (f2fs_readonly(sbi->sb))
2563 return -EROFS;
2564
Daniel Rosenberg43549942018-08-20 19:21:43 -07002565 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002566 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002567 return -EINVAL;
2568 }
2569
Chao Yu7fb17fe2016-05-09 19:56:32 +08002570 ret = mnt_want_write_file(filp);
2571 if (ret)
2572 return ret;
2573
2574 ret = f2fs_sync_fs(sbi->sb, 1);
2575
2576 mnt_drop_write_file(filp);
2577 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002578}
2579
Chao Yud323d002015-10-27 09:53:45 +08002580static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2581 struct file *filp,
2582 struct f2fs_defragment *range)
2583{
2584 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002585 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002586 .m_seg_type = NO_CHECK_TYPE ,
2587 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002588 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002589 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002590 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002591 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002592 block_t blk_end = 0;
2593 bool fragmented = false;
2594 int err;
2595
2596 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002597 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002598 return -EINVAL;
2599
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002600 pg_start = range->start >> PAGE_SHIFT;
2601 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002602
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002603 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002604
Al Viro59551022016-01-22 15:40:57 -05002605 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002606
2607 /* writeback all dirty pages in the range */
2608 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002609 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002610 if (err)
2611 goto out;
2612
2613 /*
2614 * lookup mapping info in extent cache, skip defragmenting if physical
2615 * block addresses are continuous.
2616 */
2617 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2618 if (ei.fofs + ei.len >= pg_end)
2619 goto out;
2620 }
2621
2622 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002623 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002624
2625 /*
2626 * lookup mapping info in dnode page cache, skip defragmenting if all
2627 * physical block addresses are continuous even if there are hole(s)
2628 * in logical blocks.
2629 */
2630 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002631 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002632 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002633 if (err)
2634 goto out;
2635
2636 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002637 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002638 continue;
2639 }
2640
Chao Yu25a912e2018-01-10 18:18:51 +08002641 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002642 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002643
2644 /* record total count of block that we're going to move */
2645 total += map.m_len;
2646
Chao Yud323d002015-10-27 09:53:45 +08002647 blk_end = map.m_pblk + map.m_len;
2648
2649 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002650 }
2651
Chao Yud3a1a0e2019-07-29 23:02:29 +08002652 if (!fragmented) {
2653 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002654 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002655 }
Chao Yud323d002015-10-27 09:53:45 +08002656
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002657 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002658
2659 /*
2660 * make sure there are enough free section for LFS allocation, this can
2661 * avoid defragment running in SSR mode when free section are allocated
2662 * intensively
2663 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002664 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002665 err = -EAGAIN;
2666 goto out;
2667 }
2668
Chao Yu25a912e2018-01-10 18:18:51 +08002669 map.m_lblk = pg_start;
2670 map.m_len = pg_end - pg_start;
2671 total = 0;
2672
Chao Yud323d002015-10-27 09:53:45 +08002673 while (map.m_lblk < pg_end) {
2674 pgoff_t idx;
2675 int cnt = 0;
2676
2677do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002678 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002679 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002680 if (err)
2681 goto clear_out;
2682
2683 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002684 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002685 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002686 }
2687
Jaegeuk Kim91942322016-05-20 10:13:22 -07002688 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002689
2690 idx = map.m_lblk;
2691 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2692 struct page *page;
2693
Chao Yu4d57b862018-05-30 00:20:41 +08002694 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002695 if (IS_ERR(page)) {
2696 err = PTR_ERR(page);
2697 goto clear_out;
2698 }
2699
2700 set_page_dirty(page);
2701 f2fs_put_page(page, 1);
2702
2703 idx++;
2704 cnt++;
2705 total++;
2706 }
2707
2708 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002709check:
2710 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002711 goto do_map;
2712
Jaegeuk Kim91942322016-05-20 10:13:22 -07002713 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002714
2715 err = filemap_fdatawrite(inode->i_mapping);
2716 if (err)
2717 goto out;
2718 }
2719clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002720 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002721out:
Al Viro59551022016-01-22 15:40:57 -05002722 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002723 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002724 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002725 return err;
2726}
2727
2728static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2729{
2730 struct inode *inode = file_inode(filp);
2731 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2732 struct f2fs_defragment range;
2733 int err;
2734
2735 if (!capable(CAP_SYS_ADMIN))
2736 return -EPERM;
2737
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002738 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002739 return -EINVAL;
2740
Kinglong Meed7563862017-03-10 17:55:07 +08002741 if (f2fs_readonly(sbi->sb))
2742 return -EROFS;
2743
2744 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2745 sizeof(range)))
2746 return -EFAULT;
2747
2748 /* verify alignment of offset & size */
2749 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2750 return -EINVAL;
2751
2752 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2753 sbi->max_file_blocks))
2754 return -EINVAL;
2755
Chao Yud323d002015-10-27 09:53:45 +08002756 err = mnt_want_write_file(filp);
2757 if (err)
2758 return err;
2759
Chao Yud323d002015-10-27 09:53:45 +08002760 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002761 mnt_drop_write_file(filp);
2762
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002763 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002764 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002765 return err;
Chao Yud323d002015-10-27 09:53:45 +08002766
2767 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2768 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002769 return -EFAULT;
2770
2771 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002772}
2773
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002774static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2775 struct file *file_out, loff_t pos_out, size_t len)
2776{
2777 struct inode *src = file_inode(file_in);
2778 struct inode *dst = file_inode(file_out);
2779 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2780 size_t olen = len, dst_max_i_size = 0;
2781 size_t dst_osize;
2782 int ret;
2783
2784 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2785 src->i_sb != dst->i_sb)
2786 return -EXDEV;
2787
2788 if (unlikely(f2fs_readonly(src->i_sb)))
2789 return -EROFS;
2790
Chao Yufe8494b2016-08-04 20:13:02 +08002791 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2792 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002793
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302794 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002795 return -EOPNOTSUPP;
2796
Dan Robertsonaad13832020-08-30 21:45:23 +00002797 if (pos_out < 0 || pos_in < 0)
2798 return -EINVAL;
2799
Fan Lid95fd912016-09-13 11:35:42 +08002800 if (src == dst) {
2801 if (pos_in == pos_out)
2802 return 0;
2803 if (pos_out > pos_in && pos_out < pos_in + len)
2804 return -EINVAL;
2805 }
2806
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002807 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002808 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002809 ret = -EBUSY;
2810 if (!inode_trylock(dst))
2811 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002812 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002813
2814 ret = -EINVAL;
2815 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2816 goto out_unlock;
2817 if (len == 0)
2818 olen = len = src->i_size - pos_in;
2819 if (pos_in + len == src->i_size)
2820 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2821 if (len == 0) {
2822 ret = 0;
2823 goto out_unlock;
2824 }
2825
2826 dst_osize = dst->i_size;
2827 if (pos_out + olen > dst->i_size)
2828 dst_max_i_size = pos_out + olen;
2829
2830 /* verify the end result is block aligned */
2831 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2832 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2833 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2834 goto out_unlock;
2835
2836 ret = f2fs_convert_inline_inode(src);
2837 if (ret)
2838 goto out_unlock;
2839
2840 ret = f2fs_convert_inline_inode(dst);
2841 if (ret)
2842 goto out_unlock;
2843
2844 /* write out all dirty pages from offset */
2845 ret = filemap_write_and_wait_range(src->i_mapping,
2846 pos_in, pos_in + len);
2847 if (ret)
2848 goto out_unlock;
2849
2850 ret = filemap_write_and_wait_range(dst->i_mapping,
2851 pos_out, pos_out + len);
2852 if (ret)
2853 goto out_unlock;
2854
2855 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002856
2857 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2858 if (src != dst) {
2859 ret = -EBUSY;
2860 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2861 goto out_src;
2862 }
2863
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002864 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002865 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2866 pos_out >> F2FS_BLKSIZE_BITS,
2867 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002868
2869 if (!ret) {
2870 if (dst_max_i_size)
2871 f2fs_i_size_write(dst, dst_max_i_size);
2872 else if (dst_osize != dst->i_size)
2873 f2fs_i_size_write(dst, dst_osize);
2874 }
2875 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002876
2877 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002878 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002879out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002880 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002881out_unlock:
2882 if (src != dst)
2883 inode_unlock(dst);
2884out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002885 inode_unlock(src);
2886 return ret;
2887}
2888
Chao Yude2041d2020-11-10 09:24:37 +08002889static int __f2fs_ioc_move_range(struct file *filp,
2890 struct f2fs_move_range *range)
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002891{
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002892 struct fd dst;
2893 int err;
2894
2895 if (!(filp->f_mode & FMODE_READ) ||
2896 !(filp->f_mode & FMODE_WRITE))
2897 return -EBADF;
2898
Chao Yude2041d2020-11-10 09:24:37 +08002899 dst = fdget(range->dst_fd);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002900 if (!dst.file)
2901 return -EBADF;
2902
2903 if (!(dst.file->f_mode & FMODE_WRITE)) {
2904 err = -EBADF;
2905 goto err_out;
2906 }
2907
2908 err = mnt_want_write_file(filp);
2909 if (err)
2910 goto err_out;
2911
Chao Yude2041d2020-11-10 09:24:37 +08002912 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2913 range->pos_out, range->len);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002914
2915 mnt_drop_write_file(filp);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002916err_out:
2917 fdput(dst);
2918 return err;
2919}
2920
Chao Yude2041d2020-11-10 09:24:37 +08002921static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2922{
2923 struct f2fs_move_range range;
2924
2925 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2926 sizeof(range)))
2927 return -EFAULT;
2928 return __f2fs_ioc_move_range(filp, &range);
2929}
2930
Jaegeuk Kime066b832017-04-13 15:17:00 -07002931static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2932{
2933 struct inode *inode = file_inode(filp);
2934 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2935 struct sit_info *sm = SIT_I(sbi);
2936 unsigned int start_segno = 0, end_segno = 0;
2937 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2938 struct f2fs_flush_device range;
2939 int ret;
2940
2941 if (!capable(CAP_SYS_ADMIN))
2942 return -EPERM;
2943
2944 if (f2fs_readonly(sbi->sb))
2945 return -EROFS;
2946
Daniel Rosenberg43549942018-08-20 19:21:43 -07002947 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2948 return -EINVAL;
2949
Jaegeuk Kime066b832017-04-13 15:17:00 -07002950 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2951 sizeof(range)))
2952 return -EFAULT;
2953
Damien Le Moal09168782019-03-16 09:13:06 +09002954 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002955 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002956 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2957 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002958 return -EINVAL;
2959 }
2960
2961 ret = mnt_want_write_file(filp);
2962 if (ret)
2963 return ret;
2964
2965 if (range.dev_num != 0)
2966 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2967 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2968
2969 start_segno = sm->last_victim[FLUSH_DEVICE];
2970 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2971 start_segno = dev_start_segno;
2972 end_segno = min(start_segno + range.segments, dev_end_segno);
2973
2974 while (start_segno < end_segno) {
Chao Yufb24fea2020-01-14 19:36:50 +08002975 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002976 ret = -EBUSY;
2977 goto out;
2978 }
2979 sm->last_victim[GC_CB] = end_segno + 1;
2980 sm->last_victim[GC_GREEDY] = end_segno + 1;
2981 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
Chao Yu81ba1632021-02-20 17:35:40 +08002982 ret = f2fs_gc(sbi, true, true, true, start_segno);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002983 if (ret == -EAGAIN)
2984 ret = 0;
2985 else if (ret < 0)
2986 break;
2987 start_segno++;
2988 }
2989out:
2990 mnt_drop_write_file(filp);
2991 return ret;
2992}
2993
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002994static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2995{
2996 struct inode *inode = file_inode(filp);
2997 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2998
2999 /* Must validate to set it with SQLite behavior in Android. */
3000 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3001
3002 return put_user(sb_feature, (u32 __user *)arg);
3003}
Jaegeuk Kime066b832017-04-13 15:17:00 -07003004
Chao Yu2c1d0302017-07-29 00:32:52 +08003005#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08003006int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3007{
3008 struct dquot *transfer_to[MAXQUOTAS] = {};
3009 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3010 struct super_block *sb = sbi->sb;
3011 int err = 0;
3012
3013 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3014 if (!IS_ERR(transfer_to[PRJQUOTA])) {
3015 err = __dquot_transfer(inode, transfer_to);
3016 if (err)
3017 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3018 dqput(transfer_to[PRJQUOTA]);
3019 }
3020 return err;
3021}
3022
Chao Yu2c1d0302017-07-29 00:32:52 +08003023static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3024{
3025 struct inode *inode = file_inode(filp);
3026 struct f2fs_inode_info *fi = F2FS_I(inode);
3027 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003028 struct page *ipage;
3029 kprojid_t kprojid;
3030 int err;
3031
Chao Yu7beb01f2018-10-24 18:34:26 +08003032 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08003033 if (projid != F2FS_DEF_PROJID)
3034 return -EOPNOTSUPP;
3035 else
3036 return 0;
3037 }
3038
3039 if (!f2fs_has_extra_attr(inode))
3040 return -EOPNOTSUPP;
3041
3042 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3043
3044 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3045 return 0;
3046
Chao Yu2c1d0302017-07-29 00:32:52 +08003047 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003048 /* Is it quota file? Do not allow user to mess with it */
3049 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003050 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003051
Chao Yu4d57b862018-05-30 00:20:41 +08003052 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003053 if (IS_ERR(ipage))
3054 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003055
3056 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3057 i_projid)) {
3058 err = -EOVERFLOW;
3059 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003060 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003061 }
3062 f2fs_put_page(ipage, 1);
3063
Chao Yuc22aecd2018-04-21 17:53:52 +08003064 err = dquot_initialize(inode);
3065 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003066 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003067
Chao Yu78130812018-09-25 15:36:02 +08003068 f2fs_lock_op(sbi);
3069 err = f2fs_transfer_project_quota(inode, kprojid);
3070 if (err)
3071 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003072
3073 F2FS_I(inode)->i_projid = kprojid;
3074 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003075 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003076out_unlock:
3077 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003078 return err;
3079}
3080#else
Chao Yu78130812018-09-25 15:36:02 +08003081int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3082{
3083 return 0;
3084}
3085
Chao Yu2c1d0302017-07-29 00:32:52 +08003086static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3087{
3088 if (projid != F2FS_DEF_PROJID)
3089 return -EOPNOTSUPP;
3090 return 0;
3091}
3092#endif
3093
Eric Biggers36098552019-06-04 22:59:04 -07003094/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08003095
Eric Biggers36098552019-06-04 22:59:04 -07003096/*
3097 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3098 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3099 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3100 */
3101
3102static const struct {
3103 u32 iflag;
3104 u32 xflag;
3105} f2fs_xflags_map[] = {
3106 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3107 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3108 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3109 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3110 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3111 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3112};
3113
3114#define F2FS_SUPPORTED_XFLAGS ( \
3115 FS_XFLAG_SYNC | \
3116 FS_XFLAG_IMMUTABLE | \
3117 FS_XFLAG_APPEND | \
3118 FS_XFLAG_NODUMP | \
3119 FS_XFLAG_NOATIME | \
3120 FS_XFLAG_PROJINHERIT)
3121
3122/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3123static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3124{
3125 u32 xflags = 0;
3126 int i;
3127
3128 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3129 if (iflags & f2fs_xflags_map[i].iflag)
3130 xflags |= f2fs_xflags_map[i].xflag;
3131
Chao Yu2c1d0302017-07-29 00:32:52 +08003132 return xflags;
3133}
3134
Eric Biggers36098552019-06-04 22:59:04 -07003135/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3136static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003137{
Eric Biggers36098552019-06-04 22:59:04 -07003138 u32 iflags = 0;
3139 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08003140
Eric Biggers36098552019-06-04 22:59:04 -07003141 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3142 if (xflags & f2fs_xflags_map[i].xflag)
3143 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08003144
3145 return iflags;
3146}
3147
Eric Biggers6fc93c42019-07-01 13:26:29 -07003148static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3149{
3150 struct f2fs_inode_info *fi = F2FS_I(inode);
3151
3152 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3153
3154 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3155 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3156}
3157
Chao Yu2c1d0302017-07-29 00:32:52 +08003158static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3159{
3160 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08003161 struct fsxattr fa;
3162
Eric Biggers6fc93c42019-07-01 13:26:29 -07003163 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08003164
3165 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3166 return -EFAULT;
3167 return 0;
3168}
3169
3170static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3171{
3172 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003173 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07003174 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003175 int err;
3176
3177 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3178 return -EFAULT;
3179
3180 /* Make sure caller has proper permission */
3181 if (!inode_owner_or_capable(inode))
3182 return -EACCES;
3183
Eric Biggers36098552019-06-04 22:59:04 -07003184 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08003185 return -EOPNOTSUPP;
3186
Eric Biggers36098552019-06-04 22:59:04 -07003187 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3188 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003189 return -EOPNOTSUPP;
3190
3191 err = mnt_want_write_file(filp);
3192 if (err)
3193 return err;
3194
3195 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003196
3197 f2fs_fill_fsxattr(inode, &old_fa);
3198 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09003199 if (err)
3200 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07003201
Eric Biggers36098552019-06-04 22:59:04 -07003202 err = f2fs_setflags_common(inode, iflags,
3203 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08003204 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003205 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08003206
3207 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09003208out:
3209 inode_unlock(inode);
3210 mnt_drop_write_file(filp);
3211 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003212}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003213
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003214int f2fs_pin_file_control(struct inode *inode, bool inc)
3215{
3216 struct f2fs_inode_info *fi = F2FS_I(inode);
3217 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3218
3219 /* Use i_gc_failures for normal file as a risk signal. */
3220 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003221 f2fs_i_gc_failures_write(inode,
3222 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003223
Chao Yu2ef79ec2018-05-07 20:28:54 +08003224 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003225 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3226 __func__, inode->i_ino,
3227 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003228 clear_inode_flag(inode, FI_PIN_FILE);
3229 return -EAGAIN;
3230 }
3231 return 0;
3232}
3233
3234static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3235{
3236 struct inode *inode = file_inode(filp);
3237 __u32 pin;
3238 int ret = 0;
3239
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003240 if (get_user(pin, (__u32 __user *)arg))
3241 return -EFAULT;
3242
3243 if (!S_ISREG(inode->i_mode))
3244 return -EINVAL;
3245
3246 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3247 return -EROFS;
3248
3249 ret = mnt_want_write_file(filp);
3250 if (ret)
3251 return ret;
3252
3253 inode_lock(inode);
3254
Chao Yu4d57b862018-05-30 00:20:41 +08003255 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08003256 ret = -EINVAL;
3257 goto out;
3258 }
3259
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003260 if (!pin) {
3261 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003262 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003263 goto done;
3264 }
3265
3266 if (f2fs_pin_file_control(inode, false)) {
3267 ret = -EAGAIN;
3268 goto out;
3269 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003270
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003271 ret = f2fs_convert_inline_inode(inode);
3272 if (ret)
3273 goto out;
3274
Daeho Jeong78134d02020-09-08 11:44:11 +09003275 if (!f2fs_disable_compressed_file(inode)) {
Chao Yu4c8ff702019-11-01 18:07:14 +08003276 ret = -EOPNOTSUPP;
3277 goto out;
3278 }
3279
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003280 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003281 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003282done:
3283 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3284out:
3285 inode_unlock(inode);
3286 mnt_drop_write_file(filp);
3287 return ret;
3288}
3289
3290static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3291{
3292 struct inode *inode = file_inode(filp);
3293 __u32 pin = 0;
3294
3295 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003296 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003297 return put_user(pin, (u32 __user *)arg);
3298}
3299
Chao Yuc4020b22018-01-11 14:42:30 +08003300int f2fs_precache_extents(struct inode *inode)
3301{
3302 struct f2fs_inode_info *fi = F2FS_I(inode);
3303 struct f2fs_map_blocks map;
3304 pgoff_t m_next_extent;
3305 loff_t end;
3306 int err;
3307
3308 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3309 return -EOPNOTSUPP;
3310
3311 map.m_lblk = 0;
3312 map.m_next_pgofs = NULL;
3313 map.m_next_extent = &m_next_extent;
3314 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003315 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003316 end = F2FS_I_SB(inode)->max_file_blocks;
3317
3318 while (map.m_lblk < end) {
3319 map.m_len = end - map.m_lblk;
3320
Chao Yub2532c62018-04-24 10:55:28 +08003321 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003322 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003323 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003324 if (err)
3325 return err;
3326
3327 map.m_lblk = m_next_extent;
3328 }
3329
3330 return err;
3331}
3332
3333static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3334{
3335 return f2fs_precache_extents(file_inode(filp));
3336}
3337
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003338static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3339{
3340 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3341 __u64 block_count;
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003342
3343 if (!capable(CAP_SYS_ADMIN))
3344 return -EPERM;
3345
3346 if (f2fs_readonly(sbi->sb))
3347 return -EROFS;
3348
3349 if (copy_from_user(&block_count, (void __user *)arg,
3350 sizeof(block_count)))
3351 return -EFAULT;
3352
Jaegeuk Kimb4b10062020-03-31 11:43:07 -07003353 return f2fs_resize_fs(sbi, block_count);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003354}
3355
Eric Biggers95ae2512019-07-22 09:26:24 -07003356static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3357{
3358 struct inode *inode = file_inode(filp);
3359
3360 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3361
3362 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3363 f2fs_warn(F2FS_I_SB(inode),
3364 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3365 inode->i_ino);
3366 return -EOPNOTSUPP;
3367 }
3368
3369 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3370}
3371
3372static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3373{
3374 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3375 return -EOPNOTSUPP;
3376
3377 return fsverity_ioctl_measure(filp, (void __user *)arg);
3378}
3379
Eric Biggers3357af82020-07-14 15:18:12 -07003380static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003381{
3382 struct inode *inode = file_inode(filp);
3383 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3384 char *vbuf;
3385 int count;
3386 int err = 0;
3387
3388 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3389 if (!vbuf)
3390 return -ENOMEM;
3391
3392 down_read(&sbi->sb_lock);
3393 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3394 ARRAY_SIZE(sbi->raw_super->volume_name),
3395 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3396 up_read(&sbi->sb_lock);
3397
3398 if (copy_to_user((char __user *)arg, vbuf,
3399 min(FSLABEL_MAX, count)))
3400 err = -EFAULT;
3401
Chao Yuc8eb7022020-09-14 16:47:00 +08003402 kfree(vbuf);
Chao Yu4507847c2019-07-17 17:06:11 +08003403 return err;
3404}
3405
Eric Biggers3357af82020-07-14 15:18:12 -07003406static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003407{
3408 struct inode *inode = file_inode(filp);
3409 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3410 char *vbuf;
3411 int err = 0;
3412
3413 if (!capable(CAP_SYS_ADMIN))
3414 return -EPERM;
3415
3416 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3417 if (IS_ERR(vbuf))
3418 return PTR_ERR(vbuf);
3419
3420 err = mnt_want_write_file(filp);
3421 if (err)
3422 goto out;
3423
3424 down_write(&sbi->sb_lock);
3425
3426 memset(sbi->raw_super->volume_name, 0,
3427 sizeof(sbi->raw_super->volume_name));
3428 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3429 sbi->raw_super->volume_name,
3430 ARRAY_SIZE(sbi->raw_super->volume_name));
3431
3432 err = f2fs_commit_super(sbi, false);
3433
3434 up_write(&sbi->sb_lock);
3435
3436 mnt_drop_write_file(filp);
3437out:
3438 kfree(vbuf);
3439 return err;
3440}
3441
Chao Yu439dfb12020-02-21 18:09:21 +08003442static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3443{
3444 struct inode *inode = file_inode(filp);
3445 __u64 blocks;
3446
3447 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3448 return -EOPNOTSUPP;
3449
3450 if (!f2fs_compressed_file(inode))
3451 return -EINVAL;
3452
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003453 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
Chao Yu439dfb12020-02-21 18:09:21 +08003454 return put_user(blocks, (u64 __user *)arg);
3455}
3456
Chao Yuef8d5632020-03-06 15:36:09 +08003457static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3458{
3459 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3460 unsigned int released_blocks = 0;
3461 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3462 block_t blkaddr;
3463 int i;
3464
3465 for (i = 0; i < count; i++) {
3466 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3467 dn->ofs_in_node + i);
3468
3469 if (!__is_valid_data_blkaddr(blkaddr))
3470 continue;
3471 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3472 DATA_GENERIC_ENHANCE)))
3473 return -EFSCORRUPTED;
3474 }
3475
3476 while (count) {
3477 int compr_blocks = 0;
3478
3479 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3480 blkaddr = f2fs_data_blkaddr(dn);
3481
3482 if (i == 0) {
3483 if (blkaddr == COMPRESS_ADDR)
3484 continue;
3485 dn->ofs_in_node += cluster_size;
3486 goto next;
3487 }
3488
3489 if (__is_valid_data_blkaddr(blkaddr))
3490 compr_blocks++;
3491
3492 if (blkaddr != NEW_ADDR)
3493 continue;
3494
3495 dn->data_blkaddr = NULL_ADDR;
3496 f2fs_set_data_blkaddr(dn);
3497 }
3498
3499 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3500 dec_valid_block_count(sbi, dn->inode,
3501 cluster_size - compr_blocks);
3502
3503 released_blocks += cluster_size - compr_blocks;
3504next:
3505 count -= cluster_size;
3506 }
3507
3508 return released_blocks;
3509}
3510
3511static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3512{
3513 struct inode *inode = file_inode(filp);
3514 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3515 pgoff_t page_idx = 0, last_idx;
3516 unsigned int released_blocks = 0;
3517 int ret;
3518 int writecount;
3519
3520 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3521 return -EOPNOTSUPP;
3522
3523 if (!f2fs_compressed_file(inode))
3524 return -EINVAL;
3525
3526 if (f2fs_readonly(sbi->sb))
3527 return -EROFS;
3528
3529 ret = mnt_want_write_file(filp);
3530 if (ret)
3531 return ret;
3532
3533 f2fs_balance_fs(F2FS_I_SB(inode), true);
3534
3535 inode_lock(inode);
3536
3537 writecount = atomic_read(&inode->i_writecount);
Daeho Jeong8c8cf262020-10-12 13:59:47 +09003538 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3539 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
Chao Yuef8d5632020-03-06 15:36:09 +08003540 ret = -EBUSY;
3541 goto out;
3542 }
3543
3544 if (IS_IMMUTABLE(inode)) {
3545 ret = -EINVAL;
3546 goto out;
3547 }
3548
3549 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3550 if (ret)
3551 goto out;
3552
Chao Yuef8d5632020-03-06 15:36:09 +08003553 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3554 f2fs_set_inode_flags(inode);
3555 inode->i_ctime = current_time(inode);
3556 f2fs_mark_inode_dirty_sync(inode, true);
3557
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003558 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
Daeho Jeong567c4bf2020-07-30 14:09:28 +09003559 goto out;
3560
Chao Yuef8d5632020-03-06 15:36:09 +08003561 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3562 down_write(&F2FS_I(inode)->i_mmap_sem);
3563
3564 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3565
3566 while (page_idx < last_idx) {
3567 struct dnode_of_data dn;
3568 pgoff_t end_offset, count;
3569
3570 set_new_dnode(&dn, inode, NULL, NULL, 0);
3571 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3572 if (ret) {
3573 if (ret == -ENOENT) {
3574 page_idx = f2fs_get_next_page_offset(&dn,
3575 page_idx);
3576 ret = 0;
3577 continue;
3578 }
3579 break;
3580 }
3581
3582 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3583 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003584 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuef8d5632020-03-06 15:36:09 +08003585
3586 ret = release_compress_blocks(&dn, count);
3587
3588 f2fs_put_dnode(&dn);
3589
3590 if (ret < 0)
3591 break;
3592
3593 page_idx += count;
3594 released_blocks += ret;
3595 }
3596
3597 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3598 up_write(&F2FS_I(inode)->i_mmap_sem);
3599out:
3600 inode_unlock(inode);
3601
3602 mnt_drop_write_file(filp);
3603
3604 if (ret >= 0) {
3605 ret = put_user(released_blocks, (u64 __user *)arg);
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003606 } else if (released_blocks &&
3607 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
Chao Yuef8d5632020-03-06 15:36:09 +08003608 set_sbi_flag(sbi, SBI_NEED_FSCK);
3609 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003610 "iblocks=%llu, released=%u, compr_blocks=%u, "
Chao Yuef8d5632020-03-06 15:36:09 +08003611 "run fsck to fix.",
3612 __func__, inode->i_ino, inode->i_blocks,
3613 released_blocks,
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003614 atomic_read(&F2FS_I(inode)->i_compr_blocks));
Chao Yuef8d5632020-03-06 15:36:09 +08003615 }
3616
3617 return ret;
3618}
3619
Chao Yuc75488f2020-03-06 14:35:33 +08003620static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3621{
3622 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3623 unsigned int reserved_blocks = 0;
3624 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3625 block_t blkaddr;
3626 int i;
3627
3628 for (i = 0; i < count; i++) {
3629 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3630 dn->ofs_in_node + i);
3631
3632 if (!__is_valid_data_blkaddr(blkaddr))
3633 continue;
3634 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3635 DATA_GENERIC_ENHANCE)))
3636 return -EFSCORRUPTED;
3637 }
3638
3639 while (count) {
3640 int compr_blocks = 0;
3641 blkcnt_t reserved;
3642 int ret;
3643
3644 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3645 blkaddr = f2fs_data_blkaddr(dn);
3646
3647 if (i == 0) {
3648 if (blkaddr == COMPRESS_ADDR)
3649 continue;
3650 dn->ofs_in_node += cluster_size;
3651 goto next;
3652 }
3653
3654 if (__is_valid_data_blkaddr(blkaddr)) {
3655 compr_blocks++;
3656 continue;
3657 }
3658
3659 dn->data_blkaddr = NEW_ADDR;
3660 f2fs_set_data_blkaddr(dn);
3661 }
3662
3663 reserved = cluster_size - compr_blocks;
3664 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3665 if (ret)
3666 return ret;
3667
3668 if (reserved != cluster_size - compr_blocks)
3669 return -ENOSPC;
3670
3671 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3672
3673 reserved_blocks += reserved;
3674next:
3675 count -= cluster_size;
3676 }
3677
3678 return reserved_blocks;
3679}
3680
3681static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3682{
3683 struct inode *inode = file_inode(filp);
3684 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3685 pgoff_t page_idx = 0, last_idx;
3686 unsigned int reserved_blocks = 0;
3687 int ret;
3688
3689 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3690 return -EOPNOTSUPP;
3691
3692 if (!f2fs_compressed_file(inode))
3693 return -EINVAL;
3694
3695 if (f2fs_readonly(sbi->sb))
3696 return -EROFS;
3697
3698 ret = mnt_want_write_file(filp);
3699 if (ret)
3700 return ret;
3701
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003702 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
Chao Yuc75488f2020-03-06 14:35:33 +08003703 goto out;
3704
3705 f2fs_balance_fs(F2FS_I_SB(inode), true);
3706
3707 inode_lock(inode);
3708
3709 if (!IS_IMMUTABLE(inode)) {
3710 ret = -EINVAL;
3711 goto unlock_inode;
3712 }
3713
3714 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3715 down_write(&F2FS_I(inode)->i_mmap_sem);
3716
3717 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3718
3719 while (page_idx < last_idx) {
3720 struct dnode_of_data dn;
3721 pgoff_t end_offset, count;
3722
3723 set_new_dnode(&dn, inode, NULL, NULL, 0);
3724 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3725 if (ret) {
3726 if (ret == -ENOENT) {
3727 page_idx = f2fs_get_next_page_offset(&dn,
3728 page_idx);
3729 ret = 0;
3730 continue;
3731 }
3732 break;
3733 }
3734
3735 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3736 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003737 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuc75488f2020-03-06 14:35:33 +08003738
3739 ret = reserve_compress_blocks(&dn, count);
3740
3741 f2fs_put_dnode(&dn);
3742
3743 if (ret < 0)
3744 break;
3745
3746 page_idx += count;
3747 reserved_blocks += ret;
3748 }
3749
3750 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3751 up_write(&F2FS_I(inode)->i_mmap_sem);
3752
3753 if (ret >= 0) {
3754 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3755 f2fs_set_inode_flags(inode);
3756 inode->i_ctime = current_time(inode);
3757 f2fs_mark_inode_dirty_sync(inode, true);
3758 }
3759unlock_inode:
3760 inode_unlock(inode);
3761out:
3762 mnt_drop_write_file(filp);
3763
3764 if (ret >= 0) {
3765 ret = put_user(reserved_blocks, (u64 __user *)arg);
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003766 } else if (reserved_blocks &&
3767 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
Chao Yuc75488f2020-03-06 14:35:33 +08003768 set_sbi_flag(sbi, SBI_NEED_FSCK);
3769 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003770 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
Chao Yuc75488f2020-03-06 14:35:33 +08003771 "run fsck to fix.",
3772 __func__, inode->i_ino, inode->i_blocks,
3773 reserved_blocks,
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003774 atomic_read(&F2FS_I(inode)->i_compr_blocks));
Chao Yuc75488f2020-03-06 14:35:33 +08003775 }
3776
3777 return ret;
3778}
3779
Daeho Jeong9af84642020-07-21 12:21:11 +09003780static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3781 pgoff_t off, block_t block, block_t len, u32 flags)
3782{
3783 struct request_queue *q = bdev_get_queue(bdev);
3784 sector_t sector = SECTOR_FROM_BLOCK(block);
3785 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3786 int ret = 0;
3787
3788 if (!q)
3789 return -ENXIO;
3790
3791 if (flags & F2FS_TRIM_FILE_DISCARD)
3792 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3793 blk_queue_secure_erase(q) ?
3794 BLKDEV_DISCARD_SECURE : 0);
3795
3796 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3797 if (IS_ENCRYPTED(inode))
3798 ret = fscrypt_zeroout_range(inode, off, block, len);
3799 else
3800 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3801 GFP_NOFS, 0);
3802 }
3803
3804 return ret;
3805}
3806
3807static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3808{
3809 struct inode *inode = file_inode(filp);
3810 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3811 struct address_space *mapping = inode->i_mapping;
3812 struct block_device *prev_bdev = NULL;
3813 struct f2fs_sectrim_range range;
3814 pgoff_t index, pg_end, prev_index = 0;
3815 block_t prev_block = 0, len = 0;
3816 loff_t end_addr;
3817 bool to_end = false;
3818 int ret = 0;
3819
3820 if (!(filp->f_mode & FMODE_WRITE))
3821 return -EBADF;
3822
3823 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3824 sizeof(range)))
3825 return -EFAULT;
3826
3827 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3828 !S_ISREG(inode->i_mode))
3829 return -EINVAL;
3830
3831 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3832 !f2fs_hw_support_discard(sbi)) ||
3833 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3834 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3835 return -EOPNOTSUPP;
3836
3837 file_start_write(filp);
3838 inode_lock(inode);
3839
3840 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3841 range.start >= inode->i_size) {
3842 ret = -EINVAL;
3843 goto err;
3844 }
3845
3846 if (range.len == 0)
3847 goto err;
3848
3849 if (inode->i_size - range.start > range.len) {
3850 end_addr = range.start + range.len;
3851 } else {
3852 end_addr = range.len == (u64)-1 ?
3853 sbi->sb->s_maxbytes : inode->i_size;
3854 to_end = true;
3855 }
3856
3857 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3858 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3859 ret = -EINVAL;
3860 goto err;
3861 }
3862
3863 index = F2FS_BYTES_TO_BLK(range.start);
3864 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3865
3866 ret = f2fs_convert_inline_inode(inode);
3867 if (ret)
3868 goto err;
3869
3870 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3871 down_write(&F2FS_I(inode)->i_mmap_sem);
3872
3873 ret = filemap_write_and_wait_range(mapping, range.start,
3874 to_end ? LLONG_MAX : end_addr - 1);
3875 if (ret)
3876 goto out;
3877
3878 truncate_inode_pages_range(mapping, range.start,
3879 to_end ? -1 : end_addr - 1);
3880
3881 while (index < pg_end) {
3882 struct dnode_of_data dn;
3883 pgoff_t end_offset, count;
3884 int i;
3885
3886 set_new_dnode(&dn, inode, NULL, NULL, 0);
3887 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3888 if (ret) {
3889 if (ret == -ENOENT) {
3890 index = f2fs_get_next_page_offset(&dn, index);
3891 continue;
3892 }
3893 goto out;
3894 }
3895
3896 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3897 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3898 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3899 struct block_device *cur_bdev;
3900 block_t blkaddr = f2fs_data_blkaddr(&dn);
3901
3902 if (!__is_valid_data_blkaddr(blkaddr))
3903 continue;
3904
3905 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3906 DATA_GENERIC_ENHANCE)) {
3907 ret = -EFSCORRUPTED;
3908 f2fs_put_dnode(&dn);
3909 goto out;
3910 }
3911
3912 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3913 if (f2fs_is_multi_device(sbi)) {
3914 int di = f2fs_target_device_index(sbi, blkaddr);
3915
3916 blkaddr -= FDEV(di).start_blk;
3917 }
3918
3919 if (len) {
3920 if (prev_bdev == cur_bdev &&
3921 index == prev_index + len &&
3922 blkaddr == prev_block + len) {
3923 len++;
3924 } else {
3925 ret = f2fs_secure_erase(prev_bdev,
3926 inode, prev_index, prev_block,
3927 len, range.flags);
3928 if (ret) {
3929 f2fs_put_dnode(&dn);
3930 goto out;
3931 }
3932
3933 len = 0;
3934 }
3935 }
3936
3937 if (!len) {
3938 prev_bdev = cur_bdev;
3939 prev_index = index;
3940 prev_block = blkaddr;
3941 len = 1;
3942 }
3943 }
3944
3945 f2fs_put_dnode(&dn);
3946
3947 if (fatal_signal_pending(current)) {
3948 ret = -EINTR;
3949 goto out;
3950 }
3951 cond_resched();
3952 }
3953
3954 if (len)
3955 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3956 prev_block, len, range.flags);
3957out:
3958 up_write(&F2FS_I(inode)->i_mmap_sem);
3959 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3960err:
3961 inode_unlock(inode);
3962 file_end_write(filp);
3963
3964 return ret;
3965}
3966
Chao Yude2041d2020-11-10 09:24:37 +08003967static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003968{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003969 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07003970 case FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003971 return f2fs_ioc_getflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003972 case FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003973 return f2fs_ioc_setflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003974 case FS_IOC_GETVERSION:
Chao Yud49f3e82015-01-23 20:36:04 +08003975 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003976 case F2FS_IOC_START_ATOMIC_WRITE:
3977 return f2fs_ioc_start_atomic_write(filp);
3978 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3979 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07003980 case F2FS_IOC_START_VOLATILE_WRITE:
3981 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08003982 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3983 return f2fs_ioc_release_volatile_write(filp);
3984 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3985 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08003986 case F2FS_IOC_SHUTDOWN:
3987 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003988 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003989 return f2fs_ioc_fitrim(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003990 case FS_IOC_SET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003991 return f2fs_ioc_set_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003992 case FS_IOC_GET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003993 return f2fs_ioc_get_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003994 case FS_IOC_GET_ENCRYPTION_PWSALT:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003995 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07003996 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3997 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3998 case FS_IOC_ADD_ENCRYPTION_KEY:
3999 return f2fs_ioc_add_encryption_key(filp, arg);
4000 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4001 return f2fs_ioc_remove_encryption_key(filp, arg);
4002 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4003 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4004 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4005 return f2fs_ioc_get_encryption_key_status(filp, arg);
Eric Biggersee446e12020-03-14 13:50:51 -07004006 case FS_IOC_GET_ENCRYPTION_NONCE:
4007 return f2fs_ioc_get_encryption_nonce(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08004008 case F2FS_IOC_GARBAGE_COLLECT:
4009 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07004010 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4011 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08004012 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08004013 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08004014 case F2FS_IOC_DEFRAGMENT:
4015 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004016 case F2FS_IOC_MOVE_RANGE:
4017 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07004018 case F2FS_IOC_FLUSH_DEVICE:
4019 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004020 case F2FS_IOC_GET_FEATURES:
4021 return f2fs_ioc_get_features(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004022 case FS_IOC_FSGETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08004023 return f2fs_ioc_fsgetxattr(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004024 case FS_IOC_FSSETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08004025 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004026 case F2FS_IOC_GET_PIN_FILE:
4027 return f2fs_ioc_get_pin_file(filp, arg);
4028 case F2FS_IOC_SET_PIN_FILE:
4029 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08004030 case F2FS_IOC_PRECACHE_EXTENTS:
4031 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004032 case F2FS_IOC_RESIZE_FS:
4033 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07004034 case FS_IOC_ENABLE_VERITY:
4035 return f2fs_ioc_enable_verity(filp, arg);
4036 case FS_IOC_MEASURE_VERITY:
4037 return f2fs_ioc_measure_verity(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004038 case FS_IOC_GETFSLABEL:
4039 return f2fs_ioc_getfslabel(filp, arg);
4040 case FS_IOC_SETFSLABEL:
4041 return f2fs_ioc_setfslabel(filp, arg);
Chao Yu439dfb12020-02-21 18:09:21 +08004042 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4043 return f2fs_get_compress_blocks(filp, arg);
Chao Yuef8d5632020-03-06 15:36:09 +08004044 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4045 return f2fs_release_compress_blocks(filp, arg);
Chao Yuc75488f2020-03-06 14:35:33 +08004046 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4047 return f2fs_reserve_compress_blocks(filp, arg);
Daeho Jeong9af84642020-07-21 12:21:11 +09004048 case F2FS_IOC_SEC_TRIM_FILE:
4049 return f2fs_sec_trim_file(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004050 default:
4051 return -ENOTTY;
4052 }
4053}
4054
Chao Yude2041d2020-11-10 09:24:37 +08004055long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4056{
4057 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4058 return -EIO;
4059 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4060 return -ENOSPC;
4061
4062 return __f2fs_ioctl(filp, cmd, arg);
4063}
4064
Chao Yu4c8ff702019-11-01 18:07:14 +08004065static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4066{
4067 struct file *file = iocb->ki_filp;
4068 struct inode *inode = file_inode(file);
Chao Yu8b83ac82020-04-16 18:16:56 +08004069 int ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004070
4071 if (!f2fs_is_compress_backend_ready(inode))
4072 return -EOPNOTSUPP;
4073
Chao Yu8b83ac82020-04-16 18:16:56 +08004074 ret = generic_file_read_iter(iocb, iter);
4075
4076 if (ret > 0)
4077 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4078
4079 return ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004080}
4081
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004082static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4083{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004084 struct file *file = iocb->ki_filp;
4085 struct inode *inode = file_inode(file);
4086 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004087
Chao Yu126ce722019-04-02 18:52:22 +08004088 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4089 ret = -EIO;
4090 goto out;
4091 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02004092
Chao Yu7bd29352020-02-24 19:20:15 +08004093 if (!f2fs_is_compress_backend_ready(inode)) {
4094 ret = -EOPNOTSUPP;
4095 goto out;
4096 }
Chao Yu4c8ff702019-11-01 18:07:14 +08004097
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004098 if (iocb->ki_flags & IOCB_NOWAIT) {
4099 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08004100 ret = -EAGAIN;
4101 goto out;
4102 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004103 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004104 inode_lock(inode);
4105 }
4106
Chao Yu04a49572020-12-26 18:07:01 +08004107 if (unlikely(IS_IMMUTABLE(inode))) {
4108 ret = -EPERM;
4109 goto unlock;
4110 }
4111
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004112 ret = generic_write_checks(iocb, from);
4113 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004114 bool preallocated = false;
4115 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004116 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004117
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004118 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4119 set_inode_flag(inode, FI_NO_PREALLOC);
4120
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004121 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4122 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004123 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004124 f2fs_has_inline_data(inode) ||
4125 f2fs_force_buffered_io(inode, iocb, from)) {
4126 clear_inode_flag(inode, FI_NO_PREALLOC);
4127 inode_unlock(inode);
4128 ret = -EAGAIN;
4129 goto out;
4130 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004131 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07004132 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004133
4134 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4135 goto write;
4136
4137 if (iocb->ki_flags & IOCB_DIRECT) {
4138 /*
4139 * Convert inline data for Direct I/O before entering
4140 * f2fs_direct_IO().
4141 */
4142 err = f2fs_convert_inline_inode(inode);
4143 if (err)
4144 goto out_err;
4145 /*
4146 * If force_buffere_io() is true, we have to allocate
4147 * blocks all the time, since f2fs_direct_IO will fall
4148 * back to buffered IO.
4149 */
4150 if (!f2fs_force_buffered_io(inode, iocb, from) &&
4151 allow_outplace_dio(inode, iocb, from))
4152 goto write;
4153 }
4154 preallocated = true;
4155 target_size = iocb->ki_pos + iov_iter_count(from);
4156
4157 err = f2fs_preallocate_blocks(iocb, from);
4158 if (err) {
4159out_err:
4160 clear_inode_flag(inode, FI_NO_PREALLOC);
4161 inode_unlock(inode);
4162 ret = err;
4163 goto out;
4164 }
4165write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004166 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004167 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08004168
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004169 /* if we couldn't write data, we should deallocate blocks. */
4170 if (preallocated && i_size_read(inode) < target_size)
4171 f2fs_truncate(inode);
4172
Chao Yub0af6d42017-08-02 23:21:48 +08004173 if (ret > 0)
4174 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004175 }
Chao Yu04a49572020-12-26 18:07:01 +08004176unlock:
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004177 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08004178out:
4179 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4180 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07004181 if (ret > 0)
4182 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004183 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004184}
4185
Namjae Jeone9750822013-02-04 23:41:41 +09004186#ifdef CONFIG_COMPAT
Chao Yude2041d2020-11-10 09:24:37 +08004187struct compat_f2fs_gc_range {
4188 u32 sync;
4189 compat_u64 start;
4190 compat_u64 len;
4191};
4192#define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4193 struct compat_f2fs_gc_range)
4194
4195static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4196{
4197 struct compat_f2fs_gc_range __user *urange;
4198 struct f2fs_gc_range range;
4199 int err;
4200
4201 urange = compat_ptr(arg);
4202 err = get_user(range.sync, &urange->sync);
4203 err |= get_user(range.start, &urange->start);
4204 err |= get_user(range.len, &urange->len);
4205 if (err)
4206 return -EFAULT;
4207
4208 return __f2fs_ioc_gc_range(file, &range);
4209}
4210
4211struct compat_f2fs_move_range {
4212 u32 dst_fd;
4213 compat_u64 pos_in;
4214 compat_u64 pos_out;
4215 compat_u64 len;
4216};
4217#define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4218 struct compat_f2fs_move_range)
4219
4220static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4221{
4222 struct compat_f2fs_move_range __user *urange;
4223 struct f2fs_move_range range;
4224 int err;
4225
4226 urange = compat_ptr(arg);
4227 err = get_user(range.dst_fd, &urange->dst_fd);
4228 err |= get_user(range.pos_in, &urange->pos_in);
4229 err |= get_user(range.pos_out, &urange->pos_out);
4230 err |= get_user(range.len, &urange->len);
4231 if (err)
4232 return -EFAULT;
4233
4234 return __f2fs_ioc_move_range(file, &range);
4235}
4236
Namjae Jeone9750822013-02-04 23:41:41 +09004237long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4238{
Chao Yude2041d2020-11-10 09:24:37 +08004239 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4240 return -EIO;
4241 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4242 return -ENOSPC;
4243
Namjae Jeone9750822013-02-04 23:41:41 +09004244 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07004245 case FS_IOC32_GETFLAGS:
4246 cmd = FS_IOC_GETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09004247 break;
Eric Biggers3357af82020-07-14 15:18:12 -07004248 case FS_IOC32_SETFLAGS:
4249 cmd = FS_IOC_SETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09004250 break;
Eric Biggers3357af82020-07-14 15:18:12 -07004251 case FS_IOC32_GETVERSION:
4252 cmd = FS_IOC_GETVERSION;
Chao Yu04ef4b62015-11-10 18:44:20 +08004253 break;
Chao Yude2041d2020-11-10 09:24:37 +08004254 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4255 return f2fs_compat_ioc_gc_range(file, arg);
4256 case F2FS_IOC32_MOVE_RANGE:
4257 return f2fs_compat_ioc_move_range(file, arg);
Chao Yu04ef4b62015-11-10 18:44:20 +08004258 case F2FS_IOC_START_ATOMIC_WRITE:
4259 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4260 case F2FS_IOC_START_VOLATILE_WRITE:
4261 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4262 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4263 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02004264 case FITRIM:
Eric Biggers3357af82020-07-14 15:18:12 -07004265 case FS_IOC_SET_ENCRYPTION_POLICY:
4266 case FS_IOC_GET_ENCRYPTION_PWSALT:
4267 case FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07004268 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4269 case FS_IOC_ADD_ENCRYPTION_KEY:
4270 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4271 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4272 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Eric Biggersee446e12020-03-14 13:50:51 -07004273 case FS_IOC_GET_ENCRYPTION_NONCE:
Chao Yu04ef4b62015-11-10 18:44:20 +08004274 case F2FS_IOC_GARBAGE_COLLECT:
4275 case F2FS_IOC_WRITE_CHECKPOINT:
4276 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kime066b832017-04-13 15:17:00 -07004277 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004278 case F2FS_IOC_GET_FEATURES:
Eric Biggers3357af82020-07-14 15:18:12 -07004279 case FS_IOC_FSGETXATTR:
4280 case FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004281 case F2FS_IOC_GET_PIN_FILE:
4282 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08004283 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004284 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07004285 case FS_IOC_ENABLE_VERITY:
4286 case FS_IOC_MEASURE_VERITY:
Eric Biggers3357af82020-07-14 15:18:12 -07004287 case FS_IOC_GETFSLABEL:
4288 case FS_IOC_SETFSLABEL:
Chao Yu439dfb12020-02-21 18:09:21 +08004289 case F2FS_IOC_GET_COMPRESS_BLOCKS:
Chao Yuef8d5632020-03-06 15:36:09 +08004290 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
Chao Yuc75488f2020-03-06 14:35:33 +08004291 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
Daeho Jeong9af84642020-07-21 12:21:11 +09004292 case F2FS_IOC_SEC_TRIM_FILE:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004293 break;
Namjae Jeone9750822013-02-04 23:41:41 +09004294 default:
4295 return -ENOIOCTLCMD;
4296 }
Chao Yude2041d2020-11-10 09:24:37 +08004297 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Namjae Jeone9750822013-02-04 23:41:41 +09004298}
4299#endif
4300
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004301const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08004302 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08004303 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004304 .write_iter = f2fs_file_write_iter,
4305 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08004306 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004307 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07004308 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004309 .fsync = f2fs_sync_file,
4310 .fallocate = f2fs_fallocate,
4311 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09004312#ifdef CONFIG_COMPAT
4313 .compat_ioctl = f2fs_compat_ioctl,
4314#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004315 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04004316 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004317};