blob: ab4ea2ddcc8b00b4806d11ed606084324150a617 [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>
Miklos Szeredi9b1bb012021-04-07 14:36:43 +020025#include <linux/fileattr.h>
Daeho Jeong0f6b56e2021-08-02 21:22:45 -070026#include <linux/fadvise.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090027
28#include "f2fs.h"
29#include "node.h"
30#include "segment.h"
31#include "xattr.h"
32#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080033#include "gc.h"
Daeho Jeong52118742021-08-19 20:52:28 -070034#include "iostat.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090035#include <trace/events/f2fs.h>
Chao Yufa4320c2020-11-02 14:21:31 +080036#include <uapi/linux/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090037
Souptick Joarderea4d4792018-04-15 01:40:02 +053038static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080039{
40 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053041 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080042
43 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053044 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080045 up_read(&F2FS_I(inode)->i_mmap_sem);
46
Chao Yu8b83ac82020-04-16 18:16:56 +080047 if (!ret)
48 f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
49 F2FS_BLKSIZE);
50
Chao Yud7648342019-04-15 15:22:19 +080051 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
52
Souptick Joarderea4d4792018-04-15 01:40:02 +053053 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080054}
55
Souptick Joarderea4d4792018-04-15 01:40:02 +053056static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090057{
58 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080059 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070060 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080061 struct dnode_of_data dn;
Chao Yu4c8ff702019-11-01 18:07:14 +080062 bool need_alloc = true;
63 int err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090064
Chao Yue0fcd012020-12-26 18:07:01 +080065 if (unlikely(IS_IMMUTABLE(inode)))
66 return VM_FAULT_SIGBUS;
67
Jaegeuk Kimc6140412021-05-25 11:39:35 -070068 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
69 return VM_FAULT_SIGBUS;
70
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020071 if (unlikely(f2fs_cp_error(sbi))) {
72 err = -EIO;
73 goto err;
74 }
75
Chao Yu00e09c02019-08-23 17:58:36 +080076 if (!f2fs_is_checkpoint_ready(sbi)) {
77 err = -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +080078 goto err;
Chao Yu00e09c02019-08-23 17:58:36 +080079 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090080
Chao Yuc8e43d52021-02-02 16:03:11 +080081 err = f2fs_convert_inline_inode(inode);
82 if (err)
83 goto err;
84
Chao Yu4c8ff702019-11-01 18:07:14 +080085#ifdef CONFIG_F2FS_FS_COMPRESSION
86 if (f2fs_compressed_file(inode)) {
87 int ret = f2fs_is_compressed_cluster(inode, page->index);
88
89 if (ret < 0) {
90 err = ret;
91 goto err;
92 } else if (ret) {
Chao Yu4c8ff702019-11-01 18:07:14 +080093 need_alloc = false;
94 }
95 }
96#endif
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080097 /* should do out of any locked page */
Chao Yu4c8ff702019-11-01 18:07:14 +080098 if (need_alloc)
99 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -0800100
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900101 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700102
103 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700104
Dave Jiang11bac802017-02-24 14:56:41 -0800105 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800106 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900107 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900108 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900109 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900110 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900111 unlock_page(page);
112 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800113 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900114 }
115
Chao Yu4c8ff702019-11-01 18:07:14 +0800116 if (need_alloc) {
117 /* block allocation */
Chao Yu0ef81832020-06-18 14:36:22 +0800118 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Chao Yu4c8ff702019-11-01 18:07:14 +0800119 set_new_dnode(&dn, inode, NULL, NULL, 0);
120 err = f2fs_get_block(&dn, page->index);
Chao Yu0ef81832020-06-18 14:36:22 +0800121 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Chao Yu39a86952018-09-27 18:33:18 +0800122 }
123
Chao Yu06c75402020-03-02 17:34:27 +0800124#ifdef CONFIG_F2FS_FS_COMPRESSION
125 if (!need_alloc) {
126 set_new_dnode(&dn, inode, NULL, NULL, 0);
127 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
128 f2fs_put_dnode(&dn);
129 }
130#endif
131 if (err) {
132 unlock_page(page);
133 goto out_sem;
Chao Yu39a86952018-09-27 18:33:18 +0800134 }
135
Chao Yubae0ee72018-12-25 17:43:42 +0800136 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +0800137
138 /* wait for GCed page writeback via META_MAPPING */
139 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
140
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900141 /*
142 * check to see if the page is mapped already (no holes)
143 */
144 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800145 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900146
147 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300148 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800149 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900150 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900151
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300152 offset = i_size_read(inode) & ~PAGE_MASK;
153 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900154 }
155 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700156 if (!PageUptodate(page))
157 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900158
Chao Yub0af6d42017-08-02 23:21:48 +0800159 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530160 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800161
Jaegeuk Kime943a102013-10-25 14:26:31 +0900162 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800163out_sem:
164 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800165
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900166 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200167err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900168 return block_page_mkwrite_return(err);
169}
170
171static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800172 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700173 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900174 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900175};
176
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900177static int get_parent_ino(struct inode *inode, nid_t *pino)
178{
179 struct dentry *dentry;
180
Eric Biggers84c9c2d2020-05-05 11:41:11 -0700181 /*
182 * Make sure to get the non-deleted alias. The alias associated with
183 * the open file descriptor being fsync()'ed may be deleted already.
184 */
185 dentry = d_find_alias(inode);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900186 if (!dentry)
187 return 0;
188
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900189 *pino = parent_ino(dentry);
190 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900191 return 1;
192}
193
Chao Yua5fd5052017-11-06 22:51:45 +0800194static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800195{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700196 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800197 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800198
Chao Yua5fd5052017-11-06 22:51:45 +0800199 if (!S_ISREG(inode->i_mode))
200 cp_reason = CP_NON_REGULAR;
Chao Yu4c8ff702019-11-01 18:07:14 +0800201 else if (f2fs_compressed_file(inode))
202 cp_reason = CP_COMPRESSED;
Chao Yua5fd5052017-11-06 22:51:45 +0800203 else if (inode->i_nlink != 1)
204 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700205 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800206 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800207 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800208 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800209 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800210 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800211 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800212 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700213 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800214 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800215 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800216 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800217 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800218 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
219 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
220 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800221 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800222
Chao Yua5fd5052017-11-06 22:51:45 +0800223 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800224}
225
Changman Lee9c7bb702014-12-08 15:29:40 +0900226static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
227{
228 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
229 bool ret = false;
230 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800231 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900232 ret = true;
233 f2fs_put_page(i, 0);
234 return ret;
235}
236
Changman Lee51455b12014-12-08 15:29:41 +0900237static void try_to_fix_pino(struct inode *inode)
238{
239 struct f2fs_inode_info *fi = F2FS_I(inode);
240 nid_t pino;
241
242 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900243 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
244 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700245 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900246 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900247 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700248 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900249}
250
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700251static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
252 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900253{
254 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700255 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700256 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900257 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800258 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900259 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900260 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900261 .nr_to_write = LONG_MAX,
262 .for_reclaim = 0,
263 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800264 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900265
Daniel Rosenberg43549942018-08-20 19:21:43 -0700266 if (unlikely(f2fs_readonly(inode->i_sb) ||
267 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900268 return 0;
269
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900270 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700271
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800272 if (S_ISDIR(inode->i_mode))
273 goto go_write;
274
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700275 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800276 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700277 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400278 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700279 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700280
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900281 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800282 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900283 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900284 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900285
Changman Lee9c7bb702014-12-08 15:29:40 +0900286 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800287 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700288 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900289 goto go_write;
290 }
291
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700292 /*
293 * if there is no written data, don't waste time to write recovery info.
294 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700295 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800296 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700297
Changman Lee9c7bb702014-12-08 15:29:40 +0900298 /* it may call write_inode just prior to fsync */
299 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700300 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700301
Jaegeuk Kim91942322016-05-20 10:13:22 -0700302 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800303 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700304 goto flush_out;
305 goto out;
Chao Yu27879912021-07-20 09:03:29 +0800306 } else {
307 /*
308 * for OPU case, during fsync(), node can be persisted before
309 * data when lower device doesn't support write barrier, result
310 * in data corruption after SPO.
311 * So for strict fsync mode, force to use atomic write sematics
312 * to keep write order in between data/node and last node to
313 * avoid potential data corruption.
314 */
315 if (F2FS_OPTION(sbi).fsync_mode ==
316 FSYNC_MODE_STRICT && !atomic)
317 atomic = true;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700318 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700319go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900320 /*
321 * Both of fdatasync() and fsync() are able to be recovered from
322 * sudden-power-off.
323 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700324 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800325 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700326 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900327
Chao Yua5fd5052017-11-06 22:51:45 +0800328 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900329 /* all the dirty node pages should be flushed for POR */
330 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900331
Changman Lee51455b12014-12-08 15:29:41 +0900332 /*
333 * We've secured consistency through sync_fs. Following pino
334 * will be used only for fsynced inodes after checkpoint.
335 */
336 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700337 clear_inode_flag(inode, FI_APPEND_WRITE);
338 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900339 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900340 }
Changman Lee51455b12014-12-08 15:29:41 +0900341sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800342 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800343 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800344 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700345 if (ret)
346 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900347
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800348 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800349 if (unlikely(f2fs_cp_error(sbi))) {
350 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800351 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800352 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800353
Chao Yu4d57b862018-05-30 00:20:41 +0800354 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700355 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900356 f2fs_write_inode(inode, NULL);
357 goto sync_nodes;
358 }
359
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700360 /*
361 * If it's atomic_write, it's just fine to keep write ordering. So
362 * here we don't need to wait for node write completion, since we use
363 * node chain which serializes node blocks. If one of node writes are
364 * reordered, we can see simply broken chain, resulting in stopping
365 * roll-forward recovery. It means we'll recover all or none node blocks
366 * given fsync mark.
367 */
368 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800369 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700370 if (ret)
371 goto out;
372 }
Changman Lee51455b12014-12-08 15:29:41 +0900373
374 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800375 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700376 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900377flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700378 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800379 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800380 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800381 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800382 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800383 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800384 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800385 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900386out:
Chao Yua5fd5052017-11-06 22:51:45 +0800387 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900388 return ret;
389}
390
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700391int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
392{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200393 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
394 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700395 return f2fs_do_sync_file(file, start, end, datasync, false);
396}
397
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100398static bool __found_offset(struct address_space *mapping, block_t blkaddr,
399 pgoff_t index, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900400{
401 switch (whence) {
402 case SEEK_DATA:
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100403 if (__is_valid_data_blkaddr(blkaddr))
404 return true;
405 if (blkaddr == NEW_ADDR &&
406 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900407 return true;
408 break;
409 case SEEK_HOLE:
410 if (blkaddr == NULL_ADDR)
411 return true;
412 break;
413 }
414 return false;
415}
416
Chao Yu267378d2014-04-23 14:10:24 +0800417static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
418{
419 struct inode *inode = file->f_mapping->host;
420 loff_t maxbytes = inode->i_sb->s_maxbytes;
421 struct dnode_of_data dn;
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100422 pgoff_t pgofs, end_offset;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900423 loff_t data_ofs = offset;
424 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800425 int err = 0;
426
Al Viro59551022016-01-22 15:40:57 -0500427 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800428
429 isize = i_size_read(inode);
430 if (offset >= isize)
431 goto fail;
432
433 /* handle inline data case */
Chao Yu7a6e59d2020-11-02 17:36:58 +0800434 if (f2fs_has_inline_data(inode)) {
435 if (whence == SEEK_HOLE) {
436 data_ofs = isize;
437 goto found;
438 } else if (whence == SEEK_DATA) {
439 data_ofs = offset;
440 goto found;
441 }
Chao Yu267378d2014-04-23 14:10:24 +0800442 }
443
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300444 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800445
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300446 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800447 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800448 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800449 if (err && err != -ENOENT) {
450 goto fail;
451 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900452 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800453 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800454 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800455 continue;
456 } else {
457 goto found;
458 }
459 }
460
Chao Yu81ca7352016-01-26 15:39:35 +0800461 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800462
463 /* find data/hole in dnode block */
464 for (; dn.ofs_in_node < end_offset;
465 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300466 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800467 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900468
Chao Yua2ced1c2020-02-14 17:44:10 +0800469 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu267378d2014-04-23 14:10:24 +0800470
Chao Yuc9b60782018-08-01 19:13:44 +0800471 if (__is_valid_data_blkaddr(blkaddr) &&
472 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800473 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800474 f2fs_put_dnode(&dn);
475 goto fail;
476 }
477
Matthew Wilcox (Oracle)4cb03fe2020-08-24 22:48:41 +0100478 if (__found_offset(file->f_mapping, blkaddr,
Chao Yue1da7872018-06-05 17:44:11 +0800479 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800480 f2fs_put_dnode(&dn);
481 goto found;
482 }
483 }
484 f2fs_put_dnode(&dn);
485 }
486
487 if (whence == SEEK_DATA)
488 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800489found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900490 if (whence == SEEK_HOLE && data_ofs > isize)
491 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500492 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800493 return vfs_setpos(file, data_ofs, maxbytes);
494fail:
Al Viro59551022016-01-22 15:40:57 -0500495 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800496 return -ENXIO;
497}
498
499static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
500{
501 struct inode *inode = file->f_mapping->host;
502 loff_t maxbytes = inode->i_sb->s_maxbytes;
503
Chengguang Xu6d1451b2021-01-13 13:21:54 +0800504 if (f2fs_compressed_file(inode))
505 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
506
Chao Yu267378d2014-04-23 14:10:24 +0800507 switch (whence) {
508 case SEEK_SET:
509 case SEEK_CUR:
510 case SEEK_END:
511 return generic_file_llseek_size(file, offset, whence,
512 maxbytes, i_size_read(inode));
513 case SEEK_DATA:
514 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700515 if (offset < 0)
516 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800517 return f2fs_seek_block(file, offset, whence);
518 }
519
520 return -EINVAL;
521}
522
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900523static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
524{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700525 struct inode *inode = file_inode(file);
526
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200527 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
528 return -EIO;
529
Chao Yu4c8ff702019-11-01 18:07:14 +0800530 if (!f2fs_is_compress_backend_ready(inode))
531 return -EOPNOTSUPP;
532
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900533 file_accessed(file);
534 vma->vm_ops = &f2fs_file_vm_ops;
Chao Yu4c8ff702019-11-01 18:07:14 +0800535 set_inode_flag(inode, FI_MMAP_FILE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900536 return 0;
537}
538
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700539static int f2fs_file_open(struct inode *inode, struct file *filp)
540{
Eric Biggers2e168c82017-11-29 12:35:28 -0800541 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700542
Eric Biggers2e168c82017-11-29 12:35:28 -0800543 if (err)
544 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900545
Chao Yu4c8ff702019-11-01 18:07:14 +0800546 if (!f2fs_is_compress_backend_ready(inode))
547 return -EOPNOTSUPP;
548
Eric Biggers95ae2512019-07-22 09:26:24 -0700549 err = fsverity_file_open(inode, filp);
550 if (err)
551 return err;
552
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900553 filp->f_mode |= FMODE_NOWAIT;
554
Chao Yu0abd6752017-07-09 00:13:07 +0800555 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700556}
557
Chao Yu4d57b862018-05-30 00:20:41 +0800558void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900559{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700560 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900561 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800562 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900563 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800564 int base = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800565 bool compressed_cluster = false;
566 int cluster_index = 0, valid_blocks = 0;
567 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
Daeho Jeongc2759eb2020-09-08 11:44:10 +0900568 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
Chao Yu7a2af762017-07-19 00:19:06 +0800569
570 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
571 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900572
Gu Zheng45590712013-07-15 17:57:38 +0800573 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800574 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900575
Chao Yu4c8ff702019-11-01 18:07:14 +0800576 /* Assumption: truncateion starts with cluster */
577 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900578 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900579
Chao Yu4c8ff702019-11-01 18:07:14 +0800580 if (f2fs_compressed_file(dn->inode) &&
581 !(cluster_index & (cluster_size - 1))) {
582 if (compressed_cluster)
583 f2fs_i_compr_blocks_update(dn->inode,
584 valid_blocks, false);
585 compressed_cluster = (blkaddr == COMPRESS_ADDR);
586 valid_blocks = 0;
587 }
588
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900589 if (blkaddr == NULL_ADDR)
590 continue;
591
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800592 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800593 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800594
Chao Yu4c8ff702019-11-01 18:07:14 +0800595 if (__is_valid_data_blkaddr(blkaddr)) {
596 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800597 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800598 continue;
599 if (compressed_cluster)
600 valid_blocks++;
601 }
Chao Yuc9b60782018-08-01 19:13:44 +0800602
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700603 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700604 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800605
606 f2fs_invalidate_blocks(sbi, blkaddr);
Chao Yuef8d5632020-03-06 15:36:09 +0800607
608 if (!released || blkaddr != COMPRESS_ADDR)
609 nr_free++;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900610 }
Chao Yu19b2c302015-08-26 20:34:48 +0800611
Chao Yu4c8ff702019-11-01 18:07:14 +0800612 if (compressed_cluster)
613 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
614
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900615 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800616 pgoff_t fofs;
617 /*
618 * once we invalidate valid blkaddr in range [ofs, ofs + count],
619 * we will invalidate all blkaddr in the whole range.
620 */
Chao Yu4d57b862018-05-30 00:20:41 +0800621 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800622 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800623 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900624 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900625 }
626 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900627
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800628 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900629 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
630 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900631}
632
Chao Yu4d57b862018-05-30 00:20:41 +0800633void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900634{
Chao Yud02a6e62019-03-25 21:08:19 +0800635 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900636}
637
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800638static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700639 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900640{
youngjun yoo193bea12018-05-30 04:21:14 +0900641 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300642 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700643 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900644 struct page *page;
645
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700646 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700647 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900648
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700649 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700650 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700651 if (page && PageUptodate(page))
652 goto truncate_out;
653 f2fs_put_page(page, 1);
654 return 0;
655 }
656
Chao Yu4d57b862018-05-30 00:20:41 +0800657 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900658 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800659 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700660truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800661 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300662 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700663
664 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530665 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700666 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800667 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700669 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900670}
671
Chao Yu3265d3d2020-03-18 16:22:59 +0800672int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900673{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700674 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900675 struct dnode_of_data dn;
676 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800677 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700678 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800679 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900680
Namjae Jeon51dd6242013-04-20 01:28:52 +0900681 trace_f2fs_truncate_blocks_enter(inode, from);
682
Chao Yudf033ca2018-03-20 23:08:29 +0800683 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900684
Chengguang Xu6d1451b2021-01-13 13:21:54 +0800685 if (free_from >= max_file_blocks(inode))
Chao Yu09210c92016-05-05 19:13:03 +0800686 goto free_partial;
687
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700688 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800689 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800690
Chao Yu4d57b862018-05-30 00:20:41 +0800691 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700692 if (IS_ERR(ipage)) {
693 err = PTR_ERR(ipage);
694 goto out;
695 }
696
697 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800698 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700699 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800700 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700701 goto out;
702 }
703
704 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800705 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900706 if (err) {
707 if (err == -ENOENT)
708 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700709 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700710 }
711
Chao Yu81ca7352016-01-26 15:39:35 +0800712 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900713
714 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700715 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900716
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900717 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800718 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719 free_from += count;
720 }
721
722 f2fs_put_dnode(&dn);
723free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800724 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800725out:
726 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800727 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800728free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700729 /* lastly zero out the first data page */
730 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800731 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900732
Namjae Jeon51dd6242013-04-20 01:28:52 +0900733 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900734 return err;
735}
736
Chao Yu4c8ff702019-11-01 18:07:14 +0800737int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
738{
739 u64 free_from = from;
Chao Yu3265d3d2020-03-18 16:22:59 +0800740 int err;
Chao Yu4c8ff702019-11-01 18:07:14 +0800741
Chao Yu3265d3d2020-03-18 16:22:59 +0800742#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800743 /*
744 * for compressed file, only support cluster size
745 * aligned truncation.
746 */
Chao Yu4fec3fc02020-04-08 19:55:17 +0800747 if (f2fs_compressed_file(inode))
748 free_from = round_up(from,
749 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
Chao Yu3265d3d2020-03-18 16:22:59 +0800750#endif
Chao Yu4c8ff702019-11-01 18:07:14 +0800751
Chao Yu3265d3d2020-03-18 16:22:59 +0800752 err = f2fs_do_truncate_blocks(inode, free_from, lock);
753 if (err)
754 return err;
755
756#ifdef CONFIG_F2FS_FS_COMPRESSION
Fengnan Chang4a4fc042021-08-09 10:21:04 +0800757 /*
758 * For compressed file, after release compress blocks, don't allow write
759 * direct, but we should allow write direct after truncate to zero.
760 */
761 if (f2fs_compressed_file(inode) && !free_from
762 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
763 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
764
Chao Yu17d76482020-08-10 18:38:45 +0800765 if (from != free_from) {
Chao Yu3265d3d2020-03-18 16:22:59 +0800766 err = f2fs_truncate_partial_cluster(inode, from, lock);
Chao Yu17d76482020-08-10 18:38:45 +0800767 if (err)
768 return err;
769 }
Chao Yu3265d3d2020-03-18 16:22:59 +0800770#endif
771
Chao Yu17d76482020-08-10 18:38:45 +0800772 return 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800773}
774
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700775int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900776{
Chao Yub0154892015-08-24 17:39:42 +0800777 int err;
778
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200779 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
780 return -EIO;
781
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900782 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
783 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800784 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900785
Namjae Jeon51dd6242013-04-20 01:28:52 +0900786 trace_f2fs_truncate(inode);
787
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800788 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800789 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800790 return -EIO;
791 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200792
Yi Chen25fb04d2021-01-28 17:02:56 +0800793 err = dquot_initialize(inode);
794 if (err)
795 return err;
796
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800797 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800798 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800799 err = f2fs_convert_inline_inode(inode);
800 if (err)
801 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800802 }
803
Chao Yuc42d28c2019-02-02 17:33:01 +0800804 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800805 if (err)
806 return err;
807
Deepa Dinamani078cd822016-09-14 07:48:04 -0700808 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700809 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800810 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900811}
812
Christian Brauner549c7292021-01-21 14:19:43 +0100813int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
814 struct kstat *stat, u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900815{
David Howellsa528d352017-01-31 16:46:22 +0000816 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800817 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800818 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800819 unsigned int flags;
820
Chao Yu1c1d35d2018-01-25 14:54:42 +0800821 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800822 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800823 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
824 stat->result_mask |= STATX_BTIME;
825 stat->btime.tv_sec = fi->i_crtime.tv_sec;
826 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
827 }
828
Eric Biggers36098552019-06-04 22:59:04 -0700829 flags = fi->i_flags;
Chao Yufd267252020-03-25 10:22:09 +0800830 if (flags & F2FS_COMPR_FL)
831 stat->attributes |= STATX_ATTR_COMPRESSED;
Chao Yu59c84402018-04-03 15:08:17 +0800832 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800833 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530834 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800835 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800836 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800837 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800838 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800839 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700840 if (IS_VERITY(inode))
841 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800842
Chao Yufd267252020-03-25 10:22:09 +0800843 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
844 STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800845 STATX_ATTR_ENCRYPTED |
846 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700847 STATX_ATTR_NODUMP |
848 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800849
Christian Brauner0d56a452021-01-21 14:19:30 +0100850 generic_fillattr(&init_user_ns, inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700851
852 /* we need to show initial sectors used for inline_data/dentries */
853 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
854 f2fs_has_inline_dentry(inode))
855 stat->blocks += (stat->size + 511) >> 9;
856
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900857 return 0;
858}
859
860#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christian Braunere65ce2a2021-01-21 14:19:27 +0100861static void __setattr_copy(struct user_namespace *mnt_userns,
862 struct inode *inode, const struct iattr *attr)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900863{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900864 unsigned int ia_valid = attr->ia_valid;
865
866 if (ia_valid & ATTR_UID)
867 inode->i_uid = attr->ia_uid;
868 if (ia_valid & ATTR_GID)
869 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200870 if (ia_valid & ATTR_ATIME)
871 inode->i_atime = attr->ia_atime;
872 if (ia_valid & ATTR_MTIME)
873 inode->i_mtime = attr->ia_mtime;
874 if (ia_valid & ATTR_CTIME)
875 inode->i_ctime = attr->ia_ctime;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900876 if (ia_valid & ATTR_MODE) {
877 umode_t mode = attr->ia_mode;
Christian Brauner2f221d62021-01-21 14:19:26 +0100878 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900879
Linus Torvalds7d6beb72021-02-23 13:39:45 -0800880 if (!in_group_p(kgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900881 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700882 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900883 }
884}
885#else
886#define __setattr_copy setattr_copy
887#endif
888
Christian Brauner549c7292021-01-21 14:19:43 +0100889int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
890 struct iattr *attr)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900891{
David Howells2b0143b2015-03-17 22:25:59 +0000892 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900893 int err;
894
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200895 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
896 return -EIO;
897
Chao Yue0fcd012020-12-26 18:07:01 +0800898 if (unlikely(IS_IMMUTABLE(inode)))
899 return -EPERM;
900
901 if (unlikely(IS_APPEND(inode) &&
902 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
903 ATTR_GID | ATTR_TIMES_SET))))
904 return -EPERM;
905
Chao Yu4c8ff702019-11-01 18:07:14 +0800906 if ((attr->ia_valid & ATTR_SIZE) &&
907 !f2fs_is_compress_backend_ready(inode))
908 return -EOPNOTSUPP;
909
Christian Brauner2f221d62021-01-21 14:19:26 +0100910 err = setattr_prepare(&init_user_ns, dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900911 if (err)
912 return err;
913
Eric Biggers20bb2472017-11-29 12:35:32 -0800914 err = fscrypt_prepare_setattr(dentry, attr);
915 if (err)
916 return err;
917
Eric Biggers95ae2512019-07-22 09:26:24 -0700918 err = fsverity_prepare_setattr(dentry, attr);
919 if (err)
920 return err;
921
Chao Yu0abd6752017-07-09 00:13:07 +0800922 if (is_quota_modification(inode, attr)) {
923 err = dquot_initialize(inode);
924 if (err)
925 return err;
926 }
927 if ((attr->ia_valid & ATTR_UID &&
928 !uid_eq(attr->ia_uid, inode->i_uid)) ||
929 (attr->ia_valid & ATTR_GID &&
930 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800931 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800932 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800933 if (err) {
934 set_sbi_flag(F2FS_I_SB(inode),
935 SBI_QUOTA_NEED_REPAIR);
936 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800937 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800938 }
939 /*
940 * update uid/gid under lock_op(), so that dquot and inode can
941 * be updated atomically.
942 */
943 if (attr->ia_valid & ATTR_UID)
944 inode->i_uid = attr->ia_uid;
945 if (attr->ia_valid & ATTR_GID)
946 inode->i_gid = attr->ia_gid;
947 f2fs_mark_inode_dirty_sync(inode, true);
948 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800949 }
950
Chao Yu09db6a22014-09-15 18:02:09 +0800951 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800952 loff_t old_size = i_size_read(inode);
953
954 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
955 /*
956 * should convert inline inode before i_size_write to
957 * keep smaller than inline_data size with inline flag.
958 */
959 err = f2fs_convert_inline_inode(inode);
960 if (err)
961 return err;
962 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800963
Chao Yua33c1502018-08-05 23:04:25 +0800964 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900965 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800966
967 truncate_setsize(inode, attr->ia_size);
968
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800969 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800970 err = f2fs_truncate(inode);
971 /*
972 * do not trim all blocks after i_size if target size is
973 * larger than i_size.
974 */
Chao Yua33c1502018-08-05 23:04:25 +0800975 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900976 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800977 if (err)
978 return err;
979
Chao Yuc10c9822020-02-27 19:30:03 +0800980 spin_lock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800981 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800982 F2FS_I(inode)->last_disk_size = i_size_read(inode);
Chao Yuc10c9822020-02-27 19:30:03 +0800983 spin_unlock(&F2FS_I(inode)->i_size_lock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900984 }
985
Christian Brauner2f221d62021-01-21 14:19:26 +0100986 __setattr_copy(&init_user_ns, inode, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900987
988 if (attr->ia_valid & ATTR_MODE) {
Linus Torvalds7d6beb72021-02-23 13:39:45 -0800989 err = posix_acl_chmod(&init_user_ns, inode, f2fs_get_inode_mode(inode));
Chao Yu17232e832020-12-25 16:52:27 +0800990
991 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
992 if (!err)
993 inode->i_mode = F2FS_I(inode)->i_acl_mode;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700994 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900995 }
996 }
997
Yunlei Hec0ed4402016-12-11 15:35:15 +0800998 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800999 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -07001000
1001 /* inode change will produce dirty node pages flushed by checkpoint */
1002 f2fs_balance_fs(F2FS_I_SB(inode), true);
1003
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001004 return err;
1005}
1006
1007const struct inode_operations f2fs_file_inode_operations = {
1008 .getattr = f2fs_getattr,
1009 .setattr = f2fs_setattr,
1010 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08001011 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001012 .listxattr = f2fs_listxattr,
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001013 .fiemap = f2fs_fiemap,
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02001014 .fileattr_get = f2fs_fileattr_get,
1015 .fileattr_set = f2fs_fileattr_set,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001016};
1017
Chao Yu63943282015-08-07 18:36:06 +08001018static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001019 loff_t start, loff_t len)
1020{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001021 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001022 struct page *page;
1023
1024 if (!len)
Chao Yu63943282015-08-07 18:36:06 +08001025 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001026
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001027 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +09001028
Gu Zhenge4795562013-09-27 18:08:30 +08001029 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001030 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +08001031 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001032
Chao Yu63943282015-08-07 18:36:06 +08001033 if (IS_ERR(page))
1034 return PTR_ERR(page);
1035
Chao Yubae0ee72018-12-25 17:43:42 +08001036 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +08001037 zero_user(page, start, len);
1038 set_page_dirty(page);
1039 f2fs_put_page(page, 1);
1040 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001041}
1042
Chao Yu4d57b862018-05-30 00:20:41 +08001043int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001044{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001045 int err;
1046
Chao Yuea587112015-09-17 20:22:44 +08001047 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001048 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001049 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001050
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001051 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001052 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001053 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001054 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001055 pg_start = f2fs_get_next_page_offset(&dn,
1056 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001057 continue;
Chao Yuea587112015-09-17 20:22:44 +08001058 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001059 return err;
1060 }
1061
Chao Yu81ca7352016-01-26 15:39:35 +08001062 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001063 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1064
1065 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1066
Chao Yu4d57b862018-05-30 00:20:41 +08001067 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001068 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001069
1070 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001071 }
1072 return 0;
1073}
1074
Chao Yua66c7b22013-11-22 16:52:50 +08001075static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001076{
1077 pgoff_t pg_start, pg_end;
1078 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001079 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001080
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001081 ret = f2fs_convert_inline_inode(inode);
1082 if (ret)
1083 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001084
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001085 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1086 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001087
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001088 off_start = offset & (PAGE_SIZE - 1);
1089 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001090
1091 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001092 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001093 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001094 if (ret)
1095 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001096 } else {
Chao Yu63943282015-08-07 18:36:06 +08001097 if (off_start) {
1098 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001099 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001100 if (ret)
1101 return ret;
1102 }
1103 if (off_end) {
1104 ret = fill_zero(inode, pg_end, 0, off_end);
1105 if (ret)
1106 return ret;
1107 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001108
1109 if (pg_start < pg_end) {
1110 struct address_space *mapping = inode->i_mapping;
1111 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001112 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001113
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001114 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001115
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001116 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1117 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001118
Chao Yua33c1502018-08-05 23:04:25 +08001119 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001120 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001121
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001122 truncate_inode_pages_range(mapping, blk_start,
1123 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001124
Gu Zhenge4795562013-09-27 18:08:30 +08001125 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001126 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001127 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001128
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001129 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001130 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001131 }
1132 }
1133
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001134 return ret;
1135}
1136
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001137static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1138 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001139{
1140 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1141 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001142 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001143
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001144next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001145 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001146 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001147 if (ret && ret != -ENOENT) {
1148 return ret;
1149 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001150 if (dn.max_level == 0)
1151 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001152 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1153 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001154 blkaddr += done;
1155 do_replace += done;
1156 goto next;
1157 }
1158
1159 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1160 dn.ofs_in_node, len);
1161 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001162 *blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu93770ab2019-04-15 15:26:32 +08001163
1164 if (__is_valid_data_blkaddr(*blkaddr) &&
1165 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1166 DATA_GENERIC_ENHANCE)) {
1167 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001168 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001169 }
1170
Chao Yu4d57b862018-05-30 00:20:41 +08001171 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001172
Chao Yub0332a02020-02-14 17:44:12 +08001173 if (f2fs_lfs_mode(sbi)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001174 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001175 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001176 }
1177
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001178 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001179 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001180 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001181 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001182 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001183 f2fs_put_dnode(&dn);
1184next:
1185 len -= done;
1186 off += done;
1187 if (len)
1188 goto next_dnode;
1189 return 0;
1190}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001191
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001192static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1193 int *do_replace, pgoff_t off, int len)
1194{
1195 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1196 struct dnode_of_data dn;
1197 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001198
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001199 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1200 if (*do_replace == 0)
1201 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001202
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001203 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001204 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001205 if (ret) {
1206 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001207 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001208 } else {
1209 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001210 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001211 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001212 }
1213 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001214}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001215
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001216static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1217 block_t *blkaddr, int *do_replace,
1218 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1219{
1220 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1221 pgoff_t i = 0;
1222 int ret;
1223
1224 while (i < len) {
1225 if (blkaddr[i] == NULL_ADDR && !full) {
1226 i++;
1227 continue;
1228 }
1229
1230 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1231 struct dnode_of_data dn;
1232 struct node_info ni;
1233 size_t new_size;
1234 pgoff_t ilen;
1235
1236 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001237 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001238 if (ret)
1239 return ret;
1240
Chao Yu77357302018-07-17 00:02:17 +08001241 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1242 if (ret) {
1243 f2fs_put_dnode(&dn);
1244 return ret;
1245 }
1246
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001247 ilen = min((pgoff_t)
1248 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1249 dn.ofs_in_node, len - i);
1250 do {
Chao Yua2ced1c2020-02-14 17:44:10 +08001251 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu4d57b862018-05-30 00:20:41 +08001252 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001253
1254 if (do_replace[i]) {
1255 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001256 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001257 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001258 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001259 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1260 blkaddr[i], ni.version, true, false);
1261
1262 do_replace[i] = 0;
1263 }
1264 dn.ofs_in_node++;
1265 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001266 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001267 if (dst_inode->i_size < new_size)
1268 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001269 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001270
1271 f2fs_put_dnode(&dn);
1272 } else {
1273 struct page *psrc, *pdst;
1274
Chao Yu4d57b862018-05-30 00:20:41 +08001275 psrc = f2fs_get_lock_data_page(src_inode,
1276 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001277 if (IS_ERR(psrc))
1278 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001279 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001280 true);
1281 if (IS_ERR(pdst)) {
1282 f2fs_put_page(psrc, 1);
1283 return PTR_ERR(pdst);
1284 }
1285 f2fs_copy_page(psrc, pdst);
1286 set_page_dirty(pdst);
1287 f2fs_put_page(pdst, 1);
1288 f2fs_put_page(psrc, 1);
1289
Chao Yu4d57b862018-05-30 00:20:41 +08001290 ret = f2fs_truncate_hole(src_inode,
1291 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001292 if (ret)
1293 return ret;
1294 i++;
1295 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001296 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001297 return 0;
1298}
1299
1300static int __exchange_data_block(struct inode *src_inode,
1301 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001302 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001303{
1304 block_t *src_blkaddr;
1305 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001306 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001307 int ret;
1308
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001309 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001310 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001311
Chao Yu628b3d12017-11-30 19:28:18 +08001312 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001313 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001314 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001315 if (!src_blkaddr)
1316 return -ENOMEM;
1317
Chao Yu628b3d12017-11-30 19:28:18 +08001318 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001319 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001320 GFP_NOFS);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001321 if (!do_replace) {
1322 kvfree(src_blkaddr);
1323 return -ENOMEM;
1324 }
1325
1326 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1327 do_replace, src, olen);
1328 if (ret)
1329 goto roll_back;
1330
1331 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1332 do_replace, src, dst, olen, full);
1333 if (ret)
1334 goto roll_back;
1335
1336 src += olen;
1337 dst += olen;
1338 len -= olen;
1339
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001340 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001341 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001342 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001343 return 0;
1344
1345roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001346 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001347 kvfree(src_blkaddr);
1348 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001349 return ret;
1350}
1351
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001352static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001353{
1354 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001355 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001356 pgoff_t start = offset >> PAGE_SHIFT;
1357 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001358 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001359
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001360 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001361
1362 /* avoid gc operation during block exchange */
1363 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1364 down_write(&F2FS_I(inode)->i_mmap_sem);
1365
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001366 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001367 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001368 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001369 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1370 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001371
1372 up_write(&F2FS_I(inode)->i_mmap_sem);
1373 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001374 return ret;
1375}
1376
1377static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1378{
Chao Yub4ace332015-05-06 13:09:46 +08001379 loff_t new_size;
1380 int ret;
1381
Chao Yub4ace332015-05-06 13:09:46 +08001382 if (offset + len >= i_size_read(inode))
1383 return -EINVAL;
1384
1385 /* collapse range should be aligned to block size of f2fs. */
1386 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1387 return -EINVAL;
1388
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001389 ret = f2fs_convert_inline_inode(inode);
1390 if (ret)
1391 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001392
Chao Yub4ace332015-05-06 13:09:46 +08001393 /* write out all dirty pages from offset */
1394 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1395 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001396 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001397
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001398 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001399 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001400 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001401
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001402 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001403 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001404 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1405 truncate_pagecache(inode, offset);
1406
Chao Yub4ace332015-05-06 13:09:46 +08001407 new_size = i_size_read(inode) - len;
Chao Yuc42d28c2019-02-02 17:33:01 +08001408 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001409 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001410 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001411 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001412 return ret;
1413}
1414
Chao Yu6e961942016-05-09 19:56:31 +08001415static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1416 pgoff_t end)
1417{
1418 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1419 pgoff_t index = start;
1420 unsigned int ofs_in_node = dn->ofs_in_node;
1421 blkcnt_t count = 0;
1422 int ret;
1423
1424 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001425 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001426 count++;
1427 }
1428
1429 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001430 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001431 if (ret)
1432 return ret;
1433
1434 dn->ofs_in_node = ofs_in_node;
1435 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001436 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001437 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001438 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001439 * allocation.
1440 */
1441 if (dn->data_blkaddr == NULL_ADDR) {
1442 ret = -ENOSPC;
1443 break;
1444 }
1445 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001446 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001447 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001448 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001449 }
1450 }
1451
1452 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1453
1454 return ret;
1455}
1456
Chao Yu75cd4e02015-05-06 13:11:13 +08001457static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1458 int mode)
1459{
1460 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1461 struct address_space *mapping = inode->i_mapping;
1462 pgoff_t index, pg_start, pg_end;
1463 loff_t new_size = i_size_read(inode);
1464 loff_t off_start, off_end;
1465 int ret = 0;
1466
Chao Yu75cd4e02015-05-06 13:11:13 +08001467 ret = inode_newsize_ok(inode, (len + offset));
1468 if (ret)
1469 return ret;
1470
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001471 ret = f2fs_convert_inline_inode(inode);
1472 if (ret)
1473 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001474
1475 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1476 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001477 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001478
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001479 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1480 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001481
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001482 off_start = offset & (PAGE_SIZE - 1);
1483 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001484
1485 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001486 ret = fill_zero(inode, pg_start, off_start,
1487 off_end - off_start);
1488 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001489 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001490
Chao Yu75cd4e02015-05-06 13:11:13 +08001491 new_size = max_t(loff_t, new_size, offset + len);
1492 } else {
1493 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001494 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001495 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001496 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001497 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001498
Chao Yu75cd4e02015-05-06 13:11:13 +08001499 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001500 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001501 }
1502
Chao Yu6e961942016-05-09 19:56:31 +08001503 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001504 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001505 unsigned int end_offset;
1506 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001507
Chao Yuc70798532018-08-05 23:02:22 +08001508 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001509 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001510
1511 truncate_pagecache_range(inode,
1512 (loff_t)index << PAGE_SHIFT,
1513 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1514
Chao Yu75cd4e02015-05-06 13:11:13 +08001515 f2fs_lock_op(sbi);
1516
Chao Yu6e961942016-05-09 19:56:31 +08001517 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001518 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001519 if (ret) {
1520 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001521 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001522 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001523 goto out;
1524 }
1525
Chao Yu6e961942016-05-09 19:56:31 +08001526 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1527 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1528
1529 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001530 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001531
Chao Yu75cd4e02015-05-06 13:11:13 +08001532 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001533 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001534 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001535
1536 f2fs_balance_fs(sbi, dn.node_changed);
1537
Chao Yu6e961942016-05-09 19:56:31 +08001538 if (ret)
1539 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001540
Chao Yu6e961942016-05-09 19:56:31 +08001541 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001542 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001543 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001544 }
1545
1546 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001547 ret = fill_zero(inode, pg_end, 0, off_end);
1548 if (ret)
1549 goto out;
1550
Chao Yu75cd4e02015-05-06 13:11:13 +08001551 new_size = max_t(loff_t, new_size, offset + len);
1552 }
1553 }
1554
1555out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001556 if (new_size > i_size_read(inode)) {
1557 if (mode & FALLOC_FL_KEEP_SIZE)
1558 file_set_keep_isize(inode);
1559 else
1560 f2fs_i_size_write(inode, new_size);
1561 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001562 return ret;
1563}
1564
Chao Yuf62185d2015-05-28 19:16:57 +08001565static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1566{
1567 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001568 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001569 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001570 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001571
Chao Yuf62185d2015-05-28 19:16:57 +08001572 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001573 ret = inode_newsize_ok(inode, new_size);
1574 if (ret)
1575 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001576
1577 if (offset >= i_size_read(inode))
1578 return -EINVAL;
1579
1580 /* insert range should be aligned to block size of f2fs. */
1581 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1582 return -EINVAL;
1583
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001584 ret = f2fs_convert_inline_inode(inode);
1585 if (ret)
1586 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001587
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001588 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001589
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001590 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001591 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001592 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001593 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001594 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001595
1596 /* write out all dirty pages from offset */
1597 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1598 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001599 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001600
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001601 pg_start = offset >> PAGE_SHIFT;
1602 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001603 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001604 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001605
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001606 /* avoid gc operation during block exchange */
1607 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1608 down_write(&F2FS_I(inode)->i_mmap_sem);
1609 truncate_pagecache(inode, offset);
1610
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001611 while (!ret && idx > pg_start) {
1612 nr = idx - pg_start;
1613 if (nr > delta)
1614 nr = delta;
1615 idx -= nr;
1616
Chao Yuf62185d2015-05-28 19:16:57 +08001617 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001618 f2fs_drop_extent_tree(inode);
1619
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001620 ret = __exchange_data_block(inode, inode, idx,
1621 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001622 f2fs_unlock_op(sbi);
1623 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001624 up_write(&F2FS_I(inode)->i_mmap_sem);
1625 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001626
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001627 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001628 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001629 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1630 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001631 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001632
1633 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001634 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001635 return ret;
1636}
1637
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001638static int expand_inode_data(struct inode *inode, loff_t offset,
1639 loff_t len, int mode)
1640{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001641 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001642 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001643 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1644 .m_may_create = true };
Chao Yu88f2cfc2021-03-24 11:24:33 +08001645 pgoff_t pg_start, pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001646 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001647 loff_t off_end;
Chao Yu88f2cfc2021-03-24 11:24:33 +08001648 block_t expanded = 0;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001649 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001650
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001651 err = inode_newsize_ok(inode, (len + offset));
1652 if (err)
1653 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001654
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001655 err = f2fs_convert_inline_inode(inode);
1656 if (err)
1657 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001658
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001659 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001660
Chao Yu88f2cfc2021-03-24 11:24:33 +08001661 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001662 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001663 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001664
Chao Yu88f2cfc2021-03-24 11:24:33 +08001665 map.m_lblk = pg_start;
1666 map.m_len = pg_end - pg_start;
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001667 if (off_end)
1668 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001669
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001670 if (!map.m_len)
1671 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001672
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001673 if (f2fs_is_pinned_file(inode)) {
Chao Yue1175f02021-03-05 17:56:01 +08001674 block_t sec_blks = BLKS_PER_SEC(sbi);
1675 block_t sec_len = roundup(map.m_len, sec_blks);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001676
Chao Yue1175f02021-03-05 17:56:01 +08001677 map.m_len = sec_blks;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001678next_alloc:
1679 if (has_not_enough_free_secs(sbi, 0,
1680 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
Chao Yufb24fea2020-01-14 19:36:50 +08001681 down_write(&sbi->gc_lock);
Chao Yu7dede8862021-02-20 17:35:40 +08001682 err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001683 if (err && err != -ENODATA && err != -EAGAIN)
1684 goto out_err;
1685 }
1686
1687 down_write(&sbi->pin_sem);
Daeho Jeongfd612642020-05-27 13:02:31 +09001688
1689 f2fs_lock_op(sbi);
Chao Yu509f1012021-04-21 09:54:55 +08001690 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
Daeho Jeongfd612642020-05-27 13:02:31 +09001691 f2fs_unlock_op(sbi);
1692
Chao Yud0b9e422020-08-04 21:14:45 +08001693 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001694 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Chao Yud0b9e422020-08-04 21:14:45 +08001695
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001696 up_write(&sbi->pin_sem);
1697
Chao Yu88f2cfc2021-03-24 11:24:33 +08001698 expanded += map.m_len;
Chao Yue1175f02021-03-05 17:56:01 +08001699 sec_len -= map.m_len;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001700 map.m_lblk += map.m_len;
Chao Yue1175f02021-03-05 17:56:01 +08001701 if (!err && sec_len)
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001702 goto next_alloc;
1703
Chao Yu88f2cfc2021-03-24 11:24:33 +08001704 map.m_len = expanded;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001705 } else {
1706 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Chao Yu88f2cfc2021-03-24 11:24:33 +08001707 expanded = map.m_len;
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001708 }
1709out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001710 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001711 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001712
Chao Yu88f2cfc2021-03-24 11:24:33 +08001713 if (!expanded)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001714 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001715
Chao Yu88f2cfc2021-03-24 11:24:33 +08001716 last_off = pg_start + expanded - 1;
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001717
1718 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001719 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001720 (loff_t)(last_off + 1) << PAGE_SHIFT;
1721 } else {
1722 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001723 }
1724
Chao Yue8ed90a2017-11-05 21:53:30 +08001725 if (new_size > i_size_read(inode)) {
1726 if (mode & FALLOC_FL_KEEP_SIZE)
1727 file_set_keep_isize(inode);
1728 else
1729 f2fs_i_size_write(inode, new_size);
1730 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001731
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001732 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001733}
1734
1735static long f2fs_fallocate(struct file *file, int mode,
1736 loff_t offset, loff_t len)
1737{
Al Viro6131ffa2013-02-27 16:59:05 -05001738 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001739 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001740
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001741 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1742 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001743 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1744 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001745 if (!f2fs_is_compress_backend_ready(inode))
1746 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001747
Chao Yuc9980122015-09-11 14:39:02 +08001748 /* f2fs only support ->fallocate for regular file */
1749 if (!S_ISREG(inode->i_mode))
1750 return -EINVAL;
1751
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301752 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001753 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001754 return -EOPNOTSUPP;
1755
Chao Yu4c8ff702019-11-01 18:07:14 +08001756 if (f2fs_compressed_file(inode) &&
1757 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1758 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1759 return -EOPNOTSUPP;
1760
Chao Yub4ace332015-05-06 13:09:46 +08001761 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001762 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1763 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001764 return -EOPNOTSUPP;
1765
Al Viro59551022016-01-22 15:40:57 -05001766 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001767
Taehee Yoo587c0a42015-04-21 15:59:12 +09001768 if (mode & FALLOC_FL_PUNCH_HOLE) {
1769 if (offset >= inode->i_size)
1770 goto out;
1771
Chao Yua66c7b22013-11-22 16:52:50 +08001772 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001773 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1774 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001775 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1776 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001777 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1778 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001779 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001780 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001781 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001782
Namjae Jeon3af60a42012-12-30 14:52:37 +09001783 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001784 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001785 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001786 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001787 }
Chao Yu3375f692014-01-28 10:29:26 +08001788
Taehee Yoo587c0a42015-04-21 15:59:12 +09001789out:
Al Viro59551022016-01-22 15:40:57 -05001790 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001791
Namjae Jeonc01e2852013-04-23 17:00:52 +09001792 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001793 return ret;
1794}
1795
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001796static int f2fs_release_file(struct inode *inode, struct file *filp)
1797{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001798 /*
1799 * f2fs_relase_file is called at every close calls. So we should
1800 * not drop any inmemory pages by close called by other process.
1801 */
1802 if (!(filp->f_mode & FMODE_WRITE) ||
1803 atomic_read(&inode->i_writecount) != 1)
1804 return 0;
1805
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001806 /* some remained atomic pages should discarded */
1807 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001808 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001809 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001810 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001811 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001812 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001813 clear_inode_flag(inode, FI_VOLATILE_FILE);
1814 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001815 }
1816 return 0;
1817}
1818
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001819static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001820{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001821 struct inode *inode = file_inode(file);
1822
1823 /*
1824 * If the process doing a transaction is crashed, we should do
1825 * roll-back. Otherwise, other reader/write can see corrupted database
1826 * until all the writers close its file. Since this should be done
1827 * before dropping file lock, it needs to do in ->flush.
1828 */
1829 if (f2fs_is_atomic_file(inode) &&
1830 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001831 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001832 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001833}
1834
Eric Biggers36098552019-06-04 22:59:04 -07001835static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001836{
1837 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001838 u32 masked_flags = fi->i_flags & mask;
1839
Jaegeuk Kima7531032021-05-06 12:11:14 -07001840 /* mask can be shrunk by flags_valid selector */
1841 iflags &= mask;
Chao Yu2c1d0302017-07-29 00:32:52 +08001842
1843 /* Is it quota file? Do not allow user to mess with it */
1844 if (IS_NOQUOTA(inode))
1845 return -EPERM;
1846
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001847 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001848 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1849 return -EOPNOTSUPP;
1850 if (!f2fs_empty_dir(inode))
1851 return -ENOTEMPTY;
1852 }
1853
Chao Yu4c8ff702019-11-01 18:07:14 +08001854 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1855 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1856 return -EOPNOTSUPP;
1857 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1858 return -EINVAL;
1859 }
1860
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001861 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
Chao Yuaa576972020-03-27 18:29:51 +08001862 if (masked_flags & F2FS_COMPR_FL) {
Daeho Jeong78134d02020-09-08 11:44:11 +09001863 if (!f2fs_disable_compressed_file(inode))
Chao Yu2536ac62020-03-10 20:50:09 +08001864 return -EINVAL;
1865 }
Chao Yu4c8ff702019-11-01 18:07:14 +08001866 if (iflags & F2FS_NOCOMP_FL)
1867 return -EINVAL;
1868 if (iflags & F2FS_COMPR_FL) {
Chao Yu4c8ff702019-11-01 18:07:14 +08001869 if (!f2fs_may_compress(inode))
1870 return -EINVAL;
Chao Yu519a5a22020-09-18 11:03:49 +08001871 if (S_ISREG(inode->i_mode) && inode->i_size)
1872 return -EINVAL;
Chao Yu4c8ff702019-11-01 18:07:14 +08001873
1874 set_compress_context(inode);
1875 }
1876 }
Jaegeuk Kim99eabb92020-03-05 15:20:26 -08001877 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1878 if (masked_flags & F2FS_COMPR_FL)
Chao Yu4c8ff702019-11-01 18:07:14 +08001879 return -EINVAL;
1880 }
1881
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001882 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001883 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1884 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001885
Chao Yu59c84402018-04-03 15:08:17 +08001886 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001887 set_inode_flag(inode, FI_PROJ_INHERIT);
1888 else
1889 clear_inode_flag(inode, FI_PROJ_INHERIT);
1890
1891 inode->i_ctime = current_time(inode);
1892 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001893 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001894 return 0;
1895}
1896
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02001897/* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
Eric Biggers36098552019-06-04 22:59:04 -07001898
1899/*
1900 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1901 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1902 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1903 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02001904 *
1905 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1906 * FS_IOC_FSSETXATTR is done by the VFS.
Eric Biggers36098552019-06-04 22:59:04 -07001907 */
1908
1909static const struct {
1910 u32 iflag;
1911 u32 fsflag;
1912} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001913 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001914 { F2FS_SYNC_FL, FS_SYNC_FL },
1915 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1916 { F2FS_APPEND_FL, FS_APPEND_FL },
1917 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1918 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001919 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001920 { F2FS_INDEX_FL, FS_INDEX_FL },
1921 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1922 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001923 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001924};
1925
1926#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001927 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001928 FS_SYNC_FL | \
1929 FS_IMMUTABLE_FL | \
1930 FS_APPEND_FL | \
1931 FS_NODUMP_FL | \
1932 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001933 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001934 FS_INDEX_FL | \
1935 FS_DIRSYNC_FL | \
1936 FS_PROJINHERIT_FL | \
1937 FS_ENCRYPT_FL | \
1938 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001939 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001940 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001941 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001942
1943#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001944 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001945 FS_SYNC_FL | \
1946 FS_IMMUTABLE_FL | \
1947 FS_APPEND_FL | \
1948 FS_NODUMP_FL | \
1949 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001950 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001951 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001952 FS_PROJINHERIT_FL | \
1953 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001954
1955/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1956static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1957{
1958 u32 fsflags = 0;
1959 int i;
1960
1961 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1962 if (iflags & f2fs_fsflags_map[i].iflag)
1963 fsflags |= f2fs_fsflags_map[i].fsflag;
1964
1965 return fsflags;
1966}
1967
1968/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1969static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1970{
1971 u32 iflags = 0;
1972 int i;
1973
1974 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1975 if (fsflags & f2fs_fsflags_map[i].fsflag)
1976 iflags |= f2fs_fsflags_map[i].iflag;
1977
1978 return iflags;
1979}
1980
Chao Yud49f3e82015-01-23 20:36:04 +08001981static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1982{
1983 struct inode *inode = file_inode(filp);
1984
1985 return put_user(inode->i_generation, (int __user *)arg);
1986}
1987
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001988static int f2fs_ioc_start_atomic_write(struct file *filp)
1989{
1990 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01001991 struct f2fs_inode_info *fi = F2FS_I(inode);
1992 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08001993 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001994
Christian Brauner21cb47b2021-01-21 14:19:25 +01001995 if (!inode_owner_or_capable(&init_user_ns, inode))
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001996 return -EACCES;
1997
Jaegeuk Kime8118982017-03-17 10:04:15 +08001998 if (!S_ISREG(inode->i_mode))
1999 return -EINVAL;
2000
Chao Yu038d0692019-07-25 22:39:11 +08002001 if (filp->f_flags & O_DIRECT)
2002 return -EINVAL;
2003
Chao Yu7fb17fe2016-05-09 19:56:32 +08002004 ret = mnt_want_write_file(filp);
2005 if (ret)
2006 return ret;
2007
Chao Yu0fac5582016-05-09 19:56:33 +08002008 inode_lock(inode);
2009
Chao Yu4c8ff702019-11-01 18:07:14 +08002010 f2fs_disable_compressed_file(inode);
2011
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002012 if (f2fs_is_atomic_file(inode)) {
2013 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2014 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002015 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002016 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002017
Chao Yuf4c9c742015-07-17 18:06:35 +08002018 ret = f2fs_convert_inline_inode(inode);
2019 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002020 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002021
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002022 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2023
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002024 /*
2025 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2026 * f2fs_is_atomic_file.
2027 */
2028 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002029 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2030 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002031 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002032 if (ret) {
2033 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002034 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002035 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002036
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002037 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2038 if (list_empty(&fi->inmem_ilist))
2039 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302040 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002041 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2042
2043 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002044 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002045 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002046 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002047
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002048 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002049 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002050 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002051out:
Chao Yu0fac5582016-05-09 19:56:33 +08002052 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002053 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002054 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002055}
2056
2057static int f2fs_ioc_commit_atomic_write(struct file *filp)
2058{
2059 struct inode *inode = file_inode(filp);
2060 int ret;
2061
Christian Brauner21cb47b2021-01-21 14:19:25 +01002062 if (!inode_owner_or_capable(&init_user_ns, inode))
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002063 return -EACCES;
2064
2065 ret = mnt_want_write_file(filp);
2066 if (ret)
2067 return ret;
2068
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002069 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002070
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002071 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002072
Chao Yub169c3c2018-04-18 17:45:02 +08002073 if (f2fs_is_volatile_file(inode)) {
2074 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002075 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002076 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002077
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002078 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002079 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002080 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002081 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002082
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002083 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002084 if (!ret)
2085 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002086 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002087 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002088 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002089err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002090 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2091 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2092 ret = -EINVAL;
2093 }
Chao Yu0fac5582016-05-09 19:56:33 +08002094 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002095 mnt_drop_write_file(filp);
2096 return ret;
2097}
2098
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002099static int f2fs_ioc_start_volatile_write(struct file *filp)
2100{
2101 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002102 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002103
Christian Brauner21cb47b2021-01-21 14:19:25 +01002104 if (!inode_owner_or_capable(&init_user_ns, inode))
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002105 return -EACCES;
2106
Chao Yu8ff09712017-03-17 15:43:57 +08002107 if (!S_ISREG(inode->i_mode))
2108 return -EINVAL;
2109
Chao Yu7fb17fe2016-05-09 19:56:32 +08002110 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002111 if (ret)
2112 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002113
Chao Yu0fac5582016-05-09 19:56:33 +08002114 inode_lock(inode);
2115
Chao Yu7fb17fe2016-05-09 19:56:32 +08002116 if (f2fs_is_volatile_file(inode))
2117 goto out;
2118
2119 ret = f2fs_convert_inline_inode(inode);
2120 if (ret)
2121 goto out;
2122
Chao Yu648d50b2017-03-22 17:23:45 +08002123 stat_inc_volatile_write(inode);
2124 stat_update_max_volatile_write(inode);
2125
Jaegeuk Kim91942322016-05-20 10:13:22 -07002126 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002127 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002128out:
Chao Yu0fac5582016-05-09 19:56:33 +08002129 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002130 mnt_drop_write_file(filp);
2131 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002132}
2133
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002134static int f2fs_ioc_release_volatile_write(struct file *filp)
2135{
2136 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002137 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002138
Christian Brauner21cb47b2021-01-21 14:19:25 +01002139 if (!inode_owner_or_capable(&init_user_ns, inode))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002140 return -EACCES;
2141
Chao Yu7fb17fe2016-05-09 19:56:32 +08002142 ret = mnt_want_write_file(filp);
2143 if (ret)
2144 return ret;
2145
Chao Yu0fac5582016-05-09 19:56:33 +08002146 inode_lock(inode);
2147
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002148 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002149 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002150
Chao Yu7fb17fe2016-05-09 19:56:32 +08002151 if (!f2fs_is_first_block_written(inode)) {
2152 ret = truncate_partial_data_page(inode, 0, true);
2153 goto out;
2154 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002155
Chao Yu7fb17fe2016-05-09 19:56:32 +08002156 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2157out:
Chao Yu0fac5582016-05-09 19:56:33 +08002158 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002159 mnt_drop_write_file(filp);
2160 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002161}
2162
2163static int f2fs_ioc_abort_volatile_write(struct file *filp)
2164{
2165 struct inode *inode = file_inode(filp);
2166 int ret;
2167
Christian Brauner21cb47b2021-01-21 14:19:25 +01002168 if (!inode_owner_or_capable(&init_user_ns, inode))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002169 return -EACCES;
2170
2171 ret = mnt_want_write_file(filp);
2172 if (ret)
2173 return ret;
2174
Chao Yu0fac5582016-05-09 19:56:33 +08002175 inode_lock(inode);
2176
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002177 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002178 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002179 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002180 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002181 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002182 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002183 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002184
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002185 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2186
Chao Yu0fac5582016-05-09 19:56:33 +08002187 inode_unlock(inode);
2188
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002189 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002190 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002191 return ret;
2192}
2193
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002194static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2195{
2196 struct inode *inode = file_inode(filp);
2197 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2198 struct super_block *sb = sbi->sb;
2199 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002200 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002201
2202 if (!capable(CAP_SYS_ADMIN))
2203 return -EPERM;
2204
2205 if (get_user(in, (__u32 __user *)arg))
2206 return -EFAULT;
2207
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302208 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2209 ret = mnt_want_write_file(filp);
Chao Yu86264412020-06-08 20:03:16 +08002210 if (ret) {
2211 if (ret == -EROFS) {
2212 ret = 0;
2213 f2fs_stop_checkpoint(sbi, false);
2214 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2215 trace_f2fs_shutdown(sbi, in, ret);
2216 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302217 return ret;
Chao Yu86264412020-06-08 20:03:16 +08002218 }
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302219 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002220
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002221 switch (in) {
2222 case F2FS_GOING_DOWN_FULLSYNC:
Christoph Hellwig040f04b2020-11-24 11:54:06 +01002223 ret = freeze_bdev(sb->s_bdev);
2224 if (ret)
Chao Yud027c482018-01-17 22:28:52 +08002225 goto out;
Christoph Hellwig040f04b2020-11-24 11:54:06 +01002226 f2fs_stop_checkpoint(sbi, false);
2227 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2228 thaw_bdev(sb->s_bdev);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002229 break;
2230 case F2FS_GOING_DOWN_METASYNC:
2231 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002232 ret = f2fs_sync_fs(sb, 1);
2233 if (ret)
2234 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002235 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002236 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002237 break;
2238 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002239 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002240 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002241 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002242 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002243 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002244 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002245 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002246 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002247 case F2FS_GOING_DOWN_NEED_FSCK:
2248 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002249 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2250 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002251 /* do checkpoint only */
2252 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002253 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002254 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002255 ret = -EINVAL;
2256 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002257 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002258
Chao Yu4d57b862018-05-30 00:20:41 +08002259 f2fs_stop_gc_thread(sbi);
2260 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002261
Chao Yu4d57b862018-05-30 00:20:41 +08002262 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002263 clear_opt(sbi, DISCARD);
2264
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002265 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002266out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302267 if (in != F2FS_GOING_DOWN_FULLSYNC)
2268 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002269
2270 trace_f2fs_shutdown(sbi, in, ret);
2271
Chao Yu7fb17fe2016-05-09 19:56:32 +08002272 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002273}
2274
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002275static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2276{
2277 struct inode *inode = file_inode(filp);
2278 struct super_block *sb = inode->i_sb;
2279 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2280 struct fstrim_range range;
2281 int ret;
2282
2283 if (!capable(CAP_SYS_ADMIN))
2284 return -EPERM;
2285
Chao Yu7d20c8a2018-09-04 03:52:17 +08002286 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002287 return -EOPNOTSUPP;
2288
2289 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2290 sizeof(range)))
2291 return -EFAULT;
2292
Chao Yu7fb17fe2016-05-09 19:56:32 +08002293 ret = mnt_want_write_file(filp);
2294 if (ret)
2295 return ret;
2296
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002297 range.minlen = max((unsigned int)range.minlen,
2298 q->limits.discard_granularity);
2299 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002300 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002301 if (ret < 0)
2302 return ret;
2303
2304 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2305 sizeof(range)))
2306 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002307 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002308 return 0;
2309}
2310
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002311static bool uuid_is_nonzero(__u8 u[16])
2312{
2313 int i;
2314
2315 for (i = 0; i < 16; i++)
2316 if (u[i])
2317 return true;
2318 return false;
2319}
2320
2321static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2322{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002323 struct inode *inode = file_inode(filp);
2324
Chao Yu7beb01f2018-10-24 18:34:26 +08002325 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002326 return -EOPNOTSUPP;
2327
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002328 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002329
Eric Biggersdb717d82016-11-26 19:07:49 -05002330 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002331}
2332
2333static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2334{
Chao Yu7beb01f2018-10-24 18:34:26 +08002335 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002336 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002337 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002338}
2339
2340static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2341{
2342 struct inode *inode = file_inode(filp);
2343 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2344 int err;
2345
Chao Yu7beb01f2018-10-24 18:34:26 +08002346 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002347 return -EOPNOTSUPP;
2348
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002349 err = mnt_want_write_file(filp);
2350 if (err)
2351 return err;
2352
Chao Yu846ae672018-02-26 22:04:13 +08002353 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002354
2355 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2356 goto got_it;
2357
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002358 /* update superblock with uuid */
2359 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2360
Chao Yuc5bda1c2015-06-08 13:28:03 +08002361 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002362 if (err) {
2363 /* undo new data */
2364 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002365 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002366 }
2367got_it:
2368 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2369 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002370 err = -EFAULT;
2371out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002372 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002373 mnt_drop_write_file(filp);
2374 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002375}
2376
Eric Biggers8ce589c2019-08-04 19:35:48 -07002377static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2378 unsigned long arg)
2379{
2380 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2381 return -EOPNOTSUPP;
2382
2383 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2384}
2385
2386static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2387{
2388 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2389 return -EOPNOTSUPP;
2390
2391 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2392}
2393
2394static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2395{
2396 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2397 return -EOPNOTSUPP;
2398
2399 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2400}
2401
2402static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2403 unsigned long arg)
2404{
2405 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2406 return -EOPNOTSUPP;
2407
2408 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2409}
2410
2411static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2412 unsigned long arg)
2413{
2414 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2415 return -EOPNOTSUPP;
2416
2417 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2418}
2419
Eric Biggersee446e12020-03-14 13:50:51 -07002420static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2421{
2422 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2423 return -EOPNOTSUPP;
2424
2425 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2426}
2427
Chao Yuc1c1b582015-07-10 18:08:10 +08002428static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2429{
2430 struct inode *inode = file_inode(filp);
2431 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002432 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002433 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002434
2435 if (!capable(CAP_SYS_ADMIN))
2436 return -EPERM;
2437
Chao Yud530d4d2015-10-05 22:22:44 +08002438 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002439 return -EFAULT;
2440
Chao Yud530d4d2015-10-05 22:22:44 +08002441 if (f2fs_readonly(sbi->sb))
2442 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002443
Chao Yu7fb17fe2016-05-09 19:56:32 +08002444 ret = mnt_want_write_file(filp);
2445 if (ret)
2446 return ret;
2447
Chao Yud530d4d2015-10-05 22:22:44 +08002448 if (!sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002449 if (!down_write_trylock(&sbi->gc_lock)) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002450 ret = -EBUSY;
2451 goto out;
2452 }
Chao Yud530d4d2015-10-05 22:22:44 +08002453 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002454 down_write(&sbi->gc_lock);
Chao Yuc1c1b582015-07-10 18:08:10 +08002455 }
2456
Chao Yu7dede8862021-02-20 17:35:40 +08002457 ret = f2fs_gc(sbi, sync, true, false, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002458out:
2459 mnt_drop_write_file(filp);
2460 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002461}
2462
Chao Yu34178b1b2020-11-10 09:24:37 +08002463static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002464{
Chao Yu34178b1b2020-11-10 09:24:37 +08002465 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002466 u64 end;
2467 int ret;
2468
2469 if (!capable(CAP_SYS_ADMIN))
2470 return -EPERM;
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002471 if (f2fs_readonly(sbi->sb))
2472 return -EROFS;
2473
Chao Yu34178b1b2020-11-10 09:24:37 +08002474 end = range->start + range->len;
2475 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302476 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002477 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002478
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002479 ret = mnt_want_write_file(filp);
2480 if (ret)
2481 return ret;
2482
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002483do_more:
Chao Yu34178b1b2020-11-10 09:24:37 +08002484 if (!range->sync) {
Chao Yufb24fea2020-01-14 19:36:50 +08002485 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002486 ret = -EBUSY;
2487 goto out;
2488 }
2489 } else {
Chao Yufb24fea2020-01-14 19:36:50 +08002490 down_write(&sbi->gc_lock);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002491 }
2492
Chao Yu7dede8862021-02-20 17:35:40 +08002493 ret = f2fs_gc(sbi, range->sync, true, false,
2494 GET_SEGNO(sbi, range->start));
Qilong Zhang97767502020-06-28 19:23:03 +08002495 if (ret) {
2496 if (ret == -EBUSY)
2497 ret = -EAGAIN;
2498 goto out;
2499 }
Chao Yu34178b1b2020-11-10 09:24:37 +08002500 range->start += BLKS_PER_SEC(sbi);
2501 if (range->start <= end)
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002502 goto do_more;
2503out:
2504 mnt_drop_write_file(filp);
2505 return ret;
2506}
2507
Chao Yu34178b1b2020-11-10 09:24:37 +08002508static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2509{
2510 struct f2fs_gc_range range;
2511
2512 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2513 sizeof(range)))
2514 return -EFAULT;
2515 return __f2fs_ioc_gc_range(filp, &range);
2516}
2517
Chao Yu059c0642018-07-17 20:41:49 +08002518static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002519{
2520 struct inode *inode = file_inode(filp);
2521 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002522 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002523
2524 if (!capable(CAP_SYS_ADMIN))
2525 return -EPERM;
2526
2527 if (f2fs_readonly(sbi->sb))
2528 return -EROFS;
2529
Daniel Rosenberg43549942018-08-20 19:21:43 -07002530 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002531 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002532 return -EINVAL;
2533 }
2534
Chao Yu7fb17fe2016-05-09 19:56:32 +08002535 ret = mnt_want_write_file(filp);
2536 if (ret)
2537 return ret;
2538
2539 ret = f2fs_sync_fs(sbi->sb, 1);
2540
2541 mnt_drop_write_file(filp);
2542 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002543}
2544
Chao Yud323d002015-10-27 09:53:45 +08002545static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2546 struct file *filp,
2547 struct f2fs_defragment *range)
2548{
2549 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002550 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Yi Zhuang5f029c02021-04-06 09:47:35 +08002551 .m_seg_type = NO_CHECK_TYPE,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002552 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002553 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002554 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002555 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002556 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002557 block_t blk_end = 0;
2558 bool fragmented = false;
2559 int err;
2560
2561 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002562 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002563 return -EINVAL;
2564
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002565 pg_start = range->start >> PAGE_SHIFT;
2566 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002567
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002568 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002569
Al Viro59551022016-01-22 15:40:57 -05002570 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002571
2572 /* writeback all dirty pages in the range */
2573 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002574 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002575 if (err)
2576 goto out;
2577
2578 /*
2579 * lookup mapping info in extent cache, skip defragmenting if physical
2580 * block addresses are continuous.
2581 */
2582 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2583 if (ei.fofs + ei.len >= pg_end)
2584 goto out;
2585 }
2586
2587 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002588 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002589
2590 /*
2591 * lookup mapping info in dnode page cache, skip defragmenting if all
2592 * physical block addresses are continuous even if there are hole(s)
2593 * in logical blocks.
2594 */
2595 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002596 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002597 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002598 if (err)
2599 goto out;
2600
2601 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002602 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002603 continue;
2604 }
2605
Chao Yu25a912e2018-01-10 18:18:51 +08002606 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002607 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002608
2609 /* record total count of block that we're going to move */
2610 total += map.m_len;
2611
Chao Yud323d002015-10-27 09:53:45 +08002612 blk_end = map.m_pblk + map.m_len;
2613
2614 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002615 }
2616
Chao Yud3a1a0e2019-07-29 23:02:29 +08002617 if (!fragmented) {
2618 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002619 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002620 }
Chao Yud323d002015-10-27 09:53:45 +08002621
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002622 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002623
2624 /*
2625 * make sure there are enough free section for LFS allocation, this can
2626 * avoid defragment running in SSR mode when free section are allocated
2627 * intensively
2628 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002629 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002630 err = -EAGAIN;
2631 goto out;
2632 }
2633
Chao Yu25a912e2018-01-10 18:18:51 +08002634 map.m_lblk = pg_start;
2635 map.m_len = pg_end - pg_start;
2636 total = 0;
2637
Chao Yud323d002015-10-27 09:53:45 +08002638 while (map.m_lblk < pg_end) {
2639 pgoff_t idx;
2640 int cnt = 0;
2641
2642do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002643 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002644 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002645 if (err)
2646 goto clear_out;
2647
2648 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002649 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002650 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002651 }
2652
Jaegeuk Kim91942322016-05-20 10:13:22 -07002653 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002654
2655 idx = map.m_lblk;
2656 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2657 struct page *page;
2658
Chao Yu4d57b862018-05-30 00:20:41 +08002659 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002660 if (IS_ERR(page)) {
2661 err = PTR_ERR(page);
2662 goto clear_out;
2663 }
2664
2665 set_page_dirty(page);
2666 f2fs_put_page(page, 1);
2667
2668 idx++;
2669 cnt++;
2670 total++;
2671 }
2672
2673 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002674check:
2675 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002676 goto do_map;
2677
Jaegeuk Kim91942322016-05-20 10:13:22 -07002678 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002679
2680 err = filemap_fdatawrite(inode->i_mapping);
2681 if (err)
2682 goto out;
2683 }
2684clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002685 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002686out:
Al Viro59551022016-01-22 15:40:57 -05002687 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002688 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002689 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002690 return err;
2691}
2692
2693static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2694{
2695 struct inode *inode = file_inode(filp);
2696 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2697 struct f2fs_defragment range;
2698 int err;
2699
2700 if (!capable(CAP_SYS_ADMIN))
2701 return -EPERM;
2702
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002703 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002704 return -EINVAL;
2705
Kinglong Meed7563862017-03-10 17:55:07 +08002706 if (f2fs_readonly(sbi->sb))
2707 return -EROFS;
2708
2709 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2710 sizeof(range)))
2711 return -EFAULT;
2712
2713 /* verify alignment of offset & size */
2714 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2715 return -EINVAL;
2716
2717 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
Chengguang Xu6d1451b2021-01-13 13:21:54 +08002718 max_file_blocks(inode)))
Kinglong Meed7563862017-03-10 17:55:07 +08002719 return -EINVAL;
2720
Chao Yud323d002015-10-27 09:53:45 +08002721 err = mnt_want_write_file(filp);
2722 if (err)
2723 return err;
2724
Chao Yud323d002015-10-27 09:53:45 +08002725 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002726 mnt_drop_write_file(filp);
2727
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002728 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002729 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002730 return err;
Chao Yud323d002015-10-27 09:53:45 +08002731
2732 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2733 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002734 return -EFAULT;
2735
2736 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002737}
2738
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002739static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2740 struct file *file_out, loff_t pos_out, size_t len)
2741{
2742 struct inode *src = file_inode(file_in);
2743 struct inode *dst = file_inode(file_out);
2744 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2745 size_t olen = len, dst_max_i_size = 0;
2746 size_t dst_osize;
2747 int ret;
2748
2749 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2750 src->i_sb != dst->i_sb)
2751 return -EXDEV;
2752
2753 if (unlikely(f2fs_readonly(src->i_sb)))
2754 return -EROFS;
2755
Chao Yufe8494b2016-08-04 20:13:02 +08002756 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2757 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002758
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302759 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002760 return -EOPNOTSUPP;
2761
Dan Robertsonaad13832020-08-30 21:45:23 +00002762 if (pos_out < 0 || pos_in < 0)
2763 return -EINVAL;
2764
Fan Lid95fd912016-09-13 11:35:42 +08002765 if (src == dst) {
2766 if (pos_in == pos_out)
2767 return 0;
2768 if (pos_out > pos_in && pos_out < pos_in + len)
2769 return -EINVAL;
2770 }
2771
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002772 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002773 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002774 ret = -EBUSY;
2775 if (!inode_trylock(dst))
2776 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002777 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002778
2779 ret = -EINVAL;
2780 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2781 goto out_unlock;
2782 if (len == 0)
2783 olen = len = src->i_size - pos_in;
2784 if (pos_in + len == src->i_size)
2785 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2786 if (len == 0) {
2787 ret = 0;
2788 goto out_unlock;
2789 }
2790
2791 dst_osize = dst->i_size;
2792 if (pos_out + olen > dst->i_size)
2793 dst_max_i_size = pos_out + olen;
2794
2795 /* verify the end result is block aligned */
2796 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2797 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2798 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2799 goto out_unlock;
2800
2801 ret = f2fs_convert_inline_inode(src);
2802 if (ret)
2803 goto out_unlock;
2804
2805 ret = f2fs_convert_inline_inode(dst);
2806 if (ret)
2807 goto out_unlock;
2808
2809 /* write out all dirty pages from offset */
2810 ret = filemap_write_and_wait_range(src->i_mapping,
2811 pos_in, pos_in + len);
2812 if (ret)
2813 goto out_unlock;
2814
2815 ret = filemap_write_and_wait_range(dst->i_mapping,
2816 pos_out, pos_out + len);
2817 if (ret)
2818 goto out_unlock;
2819
2820 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002821
2822 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2823 if (src != dst) {
2824 ret = -EBUSY;
2825 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2826 goto out_src;
2827 }
2828
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002829 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002830 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2831 pos_out >> F2FS_BLKSIZE_BITS,
2832 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002833
2834 if (!ret) {
2835 if (dst_max_i_size)
2836 f2fs_i_size_write(dst, dst_max_i_size);
2837 else if (dst_osize != dst->i_size)
2838 f2fs_i_size_write(dst, dst_osize);
2839 }
2840 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002841
2842 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002843 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002844out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002845 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002846out_unlock:
2847 if (src != dst)
2848 inode_unlock(dst);
2849out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002850 inode_unlock(src);
2851 return ret;
2852}
2853
Chao Yu34178b1b2020-11-10 09:24:37 +08002854static int __f2fs_ioc_move_range(struct file *filp,
2855 struct f2fs_move_range *range)
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002856{
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002857 struct fd dst;
2858 int err;
2859
2860 if (!(filp->f_mode & FMODE_READ) ||
2861 !(filp->f_mode & FMODE_WRITE))
2862 return -EBADF;
2863
Chao Yu34178b1b2020-11-10 09:24:37 +08002864 dst = fdget(range->dst_fd);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002865 if (!dst.file)
2866 return -EBADF;
2867
2868 if (!(dst.file->f_mode & FMODE_WRITE)) {
2869 err = -EBADF;
2870 goto err_out;
2871 }
2872
2873 err = mnt_want_write_file(filp);
2874 if (err)
2875 goto err_out;
2876
Chao Yu34178b1b2020-11-10 09:24:37 +08002877 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2878 range->pos_out, range->len);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002879
2880 mnt_drop_write_file(filp);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002881err_out:
2882 fdput(dst);
2883 return err;
2884}
2885
Chao Yu34178b1b2020-11-10 09:24:37 +08002886static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2887{
2888 struct f2fs_move_range range;
2889
2890 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2891 sizeof(range)))
2892 return -EFAULT;
2893 return __f2fs_ioc_move_range(filp, &range);
2894}
2895
Jaegeuk Kime066b832017-04-13 15:17:00 -07002896static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2897{
2898 struct inode *inode = file_inode(filp);
2899 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2900 struct sit_info *sm = SIT_I(sbi);
2901 unsigned int start_segno = 0, end_segno = 0;
2902 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2903 struct f2fs_flush_device range;
2904 int ret;
2905
2906 if (!capable(CAP_SYS_ADMIN))
2907 return -EPERM;
2908
2909 if (f2fs_readonly(sbi->sb))
2910 return -EROFS;
2911
Daniel Rosenberg43549942018-08-20 19:21:43 -07002912 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2913 return -EINVAL;
2914
Jaegeuk Kime066b832017-04-13 15:17:00 -07002915 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2916 sizeof(range)))
2917 return -EFAULT;
2918
Damien Le Moal09168782019-03-16 09:13:06 +09002919 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002920 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002921 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2922 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002923 return -EINVAL;
2924 }
2925
2926 ret = mnt_want_write_file(filp);
2927 if (ret)
2928 return ret;
2929
2930 if (range.dev_num != 0)
2931 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2932 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2933
2934 start_segno = sm->last_victim[FLUSH_DEVICE];
2935 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2936 start_segno = dev_start_segno;
2937 end_segno = min(start_segno + range.segments, dev_end_segno);
2938
2939 while (start_segno < end_segno) {
Chao Yufb24fea2020-01-14 19:36:50 +08002940 if (!down_write_trylock(&sbi->gc_lock)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002941 ret = -EBUSY;
2942 goto out;
2943 }
2944 sm->last_victim[GC_CB] = end_segno + 1;
2945 sm->last_victim[GC_GREEDY] = end_segno + 1;
2946 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
Chao Yu7dede8862021-02-20 17:35:40 +08002947 ret = f2fs_gc(sbi, true, true, true, start_segno);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002948 if (ret == -EAGAIN)
2949 ret = 0;
2950 else if (ret < 0)
2951 break;
2952 start_segno++;
2953 }
2954out:
2955 mnt_drop_write_file(filp);
2956 return ret;
2957}
2958
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002959static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2960{
2961 struct inode *inode = file_inode(filp);
2962 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2963
2964 /* Must validate to set it with SQLite behavior in Android. */
2965 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2966
2967 return put_user(sb_feature, (u32 __user *)arg);
2968}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002969
Chao Yu2c1d0302017-07-29 00:32:52 +08002970#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002971int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2972{
2973 struct dquot *transfer_to[MAXQUOTAS] = {};
2974 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2975 struct super_block *sb = sbi->sb;
2976 int err = 0;
2977
2978 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2979 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2980 err = __dquot_transfer(inode, transfer_to);
2981 if (err)
2982 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2983 dqput(transfer_to[PRJQUOTA]);
2984 }
2985 return err;
2986}
2987
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02002988static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
Chao Yu2c1d0302017-07-29 00:32:52 +08002989{
Chao Yu2c1d0302017-07-29 00:32:52 +08002990 struct f2fs_inode_info *fi = F2FS_I(inode);
2991 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002992 struct page *ipage;
2993 kprojid_t kprojid;
2994 int err;
2995
Chao Yu7beb01f2018-10-24 18:34:26 +08002996 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08002997 if (projid != F2FS_DEF_PROJID)
2998 return -EOPNOTSUPP;
2999 else
3000 return 0;
3001 }
3002
3003 if (!f2fs_has_extra_attr(inode))
3004 return -EOPNOTSUPP;
3005
3006 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3007
3008 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3009 return 0;
3010
Chao Yu2c1d0302017-07-29 00:32:52 +08003011 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003012 /* Is it quota file? Do not allow user to mess with it */
3013 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003014 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003015
Chao Yu4d57b862018-05-30 00:20:41 +08003016 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003017 if (IS_ERR(ipage))
3018 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003019
3020 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3021 i_projid)) {
3022 err = -EOVERFLOW;
3023 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003024 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003025 }
3026 f2fs_put_page(ipage, 1);
3027
Chao Yuc22aecd2018-04-21 17:53:52 +08003028 err = dquot_initialize(inode);
3029 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003030 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003031
Chao Yu78130812018-09-25 15:36:02 +08003032 f2fs_lock_op(sbi);
3033 err = f2fs_transfer_project_quota(inode, kprojid);
3034 if (err)
3035 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003036
3037 F2FS_I(inode)->i_projid = kprojid;
3038 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003039 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003040out_unlock:
3041 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003042 return err;
3043}
3044#else
Chao Yu78130812018-09-25 15:36:02 +08003045int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3046{
3047 return 0;
3048}
3049
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003050static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
Chao Yu2c1d0302017-07-29 00:32:52 +08003051{
3052 if (projid != F2FS_DEF_PROJID)
3053 return -EOPNOTSUPP;
3054 return 0;
3055}
3056#endif
3057
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003058int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
Eric Biggers36098552019-06-04 22:59:04 -07003059{
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003060 struct inode *inode = d_inode(dentry);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003061 struct f2fs_inode_info *fi = F2FS_I(inode);
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003062 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003063
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003064 if (IS_ENCRYPTED(inode))
3065 fsflags |= FS_ENCRYPT_FL;
3066 if (IS_VERITY(inode))
3067 fsflags |= FS_VERITY_FL;
3068 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3069 fsflags |= FS_INLINE_DATA_FL;
3070 if (is_inode_flag_set(inode, FI_PIN_FILE))
3071 fsflags |= FS_NOCOW_FL;
3072
3073 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003074
3075 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3076 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003077
Chao Yu2c1d0302017-07-29 00:32:52 +08003078 return 0;
3079}
3080
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003081int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3082 struct dentry *dentry, struct fileattr *fa)
Chao Yu2c1d0302017-07-29 00:32:52 +08003083{
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003084 struct inode *inode = d_inode(dentry);
3085 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
Eric Biggers36098552019-06-04 22:59:04 -07003086 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003087 int err;
3088
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003089 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3090 return -EIO;
3091 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3092 return -ENOSPC;
3093 if (fsflags & ~F2FS_GETTABLE_FS_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08003094 return -EOPNOTSUPP;
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003095 fsflags &= F2FS_SETTABLE_FS_FL;
3096 if (!fa->flags_valid)
3097 mask &= FS_COMMON_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08003098
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003099 iflags = f2fs_fsflags_to_iflags(fsflags);
Eric Biggers36098552019-06-04 22:59:04 -07003100 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003101 return -EOPNOTSUPP;
3102
Miklos Szeredi9b1bb012021-04-07 14:36:43 +02003103 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3104 if (!err)
3105 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
Chao Yu2c1d0302017-07-29 00:32:52 +08003106
Wang Shilongc8e92752018-09-11 08:54:21 +09003107 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003108}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003109
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003110int f2fs_pin_file_control(struct inode *inode, bool inc)
3111{
3112 struct f2fs_inode_info *fi = F2FS_I(inode);
3113 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3114
3115 /* Use i_gc_failures for normal file as a risk signal. */
3116 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003117 f2fs_i_gc_failures_write(inode,
3118 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003119
Chao Yu2ef79ec2018-05-07 20:28:54 +08003120 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003121 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3122 __func__, inode->i_ino,
3123 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003124 clear_inode_flag(inode, FI_PIN_FILE);
3125 return -EAGAIN;
3126 }
3127 return 0;
3128}
3129
3130static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3131{
3132 struct inode *inode = file_inode(filp);
3133 __u32 pin;
3134 int ret = 0;
3135
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003136 if (get_user(pin, (__u32 __user *)arg))
3137 return -EFAULT;
3138
3139 if (!S_ISREG(inode->i_mode))
3140 return -EINVAL;
3141
3142 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3143 return -EROFS;
3144
3145 ret = mnt_want_write_file(filp);
3146 if (ret)
3147 return ret;
3148
3149 inode_lock(inode);
3150
Chao Yu4d57b862018-05-30 00:20:41 +08003151 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08003152 ret = -EINVAL;
3153 goto out;
3154 }
3155
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003156 if (!pin) {
3157 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003158 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003159 goto done;
3160 }
3161
3162 if (f2fs_pin_file_control(inode, false)) {
3163 ret = -EAGAIN;
3164 goto out;
3165 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003166
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003167 ret = f2fs_convert_inline_inode(inode);
3168 if (ret)
3169 goto out;
3170
Daeho Jeong78134d02020-09-08 11:44:11 +09003171 if (!f2fs_disable_compressed_file(inode)) {
Chao Yu4c8ff702019-11-01 18:07:14 +08003172 ret = -EOPNOTSUPP;
3173 goto out;
3174 }
3175
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003176 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003177 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003178done:
3179 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3180out:
3181 inode_unlock(inode);
3182 mnt_drop_write_file(filp);
3183 return ret;
3184}
3185
3186static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3187{
3188 struct inode *inode = file_inode(filp);
3189 __u32 pin = 0;
3190
3191 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003192 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003193 return put_user(pin, (u32 __user *)arg);
3194}
3195
Chao Yuc4020b22018-01-11 14:42:30 +08003196int f2fs_precache_extents(struct inode *inode)
3197{
3198 struct f2fs_inode_info *fi = F2FS_I(inode);
3199 struct f2fs_map_blocks map;
3200 pgoff_t m_next_extent;
3201 loff_t end;
3202 int err;
3203
3204 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3205 return -EOPNOTSUPP;
3206
3207 map.m_lblk = 0;
3208 map.m_next_pgofs = NULL;
3209 map.m_next_extent = &m_next_extent;
3210 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003211 map.m_may_create = false;
Chengguang Xu6d1451b2021-01-13 13:21:54 +08003212 end = max_file_blocks(inode);
Chao Yuc4020b22018-01-11 14:42:30 +08003213
3214 while (map.m_lblk < end) {
3215 map.m_len = end - map.m_lblk;
3216
Chao Yub2532c62018-04-24 10:55:28 +08003217 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003218 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003219 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003220 if (err)
3221 return err;
3222
3223 map.m_lblk = m_next_extent;
3224 }
3225
Tom Rix4f55dc22021-05-15 11:09:41 -07003226 return 0;
Chao Yuc4020b22018-01-11 14:42:30 +08003227}
3228
3229static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3230{
3231 return f2fs_precache_extents(file_inode(filp));
3232}
3233
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003234static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3235{
3236 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3237 __u64 block_count;
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003238
3239 if (!capable(CAP_SYS_ADMIN))
3240 return -EPERM;
3241
3242 if (f2fs_readonly(sbi->sb))
3243 return -EROFS;
3244
3245 if (copy_from_user(&block_count, (void __user *)arg,
3246 sizeof(block_count)))
3247 return -EFAULT;
3248
Jaegeuk Kimb4b10062020-03-31 11:43:07 -07003249 return f2fs_resize_fs(sbi, block_count);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003250}
3251
Eric Biggers95ae2512019-07-22 09:26:24 -07003252static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3253{
3254 struct inode *inode = file_inode(filp);
3255
3256 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3257
3258 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3259 f2fs_warn(F2FS_I_SB(inode),
Joe Perches833dcd32021-05-26 13:05:36 -07003260 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
Eric Biggers95ae2512019-07-22 09:26:24 -07003261 inode->i_ino);
3262 return -EOPNOTSUPP;
3263 }
3264
3265 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3266}
3267
3268static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3269{
3270 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3271 return -EOPNOTSUPP;
3272
3273 return fsverity_ioctl_measure(filp, (void __user *)arg);
3274}
3275
Eric Biggerse17fe652021-01-15 10:18:16 -08003276static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3277{
3278 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3279 return -EOPNOTSUPP;
3280
3281 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3282}
3283
Eric Biggers3357af82020-07-14 15:18:12 -07003284static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003285{
3286 struct inode *inode = file_inode(filp);
3287 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3288 char *vbuf;
3289 int count;
3290 int err = 0;
3291
3292 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3293 if (!vbuf)
3294 return -ENOMEM;
3295
3296 down_read(&sbi->sb_lock);
3297 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3298 ARRAY_SIZE(sbi->raw_super->volume_name),
3299 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3300 up_read(&sbi->sb_lock);
3301
3302 if (copy_to_user((char __user *)arg, vbuf,
3303 min(FSLABEL_MAX, count)))
3304 err = -EFAULT;
3305
Chao Yuc8eb7022020-09-14 16:47:00 +08003306 kfree(vbuf);
Chao Yu4507847c2019-07-17 17:06:11 +08003307 return err;
3308}
3309
Eric Biggers3357af82020-07-14 15:18:12 -07003310static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
Chao Yu4507847c2019-07-17 17:06:11 +08003311{
3312 struct inode *inode = file_inode(filp);
3313 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3314 char *vbuf;
3315 int err = 0;
3316
3317 if (!capable(CAP_SYS_ADMIN))
3318 return -EPERM;
3319
3320 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3321 if (IS_ERR(vbuf))
3322 return PTR_ERR(vbuf);
3323
3324 err = mnt_want_write_file(filp);
3325 if (err)
3326 goto out;
3327
3328 down_write(&sbi->sb_lock);
3329
3330 memset(sbi->raw_super->volume_name, 0,
3331 sizeof(sbi->raw_super->volume_name));
3332 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3333 sbi->raw_super->volume_name,
3334 ARRAY_SIZE(sbi->raw_super->volume_name));
3335
3336 err = f2fs_commit_super(sbi, false);
3337
3338 up_write(&sbi->sb_lock);
3339
3340 mnt_drop_write_file(filp);
3341out:
3342 kfree(vbuf);
3343 return err;
3344}
3345
Chao Yu439dfb12020-02-21 18:09:21 +08003346static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3347{
3348 struct inode *inode = file_inode(filp);
3349 __u64 blocks;
3350
3351 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3352 return -EOPNOTSUPP;
3353
3354 if (!f2fs_compressed_file(inode))
3355 return -EINVAL;
3356
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003357 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
Chao Yu439dfb12020-02-21 18:09:21 +08003358 return put_user(blocks, (u64 __user *)arg);
3359}
3360
Chao Yuef8d5632020-03-06 15:36:09 +08003361static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3362{
3363 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3364 unsigned int released_blocks = 0;
3365 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3366 block_t blkaddr;
3367 int i;
3368
3369 for (i = 0; i < count; i++) {
3370 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3371 dn->ofs_in_node + i);
3372
3373 if (!__is_valid_data_blkaddr(blkaddr))
3374 continue;
3375 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3376 DATA_GENERIC_ENHANCE)))
3377 return -EFSCORRUPTED;
3378 }
3379
3380 while (count) {
3381 int compr_blocks = 0;
3382
3383 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3384 blkaddr = f2fs_data_blkaddr(dn);
3385
3386 if (i == 0) {
3387 if (blkaddr == COMPRESS_ADDR)
3388 continue;
3389 dn->ofs_in_node += cluster_size;
3390 goto next;
3391 }
3392
3393 if (__is_valid_data_blkaddr(blkaddr))
3394 compr_blocks++;
3395
3396 if (blkaddr != NEW_ADDR)
3397 continue;
3398
3399 dn->data_blkaddr = NULL_ADDR;
3400 f2fs_set_data_blkaddr(dn);
3401 }
3402
3403 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3404 dec_valid_block_count(sbi, dn->inode,
3405 cluster_size - compr_blocks);
3406
3407 released_blocks += cluster_size - compr_blocks;
3408next:
3409 count -= cluster_size;
3410 }
3411
3412 return released_blocks;
3413}
3414
3415static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3416{
3417 struct inode *inode = file_inode(filp);
3418 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3419 pgoff_t page_idx = 0, last_idx;
3420 unsigned int released_blocks = 0;
3421 int ret;
3422 int writecount;
3423
3424 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3425 return -EOPNOTSUPP;
3426
3427 if (!f2fs_compressed_file(inode))
3428 return -EINVAL;
3429
3430 if (f2fs_readonly(sbi->sb))
3431 return -EROFS;
3432
3433 ret = mnt_want_write_file(filp);
3434 if (ret)
3435 return ret;
3436
3437 f2fs_balance_fs(F2FS_I_SB(inode), true);
3438
3439 inode_lock(inode);
3440
3441 writecount = atomic_read(&inode->i_writecount);
Daeho Jeong8c8cf262020-10-12 13:59:47 +09003442 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3443 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
Chao Yuef8d5632020-03-06 15:36:09 +08003444 ret = -EBUSY;
3445 goto out;
3446 }
3447
Jaegeuk Kimc6140412021-05-25 11:39:35 -07003448 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
Chao Yuef8d5632020-03-06 15:36:09 +08003449 ret = -EINVAL;
3450 goto out;
3451 }
3452
3453 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3454 if (ret)
3455 goto out;
3456
Jaegeuk Kimc6140412021-05-25 11:39:35 -07003457 set_inode_flag(inode, FI_COMPRESS_RELEASED);
Chao Yuef8d5632020-03-06 15:36:09 +08003458 inode->i_ctime = current_time(inode);
3459 f2fs_mark_inode_dirty_sync(inode, true);
3460
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003461 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
Daeho Jeong567c4bf2020-07-30 14:09:28 +09003462 goto out;
3463
Chao Yuef8d5632020-03-06 15:36:09 +08003464 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3465 down_write(&F2FS_I(inode)->i_mmap_sem);
3466
3467 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3468
3469 while (page_idx < last_idx) {
3470 struct dnode_of_data dn;
3471 pgoff_t end_offset, count;
3472
3473 set_new_dnode(&dn, inode, NULL, NULL, 0);
3474 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3475 if (ret) {
3476 if (ret == -ENOENT) {
3477 page_idx = f2fs_get_next_page_offset(&dn,
3478 page_idx);
3479 ret = 0;
3480 continue;
3481 }
3482 break;
3483 }
3484
3485 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3486 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003487 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuef8d5632020-03-06 15:36:09 +08003488
3489 ret = release_compress_blocks(&dn, count);
3490
3491 f2fs_put_dnode(&dn);
3492
3493 if (ret < 0)
3494 break;
3495
3496 page_idx += count;
3497 released_blocks += ret;
3498 }
3499
3500 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3501 up_write(&F2FS_I(inode)->i_mmap_sem);
3502out:
3503 inode_unlock(inode);
3504
3505 mnt_drop_write_file(filp);
3506
3507 if (ret >= 0) {
3508 ret = put_user(released_blocks, (u64 __user *)arg);
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003509 } else if (released_blocks &&
3510 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
Chao Yuef8d5632020-03-06 15:36:09 +08003511 set_sbi_flag(sbi, SBI_NEED_FSCK);
3512 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003513 "iblocks=%llu, released=%u, compr_blocks=%u, "
Chao Yuef8d5632020-03-06 15:36:09 +08003514 "run fsck to fix.",
3515 __func__, inode->i_ino, inode->i_blocks,
3516 released_blocks,
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003517 atomic_read(&F2FS_I(inode)->i_compr_blocks));
Chao Yuef8d5632020-03-06 15:36:09 +08003518 }
3519
3520 return ret;
3521}
3522
Chao Yuc75488f2020-03-06 14:35:33 +08003523static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3524{
3525 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3526 unsigned int reserved_blocks = 0;
3527 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3528 block_t blkaddr;
3529 int i;
3530
3531 for (i = 0; i < count; i++) {
3532 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3533 dn->ofs_in_node + i);
3534
3535 if (!__is_valid_data_blkaddr(blkaddr))
3536 continue;
3537 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3538 DATA_GENERIC_ENHANCE)))
3539 return -EFSCORRUPTED;
3540 }
3541
3542 while (count) {
3543 int compr_blocks = 0;
3544 blkcnt_t reserved;
3545 int ret;
3546
3547 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3548 blkaddr = f2fs_data_blkaddr(dn);
3549
3550 if (i == 0) {
3551 if (blkaddr == COMPRESS_ADDR)
3552 continue;
3553 dn->ofs_in_node += cluster_size;
3554 goto next;
3555 }
3556
3557 if (__is_valid_data_blkaddr(blkaddr)) {
3558 compr_blocks++;
3559 continue;
3560 }
3561
3562 dn->data_blkaddr = NEW_ADDR;
3563 f2fs_set_data_blkaddr(dn);
3564 }
3565
3566 reserved = cluster_size - compr_blocks;
3567 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3568 if (ret)
3569 return ret;
3570
3571 if (reserved != cluster_size - compr_blocks)
3572 return -ENOSPC;
3573
3574 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3575
3576 reserved_blocks += reserved;
3577next:
3578 count -= cluster_size;
3579 }
3580
3581 return reserved_blocks;
3582}
3583
3584static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3585{
3586 struct inode *inode = file_inode(filp);
3587 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3588 pgoff_t page_idx = 0, last_idx;
3589 unsigned int reserved_blocks = 0;
3590 int ret;
3591
3592 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3593 return -EOPNOTSUPP;
3594
3595 if (!f2fs_compressed_file(inode))
3596 return -EINVAL;
3597
3598 if (f2fs_readonly(sbi->sb))
3599 return -EROFS;
3600
3601 ret = mnt_want_write_file(filp);
3602 if (ret)
3603 return ret;
3604
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003605 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
Chao Yuc75488f2020-03-06 14:35:33 +08003606 goto out;
3607
3608 f2fs_balance_fs(F2FS_I_SB(inode), true);
3609
3610 inode_lock(inode);
3611
Jaegeuk Kimc6140412021-05-25 11:39:35 -07003612 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
Chao Yuc75488f2020-03-06 14:35:33 +08003613 ret = -EINVAL;
3614 goto unlock_inode;
3615 }
3616
3617 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3618 down_write(&F2FS_I(inode)->i_mmap_sem);
3619
3620 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3621
3622 while (page_idx < last_idx) {
3623 struct dnode_of_data dn;
3624 pgoff_t end_offset, count;
3625
3626 set_new_dnode(&dn, inode, NULL, NULL, 0);
3627 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3628 if (ret) {
3629 if (ret == -ENOENT) {
3630 page_idx = f2fs_get_next_page_offset(&dn,
3631 page_idx);
3632 ret = 0;
3633 continue;
3634 }
3635 break;
3636 }
3637
3638 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3639 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
Chao Yu4fec3fc02020-04-08 19:55:17 +08003640 count = round_up(count, F2FS_I(inode)->i_cluster_size);
Chao Yuc75488f2020-03-06 14:35:33 +08003641
3642 ret = reserve_compress_blocks(&dn, count);
3643
3644 f2fs_put_dnode(&dn);
3645
3646 if (ret < 0)
3647 break;
3648
3649 page_idx += count;
3650 reserved_blocks += ret;
3651 }
3652
3653 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3654 up_write(&F2FS_I(inode)->i_mmap_sem);
3655
3656 if (ret >= 0) {
Jaegeuk Kimc6140412021-05-25 11:39:35 -07003657 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
Chao Yuc75488f2020-03-06 14:35:33 +08003658 inode->i_ctime = current_time(inode);
3659 f2fs_mark_inode_dirty_sync(inode, true);
3660 }
3661unlock_inode:
3662 inode_unlock(inode);
3663out:
3664 mnt_drop_write_file(filp);
3665
3666 if (ret >= 0) {
3667 ret = put_user(reserved_blocks, (u64 __user *)arg);
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003668 } else if (reserved_blocks &&
3669 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
Chao Yuc75488f2020-03-06 14:35:33 +08003670 set_sbi_flag(sbi, SBI_NEED_FSCK);
3671 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003672 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
Chao Yuc75488f2020-03-06 14:35:33 +08003673 "run fsck to fix.",
3674 __func__, inode->i_ino, inode->i_blocks,
3675 reserved_blocks,
Daeho Jeongc2759eb2020-09-08 11:44:10 +09003676 atomic_read(&F2FS_I(inode)->i_compr_blocks));
Chao Yuc75488f2020-03-06 14:35:33 +08003677 }
3678
3679 return ret;
3680}
3681
Daeho Jeong9af84642020-07-21 12:21:11 +09003682static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3683 pgoff_t off, block_t block, block_t len, u32 flags)
3684{
3685 struct request_queue *q = bdev_get_queue(bdev);
3686 sector_t sector = SECTOR_FROM_BLOCK(block);
3687 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3688 int ret = 0;
3689
3690 if (!q)
3691 return -ENXIO;
3692
3693 if (flags & F2FS_TRIM_FILE_DISCARD)
3694 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3695 blk_queue_secure_erase(q) ?
3696 BLKDEV_DISCARD_SECURE : 0);
3697
3698 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3699 if (IS_ENCRYPTED(inode))
3700 ret = fscrypt_zeroout_range(inode, off, block, len);
3701 else
3702 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3703 GFP_NOFS, 0);
3704 }
3705
3706 return ret;
3707}
3708
3709static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3710{
3711 struct inode *inode = file_inode(filp);
3712 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3713 struct address_space *mapping = inode->i_mapping;
3714 struct block_device *prev_bdev = NULL;
3715 struct f2fs_sectrim_range range;
3716 pgoff_t index, pg_end, prev_index = 0;
3717 block_t prev_block = 0, len = 0;
3718 loff_t end_addr;
3719 bool to_end = false;
3720 int ret = 0;
3721
3722 if (!(filp->f_mode & FMODE_WRITE))
3723 return -EBADF;
3724
3725 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3726 sizeof(range)))
3727 return -EFAULT;
3728
3729 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3730 !S_ISREG(inode->i_mode))
3731 return -EINVAL;
3732
3733 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3734 !f2fs_hw_support_discard(sbi)) ||
3735 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3736 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3737 return -EOPNOTSUPP;
3738
3739 file_start_write(filp);
3740 inode_lock(inode);
3741
3742 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3743 range.start >= inode->i_size) {
3744 ret = -EINVAL;
3745 goto err;
3746 }
3747
3748 if (range.len == 0)
3749 goto err;
3750
3751 if (inode->i_size - range.start > range.len) {
3752 end_addr = range.start + range.len;
3753 } else {
3754 end_addr = range.len == (u64)-1 ?
3755 sbi->sb->s_maxbytes : inode->i_size;
3756 to_end = true;
3757 }
3758
3759 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3760 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3761 ret = -EINVAL;
3762 goto err;
3763 }
3764
3765 index = F2FS_BYTES_TO_BLK(range.start);
3766 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3767
3768 ret = f2fs_convert_inline_inode(inode);
3769 if (ret)
3770 goto err;
3771
3772 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3773 down_write(&F2FS_I(inode)->i_mmap_sem);
3774
3775 ret = filemap_write_and_wait_range(mapping, range.start,
3776 to_end ? LLONG_MAX : end_addr - 1);
3777 if (ret)
3778 goto out;
3779
3780 truncate_inode_pages_range(mapping, range.start,
3781 to_end ? -1 : end_addr - 1);
3782
3783 while (index < pg_end) {
3784 struct dnode_of_data dn;
3785 pgoff_t end_offset, count;
3786 int i;
3787
3788 set_new_dnode(&dn, inode, NULL, NULL, 0);
3789 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3790 if (ret) {
3791 if (ret == -ENOENT) {
3792 index = f2fs_get_next_page_offset(&dn, index);
3793 continue;
3794 }
3795 goto out;
3796 }
3797
3798 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3799 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3800 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3801 struct block_device *cur_bdev;
3802 block_t blkaddr = f2fs_data_blkaddr(&dn);
3803
3804 if (!__is_valid_data_blkaddr(blkaddr))
3805 continue;
3806
3807 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3808 DATA_GENERIC_ENHANCE)) {
3809 ret = -EFSCORRUPTED;
3810 f2fs_put_dnode(&dn);
3811 goto out;
3812 }
3813
3814 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3815 if (f2fs_is_multi_device(sbi)) {
3816 int di = f2fs_target_device_index(sbi, blkaddr);
3817
3818 blkaddr -= FDEV(di).start_blk;
3819 }
3820
3821 if (len) {
3822 if (prev_bdev == cur_bdev &&
3823 index == prev_index + len &&
3824 blkaddr == prev_block + len) {
3825 len++;
3826 } else {
3827 ret = f2fs_secure_erase(prev_bdev,
3828 inode, prev_index, prev_block,
3829 len, range.flags);
3830 if (ret) {
3831 f2fs_put_dnode(&dn);
3832 goto out;
3833 }
3834
3835 len = 0;
3836 }
3837 }
3838
3839 if (!len) {
3840 prev_bdev = cur_bdev;
3841 prev_index = index;
3842 prev_block = blkaddr;
3843 len = 1;
3844 }
3845 }
3846
3847 f2fs_put_dnode(&dn);
3848
3849 if (fatal_signal_pending(current)) {
3850 ret = -EINTR;
3851 goto out;
3852 }
3853 cond_resched();
3854 }
3855
3856 if (len)
3857 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3858 prev_block, len, range.flags);
3859out:
3860 up_write(&F2FS_I(inode)->i_mmap_sem);
3861 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3862err:
3863 inode_unlock(inode);
3864 file_end_write(filp);
3865
3866 return ret;
3867}
3868
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09003869static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003870{
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09003871 struct inode *inode = file_inode(filp);
3872 struct f2fs_comp_option option;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003873
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09003874 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3875 return -EOPNOTSUPP;
3876
3877 inode_lock_shared(inode);
3878
3879 if (!f2fs_compressed_file(inode)) {
3880 inode_unlock_shared(inode);
3881 return -ENODATA;
3882 }
3883
3884 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3885 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3886
3887 inode_unlock_shared(inode);
3888
3889 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3890 sizeof(option)))
3891 return -EFAULT;
3892
3893 return 0;
3894}
3895
Daeho Jeonge1e8deb2020-10-30 13:10:35 +09003896static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3897{
3898 struct inode *inode = file_inode(filp);
3899 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3900 struct f2fs_comp_option option;
3901 int ret = 0;
3902
3903 if (!f2fs_sb_has_compression(sbi))
3904 return -EOPNOTSUPP;
3905
3906 if (!(filp->f_mode & FMODE_WRITE))
3907 return -EBADF;
3908
3909 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3910 sizeof(option)))
3911 return -EFAULT;
3912
3913 if (!f2fs_compressed_file(inode) ||
3914 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3915 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3916 option.algorithm >= COMPRESS_MAX)
3917 return -EINVAL;
3918
3919 file_start_write(filp);
3920 inode_lock(inode);
3921
3922 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3923 ret = -EBUSY;
3924 goto out;
3925 }
3926
3927 if (inode->i_size != 0) {
3928 ret = -EFBIG;
3929 goto out;
3930 }
3931
3932 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3933 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3934 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
3935 f2fs_mark_inode_dirty_sync(inode, true);
3936
3937 if (!f2fs_is_compress_backend_ready(inode))
3938 f2fs_warn(sbi, "compression algorithm is successfully set, "
3939 "but current kernel doesn't support this algorithm.");
3940out:
3941 inode_unlock(inode);
3942 file_end_write(filp);
3943
3944 return ret;
3945}
3946
Daeho Jeong5fdb3222020-12-03 15:56:15 +09003947static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
3948{
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +01003949 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
Daeho Jeong5fdb3222020-12-03 15:56:15 +09003950 struct address_space *mapping = inode->i_mapping;
3951 struct page *page;
3952 pgoff_t redirty_idx = page_idx;
3953 int i, page_len = 0, ret = 0;
3954
3955 page_cache_ra_unbounded(&ractl, len, 0);
3956
3957 for (i = 0; i < len; i++, page_idx++) {
3958 page = read_cache_page(mapping, page_idx, NULL, NULL);
3959 if (IS_ERR(page)) {
3960 ret = PTR_ERR(page);
3961 break;
3962 }
3963 page_len++;
3964 }
3965
3966 for (i = 0; i < page_len; i++, redirty_idx++) {
3967 page = find_lock_page(mapping, redirty_idx);
Daeho Jeongdf0736d2021-01-06 08:49:28 +09003968 if (!page) {
3969 ret = -ENOMEM;
3970 break;
3971 }
Daeho Jeong5fdb3222020-12-03 15:56:15 +09003972 set_page_dirty(page);
3973 f2fs_put_page(page, 1);
3974 f2fs_put_page(page, 0);
3975 }
3976
3977 return ret;
3978}
3979
3980static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
3981{
3982 struct inode *inode = file_inode(filp);
3983 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3984 struct f2fs_inode_info *fi = F2FS_I(inode);
3985 pgoff_t page_idx = 0, last_idx;
3986 unsigned int blk_per_seg = sbi->blocks_per_seg;
3987 int cluster_size = F2FS_I(inode)->i_cluster_size;
3988 int count, ret;
3989
3990 if (!f2fs_sb_has_compression(sbi) ||
3991 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
3992 return -EOPNOTSUPP;
3993
3994 if (!(filp->f_mode & FMODE_WRITE))
3995 return -EBADF;
3996
3997 if (!f2fs_compressed_file(inode))
3998 return -EINVAL;
3999
4000 f2fs_balance_fs(F2FS_I_SB(inode), true);
4001
4002 file_start_write(filp);
4003 inode_lock(inode);
4004
4005 if (!f2fs_is_compress_backend_ready(inode)) {
4006 ret = -EOPNOTSUPP;
4007 goto out;
4008 }
4009
4010 if (f2fs_is_mmap_file(inode)) {
4011 ret = -EBUSY;
4012 goto out;
4013 }
4014
4015 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4016 if (ret)
4017 goto out;
4018
4019 if (!atomic_read(&fi->i_compr_blocks))
4020 goto out;
4021
4022 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4023
4024 count = last_idx - page_idx;
4025 while (count) {
4026 int len = min(cluster_size, count);
4027
4028 ret = redirty_blocks(inode, page_idx, len);
4029 if (ret < 0)
4030 break;
4031
4032 if (get_dirty_pages(inode) >= blk_per_seg)
4033 filemap_fdatawrite(inode->i_mapping);
4034
4035 count -= len;
4036 page_idx += len;
4037 }
4038
4039 if (!ret)
4040 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4041 LLONG_MAX);
4042
4043 if (ret)
Joe Perches833dcd32021-05-26 13:05:36 -07004044 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4045 __func__, ret);
Daeho Jeong5fdb3222020-12-03 15:56:15 +09004046out:
4047 inode_unlock(inode);
4048 file_end_write(filp);
4049
4050 return ret;
4051}
4052
4053static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4054{
4055 struct inode *inode = file_inode(filp);
4056 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4057 pgoff_t page_idx = 0, last_idx;
4058 unsigned int blk_per_seg = sbi->blocks_per_seg;
4059 int cluster_size = F2FS_I(inode)->i_cluster_size;
4060 int count, ret;
4061
4062 if (!f2fs_sb_has_compression(sbi) ||
4063 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4064 return -EOPNOTSUPP;
4065
4066 if (!(filp->f_mode & FMODE_WRITE))
4067 return -EBADF;
4068
4069 if (!f2fs_compressed_file(inode))
4070 return -EINVAL;
4071
4072 f2fs_balance_fs(F2FS_I_SB(inode), true);
4073
4074 file_start_write(filp);
4075 inode_lock(inode);
4076
4077 if (!f2fs_is_compress_backend_ready(inode)) {
4078 ret = -EOPNOTSUPP;
4079 goto out;
4080 }
4081
4082 if (f2fs_is_mmap_file(inode)) {
4083 ret = -EBUSY;
4084 goto out;
4085 }
4086
4087 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4088 if (ret)
4089 goto out;
4090
4091 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4092
4093 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4094
4095 count = last_idx - page_idx;
4096 while (count) {
4097 int len = min(cluster_size, count);
4098
4099 ret = redirty_blocks(inode, page_idx, len);
4100 if (ret < 0)
4101 break;
4102
4103 if (get_dirty_pages(inode) >= blk_per_seg)
4104 filemap_fdatawrite(inode->i_mapping);
4105
4106 count -= len;
4107 page_idx += len;
4108 }
4109
4110 if (!ret)
4111 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4112 LLONG_MAX);
4113
4114 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4115
4116 if (ret)
Joe Perches833dcd32021-05-26 13:05:36 -07004117 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4118 __func__, ret);
Daeho Jeong5fdb3222020-12-03 15:56:15 +09004119out:
4120 inode_unlock(inode);
4121 file_end_write(filp);
4122
4123 return ret;
4124}
4125
Chao Yu34178b1b2020-11-10 09:24:37 +08004126static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004127{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004128 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07004129 case FS_IOC_GETVERSION:
Chao Yud49f3e82015-01-23 20:36:04 +08004130 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07004131 case F2FS_IOC_START_ATOMIC_WRITE:
4132 return f2fs_ioc_start_atomic_write(filp);
4133 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4134 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07004135 case F2FS_IOC_START_VOLATILE_WRITE:
4136 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08004137 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4138 return f2fs_ioc_release_volatile_write(filp);
4139 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4140 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08004141 case F2FS_IOC_SHUTDOWN:
4142 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07004143 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07004144 return f2fs_ioc_fitrim(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004145 case FS_IOC_SET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07004146 return f2fs_ioc_set_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004147 case FS_IOC_GET_ENCRYPTION_POLICY:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07004148 return f2fs_ioc_get_encryption_policy(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004149 case FS_IOC_GET_ENCRYPTION_PWSALT:
Jaegeuk Kimf424f662015-04-20 15:19:06 -07004150 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07004151 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4152 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4153 case FS_IOC_ADD_ENCRYPTION_KEY:
4154 return f2fs_ioc_add_encryption_key(filp, arg);
4155 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4156 return f2fs_ioc_remove_encryption_key(filp, arg);
4157 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4158 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4159 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4160 return f2fs_ioc_get_encryption_key_status(filp, arg);
Eric Biggersee446e12020-03-14 13:50:51 -07004161 case FS_IOC_GET_ENCRYPTION_NONCE:
4162 return f2fs_ioc_get_encryption_nonce(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08004163 case F2FS_IOC_GARBAGE_COLLECT:
4164 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07004165 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4166 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08004167 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08004168 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08004169 case F2FS_IOC_DEFRAGMENT:
4170 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004171 case F2FS_IOC_MOVE_RANGE:
4172 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07004173 case F2FS_IOC_FLUSH_DEVICE:
4174 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004175 case F2FS_IOC_GET_FEATURES:
4176 return f2fs_ioc_get_features(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004177 case F2FS_IOC_GET_PIN_FILE:
4178 return f2fs_ioc_get_pin_file(filp, arg);
4179 case F2FS_IOC_SET_PIN_FILE:
4180 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08004181 case F2FS_IOC_PRECACHE_EXTENTS:
4182 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004183 case F2FS_IOC_RESIZE_FS:
4184 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07004185 case FS_IOC_ENABLE_VERITY:
4186 return f2fs_ioc_enable_verity(filp, arg);
4187 case FS_IOC_MEASURE_VERITY:
4188 return f2fs_ioc_measure_verity(filp, arg);
Eric Biggerse17fe652021-01-15 10:18:16 -08004189 case FS_IOC_READ_VERITY_METADATA:
4190 return f2fs_ioc_read_verity_metadata(filp, arg);
Eric Biggers3357af82020-07-14 15:18:12 -07004191 case FS_IOC_GETFSLABEL:
4192 return f2fs_ioc_getfslabel(filp, arg);
4193 case FS_IOC_SETFSLABEL:
4194 return f2fs_ioc_setfslabel(filp, arg);
Chao Yu439dfb12020-02-21 18:09:21 +08004195 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4196 return f2fs_get_compress_blocks(filp, arg);
Chao Yuef8d5632020-03-06 15:36:09 +08004197 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4198 return f2fs_release_compress_blocks(filp, arg);
Chao Yuc75488f2020-03-06 14:35:33 +08004199 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4200 return f2fs_reserve_compress_blocks(filp, arg);
Daeho Jeong9af84642020-07-21 12:21:11 +09004201 case F2FS_IOC_SEC_TRIM_FILE:
4202 return f2fs_sec_trim_file(filp, arg);
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09004203 case F2FS_IOC_GET_COMPRESS_OPTION:
4204 return f2fs_ioc_get_compress_option(filp, arg);
Daeho Jeonge1e8deb2020-10-30 13:10:35 +09004205 case F2FS_IOC_SET_COMPRESS_OPTION:
4206 return f2fs_ioc_set_compress_option(filp, arg);
Daeho Jeong5fdb3222020-12-03 15:56:15 +09004207 case F2FS_IOC_DECOMPRESS_FILE:
4208 return f2fs_ioc_decompress_file(filp, arg);
4209 case F2FS_IOC_COMPRESS_FILE:
4210 return f2fs_ioc_compress_file(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004211 default:
4212 return -ENOTTY;
4213 }
4214}
4215
Chao Yu34178b1b2020-11-10 09:24:37 +08004216long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4217{
4218 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4219 return -EIO;
4220 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4221 return -ENOSPC;
4222
4223 return __f2fs_ioctl(filp, cmd, arg);
4224}
4225
Chao Yu4c8ff702019-11-01 18:07:14 +08004226static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4227{
4228 struct file *file = iocb->ki_filp;
4229 struct inode *inode = file_inode(file);
Chao Yu8b83ac82020-04-16 18:16:56 +08004230 int ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004231
4232 if (!f2fs_is_compress_backend_ready(inode))
4233 return -EOPNOTSUPP;
4234
Chao Yu8b83ac82020-04-16 18:16:56 +08004235 ret = generic_file_read_iter(iocb, iter);
4236
4237 if (ret > 0)
4238 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4239
4240 return ret;
Chao Yu4c8ff702019-11-01 18:07:14 +08004241}
4242
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004243static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4244{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004245 struct file *file = iocb->ki_filp;
4246 struct inode *inode = file_inode(file);
4247 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004248
Chao Yu126ce722019-04-02 18:52:22 +08004249 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4250 ret = -EIO;
4251 goto out;
4252 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02004253
Chao Yu7bd29352020-02-24 19:20:15 +08004254 if (!f2fs_is_compress_backend_ready(inode)) {
4255 ret = -EOPNOTSUPP;
4256 goto out;
4257 }
Chao Yu4c8ff702019-11-01 18:07:14 +08004258
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004259 if (iocb->ki_flags & IOCB_NOWAIT) {
4260 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08004261 ret = -EAGAIN;
4262 goto out;
4263 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05004264 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004265 inode_lock(inode);
4266 }
4267
Chao Yue0fcd012020-12-26 18:07:01 +08004268 if (unlikely(IS_IMMUTABLE(inode))) {
4269 ret = -EPERM;
4270 goto unlock;
4271 }
4272
Jaegeuk Kimc6140412021-05-25 11:39:35 -07004273 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4274 ret = -EPERM;
4275 goto unlock;
4276 }
4277
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004278 ret = generic_write_checks(iocb, from);
4279 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004280 bool preallocated = false;
4281 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004282 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004283
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004284 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4285 set_inode_flag(inode, FI_NO_PREALLOC);
4286
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004287 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4288 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09004289 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08004290 f2fs_has_inline_data(inode) ||
4291 f2fs_force_buffered_io(inode, iocb, from)) {
4292 clear_inode_flag(inode, FI_NO_PREALLOC);
4293 inode_unlock(inode);
4294 ret = -EAGAIN;
4295 goto out;
4296 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004297 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07004298 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004299
4300 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4301 goto write;
4302
4303 if (iocb->ki_flags & IOCB_DIRECT) {
4304 /*
4305 * Convert inline data for Direct I/O before entering
4306 * f2fs_direct_IO().
4307 */
4308 err = f2fs_convert_inline_inode(inode);
4309 if (err)
4310 goto out_err;
4311 /*
4312 * If force_buffere_io() is true, we have to allocate
4313 * blocks all the time, since f2fs_direct_IO will fall
4314 * back to buffered IO.
4315 */
4316 if (!f2fs_force_buffered_io(inode, iocb, from) &&
Eric Biggers6de86872021-07-16 09:39:12 -05004317 f2fs_lfs_mode(F2FS_I_SB(inode)))
Jaegeuk Kim47501f82019-11-26 15:01:42 -08004318 goto write;
4319 }
4320 preallocated = true;
4321 target_size = iocb->ki_pos + iov_iter_count(from);
4322
4323 err = f2fs_preallocate_blocks(iocb, from);
4324 if (err) {
4325out_err:
4326 clear_inode_flag(inode, FI_NO_PREALLOC);
4327 inode_unlock(inode);
4328 ret = err;
4329 goto out;
4330 }
4331write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08004332 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08004333 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08004334
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004335 /* if we couldn't write data, we should deallocate blocks. */
Chao Yua303b0a2021-04-01 11:01:53 +08004336 if (preallocated && i_size_read(inode) < target_size) {
4337 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4338 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004339 f2fs_truncate(inode);
Chao Yua303b0a2021-04-01 11:01:53 +08004340 up_write(&F2FS_I(inode)->i_mmap_sem);
4341 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4342 }
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07004343
Chao Yub0af6d42017-08-02 23:21:48 +08004344 if (ret > 0)
4345 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004346 }
Chao Yue0fcd012020-12-26 18:07:01 +08004347unlock:
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004348 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08004349out:
4350 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4351 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07004352 if (ret > 0)
4353 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08004354 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004355}
4356
Daeho Jeong0f6b56e2021-08-02 21:22:45 -07004357static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4358 int advice)
4359{
4360 struct inode *inode;
4361 struct address_space *mapping;
4362 struct backing_dev_info *bdi;
4363
4364 if (advice == POSIX_FADV_SEQUENTIAL) {
4365 inode = file_inode(filp);
4366 if (S_ISFIFO(inode->i_mode))
4367 return -ESPIPE;
4368
4369 mapping = filp->f_mapping;
4370 if (!mapping || len < 0)
4371 return -EINVAL;
4372
4373 bdi = inode_to_bdi(mapping->host);
4374 filp->f_ra.ra_pages = bdi->ra_pages *
4375 F2FS_I_SB(inode)->seq_file_ra_mul;
4376 spin_lock(&filp->f_lock);
4377 filp->f_mode &= ~FMODE_RANDOM;
4378 spin_unlock(&filp->f_lock);
4379 return 0;
4380 }
4381
4382 return generic_fadvise(filp, offset, len, advice);
4383}
4384
Namjae Jeone9750822013-02-04 23:41:41 +09004385#ifdef CONFIG_COMPAT
Chao Yu34178b1b2020-11-10 09:24:37 +08004386struct compat_f2fs_gc_range {
4387 u32 sync;
4388 compat_u64 start;
4389 compat_u64 len;
4390};
4391#define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4392 struct compat_f2fs_gc_range)
4393
4394static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4395{
4396 struct compat_f2fs_gc_range __user *urange;
4397 struct f2fs_gc_range range;
4398 int err;
4399
4400 urange = compat_ptr(arg);
4401 err = get_user(range.sync, &urange->sync);
4402 err |= get_user(range.start, &urange->start);
4403 err |= get_user(range.len, &urange->len);
4404 if (err)
4405 return -EFAULT;
4406
4407 return __f2fs_ioc_gc_range(file, &range);
4408}
4409
4410struct compat_f2fs_move_range {
4411 u32 dst_fd;
4412 compat_u64 pos_in;
4413 compat_u64 pos_out;
4414 compat_u64 len;
4415};
4416#define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4417 struct compat_f2fs_move_range)
4418
4419static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4420{
4421 struct compat_f2fs_move_range __user *urange;
4422 struct f2fs_move_range range;
4423 int err;
4424
4425 urange = compat_ptr(arg);
4426 err = get_user(range.dst_fd, &urange->dst_fd);
4427 err |= get_user(range.pos_in, &urange->pos_in);
4428 err |= get_user(range.pos_out, &urange->pos_out);
4429 err |= get_user(range.len, &urange->len);
4430 if (err)
4431 return -EFAULT;
4432
4433 return __f2fs_ioc_move_range(file, &range);
4434}
4435
Namjae Jeone9750822013-02-04 23:41:41 +09004436long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4437{
Chao Yu34178b1b2020-11-10 09:24:37 +08004438 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4439 return -EIO;
4440 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4441 return -ENOSPC;
4442
Namjae Jeone9750822013-02-04 23:41:41 +09004443 switch (cmd) {
Eric Biggers3357af82020-07-14 15:18:12 -07004444 case FS_IOC32_GETVERSION:
4445 cmd = FS_IOC_GETVERSION;
Chao Yu04ef4b62015-11-10 18:44:20 +08004446 break;
Chao Yu34178b1b2020-11-10 09:24:37 +08004447 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4448 return f2fs_compat_ioc_gc_range(file, arg);
4449 case F2FS_IOC32_MOVE_RANGE:
4450 return f2fs_compat_ioc_move_range(file, arg);
Chao Yu04ef4b62015-11-10 18:44:20 +08004451 case F2FS_IOC_START_ATOMIC_WRITE:
4452 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4453 case F2FS_IOC_START_VOLATILE_WRITE:
4454 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4455 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4456 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02004457 case FITRIM:
Eric Biggers3357af82020-07-14 15:18:12 -07004458 case FS_IOC_SET_ENCRYPTION_POLICY:
4459 case FS_IOC_GET_ENCRYPTION_PWSALT:
4460 case FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07004461 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4462 case FS_IOC_ADD_ENCRYPTION_KEY:
4463 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4464 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4465 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Eric Biggersee446e12020-03-14 13:50:51 -07004466 case FS_IOC_GET_ENCRYPTION_NONCE:
Chao Yu04ef4b62015-11-10 18:44:20 +08004467 case F2FS_IOC_GARBAGE_COLLECT:
4468 case F2FS_IOC_WRITE_CHECKPOINT:
4469 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kime066b832017-04-13 15:17:00 -07004470 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07004471 case F2FS_IOC_GET_FEATURES:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08004472 case F2FS_IOC_GET_PIN_FILE:
4473 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08004474 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08004475 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07004476 case FS_IOC_ENABLE_VERITY:
4477 case FS_IOC_MEASURE_VERITY:
Eric Biggerse17fe652021-01-15 10:18:16 -08004478 case FS_IOC_READ_VERITY_METADATA:
Eric Biggers3357af82020-07-14 15:18:12 -07004479 case FS_IOC_GETFSLABEL:
4480 case FS_IOC_SETFSLABEL:
Chao Yu439dfb12020-02-21 18:09:21 +08004481 case F2FS_IOC_GET_COMPRESS_BLOCKS:
Chao Yuef8d5632020-03-06 15:36:09 +08004482 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
Chao Yuc75488f2020-03-06 14:35:33 +08004483 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
Daeho Jeong9af84642020-07-21 12:21:11 +09004484 case F2FS_IOC_SEC_TRIM_FILE:
Daeho Jeong9e2a5f82020-10-30 13:10:34 +09004485 case F2FS_IOC_GET_COMPRESS_OPTION:
Daeho Jeonge1e8deb2020-10-30 13:10:35 +09004486 case F2FS_IOC_SET_COMPRESS_OPTION:
Daeho Jeong5fdb3222020-12-03 15:56:15 +09004487 case F2FS_IOC_DECOMPRESS_FILE:
4488 case F2FS_IOC_COMPRESS_FILE:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07004489 break;
Namjae Jeone9750822013-02-04 23:41:41 +09004490 default:
4491 return -ENOIOCTLCMD;
4492 }
Chao Yu34178b1b2020-11-10 09:24:37 +08004493 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Namjae Jeone9750822013-02-04 23:41:41 +09004494}
4495#endif
4496
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004497const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08004498 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08004499 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07004500 .write_iter = f2fs_file_write_iter,
4501 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08004502 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004503 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07004504 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004505 .fsync = f2fs_sync_file,
4506 .fallocate = f2fs_fallocate,
4507 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09004508#ifdef CONFIG_COMPAT
4509 .compat_ioctl = f2fs_compat_ioctl,
4510#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004511 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04004512 .splice_write = iter_file_splice_write,
Daeho Jeong0f6b56e2021-08-02 21:22:45 -07004513 .fadvise = f2fs_file_fadvise,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09004514};