blob: fe6f92fbba38de2f12a0dce128f0e14a725c7a63 [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. */
585 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
586 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;
714 if (f2fs_encrypted_inode(inode))
715 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 Kim9ab70132014-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 Kim363cad72016-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 Kim363cad72016-07-16 21:59:22 -07001161 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001162 int ret;
1163
Jaegeuk Kim363cad72016-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 Kim363cad72016-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 Kim363cad72016-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 Kim363cad72016-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
Chao Yuf62185d2015-05-28 19:16:57 +08001566 if (f2fs_encrypted_inode(inode) &&
1567 (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
Chao Yu5b72d5e2018-07-17 20:41:47 +08001650 if (f2fs_encrypted_inode(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;
Jaegeuk Kim5d539242019-01-03 17:19:08 -08001654 if (is_inode_flag_set(inode, FI_PIN_FILE))
1655 flags |= F2FS_NOCOW_FL;
Chao Yu76481152018-04-08 11:27:14 +08001656
1657 flags &= F2FS_FL_USER_VISIBLE;
1658
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001659 return put_user(flags, (int __user *)arg);
1660}
1661
Chao Yu2c1d0302017-07-29 00:32:52 +08001662static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1663{
1664 struct f2fs_inode_info *fi = F2FS_I(inode);
1665 unsigned int oldflags;
1666
1667 /* Is it quota file? Do not allow user to mess with it */
1668 if (IS_NOQUOTA(inode))
1669 return -EPERM;
1670
1671 flags = f2fs_mask_flags(inode->i_mode, flags);
1672
1673 oldflags = fi->i_flags;
1674
Chao Yu59c84402018-04-03 15:08:17 +08001675 if ((flags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
Chao Yu2c1d0302017-07-29 00:32:52 +08001676 if (!capable(CAP_LINUX_IMMUTABLE))
1677 return -EPERM;
1678
Chao Yuc807a7c2018-04-08 11:25:53 +08001679 flags = flags & F2FS_FL_USER_MODIFIABLE;
1680 flags |= oldflags & ~F2FS_FL_USER_MODIFIABLE;
Chao Yu2c1d0302017-07-29 00:32:52 +08001681 fi->i_flags = flags;
1682
Chao Yu59c84402018-04-03 15:08:17 +08001683 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001684 set_inode_flag(inode, FI_PROJ_INHERIT);
1685 else
1686 clear_inode_flag(inode, FI_PROJ_INHERIT);
1687
1688 inode->i_ctime = current_time(inode);
1689 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001690 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001691 return 0;
1692}
1693
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001694static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1695{
1696 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001697 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001698 int ret;
1699
Chao Yu7fb17fe2016-05-09 19:56:32 +08001700 if (!inode_owner_or_capable(inode))
1701 return -EACCES;
1702
1703 if (get_user(flags, (int __user *)arg))
1704 return -EFAULT;
1705
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001706 ret = mnt_want_write_file(filp);
1707 if (ret)
1708 return ret;
1709
Al Viro59551022016-01-22 15:40:57 -05001710 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001711
Chao Yu2c1d0302017-07-29 00:32:52 +08001712 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001713
Chao Yua72d4b92017-05-03 23:59:13 +08001714 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001715 mnt_drop_write_file(filp);
1716 return ret;
1717}
1718
Chao Yud49f3e82015-01-23 20:36:04 +08001719static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1720{
1721 struct inode *inode = file_inode(filp);
1722
1723 return put_user(inode->i_generation, (int __user *)arg);
1724}
1725
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001726static int f2fs_ioc_start_atomic_write(struct file *filp)
1727{
1728 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001729 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001730
1731 if (!inode_owner_or_capable(inode))
1732 return -EACCES;
1733
Jaegeuk Kime8118982017-03-17 10:04:15 +08001734 if (!S_ISREG(inode->i_mode))
1735 return -EINVAL;
1736
Chao Yu7fb17fe2016-05-09 19:56:32 +08001737 ret = mnt_want_write_file(filp);
1738 if (ret)
1739 return ret;
1740
Chao Yu0fac5582016-05-09 19:56:33 +08001741 inode_lock(inode);
1742
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001743 if (f2fs_is_atomic_file(inode)) {
1744 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1745 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001746 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001747 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001748
Chao Yuf4c9c742015-07-17 18:06:35 +08001749 ret = f2fs_convert_inline_inode(inode);
1750 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001751 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001752
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001753 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1754
Jaegeuk Kim31867b22018-12-28 11:00:38 -08001755 /*
1756 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
1757 * f2fs_is_atomic_file.
1758 */
1759 if (get_dirty_pages(inode))
1760 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001761 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001762 inode->i_ino, get_dirty_pages(inode));
1763 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001764 if (ret) {
1765 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001766 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001767 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08001768
Yunlei He054afda2018-04-18 11:06:39 +08001769 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001770 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001771 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001772
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001773 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001774 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001775 stat_inc_atomic_write(inode);
1776 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001777out:
Chao Yu0fac5582016-05-09 19:56:33 +08001778 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001779 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001780 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001781}
1782
1783static int f2fs_ioc_commit_atomic_write(struct file *filp)
1784{
1785 struct inode *inode = file_inode(filp);
1786 int ret;
1787
1788 if (!inode_owner_or_capable(inode))
1789 return -EACCES;
1790
1791 ret = mnt_want_write_file(filp);
1792 if (ret)
1793 return ret;
1794
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001795 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08001796
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001797 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08001798
Chao Yub169c3c2018-04-18 17:45:02 +08001799 if (f2fs_is_volatile_file(inode)) {
1800 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001801 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08001802 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001803
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001804 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08001805 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001806 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001807 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001808
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001809 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001810 if (!ret) {
1811 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001812 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +08001813 stat_dec_atomic_write(inode);
1814 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001815 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001816 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001817 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001818err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08001819 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1820 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1821 ret = -EINVAL;
1822 }
Chao Yu0fac5582016-05-09 19:56:33 +08001823 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001824 mnt_drop_write_file(filp);
1825 return ret;
1826}
1827
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001828static int f2fs_ioc_start_volatile_write(struct file *filp)
1829{
1830 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001831 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001832
1833 if (!inode_owner_or_capable(inode))
1834 return -EACCES;
1835
Chao Yu8ff09712017-03-17 15:43:57 +08001836 if (!S_ISREG(inode->i_mode))
1837 return -EINVAL;
1838
Chao Yu7fb17fe2016-05-09 19:56:32 +08001839 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001840 if (ret)
1841 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001842
Chao Yu0fac5582016-05-09 19:56:33 +08001843 inode_lock(inode);
1844
Chao Yu7fb17fe2016-05-09 19:56:32 +08001845 if (f2fs_is_volatile_file(inode))
1846 goto out;
1847
1848 ret = f2fs_convert_inline_inode(inode);
1849 if (ret)
1850 goto out;
1851
Chao Yu648d50b2017-03-22 17:23:45 +08001852 stat_inc_volatile_write(inode);
1853 stat_update_max_volatile_write(inode);
1854
Jaegeuk Kim91942322016-05-20 10:13:22 -07001855 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001856 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001857out:
Chao Yu0fac5582016-05-09 19:56:33 +08001858 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001859 mnt_drop_write_file(filp);
1860 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001861}
1862
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001863static int f2fs_ioc_release_volatile_write(struct file *filp)
1864{
1865 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001866 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001867
1868 if (!inode_owner_or_capable(inode))
1869 return -EACCES;
1870
Chao Yu7fb17fe2016-05-09 19:56:32 +08001871 ret = mnt_want_write_file(filp);
1872 if (ret)
1873 return ret;
1874
Chao Yu0fac5582016-05-09 19:56:33 +08001875 inode_lock(inode);
1876
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001877 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001878 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001879
Chao Yu7fb17fe2016-05-09 19:56:32 +08001880 if (!f2fs_is_first_block_written(inode)) {
1881 ret = truncate_partial_data_page(inode, 0, true);
1882 goto out;
1883 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001884
Chao Yu7fb17fe2016-05-09 19:56:32 +08001885 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1886out:
Chao Yu0fac5582016-05-09 19:56:33 +08001887 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001888 mnt_drop_write_file(filp);
1889 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001890}
1891
1892static int f2fs_ioc_abort_volatile_write(struct file *filp)
1893{
1894 struct inode *inode = file_inode(filp);
1895 int ret;
1896
1897 if (!inode_owner_or_capable(inode))
1898 return -EACCES;
1899
1900 ret = mnt_want_write_file(filp);
1901 if (ret)
1902 return ret;
1903
Chao Yu0fac5582016-05-09 19:56:33 +08001904 inode_lock(inode);
1905
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001906 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001907 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001908 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001909 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001910 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001911 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001912 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001913
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001914 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1915
Chao Yu0fac5582016-05-09 19:56:33 +08001916 inode_unlock(inode);
1917
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001918 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001919 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001920 return ret;
1921}
1922
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001923static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1924{
1925 struct inode *inode = file_inode(filp);
1926 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1927 struct super_block *sb = sbi->sb;
1928 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03001929 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001930
1931 if (!capable(CAP_SYS_ADMIN))
1932 return -EPERM;
1933
1934 if (get_user(in, (__u32 __user *)arg))
1935 return -EFAULT;
1936
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301937 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1938 ret = mnt_want_write_file(filp);
1939 if (ret)
1940 return ret;
1941 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001942
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001943 switch (in) {
1944 case F2FS_GOING_DOWN_FULLSYNC:
1945 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08001946 if (IS_ERR(sb)) {
1947 ret = PTR_ERR(sb);
1948 goto out;
1949 }
1950 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001951 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001952 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001953 thaw_bdev(sb->s_bdev, sb);
1954 }
1955 break;
1956 case F2FS_GOING_DOWN_METASYNC:
1957 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08001958 ret = f2fs_sync_fs(sb, 1);
1959 if (ret)
1960 goto out;
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;
1964 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001965 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001966 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001967 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001968 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08001969 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001970 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001971 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001972 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08001973 case F2FS_GOING_DOWN_NEED_FSCK:
1974 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08001975 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
1976 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08001977 /* do checkpoint only */
1978 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08001979 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001980 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001981 ret = -EINVAL;
1982 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001983 }
Chao Yu7950e9a2018-01-18 17:23:29 +08001984
Chao Yu4d57b862018-05-30 00:20:41 +08001985 f2fs_stop_gc_thread(sbi);
1986 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001987
Chao Yu4d57b862018-05-30 00:20:41 +08001988 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001989 clear_opt(sbi, DISCARD);
1990
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001991 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001992out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301993 if (in != F2FS_GOING_DOWN_FULLSYNC)
1994 mnt_drop_write_file(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001995 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001996}
1997
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001998static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1999{
2000 struct inode *inode = file_inode(filp);
2001 struct super_block *sb = inode->i_sb;
2002 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2003 struct fstrim_range range;
2004 int ret;
2005
2006 if (!capable(CAP_SYS_ADMIN))
2007 return -EPERM;
2008
Chao Yu7d20c8a2018-09-04 03:52:17 +08002009 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002010 return -EOPNOTSUPP;
2011
2012 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2013 sizeof(range)))
2014 return -EFAULT;
2015
Chao Yu7fb17fe2016-05-09 19:56:32 +08002016 ret = mnt_want_write_file(filp);
2017 if (ret)
2018 return ret;
2019
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002020 range.minlen = max((unsigned int)range.minlen,
2021 q->limits.discard_granularity);
2022 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002023 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002024 if (ret < 0)
2025 return ret;
2026
2027 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2028 sizeof(range)))
2029 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002030 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002031 return 0;
2032}
2033
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002034static bool uuid_is_nonzero(__u8 u[16])
2035{
2036 int i;
2037
2038 for (i = 0; i < 16; i++)
2039 if (u[i])
2040 return true;
2041 return false;
2042}
2043
2044static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2045{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002046 struct inode *inode = file_inode(filp);
2047
Chao Yu7beb01f2018-10-24 18:34:26 +08002048 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002049 return -EOPNOTSUPP;
2050
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002051 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002052
Eric Biggersdb717d82016-11-26 19:07:49 -05002053 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002054}
2055
2056static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2057{
Chao Yu7beb01f2018-10-24 18:34:26 +08002058 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002059 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002060 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002061}
2062
2063static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2064{
2065 struct inode *inode = file_inode(filp);
2066 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2067 int err;
2068
Chao Yu7beb01f2018-10-24 18:34:26 +08002069 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002070 return -EOPNOTSUPP;
2071
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002072 err = mnt_want_write_file(filp);
2073 if (err)
2074 return err;
2075
Chao Yu846ae672018-02-26 22:04:13 +08002076 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002077
2078 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2079 goto got_it;
2080
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002081 /* update superblock with uuid */
2082 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2083
Chao Yuc5bda1c2015-06-08 13:28:03 +08002084 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002085 if (err) {
2086 /* undo new data */
2087 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002088 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002089 }
2090got_it:
2091 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2092 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002093 err = -EFAULT;
2094out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002095 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002096 mnt_drop_write_file(filp);
2097 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002098}
2099
Chao Yuc1c1b582015-07-10 18:08:10 +08002100static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2101{
2102 struct inode *inode = file_inode(filp);
2103 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002104 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002105 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002106
2107 if (!capable(CAP_SYS_ADMIN))
2108 return -EPERM;
2109
Chao Yud530d4d2015-10-05 22:22:44 +08002110 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002111 return -EFAULT;
2112
Chao Yud530d4d2015-10-05 22:22:44 +08002113 if (f2fs_readonly(sbi->sb))
2114 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002115
Chao Yu7fb17fe2016-05-09 19:56:32 +08002116 ret = mnt_want_write_file(filp);
2117 if (ret)
2118 return ret;
2119
Chao Yud530d4d2015-10-05 22:22:44 +08002120 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002121 if (!mutex_trylock(&sbi->gc_mutex)) {
2122 ret = -EBUSY;
2123 goto out;
2124 }
Chao Yud530d4d2015-10-05 22:22:44 +08002125 } else {
2126 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002127 }
2128
Jaegeuk Kime066b832017-04-13 15:17:00 -07002129 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002130out:
2131 mnt_drop_write_file(filp);
2132 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002133}
2134
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002135static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2136{
2137 struct inode *inode = file_inode(filp);
2138 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2139 struct f2fs_gc_range range;
2140 u64 end;
2141 int ret;
2142
2143 if (!capable(CAP_SYS_ADMIN))
2144 return -EPERM;
2145
2146 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2147 sizeof(range)))
2148 return -EFAULT;
2149
2150 if (f2fs_readonly(sbi->sb))
2151 return -EROFS;
2152
Yunlei Heb82f6e32018-04-24 11:40:30 +08002153 end = range.start + range.len;
2154 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
2155 return -EINVAL;
2156 }
2157
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002158 ret = mnt_want_write_file(filp);
2159 if (ret)
2160 return ret;
2161
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002162do_more:
2163 if (!range.sync) {
2164 if (!mutex_trylock(&sbi->gc_mutex)) {
2165 ret = -EBUSY;
2166 goto out;
2167 }
2168 } else {
2169 mutex_lock(&sbi->gc_mutex);
2170 }
2171
2172 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Yunlong Song67b0e422018-10-30 20:37:55 +08002173 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002174 if (range.start <= end)
2175 goto do_more;
2176out:
2177 mnt_drop_write_file(filp);
2178 return ret;
2179}
2180
Chao Yu059c0642018-07-17 20:41:49 +08002181static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002182{
2183 struct inode *inode = file_inode(filp);
2184 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002185 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002186
2187 if (!capable(CAP_SYS_ADMIN))
2188 return -EPERM;
2189
2190 if (f2fs_readonly(sbi->sb))
2191 return -EROFS;
2192
Daniel Rosenberg43549942018-08-20 19:21:43 -07002193 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2194 f2fs_msg(sbi->sb, KERN_INFO,
2195 "Skipping Checkpoint. Checkpoints currently disabled.");
2196 return -EINVAL;
2197 }
2198
Chao Yu7fb17fe2016-05-09 19:56:32 +08002199 ret = mnt_want_write_file(filp);
2200 if (ret)
2201 return ret;
2202
2203 ret = f2fs_sync_fs(sbi->sb, 1);
2204
2205 mnt_drop_write_file(filp);
2206 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002207}
2208
Chao Yud323d002015-10-27 09:53:45 +08002209static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2210 struct file *filp,
2211 struct f2fs_defragment *range)
2212{
2213 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002214 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002215 .m_seg_type = NO_CHECK_TYPE ,
2216 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002217 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002218 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002219 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002220 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002221 block_t blk_end = 0;
2222 bool fragmented = false;
2223 int err;
2224
2225 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002226 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002227 return -EINVAL;
2228
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002229 pg_start = range->start >> PAGE_SHIFT;
2230 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002231
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002232 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002233
Al Viro59551022016-01-22 15:40:57 -05002234 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002235
2236 /* writeback all dirty pages in the range */
2237 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002238 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002239 if (err)
2240 goto out;
2241
2242 /*
2243 * lookup mapping info in extent cache, skip defragmenting if physical
2244 * block addresses are continuous.
2245 */
2246 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2247 if (ei.fofs + ei.len >= pg_end)
2248 goto out;
2249 }
2250
2251 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002252 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002253
2254 /*
2255 * lookup mapping info in dnode page cache, skip defragmenting if all
2256 * physical block addresses are continuous even if there are hole(s)
2257 * in logical blocks.
2258 */
2259 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002260 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002261 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002262 if (err)
2263 goto out;
2264
2265 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002266 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002267 continue;
2268 }
2269
Chao Yu25a912e2018-01-10 18:18:51 +08002270 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002271 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002272
2273 /* record total count of block that we're going to move */
2274 total += map.m_len;
2275
Chao Yud323d002015-10-27 09:53:45 +08002276 blk_end = map.m_pblk + map.m_len;
2277
2278 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002279 }
2280
2281 if (!fragmented)
2282 goto out;
2283
Chao Yu25a912e2018-01-10 18:18:51 +08002284 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002285
2286 /*
2287 * make sure there are enough free section for LFS allocation, this can
2288 * avoid defragment running in SSR mode when free section are allocated
2289 * intensively
2290 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002291 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002292 err = -EAGAIN;
2293 goto out;
2294 }
2295
Chao Yu25a912e2018-01-10 18:18:51 +08002296 map.m_lblk = pg_start;
2297 map.m_len = pg_end - pg_start;
2298 total = 0;
2299
Chao Yud323d002015-10-27 09:53:45 +08002300 while (map.m_lblk < pg_end) {
2301 pgoff_t idx;
2302 int cnt = 0;
2303
2304do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002305 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002306 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002307 if (err)
2308 goto clear_out;
2309
2310 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002311 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002312 continue;
2313 }
2314
Jaegeuk Kim91942322016-05-20 10:13:22 -07002315 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002316
2317 idx = map.m_lblk;
2318 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2319 struct page *page;
2320
Chao Yu4d57b862018-05-30 00:20:41 +08002321 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002322 if (IS_ERR(page)) {
2323 err = PTR_ERR(page);
2324 goto clear_out;
2325 }
2326
2327 set_page_dirty(page);
2328 f2fs_put_page(page, 1);
2329
2330 idx++;
2331 cnt++;
2332 total++;
2333 }
2334
2335 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002336
2337 if (idx < pg_end && cnt < blk_per_seg)
2338 goto do_map;
2339
Jaegeuk Kim91942322016-05-20 10:13:22 -07002340 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002341
2342 err = filemap_fdatawrite(inode->i_mapping);
2343 if (err)
2344 goto out;
2345 }
2346clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002347 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002348out:
Al Viro59551022016-01-22 15:40:57 -05002349 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002350 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002351 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002352 return err;
2353}
2354
2355static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2356{
2357 struct inode *inode = file_inode(filp);
2358 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2359 struct f2fs_defragment range;
2360 int err;
2361
2362 if (!capable(CAP_SYS_ADMIN))
2363 return -EPERM;
2364
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002365 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002366 return -EINVAL;
2367
Kinglong Meed7563862017-03-10 17:55:07 +08002368 if (f2fs_readonly(sbi->sb))
2369 return -EROFS;
2370
2371 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2372 sizeof(range)))
2373 return -EFAULT;
2374
2375 /* verify alignment of offset & size */
2376 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2377 return -EINVAL;
2378
2379 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2380 sbi->max_file_blocks))
2381 return -EINVAL;
2382
Chao Yud323d002015-10-27 09:53:45 +08002383 err = mnt_want_write_file(filp);
2384 if (err)
2385 return err;
2386
Chao Yud323d002015-10-27 09:53:45 +08002387 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002388 mnt_drop_write_file(filp);
2389
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002390 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002391 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002392 return err;
Chao Yud323d002015-10-27 09:53:45 +08002393
2394 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2395 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002396 return -EFAULT;
2397
2398 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002399}
2400
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002401static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2402 struct file *file_out, loff_t pos_out, size_t len)
2403{
2404 struct inode *src = file_inode(file_in);
2405 struct inode *dst = file_inode(file_out);
2406 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2407 size_t olen = len, dst_max_i_size = 0;
2408 size_t dst_osize;
2409 int ret;
2410
2411 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2412 src->i_sb != dst->i_sb)
2413 return -EXDEV;
2414
2415 if (unlikely(f2fs_readonly(src->i_sb)))
2416 return -EROFS;
2417
Chao Yufe8494b2016-08-04 20:13:02 +08002418 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2419 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002420
2421 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2422 return -EOPNOTSUPP;
2423
Fan Lid95fd912016-09-13 11:35:42 +08002424 if (src == dst) {
2425 if (pos_in == pos_out)
2426 return 0;
2427 if (pos_out > pos_in && pos_out < pos_in + len)
2428 return -EINVAL;
2429 }
2430
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002431 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002432 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002433 ret = -EBUSY;
2434 if (!inode_trylock(dst))
2435 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002436 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002437
2438 ret = -EINVAL;
2439 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2440 goto out_unlock;
2441 if (len == 0)
2442 olen = len = src->i_size - pos_in;
2443 if (pos_in + len == src->i_size)
2444 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2445 if (len == 0) {
2446 ret = 0;
2447 goto out_unlock;
2448 }
2449
2450 dst_osize = dst->i_size;
2451 if (pos_out + olen > dst->i_size)
2452 dst_max_i_size = pos_out + olen;
2453
2454 /* verify the end result is block aligned */
2455 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2456 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2457 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2458 goto out_unlock;
2459
2460 ret = f2fs_convert_inline_inode(src);
2461 if (ret)
2462 goto out_unlock;
2463
2464 ret = f2fs_convert_inline_inode(dst);
2465 if (ret)
2466 goto out_unlock;
2467
2468 /* write out all dirty pages from offset */
2469 ret = filemap_write_and_wait_range(src->i_mapping,
2470 pos_in, pos_in + len);
2471 if (ret)
2472 goto out_unlock;
2473
2474 ret = filemap_write_and_wait_range(dst->i_mapping,
2475 pos_out, pos_out + len);
2476 if (ret)
2477 goto out_unlock;
2478
2479 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002480
2481 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2482 if (src != dst) {
2483 ret = -EBUSY;
2484 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2485 goto out_src;
2486 }
2487
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002488 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002489 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2490 pos_out >> F2FS_BLKSIZE_BITS,
2491 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002492
2493 if (!ret) {
2494 if (dst_max_i_size)
2495 f2fs_i_size_write(dst, dst_max_i_size);
2496 else if (dst_osize != dst->i_size)
2497 f2fs_i_size_write(dst, dst_osize);
2498 }
2499 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002500
2501 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002502 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002503out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002504 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002505out_unlock:
2506 if (src != dst)
2507 inode_unlock(dst);
2508out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002509 inode_unlock(src);
2510 return ret;
2511}
2512
2513static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2514{
2515 struct f2fs_move_range range;
2516 struct fd dst;
2517 int err;
2518
2519 if (!(filp->f_mode & FMODE_READ) ||
2520 !(filp->f_mode & FMODE_WRITE))
2521 return -EBADF;
2522
2523 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2524 sizeof(range)))
2525 return -EFAULT;
2526
2527 dst = fdget(range.dst_fd);
2528 if (!dst.file)
2529 return -EBADF;
2530
2531 if (!(dst.file->f_mode & FMODE_WRITE)) {
2532 err = -EBADF;
2533 goto err_out;
2534 }
2535
2536 err = mnt_want_write_file(filp);
2537 if (err)
2538 goto err_out;
2539
2540 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2541 range.pos_out, range.len);
2542
2543 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002544 if (err)
2545 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002546
2547 if (copy_to_user((struct f2fs_move_range __user *)arg,
2548 &range, sizeof(range)))
2549 err = -EFAULT;
2550err_out:
2551 fdput(dst);
2552 return err;
2553}
2554
Jaegeuk Kime066b832017-04-13 15:17:00 -07002555static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2556{
2557 struct inode *inode = file_inode(filp);
2558 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2559 struct sit_info *sm = SIT_I(sbi);
2560 unsigned int start_segno = 0, end_segno = 0;
2561 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2562 struct f2fs_flush_device range;
2563 int ret;
2564
2565 if (!capable(CAP_SYS_ADMIN))
2566 return -EPERM;
2567
2568 if (f2fs_readonly(sbi->sb))
2569 return -EROFS;
2570
Daniel Rosenberg43549942018-08-20 19:21:43 -07002571 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2572 return -EINVAL;
2573
Jaegeuk Kime066b832017-04-13 15:17:00 -07002574 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2575 sizeof(range)))
2576 return -EFAULT;
2577
2578 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002579 __is_large_section(sbi)) {
Jaegeuk Kime066b832017-04-13 15:17:00 -07002580 f2fs_msg(sbi->sb, KERN_WARNING,
2581 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2582 range.dev_num, sbi->s_ndevs,
2583 sbi->segs_per_sec);
2584 return -EINVAL;
2585 }
2586
2587 ret = mnt_want_write_file(filp);
2588 if (ret)
2589 return ret;
2590
2591 if (range.dev_num != 0)
2592 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2593 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2594
2595 start_segno = sm->last_victim[FLUSH_DEVICE];
2596 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2597 start_segno = dev_start_segno;
2598 end_segno = min(start_segno + range.segments, dev_end_segno);
2599
2600 while (start_segno < end_segno) {
2601 if (!mutex_trylock(&sbi->gc_mutex)) {
2602 ret = -EBUSY;
2603 goto out;
2604 }
2605 sm->last_victim[GC_CB] = end_segno + 1;
2606 sm->last_victim[GC_GREEDY] = end_segno + 1;
2607 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2608 ret = f2fs_gc(sbi, true, true, start_segno);
2609 if (ret == -EAGAIN)
2610 ret = 0;
2611 else if (ret < 0)
2612 break;
2613 start_segno++;
2614 }
2615out:
2616 mnt_drop_write_file(filp);
2617 return ret;
2618}
2619
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002620static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2621{
2622 struct inode *inode = file_inode(filp);
2623 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2624
2625 /* Must validate to set it with SQLite behavior in Android. */
2626 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2627
2628 return put_user(sb_feature, (u32 __user *)arg);
2629}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002630
Chao Yu2c1d0302017-07-29 00:32:52 +08002631#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002632int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2633{
2634 struct dquot *transfer_to[MAXQUOTAS] = {};
2635 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2636 struct super_block *sb = sbi->sb;
2637 int err = 0;
2638
2639 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2640 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2641 err = __dquot_transfer(inode, transfer_to);
2642 if (err)
2643 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2644 dqput(transfer_to[PRJQUOTA]);
2645 }
2646 return err;
2647}
2648
Chao Yu2c1d0302017-07-29 00:32:52 +08002649static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2650{
2651 struct inode *inode = file_inode(filp);
2652 struct f2fs_inode_info *fi = F2FS_I(inode);
2653 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002654 struct page *ipage;
2655 kprojid_t kprojid;
2656 int err;
2657
Chao Yu7beb01f2018-10-24 18:34:26 +08002658 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08002659 if (projid != F2FS_DEF_PROJID)
2660 return -EOPNOTSUPP;
2661 else
2662 return 0;
2663 }
2664
2665 if (!f2fs_has_extra_attr(inode))
2666 return -EOPNOTSUPP;
2667
2668 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2669
2670 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2671 return 0;
2672
Chao Yu2c1d0302017-07-29 00:32:52 +08002673 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08002674 /* Is it quota file? Do not allow user to mess with it */
2675 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09002676 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002677
Chao Yu4d57b862018-05-30 00:20:41 +08002678 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09002679 if (IS_ERR(ipage))
2680 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08002681
2682 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2683 i_projid)) {
2684 err = -EOVERFLOW;
2685 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09002686 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002687 }
2688 f2fs_put_page(ipage, 1);
2689
Chao Yuc22aecd2018-04-21 17:53:52 +08002690 err = dquot_initialize(inode);
2691 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002692 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002693
Chao Yu78130812018-09-25 15:36:02 +08002694 f2fs_lock_op(sbi);
2695 err = f2fs_transfer_project_quota(inode, kprojid);
2696 if (err)
2697 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08002698
2699 F2FS_I(inode)->i_projid = kprojid;
2700 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002701 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08002702out_unlock:
2703 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08002704 return err;
2705}
2706#else
Chao Yu78130812018-09-25 15:36:02 +08002707int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2708{
2709 return 0;
2710}
2711
Chao Yu2c1d0302017-07-29 00:32:52 +08002712static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2713{
2714 if (projid != F2FS_DEF_PROJID)
2715 return -EOPNOTSUPP;
2716 return 0;
2717}
2718#endif
2719
2720/* Transfer internal flags to xflags */
2721static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2722{
2723 __u32 xflags = 0;
2724
Chao Yu59c84402018-04-03 15:08:17 +08002725 if (iflags & F2FS_SYNC_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002726 xflags |= FS_XFLAG_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +08002727 if (iflags & F2FS_IMMUTABLE_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002728 xflags |= FS_XFLAG_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +08002729 if (iflags & F2FS_APPEND_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002730 xflags |= FS_XFLAG_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +08002731 if (iflags & F2FS_NODUMP_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002732 xflags |= FS_XFLAG_NODUMP;
Chao Yu59c84402018-04-03 15:08:17 +08002733 if (iflags & F2FS_NOATIME_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002734 xflags |= FS_XFLAG_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +08002735 if (iflags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002736 xflags |= FS_XFLAG_PROJINHERIT;
2737 return xflags;
2738}
2739
2740#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2741 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2742 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2743
Chao Yu2c1d0302017-07-29 00:32:52 +08002744/* Transfer xflags flags to internal */
2745static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2746{
2747 unsigned long iflags = 0;
2748
2749 if (xflags & FS_XFLAG_SYNC)
Chao Yu59c84402018-04-03 15:08:17 +08002750 iflags |= F2FS_SYNC_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002751 if (xflags & FS_XFLAG_IMMUTABLE)
Chao Yu59c84402018-04-03 15:08:17 +08002752 iflags |= F2FS_IMMUTABLE_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002753 if (xflags & FS_XFLAG_APPEND)
Chao Yu59c84402018-04-03 15:08:17 +08002754 iflags |= F2FS_APPEND_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002755 if (xflags & FS_XFLAG_NODUMP)
Chao Yu59c84402018-04-03 15:08:17 +08002756 iflags |= F2FS_NODUMP_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002757 if (xflags & FS_XFLAG_NOATIME)
Chao Yu59c84402018-04-03 15:08:17 +08002758 iflags |= F2FS_NOATIME_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002759 if (xflags & FS_XFLAG_PROJINHERIT)
Chao Yu59c84402018-04-03 15:08:17 +08002760 iflags |= F2FS_PROJINHERIT_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002761
2762 return iflags;
2763}
2764
2765static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2766{
2767 struct inode *inode = file_inode(filp);
2768 struct f2fs_inode_info *fi = F2FS_I(inode);
2769 struct fsxattr fa;
2770
2771 memset(&fa, 0, sizeof(struct fsxattr));
2772 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
Chao Yuc807a7c2018-04-08 11:25:53 +08002773 F2FS_FL_USER_VISIBLE);
Chao Yu2c1d0302017-07-29 00:32:52 +08002774
Chao Yu7beb01f2018-10-24 18:34:26 +08002775 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
Chao Yu2c1d0302017-07-29 00:32:52 +08002776 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2777 fi->i_projid);
2778
2779 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2780 return -EFAULT;
2781 return 0;
2782}
2783
Wang Shilongc8e92752018-09-11 08:54:21 +09002784static int f2fs_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
2785{
2786 /*
2787 * Project Quota ID state is only allowed to change from within the init
2788 * namespace. Enforce that restriction only if we are trying to change
2789 * the quota ID state. Everything else is allowed in user namespaces.
2790 */
2791 if (current_user_ns() == &init_user_ns)
2792 return 0;
2793
2794 if (__kprojid_val(F2FS_I(inode)->i_projid) != fa->fsx_projid)
2795 return -EINVAL;
2796
2797 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) {
2798 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
2799 return -EINVAL;
2800 } else {
2801 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
2802 return -EINVAL;
2803 }
2804
2805 return 0;
2806}
2807
Chao Yu2c1d0302017-07-29 00:32:52 +08002808static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2809{
2810 struct inode *inode = file_inode(filp);
2811 struct f2fs_inode_info *fi = F2FS_I(inode);
2812 struct fsxattr fa;
2813 unsigned int flags;
2814 int err;
2815
2816 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2817 return -EFAULT;
2818
2819 /* Make sure caller has proper permission */
2820 if (!inode_owner_or_capable(inode))
2821 return -EACCES;
2822
2823 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2824 return -EOPNOTSUPP;
2825
2826 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2827 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2828 return -EOPNOTSUPP;
2829
2830 err = mnt_want_write_file(filp);
2831 if (err)
2832 return err;
2833
2834 inode_lock(inode);
Wang Shilongc8e92752018-09-11 08:54:21 +09002835 err = f2fs_ioctl_check_project(inode, &fa);
2836 if (err)
2837 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08002838 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2839 (flags & F2FS_FL_XFLAG_VISIBLE);
2840 err = __f2fs_ioc_setflags(inode, flags);
Chao Yu2c1d0302017-07-29 00:32:52 +08002841 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002842 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08002843
2844 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09002845out:
2846 inode_unlock(inode);
2847 mnt_drop_write_file(filp);
2848 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002849}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002850
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002851int f2fs_pin_file_control(struct inode *inode, bool inc)
2852{
2853 struct f2fs_inode_info *fi = F2FS_I(inode);
2854 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2855
2856 /* Use i_gc_failures for normal file as a risk signal. */
2857 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08002858 f2fs_i_gc_failures_write(inode,
2859 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002860
Chao Yu2ef79ec2018-05-07 20:28:54 +08002861 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002862 f2fs_msg(sbi->sb, KERN_WARNING,
2863 "%s: Enable GC = ino %lx after %x GC trials\n",
Chao Yu2ef79ec2018-05-07 20:28:54 +08002864 __func__, inode->i_ino,
2865 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002866 clear_inode_flag(inode, FI_PIN_FILE);
2867 return -EAGAIN;
2868 }
2869 return 0;
2870}
2871
2872static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2873{
2874 struct inode *inode = file_inode(filp);
2875 __u32 pin;
2876 int ret = 0;
2877
2878 if (!inode_owner_or_capable(inode))
2879 return -EACCES;
2880
2881 if (get_user(pin, (__u32 __user *)arg))
2882 return -EFAULT;
2883
2884 if (!S_ISREG(inode->i_mode))
2885 return -EINVAL;
2886
2887 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2888 return -EROFS;
2889
2890 ret = mnt_want_write_file(filp);
2891 if (ret)
2892 return ret;
2893
2894 inode_lock(inode);
2895
Chao Yu4d57b862018-05-30 00:20:41 +08002896 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb2018-01-17 16:31:38 +08002897 ret = -EINVAL;
2898 goto out;
2899 }
2900
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002901 if (!pin) {
2902 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08002903 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002904 goto done;
2905 }
2906
2907 if (f2fs_pin_file_control(inode, false)) {
2908 ret = -EAGAIN;
2909 goto out;
2910 }
2911 ret = f2fs_convert_inline_inode(inode);
2912 if (ret)
2913 goto out;
2914
2915 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002916 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002917done:
2918 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2919out:
2920 inode_unlock(inode);
2921 mnt_drop_write_file(filp);
2922 return ret;
2923}
2924
2925static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2926{
2927 struct inode *inode = file_inode(filp);
2928 __u32 pin = 0;
2929
2930 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08002931 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002932 return put_user(pin, (u32 __user *)arg);
2933}
2934
Chao Yuc4020b22018-01-11 14:42:30 +08002935int f2fs_precache_extents(struct inode *inode)
2936{
2937 struct f2fs_inode_info *fi = F2FS_I(inode);
2938 struct f2fs_map_blocks map;
2939 pgoff_t m_next_extent;
2940 loff_t end;
2941 int err;
2942
2943 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2944 return -EOPNOTSUPP;
2945
2946 map.m_lblk = 0;
2947 map.m_next_pgofs = NULL;
2948 map.m_next_extent = &m_next_extent;
2949 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08002950 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08002951 end = F2FS_I_SB(inode)->max_file_blocks;
2952
2953 while (map.m_lblk < end) {
2954 map.m_len = end - map.m_lblk;
2955
Chao Yub2532c62018-04-24 10:55:28 +08002956 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002957 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08002958 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002959 if (err)
2960 return err;
2961
2962 map.m_lblk = m_next_extent;
2963 }
2964
2965 return err;
2966}
2967
2968static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2969{
2970 return f2fs_precache_extents(file_inode(filp));
2971}
2972
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002973long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2974{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002975 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2976 return -EIO;
2977
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002978 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002979 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002980 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002981 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002982 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002983 case F2FS_IOC_GETVERSION:
2984 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002985 case F2FS_IOC_START_ATOMIC_WRITE:
2986 return f2fs_ioc_start_atomic_write(filp);
2987 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2988 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002989 case F2FS_IOC_START_VOLATILE_WRITE:
2990 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002991 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2992 return f2fs_ioc_release_volatile_write(filp);
2993 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2994 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002995 case F2FS_IOC_SHUTDOWN:
2996 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002997 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002998 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002999 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3000 return f2fs_ioc_set_encryption_policy(filp, arg);
3001 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3002 return f2fs_ioc_get_encryption_policy(filp, arg);
3003 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3004 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003005 case F2FS_IOC_GARBAGE_COLLECT:
3006 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003007 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3008 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08003009 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08003010 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08003011 case F2FS_IOC_DEFRAGMENT:
3012 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003013 case F2FS_IOC_MOVE_RANGE:
3014 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07003015 case F2FS_IOC_FLUSH_DEVICE:
3016 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003017 case F2FS_IOC_GET_FEATURES:
3018 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08003019 case F2FS_IOC_FSGETXATTR:
3020 return f2fs_ioc_fsgetxattr(filp, arg);
3021 case F2FS_IOC_FSSETXATTR:
3022 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003023 case F2FS_IOC_GET_PIN_FILE:
3024 return f2fs_ioc_get_pin_file(filp, arg);
3025 case F2FS_IOC_SET_PIN_FILE:
3026 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08003027 case F2FS_IOC_PRECACHE_EXTENTS:
3028 return f2fs_ioc_precache_extents(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003029 default:
3030 return -ENOTTY;
3031 }
3032}
3033
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003034static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3035{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003036 struct file *file = iocb->ki_filp;
3037 struct inode *inode = file_inode(file);
3038 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003039
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003040 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3041 return -EIO;
3042
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003043 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
3044 return -EINVAL;
3045
3046 if (!inode_trylock(inode)) {
3047 if (iocb->ki_flags & IOCB_NOWAIT)
3048 return -EAGAIN;
3049 inode_lock(inode);
3050 }
3051
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003052 ret = generic_write_checks(iocb, from);
3053 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003054 bool preallocated = false;
3055 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003056 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003057
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003058 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3059 set_inode_flag(inode, FI_NO_PREALLOC);
3060
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003061 if ((iocb->ki_flags & IOCB_NOWAIT) &&
3062 (iocb->ki_flags & IOCB_DIRECT)) {
3063 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
3064 iov_iter_count(from)) ||
3065 f2fs_has_inline_data(inode) ||
Chao Yuf847c692018-09-27 18:34:52 +08003066 f2fs_force_buffered_io(inode,
3067 iocb, from)) {
Yunlei Hecba41be2018-04-24 11:40:19 +08003068 clear_inode_flag(inode,
3069 FI_NO_PREALLOC);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003070 inode_unlock(inode);
3071 return -EAGAIN;
3072 }
3073
3074 } else {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003075 preallocated = true;
3076 target_size = iocb->ki_pos + iov_iter_count(from);
3077
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003078 err = f2fs_preallocate_blocks(iocb, from);
3079 if (err) {
3080 clear_inode_flag(inode, FI_NO_PREALLOC);
3081 inode_unlock(inode);
3082 return err;
3083 }
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003084 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003085 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003086 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003087
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003088 /* if we couldn't write data, we should deallocate blocks. */
3089 if (preallocated && i_size_read(inode) < target_size)
3090 f2fs_truncate(inode);
3091
Chao Yub0af6d42017-08-02 23:21:48 +08003092 if (ret > 0)
3093 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003094 }
3095 inode_unlock(inode);
3096
Christoph Hellwige2592212016-04-07 08:52:01 -07003097 if (ret > 0)
3098 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003099 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003100}
3101
Namjae Jeone9750822013-02-04 23:41:41 +09003102#ifdef CONFIG_COMPAT
3103long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3104{
3105 switch (cmd) {
3106 case F2FS_IOC32_GETFLAGS:
3107 cmd = F2FS_IOC_GETFLAGS;
3108 break;
3109 case F2FS_IOC32_SETFLAGS:
3110 cmd = F2FS_IOC_SETFLAGS;
3111 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003112 case F2FS_IOC32_GETVERSION:
3113 cmd = F2FS_IOC_GETVERSION;
3114 break;
3115 case F2FS_IOC_START_ATOMIC_WRITE:
3116 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3117 case F2FS_IOC_START_VOLATILE_WRITE:
3118 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3119 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3120 case F2FS_IOC_SHUTDOWN:
3121 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3122 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3123 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3124 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003125 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003126 case F2FS_IOC_WRITE_CHECKPOINT:
3127 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003128 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003129 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003130 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003131 case F2FS_IOC_FSGETXATTR:
3132 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003133 case F2FS_IOC_GET_PIN_FILE:
3134 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003135 case F2FS_IOC_PRECACHE_EXTENTS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003136 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003137 default:
3138 return -ENOIOCTLCMD;
3139 }
3140 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3141}
3142#endif
3143
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003144const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003145 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04003146 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003147 .write_iter = f2fs_file_write_iter,
3148 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003149 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003150 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003151 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003152 .fsync = f2fs_sync_file,
3153 .fallocate = f2fs_fallocate,
3154 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003155#ifdef CONFIG_COMPAT
3156 .compat_ioctl = f2fs_compat_ioctl,
3157#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003158 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003159 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003160};