blob: 8a422400e824d71ac3a47e473ab38b83fc002e05 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/stat.h>
11#include <linux/buffer_head.h>
12#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090013#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090014#include <linux/falloc.h>
15#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090016#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/uaccess.h>
18#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090019#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080020#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070021#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070022#include <linux/file.h>
Chao Yu4507847c2019-07-17 17:06:11 +080023#include <linux/nls.h>
Daeho Jeong9af84642020-07-21 12:21:11 +090024#include <linux/sched/signal.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090025
26#include "f2fs.h"
27#include "node.h"
28#include "segment.h"
29#include "xattr.h"
30#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080031#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080032#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090033#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090034
Souptick Joarderea4d4792018-04-15 01:40:02 +053035static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080036{
37 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053038 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080039
40 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053041 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080042 up_read(&F2FS_I(inode)->i_mmap_sem);
43
Chao Yu8b83ac82020-04-16 18:16:56 +080044 if (!ret)
45 f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
46 F2FS_BLKSIZE);
47
Chao Yud7648342019-04-15 15:22:19 +080048 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
49
Souptick Joarderea4d4792018-04-15 01:40:02 +053050 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080051}
52
Souptick Joarderea4d4792018-04-15 01:40:02 +053053static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090054{
55 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080056 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070057 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080058 struct dnode_of_data dn;
Chao Yu4c8ff702019-11-01 18:07:14 +080059 bool need_alloc = true;
60 int err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090061
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020062 if (unlikely(f2fs_cp_error(sbi))) {
63 err = -EIO;
64 goto err;
65 }
66
Chao Yu00e09c02019-08-23 17:58:36 +080067 if (!f2fs_is_checkpoint_ready(sbi)) {
68 err = -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +080069 goto err;
Chao Yu00e09c02019-08-23 17:58:36 +080070 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090071
Chao Yu4c8ff702019-11-01 18:07:14 +080072#ifdef CONFIG_F2FS_FS_COMPRESSION
73 if (f2fs_compressed_file(inode)) {
74 int ret = f2fs_is_compressed_cluster(inode, page->index);
75
76 if (ret < 0) {
77 err = ret;
78 goto err;
79 } else if (ret) {
80 if (ret < F2FS_I(inode)->i_cluster_size) {
81 err = -EAGAIN;
82 goto err;
83 }
84 need_alloc = false;
85 }
86 }
87#endif
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080088 /* should do out of any locked page */
Chao Yu4c8ff702019-11-01 18:07:14 +080089 if (need_alloc)
90 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080091
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090092 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070093
94 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070095
Dave Jiang11bac802017-02-24 14:56:41 -080096 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080097 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090098 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090099 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900100 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900101 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900102 unlock_page(page);
103 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800104 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900105 }
106
Chao Yu4c8ff702019-11-01 18:07:14 +0800107 if (need_alloc) {
108 /* block allocation */
Chao Yu0ef81832020-06-18 14:36:22 +0800109 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +0800110 set_new_dnode(&dn, inode, NULL, NULL, 0);
111 err = f2fs_get_block(&dn, page->index);
112 f2fs_put_dnode(&dn);
Chao Yu0ef81832020-06-18 14:36:22 +0800113 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu39a86952018-09-27 18:33:18 +0800114 }
115
Chao Yu06c75402020-03-02 17:34:27 +0800116#ifdef CONFIG_F2FS_FS_COMPRESSION
117 if (!need_alloc) {
118 set_new_dnode(&dn, inode, NULL, NULL, 0);
119 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
120 f2fs_put_dnode(&dn);
121 }
122#endif
123 if (err) {
124 unlock_page(page);
125 goto out_sem;
Chao Yu39a86952018-09-27 18:33:18 +0800126 }
127
Chao Yubae0ee72018-12-25 17:43:42 +0800128 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +0800129
130 /* wait for GCed page writeback via META_MAPPING */
131 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
132
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900133 /*
134 * check to see if the page is mapped already (no holes)
135 */
136 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800137 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900138
139 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300140 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800141 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900142 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900143
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300144 offset = i_size_read(inode) & ~PAGE_MASK;
145 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900146 }
147 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700148 if (!PageUptodate(page))
149 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900150
Chao Yub0af6d42017-08-02 23:21:48 +0800151 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530152 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800153
Jaegeuk Kime943a102013-10-25 14:26:31 +0900154 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800155out_sem:
156 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800157
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900158 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200159err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900160 return block_page_mkwrite_return(err);
161}
162
163static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800164 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700165 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900166 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900167};
168
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900169static int get_parent_ino(struct inode *inode, nid_t *pino)
170{
171 struct dentry *dentry;
172
Eric Biggers84c9c2d2020-05-05 11:41:11 -0700173 /*
174 * Make sure to get the non-deleted alias. The alias associated with
175 * the open file descriptor being fsync()'ed may be deleted already.
176 */
177 dentry = d_find_alias(inode);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900178 if (!dentry)
179 return 0;
180
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900181 *pino = parent_ino(dentry);
182 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900183 return 1;
184}
185
Chao Yua5fd5052017-11-06 22:51:45 +0800186static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800187{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700188 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800189 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800190
Chao Yua5fd5052017-11-06 22:51:45 +0800191 if (!S_ISREG(inode->i_mode))
192 cp_reason = CP_NON_REGULAR;
Chao Yu4c8ff702019-11-01 18:07:14 +0800193 else if (f2fs_compressed_file(inode))
194 cp_reason = CP_COMPRESSED;
Chao Yua5fd5052017-11-06 22:51:45 +0800195 else if (inode->i_nlink != 1)
196 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700197 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800198 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800199 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800200 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800201 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800202 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800203 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800204 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700205 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800206 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800207 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800208 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800209 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800210 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
211 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
212 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800213 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800214
Chao Yua5fd5052017-11-06 22:51:45 +0800215 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800216}
217
Changman Lee9c7bb702014-12-08 15:29:40 +0900218static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
219{
220 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
221 bool ret = false;
222 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800223 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900224 ret = true;
225 f2fs_put_page(i, 0);
226 return ret;
227}
228
Changman Lee51455b12014-12-08 15:29:41 +0900229static void try_to_fix_pino(struct inode *inode)
230{
231 struct f2fs_inode_info *fi = F2FS_I(inode);
232 nid_t pino;
233
234 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900235 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
236 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700237 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900238 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900239 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700240 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900241}
242
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700243static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
244 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900245{
246 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700247 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700248 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900249 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800250 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900251 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900252 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900253 .nr_to_write = LONG_MAX,
254 .for_reclaim = 0,
255 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800256 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900257
Daniel Rosenberg43549942018-08-20 19:21:43 -0700258 if (unlikely(f2fs_readonly(inode->i_sb) ||
259 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900260 return 0;
261
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900262 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700263
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800264 if (S_ISDIR(inode->i_mode))
265 goto go_write;
266
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700267 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800268 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700269 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400270 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700271 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700272
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900273 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800274 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900275 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900276 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900277
Changman Lee9c7bb702014-12-08 15:29:40 +0900278 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800279 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700280 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900281 goto go_write;
282 }
283
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700284 /*
285 * if there is no written data, don't waste time to write recovery info.
286 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700287 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800288 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700289
Changman Lee9c7bb702014-12-08 15:29:40 +0900290 /* it may call write_inode just prior to fsync */
291 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700292 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700293
Jaegeuk Kim91942322016-05-20 10:13:22 -0700294 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800295 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700296 goto flush_out;
297 goto out;
298 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700299go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900300 /*
301 * Both of fdatasync() and fsync() are able to be recovered from
302 * sudden-power-off.
303 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700304 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800305 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700306 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900307
Chao Yua5fd5052017-11-06 22:51:45 +0800308 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900309 /* all the dirty node pages should be flushed for POR */
310 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900311
Changman Lee51455b12014-12-08 15:29:41 +0900312 /*
313 * We've secured consistency through sync_fs. Following pino
314 * will be used only for fsynced inodes after checkpoint.
315 */
316 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700317 clear_inode_flag(inode, FI_APPEND_WRITE);
318 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900319 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900320 }
Changman Lee51455b12014-12-08 15:29:41 +0900321sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800322 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800323 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800324 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700325 if (ret)
326 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900327
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800328 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800329 if (unlikely(f2fs_cp_error(sbi))) {
330 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800331 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800332 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800333
Chao Yu4d57b862018-05-30 00:20:41 +0800334 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700335 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900336 f2fs_write_inode(inode, NULL);
337 goto sync_nodes;
338 }
339
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700340 /*
341 * If it's atomic_write, it's just fine to keep write ordering. So
342 * here we don't need to wait for node write completion, since we use
343 * node chain which serializes node blocks. If one of node writes are
344 * reordered, we can see simply broken chain, resulting in stopping
345 * roll-forward recovery. It means we'll recover all or none node blocks
346 * given fsync mark.
347 */
348 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800349 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700350 if (ret)
351 goto out;
352 }
Changman Lee51455b12014-12-08 15:29:41 +0900353
354 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800355 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700356 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900357flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700358 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800359 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800360 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800361 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800362 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800363 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800364 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800365 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900366out:
Chao Yua5fd5052017-11-06 22:51:45 +0800367 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700368 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900369 return ret;
370}
371
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700372int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
373{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200374 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
375 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700376 return f2fs_do_sync_file(file, start, end, datasync, false);
377}
378
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900379static pgoff_t __get_first_dirty_index(struct address_space *mapping,
380 pgoff_t pgofs, int whence)
381{
Jan Kara8faab642017-11-15 17:34:55 -0800382 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900383 int nr_pages;
384
385 if (whence != SEEK_DATA)
386 return 0;
387
388 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800389 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
390 1, &page);
391 if (!nr_pages)
392 return ULONG_MAX;
393 pgofs = page->index;
394 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900395 return pgofs;
396}
397
Chao Yue1da7872018-06-05 17:44:11 +0800398static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
399 pgoff_t dirty, pgoff_t pgofs, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900400{
401 switch (whence) {
402 case SEEK_DATA:
403 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yu93770ab2019-04-15 15:26:32 +0800404 __is_valid_data_blkaddr(blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900405 return true;
406 break;
407 case SEEK_HOLE:
408 if (blkaddr == NULL_ADDR)
409 return true;
410 break;
411 }
412 return false;
413}
414
Chao Yu267378d2014-04-23 14:10:24 +0800415static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
416{
417 struct inode *inode = file->f_mapping->host;
418 loff_t maxbytes = inode->i_sb->s_maxbytes;
419 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900420 pgoff_t pgofs, end_offset, dirty;
421 loff_t data_ofs = offset;
422 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800423 int err = 0;
424
Al Viro59551022016-01-22 15:40:57 -0500425 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800426
427 isize = i_size_read(inode);
428 if (offset >= isize)
429 goto fail;
430
431 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800432 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800433 if (whence == SEEK_HOLE)
434 data_ofs = isize;
435 goto found;
436 }
437
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300438 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800439
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900440 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
441
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300442 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800443 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800444 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800445 if (err && err != -ENOENT) {
446 goto fail;
447 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900448 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800449 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800450 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800451 continue;
452 } else {
453 goto found;
454 }
455 }
456
Chao Yu81ca7352016-01-26 15:39:35 +0800457 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800458
459 /* find data/hole in dnode block */
460 for (; dn.ofs_in_node < end_offset;
461 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300462 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800463 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900464
Chao Yua2ced1c2020-02-14 17:44:10 +0800465 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu267378d2014-04-23 14:10:24 +0800466
Chao Yuc9b60782018-08-01 19:13:44 +0800467 if (__is_valid_data_blkaddr(blkaddr) &&
468 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800469 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800470 f2fs_put_dnode(&dn);
471 goto fail;
472 }
473
Chao Yue1da7872018-06-05 17:44:11 +0800474 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
475 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800476 f2fs_put_dnode(&dn);
477 goto found;
478 }
479 }
480 f2fs_put_dnode(&dn);
481 }
482
483 if (whence == SEEK_DATA)
484 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800485found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900486 if (whence == SEEK_HOLE && data_ofs > isize)
487 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500488 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800489 return vfs_setpos(file, data_ofs, maxbytes);
490fail:
Al Viro59551022016-01-22 15:40:57 -0500491 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800492 return -ENXIO;
493}
494
495static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
496{
497 struct inode *inode = file->f_mapping->host;
498 loff_t maxbytes = inode->i_sb->s_maxbytes;
499
500 switch (whence) {
501 case SEEK_SET:
502 case SEEK_CUR:
503 case SEEK_END:
504 return generic_file_llseek_size(file, offset, whence,
505 maxbytes, i_size_read(inode));
506 case SEEK_DATA:
507 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700508 if (offset < 0)
509 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800510 return f2fs_seek_block(file, offset, whence);
511 }
512
513 return -EINVAL;
514}
515
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900516static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
517{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700518 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800519 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700520
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200521 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
522 return -EIO;
523
Chao Yu4c8ff702019-11-01 18:07:14 +0800524 if (!f2fs_is_compress_backend_ready(inode))
525 return -EOPNOTSUPP;
526
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700527 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800528 err = f2fs_convert_inline_inode(inode);
529 if (err)
530 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700531
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 file_accessed(file);
533 vma->vm_ops = &f2fs_file_vm_ops;
Chao Yu4c8ff702019-11-01 18:07:14 +0800534 set_inode_flag(inode, FI_MMAP_FILE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900535 return 0;
536}
537
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700538static int f2fs_file_open(struct inode *inode, struct file *filp)
539{
Eric Biggers2e168c82017-11-29 12:35:28 -0800540 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700541
Eric Biggers2e168c82017-11-29 12:35:28 -0800542 if (err)
543 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900544
Chao Yu4c8ff702019-11-01 18:07:14 +0800545 if (!f2fs_is_compress_backend_ready(inode))
546 return -EOPNOTSUPP;
547
Eric Biggers95ae2512019-07-22 09:26:24 -0700548 err = fsverity_file_open(inode, filp);
549 if (err)
550 return err;
551
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900552 filp->f_mode |= FMODE_NOWAIT;
553
Chao Yu0abd6752017-07-09 00:13:07 +0800554 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700555}
556
Chao Yu4d57b862018-05-30 00:20:41 +0800557void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900558{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700559 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900560 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800561 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900562 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800563 int base = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800564 bool compressed_cluster = false;
565 int cluster_index = 0, valid_blocks = 0;
566 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
Chao Yuef8d5632020-03-06 15:36:09 +0800567 bool released = !F2FS_I(dn->inode)->i_compr_blocks;
Chao Yu7a2af762017-07-19 00:19:06 +0800568
569 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
570 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900571
Gu Zheng45590712013-07-15 17:57:38 +0800572 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800573 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900574
Chao Yu4c8ff702019-11-01 18:07:14 +0800575 /* Assumption: truncateion starts with cluster */
576 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900577 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900578
Chao Yu4c8ff702019-11-01 18:07:14 +0800579 if (f2fs_compressed_file(dn->inode) &&
580 !(cluster_index & (cluster_size - 1))) {
581 if (compressed_cluster)
582 f2fs_i_compr_blocks_update(dn->inode,
583 valid_blocks, false);
584 compressed_cluster = (blkaddr == COMPRESS_ADDR);
585 valid_blocks = 0;
586 }
587
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900588 if (blkaddr == NULL_ADDR)
589 continue;
590
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800591 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800592 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800593
Chao Yu4c8ff702019-11-01 18:07:14 +0800594 if (__is_valid_data_blkaddr(blkaddr)) {
595 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800596 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800597 continue;
598 if (compressed_cluster)
599 valid_blocks++;
600 }
Chao Yuc9b60782018-08-01 19:13:44 +0800601
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700602 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700603 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800604
605 f2fs_invalidate_blocks(sbi, blkaddr);
Chao Yuef8d5632020-03-06 15:36:09 +0800606
607 if (!released || blkaddr != COMPRESS_ADDR)
608 nr_free++;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900609 }
Chao Yu19b2c302015-08-26 20:34:48 +0800610
Chao Yu4c8ff702019-11-01 18:07:14 +0800611 if (compressed_cluster)
612 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
613
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900614 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800615 pgoff_t fofs;
616 /*
617 * once we invalidate valid blkaddr in range [ofs, ofs + count],
618 * we will invalidate all blkaddr in the whole range.
619 */
Chao Yu4d57b862018-05-30 00:20:41 +0800620 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800621 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800622 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900623 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900624 }
625 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900626
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800627 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900628 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
629 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900630}
631
Chao Yu4d57b862018-05-30 00:20:41 +0800632void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900633{
Chao Yud02a6e62019-03-25 21:08:19 +0800634 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900635}
636
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800637static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700638 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639{
youngjun yoo193bea12018-05-30 04:21:14 +0900640 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300641 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700642 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900643 struct page *page;
644
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700645 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700646 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900647
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700648 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700649 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700650 if (page && PageUptodate(page))
651 goto truncate_out;
652 f2fs_put_page(page, 1);
653 return 0;
654 }
655
Chao Yu4d57b862018-05-30 00:20:41 +0800656 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900657 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800658 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700659truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800660 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300661 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700662
663 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530664 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700665 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800666 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900667 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700668 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900669}
670
Chao Yu3265d3d2020-03-18 16:22:59 +0800671int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900672{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700673 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900674 struct dnode_of_data dn;
675 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800676 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700677 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800678 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900679
Namjae Jeon51dd6242013-04-20 01:28:52 +0900680 trace_f2fs_truncate_blocks_enter(inode, from);
681
Chao Yudf033ca2018-03-20 23:08:29 +0800682 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900683
Chao Yu09210c92016-05-05 19:13:03 +0800684 if (free_from >= sbi->max_file_blocks)
685 goto free_partial;
686
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700687 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800688 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800689
Chao Yu4d57b862018-05-30 00:20:41 +0800690 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700691 if (IS_ERR(ipage)) {
692 err = PTR_ERR(ipage);
693 goto out;
694 }
695
696 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800697 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700698 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800699 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700700 goto out;
701 }
702
703 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800704 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900705 if (err) {
706 if (err == -ENOENT)
707 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700708 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700709 }
710
Chao Yu81ca7352016-01-26 15:39:35 +0800711 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900712
713 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700714 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900715
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900716 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800717 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900718 free_from += count;
719 }
720
721 f2fs_put_dnode(&dn);
722free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800723 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800724out:
725 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800726 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800727free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700728 /* lastly zero out the first data page */
729 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800730 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900731
Namjae Jeon51dd6242013-04-20 01:28:52 +0900732 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900733 return err;
734}
735
Chao Yu4c8ff702019-11-01 18:07:14 +0800736int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
737{
738 u64 free_from = from;
Chao Yu3265d3d2020-03-18 16:22:59 +0800739 int err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800740
Chao Yu3265d3d2020-03-18 16:22:59 +0800741#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800742 /*
743 * for compressed file, only support cluster size
744 * aligned truncation.
745 */
Chao Yu4fec3fc02020-04-08 19:55:17 +0800746 if (f2fs_compressed_file(inode))
747 free_from = round_up(from,
748 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
Chao Yu3265d3d2020-03-18 16:22:59 +0800749#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800750
Chao Yu3265d3d2020-03-18 16:22:59 +0800751 err = f2fs_do_truncate_blocks(inode, free_from, lock);
752 if (err)
753 return err;
754
755#ifdef CONFIG_F2FS_FS_COMPRESSION
756 if (from != free_from)
757 err = f2fs_truncate_partial_cluster(inode, from, lock);
758#endif
759
760 return err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800761}
762
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700763int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900764{
Chao Yub0154892015-08-24 17:39:42 +0800765 int err;
766
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200767 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
768 return -EIO;
769
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900770 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
771 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800772 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900773
Namjae Jeon51dd6242013-04-20 01:28:52 +0900774 trace_f2fs_truncate(inode);
775
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800776 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800777 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800778 return -EIO;
779 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200780
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800781 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800782 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800783 err = f2fs_convert_inline_inode(inode);
784 if (err)
785 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800786 }
787
Chao Yuc42d28c2019-02-02 17:33:01 +0800788 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800789 if (err)
790 return err;
791
Deepa Dinamani078cd822016-09-14 07:48:04 -0700792 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700793 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800794 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900795}
796
David Howellsa528d352017-01-31 16:46:22 +0000797int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800798 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900799{
David Howellsa528d352017-01-31 16:46:22 +0000800 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800801 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800802 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800803 unsigned int flags;
804
Chao Yu1c1d35d2018-01-25 14:54:42 +0800805 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800806 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800807 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
808 stat->result_mask |= STATX_BTIME;
809 stat->btime.tv_sec = fi->i_crtime.tv_sec;
810 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
811 }
812
Eric Biggers36098552019-06-04 22:59:04 -0700813 flags = fi->i_flags;
Chao Yufd267252020-03-25 10:22:09 +0800814 if (flags & F2FS_COMPR_FL)
815 stat->attributes |= STATX_ATTR_COMPRESSED;
Chao Yu59c84402018-04-03 15:08:17 +0800816 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800817 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530818 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800819 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800820 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800821 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800822 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800823 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700824 if (IS_VERITY(inode))
825 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800826
Chao Yufd267252020-03-25 10:22:09 +0800827 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
828 STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800829 STATX_ATTR_ENCRYPTED |
830 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700831 STATX_ATTR_NODUMP |
832 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800833
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900834 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700835
836 /* we need to show initial sectors used for inline_data/dentries */
837 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
838 f2fs_has_inline_dentry(inode))
839 stat->blocks += (stat->size + 511) >> 9;
840
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900841 return 0;
842}
843
844#ifdef CONFIG_F2FS_FS_POSIX_ACL
845static void __setattr_copy(struct inode *inode, const struct iattr *attr)
846{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900847 unsigned int ia_valid = attr->ia_valid;
848
849 if (ia_valid & ATTR_UID)
850 inode->i_uid = attr->ia_uid;
851 if (ia_valid & ATTR_GID)
852 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200853 if (ia_valid & ATTR_ATIME)
854 inode->i_atime = attr->ia_atime;
855 if (ia_valid & ATTR_MTIME)
856 inode->i_mtime = attr->ia_mtime;
857 if (ia_valid & ATTR_CTIME)
858 inode->i_ctime = attr->ia_ctime;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900859 if (ia_valid & ATTR_MODE) {
860 umode_t mode = attr->ia_mode;
861
862 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
863 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700864 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900865 }
866}
867#else
868#define __setattr_copy setattr_copy
869#endif
870
871int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
872{
David Howells2b0143b2015-03-17 22:25:59 +0000873 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900874 int err;
875
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200876 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
877 return -EIO;
878
Chao Yu4c8ff702019-11-01 18:07:14 +0800879 if ((attr->ia_valid & ATTR_SIZE) &&
880 !f2fs_is_compress_backend_ready(inode))
881 return -EOPNOTSUPP;
882
Jan Kara31051c82016-05-26 16:55:18 +0200883 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900884 if (err)
885 return err;
886
Eric Biggers20bb2472017-11-29 12:35:32 -0800887 err = fscrypt_prepare_setattr(dentry, attr);
888 if (err)
889 return err;
890
Eric Biggers95ae2512019-07-22 09:26:24 -0700891 err = fsverity_prepare_setattr(dentry, attr);
892 if (err)
893 return err;
894
Chao Yu0abd6752017-07-09 00:13:07 +0800895 if (is_quota_modification(inode, attr)) {
896 err = dquot_initialize(inode);
897 if (err)
898 return err;
899 }
900 if ((attr->ia_valid & ATTR_UID &&
901 !uid_eq(attr->ia_uid, inode->i_uid)) ||
902 (attr->ia_valid & ATTR_GID &&
903 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800904 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800905 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800906 if (err) {
907 set_sbi_flag(F2FS_I_SB(inode),
908 SBI_QUOTA_NEED_REPAIR);
909 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800910 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800911 }
912 /*
913 * update uid/gid under lock_op(), so that dquot and inode can
914 * be updated atomically.
915 */
916 if (attr->ia_valid & ATTR_UID)
917 inode->i_uid = attr->ia_uid;
918 if (attr->ia_valid & ATTR_GID)
919 inode->i_gid = attr->ia_gid;
920 f2fs_mark_inode_dirty_sync(inode, true);
921 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800922 }
923
Chao Yu09db6a22014-09-15 18:02:09 +0800924 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800925 loff_t old_size = i_size_read(inode);
926
927 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
928 /*
929 * should convert inline inode before i_size_write to
930 * keep smaller than inline_data size with inline flag.
931 */
932 err = f2fs_convert_inline_inode(inode);
933 if (err)
934 return err;
935 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800936
Chao Yua33c1502018-08-05 23:04:25 +0800937 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900938 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800939
940 truncate_setsize(inode, attr->ia_size);
941
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800942 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800943 err = f2fs_truncate(inode);
944 /*
945 * do not trim all blocks after i_size if target size is
946 * larger than i_size.
947 */
Chao Yua33c1502018-08-05 23:04:25 +0800948 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900949 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800950 if (err)
951 return err;
952
Chao Yuc10c9822020-02-27 19:30:03 +0800953 spin_lock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800954 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800955 F2FS_I(inode)->last_disk_size = i_size_read(inode);
Chao Yuc10c9822020-02-27 19:30:03 +0800956 spin_unlock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900957 }
958
959 __setattr_copy(inode, attr);
960
961 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800962 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700963 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
964 inode->i_mode = F2FS_I(inode)->i_acl_mode;
965 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900966 }
967 }
968
Yunlei Hec0ed4402016-12-11 15:35:15 +0800969 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800970 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700971
972 /* inode change will produce dirty node pages flushed by checkpoint */
973 f2fs_balance_fs(F2FS_I_SB(inode), true);
974
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900975 return err;
976}
977
978const struct inode_operations f2fs_file_inode_operations = {
979 .getattr = f2fs_getattr,
980 .setattr = f2fs_setattr,
981 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800982 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900983 .listxattr = f2fs_listxattr,
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900984 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900985};
986
Chao Yu63943282015-08-07 18:36:06 +0800987static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900988 loff_t start, loff_t len)
989{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700990 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900991 struct page *page;
992
993 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800994 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900995
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800996 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900997
Gu Zhenge4795562013-09-27 18:08:30 +0800998 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800999 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +08001000 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001001
Chao Yu63943282015-08-07 18:36:06 +08001002 if (IS_ERR(page))
1003 return PTR_ERR(page);
1004
Chao Yubae0ee72018-12-25 17:43:42 +08001005 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +08001006 zero_user(page, start, len);
1007 set_page_dirty(page);
1008 f2fs_put_page(page, 1);
1009 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001010}
1011
Chao Yu4d57b862018-05-30 00:20:41 +08001012int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001013{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001014 int err;
1015
Chao Yuea587112015-09-17 20:22:44 +08001016 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001017 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001018 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001019
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001020 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001021 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001022 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001023 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001024 pg_start = f2fs_get_next_page_offset(&dn,
1025 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001026 continue;
Chao Yuea587112015-09-17 20:22:44 +08001027 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001028 return err;
1029 }
1030
Chao Yu81ca7352016-01-26 15:39:35 +08001031 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001032 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1033
1034 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1035
Chao Yu4d57b862018-05-30 00:20:41 +08001036 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001037 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001038
1039 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001040 }
1041 return 0;
1042}
1043
Chao Yua66c7b22013-11-22 16:52:50 +08001044static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001045{
1046 pgoff_t pg_start, pg_end;
1047 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001048 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001049
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001050 ret = f2fs_convert_inline_inode(inode);
1051 if (ret)
1052 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001053
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001054 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1055 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001056
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001057 off_start = offset & (PAGE_SIZE - 1);
1058 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001059
1060 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001061 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001062 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001063 if (ret)
1064 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001065 } else {
Chao Yu63943282015-08-07 18:36:06 +08001066 if (off_start) {
1067 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001068 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001069 if (ret)
1070 return ret;
1071 }
1072 if (off_end) {
1073 ret = fill_zero(inode, pg_end, 0, off_end);
1074 if (ret)
1075 return ret;
1076 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001077
1078 if (pg_start < pg_end) {
1079 struct address_space *mapping = inode->i_mapping;
1080 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001081 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001082
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001083 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001084
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001085 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1086 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001087
Chao Yua33c1502018-08-05 23:04:25 +08001088 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001089 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001090
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001091 truncate_inode_pages_range(mapping, blk_start,
1092 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001093
Gu Zhenge4795562013-09-27 18:08:30 +08001094 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001095 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001096 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001097
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001098 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001099 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001100 }
1101 }
1102
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001103 return ret;
1104}
1105
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001106static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1107 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001108{
1109 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1110 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001111 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001112
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001113next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001114 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001115 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001116 if (ret && ret != -ENOENT) {
1117 return ret;
1118 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001119 if (dn.max_level == 0)
1120 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001121 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1122 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001123 blkaddr += done;
1124 do_replace += done;
1125 goto next;
1126 }
1127
1128 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1129 dn.ofs_in_node, len);
1130 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001131 *blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu93770ab2019-04-15 15:26:32 +08001132
1133 if (__is_valid_data_blkaddr(*blkaddr) &&
1134 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1135 DATA_GENERIC_ENHANCE)) {
1136 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001137 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001138 }
1139
Chao Yu4d57b862018-05-30 00:20:41 +08001140 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001141
Chao Yub0332a02020-02-14 17:44:12 +08001142 if (f2fs_lfs_mode(sbi)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001143 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001144 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001145 }
1146
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001147 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001148 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001149 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001150 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001151 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001152 f2fs_put_dnode(&dn);
1153next:
1154 len -= done;
1155 off += done;
1156 if (len)
1157 goto next_dnode;
1158 return 0;
1159}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001160
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001161static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1162 int *do_replace, pgoff_t off, int len)
1163{
1164 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1165 struct dnode_of_data dn;
1166 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001167
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001168 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1169 if (*do_replace == 0)
1170 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001171
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001172 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001173 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001174 if (ret) {
1175 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001176 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001177 } else {
1178 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001179 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001180 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001181 }
1182 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001183}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001184
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001185static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1186 block_t *blkaddr, int *do_replace,
1187 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1188{
1189 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1190 pgoff_t i = 0;
1191 int ret;
1192
1193 while (i < len) {
1194 if (blkaddr[i] == NULL_ADDR && !full) {
1195 i++;
1196 continue;
1197 }
1198
1199 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1200 struct dnode_of_data dn;
1201 struct node_info ni;
1202 size_t new_size;
1203 pgoff_t ilen;
1204
1205 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001206 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001207 if (ret)
1208 return ret;
1209
Chao Yu77357302018-07-17 00:02:17 +08001210 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1211 if (ret) {
1212 f2fs_put_dnode(&dn);
1213 return ret;
1214 }
1215
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001216 ilen = min((pgoff_t)
1217 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1218 dn.ofs_in_node, len - i);
1219 do {
Chao Yua2ced1c2020-02-14 17:44:10 +08001220 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu4d57b862018-05-30 00:20:41 +08001221 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001222
1223 if (do_replace[i]) {
1224 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001225 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001226 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001227 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001228 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1229 blkaddr[i], ni.version, true, false);
1230
1231 do_replace[i] = 0;
1232 }
1233 dn.ofs_in_node++;
1234 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001235 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001236 if (dst_inode->i_size < new_size)
1237 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001238 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001239
1240 f2fs_put_dnode(&dn);
1241 } else {
1242 struct page *psrc, *pdst;
1243
Chao Yu4d57b862018-05-30 00:20:41 +08001244 psrc = f2fs_get_lock_data_page(src_inode,
1245 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001246 if (IS_ERR(psrc))
1247 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001248 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001249 true);
1250 if (IS_ERR(pdst)) {
1251 f2fs_put_page(psrc, 1);
1252 return PTR_ERR(pdst);
1253 }
1254 f2fs_copy_page(psrc, pdst);
1255 set_page_dirty(pdst);
1256 f2fs_put_page(pdst, 1);
1257 f2fs_put_page(psrc, 1);
1258
Chao Yu4d57b862018-05-30 00:20:41 +08001259 ret = f2fs_truncate_hole(src_inode,
1260 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001261 if (ret)
1262 return ret;
1263 i++;
1264 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001265 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001266 return 0;
1267}
1268
1269static int __exchange_data_block(struct inode *src_inode,
1270 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001271 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001272{
1273 block_t *src_blkaddr;
1274 int *do_replace;
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001275 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001276 int ret;
1277
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001278 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001279 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001280
Chao Yu628b3d12017-11-30 19:28:18 +08001281 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001282 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001283 GFP_NOFS);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001284 if (!src_blkaddr)
1285 return -ENOMEM;
1286
Chao Yu628b3d12017-11-30 19:28:18 +08001287 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001288 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001289 GFP_NOFS);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001290 if (!do_replace) {
1291 kvfree(src_blkaddr);
1292 return -ENOMEM;
1293 }
1294
1295 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1296 do_replace, src, olen);
1297 if (ret)
1298 goto roll_back;
1299
1300 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1301 do_replace, src, dst, olen, full);
1302 if (ret)
1303 goto roll_back;
1304
1305 src += olen;
1306 dst += olen;
1307 len -= olen;
1308
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001309 kvfree(src_blkaddr);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001310 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001311 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001312 return 0;
1313
1314roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001315 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001316 kvfree(src_blkaddr);
1317 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001318 return ret;
1319}
1320
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001321static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001322{
1323 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001324 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001325 pgoff_t start = offset >> PAGE_SHIFT;
1326 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001327 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001328
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001329 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001330
1331 /* avoid gc operation during block exchange */
1332 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1333 down_write(&F2FS_I(inode)->i_mmap_sem);
1334
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001335 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001336 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001337 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001338 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1339 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001340
1341 up_write(&F2FS_I(inode)->i_mmap_sem);
1342 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001343 return ret;
1344}
1345
1346static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1347{
Chao Yub4ace332015-05-06 13:09:46 +08001348 loff_t new_size;
1349 int ret;
1350
Chao Yub4ace332015-05-06 13:09:46 +08001351 if (offset + len >= i_size_read(inode))
1352 return -EINVAL;
1353
1354 /* collapse range should be aligned to block size of f2fs. */
1355 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1356 return -EINVAL;
1357
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001358 ret = f2fs_convert_inline_inode(inode);
1359 if (ret)
1360 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001361
Chao Yub4ace332015-05-06 13:09:46 +08001362 /* write out all dirty pages from offset */
1363 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1364 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001365 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001366
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001367 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001368 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001369 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001370
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001371 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001372 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001373 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1374 truncate_pagecache(inode, offset);
1375
Chao Yub4ace332015-05-06 13:09:46 +08001376 new_size = i_size_read(inode) - len;
Chao Yuc42d28c2019-02-02 17:33:01 +08001377 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001378 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001379 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001380 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001381 return ret;
1382}
1383
Chao Yu6e961942016-05-09 19:56:31 +08001384static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1385 pgoff_t end)
1386{
1387 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1388 pgoff_t index = start;
1389 unsigned int ofs_in_node = dn->ofs_in_node;
1390 blkcnt_t count = 0;
1391 int ret;
1392
1393 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001394 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001395 count++;
1396 }
1397
1398 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001399 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001400 if (ret)
1401 return ret;
1402
1403 dn->ofs_in_node = ofs_in_node;
1404 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001405 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001406 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001407 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001408 * allocation.
1409 */
1410 if (dn->data_blkaddr == NULL_ADDR) {
1411 ret = -ENOSPC;
1412 break;
1413 }
1414 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001415 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001416 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001417 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001418 }
1419 }
1420
1421 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1422
1423 return ret;
1424}
1425
Chao Yu75cd4e02015-05-06 13:11:13 +08001426static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1427 int mode)
1428{
1429 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1430 struct address_space *mapping = inode->i_mapping;
1431 pgoff_t index, pg_start, pg_end;
1432 loff_t new_size = i_size_read(inode);
1433 loff_t off_start, off_end;
1434 int ret = 0;
1435
Chao Yu75cd4e02015-05-06 13:11:13 +08001436 ret = inode_newsize_ok(inode, (len + offset));
1437 if (ret)
1438 return ret;
1439
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001440 ret = f2fs_convert_inline_inode(inode);
1441 if (ret)
1442 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001443
1444 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1445 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001446 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001447
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001448 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1449 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001450
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001451 off_start = offset & (PAGE_SIZE - 1);
1452 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001453
1454 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001455 ret = fill_zero(inode, pg_start, off_start,
1456 off_end - off_start);
1457 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001458 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001459
Chao Yu75cd4e02015-05-06 13:11:13 +08001460 new_size = max_t(loff_t, new_size, offset + len);
1461 } else {
1462 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001463 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001464 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001465 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001466 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001467
Chao Yu75cd4e02015-05-06 13:11:13 +08001468 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001469 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001470 }
1471
Chao Yu6e961942016-05-09 19:56:31 +08001472 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001473 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001474 unsigned int end_offset;
1475 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001476
Chao Yuc70798532018-08-05 23:02:22 +08001477 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001478 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001479
1480 truncate_pagecache_range(inode,
1481 (loff_t)index << PAGE_SHIFT,
1482 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1483
Chao Yu75cd4e02015-05-06 13:11:13 +08001484 f2fs_lock_op(sbi);
1485
Chao Yu6e961942016-05-09 19:56:31 +08001486 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001487 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001488 if (ret) {
1489 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001490 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001491 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001492 goto out;
1493 }
1494
Chao Yu6e961942016-05-09 19:56:31 +08001495 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1496 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1497
1498 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001499 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001500
Chao Yu75cd4e02015-05-06 13:11:13 +08001501 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001502 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001503 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001504
1505 f2fs_balance_fs(sbi, dn.node_changed);
1506
Chao Yu6e961942016-05-09 19:56:31 +08001507 if (ret)
1508 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001509
Chao Yu6e961942016-05-09 19:56:31 +08001510 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001511 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001512 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001513 }
1514
1515 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001516 ret = fill_zero(inode, pg_end, 0, off_end);
1517 if (ret)
1518 goto out;
1519
Chao Yu75cd4e02015-05-06 13:11:13 +08001520 new_size = max_t(loff_t, new_size, offset + len);
1521 }
1522 }
1523
1524out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001525 if (new_size > i_size_read(inode)) {
1526 if (mode & FALLOC_FL_KEEP_SIZE)
1527 file_set_keep_isize(inode);
1528 else
1529 f2fs_i_size_write(inode, new_size);
1530 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001531 return ret;
1532}
1533
Chao Yuf62185d2015-05-28 19:16:57 +08001534static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1535{
1536 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001537 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001538 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001539 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001540
Chao Yuf62185d2015-05-28 19:16:57 +08001541 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001542 ret = inode_newsize_ok(inode, new_size);
1543 if (ret)
1544 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001545
1546 if (offset >= i_size_read(inode))
1547 return -EINVAL;
1548
1549 /* insert range should be aligned to block size of f2fs. */
1550 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1551 return -EINVAL;
1552
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001553 ret = f2fs_convert_inline_inode(inode);
1554 if (ret)
1555 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001556
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001557 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001558
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001559 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001560 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001561 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001562 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001563 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001564
1565 /* write out all dirty pages from offset */
1566 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1567 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001568 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001569
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001570 pg_start = offset >> PAGE_SHIFT;
1571 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001572 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001573 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001574
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001575 /* avoid gc operation during block exchange */
1576 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1577 down_write(&F2FS_I(inode)->i_mmap_sem);
1578 truncate_pagecache(inode, offset);
1579
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001580 while (!ret && idx > pg_start) {
1581 nr = idx - pg_start;
1582 if (nr > delta)
1583 nr = delta;
1584 idx -= nr;
1585
Chao Yuf62185d2015-05-28 19:16:57 +08001586 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001587 f2fs_drop_extent_tree(inode);
1588
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001589 ret = __exchange_data_block(inode, inode, idx,
1590 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001591 f2fs_unlock_op(sbi);
1592 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001593 up_write(&F2FS_I(inode)->i_mmap_sem);
1594 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001595
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001596 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001597 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001598 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1599 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001600 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001601
1602 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001603 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001604 return ret;
1605}
1606
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001607static int expand_inode_data(struct inode *inode, loff_t offset,
1608 loff_t len, int mode)
1609{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001610 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001611 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001612 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1613 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001614 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001615 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001616 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001617 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001618
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001619 err = inode_newsize_ok(inode, (len + offset));
1620 if (err)
1621 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001622
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001623 err = f2fs_convert_inline_inode(inode);
1624 if (err)
1625 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001626
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001627 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001628
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001629 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001630 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001631
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001632 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1633 map.m_len = pg_end - map.m_lblk;
1634 if (off_end)
1635 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001636
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001637 if (!map.m_len)
1638 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001639
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001640 if (f2fs_is_pinned_file(inode)) {
1641 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1642 sbi->log_blocks_per_seg;
1643 block_t done = 0;
1644
1645 if (map.m_len % sbi->blocks_per_seg)
1646 len += sbi->blocks_per_seg;
1647
1648 map.m_len = sbi->blocks_per_seg;
1649next_alloc:
1650 if (has_not_enough_free_secs(sbi, 0,
1651 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
Chao Yufb24fea2020-01-14 19:36:50 +08001652 down_write(&sbi->gc_lock);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001653 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1654 if (err && err != -ENODATA && err != -EAGAIN)
1655 goto out_err;
1656 }
1657
1658 down_write(&sbi->pin_sem);
1659 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
Daeho Jeongfd612642020-05-27 13:02:31 +09001660
1661 f2fs_lock_op(sbi);
Chao Yu901d745f2020-06-22 17:38:48 +08001662 f2fs_allocate_new_segment(sbi, CURSEG_COLD_DATA);
Daeho Jeongfd612642020-05-27 13:02:31 +09001663 f2fs_unlock_op(sbi);
1664
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001665 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1666 up_write(&sbi->pin_sem);
1667
1668 done += map.m_len;
1669 len -= map.m_len;
1670 map.m_lblk += map.m_len;
1671 if (!err && len)
1672 goto next_alloc;
1673
1674 map.m_len = done;
1675 } else {
1676 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1677 }
1678out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001679 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001680 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001681
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001682 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001683 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001684
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001685 last_off = map.m_lblk + map.m_len - 1;
1686
1687 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001688 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001689 (loff_t)(last_off + 1) << PAGE_SHIFT;
1690 } else {
1691 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001692 }
1693
Chao Yue8ed90a2017-11-05 21:53:30 +08001694 if (new_size > i_size_read(inode)) {
1695 if (mode & FALLOC_FL_KEEP_SIZE)
1696 file_set_keep_isize(inode);
1697 else
1698 f2fs_i_size_write(inode, new_size);
1699 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001700
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001701 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001702}
1703
1704static long f2fs_fallocate(struct file *file, int mode,
1705 loff_t offset, loff_t len)
1706{
Al Viro6131ffa2013-02-27 16:59:05 -05001707 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001708 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001709
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001710 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1711 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001712 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1713 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001714 if (!f2fs_is_compress_backend_ready(inode))
1715 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001716
Chao Yuc9980122015-09-11 14:39:02 +08001717 /* f2fs only support ->fallocate for regular file */
1718 if (!S_ISREG(inode->i_mode))
1719 return -EINVAL;
1720
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301721 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001722 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001723 return -EOPNOTSUPP;
1724
Chao Yu4c8ff702019-11-01 18:07:14 +08001725 if (f2fs_compressed_file(inode) &&
1726 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1727 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1728 return -EOPNOTSUPP;
1729
Chao Yub4ace332015-05-06 13:09:46 +08001730 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001731 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1732 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001733 return -EOPNOTSUPP;
1734
Al Viro59551022016-01-22 15:40:57 -05001735 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001736
Taehee Yoo587c0a42015-04-21 15:59:12 +09001737 if (mode & FALLOC_FL_PUNCH_HOLE) {
1738 if (offset >= inode->i_size)
1739 goto out;
1740
Chao Yua66c7b22013-11-22 16:52:50 +08001741 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001742 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1743 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001744 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1745 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001746 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1747 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001748 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001749 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001750 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001751
Namjae Jeon3af60a42012-12-30 14:52:37 +09001752 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001753 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001754 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001755 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001756 }
Chao Yu3375f692014-01-28 10:29:26 +08001757
Taehee Yoo587c0a42015-04-21 15:59:12 +09001758out:
Al Viro59551022016-01-22 15:40:57 -05001759 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001760
Namjae Jeonc01e2852013-04-23 17:00:52 +09001761 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001762 return ret;
1763}
1764
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001765static int f2fs_release_file(struct inode *inode, struct file *filp)
1766{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001767 /*
1768 * f2fs_relase_file is called at every close calls. So we should
1769 * not drop any inmemory pages by close called by other process.
1770 */
1771 if (!(filp->f_mode & FMODE_WRITE) ||
1772 atomic_read(&inode->i_writecount) != 1)
1773 return 0;
1774
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001775 /* some remained atomic pages should discarded */
1776 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001777 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001778 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001779 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001780 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001781 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001782 clear_inode_flag(inode, FI_VOLATILE_FILE);
1783 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001784 }
1785 return 0;
1786}
1787
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001788static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001789{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001790 struct inode *inode = file_inode(file);
1791
1792 /*
1793 * If the process doing a transaction is crashed, we should do
1794 * roll-back. Otherwise, other reader/write can see corrupted database
1795 * until all the writers close its file. Since this should be done
1796 * before dropping file lock, it needs to do in ->flush.
1797 */
1798 if (f2fs_is_atomic_file(inode) &&
1799 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001800 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001801 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001802}
1803
Eric Biggers36098552019-06-04 22:59:04 -07001804static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001805{
1806 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001807 u32 masked_flags = fi->i_flags & mask;
1808
1809 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
Chao Yu2c1d0302017-07-29 00:32:52 +08001810
1811 /* Is it quota file? Do not allow user to mess with it */
1812 if (IS_NOQUOTA(inode))
1813 return -EPERM;
1814
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001815 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001816 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1817 return -EOPNOTSUPP;
1818 if (!f2fs_empty_dir(inode))
1819 return -ENOTEMPTY;
1820 }
1821
Chao Yu4c8ff702019-11-01 18:07:14 +08001822 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1823 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1824 return -EOPNOTSUPP;
1825 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1826 return -EINVAL;
1827 }
1828
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001829 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
Chao Yuaa576972020-03-27 18:29:51 +08001830 if (masked_flags & F2FS_COMPR_FL) {
Chao Yu2536ac62020-03-10 20:50:09 +08001831 if (f2fs_disable_compressed_file(inode))
1832 return -EINVAL;
1833 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001834 if (iflags & F2FS_NOCOMP_FL)
1835 return -EINVAL;
1836 if (iflags & F2FS_COMPR_FL) {
Chao Yu4c8ff702019-11-01 18:07:14 +08001837 if (!f2fs_may_compress(inode))
1838 return -EINVAL;
1839
1840 set_compress_context(inode);
1841 }
1842 }
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001843 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1844 if (masked_flags & F2FS_COMPR_FL)
Chao Yu4c8ff702019-11-01 18:07:14 +08001845 return -EINVAL;
1846 }
1847
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001848 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001849 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1850 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001851
Chao Yu59c84402018-04-03 15:08:17 +08001852 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001853 set_inode_flag(inode, FI_PROJ_INHERIT);
1854 else
1855 clear_inode_flag(inode, FI_PROJ_INHERIT);
1856
1857 inode->i_ctime = current_time(inode);
1858 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001859 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001860 return 0;
1861}
1862
Eric Biggers36098552019-06-04 22:59:04 -07001863/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1864
1865/*
1866 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1867 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1868 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1869 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1870 */
1871
1872static const struct {
1873 u32 iflag;
1874 u32 fsflag;
1875} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001876 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001877 { F2FS_SYNC_FL, FS_SYNC_FL },
1878 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1879 { F2FS_APPEND_FL, FS_APPEND_FL },
1880 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1881 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001882 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001883 { F2FS_INDEX_FL, FS_INDEX_FL },
1884 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1885 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001886 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001887};
1888
1889#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001890 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001891 FS_SYNC_FL | \
1892 FS_IMMUTABLE_FL | \
1893 FS_APPEND_FL | \
1894 FS_NODUMP_FL | \
1895 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001896 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001897 FS_INDEX_FL | \
1898 FS_DIRSYNC_FL | \
1899 FS_PROJINHERIT_FL | \
1900 FS_ENCRYPT_FL | \
1901 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001902 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001903 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001904 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001905
1906#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001907 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001908 FS_SYNC_FL | \
1909 FS_IMMUTABLE_FL | \
1910 FS_APPEND_FL | \
1911 FS_NODUMP_FL | \
1912 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001913 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001914 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001915 FS_PROJINHERIT_FL | \
1916 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001917
1918/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1919static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1920{
1921 u32 fsflags = 0;
1922 int i;
1923
1924 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1925 if (iflags & f2fs_fsflags_map[i].iflag)
1926 fsflags |= f2fs_fsflags_map[i].fsflag;
1927
1928 return fsflags;
1929}
1930
1931/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1932static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1933{
1934 u32 iflags = 0;
1935 int i;
1936
1937 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1938 if (fsflags & f2fs_fsflags_map[i].fsflag)
1939 iflags |= f2fs_fsflags_map[i].iflag;
1940
1941 return iflags;
1942}
1943
1944static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1945{
1946 struct inode *inode = file_inode(filp);
1947 struct f2fs_inode_info *fi = F2FS_I(inode);
1948 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1949
1950 if (IS_ENCRYPTED(inode))
1951 fsflags |= FS_ENCRYPT_FL;
Eric Biggers95ae2512019-07-22 09:26:24 -07001952 if (IS_VERITY(inode))
1953 fsflags |= FS_VERITY_FL;
Eric Biggers36098552019-06-04 22:59:04 -07001954 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1955 fsflags |= FS_INLINE_DATA_FL;
1956 if (is_inode_flag_set(inode, FI_PIN_FILE))
1957 fsflags |= FS_NOCOW_FL;
1958
1959 fsflags &= F2FS_GETTABLE_FS_FL;
1960
1961 return put_user(fsflags, (int __user *)arg);
1962}
1963
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001964static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1965{
1966 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001967 struct f2fs_inode_info *fi = F2FS_I(inode);
1968 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001969 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001970 int ret;
1971
Chao Yu7fb17fe2016-05-09 19:56:32 +08001972 if (!inode_owner_or_capable(inode))
1973 return -EACCES;
1974
Eric Biggers36098552019-06-04 22:59:04 -07001975 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001976 return -EFAULT;
1977
Eric Biggers36098552019-06-04 22:59:04 -07001978 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1979 return -EOPNOTSUPP;
1980 fsflags &= F2FS_SETTABLE_FS_FL;
1981
1982 iflags = f2fs_fsflags_to_iflags(fsflags);
1983 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1984 return -EOPNOTSUPP;
1985
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001986 ret = mnt_want_write_file(filp);
1987 if (ret)
1988 return ret;
1989
Al Viro59551022016-01-22 15:40:57 -05001990 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001991
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001992 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1993 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1994 if (ret)
1995 goto out;
1996
Eric Biggers36098552019-06-04 22:59:04 -07001997 ret = f2fs_setflags_common(inode, iflags,
1998 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001999out:
Chao Yua72d4b92017-05-03 23:59:13 +08002000 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002001 mnt_drop_write_file(filp);
2002 return ret;
2003}
2004
Chao Yud49f3e82015-01-23 20:36:04 +08002005static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2006{
2007 struct inode *inode = file_inode(filp);
2008
2009 return put_user(inode->i_generation, (int __user *)arg);
2010}
2011
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002012static int f2fs_ioc_start_atomic_write(struct file *filp)
2013{
2014 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002015 struct f2fs_inode_info *fi = F2FS_I(inode);
2016 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08002017 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002018
2019 if (!inode_owner_or_capable(inode))
2020 return -EACCES;
2021
Jaegeuk Kime8118982017-03-17 10:04:15 +08002022 if (!S_ISREG(inode->i_mode))
2023 return -EINVAL;
2024
Chao Yu038d0692019-07-25 22:39:11 +08002025 if (filp->f_flags & O_DIRECT)
2026 return -EINVAL;
2027
Chao Yu7fb17fe2016-05-09 19:56:32 +08002028 ret = mnt_want_write_file(filp);
2029 if (ret)
2030 return ret;
2031
Chao Yu0fac5582016-05-09 19:56:33 +08002032 inode_lock(inode);
2033
Chao Yu4c8ff702019-11-01 18:07:14 +08002034 f2fs_disable_compressed_file(inode);
2035
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002036 if (f2fs_is_atomic_file(inode)) {
2037 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2038 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002039 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002040 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002041
Chao Yuf4c9c742015-07-17 18:06:35 +08002042 ret = f2fs_convert_inline_inode(inode);
2043 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002044 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002045
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002046 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2047
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002048 /*
2049 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2050 * f2fs_is_atomic_file.
2051 */
2052 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002053 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2054 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002055 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002056 if (ret) {
2057 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002058 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002059 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002060
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002061 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2062 if (list_empty(&fi->inmem_ilist))
2063 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302064 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002065 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2066
2067 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002068 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002069 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002070 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002071
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002072 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002073 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002074 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002075out:
Chao Yu0fac5582016-05-09 19:56:33 +08002076 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002077 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002078 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002079}
2080
2081static int f2fs_ioc_commit_atomic_write(struct file *filp)
2082{
2083 struct inode *inode = file_inode(filp);
2084 int ret;
2085
2086 if (!inode_owner_or_capable(inode))
2087 return -EACCES;
2088
2089 ret = mnt_want_write_file(filp);
2090 if (ret)
2091 return ret;
2092
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002093 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002094
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002095 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002096
Chao Yub169c3c2018-04-18 17:45:02 +08002097 if (f2fs_is_volatile_file(inode)) {
2098 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002099 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002100 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002101
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002102 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002103 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002104 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002105 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002106
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002107 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002108 if (!ret)
2109 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002110 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002111 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002112 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002113err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002114 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2115 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2116 ret = -EINVAL;
2117 }
Chao Yu0fac5582016-05-09 19:56:33 +08002118 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002119 mnt_drop_write_file(filp);
2120 return ret;
2121}
2122
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002123static int f2fs_ioc_start_volatile_write(struct file *filp)
2124{
2125 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002126 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002127
2128 if (!inode_owner_or_capable(inode))
2129 return -EACCES;
2130
Chao Yu8ff09712017-03-17 15:43:57 +08002131 if (!S_ISREG(inode->i_mode))
2132 return -EINVAL;
2133
Chao Yu7fb17fe2016-05-09 19:56:32 +08002134 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002135 if (ret)
2136 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002137
Chao Yu0fac5582016-05-09 19:56:33 +08002138 inode_lock(inode);
2139
Chao Yu7fb17fe2016-05-09 19:56:32 +08002140 if (f2fs_is_volatile_file(inode))
2141 goto out;
2142
2143 ret = f2fs_convert_inline_inode(inode);
2144 if (ret)
2145 goto out;
2146
Chao Yu648d50b2017-03-22 17:23:45 +08002147 stat_inc_volatile_write(inode);
2148 stat_update_max_volatile_write(inode);
2149
Jaegeuk Kim91942322016-05-20 10:13:22 -07002150 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002151 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002152out:
Chao Yu0fac5582016-05-09 19:56:33 +08002153 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002154 mnt_drop_write_file(filp);
2155 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002156}
2157
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002158static int f2fs_ioc_release_volatile_write(struct file *filp)
2159{
2160 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002161 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002162
2163 if (!inode_owner_or_capable(inode))
2164 return -EACCES;
2165
Chao Yu7fb17fe2016-05-09 19:56:32 +08002166 ret = mnt_want_write_file(filp);
2167 if (ret)
2168 return ret;
2169
Chao Yu0fac5582016-05-09 19:56:33 +08002170 inode_lock(inode);
2171
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002172 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002173 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002174
Chao Yu7fb17fe2016-05-09 19:56:32 +08002175 if (!f2fs_is_first_block_written(inode)) {
2176 ret = truncate_partial_data_page(inode, 0, true);
2177 goto out;
2178 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002179
Chao Yu7fb17fe2016-05-09 19:56:32 +08002180 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2181out:
Chao Yu0fac5582016-05-09 19:56:33 +08002182 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002183 mnt_drop_write_file(filp);
2184 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002185}
2186
2187static int f2fs_ioc_abort_volatile_write(struct file *filp)
2188{
2189 struct inode *inode = file_inode(filp);
2190 int ret;
2191
2192 if (!inode_owner_or_capable(inode))
2193 return -EACCES;
2194
2195 ret = mnt_want_write_file(filp);
2196 if (ret)
2197 return ret;
2198
Chao Yu0fac5582016-05-09 19:56:33 +08002199 inode_lock(inode);
2200
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002201 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002202 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002203 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002204 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002205 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002206 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002207 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002208
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002209 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2210
Chao Yu0fac5582016-05-09 19:56:33 +08002211 inode_unlock(inode);
2212
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002213 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002214 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002215 return ret;
2216}
2217
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002218static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2219{
2220 struct inode *inode = file_inode(filp);
2221 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2222 struct super_block *sb = sbi->sb;
2223 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002224 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002225
2226 if (!capable(CAP_SYS_ADMIN))
2227 return -EPERM;
2228
2229 if (get_user(in, (__u32 __user *)arg))
2230 return -EFAULT;
2231
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302232 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2233 ret = mnt_want_write_file(filp);
Chao Yu86264412020-06-08 20:03:16 +08002234 if (ret) {
2235 if (ret == -EROFS) {
2236 ret = 0;
2237 f2fs_stop_checkpoint(sbi, false);
2238 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2239 trace_f2fs_shutdown(sbi, in, ret);
2240 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302241 return ret;
Chao Yu86264412020-06-08 20:03:16 +08002242 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302243 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002244
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002245 switch (in) {
2246 case F2FS_GOING_DOWN_FULLSYNC:
2247 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08002248 if (IS_ERR(sb)) {
2249 ret = PTR_ERR(sb);
2250 goto out;
2251 }
2252 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002253 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002254 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002255 thaw_bdev(sb->s_bdev, sb);
2256 }
2257 break;
2258 case F2FS_GOING_DOWN_METASYNC:
2259 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002260 ret = f2fs_sync_fs(sb, 1);
2261 if (ret)
2262 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002263 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002264 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002265 break;
2266 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002267 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002268 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002269 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002270 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002271 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002272 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002273 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002274 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002275 case F2FS_GOING_DOWN_NEED_FSCK:
2276 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002277 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2278 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002279 /* do checkpoint only */
2280 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002281 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002282 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002283 ret = -EINVAL;
2284 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002285 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002286
Chao Yu4d57b862018-05-30 00:20:41 +08002287 f2fs_stop_gc_thread(sbi);
2288 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002289
Chao Yu4d57b862018-05-30 00:20:41 +08002290 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002291 clear_opt(sbi, DISCARD);
2292
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002293 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002294out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302295 if (in != F2FS_GOING_DOWN_FULLSYNC)
2296 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002297
2298 trace_f2fs_shutdown(sbi, in, ret);
2299
Chao Yu7fb17fe2016-05-09 19:56:32 +08002300 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002301}
2302
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002303static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2304{
2305 struct inode *inode = file_inode(filp);
2306 struct super_block *sb = inode->i_sb;
2307 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2308 struct fstrim_range range;
2309 int ret;
2310
2311 if (!capable(CAP_SYS_ADMIN))
2312 return -EPERM;
2313
Chao Yu7d20c8a2018-09-04 03:52:17 +08002314 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002315 return -EOPNOTSUPP;
2316
2317 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2318 sizeof(range)))
2319 return -EFAULT;
2320
Chao Yu7fb17fe2016-05-09 19:56:32 +08002321 ret = mnt_want_write_file(filp);
2322 if (ret)
2323 return ret;
2324
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002325 range.minlen = max((unsigned int)range.minlen,
2326 q->limits.discard_granularity);
2327 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002328 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002329 if (ret < 0)
2330 return ret;
2331
2332 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2333 sizeof(range)))
2334 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002335 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002336 return 0;
2337}
2338
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002339static bool uuid_is_nonzero(__u8 u[16])
2340{
2341 int i;
2342
2343 for (i = 0; i < 16; i++)
2344 if (u[i])
2345 return true;
2346 return false;
2347}
2348
2349static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2350{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002351 struct inode *inode = file_inode(filp);
2352
Chao Yu7beb01f2018-10-24 18:34:26 +08002353 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002354 return -EOPNOTSUPP;
2355
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002356 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002357
Eric Biggersdb717d82016-11-26 19:07:49 -05002358 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002359}
2360
2361static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2362{
Chao Yu7beb01f2018-10-24 18:34:26 +08002363 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002364 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002365 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002366}
2367
2368static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2369{
2370 struct inode *inode = file_inode(filp);
2371 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2372 int err;
2373
Chao Yu7beb01f2018-10-24 18:34:26 +08002374 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002375 return -EOPNOTSUPP;
2376
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002377 err = mnt_want_write_file(filp);
2378 if (err)
2379 return err;
2380
Chao Yu846ae672018-02-26 22:04:13 +08002381 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002382
2383 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2384 goto got_it;
2385
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002386 /* update superblock with uuid */
2387 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2388
Chao Yuc5bda1c2015-06-08 13:28:03 +08002389 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002390 if (err) {
2391 /* undo new data */
2392 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002393 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002394 }
2395got_it:
2396 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2397 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002398 err = -EFAULT;
2399out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002400 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002401 mnt_drop_write_file(filp);
2402 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002403}
2404
Eric Biggers8ce589c2019-08-04 19:35:48 -07002405static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2406 unsigned long arg)
2407{
2408 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2409 return -EOPNOTSUPP;
2410
2411 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2412}
2413
2414static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2415{
2416 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2417 return -EOPNOTSUPP;
2418
2419 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2420}
2421
2422static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2423{
2424 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2425 return -EOPNOTSUPP;
2426
2427 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2428}
2429
2430static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2431 unsigned long arg)
2432{
2433 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2434 return -EOPNOTSUPP;
2435
2436 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2437}
2438
2439static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2440 unsigned long arg)
2441{
2442 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2443 return -EOPNOTSUPP;
2444
2445 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2446}
2447
Eric Biggersee446e12020-03-14 13:50:51 -07002448static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2449{
2450 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2451 return -EOPNOTSUPP;
2452
2453 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2454}
2455
Chao Yuc1c1b582015-07-10 18:08:10 +08002456static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2457{
2458 struct inode *inode = file_inode(filp);
2459 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002460 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002461 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002462
2463 if (!capable(CAP_SYS_ADMIN))
2464 return -EPERM;
2465
Chao Yud530d4d2015-10-05 22:22:44 +08002466 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002467 return -EFAULT;
2468
Chao Yud530d4d2015-10-05 22:22:44 +08002469 if (f2fs_readonly(sbi->sb))
2470 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002471
Chao Yu7fb17fe2016-05-09 19:56:32 +08002472 ret = mnt_want_write_file(filp);
2473 if (ret)
2474 return ret;
2475
Chao Yud530d4d2015-10-05 22:22:44 +08002476 if (!sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002477 if (!down_write_trylock(&sbi->gc_lock)) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002478 ret = -EBUSY;
2479 goto out;
2480 }
Chao Yud530d4d2015-10-05 22:22:44 +08002481 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002482 down_write(&sbi->gc_lock);
Chao Yuc1c1b582015-07-10 18:08:10 +08002483 }
2484
Jaegeuk Kime066b832017-04-13 15:17:00 -07002485 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002486out:
2487 mnt_drop_write_file(filp);
2488 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002489}
2490
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002491static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2492{
2493 struct inode *inode = file_inode(filp);
2494 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2495 struct f2fs_gc_range range;
2496 u64 end;
2497 int ret;
2498
2499 if (!capable(CAP_SYS_ADMIN))
2500 return -EPERM;
2501
2502 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2503 sizeof(range)))
2504 return -EFAULT;
2505
2506 if (f2fs_readonly(sbi->sb))
2507 return -EROFS;
2508
Yunlei Heb82f6e32018-04-24 11:40:30 +08002509 end = range.start + range.len;
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302510 if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
2511 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002512 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002513
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002514 ret = mnt_want_write_file(filp);
2515 if (ret)
2516 return ret;
2517
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002518do_more:
2519 if (!range.sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002520 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002521 ret = -EBUSY;
2522 goto out;
2523 }
2524 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002525 down_write(&sbi->gc_lock);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002526 }
2527
2528 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Qilong Zhang97767502020-06-28 19:23:03 +08002529 if (ret) {
2530 if (ret == -EBUSY)
2531 ret = -EAGAIN;
2532 goto out;
2533 }
Yunlong Song67b0e422018-10-30 20:37:55 +08002534 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002535 if (range.start <= end)
2536 goto do_more;
2537out:
2538 mnt_drop_write_file(filp);
2539 return ret;
2540}
2541
Chao Yu059c0642018-07-17 20:41:49 +08002542static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002543{
2544 struct inode *inode = file_inode(filp);
2545 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002546 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002547
2548 if (!capable(CAP_SYS_ADMIN))
2549 return -EPERM;
2550
2551 if (f2fs_readonly(sbi->sb))
2552 return -EROFS;
2553
Daniel Rosenberg43549942018-08-20 19:21:43 -07002554 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002555 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002556 return -EINVAL;
2557 }
2558
Chao Yu7fb17fe2016-05-09 19:56:32 +08002559 ret = mnt_want_write_file(filp);
2560 if (ret)
2561 return ret;
2562
2563 ret = f2fs_sync_fs(sbi->sb, 1);
2564
2565 mnt_drop_write_file(filp);
2566 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002567}
2568
Chao Yud323d002015-10-27 09:53:45 +08002569static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2570 struct file *filp,
2571 struct f2fs_defragment *range)
2572{
2573 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002574 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002575 .m_seg_type = NO_CHECK_TYPE ,
2576 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002577 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002578 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002579 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002580 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002581 block_t blk_end = 0;
2582 bool fragmented = false;
2583 int err;
2584
2585 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002586 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002587 return -EINVAL;
2588
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002589 pg_start = range->start >> PAGE_SHIFT;
2590 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002591
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002592 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002593
Al Viro59551022016-01-22 15:40:57 -05002594 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002595
2596 /* writeback all dirty pages in the range */
2597 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002598 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002599 if (err)
2600 goto out;
2601
2602 /*
2603 * lookup mapping info in extent cache, skip defragmenting if physical
2604 * block addresses are continuous.
2605 */
2606 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2607 if (ei.fofs + ei.len >= pg_end)
2608 goto out;
2609 }
2610
2611 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002612 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002613
2614 /*
2615 * lookup mapping info in dnode page cache, skip defragmenting if all
2616 * physical block addresses are continuous even if there are hole(s)
2617 * in logical blocks.
2618 */
2619 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002620 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002621 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002622 if (err)
2623 goto out;
2624
2625 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002626 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002627 continue;
2628 }
2629
Chao Yu25a912e2018-01-10 18:18:51 +08002630 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002631 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002632
2633 /* record total count of block that we're going to move */
2634 total += map.m_len;
2635
Chao Yud323d002015-10-27 09:53:45 +08002636 blk_end = map.m_pblk + map.m_len;
2637
2638 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002639 }
2640
Chao Yud3a1a0e2019-07-29 23:02:29 +08002641 if (!fragmented) {
2642 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002643 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002644 }
Chao Yud323d002015-10-27 09:53:45 +08002645
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002646 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002647
2648 /*
2649 * make sure there are enough free section for LFS allocation, this can
2650 * avoid defragment running in SSR mode when free section are allocated
2651 * intensively
2652 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002653 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002654 err = -EAGAIN;
2655 goto out;
2656 }
2657
Chao Yu25a912e2018-01-10 18:18:51 +08002658 map.m_lblk = pg_start;
2659 map.m_len = pg_end - pg_start;
2660 total = 0;
2661
Chao Yud323d002015-10-27 09:53:45 +08002662 while (map.m_lblk < pg_end) {
2663 pgoff_t idx;
2664 int cnt = 0;
2665
2666do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002667 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002668 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002669 if (err)
2670 goto clear_out;
2671
2672 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002673 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002674 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002675 }
2676
Jaegeuk Kim91942322016-05-20 10:13:22 -07002677 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002678
2679 idx = map.m_lblk;
2680 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2681 struct page *page;
2682
Chao Yu4d57b862018-05-30 00:20:41 +08002683 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002684 if (IS_ERR(page)) {
2685 err = PTR_ERR(page);
2686 goto clear_out;
2687 }
2688
2689 set_page_dirty(page);
2690 f2fs_put_page(page, 1);
2691
2692 idx++;
2693 cnt++;
2694 total++;
2695 }
2696
2697 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002698check:
2699 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002700 goto do_map;
2701
Jaegeuk Kim91942322016-05-20 10:13:22 -07002702 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002703
2704 err = filemap_fdatawrite(inode->i_mapping);
2705 if (err)
2706 goto out;
2707 }
2708clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002709 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002710out:
Al Viro59551022016-01-22 15:40:57 -05002711 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002712 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002713 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002714 return err;
2715}
2716
2717static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2718{
2719 struct inode *inode = file_inode(filp);
2720 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2721 struct f2fs_defragment range;
2722 int err;
2723
2724 if (!capable(CAP_SYS_ADMIN))
2725 return -EPERM;
2726
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002727 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002728 return -EINVAL;
2729
Kinglong Meed7563862017-03-10 17:55:07 +08002730 if (f2fs_readonly(sbi->sb))
2731 return -EROFS;
2732
2733 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2734 sizeof(range)))
2735 return -EFAULT;
2736
2737 /* verify alignment of offset & size */
2738 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2739 return -EINVAL;
2740
2741 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2742 sbi->max_file_blocks))
2743 return -EINVAL;
2744
Chao Yud323d002015-10-27 09:53:45 +08002745 err = mnt_want_write_file(filp);
2746 if (err)
2747 return err;
2748
Chao Yud323d002015-10-27 09:53:45 +08002749 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002750 mnt_drop_write_file(filp);
2751
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002752 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002753 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002754 return err;
Chao Yud323d002015-10-27 09:53:45 +08002755
2756 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2757 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002758 return -EFAULT;
2759
2760 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002761}
2762
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002763static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2764 struct file *file_out, loff_t pos_out, size_t len)
2765{
2766 struct inode *src = file_inode(file_in);
2767 struct inode *dst = file_inode(file_out);
2768 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2769 size_t olen = len, dst_max_i_size = 0;
2770 size_t dst_osize;
2771 int ret;
2772
2773 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2774 src->i_sb != dst->i_sb)
2775 return -EXDEV;
2776
2777 if (unlikely(f2fs_readonly(src->i_sb)))
2778 return -EROFS;
2779
Chao Yufe8494b2016-08-04 20:13:02 +08002780 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2781 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002782
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302783 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002784 return -EOPNOTSUPP;
2785
Fan Lid95fd912016-09-13 11:35:42 +08002786 if (src == dst) {
2787 if (pos_in == pos_out)
2788 return 0;
2789 if (pos_out > pos_in && pos_out < pos_in + len)
2790 return -EINVAL;
2791 }
2792
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002793 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002794 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002795 ret = -EBUSY;
2796 if (!inode_trylock(dst))
2797 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002798 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002799
2800 ret = -EINVAL;
2801 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2802 goto out_unlock;
2803 if (len == 0)
2804 olen = len = src->i_size - pos_in;
2805 if (pos_in + len == src->i_size)
2806 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2807 if (len == 0) {
2808 ret = 0;
2809 goto out_unlock;
2810 }
2811
2812 dst_osize = dst->i_size;
2813 if (pos_out + olen > dst->i_size)
2814 dst_max_i_size = pos_out + olen;
2815
2816 /* verify the end result is block aligned */
2817 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2818 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2819 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2820 goto out_unlock;
2821
2822 ret = f2fs_convert_inline_inode(src);
2823 if (ret)
2824 goto out_unlock;
2825
2826 ret = f2fs_convert_inline_inode(dst);
2827 if (ret)
2828 goto out_unlock;
2829
2830 /* write out all dirty pages from offset */
2831 ret = filemap_write_and_wait_range(src->i_mapping,
2832 pos_in, pos_in + len);
2833 if (ret)
2834 goto out_unlock;
2835
2836 ret = filemap_write_and_wait_range(dst->i_mapping,
2837 pos_out, pos_out + len);
2838 if (ret)
2839 goto out_unlock;
2840
2841 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002842
2843 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2844 if (src != dst) {
2845 ret = -EBUSY;
2846 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2847 goto out_src;
2848 }
2849
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002850 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002851 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2852 pos_out >> F2FS_BLKSIZE_BITS,
2853 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002854
2855 if (!ret) {
2856 if (dst_max_i_size)
2857 f2fs_i_size_write(dst, dst_max_i_size);
2858 else if (dst_osize != dst->i_size)
2859 f2fs_i_size_write(dst, dst_osize);
2860 }
2861 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002862
2863 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002864 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002865out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002866 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002867out_unlock:
2868 if (src != dst)
2869 inode_unlock(dst);
2870out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002871 inode_unlock(src);
2872 return ret;
2873}
2874
2875static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2876{
2877 struct f2fs_move_range range;
2878 struct fd dst;
2879 int err;
2880
2881 if (!(filp->f_mode & FMODE_READ) ||
2882 !(filp->f_mode & FMODE_WRITE))
2883 return -EBADF;
2884
2885 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2886 sizeof(range)))
2887 return -EFAULT;
2888
2889 dst = fdget(range.dst_fd);
2890 if (!dst.file)
2891 return -EBADF;
2892
2893 if (!(dst.file->f_mode & FMODE_WRITE)) {
2894 err = -EBADF;
2895 goto err_out;
2896 }
2897
2898 err = mnt_want_write_file(filp);
2899 if (err)
2900 goto err_out;
2901
2902 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2903 range.pos_out, range.len);
2904
2905 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002906 if (err)
2907 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002908
2909 if (copy_to_user((struct f2fs_move_range __user *)arg,
2910 &range, sizeof(range)))
2911 err = -EFAULT;
2912err_out:
2913 fdput(dst);
2914 return err;
2915}
2916
Jaegeuk Kime066b832017-04-13 15:17:00 -07002917static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2918{
2919 struct inode *inode = file_inode(filp);
2920 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2921 struct sit_info *sm = SIT_I(sbi);
2922 unsigned int start_segno = 0, end_segno = 0;
2923 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2924 struct f2fs_flush_device range;
2925 int ret;
2926
2927 if (!capable(CAP_SYS_ADMIN))
2928 return -EPERM;
2929
2930 if (f2fs_readonly(sbi->sb))
2931 return -EROFS;
2932
Daniel Rosenberg43549942018-08-20 19:21:43 -07002933 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2934 return -EINVAL;
2935
Jaegeuk Kime066b832017-04-13 15:17:00 -07002936 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2937 sizeof(range)))
2938 return -EFAULT;
2939
Damien Le Moal09168782019-03-16 09:13:06 +09002940 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002941 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002942 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2943 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002944 return -EINVAL;
2945 }
2946
2947 ret = mnt_want_write_file(filp);
2948 if (ret)
2949 return ret;
2950
2951 if (range.dev_num != 0)
2952 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2953 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2954
2955 start_segno = sm->last_victim[FLUSH_DEVICE];
2956 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2957 start_segno = dev_start_segno;
2958 end_segno = min(start_segno + range.segments, dev_end_segno);
2959
2960 while (start_segno < end_segno) {
Chao Yufb24fea2020-01-14 19:36:50 +08002961 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002962 ret = -EBUSY;
2963 goto out;
2964 }
2965 sm->last_victim[GC_CB] = end_segno + 1;
2966 sm->last_victim[GC_GREEDY] = end_segno + 1;
2967 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2968 ret = f2fs_gc(sbi, true, true, start_segno);
2969 if (ret == -EAGAIN)
2970 ret = 0;
2971 else if (ret < 0)
2972 break;
2973 start_segno++;
2974 }
2975out:
2976 mnt_drop_write_file(filp);
2977 return ret;
2978}
2979
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002980static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2981{
2982 struct inode *inode = file_inode(filp);
2983 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2984
2985 /* Must validate to set it with SQLite behavior in Android. */
2986 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2987
2988 return put_user(sb_feature, (u32 __user *)arg);
2989}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002990
Chao Yu2c1d0302017-07-29 00:32:52 +08002991#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002992int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2993{
2994 struct dquot *transfer_to[MAXQUOTAS] = {};
2995 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2996 struct super_block *sb = sbi->sb;
2997 int err = 0;
2998
2999 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3000 if (!IS_ERR(transfer_to[PRJQUOTA])) {
3001 err = __dquot_transfer(inode, transfer_to);
3002 if (err)
3003 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3004 dqput(transfer_to[PRJQUOTA]);
3005 }
3006 return err;
3007}
3008
Chao Yu2c1d0302017-07-29 00:32:52 +08003009static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3010{
3011 struct inode *inode = file_inode(filp);
3012 struct f2fs_inode_info *fi = F2FS_I(inode);
3013 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003014 struct page *ipage;
3015 kprojid_t kprojid;
3016 int err;
3017
Chao Yu7beb01f2018-10-24 18:34:26 +08003018 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08003019 if (projid != F2FS_DEF_PROJID)
3020 return -EOPNOTSUPP;
3021 else
3022 return 0;
3023 }
3024
3025 if (!f2fs_has_extra_attr(inode))
3026 return -EOPNOTSUPP;
3027
3028 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3029
3030 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3031 return 0;
3032
Chao Yu2c1d0302017-07-29 00:32:52 +08003033 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003034 /* Is it quota file? Do not allow user to mess with it */
3035 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003036 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003037
Chao Yu4d57b862018-05-30 00:20:41 +08003038 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003039 if (IS_ERR(ipage))
3040 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003041
3042 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3043 i_projid)) {
3044 err = -EOVERFLOW;
3045 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003046 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003047 }
3048 f2fs_put_page(ipage, 1);
3049
Chao Yuc22aecd2018-04-21 17:53:52 +08003050 err = dquot_initialize(inode);
3051 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003052 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003053
Chao Yu78130812018-09-25 15:36:02 +08003054 f2fs_lock_op(sbi);
3055 err = f2fs_transfer_project_quota(inode, kprojid);
3056 if (err)
3057 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003058
3059 F2FS_I(inode)->i_projid = kprojid;
3060 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003061 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003062out_unlock:
3063 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003064 return err;
3065}
3066#else
Chao Yu78130812018-09-25 15:36:02 +08003067int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3068{
3069 return 0;
3070}
3071
Chao Yu2c1d0302017-07-29 00:32:52 +08003072static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3073{
3074 if (projid != F2FS_DEF_PROJID)
3075 return -EOPNOTSUPP;
3076 return 0;
3077}
3078#endif
3079
Eric Biggers36098552019-06-04 22:59:04 -07003080/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08003081
Eric Biggers36098552019-06-04 22:59:04 -07003082/*
3083 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3084 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3085 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3086 */
3087
3088static const struct {
3089 u32 iflag;
3090 u32 xflag;
3091} f2fs_xflags_map[] = {
3092 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3093 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3094 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3095 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3096 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3097 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3098};
3099
3100#define F2FS_SUPPORTED_XFLAGS ( \
3101 FS_XFLAG_SYNC | \
3102 FS_XFLAG_IMMUTABLE | \
3103 FS_XFLAG_APPEND | \
3104 FS_XFLAG_NODUMP | \
3105 FS_XFLAG_NOATIME | \
3106 FS_XFLAG_PROJINHERIT)
3107
3108/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3109static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3110{
3111 u32 xflags = 0;
3112 int i;
3113
3114 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3115 if (iflags & f2fs_xflags_map[i].iflag)
3116 xflags |= f2fs_xflags_map[i].xflag;
3117
Chao Yu2c1d0302017-07-29 00:32:52 +08003118 return xflags;
3119}
3120
Eric Biggers36098552019-06-04 22:59:04 -07003121/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3122static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003123{
Eric Biggers36098552019-06-04 22:59:04 -07003124 u32 iflags = 0;
3125 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08003126
Eric Biggers36098552019-06-04 22:59:04 -07003127 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3128 if (xflags & f2fs_xflags_map[i].xflag)
3129 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08003130
3131 return iflags;
3132}
3133
Eric Biggers6fc93c42019-07-01 13:26:29 -07003134static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3135{
3136 struct f2fs_inode_info *fi = F2FS_I(inode);
3137
3138 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3139
3140 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3141 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3142}
3143
Chao Yu2c1d0302017-07-29 00:32:52 +08003144static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3145{
3146 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08003147 struct fsxattr fa;
3148
Eric Biggers6fc93c42019-07-01 13:26:29 -07003149 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08003150
3151 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3152 return -EFAULT;
3153 return 0;
3154}
3155
3156static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3157{
3158 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003159 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07003160 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003161 int err;
3162
3163 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3164 return -EFAULT;
3165
3166 /* Make sure caller has proper permission */
3167 if (!inode_owner_or_capable(inode))
3168 return -EACCES;
3169
Eric Biggers36098552019-06-04 22:59:04 -07003170 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08003171 return -EOPNOTSUPP;
3172
Eric Biggers36098552019-06-04 22:59:04 -07003173 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3174 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003175 return -EOPNOTSUPP;
3176
3177 err = mnt_want_write_file(filp);
3178 if (err)
3179 return err;
3180
3181 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003182
3183 f2fs_fill_fsxattr(inode, &old_fa);
3184 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09003185 if (err)
3186 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07003187
Eric Biggers36098552019-06-04 22:59:04 -07003188 err = f2fs_setflags_common(inode, iflags,
3189 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08003190 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003191 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08003192
3193 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09003194out:
3195 inode_unlock(inode);
3196 mnt_drop_write_file(filp);
3197 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003198}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003199
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003200int f2fs_pin_file_control(struct inode *inode, bool inc)
3201{
3202 struct f2fs_inode_info *fi = F2FS_I(inode);
3203 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3204
3205 /* Use i_gc_failures for normal file as a risk signal. */
3206 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003207 f2fs_i_gc_failures_write(inode,
3208 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003209
Chao Yu2ef79ec2018-05-07 20:28:54 +08003210 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003211 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3212 __func__, inode->i_ino,
3213 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003214 clear_inode_flag(inode, FI_PIN_FILE);
3215 return -EAGAIN;
3216 }
3217 return 0;
3218}
3219
3220static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3221{
3222 struct inode *inode = file_inode(filp);
3223 __u32 pin;
3224 int ret = 0;
3225
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003226 if (get_user(pin, (__u32 __user *)arg))
3227 return -EFAULT;
3228
3229 if (!S_ISREG(inode->i_mode))
3230 return -EINVAL;
3231
3232 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3233 return -EROFS;
3234
3235 ret = mnt_want_write_file(filp);
3236 if (ret)
3237 return ret;
3238
3239 inode_lock(inode);
3240
Chao Yu4d57b862018-05-30 00:20:41 +08003241 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb2018-01-17 16:31:38 +08003242 ret = -EINVAL;
3243 goto out;
3244 }
3245
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003246 if (!pin) {
3247 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003248 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003249 goto done;
3250 }
3251
3252 if (f2fs_pin_file_control(inode, false)) {
3253 ret = -EAGAIN;
3254 goto out;
3255 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003256
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003257 ret = f2fs_convert_inline_inode(inode);
3258 if (ret)
3259 goto out;
3260
Chao Yu4c8ff702019-11-01 18:07:14 +08003261 if (f2fs_disable_compressed_file(inode)) {
3262 ret = -EOPNOTSUPP;
3263 goto out;
3264 }
3265
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003266 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003267 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003268done:
3269 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3270out:
3271 inode_unlock(inode);
3272 mnt_drop_write_file(filp);
3273 return ret;
3274}
3275
3276static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3277{
3278 struct inode *inode = file_inode(filp);
3279 __u32 pin = 0;
3280
3281 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003282 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003283 return put_user(pin, (u32 __user *)arg);
3284}
3285
Chao Yuc4020b22018-01-11 14:42:30 +08003286int f2fs_precache_extents(struct inode *inode)
3287{
3288 struct f2fs_inode_info *fi = F2FS_I(inode);
3289 struct f2fs_map_blocks map;
3290 pgoff_t m_next_extent;
3291 loff_t end;
3292 int err;
3293
3294 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3295 return -EOPNOTSUPP;
3296
3297 map.m_lblk = 0;
3298 map.m_next_pgofs = NULL;
3299 map.m_next_extent = &m_next_extent;
3300 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003301 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003302 end = F2FS_I_SB(inode)->max_file_blocks;
3303
3304 while (map.m_lblk < end) {
3305 map.m_len = end - map.m_lblk;
3306
Chao Yub2532c62018-04-24 10:55:28 +08003307 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003308 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003309 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003310 if (err)
3311 return err;
3312
3313 map.m_lblk = m_next_extent;
3314 }
3315
3316 return err;
3317}
3318
3319static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3320{
3321 return f2fs_precache_extents(file_inode(filp));
3322}
3323
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003324static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3325{
3326 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3327 __u64 block_count;
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003328
3329 if (!capable(CAP_SYS_ADMIN))
3330 return -EPERM;
3331
3332 if (f2fs_readonly(sbi->sb))
3333 return -EROFS;
3334
3335 if (copy_from_user(&block_count, (void __user *)arg,
3336 sizeof(block_count)))
3337 return -EFAULT;
3338
Jaegeuk Kimb4b10062020-03-31 11:43:07 -07003339 return f2fs_resize_fs(sbi, block_count);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003340}
3341
Eric Biggers95ae2512019-07-22 09:26:24 -07003342static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3343{
3344 struct inode *inode = file_inode(filp);
3345
3346 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3347
3348 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3349 f2fs_warn(F2FS_I_SB(inode),
3350 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3351 inode->i_ino);
3352 return -EOPNOTSUPP;
3353 }
3354
3355 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3356}
3357
3358static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3359{
3360 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3361 return -EOPNOTSUPP;
3362
3363 return fsverity_ioctl_measure(filp, (void __user *)arg);
3364}
3365
Eric Biggers3357af82020-07-14 15:18:12 -07003366static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003367{
3368 struct inode *inode = file_inode(filp);
3369 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3370 char *vbuf;
3371 int count;
3372 int err = 0;
3373
3374 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3375 if (!vbuf)
3376 return -ENOMEM;
3377
3378 down_read(&sbi->sb_lock);
3379 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3380 ARRAY_SIZE(sbi->raw_super->volume_name),
3381 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3382 up_read(&sbi->sb_lock);
3383
3384 if (copy_to_user((char __user *)arg, vbuf,
3385 min(FSLABEL_MAX, count)))
3386 err = -EFAULT;
3387
3388 kvfree(vbuf);
3389 return err;
3390}
3391
Eric Biggers3357af82020-07-14 15:18:12 -07003392static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003393{
3394 struct inode *inode = file_inode(filp);
3395 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3396 char *vbuf;
3397 int err = 0;
3398
3399 if (!capable(CAP_SYS_ADMIN))
3400 return -EPERM;
3401
3402 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3403 if (IS_ERR(vbuf))
3404 return PTR_ERR(vbuf);
3405
3406 err = mnt_want_write_file(filp);
3407 if (err)
3408 goto out;
3409
3410 down_write(&sbi->sb_lock);
3411
3412 memset(sbi->raw_super->volume_name, 0,
3413 sizeof(sbi->raw_super->volume_name));
3414 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3415 sbi->raw_super->volume_name,
3416 ARRAY_SIZE(sbi->raw_super->volume_name));
3417
3418 err = f2fs_commit_super(sbi, false);
3419
3420 up_write(&sbi->sb_lock);
3421
3422 mnt_drop_write_file(filp);
3423out:
3424 kfree(vbuf);
3425 return err;
3426}
3427
Chao Yu439dfb12020-02-21 18:09:21 +08003428static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3429{
3430 struct inode *inode = file_inode(filp);
3431 __u64 blocks;
3432
3433 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3434 return -EOPNOTSUPP;
3435
3436 if (!f2fs_compressed_file(inode))
3437 return -EINVAL;
3438
3439 blocks = F2FS_I(inode)->i_compr_blocks;
3440 return put_user(blocks, (u64 __user *)arg);
3441}
3442
Chao Yuef8d5632020-03-06 15:36:09 +08003443static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3444{
3445 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3446 unsigned int released_blocks = 0;
3447 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3448 block_t blkaddr;
3449 int i;
3450
3451 for (i = 0; i < count; i++) {
3452 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3453 dn->ofs_in_node + i);
3454
3455 if (!__is_valid_data_blkaddr(blkaddr))
3456 continue;
3457 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3458 DATA_GENERIC_ENHANCE)))
3459 return -EFSCORRUPTED;
3460 }
3461
3462 while (count) {
3463 int compr_blocks = 0;
3464
3465 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3466 blkaddr = f2fs_data_blkaddr(dn);
3467
3468 if (i == 0) {
3469 if (blkaddr == COMPRESS_ADDR)
3470 continue;
3471 dn->ofs_in_node += cluster_size;
3472 goto next;
3473 }
3474
3475 if (__is_valid_data_blkaddr(blkaddr))
3476 compr_blocks++;
3477
3478 if (blkaddr != NEW_ADDR)
3479 continue;
3480
3481 dn->data_blkaddr = NULL_ADDR;
3482 f2fs_set_data_blkaddr(dn);
3483 }
3484
3485 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3486 dec_valid_block_count(sbi, dn->inode,
3487 cluster_size - compr_blocks);
3488
3489 released_blocks += cluster_size - compr_blocks;
3490next:
3491 count -= cluster_size;
3492 }
3493
3494 return released_blocks;
3495}
3496
3497static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3498{
3499 struct inode *inode = file_inode(filp);
3500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3501 pgoff_t page_idx = 0, last_idx;
3502 unsigned int released_blocks = 0;
3503 int ret;
3504 int writecount;
3505
3506 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3507 return -EOPNOTSUPP;
3508
3509 if (!f2fs_compressed_file(inode))
3510 return -EINVAL;
3511
3512 if (f2fs_readonly(sbi->sb))
3513 return -EROFS;
3514
3515 ret = mnt_want_write_file(filp);
3516 if (ret)
3517 return ret;
3518
3519 f2fs_balance_fs(F2FS_I_SB(inode), true);
3520
3521 inode_lock(inode);
3522
3523 writecount = atomic_read(&inode->i_writecount);
3524 if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
3525 ret = -EBUSY;
3526 goto out;
3527 }
3528
3529 if (IS_IMMUTABLE(inode)) {
3530 ret = -EINVAL;
3531 goto out;
3532 }
3533
3534 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3535 if (ret)
3536 goto out;
3537
Chao Yuef8d5632020-03-06 15:36:09 +08003538 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3539 f2fs_set_inode_flags(inode);
3540 inode->i_ctime = current_time(inode);
3541 f2fs_mark_inode_dirty_sync(inode, true);
3542
Daeho Jeong567c4bf2020-07-30 14:09:28 +09003543 if (!F2FS_I(inode)->i_compr_blocks)
3544 goto out;
3545
Chao Yuef8d5632020-03-06 15:36:09 +08003546 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3547 down_write(&F2FS_I(inode)->i_mmap_sem);
3548
3549 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3550
3551 while (page_idx < last_idx) {
3552 struct dnode_of_data dn;
3553 pgoff_t end_offset, count;
3554
3555 set_new_dnode(&dn, inode, NULL, NULL, 0);
3556 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3557 if (ret) {
3558 if (ret == -ENOENT) {
3559 page_idx = f2fs_get_next_page_offset(&dn,
3560 page_idx);
3561 ret = 0;
3562 continue;
3563 }
3564 break;
3565 }
3566
3567 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3568 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003569 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuef8d5632020-03-06 15:36:09 +08003570
3571 ret = release_compress_blocks(&dn, count);
3572
3573 f2fs_put_dnode(&dn);
3574
3575 if (ret < 0)
3576 break;
3577
3578 page_idx += count;
3579 released_blocks += ret;
3580 }
3581
3582 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3583 up_write(&F2FS_I(inode)->i_mmap_sem);
3584out:
3585 inode_unlock(inode);
3586
3587 mnt_drop_write_file(filp);
3588
3589 if (ret >= 0) {
3590 ret = put_user(released_blocks, (u64 __user *)arg);
3591 } else if (released_blocks && F2FS_I(inode)->i_compr_blocks) {
3592 set_sbi_flag(sbi, SBI_NEED_FSCK);
3593 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3594 "iblocks=%llu, released=%u, compr_blocks=%llu, "
3595 "run fsck to fix.",
3596 __func__, inode->i_ino, inode->i_blocks,
3597 released_blocks,
3598 F2FS_I(inode)->i_compr_blocks);
3599 }
3600
3601 return ret;
3602}
3603
Chao Yuc75488f2020-03-06 14:35:33 +08003604static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3605{
3606 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3607 unsigned int reserved_blocks = 0;
3608 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3609 block_t blkaddr;
3610 int i;
3611
3612 for (i = 0; i < count; i++) {
3613 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3614 dn->ofs_in_node + i);
3615
3616 if (!__is_valid_data_blkaddr(blkaddr))
3617 continue;
3618 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3619 DATA_GENERIC_ENHANCE)))
3620 return -EFSCORRUPTED;
3621 }
3622
3623 while (count) {
3624 int compr_blocks = 0;
3625 blkcnt_t reserved;
3626 int ret;
3627
3628 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3629 blkaddr = f2fs_data_blkaddr(dn);
3630
3631 if (i == 0) {
3632 if (blkaddr == COMPRESS_ADDR)
3633 continue;
3634 dn->ofs_in_node += cluster_size;
3635 goto next;
3636 }
3637
3638 if (__is_valid_data_blkaddr(blkaddr)) {
3639 compr_blocks++;
3640 continue;
3641 }
3642
3643 dn->data_blkaddr = NEW_ADDR;
3644 f2fs_set_data_blkaddr(dn);
3645 }
3646
3647 reserved = cluster_size - compr_blocks;
3648 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3649 if (ret)
3650 return ret;
3651
3652 if (reserved != cluster_size - compr_blocks)
3653 return -ENOSPC;
3654
3655 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3656
3657 reserved_blocks += reserved;
3658next:
3659 count -= cluster_size;
3660 }
3661
3662 return reserved_blocks;
3663}
3664
3665static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3666{
3667 struct inode *inode = file_inode(filp);
3668 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3669 pgoff_t page_idx = 0, last_idx;
3670 unsigned int reserved_blocks = 0;
3671 int ret;
3672
3673 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3674 return -EOPNOTSUPP;
3675
3676 if (!f2fs_compressed_file(inode))
3677 return -EINVAL;
3678
3679 if (f2fs_readonly(sbi->sb))
3680 return -EROFS;
3681
3682 ret = mnt_want_write_file(filp);
3683 if (ret)
3684 return ret;
3685
3686 if (F2FS_I(inode)->i_compr_blocks)
3687 goto out;
3688
3689 f2fs_balance_fs(F2FS_I_SB(inode), true);
3690
3691 inode_lock(inode);
3692
3693 if (!IS_IMMUTABLE(inode)) {
3694 ret = -EINVAL;
3695 goto unlock_inode;
3696 }
3697
3698 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3699 down_write(&F2FS_I(inode)->i_mmap_sem);
3700
3701 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3702
3703 while (page_idx < last_idx) {
3704 struct dnode_of_data dn;
3705 pgoff_t end_offset, count;
3706
3707 set_new_dnode(&dn, inode, NULL, NULL, 0);
3708 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3709 if (ret) {
3710 if (ret == -ENOENT) {
3711 page_idx = f2fs_get_next_page_offset(&dn,
3712 page_idx);
3713 ret = 0;
3714 continue;
3715 }
3716 break;
3717 }
3718
3719 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3720 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003721 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuc75488f2020-03-06 14:35:33 +08003722
3723 ret = reserve_compress_blocks(&dn, count);
3724
3725 f2fs_put_dnode(&dn);
3726
3727 if (ret < 0)
3728 break;
3729
3730 page_idx += count;
3731 reserved_blocks += ret;
3732 }
3733
3734 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3735 up_write(&F2FS_I(inode)->i_mmap_sem);
3736
3737 if (ret >= 0) {
3738 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3739 f2fs_set_inode_flags(inode);
3740 inode->i_ctime = current_time(inode);
3741 f2fs_mark_inode_dirty_sync(inode, true);
3742 }
3743unlock_inode:
3744 inode_unlock(inode);
3745out:
3746 mnt_drop_write_file(filp);
3747
3748 if (ret >= 0) {
3749 ret = put_user(reserved_blocks, (u64 __user *)arg);
3750 } else if (reserved_blocks && F2FS_I(inode)->i_compr_blocks) {
3751 set_sbi_flag(sbi, SBI_NEED_FSCK);
3752 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3753 "iblocks=%llu, reserved=%u, compr_blocks=%llu, "
3754 "run fsck to fix.",
3755 __func__, inode->i_ino, inode->i_blocks,
3756 reserved_blocks,
3757 F2FS_I(inode)->i_compr_blocks);
3758 }
3759
3760 return ret;
3761}
3762
Daeho Jeong9af84642020-07-21 12:21:11 +09003763static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3764 pgoff_t off, block_t block, block_t len, u32 flags)
3765{
3766 struct request_queue *q = bdev_get_queue(bdev);
3767 sector_t sector = SECTOR_FROM_BLOCK(block);
3768 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3769 int ret = 0;
3770
3771 if (!q)
3772 return -ENXIO;
3773
3774 if (flags & F2FS_TRIM_FILE_DISCARD)
3775 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3776 blk_queue_secure_erase(q) ?
3777 BLKDEV_DISCARD_SECURE : 0);
3778
3779 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3780 if (IS_ENCRYPTED(inode))
3781 ret = fscrypt_zeroout_range(inode, off, block, len);
3782 else
3783 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3784 GFP_NOFS, 0);
3785 }
3786
3787 return ret;
3788}
3789
3790static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3791{
3792 struct inode *inode = file_inode(filp);
3793 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3794 struct address_space *mapping = inode->i_mapping;
3795 struct block_device *prev_bdev = NULL;
3796 struct f2fs_sectrim_range range;
3797 pgoff_t index, pg_end, prev_index = 0;
3798 block_t prev_block = 0, len = 0;
3799 loff_t end_addr;
3800 bool to_end = false;
3801 int ret = 0;
3802
3803 if (!(filp->f_mode & FMODE_WRITE))
3804 return -EBADF;
3805
3806 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3807 sizeof(range)))
3808 return -EFAULT;
3809
3810 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3811 !S_ISREG(inode->i_mode))
3812 return -EINVAL;
3813
3814 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3815 !f2fs_hw_support_discard(sbi)) ||
3816 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3817 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3818 return -EOPNOTSUPP;
3819
3820 file_start_write(filp);
3821 inode_lock(inode);
3822
3823 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3824 range.start >= inode->i_size) {
3825 ret = -EINVAL;
3826 goto err;
3827 }
3828
3829 if (range.len == 0)
3830 goto err;
3831
3832 if (inode->i_size - range.start > range.len) {
3833 end_addr = range.start + range.len;
3834 } else {
3835 end_addr = range.len == (u64)-1 ?
3836 sbi->sb->s_maxbytes : inode->i_size;
3837 to_end = true;
3838 }
3839
3840 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3841 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3842 ret = -EINVAL;
3843 goto err;
3844 }
3845
3846 index = F2FS_BYTES_TO_BLK(range.start);
3847 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3848
3849 ret = f2fs_convert_inline_inode(inode);
3850 if (ret)
3851 goto err;
3852
3853 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3854 down_write(&F2FS_I(inode)->i_mmap_sem);
3855
3856 ret = filemap_write_and_wait_range(mapping, range.start,
3857 to_end ? LLONG_MAX : end_addr - 1);
3858 if (ret)
3859 goto out;
3860
3861 truncate_inode_pages_range(mapping, range.start,
3862 to_end ? -1 : end_addr - 1);
3863
3864 while (index < pg_end) {
3865 struct dnode_of_data dn;
3866 pgoff_t end_offset, count;
3867 int i;
3868
3869 set_new_dnode(&dn, inode, NULL, NULL, 0);
3870 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3871 if (ret) {
3872 if (ret == -ENOENT) {
3873 index = f2fs_get_next_page_offset(&dn, index);
3874 continue;
3875 }
3876 goto out;
3877 }
3878
3879 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3880 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3881 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3882 struct block_device *cur_bdev;
3883 block_t blkaddr = f2fs_data_blkaddr(&dn);
3884
3885 if (!__is_valid_data_blkaddr(blkaddr))
3886 continue;
3887
3888 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3889 DATA_GENERIC_ENHANCE)) {
3890 ret = -EFSCORRUPTED;
3891 f2fs_put_dnode(&dn);
3892 goto out;
3893 }
3894
3895 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3896 if (f2fs_is_multi_device(sbi)) {
3897 int di = f2fs_target_device_index(sbi, blkaddr);
3898
3899 blkaddr -= FDEV(di).start_blk;
3900 }
3901
3902 if (len) {
3903 if (prev_bdev == cur_bdev &&
3904 index == prev_index + len &&
3905 blkaddr == prev_block + len) {
3906 len++;
3907 } else {
3908 ret = f2fs_secure_erase(prev_bdev,
3909 inode, prev_index, prev_block,
3910 len, range.flags);
3911 if (ret) {
3912 f2fs_put_dnode(&dn);
3913 goto out;
3914 }
3915
3916 len = 0;
3917 }
3918 }
3919
3920 if (!len) {
3921 prev_bdev = cur_bdev;
3922 prev_index = index;
3923 prev_block = blkaddr;
3924 len = 1;
3925 }
3926 }
3927
3928 f2fs_put_dnode(&dn);
3929
3930 if (fatal_signal_pending(current)) {
3931 ret = -EINTR;
3932 goto out;
3933 }
3934 cond_resched();
3935 }
3936
3937 if (len)
3938 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3939 prev_block, len, range.flags);
3940out:
3941 up_write(&F2FS_I(inode)->i_mmap_sem);
3942 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3943err:
3944 inode_unlock(inode);
3945 file_end_write(filp);
3946
3947 return ret;
3948}
3949
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003950long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3951{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003952 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3953 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08003954 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
3955 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003956
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003957 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07003958 case FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003959 return f2fs_ioc_getflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003960 case FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003961 return f2fs_ioc_setflags(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003962 case FS_IOC_GETVERSION:
Chao Yud49f3e82015-01-23 20:36:04 +08003963 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003964 case F2FS_IOC_START_ATOMIC_WRITE:
3965 return f2fs_ioc_start_atomic_write(filp);
3966 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3967 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07003968 case F2FS_IOC_START_VOLATILE_WRITE:
3969 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08003970 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3971 return f2fs_ioc_release_volatile_write(filp);
3972 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3973 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08003974 case F2FS_IOC_SHUTDOWN:
3975 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003976 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003977 return f2fs_ioc_fitrim(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003978 case FS_IOC_SET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003979 return f2fs_ioc_set_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003980 case FS_IOC_GET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003981 return f2fs_ioc_get_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07003982 case FS_IOC_GET_ENCRYPTION_PWSALT:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003983 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07003984 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3985 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3986 case FS_IOC_ADD_ENCRYPTION_KEY:
3987 return f2fs_ioc_add_encryption_key(filp, arg);
3988 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3989 return f2fs_ioc_remove_encryption_key(filp, arg);
3990 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3991 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
3992 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3993 return f2fs_ioc_get_encryption_key_status(filp, arg);
Eric Biggersee446e12020-03-14 13:50:51 -07003994 case FS_IOC_GET_ENCRYPTION_NONCE:
3995 return f2fs_ioc_get_encryption_nonce(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003996 case F2FS_IOC_GARBAGE_COLLECT:
3997 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003998 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3999 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08004000 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08004001 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08004002 case F2FS_IOC_DEFRAGMENT:
4003 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004004 case F2FS_IOC_MOVE_RANGE:
4005 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07004006 case F2FS_IOC_FLUSH_DEVICE:
4007 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004008 case F2FS_IOC_GET_FEATURES:
4009 return f2fs_ioc_get_features(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004010 case FS_IOC_FSGETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08004011 return f2fs_ioc_fsgetxattr(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004012 case FS_IOC_FSSETXATTR:
Chao Yu2c1d0302017-07-29 00:32:52 +08004013 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004014 case F2FS_IOC_GET_PIN_FILE:
4015 return f2fs_ioc_get_pin_file(filp, arg);
4016 case F2FS_IOC_SET_PIN_FILE:
4017 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08004018 case F2FS_IOC_PRECACHE_EXTENTS:
4019 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004020 case F2FS_IOC_RESIZE_FS:
4021 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07004022 case FS_IOC_ENABLE_VERITY:
4023 return f2fs_ioc_enable_verity(filp, arg);
4024 case FS_IOC_MEASURE_VERITY:
4025 return f2fs_ioc_measure_verity(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004026 case FS_IOC_GETFSLABEL:
4027 return f2fs_ioc_getfslabel(filp, arg);
4028 case FS_IOC_SETFSLABEL:
4029 return f2fs_ioc_setfslabel(filp, arg);
Chao Yu439dfb12020-02-21 18:09:21 +08004030 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4031 return f2fs_get_compress_blocks(filp, arg);
Chao Yuef8d5632020-03-06 15:36:09 +08004032 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4033 return f2fs_release_compress_blocks(filp, arg);
Chao Yuc75488f2020-03-06 14:35:33 +08004034 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4035 return f2fs_reserve_compress_blocks(filp, arg);
Daeho Jeong9af84642020-07-21 12:21:11 +09004036 case F2FS_IOC_SEC_TRIM_FILE:
4037 return f2fs_sec_trim_file(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004038 default:
4039 return -ENOTTY;
4040 }
4041}
4042
Chao Yu4c8ff702019-11-01 18:07:14 +08004043static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4044{
4045 struct file *file = iocb->ki_filp;
4046 struct inode *inode = file_inode(file);
Chao Yu8b83ac82020-04-16 18:16:56 +08004047 int ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004048
4049 if (!f2fs_is_compress_backend_ready(inode))
4050 return -EOPNOTSUPP;
4051
Chao Yu8b83ac82020-04-16 18:16:56 +08004052 ret = generic_file_read_iter(iocb, iter);
4053
4054 if (ret > 0)
4055 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4056
4057 return ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004058}
4059
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004060static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4061{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004062 struct file *file = iocb->ki_filp;
4063 struct inode *inode = file_inode(file);
4064 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004065
Chao Yu126ce722019-04-02 18:52:22 +08004066 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4067 ret = -EIO;
4068 goto out;
4069 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02004070
Chao Yu7bd29352020-02-24 19:20:15 +08004071 if (!f2fs_is_compress_backend_ready(inode)) {
4072 ret = -EOPNOTSUPP;
4073 goto out;
4074 }
Chao Yu4c8ff702019-11-01 18:07:14 +08004075
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004076 if (iocb->ki_flags & IOCB_NOWAIT) {
4077 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08004078 ret = -EAGAIN;
4079 goto out;
4080 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004081 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004082 inode_lock(inode);
4083 }
4084
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004085 ret = generic_write_checks(iocb, from);
4086 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004087 bool preallocated = false;
4088 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004089 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004090
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004091 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4092 set_inode_flag(inode, FI_NO_PREALLOC);
4093
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004094 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4095 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004096 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004097 f2fs_has_inline_data(inode) ||
4098 f2fs_force_buffered_io(inode, iocb, from)) {
4099 clear_inode_flag(inode, FI_NO_PREALLOC);
4100 inode_unlock(inode);
4101 ret = -EAGAIN;
4102 goto out;
4103 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004104 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07004105 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004106
4107 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4108 goto write;
4109
4110 if (iocb->ki_flags & IOCB_DIRECT) {
4111 /*
4112 * Convert inline data for Direct I/O before entering
4113 * f2fs_direct_IO().
4114 */
4115 err = f2fs_convert_inline_inode(inode);
4116 if (err)
4117 goto out_err;
4118 /*
4119 * If force_buffere_io() is true, we have to allocate
4120 * blocks all the time, since f2fs_direct_IO will fall
4121 * back to buffered IO.
4122 */
4123 if (!f2fs_force_buffered_io(inode, iocb, from) &&
4124 allow_outplace_dio(inode, iocb, from))
4125 goto write;
4126 }
4127 preallocated = true;
4128 target_size = iocb->ki_pos + iov_iter_count(from);
4129
4130 err = f2fs_preallocate_blocks(iocb, from);
4131 if (err) {
4132out_err:
4133 clear_inode_flag(inode, FI_NO_PREALLOC);
4134 inode_unlock(inode);
4135 ret = err;
4136 goto out;
4137 }
4138write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004139 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004140 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08004141
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004142 /* if we couldn't write data, we should deallocate blocks. */
4143 if (preallocated && i_size_read(inode) < target_size)
4144 f2fs_truncate(inode);
4145
Chao Yub0af6d42017-08-02 23:21:48 +08004146 if (ret > 0)
4147 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004148 }
4149 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08004150out:
4151 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4152 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07004153 if (ret > 0)
4154 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004155 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004156}
4157
Namjae Jeone9750822013-02-04 23:41:41 +09004158#ifdef CONFIG_COMPAT
4159long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4160{
4161 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07004162 case FS_IOC32_GETFLAGS:
4163 cmd = FS_IOC_GETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09004164 break;
Eric Biggers3357af82020-07-14 15:18:12 -07004165 case FS_IOC32_SETFLAGS:
4166 cmd = FS_IOC_SETFLAGS;
Namjae Jeone9750822013-02-04 23:41:41 +09004167 break;
Eric Biggers3357af82020-07-14 15:18:12 -07004168 case FS_IOC32_GETVERSION:
4169 cmd = FS_IOC_GETVERSION;
Chao Yu04ef4b62015-11-10 18:44:20 +08004170 break;
4171 case F2FS_IOC_START_ATOMIC_WRITE:
4172 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4173 case F2FS_IOC_START_VOLATILE_WRITE:
4174 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4175 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4176 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02004177 case FITRIM:
Eric Biggers3357af82020-07-14 15:18:12 -07004178 case FS_IOC_SET_ENCRYPTION_POLICY:
4179 case FS_IOC_GET_ENCRYPTION_PWSALT:
4180 case FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07004181 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4182 case FS_IOC_ADD_ENCRYPTION_KEY:
4183 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4184 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4185 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Eric Biggersee446e12020-03-14 13:50:51 -07004186 case FS_IOC_GET_ENCRYPTION_NONCE:
Chao Yu04ef4b62015-11-10 18:44:20 +08004187 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07004188 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08004189 case F2FS_IOC_WRITE_CHECKPOINT:
4190 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004191 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07004192 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004193 case F2FS_IOC_GET_FEATURES:
Eric Biggers3357af82020-07-14 15:18:12 -07004194 case FS_IOC_FSGETXATTR:
4195 case FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004196 case F2FS_IOC_GET_PIN_FILE:
4197 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08004198 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004199 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07004200 case FS_IOC_ENABLE_VERITY:
4201 case FS_IOC_MEASURE_VERITY:
Eric Biggers3357af82020-07-14 15:18:12 -07004202 case FS_IOC_GETFSLABEL:
4203 case FS_IOC_SETFSLABEL:
Chao Yu439dfb12020-02-21 18:09:21 +08004204 case F2FS_IOC_GET_COMPRESS_BLOCKS:
Chao Yuef8d5632020-03-06 15:36:09 +08004205 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
Chao Yuc75488f2020-03-06 14:35:33 +08004206 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
Daeho Jeong9af84642020-07-21 12:21:11 +09004207 case F2FS_IOC_SEC_TRIM_FILE:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004208 break;
Namjae Jeone9750822013-02-04 23:41:41 +09004209 default:
4210 return -ENOIOCTLCMD;
4211 }
4212 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4213}
4214#endif
4215
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004216const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08004217 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08004218 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004219 .write_iter = f2fs_file_write_iter,
4220 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08004221 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004222 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07004223 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004224 .fsync = f2fs_sync_file,
4225 .fallocate = f2fs_fallocate,
4226 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09004227#ifdef CONFIG_COMPAT
4228 .compat_ioctl = f2fs_compat_ioctl,
4229#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004230 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04004231 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004232};