blob: 61f95731c8586cfd87abbcbf2da7c30c1b7e22c5 [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 */
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -0700115 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +0800116
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800117out_sem:
118 up_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900119out:
120 sb_end_pagefault(inode->i_sb);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800121 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200122err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900123 return block_page_mkwrite_return(err);
124}
125
126static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800127 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700128 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900129 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900130};
131
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900132static int get_parent_ino(struct inode *inode, nid_t *pino)
133{
134 struct dentry *dentry;
135
136 inode = igrab(inode);
137 dentry = d_find_any_alias(inode);
138 iput(inode);
139 if (!dentry)
140 return 0;
141
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900142 *pino = parent_ino(dentry);
143 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900144 return 1;
145}
146
Chao Yua5fd5052017-11-06 22:51:45 +0800147static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800148{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700149 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800150 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800151
Chao Yua5fd5052017-11-06 22:51:45 +0800152 if (!S_ISREG(inode->i_mode))
153 cp_reason = CP_NON_REGULAR;
154 else if (inode->i_nlink != 1)
155 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700156 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800157 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800158 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800159 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800160 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800161 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800162 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800163 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700164 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800165 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800166 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800167 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800168 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800169 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
170 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
171 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800172 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800173
Chao Yua5fd5052017-11-06 22:51:45 +0800174 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800175}
176
Changman Lee9c7bb702014-12-08 15:29:40 +0900177static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
178{
179 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
180 bool ret = false;
181 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800182 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900183 ret = true;
184 f2fs_put_page(i, 0);
185 return ret;
186}
187
Changman Lee51455b12014-12-08 15:29:41 +0900188static void try_to_fix_pino(struct inode *inode)
189{
190 struct f2fs_inode_info *fi = F2FS_I(inode);
191 nid_t pino;
192
193 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900194 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
195 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700196 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900197 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900198 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700199 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900200}
201
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700202static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
203 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900204{
205 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700206 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700207 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900208 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800209 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900210 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900211 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900212 .nr_to_write = LONG_MAX,
213 .for_reclaim = 0,
214 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800215 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900216
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 Yu50fa53e2018-08-02 23:03:19 +0800278 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800279 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700280 if (ret)
281 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900282
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800283 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800284 if (unlikely(f2fs_cp_error(sbi))) {
285 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800286 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800287 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800288
Chao Yu4d57b862018-05-30 00:20:41 +0800289 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700290 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900291 f2fs_write_inode(inode, NULL);
292 goto sync_nodes;
293 }
294
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700295 /*
296 * If it's atomic_write, it's just fine to keep write ordering. So
297 * here we don't need to wait for node write completion, since we use
298 * node chain which serializes node blocks. If one of node writes are
299 * reordered, we can see simply broken chain, resulting in stopping
300 * roll-forward recovery. It means we'll recover all or none node blocks
301 * given fsync mark.
302 */
303 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800304 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700305 if (ret)
306 goto out;
307 }
Changman Lee51455b12014-12-08 15:29:41 +0900308
309 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800310 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700311 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900312flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700313 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800314 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800315 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800316 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800317 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800318 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800319 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800320 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900321out:
Chao Yua5fd5052017-11-06 22:51:45 +0800322 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700323 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900324 return ret;
325}
326
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700327int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
328{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200329 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
330 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700331 return f2fs_do_sync_file(file, start, end, datasync, false);
332}
333
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900334static pgoff_t __get_first_dirty_index(struct address_space *mapping,
335 pgoff_t pgofs, int whence)
336{
Jan Kara8faab642017-11-15 17:34:55 -0800337 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900338 int nr_pages;
339
340 if (whence != SEEK_DATA)
341 return 0;
342
343 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800344 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
345 1, &page);
346 if (!nr_pages)
347 return ULONG_MAX;
348 pgofs = page->index;
349 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900350 return pgofs;
351}
352
Chao Yue1da7872018-06-05 17:44:11 +0800353static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
354 pgoff_t dirty, pgoff_t pgofs, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900355{
356 switch (whence) {
357 case SEEK_DATA:
358 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yue1da7872018-06-05 17:44:11 +0800359 is_valid_data_blkaddr(sbi, blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900360 return true;
361 break;
362 case SEEK_HOLE:
363 if (blkaddr == NULL_ADDR)
364 return true;
365 break;
366 }
367 return false;
368}
369
Chao Yu267378d2014-04-23 14:10:24 +0800370static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
371{
372 struct inode *inode = file->f_mapping->host;
373 loff_t maxbytes = inode->i_sb->s_maxbytes;
374 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900375 pgoff_t pgofs, end_offset, dirty;
376 loff_t data_ofs = offset;
377 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800378 int err = 0;
379
Al Viro59551022016-01-22 15:40:57 -0500380 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800381
382 isize = i_size_read(inode);
383 if (offset >= isize)
384 goto fail;
385
386 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800387 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800388 if (whence == SEEK_HOLE)
389 data_ofs = isize;
390 goto found;
391 }
392
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300393 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800394
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900395 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
396
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300397 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800398 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800399 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800400 if (err && err != -ENOENT) {
401 goto fail;
402 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900403 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800404 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800405 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800406 continue;
407 } else {
408 goto found;
409 }
410 }
411
Chao Yu81ca7352016-01-26 15:39:35 +0800412 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800413
414 /* find data/hole in dnode block */
415 for (; dn.ofs_in_node < end_offset;
416 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300417 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800418 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900419
Chao Yu7a2af762017-07-19 00:19:06 +0800420 blkaddr = datablock_addr(dn.inode,
421 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800422
Chao Yuc9b60782018-08-01 19:13:44 +0800423 if (__is_valid_data_blkaddr(blkaddr) &&
424 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
425 blkaddr, DATA_GENERIC)) {
426 f2fs_put_dnode(&dn);
427 goto fail;
428 }
429
Chao Yue1da7872018-06-05 17:44:11 +0800430 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
431 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800432 f2fs_put_dnode(&dn);
433 goto found;
434 }
435 }
436 f2fs_put_dnode(&dn);
437 }
438
439 if (whence == SEEK_DATA)
440 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800441found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900442 if (whence == SEEK_HOLE && data_ofs > isize)
443 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500444 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800445 return vfs_setpos(file, data_ofs, maxbytes);
446fail:
Al Viro59551022016-01-22 15:40:57 -0500447 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800448 return -ENXIO;
449}
450
451static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
452{
453 struct inode *inode = file->f_mapping->host;
454 loff_t maxbytes = inode->i_sb->s_maxbytes;
455
456 switch (whence) {
457 case SEEK_SET:
458 case SEEK_CUR:
459 case SEEK_END:
460 return generic_file_llseek_size(file, offset, whence,
461 maxbytes, i_size_read(inode));
462 case SEEK_DATA:
463 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700464 if (offset < 0)
465 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800466 return f2fs_seek_block(file, offset, whence);
467 }
468
469 return -EINVAL;
470}
471
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900472static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
473{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700474 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800475 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700476
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200477 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
478 return -EIO;
479
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700480 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800481 err = f2fs_convert_inline_inode(inode);
482 if (err)
483 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700484
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900485 file_accessed(file);
486 vma->vm_ops = &f2fs_file_vm_ops;
487 return 0;
488}
489
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700490static int f2fs_file_open(struct inode *inode, struct file *filp)
491{
Eric Biggers2e168c82017-11-29 12:35:28 -0800492 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700493
Eric Biggers2e168c82017-11-29 12:35:28 -0800494 if (err)
495 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900496
497 filp->f_mode |= FMODE_NOWAIT;
498
Chao Yu0abd6752017-07-09 00:13:07 +0800499 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700500}
501
Chao Yu4d57b862018-05-30 00:20:41 +0800502void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900503{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700504 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900505 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800506 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900507 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800508 int base = 0;
509
510 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
511 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900512
Gu Zheng45590712013-07-15 17:57:38 +0800513 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800514 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900515
Chris Fries6c311ec2014-01-17 14:44:39 -0600516 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900517 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900518
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900519 if (blkaddr == NULL_ADDR)
520 continue;
521
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800522 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800523 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800524
525 if (__is_valid_data_blkaddr(blkaddr) &&
526 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
527 continue;
528
Chao Yu4d57b862018-05-30 00:20:41 +0800529 f2fs_invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700530 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700531 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 nr_free++;
533 }
Chao Yu19b2c302015-08-26 20:34:48 +0800534
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900535 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800536 pgoff_t fofs;
537 /*
538 * once we invalidate valid blkaddr in range [ofs, ofs + count],
539 * we will invalidate all blkaddr in the whole range.
540 */
Chao Yu4d57b862018-05-30 00:20:41 +0800541 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800542 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800543 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900544 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545 }
546 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900547
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800548 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900549 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
550 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900551}
552
Chao Yu4d57b862018-05-30 00:20:41 +0800553void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900554{
Chao Yu4d57b862018-05-30 00:20:41 +0800555 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900556}
557
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800558static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700559 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900560{
youngjun yoo193bea12018-05-30 04:21:14 +0900561 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300562 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700563 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900564 struct page *page;
565
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700566 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700567 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900568
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700569 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700570 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700571 if (page && PageUptodate(page))
572 goto truncate_out;
573 f2fs_put_page(page, 1);
574 return 0;
575 }
576
Chao Yu4d57b862018-05-30 00:20:41 +0800577 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900578 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800579 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700580truncate_out:
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 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
671 f2fs_show_injection_info(FAULT_TRUNCATE);
672 return -EIO;
673 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200674
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800675 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800676 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800677 err = f2fs_convert_inline_inode(inode);
678 if (err)
679 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800680 }
681
Chao Yu4d57b862018-05-30 00:20:41 +0800682 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800683 if (err)
684 return err;
685
Deepa Dinamani078cd822016-09-14 07:48:04 -0700686 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700687 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800688 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900689}
690
David Howellsa528d352017-01-31 16:46:22 +0000691int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800692 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900693{
David Howellsa528d352017-01-31 16:46:22 +0000694 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800695 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800696 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800697 unsigned int flags;
698
Chao Yu1c1d35d2018-01-25 14:54:42 +0800699 if (f2fs_has_extra_attr(inode) &&
700 f2fs_sb_has_inode_crtime(inode->i_sb) &&
701 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
702 stat->result_mask |= STATX_BTIME;
703 stat->btime.tv_sec = fi->i_crtime.tv_sec;
704 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
705 }
706
Chao Yuc807a7c2018-04-08 11:25:53 +0800707 flags = fi->i_flags & F2FS_FL_USER_VISIBLE;
Chao Yu59c84402018-04-03 15:08:17 +0800708 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800709 stat->attributes |= STATX_ATTR_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +0800710 if (flags & F2FS_COMPR_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800711 stat->attributes |= STATX_ATTR_COMPRESSED;
712 if (f2fs_encrypted_inode(inode))
713 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800714 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800715 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800716 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800717 stat->attributes |= STATX_ATTR_NODUMP;
718
719 stat->attributes_mask |= (STATX_ATTR_APPEND |
720 STATX_ATTR_COMPRESSED |
721 STATX_ATTR_ENCRYPTED |
722 STATX_ATTR_IMMUTABLE |
723 STATX_ATTR_NODUMP);
724
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900725 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700726
727 /* we need to show initial sectors used for inline_data/dentries */
728 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
729 f2fs_has_inline_dentry(inode))
730 stat->blocks += (stat->size + 511) >> 9;
731
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900732 return 0;
733}
734
735#ifdef CONFIG_F2FS_FS_POSIX_ACL
736static void __setattr_copy(struct inode *inode, const struct iattr *attr)
737{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900738 unsigned int ia_valid = attr->ia_valid;
739
740 if (ia_valid & ATTR_UID)
741 inode->i_uid = attr->ia_uid;
742 if (ia_valid & ATTR_GID)
743 inode->i_gid = attr->ia_gid;
744 if (ia_valid & ATTR_ATIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700745 inode->i_atime = timespec64_trunc(attr->ia_atime,
746 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900747 if (ia_valid & ATTR_MTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700748 inode->i_mtime = timespec64_trunc(attr->ia_mtime,
749 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900750 if (ia_valid & ATTR_CTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700751 inode->i_ctime = timespec64_trunc(attr->ia_ctime,
752 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900753 if (ia_valid & ATTR_MODE) {
754 umode_t mode = attr->ia_mode;
755
756 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
757 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700758 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900759 }
760}
761#else
762#define __setattr_copy setattr_copy
763#endif
764
765int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
766{
David Howells2b0143b2015-03-17 22:25:59 +0000767 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900768 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800769 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900770
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200771 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
772 return -EIO;
773
Jan Kara31051c82016-05-26 16:55:18 +0200774 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900775 if (err)
776 return err;
777
Eric Biggers20bb2472017-11-29 12:35:32 -0800778 err = fscrypt_prepare_setattr(dentry, attr);
779 if (err)
780 return err;
781
Chao Yu0abd6752017-07-09 00:13:07 +0800782 if (is_quota_modification(inode, attr)) {
783 err = dquot_initialize(inode);
784 if (err)
785 return err;
786 }
787 if ((attr->ia_valid & ATTR_UID &&
788 !uid_eq(attr->ia_uid, inode->i_uid)) ||
789 (attr->ia_valid & ATTR_GID &&
790 !gid_eq(attr->ia_gid, inode->i_gid))) {
791 err = dquot_transfer(inode, attr);
792 if (err)
793 return err;
794 }
795
Chao Yu09db6a22014-09-15 18:02:09 +0800796 if (attr->ia_valid & ATTR_SIZE) {
Chao Yua33c1502018-08-05 23:04:25 +0800797 bool to_smaller = (attr->ia_size <= i_size_read(inode));
Chao Yu0cab80e2015-12-01 11:36:16 +0800798
Chao Yua33c1502018-08-05 23:04:25 +0800799 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900800 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800801
802 truncate_setsize(inode, attr->ia_size);
803
804 if (to_smaller)
805 err = f2fs_truncate(inode);
806 /*
807 * do not trim all blocks after i_size if target size is
808 * larger than i_size.
809 */
Chao Yua33c1502018-08-05 23:04:25 +0800810 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900811 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800812
813 if (err)
814 return err;
815
816 if (!to_smaller) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800817 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800818 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800819 err = f2fs_convert_inline_inode(inode);
820 if (err)
821 return err;
822 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700823 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800824 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800825
Chao Yua0d00fa2017-10-09 17:55:19 +0800826 down_write(&F2FS_I(inode)->i_sem);
827 F2FS_I(inode)->last_disk_size = i_size_read(inode);
828 up_write(&F2FS_I(inode)->i_sem);
829
Yunlei Hec0ed4402016-12-11 15:35:15 +0800830 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900831 }
832
833 __setattr_copy(inode, attr);
834
835 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800836 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700837 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
838 inode->i_mode = F2FS_I(inode)->i_acl_mode;
839 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900840 }
841 }
842
Yunlei Hec0ed4402016-12-11 15:35:15 +0800843 /* file size may changed here */
844 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700845
846 /* inode change will produce dirty node pages flushed by checkpoint */
847 f2fs_balance_fs(F2FS_I_SB(inode), true);
848
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900849 return err;
850}
851
852const struct inode_operations f2fs_file_inode_operations = {
853 .getattr = f2fs_getattr,
854 .setattr = f2fs_setattr,
855 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800856 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900857#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900858 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900859#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900860 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900861};
862
Chao Yu63943282015-08-07 18:36:06 +0800863static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900864 loff_t start, loff_t len)
865{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700866 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900867 struct page *page;
868
869 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800870 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900871
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800872 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900873
Gu Zhenge4795562013-09-27 18:08:30 +0800874 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800875 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800876 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900877
Chao Yu63943282015-08-07 18:36:06 +0800878 if (IS_ERR(page))
879 return PTR_ERR(page);
880
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800881 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800882 zero_user(page, start, len);
883 set_page_dirty(page);
884 f2fs_put_page(page, 1);
885 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900886}
887
Chao Yu4d57b862018-05-30 00:20:41 +0800888int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900889{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900890 int err;
891
Chao Yuea587112015-09-17 20:22:44 +0800892 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900893 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800894 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900895
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900896 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800897 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900898 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800899 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +0800900 pg_start = f2fs_get_next_page_offset(&dn,
901 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900902 continue;
Chao Yuea587112015-09-17 20:22:44 +0800903 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900904 return err;
905 }
906
Chao Yu81ca7352016-01-26 15:39:35 +0800907 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800908 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
909
910 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
911
Chao Yu4d57b862018-05-30 00:20:41 +0800912 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900913 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800914
915 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900916 }
917 return 0;
918}
919
Chao Yua66c7b22013-11-22 16:52:50 +0800920static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900921{
922 pgoff_t pg_start, pg_end;
923 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800924 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900925
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800926 ret = f2fs_convert_inline_inode(inode);
927 if (ret)
928 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800929
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300930 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
931 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900932
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300933 off_start = offset & (PAGE_SIZE - 1);
934 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900935
936 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800937 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900938 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800939 if (ret)
940 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900941 } else {
Chao Yu63943282015-08-07 18:36:06 +0800942 if (off_start) {
943 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300944 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800945 if (ret)
946 return ret;
947 }
948 if (off_end) {
949 ret = fill_zero(inode, pg_end, 0, off_end);
950 if (ret)
951 return ret;
952 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900953
954 if (pg_start < pg_end) {
955 struct address_space *mapping = inode->i_mapping;
956 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700957 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500958
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800959 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900960
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300961 blk_start = (loff_t)pg_start << PAGE_SHIFT;
962 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +0800963
Chao Yua33c1502018-08-05 23:04:25 +0800964 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900965 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800966
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900967 truncate_inode_pages_range(mapping, blk_start,
968 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900969
Gu Zhenge4795562013-09-27 18:08:30 +0800970 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800971 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800972 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +0800973
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800974 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900975 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900976 }
977 }
978
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900979 return ret;
980}
981
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700982static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
983 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800984{
985 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
986 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700987 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700988
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700989next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700990 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800991 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700992 if (ret && ret != -ENOENT) {
993 return ret;
994 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700995 if (dn.max_level == 0)
996 return -ENOENT;
997 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
998 blkaddr += done;
999 do_replace += done;
1000 goto next;
1001 }
1002
1003 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1004 dn.ofs_in_node, len);
1005 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001006 *blkaddr = datablock_addr(dn.inode,
1007 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001008 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001009
1010 if (test_opt(sbi, LFS)) {
1011 f2fs_put_dnode(&dn);
1012 return -ENOTSUPP;
1013 }
1014
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001015 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001016 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001017 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001018 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001019 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001020 f2fs_put_dnode(&dn);
1021next:
1022 len -= done;
1023 off += done;
1024 if (len)
1025 goto next_dnode;
1026 return 0;
1027}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001028
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001029static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1030 int *do_replace, pgoff_t off, int len)
1031{
1032 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1033 struct dnode_of_data dn;
1034 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001035
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001036 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1037 if (*do_replace == 0)
1038 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001039
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001040 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001041 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001042 if (ret) {
1043 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001044 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001045 } else {
1046 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001047 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001048 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001049 }
1050 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001051}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001052
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001053static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1054 block_t *blkaddr, int *do_replace,
1055 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1056{
1057 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1058 pgoff_t i = 0;
1059 int ret;
1060
1061 while (i < len) {
1062 if (blkaddr[i] == NULL_ADDR && !full) {
1063 i++;
1064 continue;
1065 }
1066
1067 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1068 struct dnode_of_data dn;
1069 struct node_info ni;
1070 size_t new_size;
1071 pgoff_t ilen;
1072
1073 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001074 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001075 if (ret)
1076 return ret;
1077
Chao Yu77357302018-07-17 00:02:17 +08001078 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1079 if (ret) {
1080 f2fs_put_dnode(&dn);
1081 return ret;
1082 }
1083
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001084 ilen = min((pgoff_t)
1085 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1086 dn.ofs_in_node, len - i);
1087 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001088 dn.data_blkaddr = datablock_addr(dn.inode,
1089 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001090 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001091
1092 if (do_replace[i]) {
1093 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001094 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001095 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001096 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001097 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1098 blkaddr[i], ni.version, true, false);
1099
1100 do_replace[i] = 0;
1101 }
1102 dn.ofs_in_node++;
1103 i++;
1104 new_size = (dst + i) << PAGE_SHIFT;
1105 if (dst_inode->i_size < new_size)
1106 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001107 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001108
1109 f2fs_put_dnode(&dn);
1110 } else {
1111 struct page *psrc, *pdst;
1112
Chao Yu4d57b862018-05-30 00:20:41 +08001113 psrc = f2fs_get_lock_data_page(src_inode,
1114 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001115 if (IS_ERR(psrc))
1116 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001117 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001118 true);
1119 if (IS_ERR(pdst)) {
1120 f2fs_put_page(psrc, 1);
1121 return PTR_ERR(pdst);
1122 }
1123 f2fs_copy_page(psrc, pdst);
1124 set_page_dirty(pdst);
1125 f2fs_put_page(pdst, 1);
1126 f2fs_put_page(psrc, 1);
1127
Chao Yu4d57b862018-05-30 00:20:41 +08001128 ret = f2fs_truncate_hole(src_inode,
1129 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001130 if (ret)
1131 return ret;
1132 i++;
1133 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001134 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001135 return 0;
1136}
1137
1138static int __exchange_data_block(struct inode *src_inode,
1139 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001140 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001141{
1142 block_t *src_blkaddr;
1143 int *do_replace;
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001144 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001145 int ret;
1146
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001147 while (len) {
1148 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001149
Chao Yu628b3d12017-11-30 19:28:18 +08001150 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001151 array_size(olen, sizeof(block_t)),
1152 GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001153 if (!src_blkaddr)
1154 return -ENOMEM;
1155
Chao Yu628b3d12017-11-30 19:28:18 +08001156 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001157 array_size(olen, sizeof(int)),
1158 GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001159 if (!do_replace) {
1160 kvfree(src_blkaddr);
1161 return -ENOMEM;
1162 }
1163
1164 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1165 do_replace, src, olen);
1166 if (ret)
1167 goto roll_back;
1168
1169 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1170 do_replace, src, dst, olen, full);
1171 if (ret)
1172 goto roll_back;
1173
1174 src += olen;
1175 dst += olen;
1176 len -= olen;
1177
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001178 kvfree(src_blkaddr);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001179 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001180 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001181 return 0;
1182
1183roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001184 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001185 kvfree(src_blkaddr);
1186 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001187 return ret;
1188}
1189
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001190static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001191{
1192 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001193 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001194 pgoff_t start = offset >> PAGE_SHIFT;
1195 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001196 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001197
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001198 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001199
1200 /* avoid gc operation during block exchange */
1201 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1202 down_write(&F2FS_I(inode)->i_mmap_sem);
1203
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001204 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001205 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001206 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001207 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1208 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001209
1210 up_write(&F2FS_I(inode)->i_mmap_sem);
1211 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001212 return ret;
1213}
1214
1215static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1216{
Chao Yub4ace332015-05-06 13:09:46 +08001217 loff_t new_size;
1218 int ret;
1219
Chao Yub4ace332015-05-06 13:09:46 +08001220 if (offset + len >= i_size_read(inode))
1221 return -EINVAL;
1222
1223 /* collapse range should be aligned to block size of f2fs. */
1224 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1225 return -EINVAL;
1226
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001227 ret = f2fs_convert_inline_inode(inode);
1228 if (ret)
1229 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001230
Chao Yub4ace332015-05-06 13:09:46 +08001231 /* write out all dirty pages from offset */
1232 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1233 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001234 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001235
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001236 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001237 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001238 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001239
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001240 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001241 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001242 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1243 truncate_pagecache(inode, offset);
1244
Chao Yub4ace332015-05-06 13:09:46 +08001245 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001246 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001247
Chao Yu4d57b862018-05-30 00:20:41 +08001248 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001249 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001250 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001251 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001252 return ret;
1253}
1254
Chao Yu6e961942016-05-09 19:56:31 +08001255static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1256 pgoff_t end)
1257{
1258 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1259 pgoff_t index = start;
1260 unsigned int ofs_in_node = dn->ofs_in_node;
1261 blkcnt_t count = 0;
1262 int ret;
1263
1264 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001265 if (datablock_addr(dn->inode, dn->node_page,
1266 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001267 count++;
1268 }
1269
1270 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001271 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001272 if (ret)
1273 return ret;
1274
1275 dn->ofs_in_node = ofs_in_node;
1276 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001277 dn->data_blkaddr = datablock_addr(dn->inode,
1278 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001279 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001280 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001281 * allocation.
1282 */
1283 if (dn->data_blkaddr == NULL_ADDR) {
1284 ret = -ENOSPC;
1285 break;
1286 }
1287 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001288 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001289 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001290 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001291 }
1292 }
1293
1294 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1295
1296 return ret;
1297}
1298
Chao Yu75cd4e02015-05-06 13:11:13 +08001299static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1300 int mode)
1301{
1302 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1303 struct address_space *mapping = inode->i_mapping;
1304 pgoff_t index, pg_start, pg_end;
1305 loff_t new_size = i_size_read(inode);
1306 loff_t off_start, off_end;
1307 int ret = 0;
1308
Chao Yu75cd4e02015-05-06 13:11:13 +08001309 ret = inode_newsize_ok(inode, (len + offset));
1310 if (ret)
1311 return ret;
1312
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001313 ret = f2fs_convert_inline_inode(inode);
1314 if (ret)
1315 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001316
1317 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1318 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001319 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001320
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001321 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1322 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001323
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001324 off_start = offset & (PAGE_SIZE - 1);
1325 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001326
1327 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001328 ret = fill_zero(inode, pg_start, off_start,
1329 off_end - off_start);
1330 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001331 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001332
Chao Yu75cd4e02015-05-06 13:11:13 +08001333 new_size = max_t(loff_t, new_size, offset + len);
1334 } else {
1335 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001336 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001337 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001338 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001339 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001340
Chao Yu75cd4e02015-05-06 13:11:13 +08001341 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001342 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001343 }
1344
Chao Yu6e961942016-05-09 19:56:31 +08001345 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001346 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001347 unsigned int end_offset;
1348 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001349
Chao Yuc70798532018-08-05 23:02:22 +08001350 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001351 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001352
1353 truncate_pagecache_range(inode,
1354 (loff_t)index << PAGE_SHIFT,
1355 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1356
Chao Yu75cd4e02015-05-06 13:11:13 +08001357 f2fs_lock_op(sbi);
1358
Chao Yu6e961942016-05-09 19:56:31 +08001359 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001360 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001361 if (ret) {
1362 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001363 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001364 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001365 goto out;
1366 }
1367
Chao Yu6e961942016-05-09 19:56:31 +08001368 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1369 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1370
1371 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001372 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001373
Chao Yu75cd4e02015-05-06 13:11:13 +08001374 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001375 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001376 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001377
1378 f2fs_balance_fs(sbi, dn.node_changed);
1379
Chao Yu6e961942016-05-09 19:56:31 +08001380 if (ret)
1381 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001382
Chao Yu6e961942016-05-09 19:56:31 +08001383 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001384 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001385 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001386 }
1387
1388 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001389 ret = fill_zero(inode, pg_end, 0, off_end);
1390 if (ret)
1391 goto out;
1392
Chao Yu75cd4e02015-05-06 13:11:13 +08001393 new_size = max_t(loff_t, new_size, offset + len);
1394 }
1395 }
1396
1397out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001398 if (new_size > i_size_read(inode)) {
1399 if (mode & FALLOC_FL_KEEP_SIZE)
1400 file_set_keep_isize(inode);
1401 else
1402 f2fs_i_size_write(inode, new_size);
1403 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001404 return ret;
1405}
1406
Chao Yuf62185d2015-05-28 19:16:57 +08001407static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1408{
1409 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001410 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001411 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001412 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001413
Chao Yuf62185d2015-05-28 19:16:57 +08001414 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001415 ret = inode_newsize_ok(inode, new_size);
1416 if (ret)
1417 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001418
1419 if (offset >= i_size_read(inode))
1420 return -EINVAL;
1421
1422 /* insert range should be aligned to block size of f2fs. */
1423 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1424 return -EINVAL;
1425
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001426 ret = f2fs_convert_inline_inode(inode);
1427 if (ret)
1428 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001429
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001430 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001431
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001432 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu4d57b862018-05-30 00:20:41 +08001433 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001434 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001435 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001436 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001437
1438 /* write out all dirty pages from offset */
1439 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1440 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001441 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001442
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001443 pg_start = offset >> PAGE_SHIFT;
1444 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001445 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001446 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001447
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001448 /* avoid gc operation during block exchange */
1449 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1450 down_write(&F2FS_I(inode)->i_mmap_sem);
1451 truncate_pagecache(inode, offset);
1452
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001453 while (!ret && idx > pg_start) {
1454 nr = idx - pg_start;
1455 if (nr > delta)
1456 nr = delta;
1457 idx -= nr;
1458
Chao Yuf62185d2015-05-28 19:16:57 +08001459 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001460 f2fs_drop_extent_tree(inode);
1461
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001462 ret = __exchange_data_block(inode, inode, idx,
1463 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001464 f2fs_unlock_op(sbi);
1465 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001466 up_write(&F2FS_I(inode)->i_mmap_sem);
1467 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001468
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001469 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001470 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001471 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1472 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001473 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001474
1475 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001476 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001477 return ret;
1478}
1479
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001480static int expand_inode_data(struct inode *inode, loff_t offset,
1481 loff_t len, int mode)
1482{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001483 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001484 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuc4020b22018-01-11 14:42:30 +08001485 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001486 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001487 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001488 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001489 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001490
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001491 err = inode_newsize_ok(inode, (len + offset));
1492 if (err)
1493 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001494
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001495 err = f2fs_convert_inline_inode(inode);
1496 if (err)
1497 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001498
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001499 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001500
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001501 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001502 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001503
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001504 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1505 map.m_len = pg_end - map.m_lblk;
1506 if (off_end)
1507 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001508
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001509 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1510 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001511 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001512
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001513 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001514 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001515
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001516 last_off = map.m_lblk + map.m_len - 1;
1517
1518 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001519 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001520 (loff_t)(last_off + 1) << PAGE_SHIFT;
1521 } else {
1522 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001523 }
1524
Chao Yue8ed90a2017-11-05 21:53:30 +08001525 if (new_size > i_size_read(inode)) {
1526 if (mode & FALLOC_FL_KEEP_SIZE)
1527 file_set_keep_isize(inode);
1528 else
1529 f2fs_i_size_write(inode, new_size);
1530 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001531
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001532 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001533}
1534
1535static long f2fs_fallocate(struct file *file, int mode,
1536 loff_t offset, loff_t len)
1537{
Al Viro6131ffa2013-02-27 16:59:05 -05001538 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001539 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001540
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001541 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1542 return -EIO;
1543
Chao Yuc9980122015-09-11 14:39:02 +08001544 /* f2fs only support ->fallocate for regular file */
1545 if (!S_ISREG(inode->i_mode))
1546 return -EINVAL;
1547
Chao Yuf62185d2015-05-28 19:16:57 +08001548 if (f2fs_encrypted_inode(inode) &&
1549 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001550 return -EOPNOTSUPP;
1551
Chao Yub4ace332015-05-06 13:09:46 +08001552 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001553 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1554 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001555 return -EOPNOTSUPP;
1556
Al Viro59551022016-01-22 15:40:57 -05001557 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001558
Taehee Yoo587c0a42015-04-21 15:59:12 +09001559 if (mode & FALLOC_FL_PUNCH_HOLE) {
1560 if (offset >= inode->i_size)
1561 goto out;
1562
Chao Yua66c7b22013-11-22 16:52:50 +08001563 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001564 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1565 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001566 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1567 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001568 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1569 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001570 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001571 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001572 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001573
Namjae Jeon3af60a42012-12-30 14:52:37 +09001574 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001575 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001576 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001577 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001578 }
Chao Yu3375f692014-01-28 10:29:26 +08001579
Taehee Yoo587c0a42015-04-21 15:59:12 +09001580out:
Al Viro59551022016-01-22 15:40:57 -05001581 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001582
Namjae Jeonc01e2852013-04-23 17:00:52 +09001583 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001584 return ret;
1585}
1586
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001587static int f2fs_release_file(struct inode *inode, struct file *filp)
1588{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001589 /*
1590 * f2fs_relase_file is called at every close calls. So we should
1591 * not drop any inmemory pages by close called by other process.
1592 */
1593 if (!(filp->f_mode & FMODE_WRITE) ||
1594 atomic_read(&inode->i_writecount) != 1)
1595 return 0;
1596
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001597 /* some remained atomic pages should discarded */
1598 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001599 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001600 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001601 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001602 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001603 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001604 clear_inode_flag(inode, FI_VOLATILE_FILE);
1605 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001606 }
1607 return 0;
1608}
1609
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001610static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001611{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001612 struct inode *inode = file_inode(file);
1613
1614 /*
1615 * If the process doing a transaction is crashed, we should do
1616 * roll-back. Otherwise, other reader/write can see corrupted database
1617 * until all the writers close its file. Since this should be done
1618 * before dropping file lock, it needs to do in ->flush.
1619 */
1620 if (f2fs_is_atomic_file(inode) &&
1621 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001622 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001623 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001624}
1625
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001626static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001627{
Al Viro6131ffa2013-02-27 16:59:05 -05001628 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001629 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu76481152018-04-08 11:27:14 +08001630 unsigned int flags = fi->i_flags;
1631
Chao Yu5b72d5e2018-07-17 20:41:47 +08001632 if (f2fs_encrypted_inode(inode))
Chao Yu76481152018-04-08 11:27:14 +08001633 flags |= F2FS_ENCRYPT_FL;
1634 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1635 flags |= F2FS_INLINE_DATA_FL;
1636
1637 flags &= F2FS_FL_USER_VISIBLE;
1638
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001639 return put_user(flags, (int __user *)arg);
1640}
1641
Chao Yu2c1d0302017-07-29 00:32:52 +08001642static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1643{
1644 struct f2fs_inode_info *fi = F2FS_I(inode);
1645 unsigned int oldflags;
1646
1647 /* Is it quota file? Do not allow user to mess with it */
1648 if (IS_NOQUOTA(inode))
1649 return -EPERM;
1650
1651 flags = f2fs_mask_flags(inode->i_mode, flags);
1652
1653 oldflags = fi->i_flags;
1654
Chao Yu59c84402018-04-03 15:08:17 +08001655 if ((flags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
Chao Yu2c1d0302017-07-29 00:32:52 +08001656 if (!capable(CAP_LINUX_IMMUTABLE))
1657 return -EPERM;
1658
Chao Yuc807a7c2018-04-08 11:25:53 +08001659 flags = flags & F2FS_FL_USER_MODIFIABLE;
1660 flags |= oldflags & ~F2FS_FL_USER_MODIFIABLE;
Chao Yu2c1d0302017-07-29 00:32:52 +08001661 fi->i_flags = flags;
1662
Chao Yu59c84402018-04-03 15:08:17 +08001663 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001664 set_inode_flag(inode, FI_PROJ_INHERIT);
1665 else
1666 clear_inode_flag(inode, FI_PROJ_INHERIT);
1667
1668 inode->i_ctime = current_time(inode);
1669 f2fs_set_inode_flags(inode);
1670 f2fs_mark_inode_dirty_sync(inode, false);
1671 return 0;
1672}
1673
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001674static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1675{
1676 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001677 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001678 int ret;
1679
Chao Yu7fb17fe2016-05-09 19:56:32 +08001680 if (!inode_owner_or_capable(inode))
1681 return -EACCES;
1682
1683 if (get_user(flags, (int __user *)arg))
1684 return -EFAULT;
1685
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001686 ret = mnt_want_write_file(filp);
1687 if (ret)
1688 return ret;
1689
Al Viro59551022016-01-22 15:40:57 -05001690 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001691
Chao Yu2c1d0302017-07-29 00:32:52 +08001692 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001693
Chao Yua72d4b92017-05-03 23:59:13 +08001694 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001695 mnt_drop_write_file(filp);
1696 return ret;
1697}
1698
Chao Yud49f3e82015-01-23 20:36:04 +08001699static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1700{
1701 struct inode *inode = file_inode(filp);
1702
1703 return put_user(inode->i_generation, (int __user *)arg);
1704}
1705
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001706static int f2fs_ioc_start_atomic_write(struct file *filp)
1707{
1708 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001709 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001710
1711 if (!inode_owner_or_capable(inode))
1712 return -EACCES;
1713
Jaegeuk Kime8118982017-03-17 10:04:15 +08001714 if (!S_ISREG(inode->i_mode))
1715 return -EINVAL;
1716
Chao Yu7fb17fe2016-05-09 19:56:32 +08001717 ret = mnt_want_write_file(filp);
1718 if (ret)
1719 return ret;
1720
Chao Yu0fac5582016-05-09 19:56:33 +08001721 inode_lock(inode);
1722
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001723 if (f2fs_is_atomic_file(inode)) {
1724 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1725 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001726 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001727 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001728
Chao Yuf4c9c742015-07-17 18:06:35 +08001729 ret = f2fs_convert_inline_inode(inode);
1730 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001731 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001732
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001733 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1734
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001735 if (!get_dirty_pages(inode))
Yunlei He054afda2018-04-18 11:06:39 +08001736 goto skip_flush;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001737
1738 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001739 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001740 inode->i_ino, get_dirty_pages(inode));
1741 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001742 if (ret) {
1743 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001744 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001745 }
Yunlei He054afda2018-04-18 11:06:39 +08001746skip_flush:
Yunlei He054afda2018-04-18 11:06:39 +08001747 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001748 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001749 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001750
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001751 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001752 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001753 stat_inc_atomic_write(inode);
1754 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001755out:
Chao Yu0fac5582016-05-09 19:56:33 +08001756 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001757 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001758 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001759}
1760
1761static int f2fs_ioc_commit_atomic_write(struct file *filp)
1762{
1763 struct inode *inode = file_inode(filp);
1764 int ret;
1765
1766 if (!inode_owner_or_capable(inode))
1767 return -EACCES;
1768
1769 ret = mnt_want_write_file(filp);
1770 if (ret)
1771 return ret;
1772
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001773 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08001774
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001775 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08001776
Chao Yub169c3c2018-04-18 17:45:02 +08001777 if (f2fs_is_volatile_file(inode)) {
1778 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001779 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08001780 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001781
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001782 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08001783 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001784 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001785 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001786
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001787 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001788 if (!ret) {
1789 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001790 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +08001791 stat_dec_atomic_write(inode);
1792 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001793 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001794 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001795 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001796err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08001797 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1798 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1799 ret = -EINVAL;
1800 }
Chao Yu0fac5582016-05-09 19:56:33 +08001801 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001802 mnt_drop_write_file(filp);
1803 return ret;
1804}
1805
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001806static int f2fs_ioc_start_volatile_write(struct file *filp)
1807{
1808 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001809 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001810
1811 if (!inode_owner_or_capable(inode))
1812 return -EACCES;
1813
Chao Yu8ff09712017-03-17 15:43:57 +08001814 if (!S_ISREG(inode->i_mode))
1815 return -EINVAL;
1816
Chao Yu7fb17fe2016-05-09 19:56:32 +08001817 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001818 if (ret)
1819 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001820
Chao Yu0fac5582016-05-09 19:56:33 +08001821 inode_lock(inode);
1822
Chao Yu7fb17fe2016-05-09 19:56:32 +08001823 if (f2fs_is_volatile_file(inode))
1824 goto out;
1825
1826 ret = f2fs_convert_inline_inode(inode);
1827 if (ret)
1828 goto out;
1829
Chao Yu648d50b2017-03-22 17:23:45 +08001830 stat_inc_volatile_write(inode);
1831 stat_update_max_volatile_write(inode);
1832
Jaegeuk Kim91942322016-05-20 10:13:22 -07001833 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001834 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001835out:
Chao Yu0fac5582016-05-09 19:56:33 +08001836 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001837 mnt_drop_write_file(filp);
1838 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001839}
1840
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001841static int f2fs_ioc_release_volatile_write(struct file *filp)
1842{
1843 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001844 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001845
1846 if (!inode_owner_or_capable(inode))
1847 return -EACCES;
1848
Chao Yu7fb17fe2016-05-09 19:56:32 +08001849 ret = mnt_want_write_file(filp);
1850 if (ret)
1851 return ret;
1852
Chao Yu0fac5582016-05-09 19:56:33 +08001853 inode_lock(inode);
1854
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001855 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001856 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001857
Chao Yu7fb17fe2016-05-09 19:56:32 +08001858 if (!f2fs_is_first_block_written(inode)) {
1859 ret = truncate_partial_data_page(inode, 0, true);
1860 goto out;
1861 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001862
Chao Yu7fb17fe2016-05-09 19:56:32 +08001863 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1864out:
Chao Yu0fac5582016-05-09 19:56:33 +08001865 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001866 mnt_drop_write_file(filp);
1867 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001868}
1869
1870static int f2fs_ioc_abort_volatile_write(struct file *filp)
1871{
1872 struct inode *inode = file_inode(filp);
1873 int ret;
1874
1875 if (!inode_owner_or_capable(inode))
1876 return -EACCES;
1877
1878 ret = mnt_want_write_file(filp);
1879 if (ret)
1880 return ret;
1881
Chao Yu0fac5582016-05-09 19:56:33 +08001882 inode_lock(inode);
1883
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001884 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001885 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001886 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001887 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001888 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001889 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001890 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001891
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09001892 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1893
Chao Yu0fac5582016-05-09 19:56:33 +08001894 inode_unlock(inode);
1895
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001896 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001897 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001898 return ret;
1899}
1900
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001901static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1902{
1903 struct inode *inode = file_inode(filp);
1904 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1905 struct super_block *sb = sbi->sb;
1906 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03001907 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001908
1909 if (!capable(CAP_SYS_ADMIN))
1910 return -EPERM;
1911
1912 if (get_user(in, (__u32 __user *)arg))
1913 return -EFAULT;
1914
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301915 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1916 ret = mnt_want_write_file(filp);
1917 if (ret)
1918 return ret;
1919 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001920
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001921 switch (in) {
1922 case F2FS_GOING_DOWN_FULLSYNC:
1923 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08001924 if (IS_ERR(sb)) {
1925 ret = PTR_ERR(sb);
1926 goto out;
1927 }
1928 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001929 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001930 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001931 thaw_bdev(sb->s_bdev, sb);
1932 }
1933 break;
1934 case F2FS_GOING_DOWN_METASYNC:
1935 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08001936 ret = f2fs_sync_fs(sb, 1);
1937 if (ret)
1938 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001939 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001940 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001941 break;
1942 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001943 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001944 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001945 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001946 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08001947 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001948 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001949 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001950 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001951 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001952 ret = -EINVAL;
1953 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001954 }
Chao Yu7950e9a2018-01-18 17:23:29 +08001955
Chao Yu4d57b862018-05-30 00:20:41 +08001956 f2fs_stop_gc_thread(sbi);
1957 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001958
Chao Yu4d57b862018-05-30 00:20:41 +08001959 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001960 clear_opt(sbi, DISCARD);
1961
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001962 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001963out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301964 if (in != F2FS_GOING_DOWN_FULLSYNC)
1965 mnt_drop_write_file(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001966 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001967}
1968
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001969static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1970{
1971 struct inode *inode = file_inode(filp);
1972 struct super_block *sb = inode->i_sb;
1973 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1974 struct fstrim_range range;
1975 int ret;
1976
1977 if (!capable(CAP_SYS_ADMIN))
1978 return -EPERM;
1979
Chao Yu7d20c8a2018-09-04 03:52:17 +08001980 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001981 return -EOPNOTSUPP;
1982
1983 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1984 sizeof(range)))
1985 return -EFAULT;
1986
Chao Yu7fb17fe2016-05-09 19:56:32 +08001987 ret = mnt_want_write_file(filp);
1988 if (ret)
1989 return ret;
1990
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001991 range.minlen = max((unsigned int)range.minlen,
1992 q->limits.discard_granularity);
1993 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001994 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001995 if (ret < 0)
1996 return ret;
1997
1998 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1999 sizeof(range)))
2000 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002001 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002002 return 0;
2003}
2004
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002005static bool uuid_is_nonzero(__u8 u[16])
2006{
2007 int i;
2008
2009 for (i = 0; i < 16; i++)
2010 if (u[i])
2011 return true;
2012 return false;
2013}
2014
2015static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2016{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002017 struct inode *inode = file_inode(filp);
2018
Sheng Yongccd31cb2018-02-06 12:31:17 +08002019 if (!f2fs_sb_has_encrypt(inode->i_sb))
Chao Yuead710b2017-11-14 19:28:42 +08002020 return -EOPNOTSUPP;
2021
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002022 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002023
Eric Biggersdb717d82016-11-26 19:07:49 -05002024 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002025}
2026
2027static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2028{
Sheng Yongccd31cb2018-02-06 12:31:17 +08002029 if (!f2fs_sb_has_encrypt(file_inode(filp)->i_sb))
Chao Yuead710b2017-11-14 19:28:42 +08002030 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002031 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002032}
2033
2034static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2035{
2036 struct inode *inode = file_inode(filp);
2037 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2038 int err;
2039
Sheng Yongccd31cb2018-02-06 12:31:17 +08002040 if (!f2fs_sb_has_encrypt(inode->i_sb))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002041 return -EOPNOTSUPP;
2042
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002043 err = mnt_want_write_file(filp);
2044 if (err)
2045 return err;
2046
Chao Yu846ae672018-02-26 22:04:13 +08002047 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002048
2049 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2050 goto got_it;
2051
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002052 /* update superblock with uuid */
2053 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2054
Chao Yuc5bda1c2015-06-08 13:28:03 +08002055 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002056 if (err) {
2057 /* undo new data */
2058 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002059 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002060 }
2061got_it:
2062 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2063 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002064 err = -EFAULT;
2065out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002066 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002067 mnt_drop_write_file(filp);
2068 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002069}
2070
Chao Yuc1c1b582015-07-10 18:08:10 +08002071static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2072{
2073 struct inode *inode = file_inode(filp);
2074 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002075 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002076 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002077
2078 if (!capable(CAP_SYS_ADMIN))
2079 return -EPERM;
2080
Chao Yud530d4d2015-10-05 22:22:44 +08002081 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002082 return -EFAULT;
2083
Chao Yud530d4d2015-10-05 22:22:44 +08002084 if (f2fs_readonly(sbi->sb))
2085 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002086
Chao Yu7fb17fe2016-05-09 19:56:32 +08002087 ret = mnt_want_write_file(filp);
2088 if (ret)
2089 return ret;
2090
Chao Yud530d4d2015-10-05 22:22:44 +08002091 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002092 if (!mutex_trylock(&sbi->gc_mutex)) {
2093 ret = -EBUSY;
2094 goto out;
2095 }
Chao Yud530d4d2015-10-05 22:22:44 +08002096 } else {
2097 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002098 }
2099
Jaegeuk Kime066b832017-04-13 15:17:00 -07002100 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002101out:
2102 mnt_drop_write_file(filp);
2103 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002104}
2105
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002106static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2107{
2108 struct inode *inode = file_inode(filp);
2109 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2110 struct f2fs_gc_range range;
2111 u64 end;
2112 int ret;
2113
2114 if (!capable(CAP_SYS_ADMIN))
2115 return -EPERM;
2116
2117 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2118 sizeof(range)))
2119 return -EFAULT;
2120
2121 if (f2fs_readonly(sbi->sb))
2122 return -EROFS;
2123
Yunlei Heb82f6e32018-04-24 11:40:30 +08002124 end = range.start + range.len;
2125 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
2126 return -EINVAL;
2127 }
2128
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002129 ret = mnt_want_write_file(filp);
2130 if (ret)
2131 return ret;
2132
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002133do_more:
2134 if (!range.sync) {
2135 if (!mutex_trylock(&sbi->gc_mutex)) {
2136 ret = -EBUSY;
2137 goto out;
2138 }
2139 } else {
2140 mutex_lock(&sbi->gc_mutex);
2141 }
2142
2143 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2144 range.start += sbi->blocks_per_seg;
2145 if (range.start <= end)
2146 goto do_more;
2147out:
2148 mnt_drop_write_file(filp);
2149 return ret;
2150}
2151
Chao Yu059c0642018-07-17 20:41:49 +08002152static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002153{
2154 struct inode *inode = file_inode(filp);
2155 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002156 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002157
2158 if (!capable(CAP_SYS_ADMIN))
2159 return -EPERM;
2160
2161 if (f2fs_readonly(sbi->sb))
2162 return -EROFS;
2163
Chao Yu7fb17fe2016-05-09 19:56:32 +08002164 ret = mnt_want_write_file(filp);
2165 if (ret)
2166 return ret;
2167
2168 ret = f2fs_sync_fs(sbi->sb, 1);
2169
2170 mnt_drop_write_file(filp);
2171 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002172}
2173
Chao Yud323d002015-10-27 09:53:45 +08002174static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2175 struct file *filp,
2176 struct f2fs_defragment *range)
2177{
2178 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002179 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2180 .m_seg_type = NO_CHECK_TYPE };
youngjun yoo1061fd42018-05-30 04:34:58 +09002181 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002182 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002183 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002184 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002185 block_t blk_end = 0;
2186 bool fragmented = false;
2187 int err;
2188
2189 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002190 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002191 return -EINVAL;
2192
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002193 pg_start = range->start >> PAGE_SHIFT;
2194 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002195
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002196 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002197
Al Viro59551022016-01-22 15:40:57 -05002198 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002199
2200 /* writeback all dirty pages in the range */
2201 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002202 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002203 if (err)
2204 goto out;
2205
2206 /*
2207 * lookup mapping info in extent cache, skip defragmenting if physical
2208 * block addresses are continuous.
2209 */
2210 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2211 if (ei.fofs + ei.len >= pg_end)
2212 goto out;
2213 }
2214
2215 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002216 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002217
2218 /*
2219 * lookup mapping info in dnode page cache, skip defragmenting if all
2220 * physical block addresses are continuous even if there are hole(s)
2221 * in logical blocks.
2222 */
2223 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002224 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002225 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002226 if (err)
2227 goto out;
2228
2229 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002230 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002231 continue;
2232 }
2233
Chao Yu25a912e2018-01-10 18:18:51 +08002234 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002235 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002236
2237 /* record total count of block that we're going to move */
2238 total += map.m_len;
2239
Chao Yud323d002015-10-27 09:53:45 +08002240 blk_end = map.m_pblk + map.m_len;
2241
2242 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002243 }
2244
2245 if (!fragmented)
2246 goto out;
2247
Chao Yu25a912e2018-01-10 18:18:51 +08002248 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002249
2250 /*
2251 * make sure there are enough free section for LFS allocation, this can
2252 * avoid defragment running in SSR mode when free section are allocated
2253 * intensively
2254 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002255 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002256 err = -EAGAIN;
2257 goto out;
2258 }
2259
Chao Yu25a912e2018-01-10 18:18:51 +08002260 map.m_lblk = pg_start;
2261 map.m_len = pg_end - pg_start;
2262 total = 0;
2263
Chao Yud323d002015-10-27 09:53:45 +08002264 while (map.m_lblk < pg_end) {
2265 pgoff_t idx;
2266 int cnt = 0;
2267
2268do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002269 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002270 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002271 if (err)
2272 goto clear_out;
2273
2274 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002275 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002276 continue;
2277 }
2278
Jaegeuk Kim91942322016-05-20 10:13:22 -07002279 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002280
2281 idx = map.m_lblk;
2282 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2283 struct page *page;
2284
Chao Yu4d57b862018-05-30 00:20:41 +08002285 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002286 if (IS_ERR(page)) {
2287 err = PTR_ERR(page);
2288 goto clear_out;
2289 }
2290
2291 set_page_dirty(page);
2292 f2fs_put_page(page, 1);
2293
2294 idx++;
2295 cnt++;
2296 total++;
2297 }
2298
2299 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002300
2301 if (idx < pg_end && cnt < blk_per_seg)
2302 goto do_map;
2303
Jaegeuk Kim91942322016-05-20 10:13:22 -07002304 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002305
2306 err = filemap_fdatawrite(inode->i_mapping);
2307 if (err)
2308 goto out;
2309 }
2310clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002311 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002312out:
Al Viro59551022016-01-22 15:40:57 -05002313 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002314 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002315 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002316 return err;
2317}
2318
2319static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2320{
2321 struct inode *inode = file_inode(filp);
2322 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2323 struct f2fs_defragment range;
2324 int err;
2325
2326 if (!capable(CAP_SYS_ADMIN))
2327 return -EPERM;
2328
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002329 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002330 return -EINVAL;
2331
Kinglong Meed7563862017-03-10 17:55:07 +08002332 if (f2fs_readonly(sbi->sb))
2333 return -EROFS;
2334
2335 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2336 sizeof(range)))
2337 return -EFAULT;
2338
2339 /* verify alignment of offset & size */
2340 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2341 return -EINVAL;
2342
2343 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2344 sbi->max_file_blocks))
2345 return -EINVAL;
2346
Chao Yud323d002015-10-27 09:53:45 +08002347 err = mnt_want_write_file(filp);
2348 if (err)
2349 return err;
2350
Chao Yud323d002015-10-27 09:53:45 +08002351 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002352 mnt_drop_write_file(filp);
2353
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002354 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002355 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002356 return err;
Chao Yud323d002015-10-27 09:53:45 +08002357
2358 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2359 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002360 return -EFAULT;
2361
2362 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002363}
2364
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002365static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2366 struct file *file_out, loff_t pos_out, size_t len)
2367{
2368 struct inode *src = file_inode(file_in);
2369 struct inode *dst = file_inode(file_out);
2370 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2371 size_t olen = len, dst_max_i_size = 0;
2372 size_t dst_osize;
2373 int ret;
2374
2375 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2376 src->i_sb != dst->i_sb)
2377 return -EXDEV;
2378
2379 if (unlikely(f2fs_readonly(src->i_sb)))
2380 return -EROFS;
2381
Chao Yufe8494b2016-08-04 20:13:02 +08002382 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2383 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002384
2385 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2386 return -EOPNOTSUPP;
2387
Fan Lid95fd912016-09-13 11:35:42 +08002388 if (src == dst) {
2389 if (pos_in == pos_out)
2390 return 0;
2391 if (pos_out > pos_in && pos_out < pos_in + len)
2392 return -EINVAL;
2393 }
2394
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002395 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002396 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002397 ret = -EBUSY;
2398 if (!inode_trylock(dst))
2399 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002400 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002401
2402 ret = -EINVAL;
2403 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2404 goto out_unlock;
2405 if (len == 0)
2406 olen = len = src->i_size - pos_in;
2407 if (pos_in + len == src->i_size)
2408 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2409 if (len == 0) {
2410 ret = 0;
2411 goto out_unlock;
2412 }
2413
2414 dst_osize = dst->i_size;
2415 if (pos_out + olen > dst->i_size)
2416 dst_max_i_size = pos_out + olen;
2417
2418 /* verify the end result is block aligned */
2419 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2420 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2421 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2422 goto out_unlock;
2423
2424 ret = f2fs_convert_inline_inode(src);
2425 if (ret)
2426 goto out_unlock;
2427
2428 ret = f2fs_convert_inline_inode(dst);
2429 if (ret)
2430 goto out_unlock;
2431
2432 /* write out all dirty pages from offset */
2433 ret = filemap_write_and_wait_range(src->i_mapping,
2434 pos_in, pos_in + len);
2435 if (ret)
2436 goto out_unlock;
2437
2438 ret = filemap_write_and_wait_range(dst->i_mapping,
2439 pos_out, pos_out + len);
2440 if (ret)
2441 goto out_unlock;
2442
2443 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002444
2445 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2446 if (src != dst) {
2447 ret = -EBUSY;
2448 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2449 goto out_src;
2450 }
2451
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002452 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002453 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2454 pos_out >> F2FS_BLKSIZE_BITS,
2455 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002456
2457 if (!ret) {
2458 if (dst_max_i_size)
2459 f2fs_i_size_write(dst, dst_max_i_size);
2460 else if (dst_osize != dst->i_size)
2461 f2fs_i_size_write(dst, dst_osize);
2462 }
2463 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002464
2465 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002466 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002467out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002468 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002469out_unlock:
2470 if (src != dst)
2471 inode_unlock(dst);
2472out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002473 inode_unlock(src);
2474 return ret;
2475}
2476
2477static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2478{
2479 struct f2fs_move_range range;
2480 struct fd dst;
2481 int err;
2482
2483 if (!(filp->f_mode & FMODE_READ) ||
2484 !(filp->f_mode & FMODE_WRITE))
2485 return -EBADF;
2486
2487 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2488 sizeof(range)))
2489 return -EFAULT;
2490
2491 dst = fdget(range.dst_fd);
2492 if (!dst.file)
2493 return -EBADF;
2494
2495 if (!(dst.file->f_mode & FMODE_WRITE)) {
2496 err = -EBADF;
2497 goto err_out;
2498 }
2499
2500 err = mnt_want_write_file(filp);
2501 if (err)
2502 goto err_out;
2503
2504 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2505 range.pos_out, range.len);
2506
2507 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002508 if (err)
2509 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002510
2511 if (copy_to_user((struct f2fs_move_range __user *)arg,
2512 &range, sizeof(range)))
2513 err = -EFAULT;
2514err_out:
2515 fdput(dst);
2516 return err;
2517}
2518
Jaegeuk Kime066b832017-04-13 15:17:00 -07002519static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2520{
2521 struct inode *inode = file_inode(filp);
2522 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2523 struct sit_info *sm = SIT_I(sbi);
2524 unsigned int start_segno = 0, end_segno = 0;
2525 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2526 struct f2fs_flush_device range;
2527 int ret;
2528
2529 if (!capable(CAP_SYS_ADMIN))
2530 return -EPERM;
2531
2532 if (f2fs_readonly(sbi->sb))
2533 return -EROFS;
2534
2535 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2536 sizeof(range)))
2537 return -EFAULT;
2538
2539 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2540 sbi->segs_per_sec != 1) {
2541 f2fs_msg(sbi->sb, KERN_WARNING,
2542 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2543 range.dev_num, sbi->s_ndevs,
2544 sbi->segs_per_sec);
2545 return -EINVAL;
2546 }
2547
2548 ret = mnt_want_write_file(filp);
2549 if (ret)
2550 return ret;
2551
2552 if (range.dev_num != 0)
2553 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2554 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2555
2556 start_segno = sm->last_victim[FLUSH_DEVICE];
2557 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2558 start_segno = dev_start_segno;
2559 end_segno = min(start_segno + range.segments, dev_end_segno);
2560
2561 while (start_segno < end_segno) {
2562 if (!mutex_trylock(&sbi->gc_mutex)) {
2563 ret = -EBUSY;
2564 goto out;
2565 }
2566 sm->last_victim[GC_CB] = end_segno + 1;
2567 sm->last_victim[GC_GREEDY] = end_segno + 1;
2568 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2569 ret = f2fs_gc(sbi, true, true, start_segno);
2570 if (ret == -EAGAIN)
2571 ret = 0;
2572 else if (ret < 0)
2573 break;
2574 start_segno++;
2575 }
2576out:
2577 mnt_drop_write_file(filp);
2578 return ret;
2579}
2580
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002581static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2582{
2583 struct inode *inode = file_inode(filp);
2584 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2585
2586 /* Must validate to set it with SQLite behavior in Android. */
2587 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2588
2589 return put_user(sb_feature, (u32 __user *)arg);
2590}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002591
Chao Yu2c1d0302017-07-29 00:32:52 +08002592#ifdef CONFIG_QUOTA
2593static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2594{
2595 struct inode *inode = file_inode(filp);
2596 struct f2fs_inode_info *fi = F2FS_I(inode);
2597 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2598 struct super_block *sb = sbi->sb;
2599 struct dquot *transfer_to[MAXQUOTAS] = {};
2600 struct page *ipage;
2601 kprojid_t kprojid;
2602 int err;
2603
2604 if (!f2fs_sb_has_project_quota(sb)) {
2605 if (projid != F2FS_DEF_PROJID)
2606 return -EOPNOTSUPP;
2607 else
2608 return 0;
2609 }
2610
2611 if (!f2fs_has_extra_attr(inode))
2612 return -EOPNOTSUPP;
2613
2614 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2615
2616 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2617 return 0;
2618
Chao Yu2c1d0302017-07-29 00:32:52 +08002619 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08002620 /* Is it quota file? Do not allow user to mess with it */
2621 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09002622 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002623
Chao Yu4d57b862018-05-30 00:20:41 +08002624 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09002625 if (IS_ERR(ipage))
2626 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08002627
2628 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2629 i_projid)) {
2630 err = -EOVERFLOW;
2631 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09002632 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002633 }
2634 f2fs_put_page(ipage, 1);
2635
Chao Yuc22aecd2018-04-21 17:53:52 +08002636 err = dquot_initialize(inode);
2637 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002638 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002639
2640 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2641 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2642 err = __dquot_transfer(inode, transfer_to);
2643 dqput(transfer_to[PRJQUOTA]);
2644 if (err)
2645 goto out_dirty;
2646 }
2647
2648 F2FS_I(inode)->i_projid = kprojid;
2649 inode->i_ctime = current_time(inode);
2650out_dirty:
2651 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08002652 return err;
2653}
2654#else
2655static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2656{
2657 if (projid != F2FS_DEF_PROJID)
2658 return -EOPNOTSUPP;
2659 return 0;
2660}
2661#endif
2662
2663/* Transfer internal flags to xflags */
2664static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2665{
2666 __u32 xflags = 0;
2667
Chao Yu59c84402018-04-03 15:08:17 +08002668 if (iflags & F2FS_SYNC_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002669 xflags |= FS_XFLAG_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +08002670 if (iflags & F2FS_IMMUTABLE_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002671 xflags |= FS_XFLAG_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +08002672 if (iflags & F2FS_APPEND_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002673 xflags |= FS_XFLAG_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +08002674 if (iflags & F2FS_NODUMP_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002675 xflags |= FS_XFLAG_NODUMP;
Chao Yu59c84402018-04-03 15:08:17 +08002676 if (iflags & F2FS_NOATIME_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002677 xflags |= FS_XFLAG_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +08002678 if (iflags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002679 xflags |= FS_XFLAG_PROJINHERIT;
2680 return xflags;
2681}
2682
2683#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2684 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2685 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2686
Chao Yu2c1d0302017-07-29 00:32:52 +08002687/* Transfer xflags flags to internal */
2688static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2689{
2690 unsigned long iflags = 0;
2691
2692 if (xflags & FS_XFLAG_SYNC)
Chao Yu59c84402018-04-03 15:08:17 +08002693 iflags |= F2FS_SYNC_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002694 if (xflags & FS_XFLAG_IMMUTABLE)
Chao Yu59c84402018-04-03 15:08:17 +08002695 iflags |= F2FS_IMMUTABLE_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002696 if (xflags & FS_XFLAG_APPEND)
Chao Yu59c84402018-04-03 15:08:17 +08002697 iflags |= F2FS_APPEND_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002698 if (xflags & FS_XFLAG_NODUMP)
Chao Yu59c84402018-04-03 15:08:17 +08002699 iflags |= F2FS_NODUMP_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002700 if (xflags & FS_XFLAG_NOATIME)
Chao Yu59c84402018-04-03 15:08:17 +08002701 iflags |= F2FS_NOATIME_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002702 if (xflags & FS_XFLAG_PROJINHERIT)
Chao Yu59c84402018-04-03 15:08:17 +08002703 iflags |= F2FS_PROJINHERIT_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002704
2705 return iflags;
2706}
2707
2708static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2709{
2710 struct inode *inode = file_inode(filp);
2711 struct f2fs_inode_info *fi = F2FS_I(inode);
2712 struct fsxattr fa;
2713
2714 memset(&fa, 0, sizeof(struct fsxattr));
2715 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
Chao Yuc807a7c2018-04-08 11:25:53 +08002716 F2FS_FL_USER_VISIBLE);
Chao Yu2c1d0302017-07-29 00:32:52 +08002717
2718 if (f2fs_sb_has_project_quota(inode->i_sb))
2719 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2720 fi->i_projid);
2721
2722 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2723 return -EFAULT;
2724 return 0;
2725}
2726
Wang Shilongc8e92752018-09-11 08:54:21 +09002727static int f2fs_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
2728{
2729 /*
2730 * Project Quota ID state is only allowed to change from within the init
2731 * namespace. Enforce that restriction only if we are trying to change
2732 * the quota ID state. Everything else is allowed in user namespaces.
2733 */
2734 if (current_user_ns() == &init_user_ns)
2735 return 0;
2736
2737 if (__kprojid_val(F2FS_I(inode)->i_projid) != fa->fsx_projid)
2738 return -EINVAL;
2739
2740 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) {
2741 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
2742 return -EINVAL;
2743 } else {
2744 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
2745 return -EINVAL;
2746 }
2747
2748 return 0;
2749}
2750
Chao Yu2c1d0302017-07-29 00:32:52 +08002751static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2752{
2753 struct inode *inode = file_inode(filp);
2754 struct f2fs_inode_info *fi = F2FS_I(inode);
2755 struct fsxattr fa;
2756 unsigned int flags;
2757 int err;
2758
2759 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2760 return -EFAULT;
2761
2762 /* Make sure caller has proper permission */
2763 if (!inode_owner_or_capable(inode))
2764 return -EACCES;
2765
2766 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2767 return -EOPNOTSUPP;
2768
2769 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2770 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2771 return -EOPNOTSUPP;
2772
2773 err = mnt_want_write_file(filp);
2774 if (err)
2775 return err;
2776
2777 inode_lock(inode);
Wang Shilongc8e92752018-09-11 08:54:21 +09002778 err = f2fs_ioctl_check_project(inode, &fa);
2779 if (err)
2780 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08002781 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2782 (flags & F2FS_FL_XFLAG_VISIBLE);
2783 err = __f2fs_ioc_setflags(inode, flags);
Chao Yu2c1d0302017-07-29 00:32:52 +08002784 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09002785 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08002786
2787 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09002788out:
2789 inode_unlock(inode);
2790 mnt_drop_write_file(filp);
2791 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08002792}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002793
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002794int f2fs_pin_file_control(struct inode *inode, bool inc)
2795{
2796 struct f2fs_inode_info *fi = F2FS_I(inode);
2797 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2798
2799 /* Use i_gc_failures for normal file as a risk signal. */
2800 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08002801 f2fs_i_gc_failures_write(inode,
2802 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002803
Chao Yu2ef79ec2018-05-07 20:28:54 +08002804 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002805 f2fs_msg(sbi->sb, KERN_WARNING,
2806 "%s: Enable GC = ino %lx after %x GC trials\n",
Chao Yu2ef79ec2018-05-07 20:28:54 +08002807 __func__, inode->i_ino,
2808 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002809 clear_inode_flag(inode, FI_PIN_FILE);
2810 return -EAGAIN;
2811 }
2812 return 0;
2813}
2814
2815static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2816{
2817 struct inode *inode = file_inode(filp);
2818 __u32 pin;
2819 int ret = 0;
2820
2821 if (!inode_owner_or_capable(inode))
2822 return -EACCES;
2823
2824 if (get_user(pin, (__u32 __user *)arg))
2825 return -EFAULT;
2826
2827 if (!S_ISREG(inode->i_mode))
2828 return -EINVAL;
2829
2830 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2831 return -EROFS;
2832
2833 ret = mnt_want_write_file(filp);
2834 if (ret)
2835 return ret;
2836
2837 inode_lock(inode);
2838
Chao Yu4d57b862018-05-30 00:20:41 +08002839 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb2018-01-17 16:31:38 +08002840 ret = -EINVAL;
2841 goto out;
2842 }
2843
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002844 if (!pin) {
2845 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08002846 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002847 goto done;
2848 }
2849
2850 if (f2fs_pin_file_control(inode, false)) {
2851 ret = -EAGAIN;
2852 goto out;
2853 }
2854 ret = f2fs_convert_inline_inode(inode);
2855 if (ret)
2856 goto out;
2857
2858 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002859 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002860done:
2861 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2862out:
2863 inode_unlock(inode);
2864 mnt_drop_write_file(filp);
2865 return ret;
2866}
2867
2868static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2869{
2870 struct inode *inode = file_inode(filp);
2871 __u32 pin = 0;
2872
2873 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08002874 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002875 return put_user(pin, (u32 __user *)arg);
2876}
2877
Chao Yuc4020b22018-01-11 14:42:30 +08002878int f2fs_precache_extents(struct inode *inode)
2879{
2880 struct f2fs_inode_info *fi = F2FS_I(inode);
2881 struct f2fs_map_blocks map;
2882 pgoff_t m_next_extent;
2883 loff_t end;
2884 int err;
2885
2886 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2887 return -EOPNOTSUPP;
2888
2889 map.m_lblk = 0;
2890 map.m_next_pgofs = NULL;
2891 map.m_next_extent = &m_next_extent;
2892 map.m_seg_type = NO_CHECK_TYPE;
2893 end = F2FS_I_SB(inode)->max_file_blocks;
2894
2895 while (map.m_lblk < end) {
2896 map.m_len = end - map.m_lblk;
2897
Chao Yub2532c62018-04-24 10:55:28 +08002898 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002899 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08002900 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002901 if (err)
2902 return err;
2903
2904 map.m_lblk = m_next_extent;
2905 }
2906
2907 return err;
2908}
2909
2910static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2911{
2912 return f2fs_precache_extents(file_inode(filp));
2913}
2914
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002915long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2916{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002917 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2918 return -EIO;
2919
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002920 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002921 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002922 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002923 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002924 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002925 case F2FS_IOC_GETVERSION:
2926 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002927 case F2FS_IOC_START_ATOMIC_WRITE:
2928 return f2fs_ioc_start_atomic_write(filp);
2929 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2930 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002931 case F2FS_IOC_START_VOLATILE_WRITE:
2932 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002933 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2934 return f2fs_ioc_release_volatile_write(filp);
2935 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2936 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002937 case F2FS_IOC_SHUTDOWN:
2938 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002939 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002940 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002941 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2942 return f2fs_ioc_set_encryption_policy(filp, arg);
2943 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2944 return f2fs_ioc_get_encryption_policy(filp, arg);
2945 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2946 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002947 case F2FS_IOC_GARBAGE_COLLECT:
2948 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002949 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2950 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002951 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08002952 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002953 case F2FS_IOC_DEFRAGMENT:
2954 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002955 case F2FS_IOC_MOVE_RANGE:
2956 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002957 case F2FS_IOC_FLUSH_DEVICE:
2958 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002959 case F2FS_IOC_GET_FEATURES:
2960 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08002961 case F2FS_IOC_FSGETXATTR:
2962 return f2fs_ioc_fsgetxattr(filp, arg);
2963 case F2FS_IOC_FSSETXATTR:
2964 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002965 case F2FS_IOC_GET_PIN_FILE:
2966 return f2fs_ioc_get_pin_file(filp, arg);
2967 case F2FS_IOC_SET_PIN_FILE:
2968 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08002969 case F2FS_IOC_PRECACHE_EXTENTS:
2970 return f2fs_ioc_precache_extents(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002971 default:
2972 return -ENOTTY;
2973 }
2974}
2975
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002976static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2977{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002978 struct file *file = iocb->ki_filp;
2979 struct inode *inode = file_inode(file);
2980 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002981
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002982 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2983 return -EIO;
2984
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002985 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2986 return -EINVAL;
2987
2988 if (!inode_trylock(inode)) {
2989 if (iocb->ki_flags & IOCB_NOWAIT)
2990 return -EAGAIN;
2991 inode_lock(inode);
2992 }
2993
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002994 ret = generic_write_checks(iocb, from);
2995 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07002996 bool preallocated = false;
2997 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002998 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002999
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003000 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3001 set_inode_flag(inode, FI_NO_PREALLOC);
3002
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003003 if ((iocb->ki_flags & IOCB_NOWAIT) &&
3004 (iocb->ki_flags & IOCB_DIRECT)) {
3005 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
3006 iov_iter_count(from)) ||
3007 f2fs_has_inline_data(inode) ||
3008 f2fs_force_buffered_io(inode, WRITE)) {
Yunlei Hecba41be2018-04-24 11:40:19 +08003009 clear_inode_flag(inode,
3010 FI_NO_PREALLOC);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003011 inode_unlock(inode);
3012 return -EAGAIN;
3013 }
3014
3015 } else {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003016 preallocated = true;
3017 target_size = iocb->ki_pos + iov_iter_count(from);
3018
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003019 err = f2fs_preallocate_blocks(iocb, from);
3020 if (err) {
3021 clear_inode_flag(inode, FI_NO_PREALLOC);
3022 inode_unlock(inode);
3023 return err;
3024 }
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003025 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003026 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003027 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003028
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003029 /* if we couldn't write data, we should deallocate blocks. */
3030 if (preallocated && i_size_read(inode) < target_size)
3031 f2fs_truncate(inode);
3032
Chao Yub0af6d42017-08-02 23:21:48 +08003033 if (ret > 0)
3034 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003035 }
3036 inode_unlock(inode);
3037
Christoph Hellwige2592212016-04-07 08:52:01 -07003038 if (ret > 0)
3039 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003040 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003041}
3042
Namjae Jeone9750822013-02-04 23:41:41 +09003043#ifdef CONFIG_COMPAT
3044long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3045{
3046 switch (cmd) {
3047 case F2FS_IOC32_GETFLAGS:
3048 cmd = F2FS_IOC_GETFLAGS;
3049 break;
3050 case F2FS_IOC32_SETFLAGS:
3051 cmd = F2FS_IOC_SETFLAGS;
3052 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003053 case F2FS_IOC32_GETVERSION:
3054 cmd = F2FS_IOC_GETVERSION;
3055 break;
3056 case F2FS_IOC_START_ATOMIC_WRITE:
3057 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3058 case F2FS_IOC_START_VOLATILE_WRITE:
3059 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3060 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3061 case F2FS_IOC_SHUTDOWN:
3062 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3063 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3064 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3065 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003066 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003067 case F2FS_IOC_WRITE_CHECKPOINT:
3068 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003069 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003070 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003071 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003072 case F2FS_IOC_FSGETXATTR:
3073 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003074 case F2FS_IOC_GET_PIN_FILE:
3075 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003076 case F2FS_IOC_PRECACHE_EXTENTS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003077 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003078 default:
3079 return -ENOIOCTLCMD;
3080 }
3081 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3082}
3083#endif
3084
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003085const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003086 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04003087 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003088 .write_iter = f2fs_file_write_iter,
3089 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003090 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003091 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003092 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003093 .fsync = f2fs_sync_file,
3094 .fallocate = f2fs_fallocate,
3095 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003096#ifdef CONFIG_COMPAT
3097 .compat_ioctl = f2fs_compat_ioctl,
3098#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003099 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003100 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003101};