blob: 81bfc5b44fa136f4151633bdaf2b01c506ec39b3 [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 Yu8b83ac82020-04-16 18:16:56 +080043 if (!ret)
44 f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
45 F2FS_BLKSIZE);
46
Chao Yud7648342019-04-15 15:22:19 +080047 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
48
Souptick Joarderea4d4792018-04-15 01:40:02 +053049 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080050}
51
Souptick Joarderea4d4792018-04-15 01:40:02 +053052static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090053{
54 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080055 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070056 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080057 struct dnode_of_data dn;
Chao Yu4c8ff702019-11-01 18:07:14 +080058 bool need_alloc = true;
59 int err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090060
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020061 if (unlikely(f2fs_cp_error(sbi))) {
62 err = -EIO;
63 goto err;
64 }
65
Chao Yu00e09c02019-08-23 17:58:36 +080066 if (!f2fs_is_checkpoint_ready(sbi)) {
67 err = -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +080068 goto err;
Chao Yu00e09c02019-08-23 17:58:36 +080069 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090070
Chao Yu4c8ff702019-11-01 18:07:14 +080071#ifdef CONFIG_F2FS_FS_COMPRESSION
72 if (f2fs_compressed_file(inode)) {
73 int ret = f2fs_is_compressed_cluster(inode, page->index);
74
75 if (ret < 0) {
76 err = ret;
77 goto err;
78 } else if (ret) {
79 if (ret < F2FS_I(inode)->i_cluster_size) {
80 err = -EAGAIN;
81 goto err;
82 }
83 need_alloc = false;
84 }
85 }
86#endif
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080087 /* should do out of any locked page */
Chao Yu4c8ff702019-11-01 18:07:14 +080088 if (need_alloc)
89 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080090
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090091 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070092
93 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070094
Dave Jiang11bac802017-02-24 14:56:41 -080095 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080096 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090097 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090098 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090099 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900100 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900101 unlock_page(page);
102 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800103 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900104 }
105
Chao Yu4c8ff702019-11-01 18:07:14 +0800106 if (need_alloc) {
107 /* block allocation */
108 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
109 set_new_dnode(&dn, inode, NULL, NULL, 0);
110 err = f2fs_get_block(&dn, page->index);
111 f2fs_put_dnode(&dn);
112 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu39a86952018-09-27 18:33:18 +0800113 }
114
Chao Yu06c75402020-03-02 17:34:27 +0800115#ifdef CONFIG_F2FS_FS_COMPRESSION
116 if (!need_alloc) {
117 set_new_dnode(&dn, inode, NULL, NULL, 0);
118 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
119 f2fs_put_dnode(&dn);
120 }
121#endif
122 if (err) {
123 unlock_page(page);
124 goto out_sem;
Chao Yu39a86952018-09-27 18:33:18 +0800125 }
126
Chao Yubae0ee72018-12-25 17:43:42 +0800127 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +0800128
129 /* wait for GCed page writeback via META_MAPPING */
130 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
131
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900132 /*
133 * check to see if the page is mapped already (no holes)
134 */
135 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800136 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900137
138 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300139 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800140 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900141 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900142
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300143 offset = i_size_read(inode) & ~PAGE_MASK;
144 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900145 }
146 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700147 if (!PageUptodate(page))
148 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900149
Chao Yub0af6d42017-08-02 23:21:48 +0800150 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530151 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800152
Jaegeuk Kime943a102013-10-25 14:26:31 +0900153 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800154out_sem:
155 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800156
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900157 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200158err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900159 return block_page_mkwrite_return(err);
160}
161
162static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800163 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700164 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900165 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900166};
167
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900168static int get_parent_ino(struct inode *inode, nid_t *pino)
169{
170 struct dentry *dentry;
171
Eric Biggers84c9c2d2020-05-05 11:41:11 -0700172 /*
173 * Make sure to get the non-deleted alias. The alias associated with
174 * the open file descriptor being fsync()'ed may be deleted already.
175 */
176 dentry = d_find_alias(inode);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900177 if (!dentry)
178 return 0;
179
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900180 *pino = parent_ino(dentry);
181 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900182 return 1;
183}
184
Chao Yua5fd5052017-11-06 22:51:45 +0800185static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800186{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700187 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800188 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800189
Chao Yua5fd5052017-11-06 22:51:45 +0800190 if (!S_ISREG(inode->i_mode))
191 cp_reason = CP_NON_REGULAR;
Chao Yu4c8ff702019-11-01 18:07:14 +0800192 else if (f2fs_compressed_file(inode))
193 cp_reason = CP_COMPRESSED;
Chao Yua5fd5052017-11-06 22:51:45 +0800194 else if (inode->i_nlink != 1)
195 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700196 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800197 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800198 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800199 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800200 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800201 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800202 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800203 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700204 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800205 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800206 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800207 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800208 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800209 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
210 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
211 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800212 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800213
Chao Yua5fd5052017-11-06 22:51:45 +0800214 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800215}
216
Changman Lee9c7bb702014-12-08 15:29:40 +0900217static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
218{
219 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
220 bool ret = false;
221 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800222 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900223 ret = true;
224 f2fs_put_page(i, 0);
225 return ret;
226}
227
Changman Lee51455b12014-12-08 15:29:41 +0900228static void try_to_fix_pino(struct inode *inode)
229{
230 struct f2fs_inode_info *fi = F2FS_I(inode);
231 nid_t pino;
232
233 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900234 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
235 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700236 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900237 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900238 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700239 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900240}
241
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700242static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
243 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900244{
245 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700246 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700247 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900248 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800249 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900250 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900251 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900252 .nr_to_write = LONG_MAX,
253 .for_reclaim = 0,
254 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800255 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900256
Daniel Rosenberg43549942018-08-20 19:21:43 -0700257 if (unlikely(f2fs_readonly(inode->i_sb) ||
258 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900259 return 0;
260
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900261 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700262
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800263 if (S_ISDIR(inode->i_mode))
264 goto go_write;
265
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700266 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800267 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700268 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400269 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700270 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700271
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900272 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800273 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900274 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900275 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900276
Changman Lee9c7bb702014-12-08 15:29:40 +0900277 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800278 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700279 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900280 goto go_write;
281 }
282
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700283 /*
284 * if there is no written data, don't waste time to write recovery info.
285 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700286 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800287 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700288
Changman Lee9c7bb702014-12-08 15:29:40 +0900289 /* it may call write_inode just prior to fsync */
290 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700291 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700292
Jaegeuk Kim91942322016-05-20 10:13:22 -0700293 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800294 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700295 goto flush_out;
296 goto out;
297 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700298go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900299 /*
300 * Both of fdatasync() and fsync() are able to be recovered from
301 * sudden-power-off.
302 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700303 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800304 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700305 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900306
Chao Yua5fd5052017-11-06 22:51:45 +0800307 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900308 /* all the dirty node pages should be flushed for POR */
309 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900310
Changman Lee51455b12014-12-08 15:29:41 +0900311 /*
312 * We've secured consistency through sync_fs. Following pino
313 * will be used only for fsynced inodes after checkpoint.
314 */
315 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700316 clear_inode_flag(inode, FI_APPEND_WRITE);
317 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900318 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900319 }
Changman Lee51455b12014-12-08 15:29:41 +0900320sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800321 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800322 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800323 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700324 if (ret)
325 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900326
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800327 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800328 if (unlikely(f2fs_cp_error(sbi))) {
329 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800330 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800331 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800332
Chao Yu4d57b862018-05-30 00:20:41 +0800333 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700334 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900335 f2fs_write_inode(inode, NULL);
336 goto sync_nodes;
337 }
338
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700339 /*
340 * If it's atomic_write, it's just fine to keep write ordering. So
341 * here we don't need to wait for node write completion, since we use
342 * node chain which serializes node blocks. If one of node writes are
343 * reordered, we can see simply broken chain, resulting in stopping
344 * roll-forward recovery. It means we'll recover all or none node blocks
345 * given fsync mark.
346 */
347 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800348 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700349 if (ret)
350 goto out;
351 }
Changman Lee51455b12014-12-08 15:29:41 +0900352
353 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800354 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700355 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900356flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700357 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800358 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800359 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800360 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800361 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800362 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800363 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800364 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900365out:
Chao Yua5fd5052017-11-06 22:51:45 +0800366 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700367 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900368 return ret;
369}
370
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700371int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
372{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200373 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
374 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700375 return f2fs_do_sync_file(file, start, end, datasync, false);
376}
377
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900378static pgoff_t __get_first_dirty_index(struct address_space *mapping,
379 pgoff_t pgofs, int whence)
380{
Jan Kara8faab642017-11-15 17:34:55 -0800381 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900382 int nr_pages;
383
384 if (whence != SEEK_DATA)
385 return 0;
386
387 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800388 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
389 1, &page);
390 if (!nr_pages)
391 return ULONG_MAX;
392 pgofs = page->index;
393 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900394 return pgofs;
395}
396
Chao Yue1da7872018-06-05 17:44:11 +0800397static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
398 pgoff_t dirty, pgoff_t pgofs, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900399{
400 switch (whence) {
401 case SEEK_DATA:
402 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yu93770ab2019-04-15 15:26:32 +0800403 __is_valid_data_blkaddr(blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900404 return true;
405 break;
406 case SEEK_HOLE:
407 if (blkaddr == NULL_ADDR)
408 return true;
409 break;
410 }
411 return false;
412}
413
Chao Yu267378d2014-04-23 14:10:24 +0800414static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
415{
416 struct inode *inode = file->f_mapping->host;
417 loff_t maxbytes = inode->i_sb->s_maxbytes;
418 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900419 pgoff_t pgofs, end_offset, dirty;
420 loff_t data_ofs = offset;
421 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800422 int err = 0;
423
Al Viro59551022016-01-22 15:40:57 -0500424 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800425
426 isize = i_size_read(inode);
427 if (offset >= isize)
428 goto fail;
429
430 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800431 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800432 if (whence == SEEK_HOLE)
433 data_ofs = isize;
434 goto found;
435 }
436
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300437 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800438
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900439 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
440
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300441 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800442 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800443 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800444 if (err && err != -ENOENT) {
445 goto fail;
446 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900447 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800448 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800449 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800450 continue;
451 } else {
452 goto found;
453 }
454 }
455
Chao Yu81ca7352016-01-26 15:39:35 +0800456 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800457
458 /* find data/hole in dnode block */
459 for (; dn.ofs_in_node < end_offset;
460 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300461 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800462 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900463
Chao Yua2ced1c2020-02-14 17:44:10 +0800464 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu267378d2014-04-23 14:10:24 +0800465
Chao Yuc9b60782018-08-01 19:13:44 +0800466 if (__is_valid_data_blkaddr(blkaddr) &&
467 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800468 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800469 f2fs_put_dnode(&dn);
470 goto fail;
471 }
472
Chao Yue1da7872018-06-05 17:44:11 +0800473 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
474 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800475 f2fs_put_dnode(&dn);
476 goto found;
477 }
478 }
479 f2fs_put_dnode(&dn);
480 }
481
482 if (whence == SEEK_DATA)
483 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800484found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900485 if (whence == SEEK_HOLE && data_ofs > isize)
486 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500487 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800488 return vfs_setpos(file, data_ofs, maxbytes);
489fail:
Al Viro59551022016-01-22 15:40:57 -0500490 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800491 return -ENXIO;
492}
493
494static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
495{
496 struct inode *inode = file->f_mapping->host;
497 loff_t maxbytes = inode->i_sb->s_maxbytes;
498
499 switch (whence) {
500 case SEEK_SET:
501 case SEEK_CUR:
502 case SEEK_END:
503 return generic_file_llseek_size(file, offset, whence,
504 maxbytes, i_size_read(inode));
505 case SEEK_DATA:
506 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700507 if (offset < 0)
508 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800509 return f2fs_seek_block(file, offset, whence);
510 }
511
512 return -EINVAL;
513}
514
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900515static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
516{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700517 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800518 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700519
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200520 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
521 return -EIO;
522
Chao Yu4c8ff702019-11-01 18:07:14 +0800523 if (!f2fs_is_compress_backend_ready(inode))
524 return -EOPNOTSUPP;
525
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700526 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800527 err = f2fs_convert_inline_inode(inode);
528 if (err)
529 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700530
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900531 file_accessed(file);
532 vma->vm_ops = &f2fs_file_vm_ops;
Chao Yu4c8ff702019-11-01 18:07:14 +0800533 set_inode_flag(inode, FI_MMAP_FILE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900534 return 0;
535}
536
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700537static int f2fs_file_open(struct inode *inode, struct file *filp)
538{
Eric Biggers2e168c82017-11-29 12:35:28 -0800539 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700540
Eric Biggers2e168c82017-11-29 12:35:28 -0800541 if (err)
542 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900543
Chao Yu4c8ff702019-11-01 18:07:14 +0800544 if (!f2fs_is_compress_backend_ready(inode))
545 return -EOPNOTSUPP;
546
Eric Biggers95ae2512019-07-22 09:26:24 -0700547 err = fsverity_file_open(inode, filp);
548 if (err)
549 return err;
550
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900551 filp->f_mode |= FMODE_NOWAIT;
552
Chao Yu0abd6752017-07-09 00:13:07 +0800553 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700554}
555
Chao Yu4d57b862018-05-30 00:20:41 +0800556void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900557{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700558 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900559 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800560 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900561 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800562 int base = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800563 bool compressed_cluster = false;
564 int cluster_index = 0, valid_blocks = 0;
565 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
Chao Yu7a2af762017-07-19 00:19:06 +0800566
567 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
568 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900569
Gu Zheng45590712013-07-15 17:57:38 +0800570 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800571 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900572
Chao Yu4c8ff702019-11-01 18:07:14 +0800573 /* Assumption: truncateion starts with cluster */
574 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900575 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900576
Chao Yu4c8ff702019-11-01 18:07:14 +0800577 if (f2fs_compressed_file(dn->inode) &&
578 !(cluster_index & (cluster_size - 1))) {
579 if (compressed_cluster)
580 f2fs_i_compr_blocks_update(dn->inode,
581 valid_blocks, false);
582 compressed_cluster = (blkaddr == COMPRESS_ADDR);
583 valid_blocks = 0;
584 }
585
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900586 if (blkaddr == NULL_ADDR)
587 continue;
588
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800589 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800590 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800591
Chao Yu4c8ff702019-11-01 18:07:14 +0800592 if (__is_valid_data_blkaddr(blkaddr)) {
593 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800594 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800595 continue;
596 if (compressed_cluster)
597 valid_blocks++;
598 }
Chao Yuc9b60782018-08-01 19:13:44 +0800599
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700600 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700601 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800602
603 f2fs_invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900604 nr_free++;
605 }
Chao Yu19b2c302015-08-26 20:34:48 +0800606
Chao Yu4c8ff702019-11-01 18:07:14 +0800607 if (compressed_cluster)
608 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
609
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900610 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800611 pgoff_t fofs;
612 /*
613 * once we invalidate valid blkaddr in range [ofs, ofs + count],
614 * we will invalidate all blkaddr in the whole range.
615 */
Chao Yu4d57b862018-05-30 00:20:41 +0800616 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800617 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800618 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900619 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620 }
621 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900622
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800623 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900624 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
625 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900626}
627
Chao Yu4d57b862018-05-30 00:20:41 +0800628void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900629{
Chao Yud02a6e62019-03-25 21:08:19 +0800630 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900631}
632
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800633static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700634 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900635{
youngjun yoo193bea12018-05-30 04:21:14 +0900636 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300637 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700638 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639 struct page *page;
640
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700641 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700642 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900643
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700644 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700645 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700646 if (page && PageUptodate(page))
647 goto truncate_out;
648 f2fs_put_page(page, 1);
649 return 0;
650 }
651
Chao Yu4d57b862018-05-30 00:20:41 +0800652 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900653 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800654 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700655truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800656 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300657 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700658
659 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530660 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700661 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800662 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900663 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700664 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665}
666
Chao Yu3265d3d2020-03-18 16:22:59 +0800667int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700669 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900670 struct dnode_of_data dn;
671 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800672 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700673 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800674 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900675
Namjae Jeon51dd6242013-04-20 01:28:52 +0900676 trace_f2fs_truncate_blocks_enter(inode, from);
677
Chao Yudf033ca2018-03-20 23:08:29 +0800678 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900679
Chao Yu09210c92016-05-05 19:13:03 +0800680 if (free_from >= sbi->max_file_blocks)
681 goto free_partial;
682
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700683 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800684 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800685
Chao Yu4d57b862018-05-30 00:20:41 +0800686 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700687 if (IS_ERR(ipage)) {
688 err = PTR_ERR(ipage);
689 goto out;
690 }
691
692 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800693 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700694 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800695 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700696 goto out;
697 }
698
699 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800700 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900701 if (err) {
702 if (err == -ENOENT)
703 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700704 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700705 }
706
Chao Yu81ca7352016-01-26 15:39:35 +0800707 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900708
709 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700710 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900711
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900712 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800713 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900714 free_from += count;
715 }
716
717 f2fs_put_dnode(&dn);
718free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800719 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800720out:
721 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800722 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800723free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700724 /* lastly zero out the first data page */
725 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800726 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900727
Namjae Jeon51dd6242013-04-20 01:28:52 +0900728 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900729 return err;
730}
731
Chao Yu4c8ff702019-11-01 18:07:14 +0800732int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
733{
734 u64 free_from = from;
Chao Yu3265d3d2020-03-18 16:22:59 +0800735 int err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800736
Chao Yu3265d3d2020-03-18 16:22:59 +0800737#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800738 /*
739 * for compressed file, only support cluster size
740 * aligned truncation.
741 */
742 if (f2fs_compressed_file(inode)) {
743 size_t cluster_shift = PAGE_SHIFT +
744 F2FS_I(inode)->i_log_cluster_size;
745 size_t cluster_mask = (1 << cluster_shift) - 1;
746
747 free_from = from >> cluster_shift;
748 if (from & cluster_mask)
749 free_from++;
750 free_from <<= cluster_shift;
751 }
Chao Yu3265d3d2020-03-18 16:22:59 +0800752#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800753
Chao Yu3265d3d2020-03-18 16:22:59 +0800754 err = f2fs_do_truncate_blocks(inode, free_from, lock);
755 if (err)
756 return err;
757
758#ifdef CONFIG_F2FS_FS_COMPRESSION
759 if (from != free_from)
760 err = f2fs_truncate_partial_cluster(inode, from, lock);
761#endif
762
763 return err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800764}
765
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700766int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900767{
Chao Yub0154892015-08-24 17:39:42 +0800768 int err;
769
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200770 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
771 return -EIO;
772
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900773 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
774 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800775 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900776
Namjae Jeon51dd6242013-04-20 01:28:52 +0900777 trace_f2fs_truncate(inode);
778
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800779 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800780 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800781 return -EIO;
782 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200783
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800784 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800785 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800786 err = f2fs_convert_inline_inode(inode);
787 if (err)
788 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800789 }
790
Chao Yuc42d28c2019-02-02 17:33:01 +0800791 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800792 if (err)
793 return err;
794
Deepa Dinamani078cd822016-09-14 07:48:04 -0700795 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700796 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800797 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900798}
799
David Howellsa528d352017-01-31 16:46:22 +0000800int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800801 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900802{
David Howellsa528d352017-01-31 16:46:22 +0000803 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800804 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800805 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800806 unsigned int flags;
807
Chao Yu1c1d35d2018-01-25 14:54:42 +0800808 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800809 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800810 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
811 stat->result_mask |= STATX_BTIME;
812 stat->btime.tv_sec = fi->i_crtime.tv_sec;
813 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
814 }
815
Eric Biggers36098552019-06-04 22:59:04 -0700816 flags = fi->i_flags;
Chao Yufd267252020-03-25 10:22:09 +0800817 if (flags & F2FS_COMPR_FL)
818 stat->attributes |= STATX_ATTR_COMPRESSED;
Chao Yu59c84402018-04-03 15:08:17 +0800819 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800820 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530821 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800822 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800823 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800824 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800825 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800826 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700827 if (IS_VERITY(inode))
828 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800829
Chao Yufd267252020-03-25 10:22:09 +0800830 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
831 STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800832 STATX_ATTR_ENCRYPTED |
833 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700834 STATX_ATTR_NODUMP |
835 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800836
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900837 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700838
839 /* we need to show initial sectors used for inline_data/dentries */
840 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
841 f2fs_has_inline_dentry(inode))
842 stat->blocks += (stat->size + 511) >> 9;
843
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900844 return 0;
845}
846
847#ifdef CONFIG_F2FS_FS_POSIX_ACL
848static void __setattr_copy(struct inode *inode, const struct iattr *attr)
849{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900850 unsigned int ia_valid = attr->ia_valid;
851
852 if (ia_valid & ATTR_UID)
853 inode->i_uid = attr->ia_uid;
854 if (ia_valid & ATTR_GID)
855 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200856 if (ia_valid & ATTR_ATIME)
857 inode->i_atime = attr->ia_atime;
858 if (ia_valid & ATTR_MTIME)
859 inode->i_mtime = attr->ia_mtime;
860 if (ia_valid & ATTR_CTIME)
861 inode->i_ctime = attr->ia_ctime;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900862 if (ia_valid & ATTR_MODE) {
863 umode_t mode = attr->ia_mode;
864
865 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
866 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700867 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900868 }
869}
870#else
871#define __setattr_copy setattr_copy
872#endif
873
874int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
875{
David Howells2b0143b2015-03-17 22:25:59 +0000876 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900877 int err;
878
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200879 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
880 return -EIO;
881
Chao Yu4c8ff702019-11-01 18:07:14 +0800882 if ((attr->ia_valid & ATTR_SIZE) &&
883 !f2fs_is_compress_backend_ready(inode))
884 return -EOPNOTSUPP;
885
Jan Kara31051c82016-05-26 16:55:18 +0200886 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900887 if (err)
888 return err;
889
Eric Biggers20bb2472017-11-29 12:35:32 -0800890 err = fscrypt_prepare_setattr(dentry, attr);
891 if (err)
892 return err;
893
Eric Biggers95ae2512019-07-22 09:26:24 -0700894 err = fsverity_prepare_setattr(dentry, attr);
895 if (err)
896 return err;
897
Chao Yu0abd6752017-07-09 00:13:07 +0800898 if (is_quota_modification(inode, attr)) {
899 err = dquot_initialize(inode);
900 if (err)
901 return err;
902 }
903 if ((attr->ia_valid & ATTR_UID &&
904 !uid_eq(attr->ia_uid, inode->i_uid)) ||
905 (attr->ia_valid & ATTR_GID &&
906 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800907 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800908 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800909 if (err) {
910 set_sbi_flag(F2FS_I_SB(inode),
911 SBI_QUOTA_NEED_REPAIR);
912 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800913 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800914 }
915 /*
916 * update uid/gid under lock_op(), so that dquot and inode can
917 * be updated atomically.
918 */
919 if (attr->ia_valid & ATTR_UID)
920 inode->i_uid = attr->ia_uid;
921 if (attr->ia_valid & ATTR_GID)
922 inode->i_gid = attr->ia_gid;
923 f2fs_mark_inode_dirty_sync(inode, true);
924 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800925 }
926
Chao Yu09db6a22014-09-15 18:02:09 +0800927 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800928 loff_t old_size = i_size_read(inode);
929
930 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
931 /*
932 * should convert inline inode before i_size_write to
933 * keep smaller than inline_data size with inline flag.
934 */
935 err = f2fs_convert_inline_inode(inode);
936 if (err)
937 return err;
938 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800939
Chao Yua33c1502018-08-05 23:04:25 +0800940 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900941 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800942
943 truncate_setsize(inode, attr->ia_size);
944
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800945 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800946 err = f2fs_truncate(inode);
947 /*
948 * do not trim all blocks after i_size if target size is
949 * larger than i_size.
950 */
Chao Yua33c1502018-08-05 23:04:25 +0800951 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900952 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800953 if (err)
954 return err;
955
Chao Yuc10c9822020-02-27 19:30:03 +0800956 spin_lock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800957 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800958 F2FS_I(inode)->last_disk_size = i_size_read(inode);
Chao Yuc10c9822020-02-27 19:30:03 +0800959 spin_unlock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900960 }
961
962 __setattr_copy(inode, attr);
963
964 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800965 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700966 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
967 inode->i_mode = F2FS_I(inode)->i_acl_mode;
968 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900969 }
970 }
971
Yunlei Hec0ed4402016-12-11 15:35:15 +0800972 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800973 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700974
975 /* inode change will produce dirty node pages flushed by checkpoint */
976 f2fs_balance_fs(F2FS_I_SB(inode), true);
977
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900978 return err;
979}
980
981const struct inode_operations f2fs_file_inode_operations = {
982 .getattr = f2fs_getattr,
983 .setattr = f2fs_setattr,
984 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800985 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900986#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900987 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900988#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900989 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900990};
991
Chao Yu63943282015-08-07 18:36:06 +0800992static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900993 loff_t start, loff_t len)
994{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700995 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900996 struct page *page;
997
998 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800999 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001000
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001001 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +09001002
Gu Zhenge4795562013-09-27 18:08:30 +08001003 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001004 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +08001005 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001006
Chao Yu63943282015-08-07 18:36:06 +08001007 if (IS_ERR(page))
1008 return PTR_ERR(page);
1009
Chao Yubae0ee72018-12-25 17:43:42 +08001010 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +08001011 zero_user(page, start, len);
1012 set_page_dirty(page);
1013 f2fs_put_page(page, 1);
1014 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001015}
1016
Chao Yu4d57b862018-05-30 00:20:41 +08001017int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001018{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001019 int err;
1020
Chao Yuea587112015-09-17 20:22:44 +08001021 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001022 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001023 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001024
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001025 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001026 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001027 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001028 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001029 pg_start = f2fs_get_next_page_offset(&dn,
1030 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001031 continue;
Chao Yuea587112015-09-17 20:22:44 +08001032 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001033 return err;
1034 }
1035
Chao Yu81ca7352016-01-26 15:39:35 +08001036 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001037 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1038
1039 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1040
Chao Yu4d57b862018-05-30 00:20:41 +08001041 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001042 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001043
1044 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001045 }
1046 return 0;
1047}
1048
Chao Yua66c7b22013-11-22 16:52:50 +08001049static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001050{
1051 pgoff_t pg_start, pg_end;
1052 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001053 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001054
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001055 ret = f2fs_convert_inline_inode(inode);
1056 if (ret)
1057 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001058
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001059 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1060 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001061
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001062 off_start = offset & (PAGE_SIZE - 1);
1063 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001064
1065 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001066 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001067 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001068 if (ret)
1069 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001070 } else {
Chao Yu63943282015-08-07 18:36:06 +08001071 if (off_start) {
1072 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001074 if (ret)
1075 return ret;
1076 }
1077 if (off_end) {
1078 ret = fill_zero(inode, pg_end, 0, off_end);
1079 if (ret)
1080 return ret;
1081 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001082
1083 if (pg_start < pg_end) {
1084 struct address_space *mapping = inode->i_mapping;
1085 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001086 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001087
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001088 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001089
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001090 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1091 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001092
Chao Yua33c1502018-08-05 23:04:25 +08001093 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001094 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001095
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001096 truncate_inode_pages_range(mapping, blk_start,
1097 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001098
Gu Zhenge4795562013-09-27 18:08:30 +08001099 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001100 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001101 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001102
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001103 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001104 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001105 }
1106 }
1107
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001108 return ret;
1109}
1110
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001111static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1112 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001113{
1114 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1115 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001116 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001117
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001118next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001119 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001120 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001121 if (ret && ret != -ENOENT) {
1122 return ret;
1123 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001124 if (dn.max_level == 0)
1125 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001126 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1127 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001128 blkaddr += done;
1129 do_replace += done;
1130 goto next;
1131 }
1132
1133 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1134 dn.ofs_in_node, len);
1135 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001136 *blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu93770ab2019-04-15 15:26:32 +08001137
1138 if (__is_valid_data_blkaddr(*blkaddr) &&
1139 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1140 DATA_GENERIC_ENHANCE)) {
1141 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001142 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001143 }
1144
Chao Yu4d57b862018-05-30 00:20:41 +08001145 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001146
Chao Yub0332a02020-02-14 17:44:12 +08001147 if (f2fs_lfs_mode(sbi)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001148 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001149 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001150 }
1151
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001152 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001153 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001154 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001155 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001156 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001157 f2fs_put_dnode(&dn);
1158next:
1159 len -= done;
1160 off += done;
1161 if (len)
1162 goto next_dnode;
1163 return 0;
1164}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001165
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001166static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1167 int *do_replace, pgoff_t off, int len)
1168{
1169 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1170 struct dnode_of_data dn;
1171 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001172
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001173 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1174 if (*do_replace == 0)
1175 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001176
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001177 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001178 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001179 if (ret) {
1180 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001181 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001182 } else {
1183 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001184 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001185 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001186 }
1187 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001188}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001189
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001190static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1191 block_t *blkaddr, int *do_replace,
1192 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1193{
1194 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1195 pgoff_t i = 0;
1196 int ret;
1197
1198 while (i < len) {
1199 if (blkaddr[i] == NULL_ADDR && !full) {
1200 i++;
1201 continue;
1202 }
1203
1204 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1205 struct dnode_of_data dn;
1206 struct node_info ni;
1207 size_t new_size;
1208 pgoff_t ilen;
1209
1210 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001211 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001212 if (ret)
1213 return ret;
1214
Chao Yu77357302018-07-17 00:02:17 +08001215 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1216 if (ret) {
1217 f2fs_put_dnode(&dn);
1218 return ret;
1219 }
1220
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001221 ilen = min((pgoff_t)
1222 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1223 dn.ofs_in_node, len - i);
1224 do {
Chao Yua2ced1c2020-02-14 17:44:10 +08001225 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu4d57b862018-05-30 00:20:41 +08001226 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001227
1228 if (do_replace[i]) {
1229 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001230 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001231 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001232 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001233 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1234 blkaddr[i], ni.version, true, false);
1235
1236 do_replace[i] = 0;
1237 }
1238 dn.ofs_in_node++;
1239 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001240 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001241 if (dst_inode->i_size < new_size)
1242 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001243 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001244
1245 f2fs_put_dnode(&dn);
1246 } else {
1247 struct page *psrc, *pdst;
1248
Chao Yu4d57b862018-05-30 00:20:41 +08001249 psrc = f2fs_get_lock_data_page(src_inode,
1250 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001251 if (IS_ERR(psrc))
1252 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001253 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001254 true);
1255 if (IS_ERR(pdst)) {
1256 f2fs_put_page(psrc, 1);
1257 return PTR_ERR(pdst);
1258 }
1259 f2fs_copy_page(psrc, pdst);
1260 set_page_dirty(pdst);
1261 f2fs_put_page(pdst, 1);
1262 f2fs_put_page(psrc, 1);
1263
Chao Yu4d57b862018-05-30 00:20:41 +08001264 ret = f2fs_truncate_hole(src_inode,
1265 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001266 if (ret)
1267 return ret;
1268 i++;
1269 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001270 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001271 return 0;
1272}
1273
1274static int __exchange_data_block(struct inode *src_inode,
1275 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001276 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001277{
1278 block_t *src_blkaddr;
1279 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001280 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001281 int ret;
1282
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001283 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001284 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001285
Chao Yu628b3d12017-11-30 19:28:18 +08001286 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001287 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001288 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001289 if (!src_blkaddr)
1290 return -ENOMEM;
1291
Chao Yu628b3d12017-11-30 19:28:18 +08001292 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001293 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001294 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001295 if (!do_replace) {
1296 kvfree(src_blkaddr);
1297 return -ENOMEM;
1298 }
1299
1300 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1301 do_replace, src, olen);
1302 if (ret)
1303 goto roll_back;
1304
1305 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1306 do_replace, src, dst, olen, full);
1307 if (ret)
1308 goto roll_back;
1309
1310 src += olen;
1311 dst += olen;
1312 len -= olen;
1313
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001314 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001315 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001316 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001317 return 0;
1318
1319roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001320 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001321 kvfree(src_blkaddr);
1322 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001323 return ret;
1324}
1325
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001326static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001327{
1328 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001329 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001330 pgoff_t start = offset >> PAGE_SHIFT;
1331 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001332 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001333
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001334 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001335
1336 /* avoid gc operation during block exchange */
1337 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1338 down_write(&F2FS_I(inode)->i_mmap_sem);
1339
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001340 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001341 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001342 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001343 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1344 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001345
1346 up_write(&F2FS_I(inode)->i_mmap_sem);
1347 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001348 return ret;
1349}
1350
1351static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1352{
Chao Yub4ace332015-05-06 13:09:46 +08001353 loff_t new_size;
1354 int ret;
1355
Chao Yub4ace332015-05-06 13:09:46 +08001356 if (offset + len >= i_size_read(inode))
1357 return -EINVAL;
1358
1359 /* collapse range should be aligned to block size of f2fs. */
1360 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1361 return -EINVAL;
1362
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001363 ret = f2fs_convert_inline_inode(inode);
1364 if (ret)
1365 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001366
Chao Yub4ace332015-05-06 13:09:46 +08001367 /* write out all dirty pages from offset */
1368 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1369 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001370 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001371
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001372 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001373 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001374 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001375
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001376 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001377 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001378 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1379 truncate_pagecache(inode, offset);
1380
Chao Yub4ace332015-05-06 13:09:46 +08001381 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001382 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001383
Chao Yuc42d28c2019-02-02 17:33:01 +08001384 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001385 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001386 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001387 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001388 return ret;
1389}
1390
Chao Yu6e961942016-05-09 19:56:31 +08001391static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1392 pgoff_t end)
1393{
1394 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1395 pgoff_t index = start;
1396 unsigned int ofs_in_node = dn->ofs_in_node;
1397 blkcnt_t count = 0;
1398 int ret;
1399
1400 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001401 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001402 count++;
1403 }
1404
1405 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001406 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001407 if (ret)
1408 return ret;
1409
1410 dn->ofs_in_node = ofs_in_node;
1411 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001412 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001413 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001414 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001415 * allocation.
1416 */
1417 if (dn->data_blkaddr == NULL_ADDR) {
1418 ret = -ENOSPC;
1419 break;
1420 }
1421 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001422 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001423 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001424 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001425 }
1426 }
1427
1428 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1429
1430 return ret;
1431}
1432
Chao Yu75cd4e02015-05-06 13:11:13 +08001433static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1434 int mode)
1435{
1436 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1437 struct address_space *mapping = inode->i_mapping;
1438 pgoff_t index, pg_start, pg_end;
1439 loff_t new_size = i_size_read(inode);
1440 loff_t off_start, off_end;
1441 int ret = 0;
1442
Chao Yu75cd4e02015-05-06 13:11:13 +08001443 ret = inode_newsize_ok(inode, (len + offset));
1444 if (ret)
1445 return ret;
1446
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001447 ret = f2fs_convert_inline_inode(inode);
1448 if (ret)
1449 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001450
1451 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1452 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001453 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001454
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001455 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1456 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001457
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 off_start = offset & (PAGE_SIZE - 1);
1459 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001460
1461 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001462 ret = fill_zero(inode, pg_start, off_start,
1463 off_end - off_start);
1464 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001465 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001466
Chao Yu75cd4e02015-05-06 13:11:13 +08001467 new_size = max_t(loff_t, new_size, offset + len);
1468 } else {
1469 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001470 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001471 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001472 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001473 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001474
Chao Yu75cd4e02015-05-06 13:11:13 +08001475 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001476 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001477 }
1478
Chao Yu6e961942016-05-09 19:56:31 +08001479 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001480 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001481 unsigned int end_offset;
1482 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001483
Chao Yuc70798532018-08-05 23:02:22 +08001484 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001485 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001486
1487 truncate_pagecache_range(inode,
1488 (loff_t)index << PAGE_SHIFT,
1489 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1490
Chao Yu75cd4e02015-05-06 13:11:13 +08001491 f2fs_lock_op(sbi);
1492
Chao Yu6e961942016-05-09 19:56:31 +08001493 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001494 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001495 if (ret) {
1496 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001497 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001498 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001499 goto out;
1500 }
1501
Chao Yu6e961942016-05-09 19:56:31 +08001502 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1503 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1504
1505 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001506 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001507
Chao Yu75cd4e02015-05-06 13:11:13 +08001508 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001509 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001510 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001511
1512 f2fs_balance_fs(sbi, dn.node_changed);
1513
Chao Yu6e961942016-05-09 19:56:31 +08001514 if (ret)
1515 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001516
Chao Yu6e961942016-05-09 19:56:31 +08001517 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001518 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001519 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001520 }
1521
1522 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001523 ret = fill_zero(inode, pg_end, 0, off_end);
1524 if (ret)
1525 goto out;
1526
Chao Yu75cd4e02015-05-06 13:11:13 +08001527 new_size = max_t(loff_t, new_size, offset + len);
1528 }
1529 }
1530
1531out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001532 if (new_size > i_size_read(inode)) {
1533 if (mode & FALLOC_FL_KEEP_SIZE)
1534 file_set_keep_isize(inode);
1535 else
1536 f2fs_i_size_write(inode, new_size);
1537 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001538 return ret;
1539}
1540
Chao Yuf62185d2015-05-28 19:16:57 +08001541static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1542{
1543 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001544 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001545 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001546 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001547
Chao Yuf62185d2015-05-28 19:16:57 +08001548 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001549 ret = inode_newsize_ok(inode, new_size);
1550 if (ret)
1551 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001552
1553 if (offset >= i_size_read(inode))
1554 return -EINVAL;
1555
1556 /* insert range should be aligned to block size of f2fs. */
1557 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1558 return -EINVAL;
1559
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001560 ret = f2fs_convert_inline_inode(inode);
1561 if (ret)
1562 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001563
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001564 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001565
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001566 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001567 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001568 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001569 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001570 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001571
1572 /* write out all dirty pages from offset */
1573 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1574 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001575 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001576
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001577 pg_start = offset >> PAGE_SHIFT;
1578 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001579 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001580 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001581
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001582 /* avoid gc operation during block exchange */
1583 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1584 down_write(&F2FS_I(inode)->i_mmap_sem);
1585 truncate_pagecache(inode, offset);
1586
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001587 while (!ret && idx > pg_start) {
1588 nr = idx - pg_start;
1589 if (nr > delta)
1590 nr = delta;
1591 idx -= nr;
1592
Chao Yuf62185d2015-05-28 19:16:57 +08001593 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001594 f2fs_drop_extent_tree(inode);
1595
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001596 ret = __exchange_data_block(inode, inode, idx,
1597 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001598 f2fs_unlock_op(sbi);
1599 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001600 up_write(&F2FS_I(inode)->i_mmap_sem);
1601 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001602
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001603 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001604 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001605 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1606 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001607 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001608
1609 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001610 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001611 return ret;
1612}
1613
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001614static int expand_inode_data(struct inode *inode, loff_t offset,
1615 loff_t len, int mode)
1616{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001617 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001618 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001619 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1620 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001621 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001622 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001623 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001624 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001625
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001626 err = inode_newsize_ok(inode, (len + offset));
1627 if (err)
1628 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001629
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001630 err = f2fs_convert_inline_inode(inode);
1631 if (err)
1632 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001633
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001634 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001635
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001636 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001637 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001638
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001639 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1640 map.m_len = pg_end - map.m_lblk;
1641 if (off_end)
1642 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001643
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001644 if (!map.m_len)
1645 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001646
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001647 if (f2fs_is_pinned_file(inode)) {
1648 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1649 sbi->log_blocks_per_seg;
1650 block_t done = 0;
1651
1652 if (map.m_len % sbi->blocks_per_seg)
1653 len += sbi->blocks_per_seg;
1654
1655 map.m_len = sbi->blocks_per_seg;
1656next_alloc:
1657 if (has_not_enough_free_secs(sbi, 0,
1658 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
Chao Yufb24fea2020-01-14 19:36:50 +08001659 down_write(&sbi->gc_lock);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001660 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1661 if (err && err != -ENODATA && err != -EAGAIN)
1662 goto out_err;
1663 }
1664
1665 down_write(&sbi->pin_sem);
1666 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1667 f2fs_allocate_new_segments(sbi, CURSEG_COLD_DATA);
1668 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1669 up_write(&sbi->pin_sem);
1670
1671 done += map.m_len;
1672 len -= map.m_len;
1673 map.m_lblk += map.m_len;
1674 if (!err && len)
1675 goto next_alloc;
1676
1677 map.m_len = done;
1678 } else {
1679 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1680 }
1681out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001682 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001683 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001684
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001685 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001686 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001687
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001688 last_off = map.m_lblk + map.m_len - 1;
1689
1690 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001691 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001692 (loff_t)(last_off + 1) << PAGE_SHIFT;
1693 } else {
1694 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001695 }
1696
Chao Yue8ed90a2017-11-05 21:53:30 +08001697 if (new_size > i_size_read(inode)) {
1698 if (mode & FALLOC_FL_KEEP_SIZE)
1699 file_set_keep_isize(inode);
1700 else
1701 f2fs_i_size_write(inode, new_size);
1702 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001703
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001704 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001705}
1706
1707static long f2fs_fallocate(struct file *file, int mode,
1708 loff_t offset, loff_t len)
1709{
Al Viro6131ffa2013-02-27 16:59:05 -05001710 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001711 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001712
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001713 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1714 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001715 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1716 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001717 if (!f2fs_is_compress_backend_ready(inode))
1718 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001719
Chao Yuc9980122015-09-11 14:39:02 +08001720 /* f2fs only support ->fallocate for regular file */
1721 if (!S_ISREG(inode->i_mode))
1722 return -EINVAL;
1723
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301724 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001725 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001726 return -EOPNOTSUPP;
1727
Chao Yu4c8ff702019-11-01 18:07:14 +08001728 if (f2fs_compressed_file(inode) &&
1729 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1730 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1731 return -EOPNOTSUPP;
1732
Chao Yub4ace332015-05-06 13:09:46 +08001733 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001734 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1735 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001736 return -EOPNOTSUPP;
1737
Al Viro59551022016-01-22 15:40:57 -05001738 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001739
Taehee Yoo587c0a42015-04-21 15:59:12 +09001740 if (mode & FALLOC_FL_PUNCH_HOLE) {
1741 if (offset >= inode->i_size)
1742 goto out;
1743
Chao Yua66c7b22013-11-22 16:52:50 +08001744 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001745 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1746 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001747 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1748 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001749 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1750 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001751 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001752 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001753 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001754
Namjae Jeon3af60a42012-12-30 14:52:37 +09001755 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001756 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001757 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001758 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001759 }
Chao Yu3375f692014-01-28 10:29:26 +08001760
Taehee Yoo587c0a42015-04-21 15:59:12 +09001761out:
Al Viro59551022016-01-22 15:40:57 -05001762 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001763
Namjae Jeonc01e2852013-04-23 17:00:52 +09001764 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001765 return ret;
1766}
1767
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001768static int f2fs_release_file(struct inode *inode, struct file *filp)
1769{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001770 /*
1771 * f2fs_relase_file is called at every close calls. So we should
1772 * not drop any inmemory pages by close called by other process.
1773 */
1774 if (!(filp->f_mode & FMODE_WRITE) ||
1775 atomic_read(&inode->i_writecount) != 1)
1776 return 0;
1777
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001778 /* some remained atomic pages should discarded */
1779 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001780 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001781 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001782 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001783 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001784 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001785 clear_inode_flag(inode, FI_VOLATILE_FILE);
1786 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001787 }
1788 return 0;
1789}
1790
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001791static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001792{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001793 struct inode *inode = file_inode(file);
1794
1795 /*
1796 * If the process doing a transaction is crashed, we should do
1797 * roll-back. Otherwise, other reader/write can see corrupted database
1798 * until all the writers close its file. Since this should be done
1799 * before dropping file lock, it needs to do in ->flush.
1800 */
1801 if (f2fs_is_atomic_file(inode) &&
1802 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001803 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001804 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001805}
1806
Eric Biggers36098552019-06-04 22:59:04 -07001807static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001808{
1809 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001810 u32 masked_flags = fi->i_flags & mask;
1811
1812 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
Chao Yu2c1d0302017-07-29 00:32:52 +08001813
1814 /* Is it quota file? Do not allow user to mess with it */
1815 if (IS_NOQUOTA(inode))
1816 return -EPERM;
1817
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001818 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001819 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1820 return -EOPNOTSUPP;
1821 if (!f2fs_empty_dir(inode))
1822 return -ENOTEMPTY;
1823 }
1824
Chao Yu4c8ff702019-11-01 18:07:14 +08001825 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1826 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1827 return -EOPNOTSUPP;
1828 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1829 return -EINVAL;
1830 }
1831
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001832 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
Chao Yuaa576972020-03-27 18:29:51 +08001833 if (masked_flags & F2FS_COMPR_FL) {
Chao Yu2536ac62020-03-10 20:50:09 +08001834 if (f2fs_disable_compressed_file(inode))
1835 return -EINVAL;
1836 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001837 if (iflags & F2FS_NOCOMP_FL)
1838 return -EINVAL;
1839 if (iflags & F2FS_COMPR_FL) {
Chao Yu4c8ff702019-11-01 18:07:14 +08001840 if (!f2fs_may_compress(inode))
1841 return -EINVAL;
1842
1843 set_compress_context(inode);
1844 }
1845 }
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001846 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1847 if (masked_flags & F2FS_COMPR_FL)
Chao Yu4c8ff702019-11-01 18:07:14 +08001848 return -EINVAL;
1849 }
1850
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001851 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001852 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1853 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001854
Chao Yu59c84402018-04-03 15:08:17 +08001855 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001856 set_inode_flag(inode, FI_PROJ_INHERIT);
1857 else
1858 clear_inode_flag(inode, FI_PROJ_INHERIT);
1859
1860 inode->i_ctime = current_time(inode);
1861 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001862 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001863 return 0;
1864}
1865
Eric Biggers36098552019-06-04 22:59:04 -07001866/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1867
1868/*
1869 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1870 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1871 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1872 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1873 */
1874
1875static const struct {
1876 u32 iflag;
1877 u32 fsflag;
1878} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001879 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001880 { F2FS_SYNC_FL, FS_SYNC_FL },
1881 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1882 { F2FS_APPEND_FL, FS_APPEND_FL },
1883 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1884 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001885 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001886 { F2FS_INDEX_FL, FS_INDEX_FL },
1887 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1888 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001889 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001890};
1891
1892#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001893 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001894 FS_SYNC_FL | \
1895 FS_IMMUTABLE_FL | \
1896 FS_APPEND_FL | \
1897 FS_NODUMP_FL | \
1898 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001899 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001900 FS_INDEX_FL | \
1901 FS_DIRSYNC_FL | \
1902 FS_PROJINHERIT_FL | \
1903 FS_ENCRYPT_FL | \
1904 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001905 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001906 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001907 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001908
1909#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001910 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001911 FS_SYNC_FL | \
1912 FS_IMMUTABLE_FL | \
1913 FS_APPEND_FL | \
1914 FS_NODUMP_FL | \
1915 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001916 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001917 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001918 FS_PROJINHERIT_FL | \
1919 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001920
1921/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1922static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1923{
1924 u32 fsflags = 0;
1925 int i;
1926
1927 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1928 if (iflags & f2fs_fsflags_map[i].iflag)
1929 fsflags |= f2fs_fsflags_map[i].fsflag;
1930
1931 return fsflags;
1932}
1933
1934/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1935static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1936{
1937 u32 iflags = 0;
1938 int i;
1939
1940 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1941 if (fsflags & f2fs_fsflags_map[i].fsflag)
1942 iflags |= f2fs_fsflags_map[i].iflag;
1943
1944 return iflags;
1945}
1946
1947static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1948{
1949 struct inode *inode = file_inode(filp);
1950 struct f2fs_inode_info *fi = F2FS_I(inode);
1951 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1952
1953 if (IS_ENCRYPTED(inode))
1954 fsflags |= FS_ENCRYPT_FL;
Eric Biggers95ae2512019-07-22 09:26:24 -07001955 if (IS_VERITY(inode))
1956 fsflags |= FS_VERITY_FL;
Eric Biggers36098552019-06-04 22:59:04 -07001957 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1958 fsflags |= FS_INLINE_DATA_FL;
1959 if (is_inode_flag_set(inode, FI_PIN_FILE))
1960 fsflags |= FS_NOCOW_FL;
1961
1962 fsflags &= F2FS_GETTABLE_FS_FL;
1963
1964 return put_user(fsflags, (int __user *)arg);
1965}
1966
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001967static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1968{
1969 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001970 struct f2fs_inode_info *fi = F2FS_I(inode);
1971 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001972 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001973 int ret;
1974
Chao Yu7fb17fe2016-05-09 19:56:32 +08001975 if (!inode_owner_or_capable(inode))
1976 return -EACCES;
1977
Eric Biggers36098552019-06-04 22:59:04 -07001978 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001979 return -EFAULT;
1980
Eric Biggers36098552019-06-04 22:59:04 -07001981 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1982 return -EOPNOTSUPP;
1983 fsflags &= F2FS_SETTABLE_FS_FL;
1984
1985 iflags = f2fs_fsflags_to_iflags(fsflags);
1986 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1987 return -EOPNOTSUPP;
1988
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001989 ret = mnt_want_write_file(filp);
1990 if (ret)
1991 return ret;
1992
Al Viro59551022016-01-22 15:40:57 -05001993 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001994
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001995 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1996 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1997 if (ret)
1998 goto out;
1999
Eric Biggers36098552019-06-04 22:59:04 -07002000 ret = f2fs_setflags_common(inode, iflags,
2001 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07002002out:
Chao Yua72d4b92017-05-03 23:59:13 +08002003 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002004 mnt_drop_write_file(filp);
2005 return ret;
2006}
2007
Chao Yud49f3e82015-01-23 20:36:04 +08002008static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2009{
2010 struct inode *inode = file_inode(filp);
2011
2012 return put_user(inode->i_generation, (int __user *)arg);
2013}
2014
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002015static int f2fs_ioc_start_atomic_write(struct file *filp)
2016{
2017 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002018 struct f2fs_inode_info *fi = F2FS_I(inode);
2019 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08002020 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002021
2022 if (!inode_owner_or_capable(inode))
2023 return -EACCES;
2024
Jaegeuk Kime8118982017-03-17 10:04:15 +08002025 if (!S_ISREG(inode->i_mode))
2026 return -EINVAL;
2027
Chao Yu038d0692019-07-25 22:39:11 +08002028 if (filp->f_flags & O_DIRECT)
2029 return -EINVAL;
2030
Chao Yu7fb17fe2016-05-09 19:56:32 +08002031 ret = mnt_want_write_file(filp);
2032 if (ret)
2033 return ret;
2034
Chao Yu0fac5582016-05-09 19:56:33 +08002035 inode_lock(inode);
2036
Chao Yu4c8ff702019-11-01 18:07:14 +08002037 f2fs_disable_compressed_file(inode);
2038
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002039 if (f2fs_is_atomic_file(inode)) {
2040 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2041 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002042 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002043 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002044
Chao Yuf4c9c742015-07-17 18:06:35 +08002045 ret = f2fs_convert_inline_inode(inode);
2046 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002047 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002048
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002049 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2050
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002051 /*
2052 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2053 * f2fs_is_atomic_file.
2054 */
2055 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002056 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2057 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002058 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002059 if (ret) {
2060 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002061 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002062 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002063
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002064 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2065 if (list_empty(&fi->inmem_ilist))
2066 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302067 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002068 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2069
2070 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002071 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002072 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002073 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002074
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002075 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002076 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002077 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002078out:
Chao Yu0fac5582016-05-09 19:56:33 +08002079 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002080 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002081 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002082}
2083
2084static int f2fs_ioc_commit_atomic_write(struct file *filp)
2085{
2086 struct inode *inode = file_inode(filp);
2087 int ret;
2088
2089 if (!inode_owner_or_capable(inode))
2090 return -EACCES;
2091
2092 ret = mnt_want_write_file(filp);
2093 if (ret)
2094 return ret;
2095
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002096 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002097
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002098 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002099
Chao Yub169c3c2018-04-18 17:45:02 +08002100 if (f2fs_is_volatile_file(inode)) {
2101 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002102 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002103 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002104
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002105 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002106 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002107 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002108 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002109
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002110 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002111 if (!ret)
2112 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002113 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002114 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002115 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002116err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002117 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2118 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2119 ret = -EINVAL;
2120 }
Chao Yu0fac5582016-05-09 19:56:33 +08002121 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002122 mnt_drop_write_file(filp);
2123 return ret;
2124}
2125
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002126static int f2fs_ioc_start_volatile_write(struct file *filp)
2127{
2128 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002129 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002130
2131 if (!inode_owner_or_capable(inode))
2132 return -EACCES;
2133
Chao Yu8ff09712017-03-17 15:43:57 +08002134 if (!S_ISREG(inode->i_mode))
2135 return -EINVAL;
2136
Chao Yu7fb17fe2016-05-09 19:56:32 +08002137 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002138 if (ret)
2139 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002140
Chao Yu0fac5582016-05-09 19:56:33 +08002141 inode_lock(inode);
2142
Chao Yu7fb17fe2016-05-09 19:56:32 +08002143 if (f2fs_is_volatile_file(inode))
2144 goto out;
2145
2146 ret = f2fs_convert_inline_inode(inode);
2147 if (ret)
2148 goto out;
2149
Chao Yu648d50b2017-03-22 17:23:45 +08002150 stat_inc_volatile_write(inode);
2151 stat_update_max_volatile_write(inode);
2152
Jaegeuk Kim91942322016-05-20 10:13:22 -07002153 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002154 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002155out:
Chao Yu0fac5582016-05-09 19:56:33 +08002156 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002157 mnt_drop_write_file(filp);
2158 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002159}
2160
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002161static int f2fs_ioc_release_volatile_write(struct file *filp)
2162{
2163 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002164 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002165
2166 if (!inode_owner_or_capable(inode))
2167 return -EACCES;
2168
Chao Yu7fb17fe2016-05-09 19:56:32 +08002169 ret = mnt_want_write_file(filp);
2170 if (ret)
2171 return ret;
2172
Chao Yu0fac5582016-05-09 19:56:33 +08002173 inode_lock(inode);
2174
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002175 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002176 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002177
Chao Yu7fb17fe2016-05-09 19:56:32 +08002178 if (!f2fs_is_first_block_written(inode)) {
2179 ret = truncate_partial_data_page(inode, 0, true);
2180 goto out;
2181 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002182
Chao Yu7fb17fe2016-05-09 19:56:32 +08002183 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2184out:
Chao Yu0fac5582016-05-09 19:56:33 +08002185 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002186 mnt_drop_write_file(filp);
2187 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002188}
2189
2190static int f2fs_ioc_abort_volatile_write(struct file *filp)
2191{
2192 struct inode *inode = file_inode(filp);
2193 int ret;
2194
2195 if (!inode_owner_or_capable(inode))
2196 return -EACCES;
2197
2198 ret = mnt_want_write_file(filp);
2199 if (ret)
2200 return ret;
2201
Chao Yu0fac5582016-05-09 19:56:33 +08002202 inode_lock(inode);
2203
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002204 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002205 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002206 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002207 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002208 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002209 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002210 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002211
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002212 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2213
Chao Yu0fac5582016-05-09 19:56:33 +08002214 inode_unlock(inode);
2215
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002216 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002217 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002218 return ret;
2219}
2220
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002221static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2222{
2223 struct inode *inode = file_inode(filp);
2224 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2225 struct super_block *sb = sbi->sb;
2226 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002227 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002228
2229 if (!capable(CAP_SYS_ADMIN))
2230 return -EPERM;
2231
2232 if (get_user(in, (__u32 __user *)arg))
2233 return -EFAULT;
2234
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302235 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2236 ret = mnt_want_write_file(filp);
2237 if (ret)
2238 return ret;
2239 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002240
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002241 switch (in) {
2242 case F2FS_GOING_DOWN_FULLSYNC:
2243 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08002244 if (IS_ERR(sb)) {
2245 ret = PTR_ERR(sb);
2246 goto out;
2247 }
2248 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002249 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002250 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002251 thaw_bdev(sb->s_bdev, sb);
2252 }
2253 break;
2254 case F2FS_GOING_DOWN_METASYNC:
2255 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002256 ret = f2fs_sync_fs(sb, 1);
2257 if (ret)
2258 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002259 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002260 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002261 break;
2262 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002263 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002264 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002265 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002266 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002267 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002268 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002269 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002270 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002271 case F2FS_GOING_DOWN_NEED_FSCK:
2272 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002273 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2274 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002275 /* do checkpoint only */
2276 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002277 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002278 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002279 ret = -EINVAL;
2280 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002281 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002282
Chao Yu4d57b862018-05-30 00:20:41 +08002283 f2fs_stop_gc_thread(sbi);
2284 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002285
Chao Yu4d57b862018-05-30 00:20:41 +08002286 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002287 clear_opt(sbi, DISCARD);
2288
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002289 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002290out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302291 if (in != F2FS_GOING_DOWN_FULLSYNC)
2292 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002293
2294 trace_f2fs_shutdown(sbi, in, ret);
2295
Chao Yu7fb17fe2016-05-09 19:56:32 +08002296 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002297}
2298
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002299static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2300{
2301 struct inode *inode = file_inode(filp);
2302 struct super_block *sb = inode->i_sb;
2303 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2304 struct fstrim_range range;
2305 int ret;
2306
2307 if (!capable(CAP_SYS_ADMIN))
2308 return -EPERM;
2309
Chao Yu7d20c8a2018-09-04 03:52:17 +08002310 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002311 return -EOPNOTSUPP;
2312
2313 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2314 sizeof(range)))
2315 return -EFAULT;
2316
Chao Yu7fb17fe2016-05-09 19:56:32 +08002317 ret = mnt_want_write_file(filp);
2318 if (ret)
2319 return ret;
2320
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002321 range.minlen = max((unsigned int)range.minlen,
2322 q->limits.discard_granularity);
2323 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002324 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002325 if (ret < 0)
2326 return ret;
2327
2328 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2329 sizeof(range)))
2330 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002331 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002332 return 0;
2333}
2334
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002335static bool uuid_is_nonzero(__u8 u[16])
2336{
2337 int i;
2338
2339 for (i = 0; i < 16; i++)
2340 if (u[i])
2341 return true;
2342 return false;
2343}
2344
2345static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2346{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002347 struct inode *inode = file_inode(filp);
2348
Chao Yu7beb01f2018-10-24 18:34:26 +08002349 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002350 return -EOPNOTSUPP;
2351
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002352 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002353
Eric Biggersdb717d82016-11-26 19:07:49 -05002354 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002355}
2356
2357static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2358{
Chao Yu7beb01f2018-10-24 18:34:26 +08002359 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002360 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002361 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002362}
2363
2364static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2365{
2366 struct inode *inode = file_inode(filp);
2367 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2368 int err;
2369
Chao Yu7beb01f2018-10-24 18:34:26 +08002370 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002371 return -EOPNOTSUPP;
2372
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002373 err = mnt_want_write_file(filp);
2374 if (err)
2375 return err;
2376
Chao Yu846ae672018-02-26 22:04:13 +08002377 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002378
2379 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2380 goto got_it;
2381
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002382 /* update superblock with uuid */
2383 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2384
Chao Yuc5bda1c2015-06-08 13:28:03 +08002385 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002386 if (err) {
2387 /* undo new data */
2388 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002389 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002390 }
2391got_it:
2392 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2393 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002394 err = -EFAULT;
2395out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002396 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002397 mnt_drop_write_file(filp);
2398 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002399}
2400
Eric Biggers8ce589c2019-08-04 19:35:48 -07002401static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2402 unsigned long arg)
2403{
2404 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2405 return -EOPNOTSUPP;
2406
2407 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2408}
2409
2410static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2411{
2412 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2413 return -EOPNOTSUPP;
2414
2415 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2416}
2417
2418static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2419{
2420 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2421 return -EOPNOTSUPP;
2422
2423 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2424}
2425
2426static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2427 unsigned long arg)
2428{
2429 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2430 return -EOPNOTSUPP;
2431
2432 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2433}
2434
2435static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2436 unsigned long arg)
2437{
2438 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2439 return -EOPNOTSUPP;
2440
2441 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2442}
2443
Eric Biggersee446e12020-03-14 13:50:51 -07002444static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2445{
2446 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2447 return -EOPNOTSUPP;
2448
2449 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2450}
2451
Chao Yuc1c1b582015-07-10 18:08:10 +08002452static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2453{
2454 struct inode *inode = file_inode(filp);
2455 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002456 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002457 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002458
2459 if (!capable(CAP_SYS_ADMIN))
2460 return -EPERM;
2461
Chao Yud530d4d2015-10-05 22:22:44 +08002462 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002463 return -EFAULT;
2464
Chao Yud530d4d2015-10-05 22:22:44 +08002465 if (f2fs_readonly(sbi->sb))
2466 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002467
Chao Yu7fb17fe2016-05-09 19:56:32 +08002468 ret = mnt_want_write_file(filp);
2469 if (ret)
2470 return ret;
2471
Chao Yud530d4d2015-10-05 22:22:44 +08002472 if (!sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002473 if (!down_write_trylock(&sbi->gc_lock)) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002474 ret = -EBUSY;
2475 goto out;
2476 }
Chao Yud530d4d2015-10-05 22:22:44 +08002477 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002478 down_write(&sbi->gc_lock);
Chao Yuc1c1b582015-07-10 18:08:10 +08002479 }
2480
Jaegeuk Kime066b832017-04-13 15:17:00 -07002481 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002482out:
2483 mnt_drop_write_file(filp);
2484 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002485}
2486
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002487static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2488{
2489 struct inode *inode = file_inode(filp);
2490 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2491 struct f2fs_gc_range range;
2492 u64 end;
2493 int ret;
2494
2495 if (!capable(CAP_SYS_ADMIN))
2496 return -EPERM;
2497
2498 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2499 sizeof(range)))
2500 return -EFAULT;
2501
2502 if (f2fs_readonly(sbi->sb))
2503 return -EROFS;
2504
Yunlei Heb82f6e32018-04-24 11:40:30 +08002505 end = range.start + range.len;
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302506 if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
2507 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002508 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002509
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002510 ret = mnt_want_write_file(filp);
2511 if (ret)
2512 return ret;
2513
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002514do_more:
2515 if (!range.sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002516 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002517 ret = -EBUSY;
2518 goto out;
2519 }
2520 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002521 down_write(&sbi->gc_lock);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002522 }
2523
2524 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Yunlong Song67b0e422018-10-30 20:37:55 +08002525 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002526 if (range.start <= end)
2527 goto do_more;
2528out:
2529 mnt_drop_write_file(filp);
2530 return ret;
2531}
2532
Chao Yu059c0642018-07-17 20:41:49 +08002533static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002534{
2535 struct inode *inode = file_inode(filp);
2536 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002537 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002538
2539 if (!capable(CAP_SYS_ADMIN))
2540 return -EPERM;
2541
2542 if (f2fs_readonly(sbi->sb))
2543 return -EROFS;
2544
Daniel Rosenberg43549942018-08-20 19:21:43 -07002545 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002546 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002547 return -EINVAL;
2548 }
2549
Chao Yu7fb17fe2016-05-09 19:56:32 +08002550 ret = mnt_want_write_file(filp);
2551 if (ret)
2552 return ret;
2553
2554 ret = f2fs_sync_fs(sbi->sb, 1);
2555
2556 mnt_drop_write_file(filp);
2557 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002558}
2559
Chao Yud323d002015-10-27 09:53:45 +08002560static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2561 struct file *filp,
2562 struct f2fs_defragment *range)
2563{
2564 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002565 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002566 .m_seg_type = NO_CHECK_TYPE ,
2567 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002568 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002569 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002570 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002571 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002572 block_t blk_end = 0;
2573 bool fragmented = false;
2574 int err;
2575
2576 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002577 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002578 return -EINVAL;
2579
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002580 pg_start = range->start >> PAGE_SHIFT;
2581 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002582
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002583 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002584
Al Viro59551022016-01-22 15:40:57 -05002585 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002586
2587 /* writeback all dirty pages in the range */
2588 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002589 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002590 if (err)
2591 goto out;
2592
2593 /*
2594 * lookup mapping info in extent cache, skip defragmenting if physical
2595 * block addresses are continuous.
2596 */
2597 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2598 if (ei.fofs + ei.len >= pg_end)
2599 goto out;
2600 }
2601
2602 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002603 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002604
2605 /*
2606 * lookup mapping info in dnode page cache, skip defragmenting if all
2607 * physical block addresses are continuous even if there are hole(s)
2608 * in logical blocks.
2609 */
2610 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002611 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002612 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002613 if (err)
2614 goto out;
2615
2616 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002617 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002618 continue;
2619 }
2620
Chao Yu25a912e2018-01-10 18:18:51 +08002621 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002622 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002623
2624 /* record total count of block that we're going to move */
2625 total += map.m_len;
2626
Chao Yud323d002015-10-27 09:53:45 +08002627 blk_end = map.m_pblk + map.m_len;
2628
2629 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002630 }
2631
Chao Yud3a1a0e2019-07-29 23:02:29 +08002632 if (!fragmented) {
2633 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002634 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002635 }
Chao Yud323d002015-10-27 09:53:45 +08002636
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002637 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002638
2639 /*
2640 * make sure there are enough free section for LFS allocation, this can
2641 * avoid defragment running in SSR mode when free section are allocated
2642 * intensively
2643 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002644 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002645 err = -EAGAIN;
2646 goto out;
2647 }
2648
Chao Yu25a912e2018-01-10 18:18:51 +08002649 map.m_lblk = pg_start;
2650 map.m_len = pg_end - pg_start;
2651 total = 0;
2652
Chao Yud323d002015-10-27 09:53:45 +08002653 while (map.m_lblk < pg_end) {
2654 pgoff_t idx;
2655 int cnt = 0;
2656
2657do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002658 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002659 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002660 if (err)
2661 goto clear_out;
2662
2663 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002664 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002665 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002666 }
2667
Jaegeuk Kim91942322016-05-20 10:13:22 -07002668 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002669
2670 idx = map.m_lblk;
2671 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2672 struct page *page;
2673
Chao Yu4d57b862018-05-30 00:20:41 +08002674 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002675 if (IS_ERR(page)) {
2676 err = PTR_ERR(page);
2677 goto clear_out;
2678 }
2679
2680 set_page_dirty(page);
2681 f2fs_put_page(page, 1);
2682
2683 idx++;
2684 cnt++;
2685 total++;
2686 }
2687
2688 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002689check:
2690 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002691 goto do_map;
2692
Jaegeuk Kim91942322016-05-20 10:13:22 -07002693 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002694
2695 err = filemap_fdatawrite(inode->i_mapping);
2696 if (err)
2697 goto out;
2698 }
2699clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002700 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002701out:
Al Viro59551022016-01-22 15:40:57 -05002702 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002703 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002704 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002705 return err;
2706}
2707
2708static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2709{
2710 struct inode *inode = file_inode(filp);
2711 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2712 struct f2fs_defragment range;
2713 int err;
2714
2715 if (!capable(CAP_SYS_ADMIN))
2716 return -EPERM;
2717
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002718 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002719 return -EINVAL;
2720
Kinglong Meed7563862017-03-10 17:55:07 +08002721 if (f2fs_readonly(sbi->sb))
2722 return -EROFS;
2723
2724 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2725 sizeof(range)))
2726 return -EFAULT;
2727
2728 /* verify alignment of offset & size */
2729 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2730 return -EINVAL;
2731
2732 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2733 sbi->max_file_blocks))
2734 return -EINVAL;
2735
Chao Yud323d002015-10-27 09:53:45 +08002736 err = mnt_want_write_file(filp);
2737 if (err)
2738 return err;
2739
Chao Yud323d002015-10-27 09:53:45 +08002740 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002741 mnt_drop_write_file(filp);
2742
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002743 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002744 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002745 return err;
Chao Yud323d002015-10-27 09:53:45 +08002746
2747 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2748 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002749 return -EFAULT;
2750
2751 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002752}
2753
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002754static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2755 struct file *file_out, loff_t pos_out, size_t len)
2756{
2757 struct inode *src = file_inode(file_in);
2758 struct inode *dst = file_inode(file_out);
2759 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2760 size_t olen = len, dst_max_i_size = 0;
2761 size_t dst_osize;
2762 int ret;
2763
2764 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2765 src->i_sb != dst->i_sb)
2766 return -EXDEV;
2767
2768 if (unlikely(f2fs_readonly(src->i_sb)))
2769 return -EROFS;
2770
Chao Yufe8494b2016-08-04 20:13:02 +08002771 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2772 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002773
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302774 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002775 return -EOPNOTSUPP;
2776
Fan Lid95fd912016-09-13 11:35:42 +08002777 if (src == dst) {
2778 if (pos_in == pos_out)
2779 return 0;
2780 if (pos_out > pos_in && pos_out < pos_in + len)
2781 return -EINVAL;
2782 }
2783
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002784 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002785 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002786 ret = -EBUSY;
2787 if (!inode_trylock(dst))
2788 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002789 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002790
2791 ret = -EINVAL;
2792 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2793 goto out_unlock;
2794 if (len == 0)
2795 olen = len = src->i_size - pos_in;
2796 if (pos_in + len == src->i_size)
2797 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2798 if (len == 0) {
2799 ret = 0;
2800 goto out_unlock;
2801 }
2802
2803 dst_osize = dst->i_size;
2804 if (pos_out + olen > dst->i_size)
2805 dst_max_i_size = pos_out + olen;
2806
2807 /* verify the end result is block aligned */
2808 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2809 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2810 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2811 goto out_unlock;
2812
2813 ret = f2fs_convert_inline_inode(src);
2814 if (ret)
2815 goto out_unlock;
2816
2817 ret = f2fs_convert_inline_inode(dst);
2818 if (ret)
2819 goto out_unlock;
2820
2821 /* write out all dirty pages from offset */
2822 ret = filemap_write_and_wait_range(src->i_mapping,
2823 pos_in, pos_in + len);
2824 if (ret)
2825 goto out_unlock;
2826
2827 ret = filemap_write_and_wait_range(dst->i_mapping,
2828 pos_out, pos_out + len);
2829 if (ret)
2830 goto out_unlock;
2831
2832 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002833
2834 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2835 if (src != dst) {
2836 ret = -EBUSY;
2837 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2838 goto out_src;
2839 }
2840
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002841 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002842 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2843 pos_out >> F2FS_BLKSIZE_BITS,
2844 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002845
2846 if (!ret) {
2847 if (dst_max_i_size)
2848 f2fs_i_size_write(dst, dst_max_i_size);
2849 else if (dst_osize != dst->i_size)
2850 f2fs_i_size_write(dst, dst_osize);
2851 }
2852 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002853
2854 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002855 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002856out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002857 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002858out_unlock:
2859 if (src != dst)
2860 inode_unlock(dst);
2861out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002862 inode_unlock(src);
2863 return ret;
2864}
2865
2866static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2867{
2868 struct f2fs_move_range range;
2869 struct fd dst;
2870 int err;
2871
2872 if (!(filp->f_mode & FMODE_READ) ||
2873 !(filp->f_mode & FMODE_WRITE))
2874 return -EBADF;
2875
2876 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2877 sizeof(range)))
2878 return -EFAULT;
2879
2880 dst = fdget(range.dst_fd);
2881 if (!dst.file)
2882 return -EBADF;
2883
2884 if (!(dst.file->f_mode & FMODE_WRITE)) {
2885 err = -EBADF;
2886 goto err_out;
2887 }
2888
2889 err = mnt_want_write_file(filp);
2890 if (err)
2891 goto err_out;
2892
2893 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2894 range.pos_out, range.len);
2895
2896 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002897 if (err)
2898 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002899
2900 if (copy_to_user((struct f2fs_move_range __user *)arg,
2901 &range, sizeof(range)))
2902 err = -EFAULT;
2903err_out:
2904 fdput(dst);
2905 return err;
2906}
2907
Jaegeuk Kime066b832017-04-13 15:17:00 -07002908static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2909{
2910 struct inode *inode = file_inode(filp);
2911 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2912 struct sit_info *sm = SIT_I(sbi);
2913 unsigned int start_segno = 0, end_segno = 0;
2914 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2915 struct f2fs_flush_device range;
2916 int ret;
2917
2918 if (!capable(CAP_SYS_ADMIN))
2919 return -EPERM;
2920
2921 if (f2fs_readonly(sbi->sb))
2922 return -EROFS;
2923
Daniel Rosenberg43549942018-08-20 19:21:43 -07002924 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2925 return -EINVAL;
2926
Jaegeuk Kime066b832017-04-13 15:17:00 -07002927 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2928 sizeof(range)))
2929 return -EFAULT;
2930
Damien Le Moal09168782019-03-16 09:13:06 +09002931 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002932 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002933 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2934 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002935 return -EINVAL;
2936 }
2937
2938 ret = mnt_want_write_file(filp);
2939 if (ret)
2940 return ret;
2941
2942 if (range.dev_num != 0)
2943 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2944 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2945
2946 start_segno = sm->last_victim[FLUSH_DEVICE];
2947 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2948 start_segno = dev_start_segno;
2949 end_segno = min(start_segno + range.segments, dev_end_segno);
2950
2951 while (start_segno < end_segno) {
Chao Yufb24fea2020-01-14 19:36:50 +08002952 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002953 ret = -EBUSY;
2954 goto out;
2955 }
2956 sm->last_victim[GC_CB] = end_segno + 1;
2957 sm->last_victim[GC_GREEDY] = end_segno + 1;
2958 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2959 ret = f2fs_gc(sbi, true, true, start_segno);
2960 if (ret == -EAGAIN)
2961 ret = 0;
2962 else if (ret < 0)
2963 break;
2964 start_segno++;
2965 }
2966out:
2967 mnt_drop_write_file(filp);
2968 return ret;
2969}
2970
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002971static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2972{
2973 struct inode *inode = file_inode(filp);
2974 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2975
2976 /* Must validate to set it with SQLite behavior in Android. */
2977 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2978
2979 return put_user(sb_feature, (u32 __user *)arg);
2980}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002981
Chao Yu2c1d0302017-07-29 00:32:52 +08002982#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002983int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2984{
2985 struct dquot *transfer_to[MAXQUOTAS] = {};
2986 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2987 struct super_block *sb = sbi->sb;
2988 int err = 0;
2989
2990 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2991 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2992 err = __dquot_transfer(inode, transfer_to);
2993 if (err)
2994 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2995 dqput(transfer_to[PRJQUOTA]);
2996 }
2997 return err;
2998}
2999
Chao Yu2c1d0302017-07-29 00:32:52 +08003000static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3001{
3002 struct inode *inode = file_inode(filp);
3003 struct f2fs_inode_info *fi = F2FS_I(inode);
3004 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003005 struct page *ipage;
3006 kprojid_t kprojid;
3007 int err;
3008
Chao Yu7beb01f2018-10-24 18:34:26 +08003009 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08003010 if (projid != F2FS_DEF_PROJID)
3011 return -EOPNOTSUPP;
3012 else
3013 return 0;
3014 }
3015
3016 if (!f2fs_has_extra_attr(inode))
3017 return -EOPNOTSUPP;
3018
3019 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3020
3021 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3022 return 0;
3023
Chao Yu2c1d0302017-07-29 00:32:52 +08003024 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003025 /* Is it quota file? Do not allow user to mess with it */
3026 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003027 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003028
Chao Yu4d57b862018-05-30 00:20:41 +08003029 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003030 if (IS_ERR(ipage))
3031 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003032
3033 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3034 i_projid)) {
3035 err = -EOVERFLOW;
3036 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003037 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003038 }
3039 f2fs_put_page(ipage, 1);
3040
Chao Yuc22aecd2018-04-21 17:53:52 +08003041 err = dquot_initialize(inode);
3042 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003043 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003044
Chao Yu78130812018-09-25 15:36:02 +08003045 f2fs_lock_op(sbi);
3046 err = f2fs_transfer_project_quota(inode, kprojid);
3047 if (err)
3048 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003049
3050 F2FS_I(inode)->i_projid = kprojid;
3051 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003052 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003053out_unlock:
3054 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003055 return err;
3056}
3057#else
Chao Yu78130812018-09-25 15:36:02 +08003058int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3059{
3060 return 0;
3061}
3062
Chao Yu2c1d0302017-07-29 00:32:52 +08003063static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3064{
3065 if (projid != F2FS_DEF_PROJID)
3066 return -EOPNOTSUPP;
3067 return 0;
3068}
3069#endif
3070
Eric Biggers36098552019-06-04 22:59:04 -07003071/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08003072
Eric Biggers36098552019-06-04 22:59:04 -07003073/*
3074 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3075 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3076 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3077 */
3078
3079static const struct {
3080 u32 iflag;
3081 u32 xflag;
3082} f2fs_xflags_map[] = {
3083 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3084 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3085 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3086 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3087 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3088 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3089};
3090
3091#define F2FS_SUPPORTED_XFLAGS ( \
3092 FS_XFLAG_SYNC | \
3093 FS_XFLAG_IMMUTABLE | \
3094 FS_XFLAG_APPEND | \
3095 FS_XFLAG_NODUMP | \
3096 FS_XFLAG_NOATIME | \
3097 FS_XFLAG_PROJINHERIT)
3098
3099/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3100static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3101{
3102 u32 xflags = 0;
3103 int i;
3104
3105 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3106 if (iflags & f2fs_xflags_map[i].iflag)
3107 xflags |= f2fs_xflags_map[i].xflag;
3108
Chao Yu2c1d0302017-07-29 00:32:52 +08003109 return xflags;
3110}
3111
Eric Biggers36098552019-06-04 22:59:04 -07003112/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3113static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003114{
Eric Biggers36098552019-06-04 22:59:04 -07003115 u32 iflags = 0;
3116 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08003117
Eric Biggers36098552019-06-04 22:59:04 -07003118 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3119 if (xflags & f2fs_xflags_map[i].xflag)
3120 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08003121
3122 return iflags;
3123}
3124
Eric Biggers6fc93c42019-07-01 13:26:29 -07003125static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3126{
3127 struct f2fs_inode_info *fi = F2FS_I(inode);
3128
3129 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3130
3131 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3132 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3133}
3134
Chao Yu2c1d0302017-07-29 00:32:52 +08003135static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3136{
3137 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08003138 struct fsxattr fa;
3139
Eric Biggers6fc93c42019-07-01 13:26:29 -07003140 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08003141
3142 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3143 return -EFAULT;
3144 return 0;
3145}
3146
3147static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3148{
3149 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003150 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07003151 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003152 int err;
3153
3154 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3155 return -EFAULT;
3156
3157 /* Make sure caller has proper permission */
3158 if (!inode_owner_or_capable(inode))
3159 return -EACCES;
3160
Eric Biggers36098552019-06-04 22:59:04 -07003161 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08003162 return -EOPNOTSUPP;
3163
Eric Biggers36098552019-06-04 22:59:04 -07003164 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3165 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003166 return -EOPNOTSUPP;
3167
3168 err = mnt_want_write_file(filp);
3169 if (err)
3170 return err;
3171
3172 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003173
3174 f2fs_fill_fsxattr(inode, &old_fa);
3175 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09003176 if (err)
3177 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07003178
Eric Biggers36098552019-06-04 22:59:04 -07003179 err = f2fs_setflags_common(inode, iflags,
3180 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08003181 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003182 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08003183
3184 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09003185out:
3186 inode_unlock(inode);
3187 mnt_drop_write_file(filp);
3188 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003189}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003190
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003191int f2fs_pin_file_control(struct inode *inode, bool inc)
3192{
3193 struct f2fs_inode_info *fi = F2FS_I(inode);
3194 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3195
3196 /* Use i_gc_failures for normal file as a risk signal. */
3197 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003198 f2fs_i_gc_failures_write(inode,
3199 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003200
Chao Yu2ef79ec2018-05-07 20:28:54 +08003201 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003202 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3203 __func__, inode->i_ino,
3204 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003205 clear_inode_flag(inode, FI_PIN_FILE);
3206 return -EAGAIN;
3207 }
3208 return 0;
3209}
3210
3211static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3212{
3213 struct inode *inode = file_inode(filp);
3214 __u32 pin;
3215 int ret = 0;
3216
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003217 if (get_user(pin, (__u32 __user *)arg))
3218 return -EFAULT;
3219
3220 if (!S_ISREG(inode->i_mode))
3221 return -EINVAL;
3222
3223 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3224 return -EROFS;
3225
3226 ret = mnt_want_write_file(filp);
3227 if (ret)
3228 return ret;
3229
3230 inode_lock(inode);
3231
Chao Yu4d57b862018-05-30 00:20:41 +08003232 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08003233 ret = -EINVAL;
3234 goto out;
3235 }
3236
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003237 if (!pin) {
3238 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003239 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003240 goto done;
3241 }
3242
3243 if (f2fs_pin_file_control(inode, false)) {
3244 ret = -EAGAIN;
3245 goto out;
3246 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003247
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003248 ret = f2fs_convert_inline_inode(inode);
3249 if (ret)
3250 goto out;
3251
Chao Yu4c8ff702019-11-01 18:07:14 +08003252 if (f2fs_disable_compressed_file(inode)) {
3253 ret = -EOPNOTSUPP;
3254 goto out;
3255 }
3256
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003257 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003258 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003259done:
3260 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3261out:
3262 inode_unlock(inode);
3263 mnt_drop_write_file(filp);
3264 return ret;
3265}
3266
3267static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3268{
3269 struct inode *inode = file_inode(filp);
3270 __u32 pin = 0;
3271
3272 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003273 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003274 return put_user(pin, (u32 __user *)arg);
3275}
3276
Chao Yuc4020b22018-01-11 14:42:30 +08003277int f2fs_precache_extents(struct inode *inode)
3278{
3279 struct f2fs_inode_info *fi = F2FS_I(inode);
3280 struct f2fs_map_blocks map;
3281 pgoff_t m_next_extent;
3282 loff_t end;
3283 int err;
3284
3285 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3286 return -EOPNOTSUPP;
3287
3288 map.m_lblk = 0;
3289 map.m_next_pgofs = NULL;
3290 map.m_next_extent = &m_next_extent;
3291 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003292 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003293 end = F2FS_I_SB(inode)->max_file_blocks;
3294
3295 while (map.m_lblk < end) {
3296 map.m_len = end - map.m_lblk;
3297
Chao Yub2532c62018-04-24 10:55:28 +08003298 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003299 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003300 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003301 if (err)
3302 return err;
3303
3304 map.m_lblk = m_next_extent;
3305 }
3306
3307 return err;
3308}
3309
3310static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3311{
3312 return f2fs_precache_extents(file_inode(filp));
3313}
3314
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003315static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3316{
3317 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3318 __u64 block_count;
3319 int ret;
3320
3321 if (!capable(CAP_SYS_ADMIN))
3322 return -EPERM;
3323
3324 if (f2fs_readonly(sbi->sb))
3325 return -EROFS;
3326
3327 if (copy_from_user(&block_count, (void __user *)arg,
3328 sizeof(block_count)))
3329 return -EFAULT;
3330
3331 ret = f2fs_resize_fs(sbi, block_count);
3332
3333 return ret;
3334}
3335
Eric Biggers95ae2512019-07-22 09:26:24 -07003336static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3337{
3338 struct inode *inode = file_inode(filp);
3339
3340 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3341
3342 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3343 f2fs_warn(F2FS_I_SB(inode),
3344 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3345 inode->i_ino);
3346 return -EOPNOTSUPP;
3347 }
3348
3349 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3350}
3351
3352static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3353{
3354 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3355 return -EOPNOTSUPP;
3356
3357 return fsverity_ioctl_measure(filp, (void __user *)arg);
3358}
3359
Chao Yu4507847c2019-07-17 17:06:11 +08003360static int f2fs_get_volume_name(struct file *filp, unsigned long arg)
3361{
3362 struct inode *inode = file_inode(filp);
3363 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3364 char *vbuf;
3365 int count;
3366 int err = 0;
3367
3368 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3369 if (!vbuf)
3370 return -ENOMEM;
3371
3372 down_read(&sbi->sb_lock);
3373 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3374 ARRAY_SIZE(sbi->raw_super->volume_name),
3375 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3376 up_read(&sbi->sb_lock);
3377
3378 if (copy_to_user((char __user *)arg, vbuf,
3379 min(FSLABEL_MAX, count)))
3380 err = -EFAULT;
3381
3382 kvfree(vbuf);
3383 return err;
3384}
3385
3386static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
3387{
3388 struct inode *inode = file_inode(filp);
3389 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3390 char *vbuf;
3391 int err = 0;
3392
3393 if (!capable(CAP_SYS_ADMIN))
3394 return -EPERM;
3395
3396 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3397 if (IS_ERR(vbuf))
3398 return PTR_ERR(vbuf);
3399
3400 err = mnt_want_write_file(filp);
3401 if (err)
3402 goto out;
3403
3404 down_write(&sbi->sb_lock);
3405
3406 memset(sbi->raw_super->volume_name, 0,
3407 sizeof(sbi->raw_super->volume_name));
3408 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3409 sbi->raw_super->volume_name,
3410 ARRAY_SIZE(sbi->raw_super->volume_name));
3411
3412 err = f2fs_commit_super(sbi, false);
3413
3414 up_write(&sbi->sb_lock);
3415
3416 mnt_drop_write_file(filp);
3417out:
3418 kfree(vbuf);
3419 return err;
3420}
3421
Chao Yu439dfb12020-02-21 18:09:21 +08003422static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3423{
3424 struct inode *inode = file_inode(filp);
3425 __u64 blocks;
3426
3427 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3428 return -EOPNOTSUPP;
3429
3430 if (!f2fs_compressed_file(inode))
3431 return -EINVAL;
3432
3433 blocks = F2FS_I(inode)->i_compr_blocks;
3434 return put_user(blocks, (u64 __user *)arg);
3435}
3436
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003437long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3438{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003439 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3440 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08003441 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
3442 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003443
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003444 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003445 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003446 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003447 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003448 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08003449 case F2FS_IOC_GETVERSION:
3450 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003451 case F2FS_IOC_START_ATOMIC_WRITE:
3452 return f2fs_ioc_start_atomic_write(filp);
3453 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3454 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07003455 case F2FS_IOC_START_VOLATILE_WRITE:
3456 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08003457 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3458 return f2fs_ioc_release_volatile_write(filp);
3459 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3460 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08003461 case F2FS_IOC_SHUTDOWN:
3462 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003463 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003464 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003465 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3466 return f2fs_ioc_set_encryption_policy(filp, arg);
3467 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3468 return f2fs_ioc_get_encryption_policy(filp, arg);
3469 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3470 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07003471 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3472 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3473 case FS_IOC_ADD_ENCRYPTION_KEY:
3474 return f2fs_ioc_add_encryption_key(filp, arg);
3475 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3476 return f2fs_ioc_remove_encryption_key(filp, arg);
3477 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3478 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
3479 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3480 return f2fs_ioc_get_encryption_key_status(filp, arg);
Eric Biggersee446e12020-03-14 13:50:51 -07003481 case FS_IOC_GET_ENCRYPTION_NONCE:
3482 return f2fs_ioc_get_encryption_nonce(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003483 case F2FS_IOC_GARBAGE_COLLECT:
3484 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003485 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3486 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08003487 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08003488 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08003489 case F2FS_IOC_DEFRAGMENT:
3490 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003491 case F2FS_IOC_MOVE_RANGE:
3492 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07003493 case F2FS_IOC_FLUSH_DEVICE:
3494 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003495 case F2FS_IOC_GET_FEATURES:
3496 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08003497 case F2FS_IOC_FSGETXATTR:
3498 return f2fs_ioc_fsgetxattr(filp, arg);
3499 case F2FS_IOC_FSSETXATTR:
3500 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003501 case F2FS_IOC_GET_PIN_FILE:
3502 return f2fs_ioc_get_pin_file(filp, arg);
3503 case F2FS_IOC_SET_PIN_FILE:
3504 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08003505 case F2FS_IOC_PRECACHE_EXTENTS:
3506 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003507 case F2FS_IOC_RESIZE_FS:
3508 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07003509 case FS_IOC_ENABLE_VERITY:
3510 return f2fs_ioc_enable_verity(filp, arg);
3511 case FS_IOC_MEASURE_VERITY:
3512 return f2fs_ioc_measure_verity(filp, arg);
Chao Yu4507847c2019-07-17 17:06:11 +08003513 case F2FS_IOC_GET_VOLUME_NAME:
3514 return f2fs_get_volume_name(filp, arg);
3515 case F2FS_IOC_SET_VOLUME_NAME:
3516 return f2fs_set_volume_name(filp, arg);
Chao Yu439dfb12020-02-21 18:09:21 +08003517 case F2FS_IOC_GET_COMPRESS_BLOCKS:
3518 return f2fs_get_compress_blocks(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003519 default:
3520 return -ENOTTY;
3521 }
3522}
3523
Chao Yu4c8ff702019-11-01 18:07:14 +08003524static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
3525{
3526 struct file *file = iocb->ki_filp;
3527 struct inode *inode = file_inode(file);
Chao Yu8b83ac82020-04-16 18:16:56 +08003528 int ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08003529
3530 if (!f2fs_is_compress_backend_ready(inode))
3531 return -EOPNOTSUPP;
3532
Chao Yu8b83ac82020-04-16 18:16:56 +08003533 ret = generic_file_read_iter(iocb, iter);
3534
3535 if (ret > 0)
3536 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
3537
3538 return ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08003539}
3540
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003541static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3542{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003543 struct file *file = iocb->ki_filp;
3544 struct inode *inode = file_inode(file);
3545 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003546
Chao Yu126ce722019-04-02 18:52:22 +08003547 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
3548 ret = -EIO;
3549 goto out;
3550 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003551
Chao Yu7bd29352020-02-24 19:20:15 +08003552 if (!f2fs_is_compress_backend_ready(inode)) {
3553 ret = -EOPNOTSUPP;
3554 goto out;
3555 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003556
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003557 if (iocb->ki_flags & IOCB_NOWAIT) {
3558 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08003559 ret = -EAGAIN;
3560 goto out;
3561 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003562 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003563 inode_lock(inode);
3564 }
3565
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003566 ret = generic_write_checks(iocb, from);
3567 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003568 bool preallocated = false;
3569 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003570 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003571
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003572 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3573 set_inode_flag(inode, FI_NO_PREALLOC);
3574
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003575 if ((iocb->ki_flags & IOCB_NOWAIT)) {
3576 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003577 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003578 f2fs_has_inline_data(inode) ||
3579 f2fs_force_buffered_io(inode, iocb, from)) {
3580 clear_inode_flag(inode, FI_NO_PREALLOC);
3581 inode_unlock(inode);
3582 ret = -EAGAIN;
3583 goto out;
3584 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003585 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003586 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003587
3588 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
3589 goto write;
3590
3591 if (iocb->ki_flags & IOCB_DIRECT) {
3592 /*
3593 * Convert inline data for Direct I/O before entering
3594 * f2fs_direct_IO().
3595 */
3596 err = f2fs_convert_inline_inode(inode);
3597 if (err)
3598 goto out_err;
3599 /*
3600 * If force_buffere_io() is true, we have to allocate
3601 * blocks all the time, since f2fs_direct_IO will fall
3602 * back to buffered IO.
3603 */
3604 if (!f2fs_force_buffered_io(inode, iocb, from) &&
3605 allow_outplace_dio(inode, iocb, from))
3606 goto write;
3607 }
3608 preallocated = true;
3609 target_size = iocb->ki_pos + iov_iter_count(from);
3610
3611 err = f2fs_preallocate_blocks(iocb, from);
3612 if (err) {
3613out_err:
3614 clear_inode_flag(inode, FI_NO_PREALLOC);
3615 inode_unlock(inode);
3616 ret = err;
3617 goto out;
3618 }
3619write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003620 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003621 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003622
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003623 /* if we couldn't write data, we should deallocate blocks. */
3624 if (preallocated && i_size_read(inode) < target_size)
3625 f2fs_truncate(inode);
3626
Chao Yub0af6d42017-08-02 23:21:48 +08003627 if (ret > 0)
3628 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003629 }
3630 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08003631out:
3632 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
3633 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07003634 if (ret > 0)
3635 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003636 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003637}
3638
Namjae Jeone9750822013-02-04 23:41:41 +09003639#ifdef CONFIG_COMPAT
3640long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3641{
3642 switch (cmd) {
3643 case F2FS_IOC32_GETFLAGS:
3644 cmd = F2FS_IOC_GETFLAGS;
3645 break;
3646 case F2FS_IOC32_SETFLAGS:
3647 cmd = F2FS_IOC_SETFLAGS;
3648 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003649 case F2FS_IOC32_GETVERSION:
3650 cmd = F2FS_IOC_GETVERSION;
3651 break;
3652 case F2FS_IOC_START_ATOMIC_WRITE:
3653 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3654 case F2FS_IOC_START_VOLATILE_WRITE:
3655 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3656 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3657 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02003658 case FITRIM:
Chao Yu04ef4b62015-11-10 18:44:20 +08003659 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3660 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3661 case F2FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07003662 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3663 case FS_IOC_ADD_ENCRYPTION_KEY:
3664 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3665 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3666 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Eric Biggersee446e12020-03-14 13:50:51 -07003667 case FS_IOC_GET_ENCRYPTION_NONCE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003668 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003669 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003670 case F2FS_IOC_WRITE_CHECKPOINT:
3671 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003672 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003673 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003674 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003675 case F2FS_IOC_FSGETXATTR:
3676 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003677 case F2FS_IOC_GET_PIN_FILE:
3678 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003679 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003680 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07003681 case FS_IOC_ENABLE_VERITY:
3682 case FS_IOC_MEASURE_VERITY:
Chao Yu4507847c2019-07-17 17:06:11 +08003683 case F2FS_IOC_GET_VOLUME_NAME:
3684 case F2FS_IOC_SET_VOLUME_NAME:
Chao Yu439dfb12020-02-21 18:09:21 +08003685 case F2FS_IOC_GET_COMPRESS_BLOCKS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003686 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003687 default:
3688 return -ENOIOCTLCMD;
3689 }
3690 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3691}
3692#endif
3693
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003694const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003695 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08003696 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003697 .write_iter = f2fs_file_write_iter,
3698 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003699 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003700 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003701 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003702 .fsync = f2fs_sync_file,
3703 .fallocate = f2fs_fallocate,
3704 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003705#ifdef CONFIG_COMPAT
3706 .compat_ioctl = f2fs_compat_ioctl,
3707#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003708 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003709 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003710};