blob: ff2cb8fb6934a45fe63fed73c9106954715b819b [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002 * fs/f2fs/file.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/stat.h>
14#include <linux/buffer_head.h>
15#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090016#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/falloc.h>
18#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090019#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090020#include <linux/uaccess.h>
21#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090022#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080023#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070024#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070025#include <linux/file.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
30#include "xattr.h"
31#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080032#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080033#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090034#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035
Souptick Joarderea4d4792018-04-15 01:40:02 +053036static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080037{
38 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053039 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080040
41 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053042 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080043 up_read(&F2FS_I(inode)->i_mmap_sem);
44
Souptick Joarderea4d4792018-04-15 01:40:02 +053045 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080046}
47
Souptick Joarderea4d4792018-04-15 01:40:02 +053048static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049{
50 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080051 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090053 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080054 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090055
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020056 if (unlikely(f2fs_cp_error(sbi))) {
57 err = -EIO;
58 goto err;
59 }
60
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090061 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070062
63 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070064
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090065 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080066 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070067 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070068 err = f2fs_reserve_block(&dn, page->index);
69 if (err) {
70 f2fs_unlock_op(sbi);
71 goto out;
72 }
73 f2fs_put_dnode(&dn);
74 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090075
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -080076 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -080077
Dave Jiang11bac802017-02-24 14:56:41 -080078 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080079 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090080 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090081 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090082 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090083 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090084 unlock_page(page);
85 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080086 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090087 }
88
89 /*
90 * check to see if the page is mapped already (no holes)
91 */
92 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090093 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090094
95 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +080097 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +090098 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +090099
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300100 offset = i_size_read(inode) & ~PAGE_MASK;
101 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900102 }
103 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700104 if (!PageUptodate(page))
105 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900106
Chao Yub0af6d42017-08-02 23:21:48 +0800107 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
108
Jaegeuk Kime943a102013-10-25 14:26:31 +0900109 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900110mapped:
111 /* fill the page */
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800112 f2fs_wait_on_page_writeback(page, DATA, false);
Chao Yu08b39fb2015-10-08 13:27:34 +0800113
Eric Biggers6dbb1792018-04-18 11:09:48 -0700114 /* wait for GCed page writeback via META_MAPPING */
115 if (f2fs_post_read_required(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -0700116 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +0800117
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800118out_sem:
119 up_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900120out:
121 sb_end_pagefault(inode->i_sb);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800122 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200123err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900124 return block_page_mkwrite_return(err);
125}
126
127static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800128 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700129 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900130 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900131};
132
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900133static int get_parent_ino(struct inode *inode, nid_t *pino)
134{
135 struct dentry *dentry;
136
137 inode = igrab(inode);
138 dentry = d_find_any_alias(inode);
139 iput(inode);
140 if (!dentry)
141 return 0;
142
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900143 *pino = parent_ino(dentry);
144 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900145 return 1;
146}
147
Chao Yua5fd5052017-11-06 22:51:45 +0800148static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800149{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700150 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800151 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800152
Chao Yua5fd5052017-11-06 22:51:45 +0800153 if (!S_ISREG(inode->i_mode))
154 cp_reason = CP_NON_REGULAR;
155 else if (inode->i_nlink != 1)
156 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700157 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800158 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800159 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800160 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800161 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800162 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800163 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800164 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700165 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800166 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800167 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800168 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800169 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800170 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
171 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
172 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800173 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800174
Chao Yua5fd5052017-11-06 22:51:45 +0800175 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800176}
177
Changman Lee9c7bb702014-12-08 15:29:40 +0900178static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
179{
180 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
181 bool ret = false;
182 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800183 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900184 ret = true;
185 f2fs_put_page(i, 0);
186 return ret;
187}
188
Changman Lee51455b12014-12-08 15:29:41 +0900189static void try_to_fix_pino(struct inode *inode)
190{
191 struct f2fs_inode_info *fi = F2FS_I(inode);
192 nid_t pino;
193
194 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900195 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
196 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700197 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900198 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900199 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700200 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900201}
202
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700203static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
204 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205{
206 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700207 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700208 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900209 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800210 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900211 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900212 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900213 .nr_to_write = LONG_MAX,
214 .for_reclaim = 0,
215 };
216
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900217 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900218 return 0;
219
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900220 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700221
222 /* 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 Yu4d57b862018-05-30 00:20:41 +0800278 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic);
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 Yu4d57b862018-05-30 00:20:41 +0800304 ret = f2fs_wait_on_node_pages_writeback(sbi, ino);
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:
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800581 f2fs_wait_on_page_writeback(page, DATA, 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 Yu4d57b862018-05-30 00:20:41 +0800592int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900593{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700594 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900595 struct dnode_of_data dn;
596 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800597 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700598 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800599 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900600
Namjae Jeon51dd6242013-04-20 01:28:52 +0900601 trace_f2fs_truncate_blocks_enter(inode, from);
602
Chao Yudf033ca2018-03-20 23:08:29 +0800603 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900604
Chao Yu09210c92016-05-05 19:13:03 +0800605 if (free_from >= sbi->max_file_blocks)
606 goto free_partial;
607
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700608 if (lock)
609 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800610
Chao Yu4d57b862018-05-30 00:20:41 +0800611 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700612 if (IS_ERR(ipage)) {
613 err = PTR_ERR(ipage);
614 goto out;
615 }
616
617 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800618 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700619 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800620 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700621 goto out;
622 }
623
624 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800625 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900626 if (err) {
627 if (err == -ENOENT)
628 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700629 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700630 }
631
Chao Yu81ca7352016-01-26 15:39:35 +0800632 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900633
634 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700635 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900636
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900637 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800638 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639 free_from += count;
640 }
641
642 f2fs_put_dnode(&dn);
643free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800644 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800645out:
646 if (lock)
647 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800648free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700649 /* lastly zero out the first data page */
650 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800651 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900652
Namjae Jeon51dd6242013-04-20 01:28:52 +0900653 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900654 return err;
655}
656
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700657int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900658{
Chao Yub0154892015-08-24 17:39:42 +0800659 int err;
660
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200661 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
662 return -EIO;
663
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900664 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
665 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800666 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900667
Namjae Jeon51dd6242013-04-20 01:28:52 +0900668 trace_f2fs_truncate(inode);
669
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800670#ifdef CONFIG_F2FS_FAULT_INJECTION
671 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
672 f2fs_show_injection_info(FAULT_TRUNCATE);
673 return -EIO;
674 }
675#endif
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800676 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800677 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800678 err = f2fs_convert_inline_inode(inode);
679 if (err)
680 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800681 }
682
Chao Yu4d57b862018-05-30 00:20:41 +0800683 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800684 if (err)
685 return err;
686
Deepa Dinamani078cd822016-09-14 07:48:04 -0700687 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700688 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800689 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900690}
691
David Howellsa528d352017-01-31 16:46:22 +0000692int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800693 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900694{
David Howellsa528d352017-01-31 16:46:22 +0000695 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800696 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800697 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800698 unsigned int flags;
699
Chao Yu1c1d35d2018-01-25 14:54:42 +0800700 if (f2fs_has_extra_attr(inode) &&
701 f2fs_sb_has_inode_crtime(inode->i_sb) &&
702 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
703 stat->result_mask |= STATX_BTIME;
704 stat->btime.tv_sec = fi->i_crtime.tv_sec;
705 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
706 }
707
Chao Yuc807a7c2018-04-08 11:25:53 +0800708 flags = fi->i_flags & F2FS_FL_USER_VISIBLE;
Chao Yu59c84402018-04-03 15:08:17 +0800709 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800710 stat->attributes |= STATX_ATTR_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +0800711 if (flags & F2FS_COMPR_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800712 stat->attributes |= STATX_ATTR_COMPRESSED;
713 if (f2fs_encrypted_inode(inode))
714 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800715 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800716 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800717 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800718 stat->attributes |= STATX_ATTR_NODUMP;
719
720 stat->attributes_mask |= (STATX_ATTR_APPEND |
721 STATX_ATTR_COMPRESSED |
722 STATX_ATTR_ENCRYPTED |
723 STATX_ATTR_IMMUTABLE |
724 STATX_ATTR_NODUMP);
725
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700727
728 /* we need to show initial sectors used for inline_data/dentries */
729 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
730 f2fs_has_inline_dentry(inode))
731 stat->blocks += (stat->size + 511) >> 9;
732
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900733 return 0;
734}
735
736#ifdef CONFIG_F2FS_FS_POSIX_ACL
737static void __setattr_copy(struct inode *inode, const struct iattr *attr)
738{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900739 unsigned int ia_valid = attr->ia_valid;
740
741 if (ia_valid & ATTR_UID)
742 inode->i_uid = attr->ia_uid;
743 if (ia_valid & ATTR_GID)
744 inode->i_gid = attr->ia_gid;
745 if (ia_valid & ATTR_ATIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700746 inode->i_atime = timespec64_trunc(attr->ia_atime,
747 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900748 if (ia_valid & ATTR_MTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700749 inode->i_mtime = timespec64_trunc(attr->ia_mtime,
750 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900751 if (ia_valid & ATTR_CTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700752 inode->i_ctime = timespec64_trunc(attr->ia_ctime,
753 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 if (ia_valid & ATTR_MODE) {
755 umode_t mode = attr->ia_mode;
756
757 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
758 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700759 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900760 }
761}
762#else
763#define __setattr_copy setattr_copy
764#endif
765
766int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
767{
David Howells2b0143b2015-03-17 22:25:59 +0000768 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900769 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800770 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900771
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200772 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
773 return -EIO;
774
Jan Kara31051c82016-05-26 16:55:18 +0200775 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900776 if (err)
777 return err;
778
Eric Biggers20bb2472017-11-29 12:35:32 -0800779 err = fscrypt_prepare_setattr(dentry, attr);
780 if (err)
781 return err;
782
Chao Yu0abd6752017-07-09 00:13:07 +0800783 if (is_quota_modification(inode, attr)) {
784 err = dquot_initialize(inode);
785 if (err)
786 return err;
787 }
788 if ((attr->ia_valid & ATTR_UID &&
789 !uid_eq(attr->ia_uid, inode->i_uid)) ||
790 (attr->ia_valid & ATTR_GID &&
791 !gid_eq(attr->ia_gid, inode->i_gid))) {
792 err = dquot_transfer(inode, attr);
793 if (err)
794 return err;
795 }
796
Chao Yu09db6a22014-09-15 18:02:09 +0800797 if (attr->ia_valid & ATTR_SIZE) {
Chao Yu3c454142015-06-05 18:34:02 +0800798 if (attr->ia_size <= i_size_read(inode)) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800799 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu09db6a22014-09-15 18:02:09 +0800800 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700801 err = f2fs_truncate(inode);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800802 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub0154892015-08-24 17:39:42 +0800803 if (err)
804 return err;
Chao Yu09db6a22014-09-15 18:02:09 +0800805 } else {
806 /*
Chao Yu3c454142015-06-05 18:34:02 +0800807 * do not trim all blocks after i_size if target size is
808 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800809 */
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800810 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3c454142015-06-05 18:34:02 +0800811 truncate_setsize(inode, attr->ia_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800812 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu0cab80e2015-12-01 11:36:16 +0800813
814 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800815 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800816 err = f2fs_convert_inline_inode(inode);
817 if (err)
818 return err;
819 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700820 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800821 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800822
Chao Yua0d00fa2017-10-09 17:55:19 +0800823 down_write(&F2FS_I(inode)->i_sem);
824 F2FS_I(inode)->last_disk_size = i_size_read(inode);
825 up_write(&F2FS_I(inode)->i_sem);
826
Yunlei Hec0ed4402016-12-11 15:35:15 +0800827 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900828 }
829
830 __setattr_copy(inode, attr);
831
832 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800833 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700834 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
835 inode->i_mode = F2FS_I(inode)->i_acl_mode;
836 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900837 }
838 }
839
Yunlei Hec0ed4402016-12-11 15:35:15 +0800840 /* file size may changed here */
841 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700842
843 /* inode change will produce dirty node pages flushed by checkpoint */
844 f2fs_balance_fs(F2FS_I_SB(inode), true);
845
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900846 return err;
847}
848
849const struct inode_operations f2fs_file_inode_operations = {
850 .getattr = f2fs_getattr,
851 .setattr = f2fs_setattr,
852 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800853 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900854#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900855 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900856#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900857 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900858};
859
Chao Yu63943282015-08-07 18:36:06 +0800860static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900861 loff_t start, loff_t len)
862{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700863 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900864 struct page *page;
865
866 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800867 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900868
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800869 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900870
Gu Zhenge4795562013-09-27 18:08:30 +0800871 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800872 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800873 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900874
Chao Yu63943282015-08-07 18:36:06 +0800875 if (IS_ERR(page))
876 return PTR_ERR(page);
877
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800878 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800879 zero_user(page, start, len);
880 set_page_dirty(page);
881 f2fs_put_page(page, 1);
882 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900883}
884
Chao Yu4d57b862018-05-30 00:20:41 +0800885int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900886{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900887 int err;
888
Chao Yuea587112015-09-17 20:22:44 +0800889 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900890 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800891 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900892
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900893 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800894 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900895 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800896 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +0800897 pg_start = f2fs_get_next_page_offset(&dn,
898 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900899 continue;
Chao Yuea587112015-09-17 20:22:44 +0800900 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900901 return err;
902 }
903
Chao Yu81ca7352016-01-26 15:39:35 +0800904 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800905 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
906
907 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
908
Chao Yu4d57b862018-05-30 00:20:41 +0800909 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900910 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800911
912 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900913 }
914 return 0;
915}
916
Chao Yua66c7b22013-11-22 16:52:50 +0800917static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900918{
919 pgoff_t pg_start, pg_end;
920 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800921 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900922
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800923 ret = f2fs_convert_inline_inode(inode);
924 if (ret)
925 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800926
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300927 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
928 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900929
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300930 off_start = offset & (PAGE_SIZE - 1);
931 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900932
933 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800934 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900935 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800936 if (ret)
937 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900938 } else {
Chao Yu63943282015-08-07 18:36:06 +0800939 if (off_start) {
940 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300941 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800942 if (ret)
943 return ret;
944 }
945 if (off_end) {
946 ret = fill_zero(inode, pg_end, 0, off_end);
947 if (ret)
948 return ret;
949 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900950
951 if (pg_start < pg_end) {
952 struct address_space *mapping = inode->i_mapping;
953 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700954 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500955
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800956 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900957
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300958 blk_start = (loff_t)pg_start << PAGE_SHIFT;
959 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800960 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900961 truncate_inode_pages_range(mapping, blk_start,
962 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900963
Gu Zhenge4795562013-09-27 18:08:30 +0800964 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800965 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800966 f2fs_unlock_op(sbi);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800967 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900968 }
969 }
970
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900971 return ret;
972}
973
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700974static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
975 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800976{
977 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
978 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700979 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700980
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700981next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700982 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800983 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700984 if (ret && ret != -ENOENT) {
985 return ret;
986 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700987 if (dn.max_level == 0)
988 return -ENOENT;
989 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
990 blkaddr += done;
991 do_replace += done;
992 goto next;
993 }
994
995 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
996 dn.ofs_in_node, len);
997 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800998 *blkaddr = datablock_addr(dn.inode,
999 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001000 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001001
1002 if (test_opt(sbi, LFS)) {
1003 f2fs_put_dnode(&dn);
1004 return -ENOTSUPP;
1005 }
1006
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001007 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001008 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001009 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001010 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001011 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001012 f2fs_put_dnode(&dn);
1013next:
1014 len -= done;
1015 off += done;
1016 if (len)
1017 goto next_dnode;
1018 return 0;
1019}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001020
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001021static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1022 int *do_replace, pgoff_t off, int len)
1023{
1024 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1025 struct dnode_of_data dn;
1026 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001027
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001028 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1029 if (*do_replace == 0)
1030 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001031
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001032 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001033 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001034 if (ret) {
1035 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001036 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001037 } else {
1038 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001039 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001040 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001041 }
1042 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001043}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001044
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001045static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1046 block_t *blkaddr, int *do_replace,
1047 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1048{
1049 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1050 pgoff_t i = 0;
1051 int ret;
1052
1053 while (i < len) {
1054 if (blkaddr[i] == NULL_ADDR && !full) {
1055 i++;
1056 continue;
1057 }
1058
1059 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1060 struct dnode_of_data dn;
1061 struct node_info ni;
1062 size_t new_size;
1063 pgoff_t ilen;
1064
1065 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001066 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001067 if (ret)
1068 return ret;
1069
Chao Yu77357302018-07-17 00:02:17 +08001070 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1071 if (ret) {
1072 f2fs_put_dnode(&dn);
1073 return ret;
1074 }
1075
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001076 ilen = min((pgoff_t)
1077 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1078 dn.ofs_in_node, len - i);
1079 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001080 dn.data_blkaddr = datablock_addr(dn.inode,
1081 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001082 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001083
1084 if (do_replace[i]) {
1085 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001086 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001087 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001088 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001089 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1090 blkaddr[i], ni.version, true, false);
1091
1092 do_replace[i] = 0;
1093 }
1094 dn.ofs_in_node++;
1095 i++;
1096 new_size = (dst + i) << PAGE_SHIFT;
1097 if (dst_inode->i_size < new_size)
1098 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001099 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001100
1101 f2fs_put_dnode(&dn);
1102 } else {
1103 struct page *psrc, *pdst;
1104
Chao Yu4d57b862018-05-30 00:20:41 +08001105 psrc = f2fs_get_lock_data_page(src_inode,
1106 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001107 if (IS_ERR(psrc))
1108 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001109 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001110 true);
1111 if (IS_ERR(pdst)) {
1112 f2fs_put_page(psrc, 1);
1113 return PTR_ERR(pdst);
1114 }
1115 f2fs_copy_page(psrc, pdst);
1116 set_page_dirty(pdst);
1117 f2fs_put_page(pdst, 1);
1118 f2fs_put_page(psrc, 1);
1119
Chao Yu4d57b862018-05-30 00:20:41 +08001120 ret = f2fs_truncate_hole(src_inode,
1121 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001122 if (ret)
1123 return ret;
1124 i++;
1125 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001126 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001127 return 0;
1128}
1129
1130static int __exchange_data_block(struct inode *src_inode,
1131 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001132 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001133{
1134 block_t *src_blkaddr;
1135 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001136 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001137 int ret;
1138
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001139 while (len) {
1140 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001141
Chao Yu628b3d12017-11-30 19:28:18 +08001142 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001143 array_size(olen, sizeof(block_t)),
1144 GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001145 if (!src_blkaddr)
1146 return -ENOMEM;
1147
Chao Yu628b3d12017-11-30 19:28:18 +08001148 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001149 array_size(olen, sizeof(int)),
1150 GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001151 if (!do_replace) {
1152 kvfree(src_blkaddr);
1153 return -ENOMEM;
1154 }
1155
1156 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1157 do_replace, src, olen);
1158 if (ret)
1159 goto roll_back;
1160
1161 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1162 do_replace, src, dst, olen, full);
1163 if (ret)
1164 goto roll_back;
1165
1166 src += olen;
1167 dst += olen;
1168 len -= olen;
1169
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001170 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001171 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001172 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001173 return 0;
1174
1175roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001176 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001177 kvfree(src_blkaddr);
1178 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001179 return ret;
1180}
1181
1182static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1183{
1184 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001185 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001186 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001187
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001188 f2fs_balance_fs(sbi, true);
1189 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001190
1191 f2fs_drop_extent_tree(inode);
1192
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001193 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1194 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +08001195 return ret;
1196}
1197
1198static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1199{
1200 pgoff_t pg_start, pg_end;
1201 loff_t new_size;
1202 int ret;
1203
Chao Yub4ace332015-05-06 13:09:46 +08001204 if (offset + len >= i_size_read(inode))
1205 return -EINVAL;
1206
1207 /* collapse range should be aligned to block size of f2fs. */
1208 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1209 return -EINVAL;
1210
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001211 ret = f2fs_convert_inline_inode(inode);
1212 if (ret)
1213 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001214
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001215 pg_start = offset >> PAGE_SHIFT;
1216 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yub4ace332015-05-06 13:09:46 +08001217
Chao Yu21020812017-11-23 23:26:52 +08001218 /* avoid gc operation during block exchange */
Chao Yub2532c62018-04-24 10:55:28 +08001219 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu21020812017-11-23 23:26:52 +08001220
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001221 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001222 /* write out all dirty pages from offset */
1223 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1224 if (ret)
Chao Yu21020812017-11-23 23:26:52 +08001225 goto out_unlock;
Chao Yubb066642017-11-03 10:21:05 +08001226
Chao Yub4ace332015-05-06 13:09:46 +08001227 truncate_pagecache(inode, offset);
1228
1229 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1230 if (ret)
Chao Yubb066642017-11-03 10:21:05 +08001231 goto out_unlock;
Chao Yub4ace332015-05-06 13:09:46 +08001232
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001233 /* write out all moved pages, if possible */
1234 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1235 truncate_pagecache(inode, offset);
1236
Chao Yub4ace332015-05-06 13:09:46 +08001237 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001238 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001239
Chao Yu4d57b862018-05-30 00:20:41 +08001240 ret = f2fs_truncate_blocks(inode, new_size, true);
Chao Yub4ace332015-05-06 13:09:46 +08001241 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001242 f2fs_i_size_write(inode, new_size);
Chao Yubb066642017-11-03 10:21:05 +08001243out_unlock:
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001244 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub2532c62018-04-24 10:55:28 +08001245 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001246 return ret;
1247}
1248
Chao Yu6e961942016-05-09 19:56:31 +08001249static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1250 pgoff_t end)
1251{
1252 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1253 pgoff_t index = start;
1254 unsigned int ofs_in_node = dn->ofs_in_node;
1255 blkcnt_t count = 0;
1256 int ret;
1257
1258 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001259 if (datablock_addr(dn->inode, dn->node_page,
1260 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001261 count++;
1262 }
1263
1264 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001265 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001266 if (ret)
1267 return ret;
1268
1269 dn->ofs_in_node = ofs_in_node;
1270 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001271 dn->data_blkaddr = datablock_addr(dn->inode,
1272 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001273 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001274 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001275 * allocation.
1276 */
1277 if (dn->data_blkaddr == NULL_ADDR) {
1278 ret = -ENOSPC;
1279 break;
1280 }
1281 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001282 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001283 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001284 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001285 }
1286 }
1287
1288 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1289
1290 return ret;
1291}
1292
Chao Yu75cd4e02015-05-06 13:11:13 +08001293static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1294 int mode)
1295{
1296 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1297 struct address_space *mapping = inode->i_mapping;
1298 pgoff_t index, pg_start, pg_end;
1299 loff_t new_size = i_size_read(inode);
1300 loff_t off_start, off_end;
1301 int ret = 0;
1302
Chao Yu75cd4e02015-05-06 13:11:13 +08001303 ret = inode_newsize_ok(inode, (len + offset));
1304 if (ret)
1305 return ret;
1306
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001307 ret = f2fs_convert_inline_inode(inode);
1308 if (ret)
1309 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001310
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001311 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001312 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1313 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001314 goto out_sem;
Chao Yu75cd4e02015-05-06 13:11:13 +08001315
1316 truncate_pagecache_range(inode, offset, offset + len - 1);
1317
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001318 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1319 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001320
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001321 off_start = offset & (PAGE_SIZE - 1);
1322 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001323
1324 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001325 ret = fill_zero(inode, pg_start, off_start,
1326 off_end - off_start);
1327 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001328 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001329
Chao Yu75cd4e02015-05-06 13:11:13 +08001330 new_size = max_t(loff_t, new_size, offset + len);
1331 } else {
1332 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001333 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001334 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001335 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001336 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001337
Chao Yu75cd4e02015-05-06 13:11:13 +08001338 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001339 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001340 }
1341
Chao Yu6e961942016-05-09 19:56:31 +08001342 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001343 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001344 unsigned int end_offset;
1345 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001346
1347 f2fs_lock_op(sbi);
1348
Chao Yu6e961942016-05-09 19:56:31 +08001349 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001350 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001351 if (ret) {
1352 f2fs_unlock_op(sbi);
1353 goto out;
1354 }
1355
Chao Yu6e961942016-05-09 19:56:31 +08001356 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1357 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1358
1359 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001360 f2fs_put_dnode(&dn);
1361 f2fs_unlock_op(sbi);
Chao Yu9434fcd2016-10-11 22:57:02 +08001362
1363 f2fs_balance_fs(sbi, dn.node_changed);
1364
Chao Yu6e961942016-05-09 19:56:31 +08001365 if (ret)
1366 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001367
Chao Yu6e961942016-05-09 19:56:31 +08001368 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001369 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001370 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001371 }
1372
1373 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001374 ret = fill_zero(inode, pg_end, 0, off_end);
1375 if (ret)
1376 goto out;
1377
Chao Yu75cd4e02015-05-06 13:11:13 +08001378 new_size = max_t(loff_t, new_size, offset + len);
1379 }
1380 }
1381
1382out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001383 if (new_size > i_size_read(inode)) {
1384 if (mode & FALLOC_FL_KEEP_SIZE)
1385 file_set_keep_isize(inode);
1386 else
1387 f2fs_i_size_write(inode, new_size);
1388 }
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001389out_sem:
1390 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001391
1392 return ret;
1393}
1394
Chao Yuf62185d2015-05-28 19:16:57 +08001395static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1396{
1397 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001398 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001399 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001400 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001401
Chao Yuf62185d2015-05-28 19:16:57 +08001402 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001403 ret = inode_newsize_ok(inode, new_size);
1404 if (ret)
1405 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001406
1407 if (offset >= i_size_read(inode))
1408 return -EINVAL;
1409
1410 /* insert range should be aligned to block size of f2fs. */
1411 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1412 return -EINVAL;
1413
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001414 ret = f2fs_convert_inline_inode(inode);
1415 if (ret)
1416 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001417
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001418 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001419
Chao Yu21020812017-11-23 23:26:52 +08001420 /* avoid gc operation during block exchange */
Chao Yub2532c62018-04-24 10:55:28 +08001421 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu21020812017-11-23 23:26:52 +08001422
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001423 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu4d57b862018-05-30 00:20:41 +08001424 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yuf62185d2015-05-28 19:16:57 +08001425 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001426 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001427
1428 /* write out all dirty pages from offset */
1429 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1430 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001431 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001432
1433 truncate_pagecache(inode, offset);
1434
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001435 pg_start = offset >> PAGE_SHIFT;
1436 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001437 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001438 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001439
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001440 while (!ret && idx > pg_start) {
1441 nr = idx - pg_start;
1442 if (nr > delta)
1443 nr = delta;
1444 idx -= nr;
1445
Chao Yuf62185d2015-05-28 19:16:57 +08001446 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001447 f2fs_drop_extent_tree(inode);
1448
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001449 ret = __exchange_data_block(inode, inode, idx,
1450 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001451 f2fs_unlock_op(sbi);
1452 }
1453
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001454 /* write out all moved pages, if possible */
1455 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1456 truncate_pagecache(inode, offset);
1457
1458 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001459 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001460out:
1461 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub2532c62018-04-24 10:55:28 +08001462 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001463 return ret;
1464}
1465
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001466static int expand_inode_data(struct inode *inode, loff_t offset,
1467 loff_t len, int mode)
1468{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001469 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001470 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuc4020b22018-01-11 14:42:30 +08001471 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001472 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001473 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001474 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001475 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001476
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001477 err = inode_newsize_ok(inode, (len + offset));
1478 if (err)
1479 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001480
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001481 err = f2fs_convert_inline_inode(inode);
1482 if (err)
1483 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001484
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001485 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001486
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001487 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001488 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001489
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001490 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1491 map.m_len = pg_end - map.m_lblk;
1492 if (off_end)
1493 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001494
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001495 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1496 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001497 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001498
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001499 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001500 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001501
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001502 last_off = map.m_lblk + map.m_len - 1;
1503
1504 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001505 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001506 (loff_t)(last_off + 1) << PAGE_SHIFT;
1507 } else {
1508 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001509 }
1510
Chao Yue8ed90a2017-11-05 21:53:30 +08001511 if (new_size > i_size_read(inode)) {
1512 if (mode & FALLOC_FL_KEEP_SIZE)
1513 file_set_keep_isize(inode);
1514 else
1515 f2fs_i_size_write(inode, new_size);
1516 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001517
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001518 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001519}
1520
1521static long f2fs_fallocate(struct file *file, int mode,
1522 loff_t offset, loff_t len)
1523{
Al Viro6131ffa2013-02-27 16:59:05 -05001524 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001525 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001526
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001527 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1528 return -EIO;
1529
Chao Yuc9980122015-09-11 14:39:02 +08001530 /* f2fs only support ->fallocate for regular file */
1531 if (!S_ISREG(inode->i_mode))
1532 return -EINVAL;
1533
Chao Yuf62185d2015-05-28 19:16:57 +08001534 if (f2fs_encrypted_inode(inode) &&
1535 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001536 return -EOPNOTSUPP;
1537
Chao Yub4ace332015-05-06 13:09:46 +08001538 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001539 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1540 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001541 return -EOPNOTSUPP;
1542
Al Viro59551022016-01-22 15:40:57 -05001543 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001544
Taehee Yoo587c0a42015-04-21 15:59:12 +09001545 if (mode & FALLOC_FL_PUNCH_HOLE) {
1546 if (offset >= inode->i_size)
1547 goto out;
1548
Chao Yua66c7b22013-11-22 16:52:50 +08001549 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001550 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1551 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001552 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1553 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001554 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1555 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001556 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001557 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001558 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001559
Namjae Jeon3af60a42012-12-30 14:52:37 +09001560 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001561 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001562 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001563 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001564 }
Chao Yu3375f692014-01-28 10:29:26 +08001565
Taehee Yoo587c0a42015-04-21 15:59:12 +09001566out:
Al Viro59551022016-01-22 15:40:57 -05001567 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001568
Namjae Jeonc01e2852013-04-23 17:00:52 +09001569 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001570 return ret;
1571}
1572
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001573static int f2fs_release_file(struct inode *inode, struct file *filp)
1574{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001575 /*
1576 * f2fs_relase_file is called at every close calls. So we should
1577 * not drop any inmemory pages by close called by other process.
1578 */
1579 if (!(filp->f_mode & FMODE_WRITE) ||
1580 atomic_read(&inode->i_writecount) != 1)
1581 return 0;
1582
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001583 /* some remained atomic pages should discarded */
1584 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001585 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001586 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001587 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001588 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001589 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001590 clear_inode_flag(inode, FI_VOLATILE_FILE);
1591 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001592 }
1593 return 0;
1594}
1595
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001596static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001597{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001598 struct inode *inode = file_inode(file);
1599
1600 /*
1601 * If the process doing a transaction is crashed, we should do
1602 * roll-back. Otherwise, other reader/write can see corrupted database
1603 * until all the writers close its file. Since this should be done
1604 * before dropping file lock, it needs to do in ->flush.
1605 */
1606 if (f2fs_is_atomic_file(inode) &&
1607 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001608 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001609 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001610}
1611
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001612static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001613{
Al Viro6131ffa2013-02-27 16:59:05 -05001614 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001615 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu76481152018-04-08 11:27:14 +08001616 unsigned int flags = fi->i_flags;
1617
Chao Yu5b72d5e2018-07-17 20:41:47 +08001618 if (f2fs_encrypted_inode(inode))
Chao Yu76481152018-04-08 11:27:14 +08001619 flags |= F2FS_ENCRYPT_FL;
1620 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1621 flags |= F2FS_INLINE_DATA_FL;
1622
1623 flags &= F2FS_FL_USER_VISIBLE;
1624
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001625 return put_user(flags, (int __user *)arg);
1626}
1627
Chao Yu2c1d0302017-07-29 00:32:52 +08001628static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1629{
1630 struct f2fs_inode_info *fi = F2FS_I(inode);
1631 unsigned int oldflags;
1632
1633 /* Is it quota file? Do not allow user to mess with it */
1634 if (IS_NOQUOTA(inode))
1635 return -EPERM;
1636
1637 flags = f2fs_mask_flags(inode->i_mode, flags);
1638
1639 oldflags = fi->i_flags;
1640
Chao Yu59c84402018-04-03 15:08:17 +08001641 if ((flags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
Chao Yu2c1d0302017-07-29 00:32:52 +08001642 if (!capable(CAP_LINUX_IMMUTABLE))
1643 return -EPERM;
1644
Chao Yuc807a7c2018-04-08 11:25:53 +08001645 flags = flags & F2FS_FL_USER_MODIFIABLE;
1646 flags |= oldflags & ~F2FS_FL_USER_MODIFIABLE;
Chao Yu2c1d0302017-07-29 00:32:52 +08001647 fi->i_flags = flags;
1648
Chao Yu59c84402018-04-03 15:08:17 +08001649 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001650 set_inode_flag(inode, FI_PROJ_INHERIT);
1651 else
1652 clear_inode_flag(inode, FI_PROJ_INHERIT);
1653
1654 inode->i_ctime = current_time(inode);
1655 f2fs_set_inode_flags(inode);
1656 f2fs_mark_inode_dirty_sync(inode, false);
1657 return 0;
1658}
1659
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001660static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1661{
1662 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001663 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001664 int ret;
1665
Chao Yu7fb17fe2016-05-09 19:56:32 +08001666 if (!inode_owner_or_capable(inode))
1667 return -EACCES;
1668
1669 if (get_user(flags, (int __user *)arg))
1670 return -EFAULT;
1671
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001672 ret = mnt_want_write_file(filp);
1673 if (ret)
1674 return ret;
1675
Al Viro59551022016-01-22 15:40:57 -05001676 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001677
Chao Yu2c1d0302017-07-29 00:32:52 +08001678 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001679
Chao Yua72d4b92017-05-03 23:59:13 +08001680 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001681 mnt_drop_write_file(filp);
1682 return ret;
1683}
1684
Chao Yud49f3e82015-01-23 20:36:04 +08001685static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1686{
1687 struct inode *inode = file_inode(filp);
1688
1689 return put_user(inode->i_generation, (int __user *)arg);
1690}
1691
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001692static int f2fs_ioc_start_atomic_write(struct file *filp)
1693{
1694 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001695 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001696
1697 if (!inode_owner_or_capable(inode))
1698 return -EACCES;
1699
Jaegeuk Kime8118982017-03-17 10:04:15 +08001700 if (!S_ISREG(inode->i_mode))
1701 return -EINVAL;
1702
Chao Yu7fb17fe2016-05-09 19:56:32 +08001703 ret = mnt_want_write_file(filp);
1704 if (ret)
1705 return ret;
1706
Chao Yu0fac5582016-05-09 19:56:33 +08001707 inode_lock(inode);
1708
Chao Yub2532c62018-04-24 10:55:28 +08001709 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu27319ba2018-04-17 17:51:28 +08001710
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001711 if (f2fs_is_atomic_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001712 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001713
Chao Yuf4c9c742015-07-17 18:06:35 +08001714 ret = f2fs_convert_inline_inode(inode);
1715 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001716 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001717
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001718 if (!get_dirty_pages(inode))
Yunlei He054afda2018-04-18 11:06:39 +08001719 goto skip_flush;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001720
1721 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001722 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001723 inode->i_ino, get_dirty_pages(inode));
1724 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Yunlei He054afda2018-04-18 11:06:39 +08001725 if (ret)
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001726 goto out;
Yunlei He054afda2018-04-18 11:06:39 +08001727skip_flush:
Yunlei He054afda2018-04-18 11:06:39 +08001728 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001729 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Yunlei He054afda2018-04-18 11:06:39 +08001730 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001731
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001732 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001733 stat_inc_atomic_write(inode);
1734 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001735out:
Chao Yub2532c62018-04-24 10:55:28 +08001736 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu0fac5582016-05-09 19:56:33 +08001737 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001738 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001739 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001740}
1741
1742static int f2fs_ioc_commit_atomic_write(struct file *filp)
1743{
1744 struct inode *inode = file_inode(filp);
1745 int ret;
1746
1747 if (!inode_owner_or_capable(inode))
1748 return -EACCES;
1749
1750 ret = mnt_want_write_file(filp);
1751 if (ret)
1752 return ret;
1753
Chao Yu0fac5582016-05-09 19:56:33 +08001754 inode_lock(inode);
1755
Chao Yub2532c62018-04-24 10:55:28 +08001756 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu1dc0f892018-02-27 22:45:24 +08001757
Chao Yub169c3c2018-04-18 17:45:02 +08001758 if (f2fs_is_volatile_file(inode)) {
1759 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001760 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08001761 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001762
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001763 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08001764 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001765 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001766 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001767
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001768 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001769 if (!ret) {
1770 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001771 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +08001772 stat_dec_atomic_write(inode);
1773 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001774 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001775 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001776 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001777err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08001778 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1779 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1780 ret = -EINVAL;
1781 }
Chao Yub2532c62018-04-24 10:55:28 +08001782 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu0fac5582016-05-09 19:56:33 +08001783 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001784 mnt_drop_write_file(filp);
1785 return ret;
1786}
1787
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001788static int f2fs_ioc_start_volatile_write(struct file *filp)
1789{
1790 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001791 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001792
1793 if (!inode_owner_or_capable(inode))
1794 return -EACCES;
1795
Chao Yu8ff09712017-03-17 15:43:57 +08001796 if (!S_ISREG(inode->i_mode))
1797 return -EINVAL;
1798
Chao Yu7fb17fe2016-05-09 19:56:32 +08001799 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001800 if (ret)
1801 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001802
Chao Yu0fac5582016-05-09 19:56:33 +08001803 inode_lock(inode);
1804
Chao Yu7fb17fe2016-05-09 19:56:32 +08001805 if (f2fs_is_volatile_file(inode))
1806 goto out;
1807
1808 ret = f2fs_convert_inline_inode(inode);
1809 if (ret)
1810 goto out;
1811
Chao Yu648d50b2017-03-22 17:23:45 +08001812 stat_inc_volatile_write(inode);
1813 stat_update_max_volatile_write(inode);
1814
Jaegeuk Kim91942322016-05-20 10:13:22 -07001815 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001816 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001817out:
Chao Yu0fac5582016-05-09 19:56:33 +08001818 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001819 mnt_drop_write_file(filp);
1820 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001821}
1822
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001823static int f2fs_ioc_release_volatile_write(struct file *filp)
1824{
1825 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001826 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001827
1828 if (!inode_owner_or_capable(inode))
1829 return -EACCES;
1830
Chao Yu7fb17fe2016-05-09 19:56:32 +08001831 ret = mnt_want_write_file(filp);
1832 if (ret)
1833 return ret;
1834
Chao Yu0fac5582016-05-09 19:56:33 +08001835 inode_lock(inode);
1836
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001837 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001838 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001839
Chao Yu7fb17fe2016-05-09 19:56:32 +08001840 if (!f2fs_is_first_block_written(inode)) {
1841 ret = truncate_partial_data_page(inode, 0, true);
1842 goto out;
1843 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001844
Chao Yu7fb17fe2016-05-09 19:56:32 +08001845 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1846out:
Chao Yu0fac5582016-05-09 19:56:33 +08001847 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001848 mnt_drop_write_file(filp);
1849 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001850}
1851
1852static int f2fs_ioc_abort_volatile_write(struct file *filp)
1853{
1854 struct inode *inode = file_inode(filp);
1855 int ret;
1856
1857 if (!inode_owner_or_capable(inode))
1858 return -EACCES;
1859
1860 ret = mnt_want_write_file(filp);
1861 if (ret)
1862 return ret;
1863
Chao Yu0fac5582016-05-09 19:56:33 +08001864 inode_lock(inode);
1865
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001866 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001867 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001868 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001869 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001870 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001871 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001872 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001873
Chao Yu0fac5582016-05-09 19:56:33 +08001874 inode_unlock(inode);
1875
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001876 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001877 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001878 return ret;
1879}
1880
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001881static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1882{
1883 struct inode *inode = file_inode(filp);
1884 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1885 struct super_block *sb = sbi->sb;
1886 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03001887 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001888
1889 if (!capable(CAP_SYS_ADMIN))
1890 return -EPERM;
1891
1892 if (get_user(in, (__u32 __user *)arg))
1893 return -EFAULT;
1894
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301895 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1896 ret = mnt_want_write_file(filp);
1897 if (ret)
1898 return ret;
1899 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001900
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001901 switch (in) {
1902 case F2FS_GOING_DOWN_FULLSYNC:
1903 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08001904 if (IS_ERR(sb)) {
1905 ret = PTR_ERR(sb);
1906 goto out;
1907 }
1908 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001909 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001910 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001911 thaw_bdev(sb->s_bdev, sb);
1912 }
1913 break;
1914 case F2FS_GOING_DOWN_METASYNC:
1915 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08001916 ret = f2fs_sync_fs(sb, 1);
1917 if (ret)
1918 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001919 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001920 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001921 break;
1922 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001923 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001924 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001925 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001926 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08001927 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001928 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001929 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001930 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001931 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001932 ret = -EINVAL;
1933 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001934 }
Chao Yu7950e9a2018-01-18 17:23:29 +08001935
Chao Yu4d57b862018-05-30 00:20:41 +08001936 f2fs_stop_gc_thread(sbi);
1937 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001938
Chao Yu4d57b862018-05-30 00:20:41 +08001939 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001940 clear_opt(sbi, DISCARD);
1941
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001942 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001943out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301944 if (in != F2FS_GOING_DOWN_FULLSYNC)
1945 mnt_drop_write_file(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001946 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001947}
1948
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001949static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1950{
1951 struct inode *inode = file_inode(filp);
1952 struct super_block *sb = inode->i_sb;
1953 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1954 struct fstrim_range range;
1955 int ret;
1956
1957 if (!capable(CAP_SYS_ADMIN))
1958 return -EPERM;
1959
1960 if (!blk_queue_discard(q))
1961 return -EOPNOTSUPP;
1962
1963 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1964 sizeof(range)))
1965 return -EFAULT;
1966
Chao Yu7fb17fe2016-05-09 19:56:32 +08001967 ret = mnt_want_write_file(filp);
1968 if (ret)
1969 return ret;
1970
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001971 range.minlen = max((unsigned int)range.minlen,
1972 q->limits.discard_granularity);
1973 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001974 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001975 if (ret < 0)
1976 return ret;
1977
1978 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1979 sizeof(range)))
1980 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001981 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001982 return 0;
1983}
1984
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001985static bool uuid_is_nonzero(__u8 u[16])
1986{
1987 int i;
1988
1989 for (i = 0; i < 16; i++)
1990 if (u[i])
1991 return true;
1992 return false;
1993}
1994
1995static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1996{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001997 struct inode *inode = file_inode(filp);
1998
Sheng Yongccd31cb2018-02-06 12:31:17 +08001999 if (!f2fs_sb_has_encrypt(inode->i_sb))
Chao Yuead710b2017-11-14 19:28:42 +08002000 return -EOPNOTSUPP;
2001
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002002 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002003
Eric Biggersdb717d82016-11-26 19:07:49 -05002004 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002005}
2006
2007static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2008{
Sheng Yongccd31cb2018-02-06 12:31:17 +08002009 if (!f2fs_sb_has_encrypt(file_inode(filp)->i_sb))
Chao Yuead710b2017-11-14 19:28:42 +08002010 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002011 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002012}
2013
2014static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2015{
2016 struct inode *inode = file_inode(filp);
2017 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2018 int err;
2019
Sheng Yongccd31cb2018-02-06 12:31:17 +08002020 if (!f2fs_sb_has_encrypt(inode->i_sb))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002021 return -EOPNOTSUPP;
2022
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002023 err = mnt_want_write_file(filp);
2024 if (err)
2025 return err;
2026
Chao Yu846ae672018-02-26 22:04:13 +08002027 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002028
2029 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2030 goto got_it;
2031
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002032 /* update superblock with uuid */
2033 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2034
Chao Yuc5bda1c2015-06-08 13:28:03 +08002035 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002036 if (err) {
2037 /* undo new data */
2038 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002039 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002040 }
2041got_it:
2042 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2043 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002044 err = -EFAULT;
2045out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002046 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002047 mnt_drop_write_file(filp);
2048 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002049}
2050
Chao Yuc1c1b582015-07-10 18:08:10 +08002051static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2052{
2053 struct inode *inode = file_inode(filp);
2054 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002055 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002056 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002057
2058 if (!capable(CAP_SYS_ADMIN))
2059 return -EPERM;
2060
Chao Yud530d4d2015-10-05 22:22:44 +08002061 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002062 return -EFAULT;
2063
Chao Yud530d4d2015-10-05 22:22:44 +08002064 if (f2fs_readonly(sbi->sb))
2065 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002066
Chao Yu7fb17fe2016-05-09 19:56:32 +08002067 ret = mnt_want_write_file(filp);
2068 if (ret)
2069 return ret;
2070
Chao Yud530d4d2015-10-05 22:22:44 +08002071 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002072 if (!mutex_trylock(&sbi->gc_mutex)) {
2073 ret = -EBUSY;
2074 goto out;
2075 }
Chao Yud530d4d2015-10-05 22:22:44 +08002076 } else {
2077 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002078 }
2079
Jaegeuk Kime066b832017-04-13 15:17:00 -07002080 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002081out:
2082 mnt_drop_write_file(filp);
2083 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002084}
2085
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002086static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2087{
2088 struct inode *inode = file_inode(filp);
2089 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2090 struct f2fs_gc_range range;
2091 u64 end;
2092 int ret;
2093
2094 if (!capable(CAP_SYS_ADMIN))
2095 return -EPERM;
2096
2097 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2098 sizeof(range)))
2099 return -EFAULT;
2100
2101 if (f2fs_readonly(sbi->sb))
2102 return -EROFS;
2103
Yunlei Heb82f6e32018-04-24 11:40:30 +08002104 end = range.start + range.len;
2105 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
2106 return -EINVAL;
2107 }
2108
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002109 ret = mnt_want_write_file(filp);
2110 if (ret)
2111 return ret;
2112
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002113do_more:
2114 if (!range.sync) {
2115 if (!mutex_trylock(&sbi->gc_mutex)) {
2116 ret = -EBUSY;
2117 goto out;
2118 }
2119 } else {
2120 mutex_lock(&sbi->gc_mutex);
2121 }
2122
2123 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2124 range.start += sbi->blocks_per_seg;
2125 if (range.start <= end)
2126 goto do_more;
2127out:
2128 mnt_drop_write_file(filp);
2129 return ret;
2130}
2131
Chao Yu059c0642018-07-17 20:41:49 +08002132static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002133{
2134 struct inode *inode = file_inode(filp);
2135 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002136 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002137
2138 if (!capable(CAP_SYS_ADMIN))
2139 return -EPERM;
2140
2141 if (f2fs_readonly(sbi->sb))
2142 return -EROFS;
2143
Chao Yu7fb17fe2016-05-09 19:56:32 +08002144 ret = mnt_want_write_file(filp);
2145 if (ret)
2146 return ret;
2147
2148 ret = f2fs_sync_fs(sbi->sb, 1);
2149
2150 mnt_drop_write_file(filp);
2151 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002152}
2153
Chao Yud323d002015-10-27 09:53:45 +08002154static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2155 struct file *filp,
2156 struct f2fs_defragment *range)
2157{
2158 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002159 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2160 .m_seg_type = NO_CHECK_TYPE };
youngjun yoo1061fd42018-05-30 04:34:58 +09002161 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002162 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002163 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002164 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002165 block_t blk_end = 0;
2166 bool fragmented = false;
2167 int err;
2168
2169 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002170 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002171 return -EINVAL;
2172
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002173 pg_start = range->start >> PAGE_SHIFT;
2174 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002175
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002176 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002177
Al Viro59551022016-01-22 15:40:57 -05002178 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002179
2180 /* writeback all dirty pages in the range */
2181 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002182 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002183 if (err)
2184 goto out;
2185
2186 /*
2187 * lookup mapping info in extent cache, skip defragmenting if physical
2188 * block addresses are continuous.
2189 */
2190 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2191 if (ei.fofs + ei.len >= pg_end)
2192 goto out;
2193 }
2194
2195 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002196 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002197
2198 /*
2199 * lookup mapping info in dnode page cache, skip defragmenting if all
2200 * physical block addresses are continuous even if there are hole(s)
2201 * in logical blocks.
2202 */
2203 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002204 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002205 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002206 if (err)
2207 goto out;
2208
2209 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002210 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002211 continue;
2212 }
2213
Chao Yu25a912e2018-01-10 18:18:51 +08002214 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002215 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002216
2217 /* record total count of block that we're going to move */
2218 total += map.m_len;
2219
Chao Yud323d002015-10-27 09:53:45 +08002220 blk_end = map.m_pblk + map.m_len;
2221
2222 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002223 }
2224
2225 if (!fragmented)
2226 goto out;
2227
Chao Yu25a912e2018-01-10 18:18:51 +08002228 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002229
2230 /*
2231 * make sure there are enough free section for LFS allocation, this can
2232 * avoid defragment running in SSR mode when free section are allocated
2233 * intensively
2234 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002235 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002236 err = -EAGAIN;
2237 goto out;
2238 }
2239
Chao Yu25a912e2018-01-10 18:18:51 +08002240 map.m_lblk = pg_start;
2241 map.m_len = pg_end - pg_start;
2242 total = 0;
2243
Chao Yud323d002015-10-27 09:53:45 +08002244 while (map.m_lblk < pg_end) {
2245 pgoff_t idx;
2246 int cnt = 0;
2247
2248do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002249 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002250 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002251 if (err)
2252 goto clear_out;
2253
2254 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002255 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002256 continue;
2257 }
2258
Jaegeuk Kim91942322016-05-20 10:13:22 -07002259 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002260
2261 idx = map.m_lblk;
2262 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2263 struct page *page;
2264
Chao Yu4d57b862018-05-30 00:20:41 +08002265 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002266 if (IS_ERR(page)) {
2267 err = PTR_ERR(page);
2268 goto clear_out;
2269 }
2270
2271 set_page_dirty(page);
2272 f2fs_put_page(page, 1);
2273
2274 idx++;
2275 cnt++;
2276 total++;
2277 }
2278
2279 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002280
2281 if (idx < pg_end && cnt < blk_per_seg)
2282 goto do_map;
2283
Jaegeuk Kim91942322016-05-20 10:13:22 -07002284 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002285
2286 err = filemap_fdatawrite(inode->i_mapping);
2287 if (err)
2288 goto out;
2289 }
2290clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002291 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002292out:
Al Viro59551022016-01-22 15:40:57 -05002293 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002294 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002295 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002296 return err;
2297}
2298
2299static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2300{
2301 struct inode *inode = file_inode(filp);
2302 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2303 struct f2fs_defragment range;
2304 int err;
2305
2306 if (!capable(CAP_SYS_ADMIN))
2307 return -EPERM;
2308
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002309 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002310 return -EINVAL;
2311
Kinglong Meed7563862017-03-10 17:55:07 +08002312 if (f2fs_readonly(sbi->sb))
2313 return -EROFS;
2314
2315 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2316 sizeof(range)))
2317 return -EFAULT;
2318
2319 /* verify alignment of offset & size */
2320 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2321 return -EINVAL;
2322
2323 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2324 sbi->max_file_blocks))
2325 return -EINVAL;
2326
Chao Yud323d002015-10-27 09:53:45 +08002327 err = mnt_want_write_file(filp);
2328 if (err)
2329 return err;
2330
Chao Yud323d002015-10-27 09:53:45 +08002331 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002332 mnt_drop_write_file(filp);
2333
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002334 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002335 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002336 return err;
Chao Yud323d002015-10-27 09:53:45 +08002337
2338 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2339 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002340 return -EFAULT;
2341
2342 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002343}
2344
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002345static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2346 struct file *file_out, loff_t pos_out, size_t len)
2347{
2348 struct inode *src = file_inode(file_in);
2349 struct inode *dst = file_inode(file_out);
2350 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2351 size_t olen = len, dst_max_i_size = 0;
2352 size_t dst_osize;
2353 int ret;
2354
2355 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2356 src->i_sb != dst->i_sb)
2357 return -EXDEV;
2358
2359 if (unlikely(f2fs_readonly(src->i_sb)))
2360 return -EROFS;
2361
Chao Yufe8494b2016-08-04 20:13:02 +08002362 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2363 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002364
2365 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2366 return -EOPNOTSUPP;
2367
Fan Lid95fd912016-09-13 11:35:42 +08002368 if (src == dst) {
2369 if (pos_in == pos_out)
2370 return 0;
2371 if (pos_out > pos_in && pos_out < pos_in + len)
2372 return -EINVAL;
2373 }
2374
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002375 inode_lock(src);
Chao Yub2532c62018-04-24 10:55:28 +08002376 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Chao Yu20a3d612016-08-04 20:13:03 +08002377 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002378 ret = -EBUSY;
2379 if (!inode_trylock(dst))
2380 goto out;
Chao Yub2532c62018-04-24 10:55:28 +08002381 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE])) {
Chao Yubb066642017-11-03 10:21:05 +08002382 inode_unlock(dst);
Chao Yu20a3d612016-08-04 20:13:03 +08002383 goto out;
2384 }
2385 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002386
2387 ret = -EINVAL;
2388 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2389 goto out_unlock;
2390 if (len == 0)
2391 olen = len = src->i_size - pos_in;
2392 if (pos_in + len == src->i_size)
2393 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2394 if (len == 0) {
2395 ret = 0;
2396 goto out_unlock;
2397 }
2398
2399 dst_osize = dst->i_size;
2400 if (pos_out + olen > dst->i_size)
2401 dst_max_i_size = pos_out + olen;
2402
2403 /* verify the end result is block aligned */
2404 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2405 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2406 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2407 goto out_unlock;
2408
2409 ret = f2fs_convert_inline_inode(src);
2410 if (ret)
2411 goto out_unlock;
2412
2413 ret = f2fs_convert_inline_inode(dst);
2414 if (ret)
2415 goto out_unlock;
2416
2417 /* write out all dirty pages from offset */
2418 ret = filemap_write_and_wait_range(src->i_mapping,
2419 pos_in, pos_in + len);
2420 if (ret)
2421 goto out_unlock;
2422
2423 ret = filemap_write_and_wait_range(dst->i_mapping,
2424 pos_out, pos_out + len);
2425 if (ret)
2426 goto out_unlock;
2427
2428 f2fs_balance_fs(sbi, true);
2429 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002430 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2431 pos_out >> F2FS_BLKSIZE_BITS,
2432 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002433
2434 if (!ret) {
2435 if (dst_max_i_size)
2436 f2fs_i_size_write(dst, dst_max_i_size);
2437 else if (dst_osize != dst->i_size)
2438 f2fs_i_size_write(dst, dst_osize);
2439 }
2440 f2fs_unlock_op(sbi);
2441out_unlock:
Chao Yubb066642017-11-03 10:21:05 +08002442 if (src != dst) {
Chao Yub2532c62018-04-24 10:55:28 +08002443 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002444 inode_unlock(dst);
Chao Yubb066642017-11-03 10:21:05 +08002445 }
Chao Yu20a3d612016-08-04 20:13:03 +08002446out:
Chao Yub2532c62018-04-24 10:55:28 +08002447 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002448 inode_unlock(src);
2449 return ret;
2450}
2451
2452static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2453{
2454 struct f2fs_move_range range;
2455 struct fd dst;
2456 int err;
2457
2458 if (!(filp->f_mode & FMODE_READ) ||
2459 !(filp->f_mode & FMODE_WRITE))
2460 return -EBADF;
2461
2462 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2463 sizeof(range)))
2464 return -EFAULT;
2465
2466 dst = fdget(range.dst_fd);
2467 if (!dst.file)
2468 return -EBADF;
2469
2470 if (!(dst.file->f_mode & FMODE_WRITE)) {
2471 err = -EBADF;
2472 goto err_out;
2473 }
2474
2475 err = mnt_want_write_file(filp);
2476 if (err)
2477 goto err_out;
2478
2479 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2480 range.pos_out, range.len);
2481
2482 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002483 if (err)
2484 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002485
2486 if (copy_to_user((struct f2fs_move_range __user *)arg,
2487 &range, sizeof(range)))
2488 err = -EFAULT;
2489err_out:
2490 fdput(dst);
2491 return err;
2492}
2493
Jaegeuk Kime066b832017-04-13 15:17:00 -07002494static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2495{
2496 struct inode *inode = file_inode(filp);
2497 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2498 struct sit_info *sm = SIT_I(sbi);
2499 unsigned int start_segno = 0, end_segno = 0;
2500 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2501 struct f2fs_flush_device range;
2502 int ret;
2503
2504 if (!capable(CAP_SYS_ADMIN))
2505 return -EPERM;
2506
2507 if (f2fs_readonly(sbi->sb))
2508 return -EROFS;
2509
2510 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2511 sizeof(range)))
2512 return -EFAULT;
2513
2514 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2515 sbi->segs_per_sec != 1) {
2516 f2fs_msg(sbi->sb, KERN_WARNING,
2517 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2518 range.dev_num, sbi->s_ndevs,
2519 sbi->segs_per_sec);
2520 return -EINVAL;
2521 }
2522
2523 ret = mnt_want_write_file(filp);
2524 if (ret)
2525 return ret;
2526
2527 if (range.dev_num != 0)
2528 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2529 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2530
2531 start_segno = sm->last_victim[FLUSH_DEVICE];
2532 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2533 start_segno = dev_start_segno;
2534 end_segno = min(start_segno + range.segments, dev_end_segno);
2535
2536 while (start_segno < end_segno) {
2537 if (!mutex_trylock(&sbi->gc_mutex)) {
2538 ret = -EBUSY;
2539 goto out;
2540 }
2541 sm->last_victim[GC_CB] = end_segno + 1;
2542 sm->last_victim[GC_GREEDY] = end_segno + 1;
2543 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2544 ret = f2fs_gc(sbi, true, true, start_segno);
2545 if (ret == -EAGAIN)
2546 ret = 0;
2547 else if (ret < 0)
2548 break;
2549 start_segno++;
2550 }
2551out:
2552 mnt_drop_write_file(filp);
2553 return ret;
2554}
2555
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002556static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2557{
2558 struct inode *inode = file_inode(filp);
2559 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2560
2561 /* Must validate to set it with SQLite behavior in Android. */
2562 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2563
2564 return put_user(sb_feature, (u32 __user *)arg);
2565}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002566
Chao Yu2c1d0302017-07-29 00:32:52 +08002567#ifdef CONFIG_QUOTA
2568static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2569{
2570 struct inode *inode = file_inode(filp);
2571 struct f2fs_inode_info *fi = F2FS_I(inode);
2572 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2573 struct super_block *sb = sbi->sb;
2574 struct dquot *transfer_to[MAXQUOTAS] = {};
2575 struct page *ipage;
2576 kprojid_t kprojid;
2577 int err;
2578
2579 if (!f2fs_sb_has_project_quota(sb)) {
2580 if (projid != F2FS_DEF_PROJID)
2581 return -EOPNOTSUPP;
2582 else
2583 return 0;
2584 }
2585
2586 if (!f2fs_has_extra_attr(inode))
2587 return -EOPNOTSUPP;
2588
2589 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2590
2591 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2592 return 0;
2593
2594 err = mnt_want_write_file(filp);
2595 if (err)
2596 return err;
2597
2598 err = -EPERM;
2599 inode_lock(inode);
2600
2601 /* Is it quota file? Do not allow user to mess with it */
2602 if (IS_NOQUOTA(inode))
2603 goto out_unlock;
2604
Chao Yu4d57b862018-05-30 00:20:41 +08002605 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Chao Yu2c1d0302017-07-29 00:32:52 +08002606 if (IS_ERR(ipage)) {
2607 err = PTR_ERR(ipage);
2608 goto out_unlock;
2609 }
2610
2611 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2612 i_projid)) {
2613 err = -EOVERFLOW;
2614 f2fs_put_page(ipage, 1);
2615 goto out_unlock;
2616 }
2617 f2fs_put_page(ipage, 1);
2618
Chao Yuc22aecd2018-04-21 17:53:52 +08002619 err = dquot_initialize(inode);
2620 if (err)
2621 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08002622
2623 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2624 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2625 err = __dquot_transfer(inode, transfer_to);
2626 dqput(transfer_to[PRJQUOTA]);
2627 if (err)
2628 goto out_dirty;
2629 }
2630
2631 F2FS_I(inode)->i_projid = kprojid;
2632 inode->i_ctime = current_time(inode);
2633out_dirty:
2634 f2fs_mark_inode_dirty_sync(inode, true);
2635out_unlock:
2636 inode_unlock(inode);
2637 mnt_drop_write_file(filp);
2638 return err;
2639}
2640#else
2641static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2642{
2643 if (projid != F2FS_DEF_PROJID)
2644 return -EOPNOTSUPP;
2645 return 0;
2646}
2647#endif
2648
2649/* Transfer internal flags to xflags */
2650static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2651{
2652 __u32 xflags = 0;
2653
Chao Yu59c84402018-04-03 15:08:17 +08002654 if (iflags & F2FS_SYNC_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002655 xflags |= FS_XFLAG_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +08002656 if (iflags & F2FS_IMMUTABLE_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002657 xflags |= FS_XFLAG_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +08002658 if (iflags & F2FS_APPEND_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002659 xflags |= FS_XFLAG_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +08002660 if (iflags & F2FS_NODUMP_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002661 xflags |= FS_XFLAG_NODUMP;
Chao Yu59c84402018-04-03 15:08:17 +08002662 if (iflags & F2FS_NOATIME_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002663 xflags |= FS_XFLAG_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +08002664 if (iflags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002665 xflags |= FS_XFLAG_PROJINHERIT;
2666 return xflags;
2667}
2668
2669#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2670 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2671 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2672
Chao Yu2c1d0302017-07-29 00:32:52 +08002673/* Transfer xflags flags to internal */
2674static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2675{
2676 unsigned long iflags = 0;
2677
2678 if (xflags & FS_XFLAG_SYNC)
Chao Yu59c84402018-04-03 15:08:17 +08002679 iflags |= F2FS_SYNC_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002680 if (xflags & FS_XFLAG_IMMUTABLE)
Chao Yu59c84402018-04-03 15:08:17 +08002681 iflags |= F2FS_IMMUTABLE_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002682 if (xflags & FS_XFLAG_APPEND)
Chao Yu59c84402018-04-03 15:08:17 +08002683 iflags |= F2FS_APPEND_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002684 if (xflags & FS_XFLAG_NODUMP)
Chao Yu59c84402018-04-03 15:08:17 +08002685 iflags |= F2FS_NODUMP_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002686 if (xflags & FS_XFLAG_NOATIME)
Chao Yu59c84402018-04-03 15:08:17 +08002687 iflags |= F2FS_NOATIME_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002688 if (xflags & FS_XFLAG_PROJINHERIT)
Chao Yu59c84402018-04-03 15:08:17 +08002689 iflags |= F2FS_PROJINHERIT_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002690
2691 return iflags;
2692}
2693
2694static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2695{
2696 struct inode *inode = file_inode(filp);
2697 struct f2fs_inode_info *fi = F2FS_I(inode);
2698 struct fsxattr fa;
2699
2700 memset(&fa, 0, sizeof(struct fsxattr));
2701 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
Chao Yuc807a7c2018-04-08 11:25:53 +08002702 F2FS_FL_USER_VISIBLE);
Chao Yu2c1d0302017-07-29 00:32:52 +08002703
2704 if (f2fs_sb_has_project_quota(inode->i_sb))
2705 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2706 fi->i_projid);
2707
2708 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2709 return -EFAULT;
2710 return 0;
2711}
2712
2713static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2714{
2715 struct inode *inode = file_inode(filp);
2716 struct f2fs_inode_info *fi = F2FS_I(inode);
2717 struct fsxattr fa;
2718 unsigned int flags;
2719 int err;
2720
2721 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2722 return -EFAULT;
2723
2724 /* Make sure caller has proper permission */
2725 if (!inode_owner_or_capable(inode))
2726 return -EACCES;
2727
2728 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2729 return -EOPNOTSUPP;
2730
2731 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2732 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2733 return -EOPNOTSUPP;
2734
2735 err = mnt_want_write_file(filp);
2736 if (err)
2737 return err;
2738
2739 inode_lock(inode);
2740 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2741 (flags & F2FS_FL_XFLAG_VISIBLE);
2742 err = __f2fs_ioc_setflags(inode, flags);
2743 inode_unlock(inode);
2744 mnt_drop_write_file(filp);
2745 if (err)
2746 return err;
2747
2748 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2749 if (err)
2750 return err;
2751
2752 return 0;
2753}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002754
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002755int f2fs_pin_file_control(struct inode *inode, bool inc)
2756{
2757 struct f2fs_inode_info *fi = F2FS_I(inode);
2758 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2759
2760 /* Use i_gc_failures for normal file as a risk signal. */
2761 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08002762 f2fs_i_gc_failures_write(inode,
2763 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002764
Chao Yu2ef79ec2018-05-07 20:28:54 +08002765 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002766 f2fs_msg(sbi->sb, KERN_WARNING,
2767 "%s: Enable GC = ino %lx after %x GC trials\n",
Chao Yu2ef79ec2018-05-07 20:28:54 +08002768 __func__, inode->i_ino,
2769 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002770 clear_inode_flag(inode, FI_PIN_FILE);
2771 return -EAGAIN;
2772 }
2773 return 0;
2774}
2775
2776static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2777{
2778 struct inode *inode = file_inode(filp);
2779 __u32 pin;
2780 int ret = 0;
2781
2782 if (!inode_owner_or_capable(inode))
2783 return -EACCES;
2784
2785 if (get_user(pin, (__u32 __user *)arg))
2786 return -EFAULT;
2787
2788 if (!S_ISREG(inode->i_mode))
2789 return -EINVAL;
2790
2791 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2792 return -EROFS;
2793
2794 ret = mnt_want_write_file(filp);
2795 if (ret)
2796 return ret;
2797
2798 inode_lock(inode);
2799
Chao Yu4d57b862018-05-30 00:20:41 +08002800 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002801 ret = -EINVAL;
2802 goto out;
2803 }
2804
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002805 if (!pin) {
2806 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002807 F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = 1;
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002808 goto done;
2809 }
2810
2811 if (f2fs_pin_file_control(inode, false)) {
2812 ret = -EAGAIN;
2813 goto out;
2814 }
2815 ret = f2fs_convert_inline_inode(inode);
2816 if (ret)
2817 goto out;
2818
2819 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002820 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002821done:
2822 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2823out:
2824 inode_unlock(inode);
2825 mnt_drop_write_file(filp);
2826 return ret;
2827}
2828
2829static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2830{
2831 struct inode *inode = file_inode(filp);
2832 __u32 pin = 0;
2833
2834 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08002835 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002836 return put_user(pin, (u32 __user *)arg);
2837}
2838
Chao Yuc4020b22018-01-11 14:42:30 +08002839int f2fs_precache_extents(struct inode *inode)
2840{
2841 struct f2fs_inode_info *fi = F2FS_I(inode);
2842 struct f2fs_map_blocks map;
2843 pgoff_t m_next_extent;
2844 loff_t end;
2845 int err;
2846
2847 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2848 return -EOPNOTSUPP;
2849
2850 map.m_lblk = 0;
2851 map.m_next_pgofs = NULL;
2852 map.m_next_extent = &m_next_extent;
2853 map.m_seg_type = NO_CHECK_TYPE;
2854 end = F2FS_I_SB(inode)->max_file_blocks;
2855
2856 while (map.m_lblk < end) {
2857 map.m_len = end - map.m_lblk;
2858
Chao Yub2532c62018-04-24 10:55:28 +08002859 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002860 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08002861 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002862 if (err)
2863 return err;
2864
2865 map.m_lblk = m_next_extent;
2866 }
2867
2868 return err;
2869}
2870
2871static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2872{
2873 return f2fs_precache_extents(file_inode(filp));
2874}
2875
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002876long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2877{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002878 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2879 return -EIO;
2880
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002881 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002882 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002883 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002884 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002885 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002886 case F2FS_IOC_GETVERSION:
2887 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002888 case F2FS_IOC_START_ATOMIC_WRITE:
2889 return f2fs_ioc_start_atomic_write(filp);
2890 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2891 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002892 case F2FS_IOC_START_VOLATILE_WRITE:
2893 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002894 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2895 return f2fs_ioc_release_volatile_write(filp);
2896 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2897 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002898 case F2FS_IOC_SHUTDOWN:
2899 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002900 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002901 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002902 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2903 return f2fs_ioc_set_encryption_policy(filp, arg);
2904 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2905 return f2fs_ioc_get_encryption_policy(filp, arg);
2906 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2907 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002908 case F2FS_IOC_GARBAGE_COLLECT:
2909 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002910 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2911 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002912 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08002913 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002914 case F2FS_IOC_DEFRAGMENT:
2915 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002916 case F2FS_IOC_MOVE_RANGE:
2917 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002918 case F2FS_IOC_FLUSH_DEVICE:
2919 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002920 case F2FS_IOC_GET_FEATURES:
2921 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08002922 case F2FS_IOC_FSGETXATTR:
2923 return f2fs_ioc_fsgetxattr(filp, arg);
2924 case F2FS_IOC_FSSETXATTR:
2925 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002926 case F2FS_IOC_GET_PIN_FILE:
2927 return f2fs_ioc_get_pin_file(filp, arg);
2928 case F2FS_IOC_SET_PIN_FILE:
2929 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08002930 case F2FS_IOC_PRECACHE_EXTENTS:
2931 return f2fs_ioc_precache_extents(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002932 default:
2933 return -ENOTTY;
2934 }
2935}
2936
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002937static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2938{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002939 struct file *file = iocb->ki_filp;
2940 struct inode *inode = file_inode(file);
2941 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002942
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002943 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2944 return -EIO;
2945
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002946 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2947 return -EINVAL;
2948
2949 if (!inode_trylock(inode)) {
2950 if (iocb->ki_flags & IOCB_NOWAIT)
2951 return -EAGAIN;
2952 inode_lock(inode);
2953 }
2954
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002955 ret = generic_write_checks(iocb, from);
2956 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07002957 bool preallocated = false;
2958 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002959 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002960
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002961 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2962 set_inode_flag(inode, FI_NO_PREALLOC);
2963
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002964 if ((iocb->ki_flags & IOCB_NOWAIT) &&
2965 (iocb->ki_flags & IOCB_DIRECT)) {
2966 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
2967 iov_iter_count(from)) ||
2968 f2fs_has_inline_data(inode) ||
2969 f2fs_force_buffered_io(inode, WRITE)) {
Yunlei Hecba41be2018-04-24 11:40:19 +08002970 clear_inode_flag(inode,
2971 FI_NO_PREALLOC);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002972 inode_unlock(inode);
2973 return -EAGAIN;
2974 }
2975
2976 } else {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07002977 preallocated = true;
2978 target_size = iocb->ki_pos + iov_iter_count(from);
2979
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002980 err = f2fs_preallocate_blocks(iocb, from);
2981 if (err) {
2982 clear_inode_flag(inode, FI_NO_PREALLOC);
2983 inode_unlock(inode);
2984 return err;
2985 }
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002986 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002987 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002988 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08002989
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07002990 /* if we couldn't write data, we should deallocate blocks. */
2991 if (preallocated && i_size_read(inode) < target_size)
2992 f2fs_truncate(inode);
2993
Chao Yub0af6d42017-08-02 23:21:48 +08002994 if (ret > 0)
2995 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002996 }
2997 inode_unlock(inode);
2998
Christoph Hellwige2592212016-04-07 08:52:01 -07002999 if (ret > 0)
3000 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003001 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003002}
3003
Namjae Jeone9750822013-02-04 23:41:41 +09003004#ifdef CONFIG_COMPAT
3005long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3006{
3007 switch (cmd) {
3008 case F2FS_IOC32_GETFLAGS:
3009 cmd = F2FS_IOC_GETFLAGS;
3010 break;
3011 case F2FS_IOC32_SETFLAGS:
3012 cmd = F2FS_IOC_SETFLAGS;
3013 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003014 case F2FS_IOC32_GETVERSION:
3015 cmd = F2FS_IOC_GETVERSION;
3016 break;
3017 case F2FS_IOC_START_ATOMIC_WRITE:
3018 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3019 case F2FS_IOC_START_VOLATILE_WRITE:
3020 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3021 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3022 case F2FS_IOC_SHUTDOWN:
3023 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3024 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3025 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3026 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003027 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003028 case F2FS_IOC_WRITE_CHECKPOINT:
3029 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003030 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003031 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003032 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003033 case F2FS_IOC_FSGETXATTR:
3034 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003035 case F2FS_IOC_GET_PIN_FILE:
3036 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003037 case F2FS_IOC_PRECACHE_EXTENTS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003038 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003039 default:
3040 return -ENOIOCTLCMD;
3041 }
3042 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3043}
3044#endif
3045
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003046const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003047 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04003048 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003049 .write_iter = f2fs_file_write_iter,
3050 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003051 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003052 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003053 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003054 .fsync = f2fs_sync_file,
3055 .fallocate = f2fs_fallocate,
3056 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003057#ifdef CONFIG_COMPAT
3058 .compat_ioctl = f2fs_compat_ioctl,
3059#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003060 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003061 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003062};