blob: ba5954f41e1406caa3194ec09a1a6cc133a001f2 [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>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
27#include "xattr.h"
28#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080029#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080030#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090031#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090032
Souptick Joarderea4d4792018-04-15 01:40:02 +053033static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080034{
35 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053036 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080037
38 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053039 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080040 up_read(&F2FS_I(inode)->i_mmap_sem);
41
Souptick Joarderea4d4792018-04-15 01:40:02 +053042 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080043}
44
Souptick Joarderea4d4792018-04-15 01:40:02 +053045static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090046{
47 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080048 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070049 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu39a86952018-09-27 18:33:18 +080050 struct dnode_of_data dn = { .node_changed = false };
Gu Zhenge4795562013-09-27 18:08:30 +080051 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090052
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020053 if (unlikely(f2fs_cp_error(sbi))) {
54 err = -EIO;
55 goto err;
56 }
57
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090058 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070059
60 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070061
Dave Jiang11bac802017-02-24 14:56:41 -080062 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080063 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090064 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090065 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090066 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090067 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090068 unlock_page(page);
69 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080070 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090071 }
72
Chao Yu39a86952018-09-27 18:33:18 +080073 /* block allocation */
74 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
75 set_new_dnode(&dn, inode, NULL, NULL, 0);
76 err = f2fs_get_block(&dn, page->index);
77 f2fs_put_dnode(&dn);
78 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
79 if (err) {
80 unlock_page(page);
81 goto out_sem;
82 }
83
84 /* fill the page */
Chao Yubae0ee72018-12-25 17:43:42 +080085 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +080086
87 /* wait for GCed page writeback via META_MAPPING */
88 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
89
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090090 /*
91 * check to see if the page is mapped already (no holes)
92 */
93 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +080094 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090095
96 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030097 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +080098 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +090099 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900100
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300101 offset = i_size_read(inode) & ~PAGE_MASK;
102 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900103 }
104 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700105 if (!PageUptodate(page))
106 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900107
Chao Yub0af6d42017-08-02 23:21:48 +0800108 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530109 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800110
Jaegeuk Kime943a102013-10-25 14:26:31 +0900111 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800112out_sem:
113 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800114
115 f2fs_balance_fs(sbi, dn.node_changed);
116
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900117 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200118err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900119 return block_page_mkwrite_return(err);
120}
121
122static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800123 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700124 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900125 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900126};
127
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900128static int get_parent_ino(struct inode *inode, nid_t *pino)
129{
130 struct dentry *dentry;
131
132 inode = igrab(inode);
133 dentry = d_find_any_alias(inode);
134 iput(inode);
135 if (!dentry)
136 return 0;
137
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900138 *pino = parent_ino(dentry);
139 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900140 return 1;
141}
142
Chao Yua5fd5052017-11-06 22:51:45 +0800143static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800144{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700145 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800146 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800147
Chao Yua5fd5052017-11-06 22:51:45 +0800148 if (!S_ISREG(inode->i_mode))
149 cp_reason = CP_NON_REGULAR;
150 else if (inode->i_nlink != 1)
151 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700152 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800153 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800154 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800155 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800156 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800157 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800158 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800159 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700160 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800161 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800162 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800163 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800164 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800165 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
166 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
167 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800168 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800169
Chao Yua5fd5052017-11-06 22:51:45 +0800170 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800171}
172
Changman Lee9c7bb702014-12-08 15:29:40 +0900173static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
174{
175 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
176 bool ret = false;
177 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800178 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900179 ret = true;
180 f2fs_put_page(i, 0);
181 return ret;
182}
183
Changman Lee51455b12014-12-08 15:29:41 +0900184static void try_to_fix_pino(struct inode *inode)
185{
186 struct f2fs_inode_info *fi = F2FS_I(inode);
187 nid_t pino;
188
189 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900190 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
191 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700192 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900193 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900194 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700195 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900196}
197
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700198static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
199 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900200{
201 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700202 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700203 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900204 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800205 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900206 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900207 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900208 .nr_to_write = LONG_MAX,
209 .for_reclaim = 0,
210 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800211 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900212
Daniel Rosenberg43549942018-08-20 19:21:43 -0700213 if (unlikely(f2fs_readonly(inode->i_sb) ||
214 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900215 return 0;
216
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900217 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700218
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800219 if (S_ISDIR(inode->i_mode))
220 goto go_write;
221
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700222 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800223 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700224 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400225 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700226 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700227
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900228 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800229 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900230 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900231 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900232
Changman Lee9c7bb702014-12-08 15:29:40 +0900233 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800234 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700235 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900236 goto go_write;
237 }
238
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700239 /*
240 * if there is no written data, don't waste time to write recovery info.
241 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700242 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800243 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700244
Changman Lee9c7bb702014-12-08 15:29:40 +0900245 /* it may call write_inode just prior to fsync */
246 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700247 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700248
Jaegeuk Kim91942322016-05-20 10:13:22 -0700249 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800250 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700251 goto flush_out;
252 goto out;
253 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700254go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900255 /*
256 * Both of fdatasync() and fsync() are able to be recovered from
257 * sudden-power-off.
258 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700259 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800260 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700261 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900262
Chao Yua5fd5052017-11-06 22:51:45 +0800263 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900264 /* all the dirty node pages should be flushed for POR */
265 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900266
Changman Lee51455b12014-12-08 15:29:41 +0900267 /*
268 * We've secured consistency through sync_fs. Following pino
269 * will be used only for fsynced inodes after checkpoint.
270 */
271 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700272 clear_inode_flag(inode, FI_APPEND_WRITE);
273 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900274 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900275 }
Changman Lee51455b12014-12-08 15:29:41 +0900276sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800277 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800278 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800279 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700280 if (ret)
281 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900282
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800283 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800284 if (unlikely(f2fs_cp_error(sbi))) {
285 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800286 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800287 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800288
Chao Yu4d57b862018-05-30 00:20:41 +0800289 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700290 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900291 f2fs_write_inode(inode, NULL);
292 goto sync_nodes;
293 }
294
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700295 /*
296 * If it's atomic_write, it's just fine to keep write ordering. So
297 * here we don't need to wait for node write completion, since we use
298 * node chain which serializes node blocks. If one of node writes are
299 * reordered, we can see simply broken chain, resulting in stopping
300 * roll-forward recovery. It means we'll recover all or none node blocks
301 * given fsync mark.
302 */
303 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800304 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700305 if (ret)
306 goto out;
307 }
Changman Lee51455b12014-12-08 15:29:41 +0900308
309 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800310 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700311 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900312flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700313 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800314 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800315 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800316 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800317 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800318 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800319 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800320 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900321out:
Chao Yua5fd5052017-11-06 22:51:45 +0800322 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700323 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900324 return ret;
325}
326
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700327int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
328{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200329 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
330 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700331 return f2fs_do_sync_file(file, start, end, datasync, false);
332}
333
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900334static pgoff_t __get_first_dirty_index(struct address_space *mapping,
335 pgoff_t pgofs, int whence)
336{
Jan Kara8faab642017-11-15 17:34:55 -0800337 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900338 int nr_pages;
339
340 if (whence != SEEK_DATA)
341 return 0;
342
343 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800344 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
345 1, &page);
346 if (!nr_pages)
347 return ULONG_MAX;
348 pgofs = page->index;
349 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900350 return pgofs;
351}
352
Chao Yue1da7872018-06-05 17:44:11 +0800353static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
354 pgoff_t dirty, pgoff_t pgofs, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900355{
356 switch (whence) {
357 case SEEK_DATA:
358 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yue1da7872018-06-05 17:44:11 +0800359 is_valid_data_blkaddr(sbi, blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900360 return true;
361 break;
362 case SEEK_HOLE:
363 if (blkaddr == NULL_ADDR)
364 return true;
365 break;
366 }
367 return false;
368}
369
Chao Yu267378d2014-04-23 14:10:24 +0800370static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
371{
372 struct inode *inode = file->f_mapping->host;
373 loff_t maxbytes = inode->i_sb->s_maxbytes;
374 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900375 pgoff_t pgofs, end_offset, dirty;
376 loff_t data_ofs = offset;
377 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800378 int err = 0;
379
Al Viro59551022016-01-22 15:40:57 -0500380 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800381
382 isize = i_size_read(inode);
383 if (offset >= isize)
384 goto fail;
385
386 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800387 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800388 if (whence == SEEK_HOLE)
389 data_ofs = isize;
390 goto found;
391 }
392
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300393 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800394
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900395 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
396
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300397 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800398 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800399 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800400 if (err && err != -ENOENT) {
401 goto fail;
402 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900403 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800404 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800405 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800406 continue;
407 } else {
408 goto found;
409 }
410 }
411
Chao Yu81ca7352016-01-26 15:39:35 +0800412 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800413
414 /* find data/hole in dnode block */
415 for (; dn.ofs_in_node < end_offset;
416 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300417 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800418 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900419
Chao Yu7a2af762017-07-19 00:19:06 +0800420 blkaddr = datablock_addr(dn.inode,
421 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800422
Chao Yuc9b60782018-08-01 19:13:44 +0800423 if (__is_valid_data_blkaddr(blkaddr) &&
424 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
425 blkaddr, DATA_GENERIC)) {
426 f2fs_put_dnode(&dn);
427 goto fail;
428 }
429
Chao Yue1da7872018-06-05 17:44:11 +0800430 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
431 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800432 f2fs_put_dnode(&dn);
433 goto found;
434 }
435 }
436 f2fs_put_dnode(&dn);
437 }
438
439 if (whence == SEEK_DATA)
440 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800441found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900442 if (whence == SEEK_HOLE && data_ofs > isize)
443 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500444 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800445 return vfs_setpos(file, data_ofs, maxbytes);
446fail:
Al Viro59551022016-01-22 15:40:57 -0500447 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800448 return -ENXIO;
449}
450
451static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
452{
453 struct inode *inode = file->f_mapping->host;
454 loff_t maxbytes = inode->i_sb->s_maxbytes;
455
456 switch (whence) {
457 case SEEK_SET:
458 case SEEK_CUR:
459 case SEEK_END:
460 return generic_file_llseek_size(file, offset, whence,
461 maxbytes, i_size_read(inode));
462 case SEEK_DATA:
463 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700464 if (offset < 0)
465 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800466 return f2fs_seek_block(file, offset, whence);
467 }
468
469 return -EINVAL;
470}
471
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900472static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
473{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700474 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800475 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700476
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200477 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
478 return -EIO;
479
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700480 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800481 err = f2fs_convert_inline_inode(inode);
482 if (err)
483 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700484
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900485 file_accessed(file);
486 vma->vm_ops = &f2fs_file_vm_ops;
487 return 0;
488}
489
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700490static int f2fs_file_open(struct inode *inode, struct file *filp)
491{
Eric Biggers2e168c82017-11-29 12:35:28 -0800492 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700493
Eric Biggers2e168c82017-11-29 12:35:28 -0800494 if (err)
495 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900496
497 filp->f_mode |= FMODE_NOWAIT;
498
Chao Yu0abd6752017-07-09 00:13:07 +0800499 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700500}
501
Chao Yu4d57b862018-05-30 00:20:41 +0800502void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900503{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700504 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900505 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800506 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900507 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800508 int base = 0;
509
510 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
511 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900512
Gu Zheng45590712013-07-15 17:57:38 +0800513 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800514 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900515
Chris Fries6c311ec2014-01-17 14:44:39 -0600516 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900517 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900518
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900519 if (blkaddr == NULL_ADDR)
520 continue;
521
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800522 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800523 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800524
525 if (__is_valid_data_blkaddr(blkaddr) &&
526 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
527 continue;
528
Chao Yu4d57b862018-05-30 00:20:41 +0800529 f2fs_invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700530 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700531 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 nr_free++;
533 }
Chao Yu19b2c302015-08-26 20:34:48 +0800534
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900535 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800536 pgoff_t fofs;
537 /*
538 * once we invalidate valid blkaddr in range [ofs, ofs + count],
539 * we will invalidate all blkaddr in the whole range.
540 */
Chao Yu4d57b862018-05-30 00:20:41 +0800541 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800542 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800543 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900544 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545 }
546 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900547
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800548 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900549 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
550 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900551}
552
Chao Yu4d57b862018-05-30 00:20:41 +0800553void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900554{
Chao Yu4d57b862018-05-30 00:20:41 +0800555 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900556}
557
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800558static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700559 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900560{
youngjun yoo193bea12018-05-30 04:21:14 +0900561 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300562 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700563 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900564 struct page *page;
565
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700566 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700567 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900568
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700569 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700570 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700571 if (page && PageUptodate(page))
572 goto truncate_out;
573 f2fs_put_page(page, 1);
574 return 0;
575 }
576
Chao Yu4d57b862018-05-30 00:20:41 +0800577 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900578 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800579 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700580truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800581 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300582 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700583
584 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530585 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700586 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800587 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900588 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700589 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900590}
591
Chao Yuaf033b22018-09-20 20:05:00 +0800592int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock,
593 bool buf_write)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900594{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700595 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900596 struct dnode_of_data dn;
597 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800598 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700599 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800600 bool truncate_page = false;
Chao Yuaf033b22018-09-20 20:05:00 +0800601 int flag = buf_write ? F2FS_GET_BLOCK_PRE_AIO : F2FS_GET_BLOCK_PRE_DIO;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900602
Namjae Jeon51dd6242013-04-20 01:28:52 +0900603 trace_f2fs_truncate_blocks_enter(inode, from);
604
Chao Yudf033ca2018-03-20 23:08:29 +0800605 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900606
Chao Yu09210c92016-05-05 19:13:03 +0800607 if (free_from >= sbi->max_file_blocks)
608 goto free_partial;
609
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700610 if (lock)
Chao Yuaf033b22018-09-20 20:05:00 +0800611 __do_map_lock(sbi, flag, true);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800612
Chao Yu4d57b862018-05-30 00:20:41 +0800613 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700614 if (IS_ERR(ipage)) {
615 err = PTR_ERR(ipage);
616 goto out;
617 }
618
619 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800620 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700621 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800622 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700623 goto out;
624 }
625
626 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800627 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900628 if (err) {
629 if (err == -ENOENT)
630 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700631 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700632 }
633
Chao Yu81ca7352016-01-26 15:39:35 +0800634 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900635
636 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700637 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900638
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800640 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900641 free_from += count;
642 }
643
644 f2fs_put_dnode(&dn);
645free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800646 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800647out:
648 if (lock)
Chao Yuaf033b22018-09-20 20:05:00 +0800649 __do_map_lock(sbi, flag, false);
Chao Yu09210c92016-05-05 19:13:03 +0800650free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700651 /* lastly zero out the first data page */
652 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800653 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900654
Namjae Jeon51dd6242013-04-20 01:28:52 +0900655 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656 return err;
657}
658
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700659int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900660{
Chao Yub0154892015-08-24 17:39:42 +0800661 int err;
662
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200663 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
664 return -EIO;
665
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900666 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
667 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800668 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900669
Namjae Jeon51dd6242013-04-20 01:28:52 +0900670 trace_f2fs_truncate(inode);
671
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800672 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
673 f2fs_show_injection_info(FAULT_TRUNCATE);
674 return -EIO;
675 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200676
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800677 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800678 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800679 err = f2fs_convert_inline_inode(inode);
680 if (err)
681 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800682 }
683
Chao Yuaf033b22018-09-20 20:05:00 +0800684 err = f2fs_truncate_blocks(inode, i_size_read(inode), true, false);
Chao Yub0154892015-08-24 17:39:42 +0800685 if (err)
686 return err;
687
Deepa Dinamani078cd822016-09-14 07:48:04 -0700688 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700689 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800690 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900691}
692
David Howellsa528d352017-01-31 16:46:22 +0000693int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800694 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900695{
David Howellsa528d352017-01-31 16:46:22 +0000696 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800697 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800698 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800699 unsigned int flags;
700
Chao Yu1c1d35d2018-01-25 14:54:42 +0800701 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800702 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800703 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
704 stat->result_mask |= STATX_BTIME;
705 stat->btime.tv_sec = fi->i_crtime.tv_sec;
706 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
707 }
708
Chao Yuc807a7c2018-04-08 11:25:53 +0800709 flags = fi->i_flags & F2FS_FL_USER_VISIBLE;
Chao Yu59c84402018-04-03 15:08:17 +0800710 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800711 stat->attributes |= STATX_ATTR_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +0800712 if (flags & F2FS_COMPR_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800713 stat->attributes |= STATX_ATTR_COMPRESSED;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530714 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800715 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800716 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800717 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800718 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800719 stat->attributes |= STATX_ATTR_NODUMP;
720
721 stat->attributes_mask |= (STATX_ATTR_APPEND |
722 STATX_ATTR_COMPRESSED |
723 STATX_ATTR_ENCRYPTED |
724 STATX_ATTR_IMMUTABLE |
725 STATX_ATTR_NODUMP);
726
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900727 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700728
729 /* we need to show initial sectors used for inline_data/dentries */
730 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
731 f2fs_has_inline_dentry(inode))
732 stat->blocks += (stat->size + 511) >> 9;
733
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900734 return 0;
735}
736
737#ifdef CONFIG_F2FS_FS_POSIX_ACL
738static void __setattr_copy(struct inode *inode, const struct iattr *attr)
739{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900740 unsigned int ia_valid = attr->ia_valid;
741
742 if (ia_valid & ATTR_UID)
743 inode->i_uid = attr->ia_uid;
744 if (ia_valid & ATTR_GID)
745 inode->i_gid = attr->ia_gid;
746 if (ia_valid & ATTR_ATIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700747 inode->i_atime = timespec64_trunc(attr->ia_atime,
748 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900749 if (ia_valid & ATTR_MTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700750 inode->i_mtime = timespec64_trunc(attr->ia_mtime,
751 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900752 if (ia_valid & ATTR_CTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700753 inode->i_ctime = timespec64_trunc(attr->ia_ctime,
754 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900755 if (ia_valid & ATTR_MODE) {
756 umode_t mode = attr->ia_mode;
757
758 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
759 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700760 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900761 }
762}
763#else
764#define __setattr_copy setattr_copy
765#endif
766
767int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
768{
David Howells2b0143b2015-03-17 22:25:59 +0000769 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900770 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800771 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900772
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200773 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
774 return -EIO;
775
Jan Kara31051c82016-05-26 16:55:18 +0200776 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900777 if (err)
778 return err;
779
Eric Biggers20bb2472017-11-29 12:35:32 -0800780 err = fscrypt_prepare_setattr(dentry, attr);
781 if (err)
782 return err;
783
Chao Yu0abd6752017-07-09 00:13:07 +0800784 if (is_quota_modification(inode, attr)) {
785 err = dquot_initialize(inode);
786 if (err)
787 return err;
788 }
789 if ((attr->ia_valid & ATTR_UID &&
790 !uid_eq(attr->ia_uid, inode->i_uid)) ||
791 (attr->ia_valid & ATTR_GID &&
792 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800793 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800794 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800795 if (err) {
796 set_sbi_flag(F2FS_I_SB(inode),
797 SBI_QUOTA_NEED_REPAIR);
798 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800799 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800800 }
801 /*
802 * update uid/gid under lock_op(), so that dquot and inode can
803 * be updated atomically.
804 */
805 if (attr->ia_valid & ATTR_UID)
806 inode->i_uid = attr->ia_uid;
807 if (attr->ia_valid & ATTR_GID)
808 inode->i_gid = attr->ia_gid;
809 f2fs_mark_inode_dirty_sync(inode, true);
810 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800811 }
812
Chao Yu09db6a22014-09-15 18:02:09 +0800813 if (attr->ia_valid & ATTR_SIZE) {
Chao Yua33c1502018-08-05 23:04:25 +0800814 bool to_smaller = (attr->ia_size <= i_size_read(inode));
Chao Yu0cab80e2015-12-01 11:36:16 +0800815
Chao Yua33c1502018-08-05 23:04:25 +0800816 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900817 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800818
819 truncate_setsize(inode, attr->ia_size);
820
821 if (to_smaller)
822 err = f2fs_truncate(inode);
823 /*
824 * do not trim all blocks after i_size if target size is
825 * larger than i_size.
826 */
Chao Yua33c1502018-08-05 23:04:25 +0800827 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900828 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800829
830 if (err)
831 return err;
832
833 if (!to_smaller) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800834 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800835 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800836 err = f2fs_convert_inline_inode(inode);
837 if (err)
838 return err;
839 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700840 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800841 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800842
Chao Yua0d00fa2017-10-09 17:55:19 +0800843 down_write(&F2FS_I(inode)->i_sem);
844 F2FS_I(inode)->last_disk_size = i_size_read(inode);
845 up_write(&F2FS_I(inode)->i_sem);
846
Yunlei Hec0ed4402016-12-11 15:35:15 +0800847 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900848 }
849
850 __setattr_copy(inode, attr);
851
852 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800853 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700854 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
855 inode->i_mode = F2FS_I(inode)->i_acl_mode;
856 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900857 }
858 }
859
Yunlei Hec0ed4402016-12-11 15:35:15 +0800860 /* file size may changed here */
861 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700862
863 /* inode change will produce dirty node pages flushed by checkpoint */
864 f2fs_balance_fs(F2FS_I_SB(inode), true);
865
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900866 return err;
867}
868
869const struct inode_operations f2fs_file_inode_operations = {
870 .getattr = f2fs_getattr,
871 .setattr = f2fs_setattr,
872 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800873 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900874#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900875 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900876#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900877 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900878};
879
Chao Yu63943282015-08-07 18:36:06 +0800880static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900881 loff_t start, loff_t len)
882{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700883 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900884 struct page *page;
885
886 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800887 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900888
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800889 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900890
Gu Zhenge4795562013-09-27 18:08:30 +0800891 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800892 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800893 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900894
Chao Yu63943282015-08-07 18:36:06 +0800895 if (IS_ERR(page))
896 return PTR_ERR(page);
897
Chao Yubae0ee72018-12-25 17:43:42 +0800898 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +0800899 zero_user(page, start, len);
900 set_page_dirty(page);
901 f2fs_put_page(page, 1);
902 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900903}
904
Chao Yu4d57b862018-05-30 00:20:41 +0800905int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900906{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900907 int err;
908
Chao Yuea587112015-09-17 20:22:44 +0800909 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900910 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800911 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900912
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900913 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800914 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900915 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800916 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +0800917 pg_start = f2fs_get_next_page_offset(&dn,
918 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900919 continue;
Chao Yuea587112015-09-17 20:22:44 +0800920 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900921 return err;
922 }
923
Chao Yu81ca7352016-01-26 15:39:35 +0800924 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800925 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
926
927 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
928
Chao Yu4d57b862018-05-30 00:20:41 +0800929 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900930 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800931
932 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900933 }
934 return 0;
935}
936
Chao Yua66c7b22013-11-22 16:52:50 +0800937static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900938{
939 pgoff_t pg_start, pg_end;
940 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800941 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900942
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800943 ret = f2fs_convert_inline_inode(inode);
944 if (ret)
945 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800946
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300947 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
948 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900949
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300950 off_start = offset & (PAGE_SIZE - 1);
951 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900952
953 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800954 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900955 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800956 if (ret)
957 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900958 } else {
Chao Yu63943282015-08-07 18:36:06 +0800959 if (off_start) {
960 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300961 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800962 if (ret)
963 return ret;
964 }
965 if (off_end) {
966 ret = fill_zero(inode, pg_end, 0, off_end);
967 if (ret)
968 return ret;
969 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900970
971 if (pg_start < pg_end) {
972 struct address_space *mapping = inode->i_mapping;
973 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700974 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500975
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800976 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900977
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300978 blk_start = (loff_t)pg_start << PAGE_SHIFT;
979 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +0800980
Chao Yua33c1502018-08-05 23:04:25 +0800981 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900982 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800983
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900984 truncate_inode_pages_range(mapping, blk_start,
985 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900986
Gu Zhenge4795562013-09-27 18:08:30 +0800987 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800988 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800989 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +0800990
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800991 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900992 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900993 }
994 }
995
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900996 return ret;
997}
998
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700999static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1000 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001001{
1002 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1003 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001004 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001005
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001006next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001007 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001008 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001009 if (ret && ret != -ENOENT) {
1010 return ret;
1011 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001012 if (dn.max_level == 0)
1013 return -ENOENT;
1014 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
1015 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 Yu4d57b862018-05-30 00:20:41 +08001025 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001026
1027 if (test_opt(sbi, LFS)) {
1028 f2fs_put_dnode(&dn);
1029 return -ENOTSUPP;
1030 }
1031
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001032 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001033 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001034 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001035 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001036 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001037 f2fs_put_dnode(&dn);
1038next:
1039 len -= done;
1040 off += done;
1041 if (len)
1042 goto next_dnode;
1043 return 0;
1044}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001045
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001046static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1047 int *do_replace, pgoff_t off, int len)
1048{
1049 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1050 struct dnode_of_data dn;
1051 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001052
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001053 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1054 if (*do_replace == 0)
1055 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001056
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001057 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001058 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001059 if (ret) {
1060 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001061 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001062 } else {
1063 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001064 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001065 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001066 }
1067 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001068}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001069
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001070static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1071 block_t *blkaddr, int *do_replace,
1072 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1073{
1074 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1075 pgoff_t i = 0;
1076 int ret;
1077
1078 while (i < len) {
1079 if (blkaddr[i] == NULL_ADDR && !full) {
1080 i++;
1081 continue;
1082 }
1083
1084 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1085 struct dnode_of_data dn;
1086 struct node_info ni;
1087 size_t new_size;
1088 pgoff_t ilen;
1089
1090 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001091 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001092 if (ret)
1093 return ret;
1094
Chao Yu77357302018-07-17 00:02:17 +08001095 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1096 if (ret) {
1097 f2fs_put_dnode(&dn);
1098 return ret;
1099 }
1100
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001101 ilen = min((pgoff_t)
1102 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1103 dn.ofs_in_node, len - i);
1104 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001105 dn.data_blkaddr = datablock_addr(dn.inode,
1106 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001107 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001108
1109 if (do_replace[i]) {
1110 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001111 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001112 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001113 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001114 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1115 blkaddr[i], ni.version, true, false);
1116
1117 do_replace[i] = 0;
1118 }
1119 dn.ofs_in_node++;
1120 i++;
1121 new_size = (dst + i) << PAGE_SHIFT;
1122 if (dst_inode->i_size < new_size)
1123 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001124 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001125
1126 f2fs_put_dnode(&dn);
1127 } else {
1128 struct page *psrc, *pdst;
1129
Chao Yu4d57b862018-05-30 00:20:41 +08001130 psrc = f2fs_get_lock_data_page(src_inode,
1131 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001132 if (IS_ERR(psrc))
1133 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001134 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001135 true);
1136 if (IS_ERR(pdst)) {
1137 f2fs_put_page(psrc, 1);
1138 return PTR_ERR(pdst);
1139 }
1140 f2fs_copy_page(psrc, pdst);
1141 set_page_dirty(pdst);
1142 f2fs_put_page(pdst, 1);
1143 f2fs_put_page(psrc, 1);
1144
Chao Yu4d57b862018-05-30 00:20:41 +08001145 ret = f2fs_truncate_hole(src_inode,
1146 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001147 if (ret)
1148 return ret;
1149 i++;
1150 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001151 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001152 return 0;
1153}
1154
1155static int __exchange_data_block(struct inode *src_inode,
1156 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001157 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001158{
1159 block_t *src_blkaddr;
1160 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001161 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001162 int ret;
1163
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001164 while (len) {
1165 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001166
Chao Yu628b3d12017-11-30 19:28:18 +08001167 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001168 array_size(olen, sizeof(block_t)),
1169 GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001170 if (!src_blkaddr)
1171 return -ENOMEM;
1172
Chao Yu628b3d12017-11-30 19:28:18 +08001173 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001174 array_size(olen, sizeof(int)),
1175 GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001176 if (!do_replace) {
1177 kvfree(src_blkaddr);
1178 return -ENOMEM;
1179 }
1180
1181 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1182 do_replace, src, olen);
1183 if (ret)
1184 goto roll_back;
1185
1186 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1187 do_replace, src, dst, olen, full);
1188 if (ret)
1189 goto roll_back;
1190
1191 src += olen;
1192 dst += olen;
1193 len -= olen;
1194
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001195 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001196 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001197 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001198 return 0;
1199
1200roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001201 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001202 kvfree(src_blkaddr);
1203 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001204 return ret;
1205}
1206
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001207static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001208{
1209 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001210 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001211 pgoff_t start = offset >> PAGE_SHIFT;
1212 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001213 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001214
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001215 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001216
1217 /* avoid gc operation during block exchange */
1218 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1219 down_write(&F2FS_I(inode)->i_mmap_sem);
1220
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001221 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001222 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001223 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001224 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1225 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001226
1227 up_write(&F2FS_I(inode)->i_mmap_sem);
1228 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001229 return ret;
1230}
1231
1232static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1233{
Chao Yub4ace332015-05-06 13:09:46 +08001234 loff_t new_size;
1235 int ret;
1236
Chao Yub4ace332015-05-06 13:09:46 +08001237 if (offset + len >= i_size_read(inode))
1238 return -EINVAL;
1239
1240 /* collapse range should be aligned to block size of f2fs. */
1241 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1242 return -EINVAL;
1243
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001244 ret = f2fs_convert_inline_inode(inode);
1245 if (ret)
1246 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001247
Chao Yub4ace332015-05-06 13:09:46 +08001248 /* write out all dirty pages from offset */
1249 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1250 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001251 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001252
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001253 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001254 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001255 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001256
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001257 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001258 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001259 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1260 truncate_pagecache(inode, offset);
1261
Chao Yub4ace332015-05-06 13:09:46 +08001262 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001263 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001264
Chao Yuaf033b22018-09-20 20:05:00 +08001265 ret = f2fs_truncate_blocks(inode, new_size, true, false);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001266 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001267 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001268 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001269 return ret;
1270}
1271
Chao Yu6e961942016-05-09 19:56:31 +08001272static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1273 pgoff_t end)
1274{
1275 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1276 pgoff_t index = start;
1277 unsigned int ofs_in_node = dn->ofs_in_node;
1278 blkcnt_t count = 0;
1279 int ret;
1280
1281 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001282 if (datablock_addr(dn->inode, dn->node_page,
1283 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001284 count++;
1285 }
1286
1287 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001288 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001289 if (ret)
1290 return ret;
1291
1292 dn->ofs_in_node = ofs_in_node;
1293 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001294 dn->data_blkaddr = datablock_addr(dn->inode,
1295 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001296 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001297 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001298 * allocation.
1299 */
1300 if (dn->data_blkaddr == NULL_ADDR) {
1301 ret = -ENOSPC;
1302 break;
1303 }
1304 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001305 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001306 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001307 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001308 }
1309 }
1310
1311 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1312
1313 return ret;
1314}
1315
Chao Yu75cd4e02015-05-06 13:11:13 +08001316static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1317 int mode)
1318{
1319 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1320 struct address_space *mapping = inode->i_mapping;
1321 pgoff_t index, pg_start, pg_end;
1322 loff_t new_size = i_size_read(inode);
1323 loff_t off_start, off_end;
1324 int ret = 0;
1325
Chao Yu75cd4e02015-05-06 13:11:13 +08001326 ret = inode_newsize_ok(inode, (len + offset));
1327 if (ret)
1328 return ret;
1329
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001330 ret = f2fs_convert_inline_inode(inode);
1331 if (ret)
1332 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001333
1334 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1335 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001336 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001337
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001338 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1339 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001340
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001341 off_start = offset & (PAGE_SIZE - 1);
1342 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001343
1344 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001345 ret = fill_zero(inode, pg_start, off_start,
1346 off_end - off_start);
1347 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001348 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001349
Chao Yu75cd4e02015-05-06 13:11:13 +08001350 new_size = max_t(loff_t, new_size, offset + len);
1351 } else {
1352 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001353 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001354 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001355 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,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001359 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001360 }
1361
Chao Yu6e961942016-05-09 19:56:31 +08001362 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001363 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001364 unsigned int end_offset;
1365 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001366
Chao Yuc70798532018-08-05 23:02:22 +08001367 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001368 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001369
1370 truncate_pagecache_range(inode,
1371 (loff_t)index << PAGE_SHIFT,
1372 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1373
Chao Yu75cd4e02015-05-06 13:11:13 +08001374 f2fs_lock_op(sbi);
1375
Chao Yu6e961942016-05-09 19:56:31 +08001376 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001377 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001378 if (ret) {
1379 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001380 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001381 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001382 goto out;
1383 }
1384
Chao Yu6e961942016-05-09 19:56:31 +08001385 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1386 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1387
1388 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001389 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001390
Chao Yu75cd4e02015-05-06 13:11:13 +08001391 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001392 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001393 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001394
1395 f2fs_balance_fs(sbi, dn.node_changed);
1396
Chao Yu6e961942016-05-09 19:56:31 +08001397 if (ret)
1398 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001399
Chao Yu6e961942016-05-09 19:56:31 +08001400 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001401 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001402 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001403 }
1404
1405 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001406 ret = fill_zero(inode, pg_end, 0, off_end);
1407 if (ret)
1408 goto out;
1409
Chao Yu75cd4e02015-05-06 13:11:13 +08001410 new_size = max_t(loff_t, new_size, offset + len);
1411 }
1412 }
1413
1414out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001415 if (new_size > i_size_read(inode)) {
1416 if (mode & FALLOC_FL_KEEP_SIZE)
1417 file_set_keep_isize(inode);
1418 else
1419 f2fs_i_size_write(inode, new_size);
1420 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001421 return ret;
1422}
1423
Chao Yuf62185d2015-05-28 19:16:57 +08001424static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1425{
1426 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001427 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001428 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001429 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001430
Chao Yuf62185d2015-05-28 19:16:57 +08001431 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001432 ret = inode_newsize_ok(inode, new_size);
1433 if (ret)
1434 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001435
1436 if (offset >= i_size_read(inode))
1437 return -EINVAL;
1438
1439 /* insert range should be aligned to block size of f2fs. */
1440 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1441 return -EINVAL;
1442
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001443 ret = f2fs_convert_inline_inode(inode);
1444 if (ret)
1445 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001446
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001447 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001448
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001449 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuaf033b22018-09-20 20:05:00 +08001450 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true, false);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001451 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001452 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001453 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001454
1455 /* write out all dirty pages from offset */
1456 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1457 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001458 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001459
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001460 pg_start = offset >> PAGE_SHIFT;
1461 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001462 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001463 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001464
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001465 /* avoid gc operation during block exchange */
1466 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1467 down_write(&F2FS_I(inode)->i_mmap_sem);
1468 truncate_pagecache(inode, offset);
1469
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001470 while (!ret && idx > pg_start) {
1471 nr = idx - pg_start;
1472 if (nr > delta)
1473 nr = delta;
1474 idx -= nr;
1475
Chao Yuf62185d2015-05-28 19:16:57 +08001476 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001477 f2fs_drop_extent_tree(inode);
1478
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001479 ret = __exchange_data_block(inode, inode, idx,
1480 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001481 f2fs_unlock_op(sbi);
1482 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001483 up_write(&F2FS_I(inode)->i_mmap_sem);
1484 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001485
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001486 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001487 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001488 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1489 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001490 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001491
1492 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001493 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001494 return ret;
1495}
1496
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001497static int expand_inode_data(struct inode *inode, loff_t offset,
1498 loff_t len, int mode)
1499{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001501 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001502 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1503 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001504 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001505 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001506 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001507 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001508
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001509 err = inode_newsize_ok(inode, (len + offset));
1510 if (err)
1511 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001512
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001513 err = f2fs_convert_inline_inode(inode);
1514 if (err)
1515 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001516
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001517 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001518
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001519 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001520 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001521
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001522 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1523 map.m_len = pg_end - map.m_lblk;
1524 if (off_end)
1525 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001526
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001527 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1528 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001529 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001530
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001531 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001532 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001533
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001534 last_off = map.m_lblk + map.m_len - 1;
1535
1536 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001537 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001538 (loff_t)(last_off + 1) << PAGE_SHIFT;
1539 } else {
1540 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001541 }
1542
Chao Yue8ed90a2017-11-05 21:53:30 +08001543 if (new_size > i_size_read(inode)) {
1544 if (mode & FALLOC_FL_KEEP_SIZE)
1545 file_set_keep_isize(inode);
1546 else
1547 f2fs_i_size_write(inode, new_size);
1548 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001549
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001550 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001551}
1552
1553static long f2fs_fallocate(struct file *file, int mode,
1554 loff_t offset, loff_t len)
1555{
Al Viro6131ffa2013-02-27 16:59:05 -05001556 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001557 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001558
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001559 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1560 return -EIO;
1561
Chao Yuc9980122015-09-11 14:39:02 +08001562 /* f2fs only support ->fallocate for regular file */
1563 if (!S_ISREG(inode->i_mode))
1564 return -EINVAL;
1565
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301566 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001567 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001568 return -EOPNOTSUPP;
1569
Chao Yub4ace332015-05-06 13:09:46 +08001570 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001571 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1572 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001573 return -EOPNOTSUPP;
1574
Al Viro59551022016-01-22 15:40:57 -05001575 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001576
Taehee Yoo587c0a42015-04-21 15:59:12 +09001577 if (mode & FALLOC_FL_PUNCH_HOLE) {
1578 if (offset >= inode->i_size)
1579 goto out;
1580
Chao Yua66c7b22013-11-22 16:52:50 +08001581 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001582 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1583 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001584 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1585 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001586 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1587 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001588 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001589 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001590 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001591
Namjae Jeon3af60a42012-12-30 14:52:37 +09001592 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001593 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001594 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001595 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001596 }
Chao Yu3375f692014-01-28 10:29:26 +08001597
Taehee Yoo587c0a42015-04-21 15:59:12 +09001598out:
Al Viro59551022016-01-22 15:40:57 -05001599 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001600
Namjae Jeonc01e2852013-04-23 17:00:52 +09001601 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001602 return ret;
1603}
1604
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001605static int f2fs_release_file(struct inode *inode, struct file *filp)
1606{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001607 /*
1608 * f2fs_relase_file is called at every close calls. So we should
1609 * not drop any inmemory pages by close called by other process.
1610 */
1611 if (!(filp->f_mode & FMODE_WRITE) ||
1612 atomic_read(&inode->i_writecount) != 1)
1613 return 0;
1614
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001615 /* some remained atomic pages should discarded */
1616 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001617 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001618 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001619 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001620 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001621 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001622 clear_inode_flag(inode, FI_VOLATILE_FILE);
1623 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001624 }
1625 return 0;
1626}
1627
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001628static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001629{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001630 struct inode *inode = file_inode(file);
1631
1632 /*
1633 * If the process doing a transaction is crashed, we should do
1634 * roll-back. Otherwise, other reader/write can see corrupted database
1635 * until all the writers close its file. Since this should be done
1636 * before dropping file lock, it needs to do in ->flush.
1637 */
1638 if (f2fs_is_atomic_file(inode) &&
1639 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001640 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001641 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001642}
1643
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001644static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001645{
Al Viro6131ffa2013-02-27 16:59:05 -05001646 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001647 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu76481152018-04-08 11:27:14 +08001648 unsigned int flags = fi->i_flags;
1649
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301650 if (IS_ENCRYPTED(inode))
Chao Yu76481152018-04-08 11:27:14 +08001651 flags |= F2FS_ENCRYPT_FL;
1652 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1653 flags |= F2FS_INLINE_DATA_FL;
1654
1655 flags &= F2FS_FL_USER_VISIBLE;
1656
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001657 return put_user(flags, (int __user *)arg);
1658}
1659
Chao Yu2c1d0302017-07-29 00:32:52 +08001660static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1661{
1662 struct f2fs_inode_info *fi = F2FS_I(inode);
1663 unsigned int oldflags;
1664
1665 /* Is it quota file? Do not allow user to mess with it */
1666 if (IS_NOQUOTA(inode))
1667 return -EPERM;
1668
1669 flags = f2fs_mask_flags(inode->i_mode, flags);
1670
1671 oldflags = fi->i_flags;
1672
Chao Yu59c84402018-04-03 15:08:17 +08001673 if ((flags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
Chao Yu2c1d0302017-07-29 00:32:52 +08001674 if (!capable(CAP_LINUX_IMMUTABLE))
1675 return -EPERM;
1676
Chao Yuc807a7c2018-04-08 11:25:53 +08001677 flags = flags & F2FS_FL_USER_MODIFIABLE;
1678 flags |= oldflags & ~F2FS_FL_USER_MODIFIABLE;
Chao Yu2c1d0302017-07-29 00:32:52 +08001679 fi->i_flags = flags;
1680
Chao Yu59c84402018-04-03 15:08:17 +08001681 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001682 set_inode_flag(inode, FI_PROJ_INHERIT);
1683 else
1684 clear_inode_flag(inode, FI_PROJ_INHERIT);
1685
1686 inode->i_ctime = current_time(inode);
1687 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001688 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001689 return 0;
1690}
1691
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001692static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1693{
1694 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001695 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001696 int ret;
1697
Chao Yu7fb17fe2016-05-09 19:56:32 +08001698 if (!inode_owner_or_capable(inode))
1699 return -EACCES;
1700
1701 if (get_user(flags, (int __user *)arg))
1702 return -EFAULT;
1703
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001704 ret = mnt_want_write_file(filp);
1705 if (ret)
1706 return ret;
1707
Al Viro59551022016-01-22 15:40:57 -05001708 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001709
Chao Yu2c1d0302017-07-29 00:32:52 +08001710 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001711
Chao Yua72d4b92017-05-03 23:59:13 +08001712 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001713 mnt_drop_write_file(filp);
1714 return ret;
1715}
1716
Chao Yud49f3e82015-01-23 20:36:04 +08001717static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1718{
1719 struct inode *inode = file_inode(filp);
1720
1721 return put_user(inode->i_generation, (int __user *)arg);
1722}
1723
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001724static int f2fs_ioc_start_atomic_write(struct file *filp)
1725{
1726 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001727 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001728
1729 if (!inode_owner_or_capable(inode))
1730 return -EACCES;
1731
Jaegeuk Kime8118982017-03-17 10:04:15 +08001732 if (!S_ISREG(inode->i_mode))
1733 return -EINVAL;
1734
Chao Yu7fb17fe2016-05-09 19:56:32 +08001735 ret = mnt_want_write_file(filp);
1736 if (ret)
1737 return ret;
1738
Chao Yu0fac5582016-05-09 19:56:33 +08001739 inode_lock(inode);
1740
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001741 if (f2fs_is_atomic_file(inode)) {
1742 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1743 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001744 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001745 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001746
Chao Yuf4c9c742015-07-17 18:06:35 +08001747 ret = f2fs_convert_inline_inode(inode);
1748 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001749 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001750
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001751 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1752
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001753 if (!get_dirty_pages(inode))
Yunlei He054afda2018-04-18 11:06:39 +08001754 goto skip_flush;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001755
1756 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001757 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001758 inode->i_ino, get_dirty_pages(inode));
1759 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001760 if (ret) {
1761 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001762 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001763 }
Yunlei He054afda2018-04-18 11:06:39 +08001764skip_flush:
Yunlei He054afda2018-04-18 11:06:39 +08001765 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001766 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001767 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001768
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001769 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001770 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001771 stat_inc_atomic_write(inode);
1772 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001773out:
Chao Yu0fac5582016-05-09 19:56:33 +08001774 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001775 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001776 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001777}
1778
1779static int f2fs_ioc_commit_atomic_write(struct file *filp)
1780{
1781 struct inode *inode = file_inode(filp);
1782 int ret;
1783
1784 if (!inode_owner_or_capable(inode))
1785 return -EACCES;
1786
1787 ret = mnt_want_write_file(filp);
1788 if (ret)
1789 return ret;
1790
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001791 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08001792
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001793 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08001794
Chao Yub169c3c2018-04-18 17:45:02 +08001795 if (f2fs_is_volatile_file(inode)) {
1796 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001797 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08001798 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001799
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001800 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08001801 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001802 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001803 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001804
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001805 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001806 if (!ret) {
1807 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001808 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +08001809 stat_dec_atomic_write(inode);
1810 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001811 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001812 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001813 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001814err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08001815 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1816 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1817 ret = -EINVAL;
1818 }
Chao Yu0fac5582016-05-09 19:56:33 +08001819 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001820 mnt_drop_write_file(filp);
1821 return ret;
1822}
1823
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001824static int f2fs_ioc_start_volatile_write(struct file *filp)
1825{
1826 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001827 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001828
1829 if (!inode_owner_or_capable(inode))
1830 return -EACCES;
1831
Chao Yu8ff09712017-03-17 15:43:57 +08001832 if (!S_ISREG(inode->i_mode))
1833 return -EINVAL;
1834
Chao Yu7fb17fe2016-05-09 19:56:32 +08001835 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001836 if (ret)
1837 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001838
Chao Yu0fac5582016-05-09 19:56:33 +08001839 inode_lock(inode);
1840
Chao Yu7fb17fe2016-05-09 19:56:32 +08001841 if (f2fs_is_volatile_file(inode))
1842 goto out;
1843
1844 ret = f2fs_convert_inline_inode(inode);
1845 if (ret)
1846 goto out;
1847
Chao Yu648d50b2017-03-22 17:23:45 +08001848 stat_inc_volatile_write(inode);
1849 stat_update_max_volatile_write(inode);
1850
Jaegeuk Kim91942322016-05-20 10:13:22 -07001851 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001852 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001853out:
Chao Yu0fac5582016-05-09 19:56:33 +08001854 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001855 mnt_drop_write_file(filp);
1856 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001857}
1858
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001859static int f2fs_ioc_release_volatile_write(struct file *filp)
1860{
1861 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001862 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001863
1864 if (!inode_owner_or_capable(inode))
1865 return -EACCES;
1866
Chao Yu7fb17fe2016-05-09 19:56:32 +08001867 ret = mnt_want_write_file(filp);
1868 if (ret)
1869 return ret;
1870
Chao Yu0fac5582016-05-09 19:56:33 +08001871 inode_lock(inode);
1872
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001873 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001874 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001875
Chao Yu7fb17fe2016-05-09 19:56:32 +08001876 if (!f2fs_is_first_block_written(inode)) {
1877 ret = truncate_partial_data_page(inode, 0, true);
1878 goto out;
1879 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001880
Chao Yu7fb17fe2016-05-09 19:56:32 +08001881 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1882out:
Chao Yu0fac5582016-05-09 19:56:33 +08001883 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001884 mnt_drop_write_file(filp);
1885 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001886}
1887
1888static int f2fs_ioc_abort_volatile_write(struct file *filp)
1889{
1890 struct inode *inode = file_inode(filp);
1891 int ret;
1892
1893 if (!inode_owner_or_capable(inode))
1894 return -EACCES;
1895
1896 ret = mnt_want_write_file(filp);
1897 if (ret)
1898 return ret;
1899
Chao Yu0fac5582016-05-09 19:56:33 +08001900 inode_lock(inode);
1901
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001902 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001903 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001904 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001905 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001906 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001907 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001908 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001909
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001910 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1911
Chao Yu0fac5582016-05-09 19:56:33 +08001912 inode_unlock(inode);
1913
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001914 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001915 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001916 return ret;
1917}
1918
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001919static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1920{
1921 struct inode *inode = file_inode(filp);
1922 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1923 struct super_block *sb = sbi->sb;
1924 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03001925 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001926
1927 if (!capable(CAP_SYS_ADMIN))
1928 return -EPERM;
1929
1930 if (get_user(in, (__u32 __user *)arg))
1931 return -EFAULT;
1932
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301933 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1934 ret = mnt_want_write_file(filp);
1935 if (ret)
1936 return ret;
1937 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001938
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001939 switch (in) {
1940 case F2FS_GOING_DOWN_FULLSYNC:
1941 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08001942 if (IS_ERR(sb)) {
1943 ret = PTR_ERR(sb);
1944 goto out;
1945 }
1946 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001947 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001948 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001949 thaw_bdev(sb->s_bdev, sb);
1950 }
1951 break;
1952 case F2FS_GOING_DOWN_METASYNC:
1953 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08001954 ret = f2fs_sync_fs(sb, 1);
1955 if (ret)
1956 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001957 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001958 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001959 break;
1960 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001961 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001962 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001963 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001964 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08001965 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001966 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001967 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001968 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08001969 case F2FS_GOING_DOWN_NEED_FSCK:
1970 set_sbi_flag(sbi, SBI_NEED_FSCK);
1971 /* do checkpoint only */
1972 ret = f2fs_sync_fs(sb, 1);
1973 if (ret)
1974 goto out;
1975 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001976 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001977 ret = -EINVAL;
1978 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001979 }
Chao Yu7950e9a2018-01-18 17:23:29 +08001980
Chao Yu4d57b862018-05-30 00:20:41 +08001981 f2fs_stop_gc_thread(sbi);
1982 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001983
Chao Yu4d57b862018-05-30 00:20:41 +08001984 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001985 clear_opt(sbi, DISCARD);
1986
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001987 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001988out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301989 if (in != F2FS_GOING_DOWN_FULLSYNC)
1990 mnt_drop_write_file(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001991 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001992}
1993
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001994static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1995{
1996 struct inode *inode = file_inode(filp);
1997 struct super_block *sb = inode->i_sb;
1998 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1999 struct fstrim_range range;
2000 int ret;
2001
2002 if (!capable(CAP_SYS_ADMIN))
2003 return -EPERM;
2004
Chao Yu7d20c8a2018-09-04 03:52:17 +08002005 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002006 return -EOPNOTSUPP;
2007
2008 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2009 sizeof(range)))
2010 return -EFAULT;
2011
Chao Yu7fb17fe2016-05-09 19:56:32 +08002012 ret = mnt_want_write_file(filp);
2013 if (ret)
2014 return ret;
2015
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002016 range.minlen = max((unsigned int)range.minlen,
2017 q->limits.discard_granularity);
2018 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002019 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002020 if (ret < 0)
2021 return ret;
2022
2023 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2024 sizeof(range)))
2025 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002026 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002027 return 0;
2028}
2029
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002030static bool uuid_is_nonzero(__u8 u[16])
2031{
2032 int i;
2033
2034 for (i = 0; i < 16; i++)
2035 if (u[i])
2036 return true;
2037 return false;
2038}
2039
2040static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2041{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002042 struct inode *inode = file_inode(filp);
2043
Chao Yu7beb01f2018-10-24 18:34:26 +08002044 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002045 return -EOPNOTSUPP;
2046
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002047 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002048
Eric Biggersdb717d82016-11-26 19:07:49 -05002049 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002050}
2051
2052static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2053{
Chao Yu7beb01f2018-10-24 18:34:26 +08002054 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002055 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002056 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002057}
2058
2059static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2060{
2061 struct inode *inode = file_inode(filp);
2062 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2063 int err;
2064
Chao Yu7beb01f2018-10-24 18:34:26 +08002065 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002066 return -EOPNOTSUPP;
2067
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002068 err = mnt_want_write_file(filp);
2069 if (err)
2070 return err;
2071
Chao Yu846ae672018-02-26 22:04:13 +08002072 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002073
2074 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2075 goto got_it;
2076
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002077 /* update superblock with uuid */
2078 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2079
Chao Yuc5bda1c2015-06-08 13:28:03 +08002080 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002081 if (err) {
2082 /* undo new data */
2083 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002084 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002085 }
2086got_it:
2087 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2088 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002089 err = -EFAULT;
2090out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002091 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002092 mnt_drop_write_file(filp);
2093 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002094}
2095
Chao Yuc1c1b582015-07-10 18:08:10 +08002096static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2097{
2098 struct inode *inode = file_inode(filp);
2099 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002100 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002101 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002102
2103 if (!capable(CAP_SYS_ADMIN))
2104 return -EPERM;
2105
Chao Yud530d4d2015-10-05 22:22:44 +08002106 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002107 return -EFAULT;
2108
Chao Yud530d4d2015-10-05 22:22:44 +08002109 if (f2fs_readonly(sbi->sb))
2110 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002111
Chao Yu7fb17fe2016-05-09 19:56:32 +08002112 ret = mnt_want_write_file(filp);
2113 if (ret)
2114 return ret;
2115
Chao Yud530d4d2015-10-05 22:22:44 +08002116 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002117 if (!mutex_trylock(&sbi->gc_mutex)) {
2118 ret = -EBUSY;
2119 goto out;
2120 }
Chao Yud530d4d2015-10-05 22:22:44 +08002121 } else {
2122 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002123 }
2124
Jaegeuk Kime066b832017-04-13 15:17:00 -07002125 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002126out:
2127 mnt_drop_write_file(filp);
2128 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002129}
2130
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002131static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2132{
2133 struct inode *inode = file_inode(filp);
2134 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2135 struct f2fs_gc_range range;
2136 u64 end;
2137 int ret;
2138
2139 if (!capable(CAP_SYS_ADMIN))
2140 return -EPERM;
2141
2142 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2143 sizeof(range)))
2144 return -EFAULT;
2145
2146 if (f2fs_readonly(sbi->sb))
2147 return -EROFS;
2148
Yunlei Heb82f6e32018-04-24 11:40:30 +08002149 end = range.start + range.len;
2150 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
2151 return -EINVAL;
2152 }
2153
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002154 ret = mnt_want_write_file(filp);
2155 if (ret)
2156 return ret;
2157
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002158do_more:
2159 if (!range.sync) {
2160 if (!mutex_trylock(&sbi->gc_mutex)) {
2161 ret = -EBUSY;
2162 goto out;
2163 }
2164 } else {
2165 mutex_lock(&sbi->gc_mutex);
2166 }
2167
2168 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Yunlong Song67b0e422018-10-30 20:37:55 +08002169 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002170 if (range.start <= end)
2171 goto do_more;
2172out:
2173 mnt_drop_write_file(filp);
2174 return ret;
2175}
2176
Chao Yu059c0642018-07-17 20:41:49 +08002177static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002178{
2179 struct inode *inode = file_inode(filp);
2180 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002181 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002182
2183 if (!capable(CAP_SYS_ADMIN))
2184 return -EPERM;
2185
2186 if (f2fs_readonly(sbi->sb))
2187 return -EROFS;
2188
Daniel Rosenberg43549942018-08-20 19:21:43 -07002189 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2190 f2fs_msg(sbi->sb, KERN_INFO,
2191 "Skipping Checkpoint. Checkpoints currently disabled.");
2192 return -EINVAL;
2193 }
2194
Chao Yu7fb17fe2016-05-09 19:56:32 +08002195 ret = mnt_want_write_file(filp);
2196 if (ret)
2197 return ret;
2198
2199 ret = f2fs_sync_fs(sbi->sb, 1);
2200
2201 mnt_drop_write_file(filp);
2202 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002203}
2204
Chao Yud323d002015-10-27 09:53:45 +08002205static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2206 struct file *filp,
2207 struct f2fs_defragment *range)
2208{
2209 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002210 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002211 .m_seg_type = NO_CHECK_TYPE ,
2212 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002213 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002214 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002215 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002216 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002217 block_t blk_end = 0;
2218 bool fragmented = false;
2219 int err;
2220
2221 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002222 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002223 return -EINVAL;
2224
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002225 pg_start = range->start >> PAGE_SHIFT;
2226 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002227
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002228 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002229
Al Viro59551022016-01-22 15:40:57 -05002230 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002231
2232 /* writeback all dirty pages in the range */
2233 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002234 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002235 if (err)
2236 goto out;
2237
2238 /*
2239 * lookup mapping info in extent cache, skip defragmenting if physical
2240 * block addresses are continuous.
2241 */
2242 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2243 if (ei.fofs + ei.len >= pg_end)
2244 goto out;
2245 }
2246
2247 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002248 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002249
2250 /*
2251 * lookup mapping info in dnode page cache, skip defragmenting if all
2252 * physical block addresses are continuous even if there are hole(s)
2253 * in logical blocks.
2254 */
2255 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002256 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002257 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002258 if (err)
2259 goto out;
2260
2261 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002262 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002263 continue;
2264 }
2265
Chao Yu25a912e2018-01-10 18:18:51 +08002266 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002267 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002268
2269 /* record total count of block that we're going to move */
2270 total += map.m_len;
2271
Chao Yud323d002015-10-27 09:53:45 +08002272 blk_end = map.m_pblk + map.m_len;
2273
2274 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002275 }
2276
2277 if (!fragmented)
2278 goto out;
2279
Chao Yu25a912e2018-01-10 18:18:51 +08002280 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002281
2282 /*
2283 * make sure there are enough free section for LFS allocation, this can
2284 * avoid defragment running in SSR mode when free section are allocated
2285 * intensively
2286 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002287 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002288 err = -EAGAIN;
2289 goto out;
2290 }
2291
Chao Yu25a912e2018-01-10 18:18:51 +08002292 map.m_lblk = pg_start;
2293 map.m_len = pg_end - pg_start;
2294 total = 0;
2295
Chao Yud323d002015-10-27 09:53:45 +08002296 while (map.m_lblk < pg_end) {
2297 pgoff_t idx;
2298 int cnt = 0;
2299
2300do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002301 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002302 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002303 if (err)
2304 goto clear_out;
2305
2306 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002307 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002308 continue;
2309 }
2310
Jaegeuk Kim91942322016-05-20 10:13:22 -07002311 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002312
2313 idx = map.m_lblk;
2314 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2315 struct page *page;
2316
Chao Yu4d57b862018-05-30 00:20:41 +08002317 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002318 if (IS_ERR(page)) {
2319 err = PTR_ERR(page);
2320 goto clear_out;
2321 }
2322
2323 set_page_dirty(page);
2324 f2fs_put_page(page, 1);
2325
2326 idx++;
2327 cnt++;
2328 total++;
2329 }
2330
2331 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002332
2333 if (idx < pg_end && cnt < blk_per_seg)
2334 goto do_map;
2335
Jaegeuk Kim91942322016-05-20 10:13:22 -07002336 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002337
2338 err = filemap_fdatawrite(inode->i_mapping);
2339 if (err)
2340 goto out;
2341 }
2342clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002343 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002344out:
Al Viro59551022016-01-22 15:40:57 -05002345 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002346 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002347 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002348 return err;
2349}
2350
2351static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2352{
2353 struct inode *inode = file_inode(filp);
2354 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2355 struct f2fs_defragment range;
2356 int err;
2357
2358 if (!capable(CAP_SYS_ADMIN))
2359 return -EPERM;
2360
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002361 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002362 return -EINVAL;
2363
Kinglong Meed7563862017-03-10 17:55:07 +08002364 if (f2fs_readonly(sbi->sb))
2365 return -EROFS;
2366
2367 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2368 sizeof(range)))
2369 return -EFAULT;
2370
2371 /* verify alignment of offset & size */
2372 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2373 return -EINVAL;
2374
2375 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2376 sbi->max_file_blocks))
2377 return -EINVAL;
2378
Chao Yud323d002015-10-27 09:53:45 +08002379 err = mnt_want_write_file(filp);
2380 if (err)
2381 return err;
2382
Chao Yud323d002015-10-27 09:53:45 +08002383 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002384 mnt_drop_write_file(filp);
2385
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002386 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002387 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002388 return err;
Chao Yud323d002015-10-27 09:53:45 +08002389
2390 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2391 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002392 return -EFAULT;
2393
2394 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002395}
2396
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002397static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2398 struct file *file_out, loff_t pos_out, size_t len)
2399{
2400 struct inode *src = file_inode(file_in);
2401 struct inode *dst = file_inode(file_out);
2402 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2403 size_t olen = len, dst_max_i_size = 0;
2404 size_t dst_osize;
2405 int ret;
2406
2407 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2408 src->i_sb != dst->i_sb)
2409 return -EXDEV;
2410
2411 if (unlikely(f2fs_readonly(src->i_sb)))
2412 return -EROFS;
2413
Chao Yufe8494b2016-08-04 20:13:02 +08002414 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2415 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002416
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302417 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002418 return -EOPNOTSUPP;
2419
Fan Lid95fd912016-09-13 11:35:42 +08002420 if (src == dst) {
2421 if (pos_in == pos_out)
2422 return 0;
2423 if (pos_out > pos_in && pos_out < pos_in + len)
2424 return -EINVAL;
2425 }
2426
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002427 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002428 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002429 ret = -EBUSY;
2430 if (!inode_trylock(dst))
2431 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002432 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002433
2434 ret = -EINVAL;
2435 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2436 goto out_unlock;
2437 if (len == 0)
2438 olen = len = src->i_size - pos_in;
2439 if (pos_in + len == src->i_size)
2440 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2441 if (len == 0) {
2442 ret = 0;
2443 goto out_unlock;
2444 }
2445
2446 dst_osize = dst->i_size;
2447 if (pos_out + olen > dst->i_size)
2448 dst_max_i_size = pos_out + olen;
2449
2450 /* verify the end result is block aligned */
2451 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2452 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2453 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2454 goto out_unlock;
2455
2456 ret = f2fs_convert_inline_inode(src);
2457 if (ret)
2458 goto out_unlock;
2459
2460 ret = f2fs_convert_inline_inode(dst);
2461 if (ret)
2462 goto out_unlock;
2463
2464 /* write out all dirty pages from offset */
2465 ret = filemap_write_and_wait_range(src->i_mapping,
2466 pos_in, pos_in + len);
2467 if (ret)
2468 goto out_unlock;
2469
2470 ret = filemap_write_and_wait_range(dst->i_mapping,
2471 pos_out, pos_out + len);
2472 if (ret)
2473 goto out_unlock;
2474
2475 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002476
2477 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2478 if (src != dst) {
2479 ret = -EBUSY;
2480 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2481 goto out_src;
2482 }
2483
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002484 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002485 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2486 pos_out >> F2FS_BLKSIZE_BITS,
2487 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002488
2489 if (!ret) {
2490 if (dst_max_i_size)
2491 f2fs_i_size_write(dst, dst_max_i_size);
2492 else if (dst_osize != dst->i_size)
2493 f2fs_i_size_write(dst, dst_osize);
2494 }
2495 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002496
2497 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002498 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002499out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002500 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002501out_unlock:
2502 if (src != dst)
2503 inode_unlock(dst);
2504out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002505 inode_unlock(src);
2506 return ret;
2507}
2508
2509static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2510{
2511 struct f2fs_move_range range;
2512 struct fd dst;
2513 int err;
2514
2515 if (!(filp->f_mode & FMODE_READ) ||
2516 !(filp->f_mode & FMODE_WRITE))
2517 return -EBADF;
2518
2519 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2520 sizeof(range)))
2521 return -EFAULT;
2522
2523 dst = fdget(range.dst_fd);
2524 if (!dst.file)
2525 return -EBADF;
2526
2527 if (!(dst.file->f_mode & FMODE_WRITE)) {
2528 err = -EBADF;
2529 goto err_out;
2530 }
2531
2532 err = mnt_want_write_file(filp);
2533 if (err)
2534 goto err_out;
2535
2536 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2537 range.pos_out, range.len);
2538
2539 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002540 if (err)
2541 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002542
2543 if (copy_to_user((struct f2fs_move_range __user *)arg,
2544 &range, sizeof(range)))
2545 err = -EFAULT;
2546err_out:
2547 fdput(dst);
2548 return err;
2549}
2550
Jaegeuk Kime066b832017-04-13 15:17:00 -07002551static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2552{
2553 struct inode *inode = file_inode(filp);
2554 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2555 struct sit_info *sm = SIT_I(sbi);
2556 unsigned int start_segno = 0, end_segno = 0;
2557 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2558 struct f2fs_flush_device range;
2559 int ret;
2560
2561 if (!capable(CAP_SYS_ADMIN))
2562 return -EPERM;
2563
2564 if (f2fs_readonly(sbi->sb))
2565 return -EROFS;
2566
Daniel Rosenberg43549942018-08-20 19:21:43 -07002567 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2568 return -EINVAL;
2569
Jaegeuk Kime066b832017-04-13 15:17:00 -07002570 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2571 sizeof(range)))
2572 return -EFAULT;
2573
2574 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002575 __is_large_section(sbi)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002576 f2fs_msg(sbi->sb, KERN_WARNING,
2577 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2578 range.dev_num, sbi->s_ndevs,
2579 sbi->segs_per_sec);
2580 return -EINVAL;
2581 }
2582
2583 ret = mnt_want_write_file(filp);
2584 if (ret)
2585 return ret;
2586
2587 if (range.dev_num != 0)
2588 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2589 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2590
2591 start_segno = sm->last_victim[FLUSH_DEVICE];
2592 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2593 start_segno = dev_start_segno;
2594 end_segno = min(start_segno + range.segments, dev_end_segno);
2595
2596 while (start_segno < end_segno) {
2597 if (!mutex_trylock(&sbi->gc_mutex)) {
2598 ret = -EBUSY;
2599 goto out;
2600 }
2601 sm->last_victim[GC_CB] = end_segno + 1;
2602 sm->last_victim[GC_GREEDY] = end_segno + 1;
2603 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2604 ret = f2fs_gc(sbi, true, true, start_segno);
2605 if (ret == -EAGAIN)
2606 ret = 0;
2607 else if (ret < 0)
2608 break;
2609 start_segno++;
2610 }
2611out:
2612 mnt_drop_write_file(filp);
2613 return ret;
2614}
2615
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002616static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2617{
2618 struct inode *inode = file_inode(filp);
2619 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2620
2621 /* Must validate to set it with SQLite behavior in Android. */
2622 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2623
2624 return put_user(sb_feature, (u32 __user *)arg);
2625}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002626
Chao Yu2c1d0302017-07-29 00:32:52 +08002627#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002628int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2629{
2630 struct dquot *transfer_to[MAXQUOTAS] = {};
2631 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2632 struct super_block *sb = sbi->sb;
2633 int err = 0;
2634
2635 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2636 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2637 err = __dquot_transfer(inode, transfer_to);
2638 if (err)
2639 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2640 dqput(transfer_to[PRJQUOTA]);
2641 }
2642 return err;
2643}
2644
Chao Yu2c1d0302017-07-29 00:32:52 +08002645static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2646{
2647 struct inode *inode = file_inode(filp);
2648 struct f2fs_inode_info *fi = F2FS_I(inode);
2649 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002650 struct page *ipage;
2651 kprojid_t kprojid;
2652 int err;
2653
Chao Yu7beb01f2018-10-24 18:34:26 +08002654 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08002655 if (projid != F2FS_DEF_PROJID)
2656 return -EOPNOTSUPP;
2657 else
2658 return 0;
2659 }
2660
2661 if (!f2fs_has_extra_attr(inode))
2662 return -EOPNOTSUPP;
2663
2664 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2665
2666 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2667 return 0;
2668
Chao Yu2c1d0302017-07-29 00:32:52 +08002669 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08002670 /* Is it quota file? Do not allow user to mess with it */
2671 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09002672 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002673
Chao Yu4d57b862018-05-30 00:20:41 +08002674 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09002675 if (IS_ERR(ipage))
2676 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08002677
2678 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2679 i_projid)) {
2680 err = -EOVERFLOW;
2681 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09002682 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002683 }
2684 f2fs_put_page(ipage, 1);
2685
Chao Yuc22aecd2018-04-21 17:53:52 +08002686 err = dquot_initialize(inode);
2687 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002688 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002689
Chao Yu78130812018-09-25 15:36:02 +08002690 f2fs_lock_op(sbi);
2691 err = f2fs_transfer_project_quota(inode, kprojid);
2692 if (err)
2693 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08002694
2695 F2FS_I(inode)->i_projid = kprojid;
2696 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002697 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08002698out_unlock:
2699 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08002700 return err;
2701}
2702#else
Chao Yu78130812018-09-25 15:36:02 +08002703int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2704{
2705 return 0;
2706}
2707
Chao Yu2c1d0302017-07-29 00:32:52 +08002708static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2709{
2710 if (projid != F2FS_DEF_PROJID)
2711 return -EOPNOTSUPP;
2712 return 0;
2713}
2714#endif
2715
2716/* Transfer internal flags to xflags */
2717static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2718{
2719 __u32 xflags = 0;
2720
Chao Yu59c84402018-04-03 15:08:17 +08002721 if (iflags & F2FS_SYNC_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002722 xflags |= FS_XFLAG_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +08002723 if (iflags & F2FS_IMMUTABLE_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002724 xflags |= FS_XFLAG_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +08002725 if (iflags & F2FS_APPEND_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002726 xflags |= FS_XFLAG_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +08002727 if (iflags & F2FS_NODUMP_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002728 xflags |= FS_XFLAG_NODUMP;
Chao Yu59c84402018-04-03 15:08:17 +08002729 if (iflags & F2FS_NOATIME_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002730 xflags |= FS_XFLAG_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +08002731 if (iflags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002732 xflags |= FS_XFLAG_PROJINHERIT;
2733 return xflags;
2734}
2735
2736#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2737 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2738 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2739
Chao Yu2c1d0302017-07-29 00:32:52 +08002740/* Transfer xflags flags to internal */
2741static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2742{
2743 unsigned long iflags = 0;
2744
2745 if (xflags & FS_XFLAG_SYNC)
Chao Yu59c84402018-04-03 15:08:17 +08002746 iflags |= F2FS_SYNC_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002747 if (xflags & FS_XFLAG_IMMUTABLE)
Chao Yu59c84402018-04-03 15:08:17 +08002748 iflags |= F2FS_IMMUTABLE_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002749 if (xflags & FS_XFLAG_APPEND)
Chao Yu59c84402018-04-03 15:08:17 +08002750 iflags |= F2FS_APPEND_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002751 if (xflags & FS_XFLAG_NODUMP)
Chao Yu59c84402018-04-03 15:08:17 +08002752 iflags |= F2FS_NODUMP_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002753 if (xflags & FS_XFLAG_NOATIME)
Chao Yu59c84402018-04-03 15:08:17 +08002754 iflags |= F2FS_NOATIME_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002755 if (xflags & FS_XFLAG_PROJINHERIT)
Chao Yu59c84402018-04-03 15:08:17 +08002756 iflags |= F2FS_PROJINHERIT_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002757
2758 return iflags;
2759}
2760
2761static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2762{
2763 struct inode *inode = file_inode(filp);
2764 struct f2fs_inode_info *fi = F2FS_I(inode);
2765 struct fsxattr fa;
2766
2767 memset(&fa, 0, sizeof(struct fsxattr));
2768 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
Chao Yuc807a7c2018-04-08 11:25:53 +08002769 F2FS_FL_USER_VISIBLE);
Chao Yu2c1d0302017-07-29 00:32:52 +08002770
Chao Yu7beb01f2018-10-24 18:34:26 +08002771 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
Chao Yu2c1d0302017-07-29 00:32:52 +08002772 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2773 fi->i_projid);
2774
2775 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2776 return -EFAULT;
2777 return 0;
2778}
2779
Wang Shilongc8e92752018-09-11 08:54:21 +09002780static int f2fs_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
2781{
2782 /*
2783 * Project Quota ID state is only allowed to change from within the init
2784 * namespace. Enforce that restriction only if we are trying to change
2785 * the quota ID state. Everything else is allowed in user namespaces.
2786 */
2787 if (current_user_ns() == &init_user_ns)
2788 return 0;
2789
2790 if (__kprojid_val(F2FS_I(inode)->i_projid) != fa->fsx_projid)
2791 return -EINVAL;
2792
2793 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) {
2794 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
2795 return -EINVAL;
2796 } else {
2797 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
2798 return -EINVAL;
2799 }
2800
2801 return 0;
2802}
2803
Chao Yu2c1d0302017-07-29 00:32:52 +08002804static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2805{
2806 struct inode *inode = file_inode(filp);
2807 struct f2fs_inode_info *fi = F2FS_I(inode);
2808 struct fsxattr fa;
2809 unsigned int flags;
2810 int err;
2811
2812 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2813 return -EFAULT;
2814
2815 /* Make sure caller has proper permission */
2816 if (!inode_owner_or_capable(inode))
2817 return -EACCES;
2818
2819 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2820 return -EOPNOTSUPP;
2821
2822 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2823 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2824 return -EOPNOTSUPP;
2825
2826 err = mnt_want_write_file(filp);
2827 if (err)
2828 return err;
2829
2830 inode_lock(inode);
Wang Shilongc8e92752018-09-11 08:54:21 +09002831 err = f2fs_ioctl_check_project(inode, &fa);
2832 if (err)
2833 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08002834 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2835 (flags & F2FS_FL_XFLAG_VISIBLE);
2836 err = __f2fs_ioc_setflags(inode, flags);
Chao Yu2c1d0302017-07-29 00:32:52 +08002837 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002838 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08002839
2840 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09002841out:
2842 inode_unlock(inode);
2843 mnt_drop_write_file(filp);
2844 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002845}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002846
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002847int f2fs_pin_file_control(struct inode *inode, bool inc)
2848{
2849 struct f2fs_inode_info *fi = F2FS_I(inode);
2850 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2851
2852 /* Use i_gc_failures for normal file as a risk signal. */
2853 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08002854 f2fs_i_gc_failures_write(inode,
2855 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002856
Chao Yu2ef79ec2018-05-07 20:28:54 +08002857 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002858 f2fs_msg(sbi->sb, KERN_WARNING,
2859 "%s: Enable GC = ino %lx after %x GC trials\n",
Chao Yu2ef79ec2018-05-07 20:28:54 +08002860 __func__, inode->i_ino,
2861 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002862 clear_inode_flag(inode, FI_PIN_FILE);
2863 return -EAGAIN;
2864 }
2865 return 0;
2866}
2867
2868static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2869{
2870 struct inode *inode = file_inode(filp);
2871 __u32 pin;
2872 int ret = 0;
2873
2874 if (!inode_owner_or_capable(inode))
2875 return -EACCES;
2876
2877 if (get_user(pin, (__u32 __user *)arg))
2878 return -EFAULT;
2879
2880 if (!S_ISREG(inode->i_mode))
2881 return -EINVAL;
2882
2883 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2884 return -EROFS;
2885
2886 ret = mnt_want_write_file(filp);
2887 if (ret)
2888 return ret;
2889
2890 inode_lock(inode);
2891
Chao Yu4d57b862018-05-30 00:20:41 +08002892 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002893 ret = -EINVAL;
2894 goto out;
2895 }
2896
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002897 if (!pin) {
2898 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08002899 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002900 goto done;
2901 }
2902
2903 if (f2fs_pin_file_control(inode, false)) {
2904 ret = -EAGAIN;
2905 goto out;
2906 }
2907 ret = f2fs_convert_inline_inode(inode);
2908 if (ret)
2909 goto out;
2910
2911 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002912 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002913done:
2914 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2915out:
2916 inode_unlock(inode);
2917 mnt_drop_write_file(filp);
2918 return ret;
2919}
2920
2921static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2922{
2923 struct inode *inode = file_inode(filp);
2924 __u32 pin = 0;
2925
2926 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08002927 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002928 return put_user(pin, (u32 __user *)arg);
2929}
2930
Chao Yuc4020b22018-01-11 14:42:30 +08002931int f2fs_precache_extents(struct inode *inode)
2932{
2933 struct f2fs_inode_info *fi = F2FS_I(inode);
2934 struct f2fs_map_blocks map;
2935 pgoff_t m_next_extent;
2936 loff_t end;
2937 int err;
2938
2939 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2940 return -EOPNOTSUPP;
2941
2942 map.m_lblk = 0;
2943 map.m_next_pgofs = NULL;
2944 map.m_next_extent = &m_next_extent;
2945 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08002946 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08002947 end = F2FS_I_SB(inode)->max_file_blocks;
2948
2949 while (map.m_lblk < end) {
2950 map.m_len = end - map.m_lblk;
2951
Chao Yub2532c62018-04-24 10:55:28 +08002952 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002953 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08002954 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002955 if (err)
2956 return err;
2957
2958 map.m_lblk = m_next_extent;
2959 }
2960
2961 return err;
2962}
2963
2964static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2965{
2966 return f2fs_precache_extents(file_inode(filp));
2967}
2968
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002969long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2970{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002971 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2972 return -EIO;
2973
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002974 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002975 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002976 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002977 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002978 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002979 case F2FS_IOC_GETVERSION:
2980 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002981 case F2FS_IOC_START_ATOMIC_WRITE:
2982 return f2fs_ioc_start_atomic_write(filp);
2983 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2984 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002985 case F2FS_IOC_START_VOLATILE_WRITE:
2986 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002987 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2988 return f2fs_ioc_release_volatile_write(filp);
2989 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2990 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002991 case F2FS_IOC_SHUTDOWN:
2992 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002993 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002994 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002995 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2996 return f2fs_ioc_set_encryption_policy(filp, arg);
2997 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2998 return f2fs_ioc_get_encryption_policy(filp, arg);
2999 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3000 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003001 case F2FS_IOC_GARBAGE_COLLECT:
3002 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003003 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3004 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08003005 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08003006 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08003007 case F2FS_IOC_DEFRAGMENT:
3008 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003009 case F2FS_IOC_MOVE_RANGE:
3010 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07003011 case F2FS_IOC_FLUSH_DEVICE:
3012 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003013 case F2FS_IOC_GET_FEATURES:
3014 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08003015 case F2FS_IOC_FSGETXATTR:
3016 return f2fs_ioc_fsgetxattr(filp, arg);
3017 case F2FS_IOC_FSSETXATTR:
3018 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003019 case F2FS_IOC_GET_PIN_FILE:
3020 return f2fs_ioc_get_pin_file(filp, arg);
3021 case F2FS_IOC_SET_PIN_FILE:
3022 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08003023 case F2FS_IOC_PRECACHE_EXTENTS:
3024 return f2fs_ioc_precache_extents(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003025 default:
3026 return -ENOTTY;
3027 }
3028}
3029
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003030static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3031{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003032 struct file *file = iocb->ki_filp;
3033 struct inode *inode = file_inode(file);
3034 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003035
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003036 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3037 return -EIO;
3038
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003039 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
3040 return -EINVAL;
3041
3042 if (!inode_trylock(inode)) {
3043 if (iocb->ki_flags & IOCB_NOWAIT)
3044 return -EAGAIN;
3045 inode_lock(inode);
3046 }
3047
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003048 ret = generic_write_checks(iocb, from);
3049 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003050 bool preallocated = false;
3051 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003052 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003053
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003054 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3055 set_inode_flag(inode, FI_NO_PREALLOC);
3056
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003057 if ((iocb->ki_flags & IOCB_NOWAIT) &&
3058 (iocb->ki_flags & IOCB_DIRECT)) {
3059 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
3060 iov_iter_count(from)) ||
3061 f2fs_has_inline_data(inode) ||
Chao Yuf847c692018-09-27 18:34:52 +08003062 f2fs_force_buffered_io(inode,
3063 iocb, from)) {
Yunlei Hecba41be2018-04-24 11:40:19 +08003064 clear_inode_flag(inode,
3065 FI_NO_PREALLOC);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003066 inode_unlock(inode);
3067 return -EAGAIN;
3068 }
3069
3070 } else {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003071 preallocated = true;
3072 target_size = iocb->ki_pos + iov_iter_count(from);
3073
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003074 err = f2fs_preallocate_blocks(iocb, from);
3075 if (err) {
3076 clear_inode_flag(inode, FI_NO_PREALLOC);
3077 inode_unlock(inode);
3078 return err;
3079 }
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003080 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003081 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003082 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003083
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003084 /* if we couldn't write data, we should deallocate blocks. */
3085 if (preallocated && i_size_read(inode) < target_size)
3086 f2fs_truncate(inode);
3087
Chao Yub0af6d42017-08-02 23:21:48 +08003088 if (ret > 0)
3089 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003090 }
3091 inode_unlock(inode);
3092
Christoph Hellwige2592212016-04-07 08:52:01 -07003093 if (ret > 0)
3094 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003095 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003096}
3097
Namjae Jeone9750822013-02-04 23:41:41 +09003098#ifdef CONFIG_COMPAT
3099long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3100{
3101 switch (cmd) {
3102 case F2FS_IOC32_GETFLAGS:
3103 cmd = F2FS_IOC_GETFLAGS;
3104 break;
3105 case F2FS_IOC32_SETFLAGS:
3106 cmd = F2FS_IOC_SETFLAGS;
3107 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003108 case F2FS_IOC32_GETVERSION:
3109 cmd = F2FS_IOC_GETVERSION;
3110 break;
3111 case F2FS_IOC_START_ATOMIC_WRITE:
3112 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3113 case F2FS_IOC_START_VOLATILE_WRITE:
3114 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3115 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3116 case F2FS_IOC_SHUTDOWN:
3117 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3118 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3119 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3120 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003121 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003122 case F2FS_IOC_WRITE_CHECKPOINT:
3123 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003124 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003125 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003126 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003127 case F2FS_IOC_FSGETXATTR:
3128 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003129 case F2FS_IOC_GET_PIN_FILE:
3130 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003131 case F2FS_IOC_PRECACHE_EXTENTS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003132 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003133 default:
3134 return -ENOIOCTLCMD;
3135 }
3136 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3137}
3138#endif
3139
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003140const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003141 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04003142 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003143 .write_iter = f2fs_file_write_iter,
3144 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003145 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003146 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003147 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003148 .fsync = f2fs_sync_file,
3149 .fallocate = f2fs_fallocate,
3150 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003151#ifdef CONFIG_COMPAT
3152 .compat_ioctl = f2fs_compat_ioctl,
3153#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003154 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003155 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003156};