blob: 521987cd8772f29f604bed4fde0c9a72e4c9e692 [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 */
Chao Yu0ef81832020-06-18 14:36:22 +0800108 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +0800109 set_new_dnode(&dn, inode, NULL, NULL, 0);
110 err = f2fs_get_block(&dn, page->index);
111 f2fs_put_dnode(&dn);
Chao Yu0ef81832020-06-18 14:36:22 +0800112 f2fs_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 Yuef8d5632020-03-06 15:36:09 +0800566 bool released = !F2FS_I(dn->inode)->i_compr_blocks;
Chao Yu7a2af762017-07-19 00:19:06 +0800567
568 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
569 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900570
Gu Zheng45590712013-07-15 17:57:38 +0800571 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800572 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900573
Chao Yu4c8ff702019-11-01 18:07:14 +0800574 /* Assumption: truncateion starts with cluster */
575 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900576 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900577
Chao Yu4c8ff702019-11-01 18:07:14 +0800578 if (f2fs_compressed_file(dn->inode) &&
579 !(cluster_index & (cluster_size - 1))) {
580 if (compressed_cluster)
581 f2fs_i_compr_blocks_update(dn->inode,
582 valid_blocks, false);
583 compressed_cluster = (blkaddr == COMPRESS_ADDR);
584 valid_blocks = 0;
585 }
586
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900587 if (blkaddr == NULL_ADDR)
588 continue;
589
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800590 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800591 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800592
Chao Yu4c8ff702019-11-01 18:07:14 +0800593 if (__is_valid_data_blkaddr(blkaddr)) {
594 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800595 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800596 continue;
597 if (compressed_cluster)
598 valid_blocks++;
599 }
Chao Yuc9b60782018-08-01 19:13:44 +0800600
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700601 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700602 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800603
604 f2fs_invalidate_blocks(sbi, blkaddr);
Chao Yuef8d5632020-03-06 15:36:09 +0800605
606 if (!released || blkaddr != COMPRESS_ADDR)
607 nr_free++;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900608 }
Chao Yu19b2c302015-08-26 20:34:48 +0800609
Chao Yu4c8ff702019-11-01 18:07:14 +0800610 if (compressed_cluster)
611 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
612
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900613 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800614 pgoff_t fofs;
615 /*
616 * once we invalidate valid blkaddr in range [ofs, ofs + count],
617 * we will invalidate all blkaddr in the whole range.
618 */
Chao Yu4d57b862018-05-30 00:20:41 +0800619 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800620 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800621 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900622 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900623 }
624 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900625
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800626 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900627 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
628 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900629}
630
Chao Yu4d57b862018-05-30 00:20:41 +0800631void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900632{
Chao Yud02a6e62019-03-25 21:08:19 +0800633 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900634}
635
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800636static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700637 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900638{
youngjun yoo193bea12018-05-30 04:21:14 +0900639 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300640 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700641 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900642 struct page *page;
643
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700644 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700645 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900646
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700647 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700648 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700649 if (page && PageUptodate(page))
650 goto truncate_out;
651 f2fs_put_page(page, 1);
652 return 0;
653 }
654
Chao Yu4d57b862018-05-30 00:20:41 +0800655 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800657 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700658truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800659 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300660 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700661
662 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530663 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700664 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800665 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900666 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700667 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668}
669
Chao Yu3265d3d2020-03-18 16:22:59 +0800670int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900671{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700672 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900673 struct dnode_of_data dn;
674 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800675 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700676 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800677 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900678
Namjae Jeon51dd6242013-04-20 01:28:52 +0900679 trace_f2fs_truncate_blocks_enter(inode, from);
680
Chao Yudf033ca2018-03-20 23:08:29 +0800681 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900682
Chao Yu09210c92016-05-05 19:13:03 +0800683 if (free_from >= sbi->max_file_blocks)
684 goto free_partial;
685
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700686 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800687 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800688
Chao Yu4d57b862018-05-30 00:20:41 +0800689 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700690 if (IS_ERR(ipage)) {
691 err = PTR_ERR(ipage);
692 goto out;
693 }
694
695 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800696 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700697 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800698 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700699 goto out;
700 }
701
702 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800703 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900704 if (err) {
705 if (err == -ENOENT)
706 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700707 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700708 }
709
Chao Yu81ca7352016-01-26 15:39:35 +0800710 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900711
712 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700713 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900714
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900715 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800716 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900717 free_from += count;
718 }
719
720 f2fs_put_dnode(&dn);
721free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800722 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800723out:
724 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800725 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800726free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700727 /* lastly zero out the first data page */
728 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800729 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900730
Namjae Jeon51dd6242013-04-20 01:28:52 +0900731 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900732 return err;
733}
734
Chao Yu4c8ff702019-11-01 18:07:14 +0800735int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
736{
737 u64 free_from = from;
Chao Yu3265d3d2020-03-18 16:22:59 +0800738 int err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800739
Chao Yu3265d3d2020-03-18 16:22:59 +0800740#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800741 /*
742 * for compressed file, only support cluster size
743 * aligned truncation.
744 */
Chao Yu4fec3fc02020-04-08 19:55:17 +0800745 if (f2fs_compressed_file(inode))
746 free_from = round_up(from,
747 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
Chao Yu3265d3d2020-03-18 16:22:59 +0800748#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800749
Chao Yu3265d3d2020-03-18 16:22:59 +0800750 err = f2fs_do_truncate_blocks(inode, free_from, lock);
751 if (err)
752 return err;
753
754#ifdef CONFIG_F2FS_FS_COMPRESSION
755 if (from != free_from)
756 err = f2fs_truncate_partial_cluster(inode, from, lock);
757#endif
758
759 return err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800760}
761
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700762int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900763{
Chao Yub0154892015-08-24 17:39:42 +0800764 int err;
765
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200766 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
767 return -EIO;
768
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900769 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
770 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800771 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900772
Namjae Jeon51dd6242013-04-20 01:28:52 +0900773 trace_f2fs_truncate(inode);
774
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800775 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800776 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800777 return -EIO;
778 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200779
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800780 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800781 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800782 err = f2fs_convert_inline_inode(inode);
783 if (err)
784 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800785 }
786
Chao Yuc42d28c2019-02-02 17:33:01 +0800787 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800788 if (err)
789 return err;
790
Deepa Dinamani078cd822016-09-14 07:48:04 -0700791 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700792 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800793 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900794}
795
David Howellsa528d352017-01-31 16:46:22 +0000796int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800797 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900798{
David Howellsa528d352017-01-31 16:46:22 +0000799 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800800 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800801 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800802 unsigned int flags;
803
Chao Yu1c1d35d2018-01-25 14:54:42 +0800804 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800805 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800806 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
807 stat->result_mask |= STATX_BTIME;
808 stat->btime.tv_sec = fi->i_crtime.tv_sec;
809 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
810 }
811
Eric Biggers36098552019-06-04 22:59:04 -0700812 flags = fi->i_flags;
Chao Yufd267252020-03-25 10:22:09 +0800813 if (flags & F2FS_COMPR_FL)
814 stat->attributes |= STATX_ATTR_COMPRESSED;
Chao Yu59c84402018-04-03 15:08:17 +0800815 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800816 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530817 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800818 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800819 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800820 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800821 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800822 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700823 if (IS_VERITY(inode))
824 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800825
Chao Yufd267252020-03-25 10:22:09 +0800826 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
827 STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800828 STATX_ATTR_ENCRYPTED |
829 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700830 STATX_ATTR_NODUMP |
831 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800832
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900833 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700834
835 /* we need to show initial sectors used for inline_data/dentries */
836 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
837 f2fs_has_inline_dentry(inode))
838 stat->blocks += (stat->size + 511) >> 9;
839
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900840 return 0;
841}
842
843#ifdef CONFIG_F2FS_FS_POSIX_ACL
844static void __setattr_copy(struct inode *inode, const struct iattr *attr)
845{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900846 unsigned int ia_valid = attr->ia_valid;
847
848 if (ia_valid & ATTR_UID)
849 inode->i_uid = attr->ia_uid;
850 if (ia_valid & ATTR_GID)
851 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200852 if (ia_valid & ATTR_ATIME)
853 inode->i_atime = attr->ia_atime;
854 if (ia_valid & ATTR_MTIME)
855 inode->i_mtime = attr->ia_mtime;
856 if (ia_valid & ATTR_CTIME)
857 inode->i_ctime = attr->ia_ctime;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900858 if (ia_valid & ATTR_MODE) {
859 umode_t mode = attr->ia_mode;
860
861 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
862 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700863 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900864 }
865}
866#else
867#define __setattr_copy setattr_copy
868#endif
869
870int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
871{
David Howells2b0143b2015-03-17 22:25:59 +0000872 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900873 int err;
874
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200875 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
876 return -EIO;
877
Chao Yu4c8ff702019-11-01 18:07:14 +0800878 if ((attr->ia_valid & ATTR_SIZE) &&
879 !f2fs_is_compress_backend_ready(inode))
880 return -EOPNOTSUPP;
881
Jan Kara31051c82016-05-26 16:55:18 +0200882 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900883 if (err)
884 return err;
885
Eric Biggers20bb2472017-11-29 12:35:32 -0800886 err = fscrypt_prepare_setattr(dentry, attr);
887 if (err)
888 return err;
889
Eric Biggers95ae2512019-07-22 09:26:24 -0700890 err = fsverity_prepare_setattr(dentry, attr);
891 if (err)
892 return err;
893
Chao Yu0abd6752017-07-09 00:13:07 +0800894 if (is_quota_modification(inode, attr)) {
895 err = dquot_initialize(inode);
896 if (err)
897 return err;
898 }
899 if ((attr->ia_valid & ATTR_UID &&
900 !uid_eq(attr->ia_uid, inode->i_uid)) ||
901 (attr->ia_valid & ATTR_GID &&
902 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800903 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800904 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800905 if (err) {
906 set_sbi_flag(F2FS_I_SB(inode),
907 SBI_QUOTA_NEED_REPAIR);
908 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800909 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800910 }
911 /*
912 * update uid/gid under lock_op(), so that dquot and inode can
913 * be updated atomically.
914 */
915 if (attr->ia_valid & ATTR_UID)
916 inode->i_uid = attr->ia_uid;
917 if (attr->ia_valid & ATTR_GID)
918 inode->i_gid = attr->ia_gid;
919 f2fs_mark_inode_dirty_sync(inode, true);
920 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800921 }
922
Chao Yu09db6a22014-09-15 18:02:09 +0800923 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800924 loff_t old_size = i_size_read(inode);
925
926 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
927 /*
928 * should convert inline inode before i_size_write to
929 * keep smaller than inline_data size with inline flag.
930 */
931 err = f2fs_convert_inline_inode(inode);
932 if (err)
933 return err;
934 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800935
Chao Yua33c1502018-08-05 23:04:25 +0800936 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900937 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800938
939 truncate_setsize(inode, attr->ia_size);
940
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800941 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800942 err = f2fs_truncate(inode);
943 /*
944 * do not trim all blocks after i_size if target size is
945 * larger than i_size.
946 */
Chao Yua33c1502018-08-05 23:04:25 +0800947 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900948 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800949 if (err)
950 return err;
951
Chao Yuc10c9822020-02-27 19:30:03 +0800952 spin_lock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800953 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800954 F2FS_I(inode)->last_disk_size = i_size_read(inode);
Chao Yuc10c9822020-02-27 19:30:03 +0800955 spin_unlock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900956 }
957
958 __setattr_copy(inode, attr);
959
960 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800961 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700962 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
963 inode->i_mode = F2FS_I(inode)->i_acl_mode;
964 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900965 }
966 }
967
Yunlei Hec0ed4402016-12-11 15:35:15 +0800968 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800969 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700970
971 /* inode change will produce dirty node pages flushed by checkpoint */
972 f2fs_balance_fs(F2FS_I_SB(inode), true);
973
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900974 return err;
975}
976
977const struct inode_operations f2fs_file_inode_operations = {
978 .getattr = f2fs_getattr,
979 .setattr = f2fs_setattr,
980 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800981 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900982 .listxattr = f2fs_listxattr,
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900983 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900984};
985
Chao Yu63943282015-08-07 18:36:06 +0800986static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900987 loff_t start, loff_t len)
988{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700989 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900990 struct page *page;
991
992 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800993 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900994
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800995 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900996
Gu Zhenge4795562013-09-27 18:08:30 +0800997 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800998 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800999 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001000
Chao Yu63943282015-08-07 18:36:06 +08001001 if (IS_ERR(page))
1002 return PTR_ERR(page);
1003
Chao Yubae0ee72018-12-25 17:43:42 +08001004 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +08001005 zero_user(page, start, len);
1006 set_page_dirty(page);
1007 f2fs_put_page(page, 1);
1008 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001009}
1010
Chao Yu4d57b862018-05-30 00:20:41 +08001011int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001012{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001013 int err;
1014
Chao Yuea587112015-09-17 20:22:44 +08001015 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001016 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001017 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001018
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001019 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001020 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001021 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001022 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001023 pg_start = f2fs_get_next_page_offset(&dn,
1024 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001025 continue;
Chao Yuea587112015-09-17 20:22:44 +08001026 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001027 return err;
1028 }
1029
Chao Yu81ca7352016-01-26 15:39:35 +08001030 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001031 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1032
1033 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1034
Chao Yu4d57b862018-05-30 00:20:41 +08001035 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001036 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001037
1038 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001039 }
1040 return 0;
1041}
1042
Chao Yua66c7b22013-11-22 16:52:50 +08001043static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001044{
1045 pgoff_t pg_start, pg_end;
1046 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001047 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001048
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001049 ret = f2fs_convert_inline_inode(inode);
1050 if (ret)
1051 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001052
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001053 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1054 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001055
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001056 off_start = offset & (PAGE_SIZE - 1);
1057 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001058
1059 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001060 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001061 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001062 if (ret)
1063 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001064 } else {
Chao Yu63943282015-08-07 18:36:06 +08001065 if (off_start) {
1066 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001067 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001068 if (ret)
1069 return ret;
1070 }
1071 if (off_end) {
1072 ret = fill_zero(inode, pg_end, 0, off_end);
1073 if (ret)
1074 return ret;
1075 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001076
1077 if (pg_start < pg_end) {
1078 struct address_space *mapping = inode->i_mapping;
1079 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001080 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001081
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001082 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001083
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001084 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1085 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001086
Chao Yua33c1502018-08-05 23:04:25 +08001087 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001088 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001089
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001090 truncate_inode_pages_range(mapping, blk_start,
1091 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001092
Gu Zhenge4795562013-09-27 18:08:30 +08001093 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001094 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001095 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001096
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001097 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001098 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001099 }
1100 }
1101
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001102 return ret;
1103}
1104
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001105static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1106 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001107{
1108 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1109 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001110 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001111
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001112next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001113 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001114 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001115 if (ret && ret != -ENOENT) {
1116 return ret;
1117 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001118 if (dn.max_level == 0)
1119 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001120 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1121 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001122 blkaddr += done;
1123 do_replace += done;
1124 goto next;
1125 }
1126
1127 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1128 dn.ofs_in_node, len);
1129 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001130 *blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu93770ab2019-04-15 15:26:32 +08001131
1132 if (__is_valid_data_blkaddr(*blkaddr) &&
1133 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1134 DATA_GENERIC_ENHANCE)) {
1135 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001136 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001137 }
1138
Chao Yu4d57b862018-05-30 00:20:41 +08001139 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001140
Chao Yub0332a02020-02-14 17:44:12 +08001141 if (f2fs_lfs_mode(sbi)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001142 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001143 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001144 }
1145
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001146 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001147 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001148 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001149 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001150 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001151 f2fs_put_dnode(&dn);
1152next:
1153 len -= done;
1154 off += done;
1155 if (len)
1156 goto next_dnode;
1157 return 0;
1158}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001159
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001160static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1161 int *do_replace, pgoff_t off, int len)
1162{
1163 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1164 struct dnode_of_data dn;
1165 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001166
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001167 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1168 if (*do_replace == 0)
1169 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001170
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001171 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001172 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001173 if (ret) {
1174 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001175 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001176 } else {
1177 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001178 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001179 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001180 }
1181 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001182}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001183
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001184static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1185 block_t *blkaddr, int *do_replace,
1186 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1187{
1188 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1189 pgoff_t i = 0;
1190 int ret;
1191
1192 while (i < len) {
1193 if (blkaddr[i] == NULL_ADDR && !full) {
1194 i++;
1195 continue;
1196 }
1197
1198 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1199 struct dnode_of_data dn;
1200 struct node_info ni;
1201 size_t new_size;
1202 pgoff_t ilen;
1203
1204 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001205 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001206 if (ret)
1207 return ret;
1208
Chao Yu77357302018-07-17 00:02:17 +08001209 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1210 if (ret) {
1211 f2fs_put_dnode(&dn);
1212 return ret;
1213 }
1214
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001215 ilen = min((pgoff_t)
1216 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1217 dn.ofs_in_node, len - i);
1218 do {
Chao Yua2ced1c2020-02-14 17:44:10 +08001219 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu4d57b862018-05-30 00:20:41 +08001220 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001221
1222 if (do_replace[i]) {
1223 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001224 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001225 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001226 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001227 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1228 blkaddr[i], ni.version, true, false);
1229
1230 do_replace[i] = 0;
1231 }
1232 dn.ofs_in_node++;
1233 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001234 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001235 if (dst_inode->i_size < new_size)
1236 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001237 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001238
1239 f2fs_put_dnode(&dn);
1240 } else {
1241 struct page *psrc, *pdst;
1242
Chao Yu4d57b862018-05-30 00:20:41 +08001243 psrc = f2fs_get_lock_data_page(src_inode,
1244 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001245 if (IS_ERR(psrc))
1246 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001247 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001248 true);
1249 if (IS_ERR(pdst)) {
1250 f2fs_put_page(psrc, 1);
1251 return PTR_ERR(pdst);
1252 }
1253 f2fs_copy_page(psrc, pdst);
1254 set_page_dirty(pdst);
1255 f2fs_put_page(pdst, 1);
1256 f2fs_put_page(psrc, 1);
1257
Chao Yu4d57b862018-05-30 00:20:41 +08001258 ret = f2fs_truncate_hole(src_inode,
1259 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001260 if (ret)
1261 return ret;
1262 i++;
1263 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001264 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001265 return 0;
1266}
1267
1268static int __exchange_data_block(struct inode *src_inode,
1269 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001270 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001271{
1272 block_t *src_blkaddr;
1273 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001274 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001275 int ret;
1276
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001277 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001278 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001279
Chao Yu628b3d12017-11-30 19:28:18 +08001280 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001281 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001282 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001283 if (!src_blkaddr)
1284 return -ENOMEM;
1285
Chao Yu628b3d12017-11-30 19:28:18 +08001286 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001287 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001288 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001289 if (!do_replace) {
1290 kvfree(src_blkaddr);
1291 return -ENOMEM;
1292 }
1293
1294 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1295 do_replace, src, olen);
1296 if (ret)
1297 goto roll_back;
1298
1299 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1300 do_replace, src, dst, olen, full);
1301 if (ret)
1302 goto roll_back;
1303
1304 src += olen;
1305 dst += olen;
1306 len -= olen;
1307
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001308 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001309 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001310 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001311 return 0;
1312
1313roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001314 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001315 kvfree(src_blkaddr);
1316 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001317 return ret;
1318}
1319
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001320static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001321{
1322 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001323 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001324 pgoff_t start = offset >> PAGE_SHIFT;
1325 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001326 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001327
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001328 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001329
1330 /* avoid gc operation during block exchange */
1331 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1332 down_write(&F2FS_I(inode)->i_mmap_sem);
1333
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001334 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001335 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001336 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001337 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1338 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001339
1340 up_write(&F2FS_I(inode)->i_mmap_sem);
1341 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001342 return ret;
1343}
1344
1345static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1346{
Chao Yub4ace332015-05-06 13:09:46 +08001347 loff_t new_size;
1348 int ret;
1349
Chao Yub4ace332015-05-06 13:09:46 +08001350 if (offset + len >= i_size_read(inode))
1351 return -EINVAL;
1352
1353 /* collapse range should be aligned to block size of f2fs. */
1354 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1355 return -EINVAL;
1356
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001357 ret = f2fs_convert_inline_inode(inode);
1358 if (ret)
1359 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001360
Chao Yub4ace332015-05-06 13:09:46 +08001361 /* write out all dirty pages from offset */
1362 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1363 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001364 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001365
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001366 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001367 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001368 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001369
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001370 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001371 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001372 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1373 truncate_pagecache(inode, offset);
1374
Chao Yub4ace332015-05-06 13:09:46 +08001375 new_size = i_size_read(inode) - len;
Chao Yuc42d28c2019-02-02 17:33:01 +08001376 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001377 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001378 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001379 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001380 return ret;
1381}
1382
Chao Yu6e961942016-05-09 19:56:31 +08001383static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1384 pgoff_t end)
1385{
1386 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1387 pgoff_t index = start;
1388 unsigned int ofs_in_node = dn->ofs_in_node;
1389 blkcnt_t count = 0;
1390 int ret;
1391
1392 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001393 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001394 count++;
1395 }
1396
1397 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001398 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001399 if (ret)
1400 return ret;
1401
1402 dn->ofs_in_node = ofs_in_node;
1403 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001404 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001405 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001406 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001407 * allocation.
1408 */
1409 if (dn->data_blkaddr == NULL_ADDR) {
1410 ret = -ENOSPC;
1411 break;
1412 }
1413 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001414 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001415 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001416 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001417 }
1418 }
1419
1420 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1421
1422 return ret;
1423}
1424
Chao Yu75cd4e02015-05-06 13:11:13 +08001425static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1426 int mode)
1427{
1428 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1429 struct address_space *mapping = inode->i_mapping;
1430 pgoff_t index, pg_start, pg_end;
1431 loff_t new_size = i_size_read(inode);
1432 loff_t off_start, off_end;
1433 int ret = 0;
1434
Chao Yu75cd4e02015-05-06 13:11:13 +08001435 ret = inode_newsize_ok(inode, (len + offset));
1436 if (ret)
1437 return ret;
1438
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001439 ret = f2fs_convert_inline_inode(inode);
1440 if (ret)
1441 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001442
1443 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1444 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001445 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001446
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001447 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1448 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001449
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 off_start = offset & (PAGE_SIZE - 1);
1451 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001452
1453 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001454 ret = fill_zero(inode, pg_start, off_start,
1455 off_end - off_start);
1456 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001457 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001458
Chao Yu75cd4e02015-05-06 13:11:13 +08001459 new_size = max_t(loff_t, new_size, offset + len);
1460 } else {
1461 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001462 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001463 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001464 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,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001468 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001469 }
1470
Chao Yu6e961942016-05-09 19:56:31 +08001471 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001472 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001473 unsigned int end_offset;
1474 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001475
Chao Yuc70798532018-08-05 23:02:22 +08001476 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001477 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001478
1479 truncate_pagecache_range(inode,
1480 (loff_t)index << PAGE_SHIFT,
1481 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1482
Chao Yu75cd4e02015-05-06 13:11:13 +08001483 f2fs_lock_op(sbi);
1484
Chao Yu6e961942016-05-09 19:56:31 +08001485 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001486 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001487 if (ret) {
1488 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001489 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001490 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001491 goto out;
1492 }
1493
Chao Yu6e961942016-05-09 19:56:31 +08001494 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1495 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1496
1497 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001498 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001499
Chao Yu75cd4e02015-05-06 13:11:13 +08001500 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001501 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001502 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001503
1504 f2fs_balance_fs(sbi, dn.node_changed);
1505
Chao Yu6e961942016-05-09 19:56:31 +08001506 if (ret)
1507 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001508
Chao Yu6e961942016-05-09 19:56:31 +08001509 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001510 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001511 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001512 }
1513
1514 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001515 ret = fill_zero(inode, pg_end, 0, off_end);
1516 if (ret)
1517 goto out;
1518
Chao Yu75cd4e02015-05-06 13:11:13 +08001519 new_size = max_t(loff_t, new_size, offset + len);
1520 }
1521 }
1522
1523out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001524 if (new_size > i_size_read(inode)) {
1525 if (mode & FALLOC_FL_KEEP_SIZE)
1526 file_set_keep_isize(inode);
1527 else
1528 f2fs_i_size_write(inode, new_size);
1529 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001530 return ret;
1531}
1532
Chao Yuf62185d2015-05-28 19:16:57 +08001533static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1534{
1535 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001536 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001537 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001538 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001539
Chao Yuf62185d2015-05-28 19:16:57 +08001540 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001541 ret = inode_newsize_ok(inode, new_size);
1542 if (ret)
1543 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001544
1545 if (offset >= i_size_read(inode))
1546 return -EINVAL;
1547
1548 /* insert range should be aligned to block size of f2fs. */
1549 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1550 return -EINVAL;
1551
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001552 ret = f2fs_convert_inline_inode(inode);
1553 if (ret)
1554 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001555
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001556 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001557
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001558 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001559 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001560 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001561 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001562 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001563
1564 /* write out all dirty pages from offset */
1565 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1566 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001567 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001568
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001569 pg_start = offset >> PAGE_SHIFT;
1570 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001571 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001572 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001573
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001574 /* avoid gc operation during block exchange */
1575 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1576 down_write(&F2FS_I(inode)->i_mmap_sem);
1577 truncate_pagecache(inode, offset);
1578
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001579 while (!ret && idx > pg_start) {
1580 nr = idx - pg_start;
1581 if (nr > delta)
1582 nr = delta;
1583 idx -= nr;
1584
Chao Yuf62185d2015-05-28 19:16:57 +08001585 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001586 f2fs_drop_extent_tree(inode);
1587
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001588 ret = __exchange_data_block(inode, inode, idx,
1589 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001590 f2fs_unlock_op(sbi);
1591 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001592 up_write(&F2FS_I(inode)->i_mmap_sem);
1593 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001594
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001595 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001596 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001597 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1598 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001599 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001600
1601 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001602 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001603 return ret;
1604}
1605
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001606static int expand_inode_data(struct inode *inode, loff_t offset,
1607 loff_t len, int mode)
1608{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001609 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001610 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001611 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1612 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001613 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001614 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001615 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001616 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001617
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001618 err = inode_newsize_ok(inode, (len + offset));
1619 if (err)
1620 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001621
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001622 err = f2fs_convert_inline_inode(inode);
1623 if (err)
1624 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001625
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001626 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001627
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001628 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001629 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001630
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001631 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1632 map.m_len = pg_end - map.m_lblk;
1633 if (off_end)
1634 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001635
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001636 if (!map.m_len)
1637 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001638
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001639 if (f2fs_is_pinned_file(inode)) {
1640 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1641 sbi->log_blocks_per_seg;
1642 block_t done = 0;
1643
1644 if (map.m_len % sbi->blocks_per_seg)
1645 len += sbi->blocks_per_seg;
1646
1647 map.m_len = sbi->blocks_per_seg;
1648next_alloc:
1649 if (has_not_enough_free_secs(sbi, 0,
1650 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
Chao Yufb24fea2020-01-14 19:36:50 +08001651 down_write(&sbi->gc_lock);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001652 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1653 if (err && err != -ENODATA && err != -EAGAIN)
1654 goto out_err;
1655 }
1656
1657 down_write(&sbi->pin_sem);
1658 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
Daeho Jeongfd612642020-05-27 13:02:31 +09001659
1660 f2fs_lock_op(sbi);
Chao Yu901d745f2020-06-22 17:38:48 +08001661 f2fs_allocate_new_segment(sbi, CURSEG_COLD_DATA);
Daeho Jeongfd612642020-05-27 13:02:31 +09001662 f2fs_unlock_op(sbi);
1663
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001664 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1665 up_write(&sbi->pin_sem);
1666
1667 done += map.m_len;
1668 len -= map.m_len;
1669 map.m_lblk += map.m_len;
1670 if (!err && len)
1671 goto next_alloc;
1672
1673 map.m_len = done;
1674 } else {
1675 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1676 }
1677out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001678 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001679 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001680
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001681 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001682 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001683
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001684 last_off = map.m_lblk + map.m_len - 1;
1685
1686 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001687 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001688 (loff_t)(last_off + 1) << PAGE_SHIFT;
1689 } else {
1690 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001691 }
1692
Chao Yue8ed90a2017-11-05 21:53:30 +08001693 if (new_size > i_size_read(inode)) {
1694 if (mode & FALLOC_FL_KEEP_SIZE)
1695 file_set_keep_isize(inode);
1696 else
1697 f2fs_i_size_write(inode, new_size);
1698 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001699
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001700 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001701}
1702
1703static long f2fs_fallocate(struct file *file, int mode,
1704 loff_t offset, loff_t len)
1705{
Al Viro6131ffa2013-02-27 16:59:05 -05001706 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001707 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001708
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001709 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1710 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001711 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1712 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001713 if (!f2fs_is_compress_backend_ready(inode))
1714 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001715
Chao Yuc9980122015-09-11 14:39:02 +08001716 /* f2fs only support ->fallocate for regular file */
1717 if (!S_ISREG(inode->i_mode))
1718 return -EINVAL;
1719
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301720 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001721 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001722 return -EOPNOTSUPP;
1723
Chao Yu4c8ff702019-11-01 18:07:14 +08001724 if (f2fs_compressed_file(inode) &&
1725 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1726 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1727 return -EOPNOTSUPP;
1728
Chao Yub4ace332015-05-06 13:09:46 +08001729 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001730 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1731 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001732 return -EOPNOTSUPP;
1733
Al Viro59551022016-01-22 15:40:57 -05001734 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001735
Taehee Yoo587c0a42015-04-21 15:59:12 +09001736 if (mode & FALLOC_FL_PUNCH_HOLE) {
1737 if (offset >= inode->i_size)
1738 goto out;
1739
Chao Yua66c7b22013-11-22 16:52:50 +08001740 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001741 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1742 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001743 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1744 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001745 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1746 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001747 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001748 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001749 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001750
Namjae Jeon3af60a42012-12-30 14:52:37 +09001751 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001752 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001753 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001754 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001755 }
Chao Yu3375f692014-01-28 10:29:26 +08001756
Taehee Yoo587c0a42015-04-21 15:59:12 +09001757out:
Al Viro59551022016-01-22 15:40:57 -05001758 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001759
Namjae Jeonc01e2852013-04-23 17:00:52 +09001760 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001761 return ret;
1762}
1763
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001764static int f2fs_release_file(struct inode *inode, struct file *filp)
1765{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001766 /*
1767 * f2fs_relase_file is called at every close calls. So we should
1768 * not drop any inmemory pages by close called by other process.
1769 */
1770 if (!(filp->f_mode & FMODE_WRITE) ||
1771 atomic_read(&inode->i_writecount) != 1)
1772 return 0;
1773
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001774 /* some remained atomic pages should discarded */
1775 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001776 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001777 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001778 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001779 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001780 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001781 clear_inode_flag(inode, FI_VOLATILE_FILE);
1782 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001783 }
1784 return 0;
1785}
1786
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001787static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001788{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001789 struct inode *inode = file_inode(file);
1790
1791 /*
1792 * If the process doing a transaction is crashed, we should do
1793 * roll-back. Otherwise, other reader/write can see corrupted database
1794 * until all the writers close its file. Since this should be done
1795 * before dropping file lock, it needs to do in ->flush.
1796 */
1797 if (f2fs_is_atomic_file(inode) &&
1798 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001799 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001800 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001801}
1802
Eric Biggers36098552019-06-04 22:59:04 -07001803static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001804{
1805 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001806 u32 masked_flags = fi->i_flags & mask;
1807
1808 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
Chao Yu2c1d0302017-07-29 00:32:52 +08001809
1810 /* Is it quota file? Do not allow user to mess with it */
1811 if (IS_NOQUOTA(inode))
1812 return -EPERM;
1813
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001814 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001815 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1816 return -EOPNOTSUPP;
1817 if (!f2fs_empty_dir(inode))
1818 return -ENOTEMPTY;
1819 }
1820
Chao Yu4c8ff702019-11-01 18:07:14 +08001821 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1822 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1823 return -EOPNOTSUPP;
1824 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1825 return -EINVAL;
1826 }
1827
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001828 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
Chao Yuaa576972020-03-27 18:29:51 +08001829 if (masked_flags & F2FS_COMPR_FL) {
Chao Yu2536ac62020-03-10 20:50:09 +08001830 if (f2fs_disable_compressed_file(inode))
1831 return -EINVAL;
1832 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001833 if (iflags & F2FS_NOCOMP_FL)
1834 return -EINVAL;
1835 if (iflags & F2FS_COMPR_FL) {
Chao Yu4c8ff702019-11-01 18:07:14 +08001836 if (!f2fs_may_compress(inode))
1837 return -EINVAL;
1838
1839 set_compress_context(inode);
1840 }
1841 }
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001842 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1843 if (masked_flags & F2FS_COMPR_FL)
Chao Yu4c8ff702019-11-01 18:07:14 +08001844 return -EINVAL;
1845 }
1846
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001847 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001848 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1849 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001850
Chao Yu59c84402018-04-03 15:08:17 +08001851 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001852 set_inode_flag(inode, FI_PROJ_INHERIT);
1853 else
1854 clear_inode_flag(inode, FI_PROJ_INHERIT);
1855
1856 inode->i_ctime = current_time(inode);
1857 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001858 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001859 return 0;
1860}
1861
Eric Biggers36098552019-06-04 22:59:04 -07001862/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1863
1864/*
1865 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1866 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1867 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1868 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1869 */
1870
1871static const struct {
1872 u32 iflag;
1873 u32 fsflag;
1874} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001875 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001876 { F2FS_SYNC_FL, FS_SYNC_FL },
1877 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1878 { F2FS_APPEND_FL, FS_APPEND_FL },
1879 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1880 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001881 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001882 { F2FS_INDEX_FL, FS_INDEX_FL },
1883 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1884 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001885 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001886};
1887
1888#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001889 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001890 FS_SYNC_FL | \
1891 FS_IMMUTABLE_FL | \
1892 FS_APPEND_FL | \
1893 FS_NODUMP_FL | \
1894 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001895 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001896 FS_INDEX_FL | \
1897 FS_DIRSYNC_FL | \
1898 FS_PROJINHERIT_FL | \
1899 FS_ENCRYPT_FL | \
1900 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001901 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001902 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001903 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001904
1905#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001906 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001907 FS_SYNC_FL | \
1908 FS_IMMUTABLE_FL | \
1909 FS_APPEND_FL | \
1910 FS_NODUMP_FL | \
1911 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001912 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001913 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001914 FS_PROJINHERIT_FL | \
1915 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001916
1917/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1918static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1919{
1920 u32 fsflags = 0;
1921 int i;
1922
1923 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1924 if (iflags & f2fs_fsflags_map[i].iflag)
1925 fsflags |= f2fs_fsflags_map[i].fsflag;
1926
1927 return fsflags;
1928}
1929
1930/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1931static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1932{
1933 u32 iflags = 0;
1934 int i;
1935
1936 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1937 if (fsflags & f2fs_fsflags_map[i].fsflag)
1938 iflags |= f2fs_fsflags_map[i].iflag;
1939
1940 return iflags;
1941}
1942
1943static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1944{
1945 struct inode *inode = file_inode(filp);
1946 struct f2fs_inode_info *fi = F2FS_I(inode);
1947 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1948
1949 if (IS_ENCRYPTED(inode))
1950 fsflags |= FS_ENCRYPT_FL;
Eric Biggers95ae2512019-07-22 09:26:24 -07001951 if (IS_VERITY(inode))
1952 fsflags |= FS_VERITY_FL;
Eric Biggers36098552019-06-04 22:59:04 -07001953 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1954 fsflags |= FS_INLINE_DATA_FL;
1955 if (is_inode_flag_set(inode, FI_PIN_FILE))
1956 fsflags |= FS_NOCOW_FL;
1957
1958 fsflags &= F2FS_GETTABLE_FS_FL;
1959
1960 return put_user(fsflags, (int __user *)arg);
1961}
1962
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001963static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1964{
1965 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001966 struct f2fs_inode_info *fi = F2FS_I(inode);
1967 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001968 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001969 int ret;
1970
Chao Yu7fb17fe2016-05-09 19:56:32 +08001971 if (!inode_owner_or_capable(inode))
1972 return -EACCES;
1973
Eric Biggers36098552019-06-04 22:59:04 -07001974 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001975 return -EFAULT;
1976
Eric Biggers36098552019-06-04 22:59:04 -07001977 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1978 return -EOPNOTSUPP;
1979 fsflags &= F2FS_SETTABLE_FS_FL;
1980
1981 iflags = f2fs_fsflags_to_iflags(fsflags);
1982 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1983 return -EOPNOTSUPP;
1984
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001985 ret = mnt_want_write_file(filp);
1986 if (ret)
1987 return ret;
1988
Al Viro59551022016-01-22 15:40:57 -05001989 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001990
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001991 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1992 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1993 if (ret)
1994 goto out;
1995
Eric Biggers36098552019-06-04 22:59:04 -07001996 ret = f2fs_setflags_common(inode, iflags,
1997 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001998out:
Chao Yua72d4b92017-05-03 23:59:13 +08001999 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002000 mnt_drop_write_file(filp);
2001 return ret;
2002}
2003
Chao Yud49f3e82015-01-23 20:36:04 +08002004static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2005{
2006 struct inode *inode = file_inode(filp);
2007
2008 return put_user(inode->i_generation, (int __user *)arg);
2009}
2010
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002011static int f2fs_ioc_start_atomic_write(struct file *filp)
2012{
2013 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002014 struct f2fs_inode_info *fi = F2FS_I(inode);
2015 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08002016 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002017
2018 if (!inode_owner_or_capable(inode))
2019 return -EACCES;
2020
Jaegeuk Kime8118982017-03-17 10:04:15 +08002021 if (!S_ISREG(inode->i_mode))
2022 return -EINVAL;
2023
Chao Yu038d0692019-07-25 22:39:11 +08002024 if (filp->f_flags & O_DIRECT)
2025 return -EINVAL;
2026
Chao Yu7fb17fe2016-05-09 19:56:32 +08002027 ret = mnt_want_write_file(filp);
2028 if (ret)
2029 return ret;
2030
Chao Yu0fac5582016-05-09 19:56:33 +08002031 inode_lock(inode);
2032
Chao Yu4c8ff702019-11-01 18:07:14 +08002033 f2fs_disable_compressed_file(inode);
2034
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002035 if (f2fs_is_atomic_file(inode)) {
2036 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2037 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002038 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002039 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002040
Chao Yuf4c9c742015-07-17 18:06:35 +08002041 ret = f2fs_convert_inline_inode(inode);
2042 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002043 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002044
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002045 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2046
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002047 /*
2048 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2049 * f2fs_is_atomic_file.
2050 */
2051 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002052 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2053 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002054 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002055 if (ret) {
2056 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002057 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002058 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002059
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002060 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2061 if (list_empty(&fi->inmem_ilist))
2062 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302063 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002064 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2065
2066 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002067 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002068 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002069 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002070
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002071 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002072 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002073 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002074out:
Chao Yu0fac5582016-05-09 19:56:33 +08002075 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002076 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002077 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002078}
2079
2080static int f2fs_ioc_commit_atomic_write(struct file *filp)
2081{
2082 struct inode *inode = file_inode(filp);
2083 int ret;
2084
2085 if (!inode_owner_or_capable(inode))
2086 return -EACCES;
2087
2088 ret = mnt_want_write_file(filp);
2089 if (ret)
2090 return ret;
2091
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002092 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002093
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002094 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002095
Chao Yub169c3c2018-04-18 17:45:02 +08002096 if (f2fs_is_volatile_file(inode)) {
2097 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002098 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002099 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002100
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002101 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002102 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002103 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002104 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002105
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002106 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002107 if (!ret)
2108 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002109 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002110 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002111 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002112err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002113 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2114 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2115 ret = -EINVAL;
2116 }
Chao Yu0fac5582016-05-09 19:56:33 +08002117 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002118 mnt_drop_write_file(filp);
2119 return ret;
2120}
2121
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002122static int f2fs_ioc_start_volatile_write(struct file *filp)
2123{
2124 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002125 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002126
2127 if (!inode_owner_or_capable(inode))
2128 return -EACCES;
2129
Chao Yu8ff09712017-03-17 15:43:57 +08002130 if (!S_ISREG(inode->i_mode))
2131 return -EINVAL;
2132
Chao Yu7fb17fe2016-05-09 19:56:32 +08002133 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002134 if (ret)
2135 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002136
Chao Yu0fac5582016-05-09 19:56:33 +08002137 inode_lock(inode);
2138
Chao Yu7fb17fe2016-05-09 19:56:32 +08002139 if (f2fs_is_volatile_file(inode))
2140 goto out;
2141
2142 ret = f2fs_convert_inline_inode(inode);
2143 if (ret)
2144 goto out;
2145
Chao Yu648d50b2017-03-22 17:23:45 +08002146 stat_inc_volatile_write(inode);
2147 stat_update_max_volatile_write(inode);
2148
Jaegeuk Kim91942322016-05-20 10:13:22 -07002149 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002150 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002151out:
Chao Yu0fac5582016-05-09 19:56:33 +08002152 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002153 mnt_drop_write_file(filp);
2154 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002155}
2156
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002157static int f2fs_ioc_release_volatile_write(struct file *filp)
2158{
2159 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002160 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002161
2162 if (!inode_owner_or_capable(inode))
2163 return -EACCES;
2164
Chao Yu7fb17fe2016-05-09 19:56:32 +08002165 ret = mnt_want_write_file(filp);
2166 if (ret)
2167 return ret;
2168
Chao Yu0fac5582016-05-09 19:56:33 +08002169 inode_lock(inode);
2170
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002171 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002172 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002173
Chao Yu7fb17fe2016-05-09 19:56:32 +08002174 if (!f2fs_is_first_block_written(inode)) {
2175 ret = truncate_partial_data_page(inode, 0, true);
2176 goto out;
2177 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002178
Chao Yu7fb17fe2016-05-09 19:56:32 +08002179 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2180out:
Chao Yu0fac5582016-05-09 19:56:33 +08002181 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002182 mnt_drop_write_file(filp);
2183 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002184}
2185
2186static int f2fs_ioc_abort_volatile_write(struct file *filp)
2187{
2188 struct inode *inode = file_inode(filp);
2189 int ret;
2190
2191 if (!inode_owner_or_capable(inode))
2192 return -EACCES;
2193
2194 ret = mnt_want_write_file(filp);
2195 if (ret)
2196 return ret;
2197
Chao Yu0fac5582016-05-09 19:56:33 +08002198 inode_lock(inode);
2199
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002200 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002201 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002202 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002203 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002204 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002205 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002206 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002207
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002208 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2209
Chao Yu0fac5582016-05-09 19:56:33 +08002210 inode_unlock(inode);
2211
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002212 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002213 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002214 return ret;
2215}
2216
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002217static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2218{
2219 struct inode *inode = file_inode(filp);
2220 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2221 struct super_block *sb = sbi->sb;
2222 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002223 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002224
2225 if (!capable(CAP_SYS_ADMIN))
2226 return -EPERM;
2227
2228 if (get_user(in, (__u32 __user *)arg))
2229 return -EFAULT;
2230
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302231 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2232 ret = mnt_want_write_file(filp);
Chao Yu86264412020-06-08 20:03:16 +08002233 if (ret) {
2234 if (ret == -EROFS) {
2235 ret = 0;
2236 f2fs_stop_checkpoint(sbi, false);
2237 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2238 trace_f2fs_shutdown(sbi, in, ret);
2239 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302240 return ret;
Chao Yu86264412020-06-08 20:03:16 +08002241 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302242 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002243
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002244 switch (in) {
2245 case F2FS_GOING_DOWN_FULLSYNC:
2246 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08002247 if (IS_ERR(sb)) {
2248 ret = PTR_ERR(sb);
2249 goto out;
2250 }
2251 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002252 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002253 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002254 thaw_bdev(sb->s_bdev, sb);
2255 }
2256 break;
2257 case F2FS_GOING_DOWN_METASYNC:
2258 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002259 ret = f2fs_sync_fs(sb, 1);
2260 if (ret)
2261 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002262 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002263 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002264 break;
2265 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002266 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002267 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002268 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002269 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002270 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002271 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002272 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002273 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002274 case F2FS_GOING_DOWN_NEED_FSCK:
2275 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002276 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2277 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002278 /* do checkpoint only */
2279 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002280 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002281 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002282 ret = -EINVAL;
2283 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002284 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002285
Chao Yu4d57b862018-05-30 00:20:41 +08002286 f2fs_stop_gc_thread(sbi);
2287 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002288
Chao Yu4d57b862018-05-30 00:20:41 +08002289 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002290 clear_opt(sbi, DISCARD);
2291
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002292 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002293out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302294 if (in != F2FS_GOING_DOWN_FULLSYNC)
2295 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002296
2297 trace_f2fs_shutdown(sbi, in, ret);
2298
Chao Yu7fb17fe2016-05-09 19:56:32 +08002299 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002300}
2301
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002302static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2303{
2304 struct inode *inode = file_inode(filp);
2305 struct super_block *sb = inode->i_sb;
2306 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2307 struct fstrim_range range;
2308 int ret;
2309
2310 if (!capable(CAP_SYS_ADMIN))
2311 return -EPERM;
2312
Chao Yu7d20c8a2018-09-04 03:52:17 +08002313 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002314 return -EOPNOTSUPP;
2315
2316 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2317 sizeof(range)))
2318 return -EFAULT;
2319
Chao Yu7fb17fe2016-05-09 19:56:32 +08002320 ret = mnt_want_write_file(filp);
2321 if (ret)
2322 return ret;
2323
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002324 range.minlen = max((unsigned int)range.minlen,
2325 q->limits.discard_granularity);
2326 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002327 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002328 if (ret < 0)
2329 return ret;
2330
2331 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2332 sizeof(range)))
2333 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002334 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002335 return 0;
2336}
2337
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002338static bool uuid_is_nonzero(__u8 u[16])
2339{
2340 int i;
2341
2342 for (i = 0; i < 16; i++)
2343 if (u[i])
2344 return true;
2345 return false;
2346}
2347
2348static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2349{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002350 struct inode *inode = file_inode(filp);
2351
Chao Yu7beb01f2018-10-24 18:34:26 +08002352 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002353 return -EOPNOTSUPP;
2354
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002355 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002356
Eric Biggersdb717d82016-11-26 19:07:49 -05002357 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002358}
2359
2360static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2361{
Chao Yu7beb01f2018-10-24 18:34:26 +08002362 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002363 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002364 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002365}
2366
2367static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2368{
2369 struct inode *inode = file_inode(filp);
2370 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2371 int err;
2372
Chao Yu7beb01f2018-10-24 18:34:26 +08002373 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002374 return -EOPNOTSUPP;
2375
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002376 err = mnt_want_write_file(filp);
2377 if (err)
2378 return err;
2379
Chao Yu846ae672018-02-26 22:04:13 +08002380 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002381
2382 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2383 goto got_it;
2384
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002385 /* update superblock with uuid */
2386 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2387
Chao Yuc5bda1c2015-06-08 13:28:03 +08002388 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002389 if (err) {
2390 /* undo new data */
2391 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002392 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002393 }
2394got_it:
2395 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2396 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002397 err = -EFAULT;
2398out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002399 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002400 mnt_drop_write_file(filp);
2401 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002402}
2403
Eric Biggers8ce589c2019-08-04 19:35:48 -07002404static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2405 unsigned long arg)
2406{
2407 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2408 return -EOPNOTSUPP;
2409
2410 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2411}
2412
2413static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2414{
2415 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2416 return -EOPNOTSUPP;
2417
2418 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2419}
2420
2421static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2422{
2423 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2424 return -EOPNOTSUPP;
2425
2426 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2427}
2428
2429static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2430 unsigned long arg)
2431{
2432 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2433 return -EOPNOTSUPP;
2434
2435 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2436}
2437
2438static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2439 unsigned long arg)
2440{
2441 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2442 return -EOPNOTSUPP;
2443
2444 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2445}
2446
Eric Biggersee446e12020-03-14 13:50:51 -07002447static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2448{
2449 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2450 return -EOPNOTSUPP;
2451
2452 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2453}
2454
Chao Yuc1c1b582015-07-10 18:08:10 +08002455static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2456{
2457 struct inode *inode = file_inode(filp);
2458 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002459 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002460 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002461
2462 if (!capable(CAP_SYS_ADMIN))
2463 return -EPERM;
2464
Chao Yud530d4d2015-10-05 22:22:44 +08002465 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002466 return -EFAULT;
2467
Chao Yud530d4d2015-10-05 22:22:44 +08002468 if (f2fs_readonly(sbi->sb))
2469 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002470
Chao Yu7fb17fe2016-05-09 19:56:32 +08002471 ret = mnt_want_write_file(filp);
2472 if (ret)
2473 return ret;
2474
Chao Yud530d4d2015-10-05 22:22:44 +08002475 if (!sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002476 if (!down_write_trylock(&sbi->gc_lock)) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002477 ret = -EBUSY;
2478 goto out;
2479 }
Chao Yud530d4d2015-10-05 22:22:44 +08002480 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002481 down_write(&sbi->gc_lock);
Chao Yuc1c1b582015-07-10 18:08:10 +08002482 }
2483
Jaegeuk Kime066b832017-04-13 15:17:00 -07002484 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002485out:
2486 mnt_drop_write_file(filp);
2487 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002488}
2489
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002490static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2491{
2492 struct inode *inode = file_inode(filp);
2493 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2494 struct f2fs_gc_range range;
2495 u64 end;
2496 int ret;
2497
2498 if (!capable(CAP_SYS_ADMIN))
2499 return -EPERM;
2500
2501 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2502 sizeof(range)))
2503 return -EFAULT;
2504
2505 if (f2fs_readonly(sbi->sb))
2506 return -EROFS;
2507
Yunlei Heb82f6e32018-04-24 11:40:30 +08002508 end = range.start + range.len;
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302509 if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
2510 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002511 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002512
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002513 ret = mnt_want_write_file(filp);
2514 if (ret)
2515 return ret;
2516
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002517do_more:
2518 if (!range.sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002519 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002520 ret = -EBUSY;
2521 goto out;
2522 }
2523 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002524 down_write(&sbi->gc_lock);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002525 }
2526
2527 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Qilong Zhang97767502020-06-28 19:23:03 +08002528 if (ret) {
2529 if (ret == -EBUSY)
2530 ret = -EAGAIN;
2531 goto out;
2532 }
Yunlong Song67b0e422018-10-30 20:37:55 +08002533 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002534 if (range.start <= end)
2535 goto do_more;
2536out:
2537 mnt_drop_write_file(filp);
2538 return ret;
2539}
2540
Chao Yu059c0642018-07-17 20:41:49 +08002541static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002542{
2543 struct inode *inode = file_inode(filp);
2544 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002545 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002546
2547 if (!capable(CAP_SYS_ADMIN))
2548 return -EPERM;
2549
2550 if (f2fs_readonly(sbi->sb))
2551 return -EROFS;
2552
Daniel Rosenberg43549942018-08-20 19:21:43 -07002553 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002554 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002555 return -EINVAL;
2556 }
2557
Chao Yu7fb17fe2016-05-09 19:56:32 +08002558 ret = mnt_want_write_file(filp);
2559 if (ret)
2560 return ret;
2561
2562 ret = f2fs_sync_fs(sbi->sb, 1);
2563
2564 mnt_drop_write_file(filp);
2565 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002566}
2567
Chao Yud323d002015-10-27 09:53:45 +08002568static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2569 struct file *filp,
2570 struct f2fs_defragment *range)
2571{
2572 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002573 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002574 .m_seg_type = NO_CHECK_TYPE ,
2575 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002576 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002577 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002578 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002579 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002580 block_t blk_end = 0;
2581 bool fragmented = false;
2582 int err;
2583
2584 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002585 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002586 return -EINVAL;
2587
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002588 pg_start = range->start >> PAGE_SHIFT;
2589 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002590
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002591 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002592
Al Viro59551022016-01-22 15:40:57 -05002593 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002594
2595 /* writeback all dirty pages in the range */
2596 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002597 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002598 if (err)
2599 goto out;
2600
2601 /*
2602 * lookup mapping info in extent cache, skip defragmenting if physical
2603 * block addresses are continuous.
2604 */
2605 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2606 if (ei.fofs + ei.len >= pg_end)
2607 goto out;
2608 }
2609
2610 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002611 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002612
2613 /*
2614 * lookup mapping info in dnode page cache, skip defragmenting if all
2615 * physical block addresses are continuous even if there are hole(s)
2616 * in logical blocks.
2617 */
2618 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002619 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002620 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002621 if (err)
2622 goto out;
2623
2624 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002625 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002626 continue;
2627 }
2628
Chao Yu25a912e2018-01-10 18:18:51 +08002629 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002630 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002631
2632 /* record total count of block that we're going to move */
2633 total += map.m_len;
2634
Chao Yud323d002015-10-27 09:53:45 +08002635 blk_end = map.m_pblk + map.m_len;
2636
2637 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002638 }
2639
Chao Yud3a1a0e2019-07-29 23:02:29 +08002640 if (!fragmented) {
2641 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002642 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002643 }
Chao Yud323d002015-10-27 09:53:45 +08002644
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002645 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002646
2647 /*
2648 * make sure there are enough free section for LFS allocation, this can
2649 * avoid defragment running in SSR mode when free section are allocated
2650 * intensively
2651 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002652 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002653 err = -EAGAIN;
2654 goto out;
2655 }
2656
Chao Yu25a912e2018-01-10 18:18:51 +08002657 map.m_lblk = pg_start;
2658 map.m_len = pg_end - pg_start;
2659 total = 0;
2660
Chao Yud323d002015-10-27 09:53:45 +08002661 while (map.m_lblk < pg_end) {
2662 pgoff_t idx;
2663 int cnt = 0;
2664
2665do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002666 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002667 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002668 if (err)
2669 goto clear_out;
2670
2671 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002672 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002673 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002674 }
2675
Jaegeuk Kim91942322016-05-20 10:13:22 -07002676 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002677
2678 idx = map.m_lblk;
2679 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2680 struct page *page;
2681
Chao Yu4d57b862018-05-30 00:20:41 +08002682 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002683 if (IS_ERR(page)) {
2684 err = PTR_ERR(page);
2685 goto clear_out;
2686 }
2687
2688 set_page_dirty(page);
2689 f2fs_put_page(page, 1);
2690
2691 idx++;
2692 cnt++;
2693 total++;
2694 }
2695
2696 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002697check:
2698 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002699 goto do_map;
2700
Jaegeuk Kim91942322016-05-20 10:13:22 -07002701 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002702
2703 err = filemap_fdatawrite(inode->i_mapping);
2704 if (err)
2705 goto out;
2706 }
2707clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002708 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002709out:
Al Viro59551022016-01-22 15:40:57 -05002710 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002711 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002712 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002713 return err;
2714}
2715
2716static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2717{
2718 struct inode *inode = file_inode(filp);
2719 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2720 struct f2fs_defragment range;
2721 int err;
2722
2723 if (!capable(CAP_SYS_ADMIN))
2724 return -EPERM;
2725
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002726 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002727 return -EINVAL;
2728
Kinglong Meed7563862017-03-10 17:55:07 +08002729 if (f2fs_readonly(sbi->sb))
2730 return -EROFS;
2731
2732 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2733 sizeof(range)))
2734 return -EFAULT;
2735
2736 /* verify alignment of offset & size */
2737 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2738 return -EINVAL;
2739
2740 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2741 sbi->max_file_blocks))
2742 return -EINVAL;
2743
Chao Yud323d002015-10-27 09:53:45 +08002744 err = mnt_want_write_file(filp);
2745 if (err)
2746 return err;
2747
Chao Yud323d002015-10-27 09:53:45 +08002748 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002749 mnt_drop_write_file(filp);
2750
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002751 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002752 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002753 return err;
Chao Yud323d002015-10-27 09:53:45 +08002754
2755 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2756 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002757 return -EFAULT;
2758
2759 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002760}
2761
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002762static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2763 struct file *file_out, loff_t pos_out, size_t len)
2764{
2765 struct inode *src = file_inode(file_in);
2766 struct inode *dst = file_inode(file_out);
2767 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2768 size_t olen = len, dst_max_i_size = 0;
2769 size_t dst_osize;
2770 int ret;
2771
2772 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2773 src->i_sb != dst->i_sb)
2774 return -EXDEV;
2775
2776 if (unlikely(f2fs_readonly(src->i_sb)))
2777 return -EROFS;
2778
Chao Yufe8494b2016-08-04 20:13:02 +08002779 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2780 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002781
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302782 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002783 return -EOPNOTSUPP;
2784
Fan Lid95fd912016-09-13 11:35:42 +08002785 if (src == dst) {
2786 if (pos_in == pos_out)
2787 return 0;
2788 if (pos_out > pos_in && pos_out < pos_in + len)
2789 return -EINVAL;
2790 }
2791
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002792 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002793 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002794 ret = -EBUSY;
2795 if (!inode_trylock(dst))
2796 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002797 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002798
2799 ret = -EINVAL;
2800 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2801 goto out_unlock;
2802 if (len == 0)
2803 olen = len = src->i_size - pos_in;
2804 if (pos_in + len == src->i_size)
2805 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2806 if (len == 0) {
2807 ret = 0;
2808 goto out_unlock;
2809 }
2810
2811 dst_osize = dst->i_size;
2812 if (pos_out + olen > dst->i_size)
2813 dst_max_i_size = pos_out + olen;
2814
2815 /* verify the end result is block aligned */
2816 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2817 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2818 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2819 goto out_unlock;
2820
2821 ret = f2fs_convert_inline_inode(src);
2822 if (ret)
2823 goto out_unlock;
2824
2825 ret = f2fs_convert_inline_inode(dst);
2826 if (ret)
2827 goto out_unlock;
2828
2829 /* write out all dirty pages from offset */
2830 ret = filemap_write_and_wait_range(src->i_mapping,
2831 pos_in, pos_in + len);
2832 if (ret)
2833 goto out_unlock;
2834
2835 ret = filemap_write_and_wait_range(dst->i_mapping,
2836 pos_out, pos_out + len);
2837 if (ret)
2838 goto out_unlock;
2839
2840 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002841
2842 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2843 if (src != dst) {
2844 ret = -EBUSY;
2845 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2846 goto out_src;
2847 }
2848
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002849 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002850 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2851 pos_out >> F2FS_BLKSIZE_BITS,
2852 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002853
2854 if (!ret) {
2855 if (dst_max_i_size)
2856 f2fs_i_size_write(dst, dst_max_i_size);
2857 else if (dst_osize != dst->i_size)
2858 f2fs_i_size_write(dst, dst_osize);
2859 }
2860 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002861
2862 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002863 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002864out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002865 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002866out_unlock:
2867 if (src != dst)
2868 inode_unlock(dst);
2869out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002870 inode_unlock(src);
2871 return ret;
2872}
2873
2874static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2875{
2876 struct f2fs_move_range range;
2877 struct fd dst;
2878 int err;
2879
2880 if (!(filp->f_mode & FMODE_READ) ||
2881 !(filp->f_mode & FMODE_WRITE))
2882 return -EBADF;
2883
2884 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2885 sizeof(range)))
2886 return -EFAULT;
2887
2888 dst = fdget(range.dst_fd);
2889 if (!dst.file)
2890 return -EBADF;
2891
2892 if (!(dst.file->f_mode & FMODE_WRITE)) {
2893 err = -EBADF;
2894 goto err_out;
2895 }
2896
2897 err = mnt_want_write_file(filp);
2898 if (err)
2899 goto err_out;
2900
2901 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2902 range.pos_out, range.len);
2903
2904 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002905 if (err)
2906 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002907
2908 if (copy_to_user((struct f2fs_move_range __user *)arg,
2909 &range, sizeof(range)))
2910 err = -EFAULT;
2911err_out:
2912 fdput(dst);
2913 return err;
2914}
2915
Jaegeuk Kime066b832017-04-13 15:17:00 -07002916static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2917{
2918 struct inode *inode = file_inode(filp);
2919 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2920 struct sit_info *sm = SIT_I(sbi);
2921 unsigned int start_segno = 0, end_segno = 0;
2922 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2923 struct f2fs_flush_device range;
2924 int ret;
2925
2926 if (!capable(CAP_SYS_ADMIN))
2927 return -EPERM;
2928
2929 if (f2fs_readonly(sbi->sb))
2930 return -EROFS;
2931
Daniel Rosenberg43549942018-08-20 19:21:43 -07002932 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2933 return -EINVAL;
2934
Jaegeuk Kime066b832017-04-13 15:17:00 -07002935 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2936 sizeof(range)))
2937 return -EFAULT;
2938
Damien Le Moal09168782019-03-16 09:13:06 +09002939 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002940 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002941 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2942 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002943 return -EINVAL;
2944 }
2945
2946 ret = mnt_want_write_file(filp);
2947 if (ret)
2948 return ret;
2949
2950 if (range.dev_num != 0)
2951 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2952 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2953
2954 start_segno = sm->last_victim[FLUSH_DEVICE];
2955 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2956 start_segno = dev_start_segno;
2957 end_segno = min(start_segno + range.segments, dev_end_segno);
2958
2959 while (start_segno < end_segno) {
Chao Yufb24fea2020-01-14 19:36:50 +08002960 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002961 ret = -EBUSY;
2962 goto out;
2963 }
2964 sm->last_victim[GC_CB] = end_segno + 1;
2965 sm->last_victim[GC_GREEDY] = end_segno + 1;
2966 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2967 ret = f2fs_gc(sbi, true, true, start_segno);
2968 if (ret == -EAGAIN)
2969 ret = 0;
2970 else if (ret < 0)
2971 break;
2972 start_segno++;
2973 }
2974out:
2975 mnt_drop_write_file(filp);
2976 return ret;
2977}
2978
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002979static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2980{
2981 struct inode *inode = file_inode(filp);
2982 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2983
2984 /* Must validate to set it with SQLite behavior in Android. */
2985 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2986
2987 return put_user(sb_feature, (u32 __user *)arg);
2988}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002989
Chao Yu2c1d0302017-07-29 00:32:52 +08002990#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002991int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2992{
2993 struct dquot *transfer_to[MAXQUOTAS] = {};
2994 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2995 struct super_block *sb = sbi->sb;
2996 int err = 0;
2997
2998 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2999 if (!IS_ERR(transfer_to[PRJQUOTA])) {
3000 err = __dquot_transfer(inode, transfer_to);
3001 if (err)
3002 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3003 dqput(transfer_to[PRJQUOTA]);
3004 }
3005 return err;
3006}
3007
Chao Yu2c1d0302017-07-29 00:32:52 +08003008static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3009{
3010 struct inode *inode = file_inode(filp);
3011 struct f2fs_inode_info *fi = F2FS_I(inode);
3012 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003013 struct page *ipage;
3014 kprojid_t kprojid;
3015 int err;
3016
Chao Yu7beb01f2018-10-24 18:34:26 +08003017 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08003018 if (projid != F2FS_DEF_PROJID)
3019 return -EOPNOTSUPP;
3020 else
3021 return 0;
3022 }
3023
3024 if (!f2fs_has_extra_attr(inode))
3025 return -EOPNOTSUPP;
3026
3027 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3028
3029 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3030 return 0;
3031
Chao Yu2c1d0302017-07-29 00:32:52 +08003032 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003033 /* Is it quota file? Do not allow user to mess with it */
3034 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003035 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003036
Chao Yu4d57b862018-05-30 00:20:41 +08003037 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003038 if (IS_ERR(ipage))
3039 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003040
3041 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3042 i_projid)) {
3043 err = -EOVERFLOW;
3044 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003045 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003046 }
3047 f2fs_put_page(ipage, 1);
3048
Chao Yuc22aecd2018-04-21 17:53:52 +08003049 err = dquot_initialize(inode);
3050 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003051 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003052
Chao Yu78130812018-09-25 15:36:02 +08003053 f2fs_lock_op(sbi);
3054 err = f2fs_transfer_project_quota(inode, kprojid);
3055 if (err)
3056 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003057
3058 F2FS_I(inode)->i_projid = kprojid;
3059 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003060 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003061out_unlock:
3062 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003063 return err;
3064}
3065#else
Chao Yu78130812018-09-25 15:36:02 +08003066int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3067{
3068 return 0;
3069}
3070
Chao Yu2c1d0302017-07-29 00:32:52 +08003071static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3072{
3073 if (projid != F2FS_DEF_PROJID)
3074 return -EOPNOTSUPP;
3075 return 0;
3076}
3077#endif
3078
Eric Biggers36098552019-06-04 22:59:04 -07003079/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08003080
Eric Biggers36098552019-06-04 22:59:04 -07003081/*
3082 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3083 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3084 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3085 */
3086
3087static const struct {
3088 u32 iflag;
3089 u32 xflag;
3090} f2fs_xflags_map[] = {
3091 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3092 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3093 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3094 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3095 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3096 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3097};
3098
3099#define F2FS_SUPPORTED_XFLAGS ( \
3100 FS_XFLAG_SYNC | \
3101 FS_XFLAG_IMMUTABLE | \
3102 FS_XFLAG_APPEND | \
3103 FS_XFLAG_NODUMP | \
3104 FS_XFLAG_NOATIME | \
3105 FS_XFLAG_PROJINHERIT)
3106
3107/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3108static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3109{
3110 u32 xflags = 0;
3111 int i;
3112
3113 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3114 if (iflags & f2fs_xflags_map[i].iflag)
3115 xflags |= f2fs_xflags_map[i].xflag;
3116
Chao Yu2c1d0302017-07-29 00:32:52 +08003117 return xflags;
3118}
3119
Eric Biggers36098552019-06-04 22:59:04 -07003120/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3121static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003122{
Eric Biggers36098552019-06-04 22:59:04 -07003123 u32 iflags = 0;
3124 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08003125
Eric Biggers36098552019-06-04 22:59:04 -07003126 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3127 if (xflags & f2fs_xflags_map[i].xflag)
3128 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08003129
3130 return iflags;
3131}
3132
Eric Biggers6fc93c42019-07-01 13:26:29 -07003133static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3134{
3135 struct f2fs_inode_info *fi = F2FS_I(inode);
3136
3137 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3138
3139 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3140 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3141}
3142
Chao Yu2c1d0302017-07-29 00:32:52 +08003143static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3144{
3145 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08003146 struct fsxattr fa;
3147
Eric Biggers6fc93c42019-07-01 13:26:29 -07003148 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08003149
3150 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3151 return -EFAULT;
3152 return 0;
3153}
3154
3155static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3156{
3157 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003158 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07003159 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003160 int err;
3161
3162 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3163 return -EFAULT;
3164
3165 /* Make sure caller has proper permission */
3166 if (!inode_owner_or_capable(inode))
3167 return -EACCES;
3168
Eric Biggers36098552019-06-04 22:59:04 -07003169 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08003170 return -EOPNOTSUPP;
3171
Eric Biggers36098552019-06-04 22:59:04 -07003172 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3173 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003174 return -EOPNOTSUPP;
3175
3176 err = mnt_want_write_file(filp);
3177 if (err)
3178 return err;
3179
3180 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003181
3182 f2fs_fill_fsxattr(inode, &old_fa);
3183 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09003184 if (err)
3185 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07003186
Eric Biggers36098552019-06-04 22:59:04 -07003187 err = f2fs_setflags_common(inode, iflags,
3188 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08003189 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003190 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08003191
3192 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09003193out:
3194 inode_unlock(inode);
3195 mnt_drop_write_file(filp);
3196 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003197}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003198
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003199int f2fs_pin_file_control(struct inode *inode, bool inc)
3200{
3201 struct f2fs_inode_info *fi = F2FS_I(inode);
3202 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3203
3204 /* Use i_gc_failures for normal file as a risk signal. */
3205 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003206 f2fs_i_gc_failures_write(inode,
3207 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003208
Chao Yu2ef79ec2018-05-07 20:28:54 +08003209 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003210 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3211 __func__, inode->i_ino,
3212 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003213 clear_inode_flag(inode, FI_PIN_FILE);
3214 return -EAGAIN;
3215 }
3216 return 0;
3217}
3218
3219static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3220{
3221 struct inode *inode = file_inode(filp);
3222 __u32 pin;
3223 int ret = 0;
3224
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003225 if (get_user(pin, (__u32 __user *)arg))
3226 return -EFAULT;
3227
3228 if (!S_ISREG(inode->i_mode))
3229 return -EINVAL;
3230
3231 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3232 return -EROFS;
3233
3234 ret = mnt_want_write_file(filp);
3235 if (ret)
3236 return ret;
3237
3238 inode_lock(inode);
3239
Chao Yu4d57b862018-05-30 00:20:41 +08003240 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08003241 ret = -EINVAL;
3242 goto out;
3243 }
3244
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003245 if (!pin) {
3246 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003247 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003248 goto done;
3249 }
3250
3251 if (f2fs_pin_file_control(inode, false)) {
3252 ret = -EAGAIN;
3253 goto out;
3254 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003255
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003256 ret = f2fs_convert_inline_inode(inode);
3257 if (ret)
3258 goto out;
3259
Chao Yu4c8ff702019-11-01 18:07:14 +08003260 if (f2fs_disable_compressed_file(inode)) {
3261 ret = -EOPNOTSUPP;
3262 goto out;
3263 }
3264
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003265 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003266 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003267done:
3268 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3269out:
3270 inode_unlock(inode);
3271 mnt_drop_write_file(filp);
3272 return ret;
3273}
3274
3275static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3276{
3277 struct inode *inode = file_inode(filp);
3278 __u32 pin = 0;
3279
3280 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003281 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003282 return put_user(pin, (u32 __user *)arg);
3283}
3284
Chao Yuc4020b22018-01-11 14:42:30 +08003285int f2fs_precache_extents(struct inode *inode)
3286{
3287 struct f2fs_inode_info *fi = F2FS_I(inode);
3288 struct f2fs_map_blocks map;
3289 pgoff_t m_next_extent;
3290 loff_t end;
3291 int err;
3292
3293 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3294 return -EOPNOTSUPP;
3295
3296 map.m_lblk = 0;
3297 map.m_next_pgofs = NULL;
3298 map.m_next_extent = &m_next_extent;
3299 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003300 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003301 end = F2FS_I_SB(inode)->max_file_blocks;
3302
3303 while (map.m_lblk < end) {
3304 map.m_len = end - map.m_lblk;
3305
Chao Yub2532c62018-04-24 10:55:28 +08003306 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003307 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003308 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003309 if (err)
3310 return err;
3311
3312 map.m_lblk = m_next_extent;
3313 }
3314
3315 return err;
3316}
3317
3318static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3319{
3320 return f2fs_precache_extents(file_inode(filp));
3321}
3322
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003323static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3324{
3325 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3326 __u64 block_count;
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003327
3328 if (!capable(CAP_SYS_ADMIN))
3329 return -EPERM;
3330
3331 if (f2fs_readonly(sbi->sb))
3332 return -EROFS;
3333
3334 if (copy_from_user(&block_count, (void __user *)arg,
3335 sizeof(block_count)))
3336 return -EFAULT;
3337
Jaegeuk Kimb4b10062020-03-31 11:43:07 -07003338 return f2fs_resize_fs(sbi, block_count);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003339}
3340
Eric Biggers95ae2512019-07-22 09:26:24 -07003341static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3342{
3343 struct inode *inode = file_inode(filp);
3344
3345 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3346
3347 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3348 f2fs_warn(F2FS_I_SB(inode),
3349 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3350 inode->i_ino);
3351 return -EOPNOTSUPP;
3352 }
3353
3354 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3355}
3356
3357static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3358{
3359 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3360 return -EOPNOTSUPP;
3361
3362 return fsverity_ioctl_measure(filp, (void __user *)arg);
3363}
3364
Eric Biggers3357af82020-07-14 15:18:12 -07003365static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003366{
3367 struct inode *inode = file_inode(filp);
3368 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3369 char *vbuf;
3370 int count;
3371 int err = 0;
3372
3373 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3374 if (!vbuf)
3375 return -ENOMEM;
3376
3377 down_read(&sbi->sb_lock);
3378 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3379 ARRAY_SIZE(sbi->raw_super->volume_name),
3380 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3381 up_read(&sbi->sb_lock);
3382
3383 if (copy_to_user((char __user *)arg, vbuf,
3384 min(FSLABEL_MAX, count)))
3385 err = -EFAULT;
3386
3387 kvfree(vbuf);
3388 return err;
3389}
3390
Eric Biggers3357af82020-07-14 15:18:12 -07003391static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003392{
3393 struct inode *inode = file_inode(filp);
3394 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3395 char *vbuf;
3396 int err = 0;
3397
3398 if (!capable(CAP_SYS_ADMIN))
3399 return -EPERM;
3400
3401 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3402 if (IS_ERR(vbuf))
3403 return PTR_ERR(vbuf);
3404
3405 err = mnt_want_write_file(filp);
3406 if (err)
3407 goto out;
3408
3409 down_write(&sbi->sb_lock);
3410
3411 memset(sbi->raw_super->volume_name, 0,
3412 sizeof(sbi->raw_super->volume_name));
3413 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3414 sbi->raw_super->volume_name,
3415 ARRAY_SIZE(sbi->raw_super->volume_name));
3416
3417 err = f2fs_commit_super(sbi, false);
3418
3419 up_write(&sbi->sb_lock);
3420
3421 mnt_drop_write_file(filp);
3422out:
3423 kfree(vbuf);
3424 return err;
3425}
3426
Chao Yu439dfb12020-02-21 18:09:21 +08003427static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3428{
3429 struct inode *inode = file_inode(filp);
3430 __u64 blocks;
3431
3432 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3433 return -EOPNOTSUPP;
3434
3435 if (!f2fs_compressed_file(inode))
3436 return -EINVAL;
3437
3438 blocks = F2FS_I(inode)->i_compr_blocks;
3439 return put_user(blocks, (u64 __user *)arg);
3440}
3441
Chao Yuef8d5632020-03-06 15:36:09 +08003442static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3443{
3444 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3445 unsigned int released_blocks = 0;
3446 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3447 block_t blkaddr;
3448 int i;
3449
3450 for (i = 0; i < count; i++) {
3451 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3452 dn->ofs_in_node + i);
3453
3454 if (!__is_valid_data_blkaddr(blkaddr))
3455 continue;
3456 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3457 DATA_GENERIC_ENHANCE)))
3458 return -EFSCORRUPTED;
3459 }
3460
3461 while (count) {
3462 int compr_blocks = 0;
3463
3464 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3465 blkaddr = f2fs_data_blkaddr(dn);
3466
3467 if (i == 0) {
3468 if (blkaddr == COMPRESS_ADDR)
3469 continue;
3470 dn->ofs_in_node += cluster_size;
3471 goto next;
3472 }
3473
3474 if (__is_valid_data_blkaddr(blkaddr))
3475 compr_blocks++;
3476
3477 if (blkaddr != NEW_ADDR)
3478 continue;
3479
3480 dn->data_blkaddr = NULL_ADDR;
3481 f2fs_set_data_blkaddr(dn);
3482 }
3483
3484 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3485 dec_valid_block_count(sbi, dn->inode,
3486 cluster_size - compr_blocks);
3487
3488 released_blocks += cluster_size - compr_blocks;
3489next:
3490 count -= cluster_size;
3491 }
3492
3493 return released_blocks;
3494}
3495
3496static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3497{
3498 struct inode *inode = file_inode(filp);
3499 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3500 pgoff_t page_idx = 0, last_idx;
3501 unsigned int released_blocks = 0;
3502 int ret;
3503 int writecount;
3504
3505 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3506 return -EOPNOTSUPP;
3507
3508 if (!f2fs_compressed_file(inode))
3509 return -EINVAL;
3510
3511 if (f2fs_readonly(sbi->sb))
3512 return -EROFS;
3513
3514 ret = mnt_want_write_file(filp);
3515 if (ret)
3516 return ret;
3517
3518 f2fs_balance_fs(F2FS_I_SB(inode), true);
3519
3520 inode_lock(inode);
3521
3522 writecount = atomic_read(&inode->i_writecount);
3523 if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
3524 ret = -EBUSY;
3525 goto out;
3526 }
3527
3528 if (IS_IMMUTABLE(inode)) {
3529 ret = -EINVAL;
3530 goto out;
3531 }
3532
3533 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3534 if (ret)
3535 goto out;
3536
3537 if (!F2FS_I(inode)->i_compr_blocks)
3538 goto out;
3539
3540 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3541 f2fs_set_inode_flags(inode);
3542 inode->i_ctime = current_time(inode);
3543 f2fs_mark_inode_dirty_sync(inode, true);
3544
3545 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3546 down_write(&F2FS_I(inode)->i_mmap_sem);
3547
3548 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3549
3550 while (page_idx < last_idx) {
3551 struct dnode_of_data dn;
3552 pgoff_t end_offset, count;
3553
3554 set_new_dnode(&dn, inode, NULL, NULL, 0);
3555 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3556 if (ret) {
3557 if (ret == -ENOENT) {
3558 page_idx = f2fs_get_next_page_offset(&dn,
3559 page_idx);
3560 ret = 0;
3561 continue;
3562 }
3563 break;
3564 }
3565
3566 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3567 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003568 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuef8d5632020-03-06 15:36:09 +08003569
3570 ret = release_compress_blocks(&dn, count);
3571
3572 f2fs_put_dnode(&dn);
3573
3574 if (ret < 0)
3575 break;
3576
3577 page_idx += count;
3578 released_blocks += ret;
3579 }
3580
3581 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3582 up_write(&F2FS_I(inode)->i_mmap_sem);
3583out:
3584 inode_unlock(inode);
3585
3586 mnt_drop_write_file(filp);
3587
3588 if (ret >= 0) {
3589 ret = put_user(released_blocks, (u64 __user *)arg);
3590 } else if (released_blocks && F2FS_I(inode)->i_compr_blocks) {
3591 set_sbi_flag(sbi, SBI_NEED_FSCK);
3592 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3593 "iblocks=%llu, released=%u, compr_blocks=%llu, "
3594 "run fsck to fix.",
3595 __func__, inode->i_ino, inode->i_blocks,
3596 released_blocks,
3597 F2FS_I(inode)->i_compr_blocks);
3598 }
3599
3600 return ret;
3601}
3602
Chao Yuc75488f2020-03-06 14:35:33 +08003603static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3604{
3605 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3606 unsigned int reserved_blocks = 0;
3607 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3608 block_t blkaddr;
3609 int i;
3610
3611 for (i = 0; i < count; i++) {
3612 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3613 dn->ofs_in_node + i);
3614
3615 if (!__is_valid_data_blkaddr(blkaddr))
3616 continue;
3617 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3618 DATA_GENERIC_ENHANCE)))
3619 return -EFSCORRUPTED;
3620 }
3621
3622 while (count) {
3623 int compr_blocks = 0;
3624 blkcnt_t reserved;
3625 int ret;
3626
3627 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3628 blkaddr = f2fs_data_blkaddr(dn);
3629
3630 if (i == 0) {
3631 if (blkaddr == COMPRESS_ADDR)
3632 continue;
3633 dn->ofs_in_node += cluster_size;
3634 goto next;
3635 }
3636
3637 if (__is_valid_data_blkaddr(blkaddr)) {
3638 compr_blocks++;
3639 continue;
3640 }
3641
3642 dn->data_blkaddr = NEW_ADDR;
3643 f2fs_set_data_blkaddr(dn);
3644 }
3645
3646 reserved = cluster_size - compr_blocks;
3647 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3648 if (ret)
3649 return ret;
3650
3651 if (reserved != cluster_size - compr_blocks)
3652 return -ENOSPC;
3653
3654 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3655
3656 reserved_blocks += reserved;
3657next:
3658 count -= cluster_size;
3659 }
3660
3661 return reserved_blocks;
3662}
3663
3664static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3665{
3666 struct inode *inode = file_inode(filp);
3667 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3668 pgoff_t page_idx = 0, last_idx;
3669 unsigned int reserved_blocks = 0;
3670 int ret;
3671
3672 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3673 return -EOPNOTSUPP;
3674
3675 if (!f2fs_compressed_file(inode))
3676 return -EINVAL;
3677
3678 if (f2fs_readonly(sbi->sb))
3679 return -EROFS;
3680
3681 ret = mnt_want_write_file(filp);
3682 if (ret)
3683 return ret;
3684
3685 if (F2FS_I(inode)->i_compr_blocks)
3686 goto out;
3687
3688 f2fs_balance_fs(F2FS_I_SB(inode), true);
3689
3690 inode_lock(inode);
3691
3692 if (!IS_IMMUTABLE(inode)) {
3693 ret = -EINVAL;
3694 goto unlock_inode;
3695 }
3696
3697 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3698 down_write(&F2FS_I(inode)->i_mmap_sem);
3699
3700 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3701
3702 while (page_idx < last_idx) {
3703 struct dnode_of_data dn;
3704 pgoff_t end_offset, count;
3705
3706 set_new_dnode(&dn, inode, NULL, NULL, 0);
3707 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3708 if (ret) {
3709 if (ret == -ENOENT) {
3710 page_idx = f2fs_get_next_page_offset(&dn,
3711 page_idx);
3712 ret = 0;
3713 continue;
3714 }
3715 break;
3716 }
3717
3718 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3719 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003720 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuc75488f2020-03-06 14:35:33 +08003721
3722 ret = reserve_compress_blocks(&dn, count);
3723
3724 f2fs_put_dnode(&dn);
3725
3726 if (ret < 0)
3727 break;
3728
3729 page_idx += count;
3730 reserved_blocks += ret;
3731 }
3732
3733 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3734 up_write(&F2FS_I(inode)->i_mmap_sem);
3735
3736 if (ret >= 0) {
3737 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3738 f2fs_set_inode_flags(inode);
3739 inode->i_ctime = current_time(inode);
3740 f2fs_mark_inode_dirty_sync(inode, true);
3741 }
3742unlock_inode:
3743 inode_unlock(inode);
3744out:
3745 mnt_drop_write_file(filp);
3746
3747 if (ret >= 0) {
3748 ret = put_user(reserved_blocks, (u64 __user *)arg);
3749 } else if (reserved_blocks && F2FS_I(inode)->i_compr_blocks) {
3750 set_sbi_flag(sbi, SBI_NEED_FSCK);
3751 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3752 "iblocks=%llu, reserved=%u, compr_blocks=%llu, "
3753 "run fsck to fix.",
3754 __func__, inode->i_ino, inode->i_blocks,
3755 reserved_blocks,
3756 F2FS_I(inode)->i_compr_blocks);
3757 }
3758
3759 return ret;
3760}
3761
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003762long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3763{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003764 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3765 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08003766 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
3767 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003768
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003769 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07003770 case FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003771 return f2fs_ioc_getflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003772 case FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003773 return f2fs_ioc_setflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003774 case FS_IOC_GETVERSION:
Chao Yud49f3e82015-01-23 20:36:04 +08003775 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003776 case F2FS_IOC_START_ATOMIC_WRITE:
3777 return f2fs_ioc_start_atomic_write(filp);
3778 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3779 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07003780 case F2FS_IOC_START_VOLATILE_WRITE:
3781 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08003782 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3783 return f2fs_ioc_release_volatile_write(filp);
3784 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3785 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08003786 case F2FS_IOC_SHUTDOWN:
3787 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003788 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003789 return f2fs_ioc_fitrim(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003790 case FS_IOC_SET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003791 return f2fs_ioc_set_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003792 case FS_IOC_GET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003793 return f2fs_ioc_get_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003794 case FS_IOC_GET_ENCRYPTION_PWSALT:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003795 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07003796 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3797 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3798 case FS_IOC_ADD_ENCRYPTION_KEY:
3799 return f2fs_ioc_add_encryption_key(filp, arg);
3800 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3801 return f2fs_ioc_remove_encryption_key(filp, arg);
3802 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3803 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
3804 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3805 return f2fs_ioc_get_encryption_key_status(filp, arg);
Eric Biggersee446e12020-03-14 13:50:51 -07003806 case FS_IOC_GET_ENCRYPTION_NONCE:
3807 return f2fs_ioc_get_encryption_nonce(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003808 case F2FS_IOC_GARBAGE_COLLECT:
3809 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003810 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3811 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08003812 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08003813 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08003814 case F2FS_IOC_DEFRAGMENT:
3815 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003816 case F2FS_IOC_MOVE_RANGE:
3817 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07003818 case F2FS_IOC_FLUSH_DEVICE:
3819 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003820 case F2FS_IOC_GET_FEATURES:
3821 return f2fs_ioc_get_features(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003822 case FS_IOC_FSGETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08003823 return f2fs_ioc_fsgetxattr(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003824 case FS_IOC_FSSETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08003825 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003826 case F2FS_IOC_GET_PIN_FILE:
3827 return f2fs_ioc_get_pin_file(filp, arg);
3828 case F2FS_IOC_SET_PIN_FILE:
3829 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08003830 case F2FS_IOC_PRECACHE_EXTENTS:
3831 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003832 case F2FS_IOC_RESIZE_FS:
3833 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07003834 case FS_IOC_ENABLE_VERITY:
3835 return f2fs_ioc_enable_verity(filp, arg);
3836 case FS_IOC_MEASURE_VERITY:
3837 return f2fs_ioc_measure_verity(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003838 case FS_IOC_GETFSLABEL:
3839 return f2fs_ioc_getfslabel(filp, arg);
3840 case FS_IOC_SETFSLABEL:
3841 return f2fs_ioc_setfslabel(filp, arg);
Chao Yu439dfb12020-02-21 18:09:21 +08003842 case F2FS_IOC_GET_COMPRESS_BLOCKS:
3843 return f2fs_get_compress_blocks(filp, arg);
Chao Yuef8d5632020-03-06 15:36:09 +08003844 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
3845 return f2fs_release_compress_blocks(filp, arg);
Chao Yuc75488f2020-03-06 14:35:33 +08003846 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
3847 return f2fs_reserve_compress_blocks(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003848 default:
3849 return -ENOTTY;
3850 }
3851}
3852
Chao Yu4c8ff702019-11-01 18:07:14 +08003853static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
3854{
3855 struct file *file = iocb->ki_filp;
3856 struct inode *inode = file_inode(file);
Chao Yu8b83ac82020-04-16 18:16:56 +08003857 int ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08003858
3859 if (!f2fs_is_compress_backend_ready(inode))
3860 return -EOPNOTSUPP;
3861
Chao Yu8b83ac82020-04-16 18:16:56 +08003862 ret = generic_file_read_iter(iocb, iter);
3863
3864 if (ret > 0)
3865 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
3866
3867 return ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08003868}
3869
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003870static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3871{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003872 struct file *file = iocb->ki_filp;
3873 struct inode *inode = file_inode(file);
3874 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003875
Chao Yu126ce722019-04-02 18:52:22 +08003876 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
3877 ret = -EIO;
3878 goto out;
3879 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003880
Chao Yu7bd29352020-02-24 19:20:15 +08003881 if (!f2fs_is_compress_backend_ready(inode)) {
3882 ret = -EOPNOTSUPP;
3883 goto out;
3884 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003885
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003886 if (iocb->ki_flags & IOCB_NOWAIT) {
3887 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08003888 ret = -EAGAIN;
3889 goto out;
3890 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003891 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003892 inode_lock(inode);
3893 }
3894
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003895 ret = generic_write_checks(iocb, from);
3896 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003897 bool preallocated = false;
3898 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003899 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003900
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003901 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3902 set_inode_flag(inode, FI_NO_PREALLOC);
3903
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003904 if ((iocb->ki_flags & IOCB_NOWAIT)) {
3905 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003906 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003907 f2fs_has_inline_data(inode) ||
3908 f2fs_force_buffered_io(inode, iocb, from)) {
3909 clear_inode_flag(inode, FI_NO_PREALLOC);
3910 inode_unlock(inode);
3911 ret = -EAGAIN;
3912 goto out;
3913 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003914 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003915 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003916
3917 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
3918 goto write;
3919
3920 if (iocb->ki_flags & IOCB_DIRECT) {
3921 /*
3922 * Convert inline data for Direct I/O before entering
3923 * f2fs_direct_IO().
3924 */
3925 err = f2fs_convert_inline_inode(inode);
3926 if (err)
3927 goto out_err;
3928 /*
3929 * If force_buffere_io() is true, we have to allocate
3930 * blocks all the time, since f2fs_direct_IO will fall
3931 * back to buffered IO.
3932 */
3933 if (!f2fs_force_buffered_io(inode, iocb, from) &&
3934 allow_outplace_dio(inode, iocb, from))
3935 goto write;
3936 }
3937 preallocated = true;
3938 target_size = iocb->ki_pos + iov_iter_count(from);
3939
3940 err = f2fs_preallocate_blocks(iocb, from);
3941 if (err) {
3942out_err:
3943 clear_inode_flag(inode, FI_NO_PREALLOC);
3944 inode_unlock(inode);
3945 ret = err;
3946 goto out;
3947 }
3948write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003949 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003950 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003951
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003952 /* if we couldn't write data, we should deallocate blocks. */
3953 if (preallocated && i_size_read(inode) < target_size)
3954 f2fs_truncate(inode);
3955
Chao Yub0af6d42017-08-02 23:21:48 +08003956 if (ret > 0)
3957 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003958 }
3959 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08003960out:
3961 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
3962 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07003963 if (ret > 0)
3964 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003965 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003966}
3967
Namjae Jeone9750822013-02-04 23:41:41 +09003968#ifdef CONFIG_COMPAT
3969long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3970{
3971 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07003972 case FS_IOC32_GETFLAGS:
3973 cmd = FS_IOC_GETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09003974 break;
Eric Biggers3357af82020-07-14 15:18:12 -07003975 case FS_IOC32_SETFLAGS:
3976 cmd = FS_IOC_SETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09003977 break;
Eric Biggers3357af82020-07-14 15:18:12 -07003978 case FS_IOC32_GETVERSION:
3979 cmd = FS_IOC_GETVERSION;
Chao Yu04ef4b62015-11-10 18:44:20 +08003980 break;
3981 case F2FS_IOC_START_ATOMIC_WRITE:
3982 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3983 case F2FS_IOC_START_VOLATILE_WRITE:
3984 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3985 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3986 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02003987 case FITRIM:
Eric Biggers3357af82020-07-14 15:18:12 -07003988 case FS_IOC_SET_ENCRYPTION_POLICY:
3989 case FS_IOC_GET_ENCRYPTION_PWSALT:
3990 case FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07003991 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3992 case FS_IOC_ADD_ENCRYPTION_KEY:
3993 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3994 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3995 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Eric Biggersee446e12020-03-14 13:50:51 -07003996 case FS_IOC_GET_ENCRYPTION_NONCE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003997 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003998 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003999 case F2FS_IOC_WRITE_CHECKPOINT:
4000 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004001 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07004002 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004003 case F2FS_IOC_GET_FEATURES:
Eric Biggers3357af82020-07-14 15:18:12 -07004004 case FS_IOC_FSGETXATTR:
4005 case FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004006 case F2FS_IOC_GET_PIN_FILE:
4007 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08004008 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004009 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07004010 case FS_IOC_ENABLE_VERITY:
4011 case FS_IOC_MEASURE_VERITY:
Eric Biggers3357af82020-07-14 15:18:12 -07004012 case FS_IOC_GETFSLABEL:
4013 case FS_IOC_SETFSLABEL:
Chao Yu439dfb12020-02-21 18:09:21 +08004014 case F2FS_IOC_GET_COMPRESS_BLOCKS:
Chao Yuef8d5632020-03-06 15:36:09 +08004015 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
Chao Yuc75488f2020-03-06 14:35:33 +08004016 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004017 break;
Namjae Jeone9750822013-02-04 23:41:41 +09004018 default:
4019 return -ENOIOCTLCMD;
4020 }
4021 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4022}
4023#endif
4024
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004025const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08004026 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08004027 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004028 .write_iter = f2fs_file_write_iter,
4029 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08004030 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004031 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07004032 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004033 .fsync = f2fs_sync_file,
4034 .fallocate = f2fs_fallocate,
4035 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09004036#ifdef CONFIG_COMPAT
4037 .compat_ioctl = f2fs_compat_ioctl,
4038#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004039 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04004040 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004041};