blob: e3c294372db86a20799b47b48801b8da6f006d7c [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>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090024
25#include "f2fs.h"
26#include "node.h"
27#include "segment.h"
28#include "xattr.h"
29#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080030#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080031#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090032#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090033
Souptick Joarderea4d4792018-04-15 01:40:02 +053034static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080035{
36 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053037 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080038
39 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053040 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080041 up_read(&F2FS_I(inode)->i_mmap_sem);
42
Chao Yud7648342019-04-15 15:22:19 +080043 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
44
Souptick Joarderea4d4792018-04-15 01:40:02 +053045 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080046}
47
Souptick Joarderea4d4792018-04-15 01:40:02 +053048static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049{
50 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080051 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080053 struct dnode_of_data dn;
Chao Yu4c8ff702019-11-01 18:07:14 +080054 bool need_alloc = true;
55 int err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090056
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020057 if (unlikely(f2fs_cp_error(sbi))) {
58 err = -EIO;
59 goto err;
60 }
61
Chao Yu00e09c02019-08-23 17:58:36 +080062 if (!f2fs_is_checkpoint_ready(sbi)) {
63 err = -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +080064 goto err;
Chao Yu00e09c02019-08-23 17:58:36 +080065 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090066
Chao Yu4c8ff702019-11-01 18:07:14 +080067#ifdef CONFIG_F2FS_FS_COMPRESSION
68 if (f2fs_compressed_file(inode)) {
69 int ret = f2fs_is_compressed_cluster(inode, page->index);
70
71 if (ret < 0) {
72 err = ret;
73 goto err;
74 } else if (ret) {
75 if (ret < F2FS_I(inode)->i_cluster_size) {
76 err = -EAGAIN;
77 goto err;
78 }
79 need_alloc = false;
80 }
81 }
82#endif
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080083 /* should do out of any locked page */
Chao Yu4c8ff702019-11-01 18:07:14 +080084 if (need_alloc)
85 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080086
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090087 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070088
89 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070090
Dave Jiang11bac802017-02-24 14:56:41 -080091 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080092 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090093 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090094 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090095 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090096 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090097 unlock_page(page);
98 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080099 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900100 }
101
Chao Yu4c8ff702019-11-01 18:07:14 +0800102 if (need_alloc) {
103 /* block allocation */
104 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
105 set_new_dnode(&dn, inode, NULL, NULL, 0);
106 err = f2fs_get_block(&dn, page->index);
107 f2fs_put_dnode(&dn);
108 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu06c75402020-03-02 17:34:27 +0800109 }
110
111#ifdef CONFIG_F2FS_FS_COMPRESSION
112 if (!need_alloc) {
113 set_new_dnode(&dn, inode, NULL, NULL, 0);
114 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
115 f2fs_put_dnode(&dn);
116 }
117#endif
118 if (err) {
119 unlock_page(page);
120 goto out_sem;
Chao Yu39a86952018-09-27 18:33:18 +0800121 }
122
Chao Yubae0ee72018-12-25 17:43:42 +0800123 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +0800124
125 /* wait for GCed page writeback via META_MAPPING */
126 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
127
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900128 /*
129 * check to see if the page is mapped already (no holes)
130 */
131 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800132 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900133
134 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300135 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800136 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900137 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900138
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300139 offset = i_size_read(inode) & ~PAGE_MASK;
140 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900141 }
142 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700143 if (!PageUptodate(page))
144 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900145
Chao Yub0af6d42017-08-02 23:21:48 +0800146 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530147 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800148
Jaegeuk Kime943a102013-10-25 14:26:31 +0900149 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800150out_sem:
151 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800152
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900153 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200154err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900155 return block_page_mkwrite_return(err);
156}
157
158static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800159 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700160 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900161 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900162};
163
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900164static int get_parent_ino(struct inode *inode, nid_t *pino)
165{
166 struct dentry *dentry;
167
168 inode = igrab(inode);
169 dentry = d_find_any_alias(inode);
170 iput(inode);
171 if (!dentry)
172 return 0;
173
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900174 *pino = parent_ino(dentry);
175 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900176 return 1;
177}
178
Chao Yua5fd5052017-11-06 22:51:45 +0800179static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800180{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700181 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800182 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800183
Chao Yua5fd5052017-11-06 22:51:45 +0800184 if (!S_ISREG(inode->i_mode))
185 cp_reason = CP_NON_REGULAR;
Chao Yu4c8ff702019-11-01 18:07:14 +0800186 else if (f2fs_compressed_file(inode))
187 cp_reason = CP_COMPRESSED;
Chao Yua5fd5052017-11-06 22:51:45 +0800188 else if (inode->i_nlink != 1)
189 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700190 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800191 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800192 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800193 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800194 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800195 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800196 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800197 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700198 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800199 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800200 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800201 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800202 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800203 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
204 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
205 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800206 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800207
Chao Yua5fd5052017-11-06 22:51:45 +0800208 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800209}
210
Changman Lee9c7bb702014-12-08 15:29:40 +0900211static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
212{
213 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
214 bool ret = false;
215 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800216 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900217 ret = true;
218 f2fs_put_page(i, 0);
219 return ret;
220}
221
Changman Lee51455b12014-12-08 15:29:41 +0900222static void try_to_fix_pino(struct inode *inode)
223{
224 struct f2fs_inode_info *fi = F2FS_I(inode);
225 nid_t pino;
226
227 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900228 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
229 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700230 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900231 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900232 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700233 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900234}
235
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700236static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
237 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900238{
239 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700240 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700241 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900242 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800243 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900244 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900245 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900246 .nr_to_write = LONG_MAX,
247 .for_reclaim = 0,
248 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800249 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900250
Daniel Rosenberg43549942018-08-20 19:21:43 -0700251 if (unlikely(f2fs_readonly(inode->i_sb) ||
252 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900253 return 0;
254
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900255 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700256
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800257 if (S_ISDIR(inode->i_mode))
258 goto go_write;
259
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700260 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800261 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700262 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400263 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700264 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700265
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900266 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800267 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900268 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900269 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900270
Changman Lee9c7bb702014-12-08 15:29:40 +0900271 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800272 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700273 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900274 goto go_write;
275 }
276
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700277 /*
278 * if there is no written data, don't waste time to write recovery info.
279 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700280 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800281 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700282
Changman Lee9c7bb702014-12-08 15:29:40 +0900283 /* it may call write_inode just prior to fsync */
284 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700285 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700286
Jaegeuk Kim91942322016-05-20 10:13:22 -0700287 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800288 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700289 goto flush_out;
290 goto out;
291 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700292go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900293 /*
294 * Both of fdatasync() and fsync() are able to be recovered from
295 * sudden-power-off.
296 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700297 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800298 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700299 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900300
Chao Yua5fd5052017-11-06 22:51:45 +0800301 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900302 /* all the dirty node pages should be flushed for POR */
303 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900304
Changman Lee51455b12014-12-08 15:29:41 +0900305 /*
306 * We've secured consistency through sync_fs. Following pino
307 * will be used only for fsynced inodes after checkpoint.
308 */
309 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700310 clear_inode_flag(inode, FI_APPEND_WRITE);
311 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900312 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900313 }
Changman Lee51455b12014-12-08 15:29:41 +0900314sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800315 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800316 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800317 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700318 if (ret)
319 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900320
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800321 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800322 if (unlikely(f2fs_cp_error(sbi))) {
323 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800324 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800325 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800326
Chao Yu4d57b862018-05-30 00:20:41 +0800327 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700328 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900329 f2fs_write_inode(inode, NULL);
330 goto sync_nodes;
331 }
332
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700333 /*
334 * If it's atomic_write, it's just fine to keep write ordering. So
335 * here we don't need to wait for node write completion, since we use
336 * node chain which serializes node blocks. If one of node writes are
337 * reordered, we can see simply broken chain, resulting in stopping
338 * roll-forward recovery. It means we'll recover all or none node blocks
339 * given fsync mark.
340 */
341 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800342 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700343 if (ret)
344 goto out;
345 }
Changman Lee51455b12014-12-08 15:29:41 +0900346
347 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800348 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700349 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900350flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700351 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800352 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800353 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800354 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800355 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800356 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800357 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800358 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900359out:
Chao Yua5fd5052017-11-06 22:51:45 +0800360 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700361 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900362 return ret;
363}
364
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700365int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
366{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200367 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
368 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700369 return f2fs_do_sync_file(file, start, end, datasync, false);
370}
371
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900372static pgoff_t __get_first_dirty_index(struct address_space *mapping,
373 pgoff_t pgofs, int whence)
374{
Jan Kara8faab642017-11-15 17:34:55 -0800375 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900376 int nr_pages;
377
378 if (whence != SEEK_DATA)
379 return 0;
380
381 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800382 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
383 1, &page);
384 if (!nr_pages)
385 return ULONG_MAX;
386 pgofs = page->index;
387 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900388 return pgofs;
389}
390
Chao Yue1da7872018-06-05 17:44:11 +0800391static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
392 pgoff_t dirty, pgoff_t pgofs, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900393{
394 switch (whence) {
395 case SEEK_DATA:
396 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yu93770ab2019-04-15 15:26:32 +0800397 __is_valid_data_blkaddr(blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900398 return true;
399 break;
400 case SEEK_HOLE:
401 if (blkaddr == NULL_ADDR)
402 return true;
403 break;
404 }
405 return false;
406}
407
Chao Yu267378d2014-04-23 14:10:24 +0800408static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
409{
410 struct inode *inode = file->f_mapping->host;
411 loff_t maxbytes = inode->i_sb->s_maxbytes;
412 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900413 pgoff_t pgofs, end_offset, dirty;
414 loff_t data_ofs = offset;
415 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800416 int err = 0;
417
Al Viro59551022016-01-22 15:40:57 -0500418 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800419
420 isize = i_size_read(inode);
421 if (offset >= isize)
422 goto fail;
423
424 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800425 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800426 if (whence == SEEK_HOLE)
427 data_ofs = isize;
428 goto found;
429 }
430
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300431 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800432
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900433 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
434
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300435 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800436 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800437 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800438 if (err && err != -ENOENT) {
439 goto fail;
440 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900441 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800442 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800443 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800444 continue;
445 } else {
446 goto found;
447 }
448 }
449
Chao Yu81ca7352016-01-26 15:39:35 +0800450 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800451
452 /* find data/hole in dnode block */
453 for (; dn.ofs_in_node < end_offset;
454 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300455 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800456 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900457
Chao Yua2ced1c2020-02-14 17:44:10 +0800458 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu267378d2014-04-23 14:10:24 +0800459
Chao Yuc9b60782018-08-01 19:13:44 +0800460 if (__is_valid_data_blkaddr(blkaddr) &&
461 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800462 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800463 f2fs_put_dnode(&dn);
464 goto fail;
465 }
466
Chao Yue1da7872018-06-05 17:44:11 +0800467 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
468 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800469 f2fs_put_dnode(&dn);
470 goto found;
471 }
472 }
473 f2fs_put_dnode(&dn);
474 }
475
476 if (whence == SEEK_DATA)
477 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800478found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900479 if (whence == SEEK_HOLE && data_ofs > isize)
480 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500481 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800482 return vfs_setpos(file, data_ofs, maxbytes);
483fail:
Al Viro59551022016-01-22 15:40:57 -0500484 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800485 return -ENXIO;
486}
487
488static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
489{
490 struct inode *inode = file->f_mapping->host;
491 loff_t maxbytes = inode->i_sb->s_maxbytes;
492
493 switch (whence) {
494 case SEEK_SET:
495 case SEEK_CUR:
496 case SEEK_END:
497 return generic_file_llseek_size(file, offset, whence,
498 maxbytes, i_size_read(inode));
499 case SEEK_DATA:
500 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700501 if (offset < 0)
502 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800503 return f2fs_seek_block(file, offset, whence);
504 }
505
506 return -EINVAL;
507}
508
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900509static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
510{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700511 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800512 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700513
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200514 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
515 return -EIO;
516
Chao Yu4c8ff702019-11-01 18:07:14 +0800517 if (!f2fs_is_compress_backend_ready(inode))
518 return -EOPNOTSUPP;
519
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700520 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800521 err = f2fs_convert_inline_inode(inode);
522 if (err)
523 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700524
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900525 file_accessed(file);
526 vma->vm_ops = &f2fs_file_vm_ops;
Chao Yu4c8ff702019-11-01 18:07:14 +0800527 set_inode_flag(inode, FI_MMAP_FILE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900528 return 0;
529}
530
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700531static int f2fs_file_open(struct inode *inode, struct file *filp)
532{
Eric Biggers2e168c82017-11-29 12:35:28 -0800533 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700534
Eric Biggers2e168c82017-11-29 12:35:28 -0800535 if (err)
536 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900537
Chao Yu4c8ff702019-11-01 18:07:14 +0800538 if (!f2fs_is_compress_backend_ready(inode))
539 return -EOPNOTSUPP;
540
Eric Biggers95ae2512019-07-22 09:26:24 -0700541 err = fsverity_file_open(inode, filp);
542 if (err)
543 return err;
544
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900545 filp->f_mode |= FMODE_NOWAIT;
546
Chao Yu0abd6752017-07-09 00:13:07 +0800547 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700548}
549
Chao Yu4d57b862018-05-30 00:20:41 +0800550void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900551{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700552 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900553 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800554 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900555 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800556 int base = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800557 bool compressed_cluster = false;
558 int cluster_index = 0, valid_blocks = 0;
559 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
Chao Yu7a2af762017-07-19 00:19:06 +0800560
561 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
562 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900563
Gu Zheng45590712013-07-15 17:57:38 +0800564 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800565 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900566
Chao Yu4c8ff702019-11-01 18:07:14 +0800567 /* Assumption: truncateion starts with cluster */
568 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900569 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900570
Chao Yu4c8ff702019-11-01 18:07:14 +0800571 if (f2fs_compressed_file(dn->inode) &&
572 !(cluster_index & (cluster_size - 1))) {
573 if (compressed_cluster)
574 f2fs_i_compr_blocks_update(dn->inode,
575 valid_blocks, false);
576 compressed_cluster = (blkaddr == COMPRESS_ADDR);
577 valid_blocks = 0;
578 }
579
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900580 if (blkaddr == NULL_ADDR)
581 continue;
582
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800583 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800584 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800585
Chao Yu4c8ff702019-11-01 18:07:14 +0800586 if (__is_valid_data_blkaddr(blkaddr)) {
587 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800588 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800589 continue;
590 if (compressed_cluster)
591 valid_blocks++;
592 }
Chao Yuc9b60782018-08-01 19:13:44 +0800593
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700594 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700595 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800596
597 f2fs_invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900598 nr_free++;
599 }
Chao Yu19b2c302015-08-26 20:34:48 +0800600
Chao Yu4c8ff702019-11-01 18:07:14 +0800601 if (compressed_cluster)
602 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
603
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900604 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800605 pgoff_t fofs;
606 /*
607 * once we invalidate valid blkaddr in range [ofs, ofs + count],
608 * we will invalidate all blkaddr in the whole range.
609 */
Chao Yu4d57b862018-05-30 00:20:41 +0800610 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800611 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800612 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900613 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900614 }
615 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900616
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800617 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900618 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
619 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620}
621
Chao Yu4d57b862018-05-30 00:20:41 +0800622void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900623{
Chao Yud02a6e62019-03-25 21:08:19 +0800624 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900625}
626
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800627static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700628 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900629{
youngjun yoo193bea12018-05-30 04:21:14 +0900630 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300631 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700632 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900633 struct page *page;
634
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700635 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700636 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900637
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700638 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700639 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700640 if (page && PageUptodate(page))
641 goto truncate_out;
642 f2fs_put_page(page, 1);
643 return 0;
644 }
645
Chao Yu4c8ff702019-11-01 18:07:14 +0800646 if (f2fs_compressed_file(inode))
647 return 0;
648
Chao Yu4d57b862018-05-30 00:20:41 +0800649 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900650 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800651 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700652truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800653 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300654 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700655
656 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530657 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700658 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800659 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900660 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700661 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900662}
663
Chao Yu4c8ff702019-11-01 18:07:14 +0800664static int do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700666 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900667 struct dnode_of_data dn;
668 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800669 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700670 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800671 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900672
Namjae Jeon51dd6242013-04-20 01:28:52 +0900673 trace_f2fs_truncate_blocks_enter(inode, from);
674
Chao Yudf033ca2018-03-20 23:08:29 +0800675 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900676
Chao Yu09210c92016-05-05 19:13:03 +0800677 if (free_from >= sbi->max_file_blocks)
678 goto free_partial;
679
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700680 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800681 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800682
Chao Yu4d57b862018-05-30 00:20:41 +0800683 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700684 if (IS_ERR(ipage)) {
685 err = PTR_ERR(ipage);
686 goto out;
687 }
688
689 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800690 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700691 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800692 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700693 goto out;
694 }
695
696 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800697 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900698 if (err) {
699 if (err == -ENOENT)
700 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700701 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700702 }
703
Chao Yu81ca7352016-01-26 15:39:35 +0800704 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900705
706 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700707 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900708
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900709 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800710 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900711 free_from += count;
712 }
713
714 f2fs_put_dnode(&dn);
715free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800716 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800717out:
718 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800719 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800720free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700721 /* lastly zero out the first data page */
722 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800723 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900724
Namjae Jeon51dd6242013-04-20 01:28:52 +0900725 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726 return err;
727}
728
Chao Yu4c8ff702019-11-01 18:07:14 +0800729int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
730{
731 u64 free_from = from;
732
733 /*
734 * for compressed file, only support cluster size
735 * aligned truncation.
736 */
737 if (f2fs_compressed_file(inode)) {
738 size_t cluster_shift = PAGE_SHIFT +
739 F2FS_I(inode)->i_log_cluster_size;
740 size_t cluster_mask = (1 << cluster_shift) - 1;
741
742 free_from = from >> cluster_shift;
743 if (from & cluster_mask)
744 free_from++;
745 free_from <<= cluster_shift;
746 }
747
748 return do_truncate_blocks(inode, free_from, lock);
749}
750
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700751int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900752{
Chao Yub0154892015-08-24 17:39:42 +0800753 int err;
754
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200755 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
756 return -EIO;
757
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900758 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
759 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800760 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900761
Namjae Jeon51dd6242013-04-20 01:28:52 +0900762 trace_f2fs_truncate(inode);
763
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800764 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800765 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800766 return -EIO;
767 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200768
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800769 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800770 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800771 err = f2fs_convert_inline_inode(inode);
772 if (err)
773 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800774 }
775
Chao Yuc42d28c2019-02-02 17:33:01 +0800776 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800777 if (err)
778 return err;
779
Deepa Dinamani078cd822016-09-14 07:48:04 -0700780 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700781 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800782 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900783}
784
David Howellsa528d352017-01-31 16:46:22 +0000785int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800786 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900787{
David Howellsa528d352017-01-31 16:46:22 +0000788 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800789 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800790 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800791 unsigned int flags;
792
Chao Yu1c1d35d2018-01-25 14:54:42 +0800793 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800794 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800795 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
796 stat->result_mask |= STATX_BTIME;
797 stat->btime.tv_sec = fi->i_crtime.tv_sec;
798 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
799 }
800
Eric Biggers36098552019-06-04 22:59:04 -0700801 flags = fi->i_flags;
Chao Yu59c84402018-04-03 15:08:17 +0800802 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800803 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530804 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800805 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800806 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800807 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800808 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800809 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700810 if (IS_VERITY(inode))
811 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800812
813 stat->attributes_mask |= (STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800814 STATX_ATTR_ENCRYPTED |
815 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700816 STATX_ATTR_NODUMP |
817 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800818
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900819 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700820
821 /* we need to show initial sectors used for inline_data/dentries */
822 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
823 f2fs_has_inline_dentry(inode))
824 stat->blocks += (stat->size + 511) >> 9;
825
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900826 return 0;
827}
828
829#ifdef CONFIG_F2FS_FS_POSIX_ACL
830static void __setattr_copy(struct inode *inode, const struct iattr *attr)
831{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900832 unsigned int ia_valid = attr->ia_valid;
833
834 if (ia_valid & ATTR_UID)
835 inode->i_uid = attr->ia_uid;
836 if (ia_valid & ATTR_GID)
837 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200838 if (ia_valid & ATTR_ATIME)
839 inode->i_atime = attr->ia_atime;
840 if (ia_valid & ATTR_MTIME)
841 inode->i_mtime = attr->ia_mtime;
842 if (ia_valid & ATTR_CTIME)
843 inode->i_ctime = attr->ia_ctime;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900844 if (ia_valid & ATTR_MODE) {
845 umode_t mode = attr->ia_mode;
846
847 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
848 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700849 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900850 }
851}
852#else
853#define __setattr_copy setattr_copy
854#endif
855
856int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
857{
David Howells2b0143b2015-03-17 22:25:59 +0000858 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900859 int err;
860
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200861 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
862 return -EIO;
863
Chao Yu4c8ff702019-11-01 18:07:14 +0800864 if ((attr->ia_valid & ATTR_SIZE) &&
865 !f2fs_is_compress_backend_ready(inode))
866 return -EOPNOTSUPP;
867
Jan Kara31051c82016-05-26 16:55:18 +0200868 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900869 if (err)
870 return err;
871
Eric Biggers20bb2472017-11-29 12:35:32 -0800872 err = fscrypt_prepare_setattr(dentry, attr);
873 if (err)
874 return err;
875
Eric Biggers95ae2512019-07-22 09:26:24 -0700876 err = fsverity_prepare_setattr(dentry, attr);
877 if (err)
878 return err;
879
Chao Yu0abd6752017-07-09 00:13:07 +0800880 if (is_quota_modification(inode, attr)) {
881 err = dquot_initialize(inode);
882 if (err)
883 return err;
884 }
885 if ((attr->ia_valid & ATTR_UID &&
886 !uid_eq(attr->ia_uid, inode->i_uid)) ||
887 (attr->ia_valid & ATTR_GID &&
888 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800889 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800890 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800891 if (err) {
892 set_sbi_flag(F2FS_I_SB(inode),
893 SBI_QUOTA_NEED_REPAIR);
894 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800895 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800896 }
897 /*
898 * update uid/gid under lock_op(), so that dquot and inode can
899 * be updated atomically.
900 */
901 if (attr->ia_valid & ATTR_UID)
902 inode->i_uid = attr->ia_uid;
903 if (attr->ia_valid & ATTR_GID)
904 inode->i_gid = attr->ia_gid;
905 f2fs_mark_inode_dirty_sync(inode, true);
906 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800907 }
908
Chao Yu09db6a22014-09-15 18:02:09 +0800909 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800910 loff_t old_size = i_size_read(inode);
911
912 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
913 /*
914 * should convert inline inode before i_size_write to
915 * keep smaller than inline_data size with inline flag.
916 */
917 err = f2fs_convert_inline_inode(inode);
918 if (err)
919 return err;
920 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800921
Chao Yua33c1502018-08-05 23:04:25 +0800922 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900923 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800924
925 truncate_setsize(inode, attr->ia_size);
926
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800927 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800928 err = f2fs_truncate(inode);
929 /*
930 * do not trim all blocks after i_size if target size is
931 * larger than i_size.
932 */
Chao Yua33c1502018-08-05 23:04:25 +0800933 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900934 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800935 if (err)
936 return err;
937
Chao Yuc10c9822020-02-27 19:30:03 +0800938 spin_lock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800939 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800940 F2FS_I(inode)->last_disk_size = i_size_read(inode);
Chao Yuc10c9822020-02-27 19:30:03 +0800941 spin_unlock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900942 }
943
944 __setattr_copy(inode, attr);
945
946 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800947 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700948 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
949 inode->i_mode = F2FS_I(inode)->i_acl_mode;
950 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900951 }
952 }
953
Yunlei Hec0ed4402016-12-11 15:35:15 +0800954 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800955 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700956
957 /* inode change will produce dirty node pages flushed by checkpoint */
958 f2fs_balance_fs(F2FS_I_SB(inode), true);
959
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900960 return err;
961}
962
963const struct inode_operations f2fs_file_inode_operations = {
964 .getattr = f2fs_getattr,
965 .setattr = f2fs_setattr,
966 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800967 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900968#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900969 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900970#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900971 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900972};
973
Chao Yu63943282015-08-07 18:36:06 +0800974static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900975 loff_t start, loff_t len)
976{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700977 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900978 struct page *page;
979
980 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800981 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900982
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800983 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900984
Gu Zhenge4795562013-09-27 18:08:30 +0800985 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800986 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800987 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900988
Chao Yu63943282015-08-07 18:36:06 +0800989 if (IS_ERR(page))
990 return PTR_ERR(page);
991
Chao Yubae0ee72018-12-25 17:43:42 +0800992 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +0800993 zero_user(page, start, len);
994 set_page_dirty(page);
995 f2fs_put_page(page, 1);
996 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900997}
998
Chao Yu4d57b862018-05-30 00:20:41 +0800999int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001000{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001001 int err;
1002
Chao Yuea587112015-09-17 20:22:44 +08001003 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001004 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001005 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001006
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001007 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001008 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001009 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001010 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001011 pg_start = f2fs_get_next_page_offset(&dn,
1012 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001013 continue;
Chao Yuea587112015-09-17 20:22:44 +08001014 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001015 return err;
1016 }
1017
Chao Yu81ca7352016-01-26 15:39:35 +08001018 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001019 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1020
1021 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1022
Chao Yu4d57b862018-05-30 00:20:41 +08001023 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001024 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001025
1026 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001027 }
1028 return 0;
1029}
1030
Chao Yua66c7b22013-11-22 16:52:50 +08001031static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001032{
1033 pgoff_t pg_start, pg_end;
1034 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001035 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001036
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001037 ret = f2fs_convert_inline_inode(inode);
1038 if (ret)
1039 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001040
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1042 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001043
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001044 off_start = offset & (PAGE_SIZE - 1);
1045 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001046
1047 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001048 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001049 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001050 if (ret)
1051 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001052 } else {
Chao Yu63943282015-08-07 18:36:06 +08001053 if (off_start) {
1054 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001055 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001056 if (ret)
1057 return ret;
1058 }
1059 if (off_end) {
1060 ret = fill_zero(inode, pg_end, 0, off_end);
1061 if (ret)
1062 return ret;
1063 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001064
1065 if (pg_start < pg_end) {
1066 struct address_space *mapping = inode->i_mapping;
1067 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001068 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001069
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001070 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001071
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001072 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1073 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001074
Chao Yua33c1502018-08-05 23:04:25 +08001075 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001076 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001077
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001078 truncate_inode_pages_range(mapping, blk_start,
1079 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001080
Gu Zhenge4795562013-09-27 18:08:30 +08001081 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001082 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001083 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001084
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001085 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001086 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001087 }
1088 }
1089
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001090 return ret;
1091}
1092
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001093static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1094 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001095{
1096 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1097 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001098 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001099
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001100next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001101 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001102 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001103 if (ret && ret != -ENOENT) {
1104 return ret;
1105 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001106 if (dn.max_level == 0)
1107 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001108 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1109 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001110 blkaddr += done;
1111 do_replace += done;
1112 goto next;
1113 }
1114
1115 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1116 dn.ofs_in_node, len);
1117 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001118 *blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu93770ab2019-04-15 15:26:32 +08001119
1120 if (__is_valid_data_blkaddr(*blkaddr) &&
1121 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1122 DATA_GENERIC_ENHANCE)) {
1123 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001124 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001125 }
1126
Chao Yu4d57b862018-05-30 00:20:41 +08001127 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001128
Chao Yub0332a02020-02-14 17:44:12 +08001129 if (f2fs_lfs_mode(sbi)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001130 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001131 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001132 }
1133
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001134 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001135 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001136 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001137 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001138 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001139 f2fs_put_dnode(&dn);
1140next:
1141 len -= done;
1142 off += done;
1143 if (len)
1144 goto next_dnode;
1145 return 0;
1146}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001147
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001148static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1149 int *do_replace, pgoff_t off, int len)
1150{
1151 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1152 struct dnode_of_data dn;
1153 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001154
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001155 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1156 if (*do_replace == 0)
1157 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001158
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001159 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001160 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001161 if (ret) {
1162 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001163 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001164 } else {
1165 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001166 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001167 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001168 }
1169 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001170}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001171
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001172static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1173 block_t *blkaddr, int *do_replace,
1174 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1175{
1176 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1177 pgoff_t i = 0;
1178 int ret;
1179
1180 while (i < len) {
1181 if (blkaddr[i] == NULL_ADDR && !full) {
1182 i++;
1183 continue;
1184 }
1185
1186 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1187 struct dnode_of_data dn;
1188 struct node_info ni;
1189 size_t new_size;
1190 pgoff_t ilen;
1191
1192 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001193 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001194 if (ret)
1195 return ret;
1196
Chao Yu77357302018-07-17 00:02:17 +08001197 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1198 if (ret) {
1199 f2fs_put_dnode(&dn);
1200 return ret;
1201 }
1202
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001203 ilen = min((pgoff_t)
1204 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1205 dn.ofs_in_node, len - i);
1206 do {
Chao Yua2ced1c2020-02-14 17:44:10 +08001207 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu4d57b862018-05-30 00:20:41 +08001208 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001209
1210 if (do_replace[i]) {
1211 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001212 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001213 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001214 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001215 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1216 blkaddr[i], ni.version, true, false);
1217
1218 do_replace[i] = 0;
1219 }
1220 dn.ofs_in_node++;
1221 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001222 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001223 if (dst_inode->i_size < new_size)
1224 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001225 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001226
1227 f2fs_put_dnode(&dn);
1228 } else {
1229 struct page *psrc, *pdst;
1230
Chao Yu4d57b862018-05-30 00:20:41 +08001231 psrc = f2fs_get_lock_data_page(src_inode,
1232 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001233 if (IS_ERR(psrc))
1234 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001235 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001236 true);
1237 if (IS_ERR(pdst)) {
1238 f2fs_put_page(psrc, 1);
1239 return PTR_ERR(pdst);
1240 }
1241 f2fs_copy_page(psrc, pdst);
1242 set_page_dirty(pdst);
1243 f2fs_put_page(pdst, 1);
1244 f2fs_put_page(psrc, 1);
1245
Chao Yu4d57b862018-05-30 00:20:41 +08001246 ret = f2fs_truncate_hole(src_inode,
1247 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001248 if (ret)
1249 return ret;
1250 i++;
1251 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001252 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001253 return 0;
1254}
1255
1256static int __exchange_data_block(struct inode *src_inode,
1257 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001258 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001259{
1260 block_t *src_blkaddr;
1261 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001262 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001263 int ret;
1264
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001265 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001266 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001267
Chao Yu628b3d12017-11-30 19:28:18 +08001268 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001269 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001270 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001271 if (!src_blkaddr)
1272 return -ENOMEM;
1273
Chao Yu628b3d12017-11-30 19:28:18 +08001274 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001275 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001276 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001277 if (!do_replace) {
1278 kvfree(src_blkaddr);
1279 return -ENOMEM;
1280 }
1281
1282 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1283 do_replace, src, olen);
1284 if (ret)
1285 goto roll_back;
1286
1287 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1288 do_replace, src, dst, olen, full);
1289 if (ret)
1290 goto roll_back;
1291
1292 src += olen;
1293 dst += olen;
1294 len -= olen;
1295
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001296 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001297 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001298 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001299 return 0;
1300
1301roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001302 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001303 kvfree(src_blkaddr);
1304 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001305 return ret;
1306}
1307
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001308static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001309{
1310 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001311 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001312 pgoff_t start = offset >> PAGE_SHIFT;
1313 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001314 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001315
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001316 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001317
1318 /* avoid gc operation during block exchange */
1319 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1320 down_write(&F2FS_I(inode)->i_mmap_sem);
1321
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001322 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001323 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001324 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001325 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1326 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001327
1328 up_write(&F2FS_I(inode)->i_mmap_sem);
1329 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001330 return ret;
1331}
1332
1333static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1334{
Chao Yub4ace332015-05-06 13:09:46 +08001335 loff_t new_size;
1336 int ret;
1337
Chao Yub4ace332015-05-06 13:09:46 +08001338 if (offset + len >= i_size_read(inode))
1339 return -EINVAL;
1340
1341 /* collapse range should be aligned to block size of f2fs. */
1342 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1343 return -EINVAL;
1344
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001345 ret = f2fs_convert_inline_inode(inode);
1346 if (ret)
1347 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001348
Chao Yub4ace332015-05-06 13:09:46 +08001349 /* write out all dirty pages from offset */
1350 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1351 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001352 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001353
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001354 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001355 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001356 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001357
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001358 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001359 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001360 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1361 truncate_pagecache(inode, offset);
1362
Chao Yub4ace332015-05-06 13:09:46 +08001363 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001364 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001365
Chao Yuc42d28c2019-02-02 17:33:01 +08001366 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001367 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001368 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001369 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001370 return ret;
1371}
1372
Chao Yu6e961942016-05-09 19:56:31 +08001373static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1374 pgoff_t end)
1375{
1376 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1377 pgoff_t index = start;
1378 unsigned int ofs_in_node = dn->ofs_in_node;
1379 blkcnt_t count = 0;
1380 int ret;
1381
1382 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001383 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001384 count++;
1385 }
1386
1387 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001388 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001389 if (ret)
1390 return ret;
1391
1392 dn->ofs_in_node = ofs_in_node;
1393 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001394 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001395 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001396 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001397 * allocation.
1398 */
1399 if (dn->data_blkaddr == NULL_ADDR) {
1400 ret = -ENOSPC;
1401 break;
1402 }
1403 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001404 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001405 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001406 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001407 }
1408 }
1409
1410 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1411
1412 return ret;
1413}
1414
Chao Yu75cd4e02015-05-06 13:11:13 +08001415static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1416 int mode)
1417{
1418 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1419 struct address_space *mapping = inode->i_mapping;
1420 pgoff_t index, pg_start, pg_end;
1421 loff_t new_size = i_size_read(inode);
1422 loff_t off_start, off_end;
1423 int ret = 0;
1424
Chao Yu75cd4e02015-05-06 13:11:13 +08001425 ret = inode_newsize_ok(inode, (len + offset));
1426 if (ret)
1427 return ret;
1428
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001429 ret = f2fs_convert_inline_inode(inode);
1430 if (ret)
1431 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001432
1433 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1434 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001435 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001436
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001437 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1438 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001439
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001440 off_start = offset & (PAGE_SIZE - 1);
1441 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001442
1443 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001444 ret = fill_zero(inode, pg_start, off_start,
1445 off_end - off_start);
1446 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001447 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001448
Chao Yu75cd4e02015-05-06 13:11:13 +08001449 new_size = max_t(loff_t, new_size, offset + len);
1450 } else {
1451 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001452 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001453 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001454 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001455 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001456
Chao Yu75cd4e02015-05-06 13:11:13 +08001457 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001459 }
1460
Chao Yu6e961942016-05-09 19:56:31 +08001461 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001462 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001463 unsigned int end_offset;
1464 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001465
Chao Yuc70798532018-08-05 23:02:22 +08001466 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001467 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001468
1469 truncate_pagecache_range(inode,
1470 (loff_t)index << PAGE_SHIFT,
1471 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1472
Chao Yu75cd4e02015-05-06 13:11:13 +08001473 f2fs_lock_op(sbi);
1474
Chao Yu6e961942016-05-09 19:56:31 +08001475 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001476 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001477 if (ret) {
1478 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001479 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001480 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001481 goto out;
1482 }
1483
Chao Yu6e961942016-05-09 19:56:31 +08001484 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1485 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1486
1487 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001488 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001489
Chao Yu75cd4e02015-05-06 13:11:13 +08001490 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001491 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001492 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001493
1494 f2fs_balance_fs(sbi, dn.node_changed);
1495
Chao Yu6e961942016-05-09 19:56:31 +08001496 if (ret)
1497 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001498
Chao Yu6e961942016-05-09 19:56:31 +08001499 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001500 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001501 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001502 }
1503
1504 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001505 ret = fill_zero(inode, pg_end, 0, off_end);
1506 if (ret)
1507 goto out;
1508
Chao Yu75cd4e02015-05-06 13:11:13 +08001509 new_size = max_t(loff_t, new_size, offset + len);
1510 }
1511 }
1512
1513out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001514 if (new_size > i_size_read(inode)) {
1515 if (mode & FALLOC_FL_KEEP_SIZE)
1516 file_set_keep_isize(inode);
1517 else
1518 f2fs_i_size_write(inode, new_size);
1519 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001520 return ret;
1521}
1522
Chao Yuf62185d2015-05-28 19:16:57 +08001523static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1524{
1525 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001526 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001527 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001528 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001529
Chao Yuf62185d2015-05-28 19:16:57 +08001530 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001531 ret = inode_newsize_ok(inode, new_size);
1532 if (ret)
1533 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001534
1535 if (offset >= i_size_read(inode))
1536 return -EINVAL;
1537
1538 /* insert range should be aligned to block size of f2fs. */
1539 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1540 return -EINVAL;
1541
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001542 ret = f2fs_convert_inline_inode(inode);
1543 if (ret)
1544 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001545
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001546 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001547
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001548 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001549 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001550 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001551 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001552 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001553
1554 /* write out all dirty pages from offset */
1555 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1556 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001557 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001559 pg_start = offset >> PAGE_SHIFT;
1560 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001561 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001562 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001563
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001564 /* avoid gc operation during block exchange */
1565 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1566 down_write(&F2FS_I(inode)->i_mmap_sem);
1567 truncate_pagecache(inode, offset);
1568
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001569 while (!ret && idx > pg_start) {
1570 nr = idx - pg_start;
1571 if (nr > delta)
1572 nr = delta;
1573 idx -= nr;
1574
Chao Yuf62185d2015-05-28 19:16:57 +08001575 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001576 f2fs_drop_extent_tree(inode);
1577
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001578 ret = __exchange_data_block(inode, inode, idx,
1579 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001580 f2fs_unlock_op(sbi);
1581 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001582 up_write(&F2FS_I(inode)->i_mmap_sem);
1583 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001584
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001585 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001586 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001587 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1588 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001589 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001590
1591 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001592 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001593 return ret;
1594}
1595
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001596static int expand_inode_data(struct inode *inode, loff_t offset,
1597 loff_t len, int mode)
1598{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001599 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001600 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001601 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1602 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001603 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001604 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001605 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001606 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001607
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001608 err = inode_newsize_ok(inode, (len + offset));
1609 if (err)
1610 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001611
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001612 err = f2fs_convert_inline_inode(inode);
1613 if (err)
1614 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001615
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001616 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001617
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001618 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001619 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001620
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001621 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1622 map.m_len = pg_end - map.m_lblk;
1623 if (off_end)
1624 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001625
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001626 if (!map.m_len)
1627 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001628
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001629 if (f2fs_is_pinned_file(inode)) {
1630 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1631 sbi->log_blocks_per_seg;
1632 block_t done = 0;
1633
1634 if (map.m_len % sbi->blocks_per_seg)
1635 len += sbi->blocks_per_seg;
1636
1637 map.m_len = sbi->blocks_per_seg;
1638next_alloc:
1639 if (has_not_enough_free_secs(sbi, 0,
1640 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
Chao Yufb24fea2020-01-14 19:36:50 +08001641 down_write(&sbi->gc_lock);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001642 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1643 if (err && err != -ENODATA && err != -EAGAIN)
1644 goto out_err;
1645 }
1646
1647 down_write(&sbi->pin_sem);
1648 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1649 f2fs_allocate_new_segments(sbi, CURSEG_COLD_DATA);
1650 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1651 up_write(&sbi->pin_sem);
1652
1653 done += map.m_len;
1654 len -= map.m_len;
1655 map.m_lblk += map.m_len;
1656 if (!err && len)
1657 goto next_alloc;
1658
1659 map.m_len = done;
1660 } else {
1661 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1662 }
1663out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001664 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001665 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001666
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001667 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001668 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001669
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001670 last_off = map.m_lblk + map.m_len - 1;
1671
1672 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001673 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001674 (loff_t)(last_off + 1) << PAGE_SHIFT;
1675 } else {
1676 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001677 }
1678
Chao Yue8ed90a2017-11-05 21:53:30 +08001679 if (new_size > i_size_read(inode)) {
1680 if (mode & FALLOC_FL_KEEP_SIZE)
1681 file_set_keep_isize(inode);
1682 else
1683 f2fs_i_size_write(inode, new_size);
1684 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001685
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001686 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001687}
1688
1689static long f2fs_fallocate(struct file *file, int mode,
1690 loff_t offset, loff_t len)
1691{
Al Viro6131ffa2013-02-27 16:59:05 -05001692 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001693 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001694
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001695 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1696 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001697 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1698 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001699 if (!f2fs_is_compress_backend_ready(inode))
1700 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001701
Chao Yuc9980122015-09-11 14:39:02 +08001702 /* f2fs only support ->fallocate for regular file */
1703 if (!S_ISREG(inode->i_mode))
1704 return -EINVAL;
1705
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301706 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001707 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001708 return -EOPNOTSUPP;
1709
Chao Yu4c8ff702019-11-01 18:07:14 +08001710 if (f2fs_compressed_file(inode) &&
1711 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1712 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1713 return -EOPNOTSUPP;
1714
Chao Yub4ace332015-05-06 13:09:46 +08001715 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001716 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1717 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001718 return -EOPNOTSUPP;
1719
Al Viro59551022016-01-22 15:40:57 -05001720 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001721
Taehee Yoo587c0a42015-04-21 15:59:12 +09001722 if (mode & FALLOC_FL_PUNCH_HOLE) {
1723 if (offset >= inode->i_size)
1724 goto out;
1725
Chao Yua66c7b22013-11-22 16:52:50 +08001726 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001727 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1728 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001729 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1730 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001731 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1732 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001733 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001734 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001735 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001736
Namjae Jeon3af60a42012-12-30 14:52:37 +09001737 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001738 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001739 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001740 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001741 }
Chao Yu3375f692014-01-28 10:29:26 +08001742
Taehee Yoo587c0a42015-04-21 15:59:12 +09001743out:
Al Viro59551022016-01-22 15:40:57 -05001744 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001745
Namjae Jeonc01e2852013-04-23 17:00:52 +09001746 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001747 return ret;
1748}
1749
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001750static int f2fs_release_file(struct inode *inode, struct file *filp)
1751{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001752 /*
1753 * f2fs_relase_file is called at every close calls. So we should
1754 * not drop any inmemory pages by close called by other process.
1755 */
1756 if (!(filp->f_mode & FMODE_WRITE) ||
1757 atomic_read(&inode->i_writecount) != 1)
1758 return 0;
1759
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001760 /* some remained atomic pages should discarded */
1761 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001762 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001763 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001764 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001765 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001766 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001767 clear_inode_flag(inode, FI_VOLATILE_FILE);
1768 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001769 }
1770 return 0;
1771}
1772
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001773static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001774{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001775 struct inode *inode = file_inode(file);
1776
1777 /*
1778 * If the process doing a transaction is crashed, we should do
1779 * roll-back. Otherwise, other reader/write can see corrupted database
1780 * until all the writers close its file. Since this should be done
1781 * before dropping file lock, it needs to do in ->flush.
1782 */
1783 if (f2fs_is_atomic_file(inode) &&
1784 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001785 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001786 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001787}
1788
Eric Biggers36098552019-06-04 22:59:04 -07001789static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001790{
1791 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001792 u32 masked_flags = fi->i_flags & mask;
1793
1794 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
Chao Yu2c1d0302017-07-29 00:32:52 +08001795
1796 /* Is it quota file? Do not allow user to mess with it */
1797 if (IS_NOQUOTA(inode))
1798 return -EPERM;
1799
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001800 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001801 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1802 return -EOPNOTSUPP;
1803 if (!f2fs_empty_dir(inode))
1804 return -ENOTEMPTY;
1805 }
1806
Chao Yu4c8ff702019-11-01 18:07:14 +08001807 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1808 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1809 return -EOPNOTSUPP;
1810 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1811 return -EINVAL;
1812 }
1813
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001814 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
Chao Yu2536ac62020-03-10 20:50:09 +08001815 if (S_ISREG(inode->i_mode) && (masked_flags & F2FS_COMPR_FL)) {
1816 if (f2fs_disable_compressed_file(inode))
1817 return -EINVAL;
1818 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001819 if (iflags & F2FS_NOCOMP_FL)
1820 return -EINVAL;
1821 if (iflags & F2FS_COMPR_FL) {
1822 int err = f2fs_convert_inline_inode(inode);
1823
1824 if (err)
1825 return err;
1826
1827 if (!f2fs_may_compress(inode))
1828 return -EINVAL;
1829
1830 set_compress_context(inode);
1831 }
1832 }
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001833 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1834 if (masked_flags & F2FS_COMPR_FL)
Chao Yu4c8ff702019-11-01 18:07:14 +08001835 return -EINVAL;
1836 }
1837
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001838 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001839 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1840 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001841
Chao Yu59c84402018-04-03 15:08:17 +08001842 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001843 set_inode_flag(inode, FI_PROJ_INHERIT);
1844 else
1845 clear_inode_flag(inode, FI_PROJ_INHERIT);
1846
1847 inode->i_ctime = current_time(inode);
1848 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001849 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001850 return 0;
1851}
1852
Eric Biggers36098552019-06-04 22:59:04 -07001853/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1854
1855/*
1856 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1857 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1858 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1859 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1860 */
1861
1862static const struct {
1863 u32 iflag;
1864 u32 fsflag;
1865} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001866 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001867 { F2FS_SYNC_FL, FS_SYNC_FL },
1868 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1869 { F2FS_APPEND_FL, FS_APPEND_FL },
1870 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1871 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001872 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001873 { F2FS_INDEX_FL, FS_INDEX_FL },
1874 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1875 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001876 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001877};
1878
1879#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001880 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001881 FS_SYNC_FL | \
1882 FS_IMMUTABLE_FL | \
1883 FS_APPEND_FL | \
1884 FS_NODUMP_FL | \
1885 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001886 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001887 FS_INDEX_FL | \
1888 FS_DIRSYNC_FL | \
1889 FS_PROJINHERIT_FL | \
1890 FS_ENCRYPT_FL | \
1891 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001892 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001893 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001894 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001895
1896#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001897 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001898 FS_SYNC_FL | \
1899 FS_IMMUTABLE_FL | \
1900 FS_APPEND_FL | \
1901 FS_NODUMP_FL | \
1902 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001903 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001904 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001905 FS_PROJINHERIT_FL | \
1906 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001907
1908/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1909static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1910{
1911 u32 fsflags = 0;
1912 int i;
1913
1914 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1915 if (iflags & f2fs_fsflags_map[i].iflag)
1916 fsflags |= f2fs_fsflags_map[i].fsflag;
1917
1918 return fsflags;
1919}
1920
1921/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1922static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1923{
1924 u32 iflags = 0;
1925 int i;
1926
1927 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1928 if (fsflags & f2fs_fsflags_map[i].fsflag)
1929 iflags |= f2fs_fsflags_map[i].iflag;
1930
1931 return iflags;
1932}
1933
1934static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1935{
1936 struct inode *inode = file_inode(filp);
1937 struct f2fs_inode_info *fi = F2FS_I(inode);
1938 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1939
1940 if (IS_ENCRYPTED(inode))
1941 fsflags |= FS_ENCRYPT_FL;
Eric Biggers95ae2512019-07-22 09:26:24 -07001942 if (IS_VERITY(inode))
1943 fsflags |= FS_VERITY_FL;
Eric Biggers36098552019-06-04 22:59:04 -07001944 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1945 fsflags |= FS_INLINE_DATA_FL;
1946 if (is_inode_flag_set(inode, FI_PIN_FILE))
1947 fsflags |= FS_NOCOW_FL;
1948
1949 fsflags &= F2FS_GETTABLE_FS_FL;
1950
1951 return put_user(fsflags, (int __user *)arg);
1952}
1953
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001954static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1955{
1956 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001957 struct f2fs_inode_info *fi = F2FS_I(inode);
1958 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001959 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001960 int ret;
1961
Chao Yu7fb17fe2016-05-09 19:56:32 +08001962 if (!inode_owner_or_capable(inode))
1963 return -EACCES;
1964
Eric Biggers36098552019-06-04 22:59:04 -07001965 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001966 return -EFAULT;
1967
Eric Biggers36098552019-06-04 22:59:04 -07001968 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1969 return -EOPNOTSUPP;
1970 fsflags &= F2FS_SETTABLE_FS_FL;
1971
1972 iflags = f2fs_fsflags_to_iflags(fsflags);
1973 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1974 return -EOPNOTSUPP;
1975
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001976 ret = mnt_want_write_file(filp);
1977 if (ret)
1978 return ret;
1979
Al Viro59551022016-01-22 15:40:57 -05001980 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001981
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001982 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1983 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1984 if (ret)
1985 goto out;
1986
Eric Biggers36098552019-06-04 22:59:04 -07001987 ret = f2fs_setflags_common(inode, iflags,
1988 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001989out:
Chao Yua72d4b92017-05-03 23:59:13 +08001990 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001991 mnt_drop_write_file(filp);
1992 return ret;
1993}
1994
Chao Yud49f3e82015-01-23 20:36:04 +08001995static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1996{
1997 struct inode *inode = file_inode(filp);
1998
1999 return put_user(inode->i_generation, (int __user *)arg);
2000}
2001
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002002static int f2fs_ioc_start_atomic_write(struct file *filp)
2003{
2004 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002005 struct f2fs_inode_info *fi = F2FS_I(inode);
2006 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08002007 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002008
2009 if (!inode_owner_or_capable(inode))
2010 return -EACCES;
2011
Jaegeuk Kime8118982017-03-17 10:04:15 +08002012 if (!S_ISREG(inode->i_mode))
2013 return -EINVAL;
2014
Chao Yu038d0692019-07-25 22:39:11 +08002015 if (filp->f_flags & O_DIRECT)
2016 return -EINVAL;
2017
Chao Yu7fb17fe2016-05-09 19:56:32 +08002018 ret = mnt_want_write_file(filp);
2019 if (ret)
2020 return ret;
2021
Chao Yu0fac5582016-05-09 19:56:33 +08002022 inode_lock(inode);
2023
Chao Yu4c8ff702019-11-01 18:07:14 +08002024 f2fs_disable_compressed_file(inode);
2025
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002026 if (f2fs_is_atomic_file(inode)) {
2027 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2028 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002029 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002030 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002031
Chao Yuf4c9c742015-07-17 18:06:35 +08002032 ret = f2fs_convert_inline_inode(inode);
2033 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002034 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002035
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002036 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2037
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002038 /*
2039 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2040 * f2fs_is_atomic_file.
2041 */
2042 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002043 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2044 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002045 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002046 if (ret) {
2047 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002048 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002049 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002050
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002051 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2052 if (list_empty(&fi->inmem_ilist))
2053 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302054 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002055 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2056
2057 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002058 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002059 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002060 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002061
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002062 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002063 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002064 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002065out:
Chao Yu0fac5582016-05-09 19:56:33 +08002066 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002067 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002068 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002069}
2070
2071static int f2fs_ioc_commit_atomic_write(struct file *filp)
2072{
2073 struct inode *inode = file_inode(filp);
2074 int ret;
2075
2076 if (!inode_owner_or_capable(inode))
2077 return -EACCES;
2078
2079 ret = mnt_want_write_file(filp);
2080 if (ret)
2081 return ret;
2082
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002083 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002084
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002085 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002086
Chao Yub169c3c2018-04-18 17:45:02 +08002087 if (f2fs_is_volatile_file(inode)) {
2088 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002089 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002090 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002091
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002092 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002093 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002094 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002095 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002096
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002097 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002098 if (!ret)
2099 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002100 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002101 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002102 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002103err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002104 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2105 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2106 ret = -EINVAL;
2107 }
Chao Yu0fac5582016-05-09 19:56:33 +08002108 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002109 mnt_drop_write_file(filp);
2110 return ret;
2111}
2112
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002113static int f2fs_ioc_start_volatile_write(struct file *filp)
2114{
2115 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002116 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002117
2118 if (!inode_owner_or_capable(inode))
2119 return -EACCES;
2120
Chao Yu8ff09712017-03-17 15:43:57 +08002121 if (!S_ISREG(inode->i_mode))
2122 return -EINVAL;
2123
Chao Yu7fb17fe2016-05-09 19:56:32 +08002124 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002125 if (ret)
2126 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002127
Chao Yu0fac5582016-05-09 19:56:33 +08002128 inode_lock(inode);
2129
Chao Yu7fb17fe2016-05-09 19:56:32 +08002130 if (f2fs_is_volatile_file(inode))
2131 goto out;
2132
2133 ret = f2fs_convert_inline_inode(inode);
2134 if (ret)
2135 goto out;
2136
Chao Yu648d50b2017-03-22 17:23:45 +08002137 stat_inc_volatile_write(inode);
2138 stat_update_max_volatile_write(inode);
2139
Jaegeuk Kim91942322016-05-20 10:13:22 -07002140 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002141 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002142out:
Chao Yu0fac5582016-05-09 19:56:33 +08002143 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002144 mnt_drop_write_file(filp);
2145 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002146}
2147
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002148static int f2fs_ioc_release_volatile_write(struct file *filp)
2149{
2150 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002151 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002152
2153 if (!inode_owner_or_capable(inode))
2154 return -EACCES;
2155
Chao Yu7fb17fe2016-05-09 19:56:32 +08002156 ret = mnt_want_write_file(filp);
2157 if (ret)
2158 return ret;
2159
Chao Yu0fac5582016-05-09 19:56:33 +08002160 inode_lock(inode);
2161
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002162 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002163 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002164
Chao Yu7fb17fe2016-05-09 19:56:32 +08002165 if (!f2fs_is_first_block_written(inode)) {
2166 ret = truncate_partial_data_page(inode, 0, true);
2167 goto out;
2168 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002169
Chao Yu7fb17fe2016-05-09 19:56:32 +08002170 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2171out:
Chao Yu0fac5582016-05-09 19:56:33 +08002172 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002173 mnt_drop_write_file(filp);
2174 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002175}
2176
2177static int f2fs_ioc_abort_volatile_write(struct file *filp)
2178{
2179 struct inode *inode = file_inode(filp);
2180 int ret;
2181
2182 if (!inode_owner_or_capable(inode))
2183 return -EACCES;
2184
2185 ret = mnt_want_write_file(filp);
2186 if (ret)
2187 return ret;
2188
Chao Yu0fac5582016-05-09 19:56:33 +08002189 inode_lock(inode);
2190
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002191 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002192 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002193 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002194 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002195 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002196 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002197 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002198
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002199 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2200
Chao Yu0fac5582016-05-09 19:56:33 +08002201 inode_unlock(inode);
2202
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002203 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002204 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002205 return ret;
2206}
2207
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002208static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2209{
2210 struct inode *inode = file_inode(filp);
2211 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2212 struct super_block *sb = sbi->sb;
2213 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002214 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002215
2216 if (!capable(CAP_SYS_ADMIN))
2217 return -EPERM;
2218
2219 if (get_user(in, (__u32 __user *)arg))
2220 return -EFAULT;
2221
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302222 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2223 ret = mnt_want_write_file(filp);
2224 if (ret)
2225 return ret;
2226 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002227
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002228 switch (in) {
2229 case F2FS_GOING_DOWN_FULLSYNC:
2230 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08002231 if (IS_ERR(sb)) {
2232 ret = PTR_ERR(sb);
2233 goto out;
2234 }
2235 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002236 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002237 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002238 thaw_bdev(sb->s_bdev, sb);
2239 }
2240 break;
2241 case F2FS_GOING_DOWN_METASYNC:
2242 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002243 ret = f2fs_sync_fs(sb, 1);
2244 if (ret)
2245 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002246 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002247 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002248 break;
2249 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002250 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002251 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002252 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002253 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002254 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002255 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002256 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002257 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002258 case F2FS_GOING_DOWN_NEED_FSCK:
2259 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002260 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2261 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002262 /* do checkpoint only */
2263 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002264 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002265 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002266 ret = -EINVAL;
2267 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002268 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002269
Chao Yu4d57b862018-05-30 00:20:41 +08002270 f2fs_stop_gc_thread(sbi);
2271 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002272
Chao Yu4d57b862018-05-30 00:20:41 +08002273 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002274 clear_opt(sbi, DISCARD);
2275
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002276 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002277out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302278 if (in != F2FS_GOING_DOWN_FULLSYNC)
2279 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002280
2281 trace_f2fs_shutdown(sbi, in, ret);
2282
Chao Yu7fb17fe2016-05-09 19:56:32 +08002283 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002284}
2285
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002286static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2287{
2288 struct inode *inode = file_inode(filp);
2289 struct super_block *sb = inode->i_sb;
2290 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2291 struct fstrim_range range;
2292 int ret;
2293
2294 if (!capable(CAP_SYS_ADMIN))
2295 return -EPERM;
2296
Chao Yu7d20c8a2018-09-04 03:52:17 +08002297 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002298 return -EOPNOTSUPP;
2299
2300 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2301 sizeof(range)))
2302 return -EFAULT;
2303
Chao Yu7fb17fe2016-05-09 19:56:32 +08002304 ret = mnt_want_write_file(filp);
2305 if (ret)
2306 return ret;
2307
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002308 range.minlen = max((unsigned int)range.minlen,
2309 q->limits.discard_granularity);
2310 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002311 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002312 if (ret < 0)
2313 return ret;
2314
2315 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2316 sizeof(range)))
2317 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002318 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002319 return 0;
2320}
2321
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002322static bool uuid_is_nonzero(__u8 u[16])
2323{
2324 int i;
2325
2326 for (i = 0; i < 16; i++)
2327 if (u[i])
2328 return true;
2329 return false;
2330}
2331
2332static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2333{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002334 struct inode *inode = file_inode(filp);
2335
Chao Yu7beb01f2018-10-24 18:34:26 +08002336 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002337 return -EOPNOTSUPP;
2338
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002339 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002340
Eric Biggersdb717d82016-11-26 19:07:49 -05002341 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002342}
2343
2344static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2345{
Chao Yu7beb01f2018-10-24 18:34:26 +08002346 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002347 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002348 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002349}
2350
2351static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2352{
2353 struct inode *inode = file_inode(filp);
2354 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2355 int err;
2356
Chao Yu7beb01f2018-10-24 18:34:26 +08002357 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002358 return -EOPNOTSUPP;
2359
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002360 err = mnt_want_write_file(filp);
2361 if (err)
2362 return err;
2363
Chao Yu846ae672018-02-26 22:04:13 +08002364 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002365
2366 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2367 goto got_it;
2368
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002369 /* update superblock with uuid */
2370 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2371
Chao Yuc5bda1c2015-06-08 13:28:03 +08002372 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002373 if (err) {
2374 /* undo new data */
2375 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002376 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002377 }
2378got_it:
2379 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2380 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002381 err = -EFAULT;
2382out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002383 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002384 mnt_drop_write_file(filp);
2385 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002386}
2387
Eric Biggers8ce589c2019-08-04 19:35:48 -07002388static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2389 unsigned long arg)
2390{
2391 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2392 return -EOPNOTSUPP;
2393
2394 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2395}
2396
2397static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2398{
2399 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2400 return -EOPNOTSUPP;
2401
2402 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2403}
2404
2405static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2406{
2407 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2408 return -EOPNOTSUPP;
2409
2410 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2411}
2412
2413static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2414 unsigned long arg)
2415{
2416 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2417 return -EOPNOTSUPP;
2418
2419 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2420}
2421
2422static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2423 unsigned long arg)
2424{
2425 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2426 return -EOPNOTSUPP;
2427
2428 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2429}
2430
Chao Yuc1c1b582015-07-10 18:08:10 +08002431static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2432{
2433 struct inode *inode = file_inode(filp);
2434 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002435 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002436 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002437
2438 if (!capable(CAP_SYS_ADMIN))
2439 return -EPERM;
2440
Chao Yud530d4d2015-10-05 22:22:44 +08002441 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002442 return -EFAULT;
2443
Chao Yud530d4d2015-10-05 22:22:44 +08002444 if (f2fs_readonly(sbi->sb))
2445 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002446
Chao Yu7fb17fe2016-05-09 19:56:32 +08002447 ret = mnt_want_write_file(filp);
2448 if (ret)
2449 return ret;
2450
Chao Yud530d4d2015-10-05 22:22:44 +08002451 if (!sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002452 if (!down_write_trylock(&sbi->gc_lock)) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002453 ret = -EBUSY;
2454 goto out;
2455 }
Chao Yud530d4d2015-10-05 22:22:44 +08002456 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002457 down_write(&sbi->gc_lock);
Chao Yuc1c1b582015-07-10 18:08:10 +08002458 }
2459
Jaegeuk Kime066b832017-04-13 15:17:00 -07002460 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002461out:
2462 mnt_drop_write_file(filp);
2463 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002464}
2465
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002466static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2467{
2468 struct inode *inode = file_inode(filp);
2469 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2470 struct f2fs_gc_range range;
2471 u64 end;
2472 int ret;
2473
2474 if (!capable(CAP_SYS_ADMIN))
2475 return -EPERM;
2476
2477 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2478 sizeof(range)))
2479 return -EFAULT;
2480
2481 if (f2fs_readonly(sbi->sb))
2482 return -EROFS;
2483
Yunlei Heb82f6e32018-04-24 11:40:30 +08002484 end = range.start + range.len;
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302485 if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
2486 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002487 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002488
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002489 ret = mnt_want_write_file(filp);
2490 if (ret)
2491 return ret;
2492
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002493do_more:
2494 if (!range.sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002495 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002496 ret = -EBUSY;
2497 goto out;
2498 }
2499 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002500 down_write(&sbi->gc_lock);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002501 }
2502
2503 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Yunlong Song67b0e422018-10-30 20:37:55 +08002504 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002505 if (range.start <= end)
2506 goto do_more;
2507out:
2508 mnt_drop_write_file(filp);
2509 return ret;
2510}
2511
Chao Yu059c0642018-07-17 20:41:49 +08002512static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002513{
2514 struct inode *inode = file_inode(filp);
2515 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002516 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002517
2518 if (!capable(CAP_SYS_ADMIN))
2519 return -EPERM;
2520
2521 if (f2fs_readonly(sbi->sb))
2522 return -EROFS;
2523
Daniel Rosenberg43549942018-08-20 19:21:43 -07002524 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002525 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002526 return -EINVAL;
2527 }
2528
Chao Yu7fb17fe2016-05-09 19:56:32 +08002529 ret = mnt_want_write_file(filp);
2530 if (ret)
2531 return ret;
2532
2533 ret = f2fs_sync_fs(sbi->sb, 1);
2534
2535 mnt_drop_write_file(filp);
2536 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002537}
2538
Chao Yud323d002015-10-27 09:53:45 +08002539static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2540 struct file *filp,
2541 struct f2fs_defragment *range)
2542{
2543 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002544 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002545 .m_seg_type = NO_CHECK_TYPE ,
2546 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002547 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002548 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002549 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002550 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002551 block_t blk_end = 0;
2552 bool fragmented = false;
2553 int err;
2554
2555 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002556 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002557 return -EINVAL;
2558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002559 pg_start = range->start >> PAGE_SHIFT;
2560 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002561
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002562 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002563
Al Viro59551022016-01-22 15:40:57 -05002564 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002565
2566 /* writeback all dirty pages in the range */
2567 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002568 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002569 if (err)
2570 goto out;
2571
2572 /*
2573 * lookup mapping info in extent cache, skip defragmenting if physical
2574 * block addresses are continuous.
2575 */
2576 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2577 if (ei.fofs + ei.len >= pg_end)
2578 goto out;
2579 }
2580
2581 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002582 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002583
2584 /*
2585 * lookup mapping info in dnode page cache, skip defragmenting if all
2586 * physical block addresses are continuous even if there are hole(s)
2587 * in logical blocks.
2588 */
2589 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002590 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002591 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002592 if (err)
2593 goto out;
2594
2595 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002596 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002597 continue;
2598 }
2599
Chao Yu25a912e2018-01-10 18:18:51 +08002600 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002601 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002602
2603 /* record total count of block that we're going to move */
2604 total += map.m_len;
2605
Chao Yud323d002015-10-27 09:53:45 +08002606 blk_end = map.m_pblk + map.m_len;
2607
2608 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002609 }
2610
Chao Yud3a1a0e2019-07-29 23:02:29 +08002611 if (!fragmented) {
2612 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002613 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002614 }
Chao Yud323d002015-10-27 09:53:45 +08002615
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002616 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002617
2618 /*
2619 * make sure there are enough free section for LFS allocation, this can
2620 * avoid defragment running in SSR mode when free section are allocated
2621 * intensively
2622 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002623 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002624 err = -EAGAIN;
2625 goto out;
2626 }
2627
Chao Yu25a912e2018-01-10 18:18:51 +08002628 map.m_lblk = pg_start;
2629 map.m_len = pg_end - pg_start;
2630 total = 0;
2631
Chao Yud323d002015-10-27 09:53:45 +08002632 while (map.m_lblk < pg_end) {
2633 pgoff_t idx;
2634 int cnt = 0;
2635
2636do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002637 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002638 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002639 if (err)
2640 goto clear_out;
2641
2642 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002643 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002644 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002645 }
2646
Jaegeuk Kim91942322016-05-20 10:13:22 -07002647 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002648
2649 idx = map.m_lblk;
2650 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2651 struct page *page;
2652
Chao Yu4d57b862018-05-30 00:20:41 +08002653 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002654 if (IS_ERR(page)) {
2655 err = PTR_ERR(page);
2656 goto clear_out;
2657 }
2658
2659 set_page_dirty(page);
2660 f2fs_put_page(page, 1);
2661
2662 idx++;
2663 cnt++;
2664 total++;
2665 }
2666
2667 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002668check:
2669 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002670 goto do_map;
2671
Jaegeuk Kim91942322016-05-20 10:13:22 -07002672 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002673
2674 err = filemap_fdatawrite(inode->i_mapping);
2675 if (err)
2676 goto out;
2677 }
2678clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002679 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002680out:
Al Viro59551022016-01-22 15:40:57 -05002681 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002682 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002683 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002684 return err;
2685}
2686
2687static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2688{
2689 struct inode *inode = file_inode(filp);
2690 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2691 struct f2fs_defragment range;
2692 int err;
2693
2694 if (!capable(CAP_SYS_ADMIN))
2695 return -EPERM;
2696
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002697 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002698 return -EINVAL;
2699
Kinglong Meed7563862017-03-10 17:55:07 +08002700 if (f2fs_readonly(sbi->sb))
2701 return -EROFS;
2702
2703 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2704 sizeof(range)))
2705 return -EFAULT;
2706
2707 /* verify alignment of offset & size */
2708 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2709 return -EINVAL;
2710
2711 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2712 sbi->max_file_blocks))
2713 return -EINVAL;
2714
Chao Yud323d002015-10-27 09:53:45 +08002715 err = mnt_want_write_file(filp);
2716 if (err)
2717 return err;
2718
Chao Yud323d002015-10-27 09:53:45 +08002719 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002720 mnt_drop_write_file(filp);
2721
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002722 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002723 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002724 return err;
Chao Yud323d002015-10-27 09:53:45 +08002725
2726 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2727 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002728 return -EFAULT;
2729
2730 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002731}
2732
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002733static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2734 struct file *file_out, loff_t pos_out, size_t len)
2735{
2736 struct inode *src = file_inode(file_in);
2737 struct inode *dst = file_inode(file_out);
2738 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2739 size_t olen = len, dst_max_i_size = 0;
2740 size_t dst_osize;
2741 int ret;
2742
2743 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2744 src->i_sb != dst->i_sb)
2745 return -EXDEV;
2746
2747 if (unlikely(f2fs_readonly(src->i_sb)))
2748 return -EROFS;
2749
Chao Yufe8494b2016-08-04 20:13:02 +08002750 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2751 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002752
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302753 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002754 return -EOPNOTSUPP;
2755
Fan Lid95fd912016-09-13 11:35:42 +08002756 if (src == dst) {
2757 if (pos_in == pos_out)
2758 return 0;
2759 if (pos_out > pos_in && pos_out < pos_in + len)
2760 return -EINVAL;
2761 }
2762
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002763 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002764 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002765 ret = -EBUSY;
2766 if (!inode_trylock(dst))
2767 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002768 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002769
2770 ret = -EINVAL;
2771 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2772 goto out_unlock;
2773 if (len == 0)
2774 olen = len = src->i_size - pos_in;
2775 if (pos_in + len == src->i_size)
2776 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2777 if (len == 0) {
2778 ret = 0;
2779 goto out_unlock;
2780 }
2781
2782 dst_osize = dst->i_size;
2783 if (pos_out + olen > dst->i_size)
2784 dst_max_i_size = pos_out + olen;
2785
2786 /* verify the end result is block aligned */
2787 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2788 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2789 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2790 goto out_unlock;
2791
2792 ret = f2fs_convert_inline_inode(src);
2793 if (ret)
2794 goto out_unlock;
2795
2796 ret = f2fs_convert_inline_inode(dst);
2797 if (ret)
2798 goto out_unlock;
2799
2800 /* write out all dirty pages from offset */
2801 ret = filemap_write_and_wait_range(src->i_mapping,
2802 pos_in, pos_in + len);
2803 if (ret)
2804 goto out_unlock;
2805
2806 ret = filemap_write_and_wait_range(dst->i_mapping,
2807 pos_out, pos_out + len);
2808 if (ret)
2809 goto out_unlock;
2810
2811 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002812
2813 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2814 if (src != dst) {
2815 ret = -EBUSY;
2816 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2817 goto out_src;
2818 }
2819
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002820 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002821 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2822 pos_out >> F2FS_BLKSIZE_BITS,
2823 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002824
2825 if (!ret) {
2826 if (dst_max_i_size)
2827 f2fs_i_size_write(dst, dst_max_i_size);
2828 else if (dst_osize != dst->i_size)
2829 f2fs_i_size_write(dst, dst_osize);
2830 }
2831 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002832
2833 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002834 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002835out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002836 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002837out_unlock:
2838 if (src != dst)
2839 inode_unlock(dst);
2840out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002841 inode_unlock(src);
2842 return ret;
2843}
2844
2845static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2846{
2847 struct f2fs_move_range range;
2848 struct fd dst;
2849 int err;
2850
2851 if (!(filp->f_mode & FMODE_READ) ||
2852 !(filp->f_mode & FMODE_WRITE))
2853 return -EBADF;
2854
2855 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2856 sizeof(range)))
2857 return -EFAULT;
2858
2859 dst = fdget(range.dst_fd);
2860 if (!dst.file)
2861 return -EBADF;
2862
2863 if (!(dst.file->f_mode & FMODE_WRITE)) {
2864 err = -EBADF;
2865 goto err_out;
2866 }
2867
2868 err = mnt_want_write_file(filp);
2869 if (err)
2870 goto err_out;
2871
2872 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2873 range.pos_out, range.len);
2874
2875 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002876 if (err)
2877 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002878
2879 if (copy_to_user((struct f2fs_move_range __user *)arg,
2880 &range, sizeof(range)))
2881 err = -EFAULT;
2882err_out:
2883 fdput(dst);
2884 return err;
2885}
2886
Jaegeuk Kime066b832017-04-13 15:17:00 -07002887static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2888{
2889 struct inode *inode = file_inode(filp);
2890 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2891 struct sit_info *sm = SIT_I(sbi);
2892 unsigned int start_segno = 0, end_segno = 0;
2893 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2894 struct f2fs_flush_device range;
2895 int ret;
2896
2897 if (!capable(CAP_SYS_ADMIN))
2898 return -EPERM;
2899
2900 if (f2fs_readonly(sbi->sb))
2901 return -EROFS;
2902
Daniel Rosenberg43549942018-08-20 19:21:43 -07002903 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2904 return -EINVAL;
2905
Jaegeuk Kime066b832017-04-13 15:17:00 -07002906 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2907 sizeof(range)))
2908 return -EFAULT;
2909
Damien Le Moal09168782019-03-16 09:13:06 +09002910 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002911 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002912 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2913 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002914 return -EINVAL;
2915 }
2916
2917 ret = mnt_want_write_file(filp);
2918 if (ret)
2919 return ret;
2920
2921 if (range.dev_num != 0)
2922 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2923 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2924
2925 start_segno = sm->last_victim[FLUSH_DEVICE];
2926 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2927 start_segno = dev_start_segno;
2928 end_segno = min(start_segno + range.segments, dev_end_segno);
2929
2930 while (start_segno < end_segno) {
Chao Yufb24fea2020-01-14 19:36:50 +08002931 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002932 ret = -EBUSY;
2933 goto out;
2934 }
2935 sm->last_victim[GC_CB] = end_segno + 1;
2936 sm->last_victim[GC_GREEDY] = end_segno + 1;
2937 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2938 ret = f2fs_gc(sbi, true, true, start_segno);
2939 if (ret == -EAGAIN)
2940 ret = 0;
2941 else if (ret < 0)
2942 break;
2943 start_segno++;
2944 }
2945out:
2946 mnt_drop_write_file(filp);
2947 return ret;
2948}
2949
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002950static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2951{
2952 struct inode *inode = file_inode(filp);
2953 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2954
2955 /* Must validate to set it with SQLite behavior in Android. */
2956 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2957
2958 return put_user(sb_feature, (u32 __user *)arg);
2959}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002960
Chao Yu2c1d0302017-07-29 00:32:52 +08002961#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002962int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2963{
2964 struct dquot *transfer_to[MAXQUOTAS] = {};
2965 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2966 struct super_block *sb = sbi->sb;
2967 int err = 0;
2968
2969 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2970 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2971 err = __dquot_transfer(inode, transfer_to);
2972 if (err)
2973 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2974 dqput(transfer_to[PRJQUOTA]);
2975 }
2976 return err;
2977}
2978
Chao Yu2c1d0302017-07-29 00:32:52 +08002979static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2980{
2981 struct inode *inode = file_inode(filp);
2982 struct f2fs_inode_info *fi = F2FS_I(inode);
2983 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002984 struct page *ipage;
2985 kprojid_t kprojid;
2986 int err;
2987
Chao Yu7beb01f2018-10-24 18:34:26 +08002988 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08002989 if (projid != F2FS_DEF_PROJID)
2990 return -EOPNOTSUPP;
2991 else
2992 return 0;
2993 }
2994
2995 if (!f2fs_has_extra_attr(inode))
2996 return -EOPNOTSUPP;
2997
2998 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2999
3000 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3001 return 0;
3002
Chao Yu2c1d0302017-07-29 00:32:52 +08003003 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003004 /* Is it quota file? Do not allow user to mess with it */
3005 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003006 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003007
Chao Yu4d57b862018-05-30 00:20:41 +08003008 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003009 if (IS_ERR(ipage))
3010 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003011
3012 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3013 i_projid)) {
3014 err = -EOVERFLOW;
3015 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003016 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003017 }
3018 f2fs_put_page(ipage, 1);
3019
Chao Yuc22aecd2018-04-21 17:53:52 +08003020 err = dquot_initialize(inode);
3021 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003022 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003023
Chao Yu78130812018-09-25 15:36:02 +08003024 f2fs_lock_op(sbi);
3025 err = f2fs_transfer_project_quota(inode, kprojid);
3026 if (err)
3027 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003028
3029 F2FS_I(inode)->i_projid = kprojid;
3030 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003031 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003032out_unlock:
3033 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003034 return err;
3035}
3036#else
Chao Yu78130812018-09-25 15:36:02 +08003037int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3038{
3039 return 0;
3040}
3041
Chao Yu2c1d0302017-07-29 00:32:52 +08003042static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3043{
3044 if (projid != F2FS_DEF_PROJID)
3045 return -EOPNOTSUPP;
3046 return 0;
3047}
3048#endif
3049
Eric Biggers36098552019-06-04 22:59:04 -07003050/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08003051
Eric Biggers36098552019-06-04 22:59:04 -07003052/*
3053 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3054 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3055 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3056 */
3057
3058static const struct {
3059 u32 iflag;
3060 u32 xflag;
3061} f2fs_xflags_map[] = {
3062 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3063 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3064 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3065 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3066 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3067 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3068};
3069
3070#define F2FS_SUPPORTED_XFLAGS ( \
3071 FS_XFLAG_SYNC | \
3072 FS_XFLAG_IMMUTABLE | \
3073 FS_XFLAG_APPEND | \
3074 FS_XFLAG_NODUMP | \
3075 FS_XFLAG_NOATIME | \
3076 FS_XFLAG_PROJINHERIT)
3077
3078/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3079static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3080{
3081 u32 xflags = 0;
3082 int i;
3083
3084 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3085 if (iflags & f2fs_xflags_map[i].iflag)
3086 xflags |= f2fs_xflags_map[i].xflag;
3087
Chao Yu2c1d0302017-07-29 00:32:52 +08003088 return xflags;
3089}
3090
Eric Biggers36098552019-06-04 22:59:04 -07003091/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3092static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003093{
Eric Biggers36098552019-06-04 22:59:04 -07003094 u32 iflags = 0;
3095 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08003096
Eric Biggers36098552019-06-04 22:59:04 -07003097 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3098 if (xflags & f2fs_xflags_map[i].xflag)
3099 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08003100
3101 return iflags;
3102}
3103
Eric Biggers6fc93c42019-07-01 13:26:29 -07003104static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3105{
3106 struct f2fs_inode_info *fi = F2FS_I(inode);
3107
3108 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3109
3110 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3111 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3112}
3113
Chao Yu2c1d0302017-07-29 00:32:52 +08003114static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3115{
3116 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08003117 struct fsxattr fa;
3118
Eric Biggers6fc93c42019-07-01 13:26:29 -07003119 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08003120
3121 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3122 return -EFAULT;
3123 return 0;
3124}
3125
3126static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3127{
3128 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003129 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07003130 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003131 int err;
3132
3133 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3134 return -EFAULT;
3135
3136 /* Make sure caller has proper permission */
3137 if (!inode_owner_or_capable(inode))
3138 return -EACCES;
3139
Eric Biggers36098552019-06-04 22:59:04 -07003140 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08003141 return -EOPNOTSUPP;
3142
Eric Biggers36098552019-06-04 22:59:04 -07003143 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3144 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003145 return -EOPNOTSUPP;
3146
3147 err = mnt_want_write_file(filp);
3148 if (err)
3149 return err;
3150
3151 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003152
3153 f2fs_fill_fsxattr(inode, &old_fa);
3154 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09003155 if (err)
3156 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07003157
Eric Biggers36098552019-06-04 22:59:04 -07003158 err = f2fs_setflags_common(inode, iflags,
3159 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08003160 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003161 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08003162
3163 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09003164out:
3165 inode_unlock(inode);
3166 mnt_drop_write_file(filp);
3167 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003168}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003169
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003170int f2fs_pin_file_control(struct inode *inode, bool inc)
3171{
3172 struct f2fs_inode_info *fi = F2FS_I(inode);
3173 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3174
3175 /* Use i_gc_failures for normal file as a risk signal. */
3176 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003177 f2fs_i_gc_failures_write(inode,
3178 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003179
Chao Yu2ef79ec2018-05-07 20:28:54 +08003180 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003181 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3182 __func__, inode->i_ino,
3183 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003184 clear_inode_flag(inode, FI_PIN_FILE);
3185 return -EAGAIN;
3186 }
3187 return 0;
3188}
3189
3190static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3191{
3192 struct inode *inode = file_inode(filp);
3193 __u32 pin;
3194 int ret = 0;
3195
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003196 if (get_user(pin, (__u32 __user *)arg))
3197 return -EFAULT;
3198
3199 if (!S_ISREG(inode->i_mode))
3200 return -EINVAL;
3201
3202 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3203 return -EROFS;
3204
3205 ret = mnt_want_write_file(filp);
3206 if (ret)
3207 return ret;
3208
3209 inode_lock(inode);
3210
Chao Yu4d57b862018-05-30 00:20:41 +08003211 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08003212 ret = -EINVAL;
3213 goto out;
3214 }
3215
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003216 if (!pin) {
3217 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003218 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003219 goto done;
3220 }
3221
3222 if (f2fs_pin_file_control(inode, false)) {
3223 ret = -EAGAIN;
3224 goto out;
3225 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003226
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003227 ret = f2fs_convert_inline_inode(inode);
3228 if (ret)
3229 goto out;
3230
Chao Yu4c8ff702019-11-01 18:07:14 +08003231 if (f2fs_disable_compressed_file(inode)) {
3232 ret = -EOPNOTSUPP;
3233 goto out;
3234 }
3235
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003236 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003237 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003238done:
3239 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3240out:
3241 inode_unlock(inode);
3242 mnt_drop_write_file(filp);
3243 return ret;
3244}
3245
3246static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3247{
3248 struct inode *inode = file_inode(filp);
3249 __u32 pin = 0;
3250
3251 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003252 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003253 return put_user(pin, (u32 __user *)arg);
3254}
3255
Chao Yuc4020b22018-01-11 14:42:30 +08003256int f2fs_precache_extents(struct inode *inode)
3257{
3258 struct f2fs_inode_info *fi = F2FS_I(inode);
3259 struct f2fs_map_blocks map;
3260 pgoff_t m_next_extent;
3261 loff_t end;
3262 int err;
3263
3264 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3265 return -EOPNOTSUPP;
3266
3267 map.m_lblk = 0;
3268 map.m_next_pgofs = NULL;
3269 map.m_next_extent = &m_next_extent;
3270 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003271 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003272 end = F2FS_I_SB(inode)->max_file_blocks;
3273
3274 while (map.m_lblk < end) {
3275 map.m_len = end - map.m_lblk;
3276
Chao Yub2532c62018-04-24 10:55:28 +08003277 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003278 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003279 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003280 if (err)
3281 return err;
3282
3283 map.m_lblk = m_next_extent;
3284 }
3285
3286 return err;
3287}
3288
3289static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3290{
3291 return f2fs_precache_extents(file_inode(filp));
3292}
3293
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003294static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3295{
3296 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3297 __u64 block_count;
3298 int ret;
3299
3300 if (!capable(CAP_SYS_ADMIN))
3301 return -EPERM;
3302
3303 if (f2fs_readonly(sbi->sb))
3304 return -EROFS;
3305
3306 if (copy_from_user(&block_count, (void __user *)arg,
3307 sizeof(block_count)))
3308 return -EFAULT;
3309
3310 ret = f2fs_resize_fs(sbi, block_count);
3311
3312 return ret;
3313}
3314
Eric Biggers95ae2512019-07-22 09:26:24 -07003315static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3316{
3317 struct inode *inode = file_inode(filp);
3318
3319 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3320
3321 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3322 f2fs_warn(F2FS_I_SB(inode),
3323 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3324 inode->i_ino);
3325 return -EOPNOTSUPP;
3326 }
3327
3328 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3329}
3330
3331static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3332{
3333 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3334 return -EOPNOTSUPP;
3335
3336 return fsverity_ioctl_measure(filp, (void __user *)arg);
3337}
3338
Chao Yu4507847c2019-07-17 17:06:11 +08003339static int f2fs_get_volume_name(struct file *filp, unsigned long arg)
3340{
3341 struct inode *inode = file_inode(filp);
3342 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3343 char *vbuf;
3344 int count;
3345 int err = 0;
3346
3347 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3348 if (!vbuf)
3349 return -ENOMEM;
3350
3351 down_read(&sbi->sb_lock);
3352 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3353 ARRAY_SIZE(sbi->raw_super->volume_name),
3354 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3355 up_read(&sbi->sb_lock);
3356
3357 if (copy_to_user((char __user *)arg, vbuf,
3358 min(FSLABEL_MAX, count)))
3359 err = -EFAULT;
3360
3361 kvfree(vbuf);
3362 return err;
3363}
3364
3365static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
3366{
3367 struct inode *inode = file_inode(filp);
3368 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3369 char *vbuf;
3370 int err = 0;
3371
3372 if (!capable(CAP_SYS_ADMIN))
3373 return -EPERM;
3374
3375 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3376 if (IS_ERR(vbuf))
3377 return PTR_ERR(vbuf);
3378
3379 err = mnt_want_write_file(filp);
3380 if (err)
3381 goto out;
3382
3383 down_write(&sbi->sb_lock);
3384
3385 memset(sbi->raw_super->volume_name, 0,
3386 sizeof(sbi->raw_super->volume_name));
3387 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3388 sbi->raw_super->volume_name,
3389 ARRAY_SIZE(sbi->raw_super->volume_name));
3390
3391 err = f2fs_commit_super(sbi, false);
3392
3393 up_write(&sbi->sb_lock);
3394
3395 mnt_drop_write_file(filp);
3396out:
3397 kfree(vbuf);
3398 return err;
3399}
3400
Chao Yu439dfb12020-02-21 18:09:21 +08003401static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3402{
3403 struct inode *inode = file_inode(filp);
3404 __u64 blocks;
3405
3406 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3407 return -EOPNOTSUPP;
3408
3409 if (!f2fs_compressed_file(inode))
3410 return -EINVAL;
3411
3412 blocks = F2FS_I(inode)->i_compr_blocks;
3413 return put_user(blocks, (u64 __user *)arg);
3414}
3415
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003416long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3417{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003418 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3419 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08003420 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
3421 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003422
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003423 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003424 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003425 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003426 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003427 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08003428 case F2FS_IOC_GETVERSION:
3429 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003430 case F2FS_IOC_START_ATOMIC_WRITE:
3431 return f2fs_ioc_start_atomic_write(filp);
3432 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3433 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07003434 case F2FS_IOC_START_VOLATILE_WRITE:
3435 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08003436 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3437 return f2fs_ioc_release_volatile_write(filp);
3438 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3439 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08003440 case F2FS_IOC_SHUTDOWN:
3441 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003442 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003443 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003444 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3445 return f2fs_ioc_set_encryption_policy(filp, arg);
3446 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3447 return f2fs_ioc_get_encryption_policy(filp, arg);
3448 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3449 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07003450 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3451 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3452 case FS_IOC_ADD_ENCRYPTION_KEY:
3453 return f2fs_ioc_add_encryption_key(filp, arg);
3454 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3455 return f2fs_ioc_remove_encryption_key(filp, arg);
3456 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3457 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
3458 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3459 return f2fs_ioc_get_encryption_key_status(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003460 case F2FS_IOC_GARBAGE_COLLECT:
3461 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003462 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3463 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08003464 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08003465 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08003466 case F2FS_IOC_DEFRAGMENT:
3467 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003468 case F2FS_IOC_MOVE_RANGE:
3469 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07003470 case F2FS_IOC_FLUSH_DEVICE:
3471 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003472 case F2FS_IOC_GET_FEATURES:
3473 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08003474 case F2FS_IOC_FSGETXATTR:
3475 return f2fs_ioc_fsgetxattr(filp, arg);
3476 case F2FS_IOC_FSSETXATTR:
3477 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003478 case F2FS_IOC_GET_PIN_FILE:
3479 return f2fs_ioc_get_pin_file(filp, arg);
3480 case F2FS_IOC_SET_PIN_FILE:
3481 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08003482 case F2FS_IOC_PRECACHE_EXTENTS:
3483 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003484 case F2FS_IOC_RESIZE_FS:
3485 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07003486 case FS_IOC_ENABLE_VERITY:
3487 return f2fs_ioc_enable_verity(filp, arg);
3488 case FS_IOC_MEASURE_VERITY:
3489 return f2fs_ioc_measure_verity(filp, arg);
Chao Yu4507847c2019-07-17 17:06:11 +08003490 case F2FS_IOC_GET_VOLUME_NAME:
3491 return f2fs_get_volume_name(filp, arg);
3492 case F2FS_IOC_SET_VOLUME_NAME:
3493 return f2fs_set_volume_name(filp, arg);
Chao Yu439dfb12020-02-21 18:09:21 +08003494 case F2FS_IOC_GET_COMPRESS_BLOCKS:
3495 return f2fs_get_compress_blocks(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003496 default:
3497 return -ENOTTY;
3498 }
3499}
3500
Chao Yu4c8ff702019-11-01 18:07:14 +08003501static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
3502{
3503 struct file *file = iocb->ki_filp;
3504 struct inode *inode = file_inode(file);
3505
3506 if (!f2fs_is_compress_backend_ready(inode))
3507 return -EOPNOTSUPP;
3508
3509 return generic_file_read_iter(iocb, iter);
3510}
3511
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003512static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3513{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003514 struct file *file = iocb->ki_filp;
3515 struct inode *inode = file_inode(file);
3516 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003517
Chao Yu126ce722019-04-02 18:52:22 +08003518 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
3519 ret = -EIO;
3520 goto out;
3521 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003522
Chao Yu7bd29352020-02-24 19:20:15 +08003523 if (!f2fs_is_compress_backend_ready(inode)) {
3524 ret = -EOPNOTSUPP;
3525 goto out;
3526 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003527
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003528 if (iocb->ki_flags & IOCB_NOWAIT) {
3529 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08003530 ret = -EAGAIN;
3531 goto out;
3532 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003533 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003534 inode_lock(inode);
3535 }
3536
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003537 ret = generic_write_checks(iocb, from);
3538 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003539 bool preallocated = false;
3540 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003541 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003542
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003543 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3544 set_inode_flag(inode, FI_NO_PREALLOC);
3545
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003546 if ((iocb->ki_flags & IOCB_NOWAIT)) {
3547 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003548 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003549 f2fs_has_inline_data(inode) ||
3550 f2fs_force_buffered_io(inode, iocb, from)) {
3551 clear_inode_flag(inode, FI_NO_PREALLOC);
3552 inode_unlock(inode);
3553 ret = -EAGAIN;
3554 goto out;
3555 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003556 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003557 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003558
3559 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
3560 goto write;
3561
3562 if (iocb->ki_flags & IOCB_DIRECT) {
3563 /*
3564 * Convert inline data for Direct I/O before entering
3565 * f2fs_direct_IO().
3566 */
3567 err = f2fs_convert_inline_inode(inode);
3568 if (err)
3569 goto out_err;
3570 /*
3571 * If force_buffere_io() is true, we have to allocate
3572 * blocks all the time, since f2fs_direct_IO will fall
3573 * back to buffered IO.
3574 */
3575 if (!f2fs_force_buffered_io(inode, iocb, from) &&
3576 allow_outplace_dio(inode, iocb, from))
3577 goto write;
3578 }
3579 preallocated = true;
3580 target_size = iocb->ki_pos + iov_iter_count(from);
3581
3582 err = f2fs_preallocate_blocks(iocb, from);
3583 if (err) {
3584out_err:
3585 clear_inode_flag(inode, FI_NO_PREALLOC);
3586 inode_unlock(inode);
3587 ret = err;
3588 goto out;
3589 }
3590write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003591 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003592 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003593
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003594 /* if we couldn't write data, we should deallocate blocks. */
3595 if (preallocated && i_size_read(inode) < target_size)
3596 f2fs_truncate(inode);
3597
Chao Yub0af6d42017-08-02 23:21:48 +08003598 if (ret > 0)
3599 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003600 }
3601 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08003602out:
3603 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
3604 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07003605 if (ret > 0)
3606 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003607 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003608}
3609
Namjae Jeone9750822013-02-04 23:41:41 +09003610#ifdef CONFIG_COMPAT
3611long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3612{
3613 switch (cmd) {
3614 case F2FS_IOC32_GETFLAGS:
3615 cmd = F2FS_IOC_GETFLAGS;
3616 break;
3617 case F2FS_IOC32_SETFLAGS:
3618 cmd = F2FS_IOC_SETFLAGS;
3619 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003620 case F2FS_IOC32_GETVERSION:
3621 cmd = F2FS_IOC_GETVERSION;
3622 break;
3623 case F2FS_IOC_START_ATOMIC_WRITE:
3624 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3625 case F2FS_IOC_START_VOLATILE_WRITE:
3626 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3627 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3628 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02003629 case FITRIM:
Chao Yu04ef4b62015-11-10 18:44:20 +08003630 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3631 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3632 case F2FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07003633 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3634 case FS_IOC_ADD_ENCRYPTION_KEY:
3635 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3636 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3637 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Chao Yu04ef4b62015-11-10 18:44:20 +08003638 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003639 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003640 case F2FS_IOC_WRITE_CHECKPOINT:
3641 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003642 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003643 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003644 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003645 case F2FS_IOC_FSGETXATTR:
3646 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003647 case F2FS_IOC_GET_PIN_FILE:
3648 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003649 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003650 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07003651 case FS_IOC_ENABLE_VERITY:
3652 case FS_IOC_MEASURE_VERITY:
Chao Yu4507847c2019-07-17 17:06:11 +08003653 case F2FS_IOC_GET_VOLUME_NAME:
3654 case F2FS_IOC_SET_VOLUME_NAME:
Chao Yu439dfb12020-02-21 18:09:21 +08003655 case F2FS_IOC_GET_COMPRESS_BLOCKS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003656 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003657 default:
3658 return -ENOIOCTLCMD;
3659 }
3660 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3661}
3662#endif
3663
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003664const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003665 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08003666 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003667 .write_iter = f2fs_file_write_iter,
3668 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003669 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003670 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003671 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003672 .fsync = f2fs_sync_file,
3673 .fallocate = f2fs_fallocate,
3674 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003675#ifdef CONFIG_COMPAT
3676 .compat_ioctl = f2fs_compat_ioctl,
3677#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003678 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003679 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003680};