blob: 344e0bd638e51108733e2b10575c645f061f611d [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/stat.h>
11#include <linux/buffer_head.h>
12#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090013#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090014#include <linux/falloc.h>
15#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090016#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/uaccess.h>
18#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090019#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080020#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070021#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070022#include <linux/file.h>
Chao Yu4507847c2019-07-17 17:06:11 +080023#include <linux/nls.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090024
25#include "f2fs.h"
26#include "node.h"
27#include "segment.h"
28#include "xattr.h"
29#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080030#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080031#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090032#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090033
Souptick Joarderea4d4792018-04-15 01:40:02 +053034static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080035{
36 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053037 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080038
39 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053040 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080041 up_read(&F2FS_I(inode)->i_mmap_sem);
42
Chao Yud7648342019-04-15 15:22:19 +080043 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
44
Souptick Joarderea4d4792018-04-15 01:40:02 +053045 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080046}
47
Souptick Joarderea4d4792018-04-15 01:40:02 +053048static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049{
50 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080051 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu39a86952018-09-27 18:33:18 +080053 struct dnode_of_data dn = { .node_changed = false };
Gu Zhenge4795562013-09-27 18:08:30 +080054 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090055
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020056 if (unlikely(f2fs_cp_error(sbi))) {
57 err = -EIO;
58 goto err;
59 }
Chao Yu955ebcd2019-07-22 17:57:05 +080060 err = f2fs_is_checkpoint_ready(sbi);
61 if (err)
62 goto err;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020063
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090064 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070065
66 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070067
Dave Jiang11bac802017-02-24 14:56:41 -080068 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080069 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090070 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090071 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090072 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090073 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090074 unlock_page(page);
75 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080076 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090077 }
78
Chao Yu39a86952018-09-27 18:33:18 +080079 /* block allocation */
80 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
81 set_new_dnode(&dn, inode, NULL, NULL, 0);
82 err = f2fs_get_block(&dn, page->index);
83 f2fs_put_dnode(&dn);
84 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
85 if (err) {
86 unlock_page(page);
87 goto out_sem;
88 }
89
90 /* fill the page */
Chao Yubae0ee72018-12-25 17:43:42 +080091 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +080092
93 /* wait for GCed page writeback via META_MAPPING */
94 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
95
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090096 /*
97 * check to see if the page is mapped already (no holes)
98 */
99 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800100 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900101
102 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300103 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800104 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900105 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900106
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300107 offset = i_size_read(inode) & ~PAGE_MASK;
108 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900109 }
110 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700111 if (!PageUptodate(page))
112 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900113
Chao Yub0af6d42017-08-02 23:21:48 +0800114 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530115 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800116
Jaegeuk Kime943a102013-10-25 14:26:31 +0900117 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800118out_sem:
119 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800120
121 f2fs_balance_fs(sbi, dn.node_changed);
122
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900123 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200124err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900125 return block_page_mkwrite_return(err);
126}
127
128static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800129 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700130 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900131 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900132};
133
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900134static int get_parent_ino(struct inode *inode, nid_t *pino)
135{
136 struct dentry *dentry;
137
138 inode = igrab(inode);
139 dentry = d_find_any_alias(inode);
140 iput(inode);
141 if (!dentry)
142 return 0;
143
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900144 *pino = parent_ino(dentry);
145 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900146 return 1;
147}
148
Chao Yua5fd5052017-11-06 22:51:45 +0800149static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800150{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700151 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800152 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800153
Chao Yua5fd5052017-11-06 22:51:45 +0800154 if (!S_ISREG(inode->i_mode))
155 cp_reason = CP_NON_REGULAR;
156 else if (inode->i_nlink != 1)
157 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700158 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800159 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800160 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800161 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800162 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800163 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800164 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800165 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700166 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800167 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800168 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800169 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800170 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800171 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
172 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
173 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800174 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800175
Chao Yua5fd5052017-11-06 22:51:45 +0800176 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800177}
178
Changman Lee9c7bb702014-12-08 15:29:40 +0900179static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
180{
181 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
182 bool ret = false;
183 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800184 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900185 ret = true;
186 f2fs_put_page(i, 0);
187 return ret;
188}
189
Changman Lee51455b12014-12-08 15:29:41 +0900190static void try_to_fix_pino(struct inode *inode)
191{
192 struct f2fs_inode_info *fi = F2FS_I(inode);
193 nid_t pino;
194
195 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900196 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
197 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700198 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900199 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900200 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700201 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900202}
203
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700204static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
205 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900206{
207 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700208 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700209 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900210 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800211 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900212 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900213 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900214 .nr_to_write = LONG_MAX,
215 .for_reclaim = 0,
216 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800217 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900218
Daniel Rosenberg43549942018-08-20 19:21:43 -0700219 if (unlikely(f2fs_readonly(inode->i_sb) ||
220 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900221 return 0;
222
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900223 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700224
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800225 if (S_ISDIR(inode->i_mode))
226 goto go_write;
227
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700228 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800229 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700230 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400231 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700232 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700233
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900234 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800235 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900236 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900237 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900238
Changman Lee9c7bb702014-12-08 15:29:40 +0900239 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800240 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700241 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900242 goto go_write;
243 }
244
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700245 /*
246 * if there is no written data, don't waste time to write recovery info.
247 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700248 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800249 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700250
Changman Lee9c7bb702014-12-08 15:29:40 +0900251 /* it may call write_inode just prior to fsync */
252 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700253 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700254
Jaegeuk Kim91942322016-05-20 10:13:22 -0700255 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800256 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700257 goto flush_out;
258 goto out;
259 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700260go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900261 /*
262 * Both of fdatasync() and fsync() are able to be recovered from
263 * sudden-power-off.
264 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700265 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800266 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700267 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900268
Chao Yua5fd5052017-11-06 22:51:45 +0800269 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900270 /* all the dirty node pages should be flushed for POR */
271 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900272
Changman Lee51455b12014-12-08 15:29:41 +0900273 /*
274 * We've secured consistency through sync_fs. Following pino
275 * will be used only for fsynced inodes after checkpoint.
276 */
277 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700278 clear_inode_flag(inode, FI_APPEND_WRITE);
279 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900280 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900281 }
Changman Lee51455b12014-12-08 15:29:41 +0900282sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800283 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800284 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800285 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700286 if (ret)
287 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900288
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800289 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800290 if (unlikely(f2fs_cp_error(sbi))) {
291 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800292 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800293 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800294
Chao Yu4d57b862018-05-30 00:20:41 +0800295 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700296 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900297 f2fs_write_inode(inode, NULL);
298 goto sync_nodes;
299 }
300
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700301 /*
302 * If it's atomic_write, it's just fine to keep write ordering. So
303 * here we don't need to wait for node write completion, since we use
304 * node chain which serializes node blocks. If one of node writes are
305 * reordered, we can see simply broken chain, resulting in stopping
306 * roll-forward recovery. It means we'll recover all or none node blocks
307 * given fsync mark.
308 */
309 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800310 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700311 if (ret)
312 goto out;
313 }
Changman Lee51455b12014-12-08 15:29:41 +0900314
315 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800316 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700317 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900318flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700319 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800320 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800321 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800322 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800323 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800324 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800325 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800326 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900327out:
Chao Yua5fd5052017-11-06 22:51:45 +0800328 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700329 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900330 return ret;
331}
332
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700333int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
334{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200335 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
336 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700337 return f2fs_do_sync_file(file, start, end, datasync, false);
338}
339
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900340static pgoff_t __get_first_dirty_index(struct address_space *mapping,
341 pgoff_t pgofs, int whence)
342{
Jan Kara8faab642017-11-15 17:34:55 -0800343 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900344 int nr_pages;
345
346 if (whence != SEEK_DATA)
347 return 0;
348
349 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800350 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
351 1, &page);
352 if (!nr_pages)
353 return ULONG_MAX;
354 pgofs = page->index;
355 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900356 return pgofs;
357}
358
Chao Yue1da7872018-06-05 17:44:11 +0800359static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
360 pgoff_t dirty, pgoff_t pgofs, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900361{
362 switch (whence) {
363 case SEEK_DATA:
364 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yu93770ab2019-04-15 15:26:32 +0800365 __is_valid_data_blkaddr(blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900366 return true;
367 break;
368 case SEEK_HOLE:
369 if (blkaddr == NULL_ADDR)
370 return true;
371 break;
372 }
373 return false;
374}
375
Chao Yu267378d2014-04-23 14:10:24 +0800376static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
377{
378 struct inode *inode = file->f_mapping->host;
379 loff_t maxbytes = inode->i_sb->s_maxbytes;
380 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900381 pgoff_t pgofs, end_offset, dirty;
382 loff_t data_ofs = offset;
383 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800384 int err = 0;
385
Al Viro59551022016-01-22 15:40:57 -0500386 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800387
388 isize = i_size_read(inode);
389 if (offset >= isize)
390 goto fail;
391
392 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800393 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800394 if (whence == SEEK_HOLE)
395 data_ofs = isize;
396 goto found;
397 }
398
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300399 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800400
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900401 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
402
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300403 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800404 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800405 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800406 if (err && err != -ENOENT) {
407 goto fail;
408 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900409 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800410 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800411 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800412 continue;
413 } else {
414 goto found;
415 }
416 }
417
Chao Yu81ca7352016-01-26 15:39:35 +0800418 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800419
420 /* find data/hole in dnode block */
421 for (; dn.ofs_in_node < end_offset;
422 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300423 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800424 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900425
Chao Yu7a2af762017-07-19 00:19:06 +0800426 blkaddr = datablock_addr(dn.inode,
427 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800428
Chao Yuc9b60782018-08-01 19:13:44 +0800429 if (__is_valid_data_blkaddr(blkaddr) &&
430 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800431 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800432 f2fs_put_dnode(&dn);
433 goto fail;
434 }
435
Chao Yue1da7872018-06-05 17:44:11 +0800436 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
437 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800438 f2fs_put_dnode(&dn);
439 goto found;
440 }
441 }
442 f2fs_put_dnode(&dn);
443 }
444
445 if (whence == SEEK_DATA)
446 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800447found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900448 if (whence == SEEK_HOLE && data_ofs > isize)
449 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500450 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800451 return vfs_setpos(file, data_ofs, maxbytes);
452fail:
Al Viro59551022016-01-22 15:40:57 -0500453 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800454 return -ENXIO;
455}
456
457static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
458{
459 struct inode *inode = file->f_mapping->host;
460 loff_t maxbytes = inode->i_sb->s_maxbytes;
461
462 switch (whence) {
463 case SEEK_SET:
464 case SEEK_CUR:
465 case SEEK_END:
466 return generic_file_llseek_size(file, offset, whence,
467 maxbytes, i_size_read(inode));
468 case SEEK_DATA:
469 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700470 if (offset < 0)
471 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800472 return f2fs_seek_block(file, offset, whence);
473 }
474
475 return -EINVAL;
476}
477
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900478static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
479{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700480 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800481 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700482
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200483 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
484 return -EIO;
485
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700486 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800487 err = f2fs_convert_inline_inode(inode);
488 if (err)
489 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700490
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900491 file_accessed(file);
492 vma->vm_ops = &f2fs_file_vm_ops;
493 return 0;
494}
495
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700496static int f2fs_file_open(struct inode *inode, struct file *filp)
497{
Eric Biggers2e168c82017-11-29 12:35:28 -0800498 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700499
Eric Biggers2e168c82017-11-29 12:35:28 -0800500 if (err)
501 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900502
503 filp->f_mode |= FMODE_NOWAIT;
504
Chao Yu0abd6752017-07-09 00:13:07 +0800505 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700506}
507
Chao Yu4d57b862018-05-30 00:20:41 +0800508void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900509{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700510 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900511 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800512 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900513 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800514 int base = 0;
515
516 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
517 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900518
Gu Zheng45590712013-07-15 17:57:38 +0800519 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800520 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900521
Chris Fries6c311ec2014-01-17 14:44:39 -0600522 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900523 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900524
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900525 if (blkaddr == NULL_ADDR)
526 continue;
527
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800528 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800529 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800530
531 if (__is_valid_data_blkaddr(blkaddr) &&
Chao Yu93770ab2019-04-15 15:26:32 +0800532 !f2fs_is_valid_blkaddr(sbi, blkaddr,
533 DATA_GENERIC_ENHANCE))
Chao Yuc9b60782018-08-01 19:13:44 +0800534 continue;
535
Chao Yu4d57b862018-05-30 00:20:41 +0800536 f2fs_invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700537 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700538 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900539 nr_free++;
540 }
Chao Yu19b2c302015-08-26 20:34:48 +0800541
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900542 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800543 pgoff_t fofs;
544 /*
545 * once we invalidate valid blkaddr in range [ofs, ofs + count],
546 * we will invalidate all blkaddr in the whole range.
547 */
Chao Yu4d57b862018-05-30 00:20:41 +0800548 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800549 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800550 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900551 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900552 }
553 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900554
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800555 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900556 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
557 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900558}
559
Chao Yu4d57b862018-05-30 00:20:41 +0800560void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900561{
Chao Yud02a6e62019-03-25 21:08:19 +0800562 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900563}
564
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800565static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700566 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900567{
youngjun yoo193bea12018-05-30 04:21:14 +0900568 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700570 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900571 struct page *page;
572
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700573 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700574 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900575
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700576 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700577 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700578 if (page && PageUptodate(page))
579 goto truncate_out;
580 f2fs_put_page(page, 1);
581 return 0;
582 }
583
Chao Yu4d57b862018-05-30 00:20:41 +0800584 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900585 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800586 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700587truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800588 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300589 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700590
591 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530592 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700593 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800594 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900595 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700596 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900597}
598
Chao Yuc42d28c2019-02-02 17:33:01 +0800599int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900600{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700601 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900602 struct dnode_of_data dn;
603 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800604 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700605 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800606 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900607
Namjae Jeon51dd6242013-04-20 01:28:52 +0900608 trace_f2fs_truncate_blocks_enter(inode, from);
609
Chao Yudf033ca2018-03-20 23:08:29 +0800610 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900611
Chao Yu09210c92016-05-05 19:13:03 +0800612 if (free_from >= sbi->max_file_blocks)
613 goto free_partial;
614
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700615 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800616 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800617
Chao Yu4d57b862018-05-30 00:20:41 +0800618 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700619 if (IS_ERR(ipage)) {
620 err = PTR_ERR(ipage);
621 goto out;
622 }
623
624 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800625 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700626 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800627 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700628 goto out;
629 }
630
631 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800632 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900633 if (err) {
634 if (err == -ENOENT)
635 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700636 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700637 }
638
Chao Yu81ca7352016-01-26 15:39:35 +0800639 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900640
641 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700642 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900643
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900644 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800645 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900646 free_from += count;
647 }
648
649 f2fs_put_dnode(&dn);
650free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800651 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800652out:
653 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800654 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800655free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700656 /* lastly zero out the first data page */
657 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800658 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900659
Namjae Jeon51dd6242013-04-20 01:28:52 +0900660 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900661 return err;
662}
663
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700664int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665{
Chao Yub0154892015-08-24 17:39:42 +0800666 int err;
667
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200668 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
669 return -EIO;
670
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900671 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
672 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800673 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900674
Namjae Jeon51dd6242013-04-20 01:28:52 +0900675 trace_f2fs_truncate(inode);
676
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800677 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
678 f2fs_show_injection_info(FAULT_TRUNCATE);
679 return -EIO;
680 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200681
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800682 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800683 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800684 err = f2fs_convert_inline_inode(inode);
685 if (err)
686 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800687 }
688
Chao Yuc42d28c2019-02-02 17:33:01 +0800689 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800690 if (err)
691 return err;
692
Deepa Dinamani078cd822016-09-14 07:48:04 -0700693 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700694 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800695 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900696}
697
David Howellsa528d352017-01-31 16:46:22 +0000698int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800699 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900700{
David Howellsa528d352017-01-31 16:46:22 +0000701 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800702 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800703 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800704 unsigned int flags;
705
Chao Yu1c1d35d2018-01-25 14:54:42 +0800706 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800707 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800708 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
709 stat->result_mask |= STATX_BTIME;
710 stat->btime.tv_sec = fi->i_crtime.tv_sec;
711 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
712 }
713
Eric Biggers36098552019-06-04 22:59:04 -0700714 flags = fi->i_flags;
Chao Yu59c84402018-04-03 15:08:17 +0800715 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800716 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530717 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800718 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800719 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800720 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800721 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800722 stat->attributes |= STATX_ATTR_NODUMP;
723
724 stat->attributes_mask |= (STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800725 STATX_ATTR_ENCRYPTED |
726 STATX_ATTR_IMMUTABLE |
727 STATX_ATTR_NODUMP);
728
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900729 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700730
731 /* we need to show initial sectors used for inline_data/dentries */
732 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
733 f2fs_has_inline_dentry(inode))
734 stat->blocks += (stat->size + 511) >> 9;
735
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900736 return 0;
737}
738
739#ifdef CONFIG_F2FS_FS_POSIX_ACL
740static void __setattr_copy(struct inode *inode, const struct iattr *attr)
741{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900742 unsigned int ia_valid = attr->ia_valid;
743
744 if (ia_valid & ATTR_UID)
745 inode->i_uid = attr->ia_uid;
746 if (ia_valid & ATTR_GID)
747 inode->i_gid = attr->ia_gid;
748 if (ia_valid & ATTR_ATIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700749 inode->i_atime = timespec64_trunc(attr->ia_atime,
750 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900751 if (ia_valid & ATTR_MTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700752 inode->i_mtime = timespec64_trunc(attr->ia_mtime,
753 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 if (ia_valid & ATTR_CTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700755 inode->i_ctime = timespec64_trunc(attr->ia_ctime,
756 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900757 if (ia_valid & ATTR_MODE) {
758 umode_t mode = attr->ia_mode;
759
760 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
761 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700762 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900763 }
764}
765#else
766#define __setattr_copy setattr_copy
767#endif
768
769int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
770{
David Howells2b0143b2015-03-17 22:25:59 +0000771 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900772 int err;
773
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200774 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
775 return -EIO;
776
Jan Kara31051c82016-05-26 16:55:18 +0200777 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900778 if (err)
779 return err;
780
Eric Biggers20bb2472017-11-29 12:35:32 -0800781 err = fscrypt_prepare_setattr(dentry, attr);
782 if (err)
783 return err;
784
Chao Yu0abd6752017-07-09 00:13:07 +0800785 if (is_quota_modification(inode, attr)) {
786 err = dquot_initialize(inode);
787 if (err)
788 return err;
789 }
790 if ((attr->ia_valid & ATTR_UID &&
791 !uid_eq(attr->ia_uid, inode->i_uid)) ||
792 (attr->ia_valid & ATTR_GID &&
793 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800794 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800795 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800796 if (err) {
797 set_sbi_flag(F2FS_I_SB(inode),
798 SBI_QUOTA_NEED_REPAIR);
799 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800800 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800801 }
802 /*
803 * update uid/gid under lock_op(), so that dquot and inode can
804 * be updated atomically.
805 */
806 if (attr->ia_valid & ATTR_UID)
807 inode->i_uid = attr->ia_uid;
808 if (attr->ia_valid & ATTR_GID)
809 inode->i_gid = attr->ia_gid;
810 f2fs_mark_inode_dirty_sync(inode, true);
811 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800812 }
813
Chao Yu09db6a22014-09-15 18:02:09 +0800814 if (attr->ia_valid & ATTR_SIZE) {
Chao Yua33c1502018-08-05 23:04:25 +0800815 bool to_smaller = (attr->ia_size <= i_size_read(inode));
Chao Yu0cab80e2015-12-01 11:36:16 +0800816
Chao Yua33c1502018-08-05 23:04:25 +0800817 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900818 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800819
820 truncate_setsize(inode, attr->ia_size);
821
822 if (to_smaller)
823 err = f2fs_truncate(inode);
824 /*
825 * do not trim all blocks after i_size if target size is
826 * larger than i_size.
827 */
Chao Yua33c1502018-08-05 23:04:25 +0800828 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900829 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800830
831 if (err)
832 return err;
833
834 if (!to_smaller) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800835 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800836 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800837 err = f2fs_convert_inline_inode(inode);
838 if (err)
839 return err;
840 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700841 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800842 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800843
Chao Yua0d00fa2017-10-09 17:55:19 +0800844 down_write(&F2FS_I(inode)->i_sem);
845 F2FS_I(inode)->last_disk_size = i_size_read(inode);
846 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900847 }
848
849 __setattr_copy(inode, attr);
850
851 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800852 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700853 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
854 inode->i_mode = F2FS_I(inode)->i_acl_mode;
855 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900856 }
857 }
858
Yunlei Hec0ed4402016-12-11 15:35:15 +0800859 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800860 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700861
862 /* inode change will produce dirty node pages flushed by checkpoint */
863 f2fs_balance_fs(F2FS_I_SB(inode), true);
864
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900865 return err;
866}
867
868const struct inode_operations f2fs_file_inode_operations = {
869 .getattr = f2fs_getattr,
870 .setattr = f2fs_setattr,
871 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800872 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900873#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900874 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900875#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900876 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900877};
878
Chao Yu63943282015-08-07 18:36:06 +0800879static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900880 loff_t start, loff_t len)
881{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700882 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900883 struct page *page;
884
885 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800886 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900887
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800888 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900889
Gu Zhenge4795562013-09-27 18:08:30 +0800890 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800891 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800892 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900893
Chao Yu63943282015-08-07 18:36:06 +0800894 if (IS_ERR(page))
895 return PTR_ERR(page);
896
Chao Yubae0ee72018-12-25 17:43:42 +0800897 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +0800898 zero_user(page, start, len);
899 set_page_dirty(page);
900 f2fs_put_page(page, 1);
901 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900902}
903
Chao Yu4d57b862018-05-30 00:20:41 +0800904int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900905{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900906 int err;
907
Chao Yuea587112015-09-17 20:22:44 +0800908 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900909 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800910 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900911
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900912 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800913 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900914 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800915 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +0800916 pg_start = f2fs_get_next_page_offset(&dn,
917 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900918 continue;
Chao Yuea587112015-09-17 20:22:44 +0800919 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900920 return err;
921 }
922
Chao Yu81ca7352016-01-26 15:39:35 +0800923 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800924 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
925
926 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
927
Chao Yu4d57b862018-05-30 00:20:41 +0800928 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900929 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800930
931 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900932 }
933 return 0;
934}
935
Chao Yua66c7b22013-11-22 16:52:50 +0800936static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900937{
938 pgoff_t pg_start, pg_end;
939 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800940 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900941
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800942 ret = f2fs_convert_inline_inode(inode);
943 if (ret)
944 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800945
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300946 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
947 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900948
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300949 off_start = offset & (PAGE_SIZE - 1);
950 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900951
952 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800953 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900954 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800955 if (ret)
956 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900957 } else {
Chao Yu63943282015-08-07 18:36:06 +0800958 if (off_start) {
959 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300960 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800961 if (ret)
962 return ret;
963 }
964 if (off_end) {
965 ret = fill_zero(inode, pg_end, 0, off_end);
966 if (ret)
967 return ret;
968 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900969
970 if (pg_start < pg_end) {
971 struct address_space *mapping = inode->i_mapping;
972 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700973 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500974
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800975 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900976
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300977 blk_start = (loff_t)pg_start << PAGE_SHIFT;
978 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +0800979
Chao Yua33c1502018-08-05 23:04:25 +0800980 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900981 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800982
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900983 truncate_inode_pages_range(mapping, blk_start,
984 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900985
Gu Zhenge4795562013-09-27 18:08:30 +0800986 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800987 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800988 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +0800989
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800990 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900991 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900992 }
993 }
994
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900995 return ret;
996}
997
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700998static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
999 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001000{
1001 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1002 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001003 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001004
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001005next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001006 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001007 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001008 if (ret && ret != -ENOENT) {
1009 return ret;
1010 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001011 if (dn.max_level == 0)
1012 return -ENOENT;
Chao Yud02a6e62019-03-25 21:08:19 +08001013 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) - dn.ofs_in_node,
1014 len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001015 blkaddr += done;
1016 do_replace += done;
1017 goto next;
1018 }
1019
1020 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1021 dn.ofs_in_node, len);
1022 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001023 *blkaddr = datablock_addr(dn.inode,
1024 dn.node_page, dn.ofs_in_node);
Chao Yu93770ab2019-04-15 15:26:32 +08001025
1026 if (__is_valid_data_blkaddr(*blkaddr) &&
1027 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1028 DATA_GENERIC_ENHANCE)) {
1029 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001030 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001031 }
1032
Chao Yu4d57b862018-05-30 00:20:41 +08001033 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001034
1035 if (test_opt(sbi, LFS)) {
1036 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001037 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001038 }
1039
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001040 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001041 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001042 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001043 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001044 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001045 f2fs_put_dnode(&dn);
1046next:
1047 len -= done;
1048 off += done;
1049 if (len)
1050 goto next_dnode;
1051 return 0;
1052}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001053
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001054static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1055 int *do_replace, pgoff_t off, int len)
1056{
1057 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1058 struct dnode_of_data dn;
1059 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001060
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001061 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1062 if (*do_replace == 0)
1063 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001064
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001065 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001066 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001067 if (ret) {
1068 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001069 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001070 } else {
1071 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001072 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001073 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001074 }
1075 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001076}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001077
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001078static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1079 block_t *blkaddr, int *do_replace,
1080 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1081{
1082 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1083 pgoff_t i = 0;
1084 int ret;
1085
1086 while (i < len) {
1087 if (blkaddr[i] == NULL_ADDR && !full) {
1088 i++;
1089 continue;
1090 }
1091
1092 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1093 struct dnode_of_data dn;
1094 struct node_info ni;
1095 size_t new_size;
1096 pgoff_t ilen;
1097
1098 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001099 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001100 if (ret)
1101 return ret;
1102
Chao Yu77357302018-07-17 00:02:17 +08001103 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1104 if (ret) {
1105 f2fs_put_dnode(&dn);
1106 return ret;
1107 }
1108
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001109 ilen = min((pgoff_t)
1110 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1111 dn.ofs_in_node, len - i);
1112 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001113 dn.data_blkaddr = datablock_addr(dn.inode,
1114 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001115 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001116
1117 if (do_replace[i]) {
1118 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001119 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001120 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001121 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001122 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1123 blkaddr[i], ni.version, true, false);
1124
1125 do_replace[i] = 0;
1126 }
1127 dn.ofs_in_node++;
1128 i++;
1129 new_size = (dst + i) << PAGE_SHIFT;
1130 if (dst_inode->i_size < new_size)
1131 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001132 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001133
1134 f2fs_put_dnode(&dn);
1135 } else {
1136 struct page *psrc, *pdst;
1137
Chao Yu4d57b862018-05-30 00:20:41 +08001138 psrc = f2fs_get_lock_data_page(src_inode,
1139 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001140 if (IS_ERR(psrc))
1141 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001142 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001143 true);
1144 if (IS_ERR(pdst)) {
1145 f2fs_put_page(psrc, 1);
1146 return PTR_ERR(pdst);
1147 }
1148 f2fs_copy_page(psrc, pdst);
1149 set_page_dirty(pdst);
1150 f2fs_put_page(pdst, 1);
1151 f2fs_put_page(psrc, 1);
1152
Chao Yu4d57b862018-05-30 00:20:41 +08001153 ret = f2fs_truncate_hole(src_inode,
1154 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001155 if (ret)
1156 return ret;
1157 i++;
1158 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001159 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001160 return 0;
1161}
1162
1163static int __exchange_data_block(struct inode *src_inode,
1164 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001165 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001166{
1167 block_t *src_blkaddr;
1168 int *do_replace;
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001169 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001170 int ret;
1171
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001172 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001173 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001174
Chao Yu628b3d12017-11-30 19:28:18 +08001175 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001176 array_size(olen, sizeof(block_t)),
1177 GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001178 if (!src_blkaddr)
1179 return -ENOMEM;
1180
Chao Yu628b3d12017-11-30 19:28:18 +08001181 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001182 array_size(olen, sizeof(int)),
1183 GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001184 if (!do_replace) {
1185 kvfree(src_blkaddr);
1186 return -ENOMEM;
1187 }
1188
1189 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1190 do_replace, src, olen);
1191 if (ret)
1192 goto roll_back;
1193
1194 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1195 do_replace, src, dst, olen, full);
1196 if (ret)
1197 goto roll_back;
1198
1199 src += olen;
1200 dst += olen;
1201 len -= olen;
1202
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001203 kvfree(src_blkaddr);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001204 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001205 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001206 return 0;
1207
1208roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001209 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001210 kvfree(src_blkaddr);
1211 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001212 return ret;
1213}
1214
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001215static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001216{
1217 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001218 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001219 pgoff_t start = offset >> PAGE_SHIFT;
1220 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001221 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001222
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001223 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001224
1225 /* avoid gc operation during block exchange */
1226 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1227 down_write(&F2FS_I(inode)->i_mmap_sem);
1228
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001229 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001230 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001231 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001232 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1233 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001234
1235 up_write(&F2FS_I(inode)->i_mmap_sem);
1236 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001237 return ret;
1238}
1239
1240static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1241{
Chao Yub4ace332015-05-06 13:09:46 +08001242 loff_t new_size;
1243 int ret;
1244
Chao Yub4ace332015-05-06 13:09:46 +08001245 if (offset + len >= i_size_read(inode))
1246 return -EINVAL;
1247
1248 /* collapse range should be aligned to block size of f2fs. */
1249 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1250 return -EINVAL;
1251
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001252 ret = f2fs_convert_inline_inode(inode);
1253 if (ret)
1254 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001255
Chao Yub4ace332015-05-06 13:09:46 +08001256 /* write out all dirty pages from offset */
1257 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1258 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001259 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001260
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001261 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001262 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001263 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001264
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001265 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001266 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001267 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1268 truncate_pagecache(inode, offset);
1269
Chao Yub4ace332015-05-06 13:09:46 +08001270 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001271 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001272
Chao Yuc42d28c2019-02-02 17:33:01 +08001273 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001274 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001275 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001276 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001277 return ret;
1278}
1279
Chao Yu6e961942016-05-09 19:56:31 +08001280static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1281 pgoff_t end)
1282{
1283 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1284 pgoff_t index = start;
1285 unsigned int ofs_in_node = dn->ofs_in_node;
1286 blkcnt_t count = 0;
1287 int ret;
1288
1289 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001290 if (datablock_addr(dn->inode, dn->node_page,
1291 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001292 count++;
1293 }
1294
1295 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001296 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001297 if (ret)
1298 return ret;
1299
1300 dn->ofs_in_node = ofs_in_node;
1301 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001302 dn->data_blkaddr = datablock_addr(dn->inode,
1303 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001304 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001305 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001306 * allocation.
1307 */
1308 if (dn->data_blkaddr == NULL_ADDR) {
1309 ret = -ENOSPC;
1310 break;
1311 }
1312 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001313 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001314 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001315 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001316 }
1317 }
1318
1319 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1320
1321 return ret;
1322}
1323
Chao Yu75cd4e02015-05-06 13:11:13 +08001324static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1325 int mode)
1326{
1327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1328 struct address_space *mapping = inode->i_mapping;
1329 pgoff_t index, pg_start, pg_end;
1330 loff_t new_size = i_size_read(inode);
1331 loff_t off_start, off_end;
1332 int ret = 0;
1333
Chao Yu75cd4e02015-05-06 13:11:13 +08001334 ret = inode_newsize_ok(inode, (len + offset));
1335 if (ret)
1336 return ret;
1337
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001338 ret = f2fs_convert_inline_inode(inode);
1339 if (ret)
1340 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001341
1342 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1343 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001344 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001345
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001346 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1347 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001348
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001349 off_start = offset & (PAGE_SIZE - 1);
1350 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001351
1352 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001353 ret = fill_zero(inode, pg_start, off_start,
1354 off_end - off_start);
1355 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001356 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001357
Chao Yu75cd4e02015-05-06 13:11:13 +08001358 new_size = max_t(loff_t, new_size, offset + len);
1359 } else {
1360 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001361 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001362 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001363 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001364 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001365
Chao Yu75cd4e02015-05-06 13:11:13 +08001366 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001367 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001368 }
1369
Chao Yu6e961942016-05-09 19:56:31 +08001370 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001371 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001372 unsigned int end_offset;
1373 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001374
Chao Yuc70798532018-08-05 23:02:22 +08001375 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001376 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001377
1378 truncate_pagecache_range(inode,
1379 (loff_t)index << PAGE_SHIFT,
1380 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1381
Chao Yu75cd4e02015-05-06 13:11:13 +08001382 f2fs_lock_op(sbi);
1383
Chao Yu6e961942016-05-09 19:56:31 +08001384 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001385 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001386 if (ret) {
1387 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001388 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001389 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001390 goto out;
1391 }
1392
Chao Yu6e961942016-05-09 19:56:31 +08001393 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1394 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1395
1396 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001397 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001398
Chao Yu75cd4e02015-05-06 13:11:13 +08001399 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001400 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001401 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001402
1403 f2fs_balance_fs(sbi, dn.node_changed);
1404
Chao Yu6e961942016-05-09 19:56:31 +08001405 if (ret)
1406 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001407
Chao Yu6e961942016-05-09 19:56:31 +08001408 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001409 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001410 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001411 }
1412
1413 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001414 ret = fill_zero(inode, pg_end, 0, off_end);
1415 if (ret)
1416 goto out;
1417
Chao Yu75cd4e02015-05-06 13:11:13 +08001418 new_size = max_t(loff_t, new_size, offset + len);
1419 }
1420 }
1421
1422out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001423 if (new_size > i_size_read(inode)) {
1424 if (mode & FALLOC_FL_KEEP_SIZE)
1425 file_set_keep_isize(inode);
1426 else
1427 f2fs_i_size_write(inode, new_size);
1428 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001429 return ret;
1430}
1431
Chao Yuf62185d2015-05-28 19:16:57 +08001432static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1433{
1434 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001435 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001436 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001437 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001438
Chao Yuf62185d2015-05-28 19:16:57 +08001439 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001440 ret = inode_newsize_ok(inode, new_size);
1441 if (ret)
1442 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001443
1444 if (offset >= i_size_read(inode))
1445 return -EINVAL;
1446
1447 /* insert range should be aligned to block size of f2fs. */
1448 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1449 return -EINVAL;
1450
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001451 ret = f2fs_convert_inline_inode(inode);
1452 if (ret)
1453 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001454
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001455 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001456
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001457 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001458 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001459 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001460 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001461 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001462
1463 /* write out all dirty pages from offset */
1464 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1465 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001466 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001467
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001468 pg_start = offset >> PAGE_SHIFT;
1469 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001470 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001471 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001472
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001473 /* avoid gc operation during block exchange */
1474 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1475 down_write(&F2FS_I(inode)->i_mmap_sem);
1476 truncate_pagecache(inode, offset);
1477
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001478 while (!ret && idx > pg_start) {
1479 nr = idx - pg_start;
1480 if (nr > delta)
1481 nr = delta;
1482 idx -= nr;
1483
Chao Yuf62185d2015-05-28 19:16:57 +08001484 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001485 f2fs_drop_extent_tree(inode);
1486
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001487 ret = __exchange_data_block(inode, inode, idx,
1488 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001489 f2fs_unlock_op(sbi);
1490 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001491 up_write(&F2FS_I(inode)->i_mmap_sem);
1492 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001493
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001494 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001495 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001496 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1497 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001498 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001499
1500 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001501 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001502 return ret;
1503}
1504
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001505static int expand_inode_data(struct inode *inode, loff_t offset,
1506 loff_t len, int mode)
1507{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001508 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001509 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001510 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1511 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001512 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001513 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001514 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001515 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001516
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001517 err = inode_newsize_ok(inode, (len + offset));
1518 if (err)
1519 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001520
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001521 err = f2fs_convert_inline_inode(inode);
1522 if (err)
1523 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001524
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001525 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001526
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001527 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001528 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001529
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001530 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1531 map.m_len = pg_end - map.m_lblk;
1532 if (off_end)
1533 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001534
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001535 if (f2fs_is_pinned_file(inode))
1536 map.m_seg_type = CURSEG_COLD_DATA;
1537
1538 err = f2fs_map_blocks(inode, &map, 1, (f2fs_is_pinned_file(inode) ?
1539 F2FS_GET_BLOCK_PRE_DIO :
1540 F2FS_GET_BLOCK_PRE_AIO));
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001541 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001542 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001543
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001544 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001545 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001546
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001547 last_off = map.m_lblk + map.m_len - 1;
1548
1549 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001550 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001551 (loff_t)(last_off + 1) << PAGE_SHIFT;
1552 } else {
1553 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001554 }
1555
Chao Yue8ed90a2017-11-05 21:53:30 +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 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001562
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001563 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001564}
1565
1566static long f2fs_fallocate(struct file *file, int mode,
1567 loff_t offset, loff_t len)
1568{
Al Viro6131ffa2013-02-27 16:59:05 -05001569 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001570 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001571
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001572 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1573 return -EIO;
Chao Yu955ebcd2019-07-22 17:57:05 +08001574 ret = f2fs_is_checkpoint_ready(F2FS_I_SB(inode));
1575 if (ret)
1576 return ret;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001577
Chao Yuc9980122015-09-11 14:39:02 +08001578 /* f2fs only support ->fallocate for regular file */
1579 if (!S_ISREG(inode->i_mode))
1580 return -EINVAL;
1581
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301582 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001583 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001584 return -EOPNOTSUPP;
1585
Chao Yub4ace332015-05-06 13:09:46 +08001586 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001587 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1588 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001589 return -EOPNOTSUPP;
1590
Al Viro59551022016-01-22 15:40:57 -05001591 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001592
Taehee Yoo587c0a42015-04-21 15:59:12 +09001593 if (mode & FALLOC_FL_PUNCH_HOLE) {
1594 if (offset >= inode->i_size)
1595 goto out;
1596
Chao Yua66c7b22013-11-22 16:52:50 +08001597 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001598 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1599 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001600 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1601 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001602 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1603 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001604 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001605 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001606 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001607
Namjae Jeon3af60a42012-12-30 14:52:37 +09001608 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001609 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001610 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001611 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001612 }
Chao Yu3375f692014-01-28 10:29:26 +08001613
Taehee Yoo587c0a42015-04-21 15:59:12 +09001614out:
Al Viro59551022016-01-22 15:40:57 -05001615 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001616
Namjae Jeonc01e2852013-04-23 17:00:52 +09001617 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001618 return ret;
1619}
1620
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001621static int f2fs_release_file(struct inode *inode, struct file *filp)
1622{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001623 /*
1624 * f2fs_relase_file is called at every close calls. So we should
1625 * not drop any inmemory pages by close called by other process.
1626 */
1627 if (!(filp->f_mode & FMODE_WRITE) ||
1628 atomic_read(&inode->i_writecount) != 1)
1629 return 0;
1630
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001631 /* some remained atomic pages should discarded */
1632 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001633 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001634 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001635 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001636 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001637 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001638 clear_inode_flag(inode, FI_VOLATILE_FILE);
1639 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001640 }
1641 return 0;
1642}
1643
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001644static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001645{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001646 struct inode *inode = file_inode(file);
1647
1648 /*
1649 * If the process doing a transaction is crashed, we should do
1650 * roll-back. Otherwise, other reader/write can see corrupted database
1651 * until all the writers close its file. Since this should be done
1652 * before dropping file lock, it needs to do in ->flush.
1653 */
1654 if (f2fs_is_atomic_file(inode) &&
1655 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001656 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001657 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001658}
1659
Eric Biggers36098552019-06-04 22:59:04 -07001660static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001661{
1662 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08001663
1664 /* Is it quota file? Do not allow user to mess with it */
1665 if (IS_NOQUOTA(inode))
1666 return -EPERM;
1667
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001668 if ((iflags ^ fi->i_flags) & F2FS_CASEFOLD_FL) {
1669 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1670 return -EOPNOTSUPP;
1671 if (!f2fs_empty_dir(inode))
1672 return -ENOTEMPTY;
1673 }
1674
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001675 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu2c1d0302017-07-29 00:32:52 +08001676
Chao Yu59c84402018-04-03 15:08:17 +08001677 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001678 set_inode_flag(inode, FI_PROJ_INHERIT);
1679 else
1680 clear_inode_flag(inode, FI_PROJ_INHERIT);
1681
1682 inode->i_ctime = current_time(inode);
1683 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001684 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001685 return 0;
1686}
1687
Eric Biggers36098552019-06-04 22:59:04 -07001688/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1689
1690/*
1691 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1692 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1693 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1694 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1695 */
1696
1697static const struct {
1698 u32 iflag;
1699 u32 fsflag;
1700} f2fs_fsflags_map[] = {
1701 { F2FS_SYNC_FL, FS_SYNC_FL },
1702 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1703 { F2FS_APPEND_FL, FS_APPEND_FL },
1704 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1705 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1706 { F2FS_INDEX_FL, FS_INDEX_FL },
1707 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1708 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001709 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001710};
1711
1712#define F2FS_GETTABLE_FS_FL ( \
1713 FS_SYNC_FL | \
1714 FS_IMMUTABLE_FL | \
1715 FS_APPEND_FL | \
1716 FS_NODUMP_FL | \
1717 FS_NOATIME_FL | \
1718 FS_INDEX_FL | \
1719 FS_DIRSYNC_FL | \
1720 FS_PROJINHERIT_FL | \
1721 FS_ENCRYPT_FL | \
1722 FS_INLINE_DATA_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001723 FS_NOCOW_FL | \
1724 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001725
1726#define F2FS_SETTABLE_FS_FL ( \
1727 FS_SYNC_FL | \
1728 FS_IMMUTABLE_FL | \
1729 FS_APPEND_FL | \
1730 FS_NODUMP_FL | \
1731 FS_NOATIME_FL | \
1732 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001733 FS_PROJINHERIT_FL | \
1734 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001735
1736/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1737static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1738{
1739 u32 fsflags = 0;
1740 int i;
1741
1742 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1743 if (iflags & f2fs_fsflags_map[i].iflag)
1744 fsflags |= f2fs_fsflags_map[i].fsflag;
1745
1746 return fsflags;
1747}
1748
1749/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1750static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1751{
1752 u32 iflags = 0;
1753 int i;
1754
1755 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1756 if (fsflags & f2fs_fsflags_map[i].fsflag)
1757 iflags |= f2fs_fsflags_map[i].iflag;
1758
1759 return iflags;
1760}
1761
1762static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1763{
1764 struct inode *inode = file_inode(filp);
1765 struct f2fs_inode_info *fi = F2FS_I(inode);
1766 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1767
1768 if (IS_ENCRYPTED(inode))
1769 fsflags |= FS_ENCRYPT_FL;
1770 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1771 fsflags |= FS_INLINE_DATA_FL;
1772 if (is_inode_flag_set(inode, FI_PIN_FILE))
1773 fsflags |= FS_NOCOW_FL;
1774
1775 fsflags &= F2FS_GETTABLE_FS_FL;
1776
1777 return put_user(fsflags, (int __user *)arg);
1778}
1779
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001780static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1781{
1782 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001783 struct f2fs_inode_info *fi = F2FS_I(inode);
1784 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001785 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001786 int ret;
1787
Chao Yu7fb17fe2016-05-09 19:56:32 +08001788 if (!inode_owner_or_capable(inode))
1789 return -EACCES;
1790
Eric Biggers36098552019-06-04 22:59:04 -07001791 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001792 return -EFAULT;
1793
Eric Biggers36098552019-06-04 22:59:04 -07001794 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1795 return -EOPNOTSUPP;
1796 fsflags &= F2FS_SETTABLE_FS_FL;
1797
1798 iflags = f2fs_fsflags_to_iflags(fsflags);
1799 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1800 return -EOPNOTSUPP;
1801
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001802 ret = mnt_want_write_file(filp);
1803 if (ret)
1804 return ret;
1805
Al Viro59551022016-01-22 15:40:57 -05001806 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001807
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001808 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1809 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1810 if (ret)
1811 goto out;
1812
Eric Biggers36098552019-06-04 22:59:04 -07001813 ret = f2fs_setflags_common(inode, iflags,
1814 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001815out:
Chao Yua72d4b92017-05-03 23:59:13 +08001816 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001817 mnt_drop_write_file(filp);
1818 return ret;
1819}
1820
Chao Yud49f3e82015-01-23 20:36:04 +08001821static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1822{
1823 struct inode *inode = file_inode(filp);
1824
1825 return put_user(inode->i_generation, (int __user *)arg);
1826}
1827
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001828static int f2fs_ioc_start_atomic_write(struct file *filp)
1829{
1830 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001831 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001832
1833 if (!inode_owner_or_capable(inode))
1834 return -EACCES;
1835
Jaegeuk Kime8118982017-03-17 10:04:15 +08001836 if (!S_ISREG(inode->i_mode))
1837 return -EINVAL;
1838
Chao Yu038d0692019-07-25 22:39:11 +08001839 if (filp->f_flags & O_DIRECT)
1840 return -EINVAL;
1841
Chao Yu7fb17fe2016-05-09 19:56:32 +08001842 ret = mnt_want_write_file(filp);
1843 if (ret)
1844 return ret;
1845
Chao Yu0fac5582016-05-09 19:56:33 +08001846 inode_lock(inode);
1847
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001848 if (f2fs_is_atomic_file(inode)) {
1849 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1850 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001851 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001852 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001853
Chao Yuf4c9c742015-07-17 18:06:35 +08001854 ret = f2fs_convert_inline_inode(inode);
1855 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001856 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001857
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001858 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1859
Jaegeuk Kim31867b22018-12-28 11:00:38 -08001860 /*
1861 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
1862 * f2fs_is_atomic_file.
1863 */
1864 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001865 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1866 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001867 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001868 if (ret) {
1869 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001870 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001871 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08001872
Yunlei He054afda2018-04-18 11:06:39 +08001873 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001874 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001875 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001876
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001877 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001878 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001879 stat_inc_atomic_write(inode);
1880 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001881out:
Chao Yu0fac5582016-05-09 19:56:33 +08001882 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001883 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001884 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001885}
1886
1887static int f2fs_ioc_commit_atomic_write(struct file *filp)
1888{
1889 struct inode *inode = file_inode(filp);
1890 int ret;
1891
1892 if (!inode_owner_or_capable(inode))
1893 return -EACCES;
1894
1895 ret = mnt_want_write_file(filp);
1896 if (ret)
1897 return ret;
1898
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001899 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08001900
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001901 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08001902
Chao Yub169c3c2018-04-18 17:45:02 +08001903 if (f2fs_is_volatile_file(inode)) {
1904 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001905 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08001906 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001907
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001908 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08001909 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001910 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001911 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001912
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001913 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001914 if (!ret) {
1915 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001916 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +08001917 stat_dec_atomic_write(inode);
1918 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001919 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001920 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001921 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001922err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08001923 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1924 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1925 ret = -EINVAL;
1926 }
Chao Yu0fac5582016-05-09 19:56:33 +08001927 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001928 mnt_drop_write_file(filp);
1929 return ret;
1930}
1931
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001932static int f2fs_ioc_start_volatile_write(struct file *filp)
1933{
1934 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001935 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001936
1937 if (!inode_owner_or_capable(inode))
1938 return -EACCES;
1939
Chao Yu8ff09712017-03-17 15:43:57 +08001940 if (!S_ISREG(inode->i_mode))
1941 return -EINVAL;
1942
Chao Yu7fb17fe2016-05-09 19:56:32 +08001943 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001944 if (ret)
1945 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001946
Chao Yu0fac5582016-05-09 19:56:33 +08001947 inode_lock(inode);
1948
Chao Yu7fb17fe2016-05-09 19:56:32 +08001949 if (f2fs_is_volatile_file(inode))
1950 goto out;
1951
1952 ret = f2fs_convert_inline_inode(inode);
1953 if (ret)
1954 goto out;
1955
Chao Yu648d50b2017-03-22 17:23:45 +08001956 stat_inc_volatile_write(inode);
1957 stat_update_max_volatile_write(inode);
1958
Jaegeuk Kim91942322016-05-20 10:13:22 -07001959 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001960 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001961out:
Chao Yu0fac5582016-05-09 19:56:33 +08001962 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001963 mnt_drop_write_file(filp);
1964 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001965}
1966
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001967static int f2fs_ioc_release_volatile_write(struct file *filp)
1968{
1969 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001970 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001971
1972 if (!inode_owner_or_capable(inode))
1973 return -EACCES;
1974
Chao Yu7fb17fe2016-05-09 19:56:32 +08001975 ret = mnt_want_write_file(filp);
1976 if (ret)
1977 return ret;
1978
Chao Yu0fac5582016-05-09 19:56:33 +08001979 inode_lock(inode);
1980
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001981 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001982 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001983
Chao Yu7fb17fe2016-05-09 19:56:32 +08001984 if (!f2fs_is_first_block_written(inode)) {
1985 ret = truncate_partial_data_page(inode, 0, true);
1986 goto out;
1987 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001988
Chao Yu7fb17fe2016-05-09 19:56:32 +08001989 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1990out:
Chao Yu0fac5582016-05-09 19:56:33 +08001991 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001992 mnt_drop_write_file(filp);
1993 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001994}
1995
1996static int f2fs_ioc_abort_volatile_write(struct file *filp)
1997{
1998 struct inode *inode = file_inode(filp);
1999 int ret;
2000
2001 if (!inode_owner_or_capable(inode))
2002 return -EACCES;
2003
2004 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
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002010 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002011 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002012 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002013 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002014 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002015 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002016 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002017
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002018 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2019
Chao Yu0fac5582016-05-09 19:56:33 +08002020 inode_unlock(inode);
2021
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002022 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002023 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002024 return ret;
2025}
2026
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002027static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2028{
2029 struct inode *inode = file_inode(filp);
2030 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2031 struct super_block *sb = sbi->sb;
2032 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002033 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002034
2035 if (!capable(CAP_SYS_ADMIN))
2036 return -EPERM;
2037
2038 if (get_user(in, (__u32 __user *)arg))
2039 return -EFAULT;
2040
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302041 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2042 ret = mnt_want_write_file(filp);
2043 if (ret)
2044 return ret;
2045 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002046
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002047 switch (in) {
2048 case F2FS_GOING_DOWN_FULLSYNC:
2049 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08002050 if (IS_ERR(sb)) {
2051 ret = PTR_ERR(sb);
2052 goto out;
2053 }
2054 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002055 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002056 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002057 thaw_bdev(sb->s_bdev, sb);
2058 }
2059 break;
2060 case F2FS_GOING_DOWN_METASYNC:
2061 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002062 ret = f2fs_sync_fs(sb, 1);
2063 if (ret)
2064 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002065 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002066 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002067 break;
2068 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002069 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002070 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002071 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002072 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002073 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002074 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002075 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002076 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002077 case F2FS_GOING_DOWN_NEED_FSCK:
2078 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002079 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2080 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002081 /* do checkpoint only */
2082 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002083 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002084 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002085 ret = -EINVAL;
2086 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002087 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002088
Chao Yu4d57b862018-05-30 00:20:41 +08002089 f2fs_stop_gc_thread(sbi);
2090 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002091
Chao Yu4d57b862018-05-30 00:20:41 +08002092 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002093 clear_opt(sbi, DISCARD);
2094
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002095 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002096out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302097 if (in != F2FS_GOING_DOWN_FULLSYNC)
2098 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002099
2100 trace_f2fs_shutdown(sbi, in, ret);
2101
Chao Yu7fb17fe2016-05-09 19:56:32 +08002102 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002103}
2104
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002105static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2106{
2107 struct inode *inode = file_inode(filp);
2108 struct super_block *sb = inode->i_sb;
2109 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2110 struct fstrim_range range;
2111 int ret;
2112
2113 if (!capable(CAP_SYS_ADMIN))
2114 return -EPERM;
2115
Chao Yu7d20c8a2018-09-04 03:52:17 +08002116 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002117 return -EOPNOTSUPP;
2118
2119 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2120 sizeof(range)))
2121 return -EFAULT;
2122
Chao Yu7fb17fe2016-05-09 19:56:32 +08002123 ret = mnt_want_write_file(filp);
2124 if (ret)
2125 return ret;
2126
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002127 range.minlen = max((unsigned int)range.minlen,
2128 q->limits.discard_granularity);
2129 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002130 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002131 if (ret < 0)
2132 return ret;
2133
2134 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2135 sizeof(range)))
2136 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002137 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002138 return 0;
2139}
2140
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002141static bool uuid_is_nonzero(__u8 u[16])
2142{
2143 int i;
2144
2145 for (i = 0; i < 16; i++)
2146 if (u[i])
2147 return true;
2148 return false;
2149}
2150
2151static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2152{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002153 struct inode *inode = file_inode(filp);
2154
Chao Yu7beb01f2018-10-24 18:34:26 +08002155 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002156 return -EOPNOTSUPP;
2157
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002158 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002159
Eric Biggersdb717d82016-11-26 19:07:49 -05002160 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002161}
2162
2163static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2164{
Chao Yu7beb01f2018-10-24 18:34:26 +08002165 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002166 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002167 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002168}
2169
2170static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2171{
2172 struct inode *inode = file_inode(filp);
2173 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2174 int err;
2175
Chao Yu7beb01f2018-10-24 18:34:26 +08002176 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002177 return -EOPNOTSUPP;
2178
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002179 err = mnt_want_write_file(filp);
2180 if (err)
2181 return err;
2182
Chao Yu846ae672018-02-26 22:04:13 +08002183 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002184
2185 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2186 goto got_it;
2187
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002188 /* update superblock with uuid */
2189 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2190
Chao Yuc5bda1c2015-06-08 13:28:03 +08002191 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002192 if (err) {
2193 /* undo new data */
2194 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002195 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002196 }
2197got_it:
2198 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2199 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002200 err = -EFAULT;
2201out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002202 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002203 mnt_drop_write_file(filp);
2204 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002205}
2206
Chao Yuc1c1b582015-07-10 18:08:10 +08002207static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2208{
2209 struct inode *inode = file_inode(filp);
2210 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002211 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002212 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002213
2214 if (!capable(CAP_SYS_ADMIN))
2215 return -EPERM;
2216
Chao Yud530d4d2015-10-05 22:22:44 +08002217 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002218 return -EFAULT;
2219
Chao Yud530d4d2015-10-05 22:22:44 +08002220 if (f2fs_readonly(sbi->sb))
2221 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002222
Chao Yu7fb17fe2016-05-09 19:56:32 +08002223 ret = mnt_want_write_file(filp);
2224 if (ret)
2225 return ret;
2226
Chao Yud530d4d2015-10-05 22:22:44 +08002227 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002228 if (!mutex_trylock(&sbi->gc_mutex)) {
2229 ret = -EBUSY;
2230 goto out;
2231 }
Chao Yud530d4d2015-10-05 22:22:44 +08002232 } else {
2233 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002234 }
2235
Jaegeuk Kime066b832017-04-13 15:17:00 -07002236 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002237out:
2238 mnt_drop_write_file(filp);
2239 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002240}
2241
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002242static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2243{
2244 struct inode *inode = file_inode(filp);
2245 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2246 struct f2fs_gc_range range;
2247 u64 end;
2248 int ret;
2249
2250 if (!capable(CAP_SYS_ADMIN))
2251 return -EPERM;
2252
2253 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2254 sizeof(range)))
2255 return -EFAULT;
2256
2257 if (f2fs_readonly(sbi->sb))
2258 return -EROFS;
2259
Yunlei Heb82f6e32018-04-24 11:40:30 +08002260 end = range.start + range.len;
2261 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
2262 return -EINVAL;
2263 }
2264
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002265 ret = mnt_want_write_file(filp);
2266 if (ret)
2267 return ret;
2268
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002269do_more:
2270 if (!range.sync) {
2271 if (!mutex_trylock(&sbi->gc_mutex)) {
2272 ret = -EBUSY;
2273 goto out;
2274 }
2275 } else {
2276 mutex_lock(&sbi->gc_mutex);
2277 }
2278
2279 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Yunlong Song67b0e422018-10-30 20:37:55 +08002280 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002281 if (range.start <= end)
2282 goto do_more;
2283out:
2284 mnt_drop_write_file(filp);
2285 return ret;
2286}
2287
Chao Yu059c0642018-07-17 20:41:49 +08002288static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002289{
2290 struct inode *inode = file_inode(filp);
2291 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002292 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002293
2294 if (!capable(CAP_SYS_ADMIN))
2295 return -EPERM;
2296
2297 if (f2fs_readonly(sbi->sb))
2298 return -EROFS;
2299
Daniel Rosenberg43549942018-08-20 19:21:43 -07002300 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002301 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002302 return -EINVAL;
2303 }
2304
Chao Yu7fb17fe2016-05-09 19:56:32 +08002305 ret = mnt_want_write_file(filp);
2306 if (ret)
2307 return ret;
2308
2309 ret = f2fs_sync_fs(sbi->sb, 1);
2310
2311 mnt_drop_write_file(filp);
2312 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002313}
2314
Chao Yud323d002015-10-27 09:53:45 +08002315static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2316 struct file *filp,
2317 struct f2fs_defragment *range)
2318{
2319 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002320 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002321 .m_seg_type = NO_CHECK_TYPE ,
2322 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002323 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002324 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002325 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002326 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002327 block_t blk_end = 0;
2328 bool fragmented = false;
2329 int err;
2330
2331 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002332 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002333 return -EINVAL;
2334
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002335 pg_start = range->start >> PAGE_SHIFT;
2336 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002337
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002338 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002339
Al Viro59551022016-01-22 15:40:57 -05002340 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002341
2342 /* writeback all dirty pages in the range */
2343 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002344 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002345 if (err)
2346 goto out;
2347
2348 /*
2349 * lookup mapping info in extent cache, skip defragmenting if physical
2350 * block addresses are continuous.
2351 */
2352 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2353 if (ei.fofs + ei.len >= pg_end)
2354 goto out;
2355 }
2356
2357 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002358 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002359
2360 /*
2361 * lookup mapping info in dnode page cache, skip defragmenting if all
2362 * physical block addresses are continuous even if there are hole(s)
2363 * in logical blocks.
2364 */
2365 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002366 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002367 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002368 if (err)
2369 goto out;
2370
2371 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002372 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002373 continue;
2374 }
2375
Chao Yu25a912e2018-01-10 18:18:51 +08002376 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002377 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002378
2379 /* record total count of block that we're going to move */
2380 total += map.m_len;
2381
Chao Yud323d002015-10-27 09:53:45 +08002382 blk_end = map.m_pblk + map.m_len;
2383
2384 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002385 }
2386
Chao Yud3a1a0e2019-07-29 23:02:29 +08002387 if (!fragmented) {
2388 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002389 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002390 }
Chao Yud323d002015-10-27 09:53:45 +08002391
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002392 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002393
2394 /*
2395 * make sure there are enough free section for LFS allocation, this can
2396 * avoid defragment running in SSR mode when free section are allocated
2397 * intensively
2398 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002399 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002400 err = -EAGAIN;
2401 goto out;
2402 }
2403
Chao Yu25a912e2018-01-10 18:18:51 +08002404 map.m_lblk = pg_start;
2405 map.m_len = pg_end - pg_start;
2406 total = 0;
2407
Chao Yud323d002015-10-27 09:53:45 +08002408 while (map.m_lblk < pg_end) {
2409 pgoff_t idx;
2410 int cnt = 0;
2411
2412do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002413 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002414 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002415 if (err)
2416 goto clear_out;
2417
2418 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002419 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002420 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002421 }
2422
Jaegeuk Kim91942322016-05-20 10:13:22 -07002423 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002424
2425 idx = map.m_lblk;
2426 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2427 struct page *page;
2428
Chao Yu4d57b862018-05-30 00:20:41 +08002429 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002430 if (IS_ERR(page)) {
2431 err = PTR_ERR(page);
2432 goto clear_out;
2433 }
2434
2435 set_page_dirty(page);
2436 f2fs_put_page(page, 1);
2437
2438 idx++;
2439 cnt++;
2440 total++;
2441 }
2442
2443 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002444check:
2445 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002446 goto do_map;
2447
Jaegeuk Kim91942322016-05-20 10:13:22 -07002448 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002449
2450 err = filemap_fdatawrite(inode->i_mapping);
2451 if (err)
2452 goto out;
2453 }
2454clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002455 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002456out:
Al Viro59551022016-01-22 15:40:57 -05002457 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002458 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002459 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002460 return err;
2461}
2462
2463static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2464{
2465 struct inode *inode = file_inode(filp);
2466 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2467 struct f2fs_defragment range;
2468 int err;
2469
2470 if (!capable(CAP_SYS_ADMIN))
2471 return -EPERM;
2472
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002473 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002474 return -EINVAL;
2475
Kinglong Meed7563862017-03-10 17:55:07 +08002476 if (f2fs_readonly(sbi->sb))
2477 return -EROFS;
2478
2479 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2480 sizeof(range)))
2481 return -EFAULT;
2482
2483 /* verify alignment of offset & size */
2484 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2485 return -EINVAL;
2486
2487 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2488 sbi->max_file_blocks))
2489 return -EINVAL;
2490
Chao Yud323d002015-10-27 09:53:45 +08002491 err = mnt_want_write_file(filp);
2492 if (err)
2493 return err;
2494
Chao Yud323d002015-10-27 09:53:45 +08002495 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002496 mnt_drop_write_file(filp);
2497
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002498 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002499 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002500 return err;
Chao Yud323d002015-10-27 09:53:45 +08002501
2502 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2503 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002504 return -EFAULT;
2505
2506 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002507}
2508
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002509static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2510 struct file *file_out, loff_t pos_out, size_t len)
2511{
2512 struct inode *src = file_inode(file_in);
2513 struct inode *dst = file_inode(file_out);
2514 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2515 size_t olen = len, dst_max_i_size = 0;
2516 size_t dst_osize;
2517 int ret;
2518
2519 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2520 src->i_sb != dst->i_sb)
2521 return -EXDEV;
2522
2523 if (unlikely(f2fs_readonly(src->i_sb)))
2524 return -EROFS;
2525
Chao Yufe8494b2016-08-04 20:13:02 +08002526 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2527 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002528
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302529 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002530 return -EOPNOTSUPP;
2531
Fan Lid95fd912016-09-13 11:35:42 +08002532 if (src == dst) {
2533 if (pos_in == pos_out)
2534 return 0;
2535 if (pos_out > pos_in && pos_out < pos_in + len)
2536 return -EINVAL;
2537 }
2538
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002539 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002540 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002541 ret = -EBUSY;
2542 if (!inode_trylock(dst))
2543 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002544 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002545
2546 ret = -EINVAL;
2547 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2548 goto out_unlock;
2549 if (len == 0)
2550 olen = len = src->i_size - pos_in;
2551 if (pos_in + len == src->i_size)
2552 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2553 if (len == 0) {
2554 ret = 0;
2555 goto out_unlock;
2556 }
2557
2558 dst_osize = dst->i_size;
2559 if (pos_out + olen > dst->i_size)
2560 dst_max_i_size = pos_out + olen;
2561
2562 /* verify the end result is block aligned */
2563 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2564 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2565 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2566 goto out_unlock;
2567
2568 ret = f2fs_convert_inline_inode(src);
2569 if (ret)
2570 goto out_unlock;
2571
2572 ret = f2fs_convert_inline_inode(dst);
2573 if (ret)
2574 goto out_unlock;
2575
2576 /* write out all dirty pages from offset */
2577 ret = filemap_write_and_wait_range(src->i_mapping,
2578 pos_in, pos_in + len);
2579 if (ret)
2580 goto out_unlock;
2581
2582 ret = filemap_write_and_wait_range(dst->i_mapping,
2583 pos_out, pos_out + len);
2584 if (ret)
2585 goto out_unlock;
2586
2587 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002588
2589 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2590 if (src != dst) {
2591 ret = -EBUSY;
2592 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2593 goto out_src;
2594 }
2595
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002596 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002597 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2598 pos_out >> F2FS_BLKSIZE_BITS,
2599 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002600
2601 if (!ret) {
2602 if (dst_max_i_size)
2603 f2fs_i_size_write(dst, dst_max_i_size);
2604 else if (dst_osize != dst->i_size)
2605 f2fs_i_size_write(dst, dst_osize);
2606 }
2607 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002608
2609 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002610 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002611out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002612 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002613out_unlock:
2614 if (src != dst)
2615 inode_unlock(dst);
2616out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002617 inode_unlock(src);
2618 return ret;
2619}
2620
2621static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2622{
2623 struct f2fs_move_range range;
2624 struct fd dst;
2625 int err;
2626
2627 if (!(filp->f_mode & FMODE_READ) ||
2628 !(filp->f_mode & FMODE_WRITE))
2629 return -EBADF;
2630
2631 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2632 sizeof(range)))
2633 return -EFAULT;
2634
2635 dst = fdget(range.dst_fd);
2636 if (!dst.file)
2637 return -EBADF;
2638
2639 if (!(dst.file->f_mode & FMODE_WRITE)) {
2640 err = -EBADF;
2641 goto err_out;
2642 }
2643
2644 err = mnt_want_write_file(filp);
2645 if (err)
2646 goto err_out;
2647
2648 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2649 range.pos_out, range.len);
2650
2651 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002652 if (err)
2653 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002654
2655 if (copy_to_user((struct f2fs_move_range __user *)arg,
2656 &range, sizeof(range)))
2657 err = -EFAULT;
2658err_out:
2659 fdput(dst);
2660 return err;
2661}
2662
Jaegeuk Kime066b832017-04-13 15:17:00 -07002663static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2664{
2665 struct inode *inode = file_inode(filp);
2666 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2667 struct sit_info *sm = SIT_I(sbi);
2668 unsigned int start_segno = 0, end_segno = 0;
2669 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2670 struct f2fs_flush_device range;
2671 int ret;
2672
2673 if (!capable(CAP_SYS_ADMIN))
2674 return -EPERM;
2675
2676 if (f2fs_readonly(sbi->sb))
2677 return -EROFS;
2678
Daniel Rosenberg43549942018-08-20 19:21:43 -07002679 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2680 return -EINVAL;
2681
Jaegeuk Kime066b832017-04-13 15:17:00 -07002682 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2683 sizeof(range)))
2684 return -EFAULT;
2685
Damien Le Moal09168782019-03-16 09:13:06 +09002686 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002687 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002688 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2689 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002690 return -EINVAL;
2691 }
2692
2693 ret = mnt_want_write_file(filp);
2694 if (ret)
2695 return ret;
2696
2697 if (range.dev_num != 0)
2698 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2699 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2700
2701 start_segno = sm->last_victim[FLUSH_DEVICE];
2702 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2703 start_segno = dev_start_segno;
2704 end_segno = min(start_segno + range.segments, dev_end_segno);
2705
2706 while (start_segno < end_segno) {
2707 if (!mutex_trylock(&sbi->gc_mutex)) {
2708 ret = -EBUSY;
2709 goto out;
2710 }
2711 sm->last_victim[GC_CB] = end_segno + 1;
2712 sm->last_victim[GC_GREEDY] = end_segno + 1;
2713 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2714 ret = f2fs_gc(sbi, true, true, start_segno);
2715 if (ret == -EAGAIN)
2716 ret = 0;
2717 else if (ret < 0)
2718 break;
2719 start_segno++;
2720 }
2721out:
2722 mnt_drop_write_file(filp);
2723 return ret;
2724}
2725
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002726static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2727{
2728 struct inode *inode = file_inode(filp);
2729 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2730
2731 /* Must validate to set it with SQLite behavior in Android. */
2732 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2733
2734 return put_user(sb_feature, (u32 __user *)arg);
2735}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002736
Chao Yu2c1d0302017-07-29 00:32:52 +08002737#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002738int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2739{
2740 struct dquot *transfer_to[MAXQUOTAS] = {};
2741 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2742 struct super_block *sb = sbi->sb;
2743 int err = 0;
2744
2745 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2746 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2747 err = __dquot_transfer(inode, transfer_to);
2748 if (err)
2749 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2750 dqput(transfer_to[PRJQUOTA]);
2751 }
2752 return err;
2753}
2754
Chao Yu2c1d0302017-07-29 00:32:52 +08002755static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2756{
2757 struct inode *inode = file_inode(filp);
2758 struct f2fs_inode_info *fi = F2FS_I(inode);
2759 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002760 struct page *ipage;
2761 kprojid_t kprojid;
2762 int err;
2763
Chao Yu7beb01f2018-10-24 18:34:26 +08002764 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08002765 if (projid != F2FS_DEF_PROJID)
2766 return -EOPNOTSUPP;
2767 else
2768 return 0;
2769 }
2770
2771 if (!f2fs_has_extra_attr(inode))
2772 return -EOPNOTSUPP;
2773
2774 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2775
2776 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2777 return 0;
2778
Chao Yu2c1d0302017-07-29 00:32:52 +08002779 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08002780 /* Is it quota file? Do not allow user to mess with it */
2781 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09002782 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002783
Chao Yu4d57b862018-05-30 00:20:41 +08002784 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09002785 if (IS_ERR(ipage))
2786 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08002787
2788 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2789 i_projid)) {
2790 err = -EOVERFLOW;
2791 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09002792 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002793 }
2794 f2fs_put_page(ipage, 1);
2795
Chao Yuc22aecd2018-04-21 17:53:52 +08002796 err = dquot_initialize(inode);
2797 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002798 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002799
Chao Yu78130812018-09-25 15:36:02 +08002800 f2fs_lock_op(sbi);
2801 err = f2fs_transfer_project_quota(inode, kprojid);
2802 if (err)
2803 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08002804
2805 F2FS_I(inode)->i_projid = kprojid;
2806 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002807 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08002808out_unlock:
2809 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08002810 return err;
2811}
2812#else
Chao Yu78130812018-09-25 15:36:02 +08002813int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2814{
2815 return 0;
2816}
2817
Chao Yu2c1d0302017-07-29 00:32:52 +08002818static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2819{
2820 if (projid != F2FS_DEF_PROJID)
2821 return -EOPNOTSUPP;
2822 return 0;
2823}
2824#endif
2825
Eric Biggers36098552019-06-04 22:59:04 -07002826/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08002827
Eric Biggers36098552019-06-04 22:59:04 -07002828/*
2829 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
2830 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
2831 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
2832 */
2833
2834static const struct {
2835 u32 iflag;
2836 u32 xflag;
2837} f2fs_xflags_map[] = {
2838 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
2839 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
2840 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
2841 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
2842 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
2843 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
2844};
2845
2846#define F2FS_SUPPORTED_XFLAGS ( \
2847 FS_XFLAG_SYNC | \
2848 FS_XFLAG_IMMUTABLE | \
2849 FS_XFLAG_APPEND | \
2850 FS_XFLAG_NODUMP | \
2851 FS_XFLAG_NOATIME | \
2852 FS_XFLAG_PROJINHERIT)
2853
2854/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
2855static inline u32 f2fs_iflags_to_xflags(u32 iflags)
2856{
2857 u32 xflags = 0;
2858 int i;
2859
2860 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
2861 if (iflags & f2fs_xflags_map[i].iflag)
2862 xflags |= f2fs_xflags_map[i].xflag;
2863
Chao Yu2c1d0302017-07-29 00:32:52 +08002864 return xflags;
2865}
2866
Eric Biggers36098552019-06-04 22:59:04 -07002867/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
2868static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08002869{
Eric Biggers36098552019-06-04 22:59:04 -07002870 u32 iflags = 0;
2871 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08002872
Eric Biggers36098552019-06-04 22:59:04 -07002873 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
2874 if (xflags & f2fs_xflags_map[i].xflag)
2875 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08002876
2877 return iflags;
2878}
2879
Eric Biggers6fc93c42019-07-01 13:26:29 -07002880static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
2881{
2882 struct f2fs_inode_info *fi = F2FS_I(inode);
2883
2884 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
2885
2886 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
2887 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
2888}
2889
Chao Yu2c1d0302017-07-29 00:32:52 +08002890static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2891{
2892 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08002893 struct fsxattr fa;
2894
Eric Biggers6fc93c42019-07-01 13:26:29 -07002895 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08002896
2897 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2898 return -EFAULT;
2899 return 0;
2900}
2901
2902static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2903{
2904 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07002905 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07002906 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08002907 int err;
2908
2909 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2910 return -EFAULT;
2911
2912 /* Make sure caller has proper permission */
2913 if (!inode_owner_or_capable(inode))
2914 return -EACCES;
2915
Eric Biggers36098552019-06-04 22:59:04 -07002916 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08002917 return -EOPNOTSUPP;
2918
Eric Biggers36098552019-06-04 22:59:04 -07002919 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2920 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08002921 return -EOPNOTSUPP;
2922
2923 err = mnt_want_write_file(filp);
2924 if (err)
2925 return err;
2926
2927 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07002928
2929 f2fs_fill_fsxattr(inode, &old_fa);
2930 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09002931 if (err)
2932 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07002933
Eric Biggers36098552019-06-04 22:59:04 -07002934 err = f2fs_setflags_common(inode, iflags,
2935 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08002936 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002937 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08002938
2939 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09002940out:
2941 inode_unlock(inode);
2942 mnt_drop_write_file(filp);
2943 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002944}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002945
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002946int f2fs_pin_file_control(struct inode *inode, bool inc)
2947{
2948 struct f2fs_inode_info *fi = F2FS_I(inode);
2949 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2950
2951 /* Use i_gc_failures for normal file as a risk signal. */
2952 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08002953 f2fs_i_gc_failures_write(inode,
2954 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002955
Chao Yu2ef79ec2018-05-07 20:28:54 +08002956 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002957 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
2958 __func__, inode->i_ino,
2959 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002960 clear_inode_flag(inode, FI_PIN_FILE);
2961 return -EAGAIN;
2962 }
2963 return 0;
2964}
2965
2966static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2967{
2968 struct inode *inode = file_inode(filp);
2969 __u32 pin;
2970 int ret = 0;
2971
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002972 if (get_user(pin, (__u32 __user *)arg))
2973 return -EFAULT;
2974
2975 if (!S_ISREG(inode->i_mode))
2976 return -EINVAL;
2977
2978 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2979 return -EROFS;
2980
2981 ret = mnt_want_write_file(filp);
2982 if (ret)
2983 return ret;
2984
2985 inode_lock(inode);
2986
Chao Yu4d57b862018-05-30 00:20:41 +08002987 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb2018-01-17 16:31:38 +08002988 ret = -EINVAL;
2989 goto out;
2990 }
2991
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002992 if (!pin) {
2993 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08002994 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002995 goto done;
2996 }
2997
2998 if (f2fs_pin_file_control(inode, false)) {
2999 ret = -EAGAIN;
3000 goto out;
3001 }
3002 ret = f2fs_convert_inline_inode(inode);
3003 if (ret)
3004 goto out;
3005
3006 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003007 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003008done:
3009 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3010out:
3011 inode_unlock(inode);
3012 mnt_drop_write_file(filp);
3013 return ret;
3014}
3015
3016static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3017{
3018 struct inode *inode = file_inode(filp);
3019 __u32 pin = 0;
3020
3021 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003022 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003023 return put_user(pin, (u32 __user *)arg);
3024}
3025
Chao Yuc4020b22018-01-11 14:42:30 +08003026int f2fs_precache_extents(struct inode *inode)
3027{
3028 struct f2fs_inode_info *fi = F2FS_I(inode);
3029 struct f2fs_map_blocks map;
3030 pgoff_t m_next_extent;
3031 loff_t end;
3032 int err;
3033
3034 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3035 return -EOPNOTSUPP;
3036
3037 map.m_lblk = 0;
3038 map.m_next_pgofs = NULL;
3039 map.m_next_extent = &m_next_extent;
3040 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003041 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003042 end = F2FS_I_SB(inode)->max_file_blocks;
3043
3044 while (map.m_lblk < end) {
3045 map.m_len = end - map.m_lblk;
3046
Chao Yub2532c62018-04-24 10:55:28 +08003047 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003048 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003049 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003050 if (err)
3051 return err;
3052
3053 map.m_lblk = m_next_extent;
3054 }
3055
3056 return err;
3057}
3058
3059static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3060{
3061 return f2fs_precache_extents(file_inode(filp));
3062}
3063
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003064static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3065{
3066 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3067 __u64 block_count;
3068 int ret;
3069
3070 if (!capable(CAP_SYS_ADMIN))
3071 return -EPERM;
3072
3073 if (f2fs_readonly(sbi->sb))
3074 return -EROFS;
3075
3076 if (copy_from_user(&block_count, (void __user *)arg,
3077 sizeof(block_count)))
3078 return -EFAULT;
3079
3080 ret = f2fs_resize_fs(sbi, block_count);
3081
3082 return ret;
3083}
3084
Chao Yu4507847c2019-07-17 17:06:11 +08003085static int f2fs_get_volume_name(struct file *filp, unsigned long arg)
3086{
3087 struct inode *inode = file_inode(filp);
3088 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3089 char *vbuf;
3090 int count;
3091 int err = 0;
3092
3093 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3094 if (!vbuf)
3095 return -ENOMEM;
3096
3097 down_read(&sbi->sb_lock);
3098 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3099 ARRAY_SIZE(sbi->raw_super->volume_name),
3100 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3101 up_read(&sbi->sb_lock);
3102
3103 if (copy_to_user((char __user *)arg, vbuf,
3104 min(FSLABEL_MAX, count)))
3105 err = -EFAULT;
3106
3107 kvfree(vbuf);
3108 return err;
3109}
3110
3111static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
3112{
3113 struct inode *inode = file_inode(filp);
3114 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3115 char *vbuf;
3116 int err = 0;
3117
3118 if (!capable(CAP_SYS_ADMIN))
3119 return -EPERM;
3120
3121 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3122 if (IS_ERR(vbuf))
3123 return PTR_ERR(vbuf);
3124
3125 err = mnt_want_write_file(filp);
3126 if (err)
3127 goto out;
3128
3129 down_write(&sbi->sb_lock);
3130
3131 memset(sbi->raw_super->volume_name, 0,
3132 sizeof(sbi->raw_super->volume_name));
3133 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3134 sbi->raw_super->volume_name,
3135 ARRAY_SIZE(sbi->raw_super->volume_name));
3136
3137 err = f2fs_commit_super(sbi, false);
3138
3139 up_write(&sbi->sb_lock);
3140
3141 mnt_drop_write_file(filp);
3142out:
3143 kfree(vbuf);
3144 return err;
3145}
3146
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003147long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3148{
Chao Yu955ebcd2019-07-22 17:57:05 +08003149 int ret;
3150
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003151 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3152 return -EIO;
Chao Yu955ebcd2019-07-22 17:57:05 +08003153 ret = f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp)));
3154 if (ret)
3155 return ret;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003156
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003157 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003158 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003159 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003160 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003161 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08003162 case F2FS_IOC_GETVERSION:
3163 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003164 case F2FS_IOC_START_ATOMIC_WRITE:
3165 return f2fs_ioc_start_atomic_write(filp);
3166 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3167 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07003168 case F2FS_IOC_START_VOLATILE_WRITE:
3169 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08003170 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3171 return f2fs_ioc_release_volatile_write(filp);
3172 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3173 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08003174 case F2FS_IOC_SHUTDOWN:
3175 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003176 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003177 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003178 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3179 return f2fs_ioc_set_encryption_policy(filp, arg);
3180 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3181 return f2fs_ioc_get_encryption_policy(filp, arg);
3182 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3183 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003184 case F2FS_IOC_GARBAGE_COLLECT:
3185 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003186 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3187 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08003188 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08003189 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08003190 case F2FS_IOC_DEFRAGMENT:
3191 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003192 case F2FS_IOC_MOVE_RANGE:
3193 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07003194 case F2FS_IOC_FLUSH_DEVICE:
3195 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003196 case F2FS_IOC_GET_FEATURES:
3197 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08003198 case F2FS_IOC_FSGETXATTR:
3199 return f2fs_ioc_fsgetxattr(filp, arg);
3200 case F2FS_IOC_FSSETXATTR:
3201 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003202 case F2FS_IOC_GET_PIN_FILE:
3203 return f2fs_ioc_get_pin_file(filp, arg);
3204 case F2FS_IOC_SET_PIN_FILE:
3205 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08003206 case F2FS_IOC_PRECACHE_EXTENTS:
3207 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003208 case F2FS_IOC_RESIZE_FS:
3209 return f2fs_ioc_resize_fs(filp, arg);
Chao Yu4507847c2019-07-17 17:06:11 +08003210 case F2FS_IOC_GET_VOLUME_NAME:
3211 return f2fs_get_volume_name(filp, arg);
3212 case F2FS_IOC_SET_VOLUME_NAME:
3213 return f2fs_set_volume_name(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003214 default:
3215 return -ENOTTY;
3216 }
3217}
3218
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003219static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3220{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003221 struct file *file = iocb->ki_filp;
3222 struct inode *inode = file_inode(file);
3223 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003224
Chao Yu126ce722019-04-02 18:52:22 +08003225 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
3226 ret = -EIO;
3227 goto out;
3228 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003229
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003230 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08003231 if (iocb->ki_flags & IOCB_NOWAIT) {
3232 ret = -EAGAIN;
3233 goto out;
3234 }
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003235 inode_lock(inode);
3236 }
3237
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003238 ret = generic_write_checks(iocb, from);
3239 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003240 bool preallocated = false;
3241 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003242 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003243
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003244 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3245 set_inode_flag(inode, FI_NO_PREALLOC);
3246
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003247 if ((iocb->ki_flags & IOCB_NOWAIT)) {
3248 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003249 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003250 f2fs_has_inline_data(inode) ||
3251 f2fs_force_buffered_io(inode, iocb, from)) {
3252 clear_inode_flag(inode, FI_NO_PREALLOC);
3253 inode_unlock(inode);
3254 ret = -EAGAIN;
3255 goto out;
3256 }
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003257 } else {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003258 preallocated = true;
3259 target_size = iocb->ki_pos + iov_iter_count(from);
3260
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003261 err = f2fs_preallocate_blocks(iocb, from);
3262 if (err) {
3263 clear_inode_flag(inode, FI_NO_PREALLOC);
3264 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08003265 ret = err;
3266 goto out;
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003267 }
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003268 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003269 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003270 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003271
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003272 /* if we couldn't write data, we should deallocate blocks. */
3273 if (preallocated && i_size_read(inode) < target_size)
3274 f2fs_truncate(inode);
3275
Chao Yub0af6d42017-08-02 23:21:48 +08003276 if (ret > 0)
3277 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003278 }
3279 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08003280out:
3281 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
3282 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07003283 if (ret > 0)
3284 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003285 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003286}
3287
Namjae Jeone9750822013-02-04 23:41:41 +09003288#ifdef CONFIG_COMPAT
3289long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3290{
3291 switch (cmd) {
3292 case F2FS_IOC32_GETFLAGS:
3293 cmd = F2FS_IOC_GETFLAGS;
3294 break;
3295 case F2FS_IOC32_SETFLAGS:
3296 cmd = F2FS_IOC_SETFLAGS;
3297 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003298 case F2FS_IOC32_GETVERSION:
3299 cmd = F2FS_IOC_GETVERSION;
3300 break;
3301 case F2FS_IOC_START_ATOMIC_WRITE:
3302 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3303 case F2FS_IOC_START_VOLATILE_WRITE:
3304 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3305 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3306 case F2FS_IOC_SHUTDOWN:
3307 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3308 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3309 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3310 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003311 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003312 case F2FS_IOC_WRITE_CHECKPOINT:
3313 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003314 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003315 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003316 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003317 case F2FS_IOC_FSGETXATTR:
3318 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003319 case F2FS_IOC_GET_PIN_FILE:
3320 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003321 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003322 case F2FS_IOC_RESIZE_FS:
Chao Yu4507847c2019-07-17 17:06:11 +08003323 case F2FS_IOC_GET_VOLUME_NAME:
3324 case F2FS_IOC_SET_VOLUME_NAME:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003325 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003326 default:
3327 return -ENOIOCTLCMD;
3328 }
3329 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3330}
3331#endif
3332
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003333const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003334 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04003335 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003336 .write_iter = f2fs_file_write_iter,
3337 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003338 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003339 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003340 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003341 .fsync = f2fs_sync_file,
3342 .fallocate = f2fs_fallocate,
3343 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003344#ifdef CONFIG_COMPAT
3345 .compat_ioctl = f2fs_compat_ioctl,
3346#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003347 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003348 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003349};