blob: 672a542e546478cc09d8a10d564f87e9705084dc [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
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080036static int f2fs_filemap_fault(struct vm_fault *vmf)
37{
38 struct inode *inode = file_inode(vmf->vma->vm_file);
39 int err;
40
41 down_read(&F2FS_I(inode)->i_mmap_sem);
42 err = filemap_fault(vmf);
43 up_read(&F2FS_I(inode)->i_mmap_sem);
44
45 return err;
46}
47
Dave Jiang11bac802017-02-24 14:56:41 -080048static int 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)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090098 unsigned offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 offset = i_size_read(inode) & ~PAGE_MASK;
100 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900101 }
102 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700103 if (!PageUptodate(page))
104 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900105
Chao Yub0af6d42017-08-02 23:21:48 +0800106 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
107
Jaegeuk Kime943a102013-10-25 14:26:31 +0900108 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900109mapped:
110 /* fill the page */
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800111 f2fs_wait_on_page_writeback(page, DATA, false);
Chao Yu08b39fb2015-10-08 13:27:34 +0800112
113 /* wait for GCed encrypted page writeback */
Jaegeuk Kim19585932017-09-05 16:54:24 -0700114 if (f2fs_encrypted_file(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -0700115 f2fs_wait_on_block_writeback(sbi, 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 Yu9d1589e2014-08-20 18:37:35 +0800160 else if (!space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800161 cp_reason = CP_NO_SPC_ROLL;
Chao Yu9d1589e2014-08-20 18:37:35 +0800162 else if (!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;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800166 else if (sbi->active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800167 cp_reason = CP_SPEC_LOG_NUM;
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800168 else if (need_dentry_mark(sbi, inode->i_ino) &&
169 exist_written_data(sbi, F2FS_I(inode)->i_pino, TRANS_DIR_INO))
170 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800171
Chao Yua5fd5052017-11-06 22:51:45 +0800172 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800173}
174
Changman Lee9c7bb702014-12-08 15:29:40 +0900175static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
176{
177 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
178 bool ret = false;
179 /* But we need to avoid that there are some inode updates */
180 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
181 ret = true;
182 f2fs_put_page(i, 0);
183 return ret;
184}
185
Changman Lee51455b12014-12-08 15:29:41 +0900186static void try_to_fix_pino(struct inode *inode)
187{
188 struct f2fs_inode_info *fi = F2FS_I(inode);
189 nid_t pino;
190
191 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900192 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
193 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700194 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900195 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900196 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700197 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900198}
199
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700200static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
201 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900202{
203 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700204 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700205 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900206 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800207 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900208 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900209 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900210 .nr_to_write = LONG_MAX,
211 .for_reclaim = 0,
212 };
213
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900214 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900215 return 0;
216
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900217 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700218
219 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800220 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700221 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400222 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700223 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700224
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900225 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800226 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900227 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900228 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900229
Changman Lee9c7bb702014-12-08 15:29:40 +0900230 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800231 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700232 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900233 goto go_write;
234 }
235
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700236 /*
237 * if there is no written data, don't waste time to write recovery info.
238 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700239 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700240 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700241
Changman Lee9c7bb702014-12-08 15:29:40 +0900242 /* it may call write_inode just prior to fsync */
243 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700244 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700245
Jaegeuk Kim91942322016-05-20 10:13:22 -0700246 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700247 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700248 goto flush_out;
249 goto out;
250 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700251go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900252 /*
253 * Both of fdatasync() and fsync() are able to be recovered from
254 * sudden-power-off.
255 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700256 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800257 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700258 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900259
Chao Yua5fd5052017-11-06 22:51:45 +0800260 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900261 /* all the dirty node pages should be flushed for POR */
262 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900263
Changman Lee51455b12014-12-08 15:29:41 +0900264 /*
265 * We've secured consistency through sync_fs. Following pino
266 * will be used only for fsynced inodes after checkpoint.
267 */
268 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700269 clear_inode_flag(inode, FI_APPEND_WRITE);
270 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900271 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900272 }
Changman Lee51455b12014-12-08 15:29:41 +0900273sync_nodes:
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700274 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700275 if (ret)
276 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900277
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800278 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800279 if (unlikely(f2fs_cp_error(sbi))) {
280 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800281 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800282 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800283
Changman Lee51455b12014-12-08 15:29:41 +0900284 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700285 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900286 f2fs_write_inode(inode, NULL);
287 goto sync_nodes;
288 }
289
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700290 /*
291 * If it's atomic_write, it's just fine to keep write ordering. So
292 * here we don't need to wait for node write completion, since we use
293 * node chain which serializes node blocks. If one of node writes are
294 * reordered, we can see simply broken chain, resulting in stopping
295 * roll-forward recovery. It means we'll recover all or none node blocks
296 * given fsync mark.
297 */
298 if (!atomic) {
299 ret = wait_on_node_pages_writeback(sbi, ino);
300 if (ret)
301 goto out;
302 }
Changman Lee51455b12014-12-08 15:29:41 +0900303
304 /* once recovery info is written, don't need to tack this */
Chao Yua49324f2015-12-15 13:29:47 +0800305 remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700306 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900307flush_out:
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -0800308 if (!atomic)
Chao Yu39d787b2017-09-29 13:59:38 +0800309 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800310 if (!ret) {
311 remove_ino_entry(sbi, ino, UPDATE_INO);
312 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu39d787b2017-09-29 13:59:38 +0800313 remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800314 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800315 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900316out:
Chao Yua5fd5052017-11-06 22:51:45 +0800317 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700318 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900319 return ret;
320}
321
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700322int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
323{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200324 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
325 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700326 return f2fs_do_sync_file(file, start, end, datasync, false);
327}
328
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900329static pgoff_t __get_first_dirty_index(struct address_space *mapping,
330 pgoff_t pgofs, int whence)
331{
Jan Kara8faab642017-11-15 17:34:55 -0800332 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900333 int nr_pages;
334
335 if (whence != SEEK_DATA)
336 return 0;
337
338 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800339 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
340 1, &page);
341 if (!nr_pages)
342 return ULONG_MAX;
343 pgofs = page->index;
344 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900345 return pgofs;
346}
347
348static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
349 int whence)
350{
351 switch (whence) {
352 case SEEK_DATA:
353 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
354 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
355 return true;
356 break;
357 case SEEK_HOLE:
358 if (blkaddr == NULL_ADDR)
359 return true;
360 break;
361 }
362 return false;
363}
364
Chao Yu267378d2014-04-23 14:10:24 +0800365static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
366{
367 struct inode *inode = file->f_mapping->host;
368 loff_t maxbytes = inode->i_sb->s_maxbytes;
369 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900370 pgoff_t pgofs, end_offset, dirty;
371 loff_t data_ofs = offset;
372 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800373 int err = 0;
374
Al Viro59551022016-01-22 15:40:57 -0500375 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800376
377 isize = i_size_read(inode);
378 if (offset >= isize)
379 goto fail;
380
381 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800382 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800383 if (whence == SEEK_HOLE)
384 data_ofs = isize;
385 goto found;
386 }
387
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300388 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800389
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900390 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
391
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300392 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800393 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700394 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800395 if (err && err != -ENOENT) {
396 goto fail;
397 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900398 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800399 if (whence == SEEK_DATA) {
Chao Yu3cf45742016-01-26 15:40:44 +0800400 pgofs = get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800401 continue;
402 } else {
403 goto found;
404 }
405 }
406
Chao Yu81ca7352016-01-26 15:39:35 +0800407 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800408
409 /* find data/hole in dnode block */
410 for (; dn.ofs_in_node < end_offset;
411 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300412 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800413 block_t blkaddr;
Chao Yu7a2af762017-07-19 00:19:06 +0800414 blkaddr = datablock_addr(dn.inode,
415 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800416
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900417 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800418 f2fs_put_dnode(&dn);
419 goto found;
420 }
421 }
422 f2fs_put_dnode(&dn);
423 }
424
425 if (whence == SEEK_DATA)
426 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800427found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900428 if (whence == SEEK_HOLE && data_ofs > isize)
429 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500430 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800431 return vfs_setpos(file, data_ofs, maxbytes);
432fail:
Al Viro59551022016-01-22 15:40:57 -0500433 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800434 return -ENXIO;
435}
436
437static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
438{
439 struct inode *inode = file->f_mapping->host;
440 loff_t maxbytes = inode->i_sb->s_maxbytes;
441
442 switch (whence) {
443 case SEEK_SET:
444 case SEEK_CUR:
445 case SEEK_END:
446 return generic_file_llseek_size(file, offset, whence,
447 maxbytes, i_size_read(inode));
448 case SEEK_DATA:
449 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700450 if (offset < 0)
451 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800452 return f2fs_seek_block(file, offset, whence);
453 }
454
455 return -EINVAL;
456}
457
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900458static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
459{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700460 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800461 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700462
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200463 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
464 return -EIO;
465
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700466 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800467 err = f2fs_convert_inline_inode(inode);
468 if (err)
469 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700470
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900471 file_accessed(file);
472 vma->vm_ops = &f2fs_file_vm_ops;
473 return 0;
474}
475
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700476static int f2fs_file_open(struct inode *inode, struct file *filp)
477{
Eric Biggers2e168c82017-11-29 12:35:28 -0800478 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700479
Eric Biggers2e168c82017-11-29 12:35:28 -0800480 if (err)
481 return err;
Chao Yu0abd6752017-07-09 00:13:07 +0800482 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700483}
484
Chao Yuf652e9d2017-11-30 19:28:23 +0800485void truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900486{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700487 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900488 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800489 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900490 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800491 int base = 0;
492
493 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
494 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900495
Gu Zheng45590712013-07-15 17:57:38 +0800496 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800497 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900498
Chris Fries6c311ec2014-01-17 14:44:39 -0600499 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900500 block_t blkaddr = le32_to_cpu(*addr);
501 if (blkaddr == NULL_ADDR)
502 continue;
503
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800504 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800505 set_data_blkaddr(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900506 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700507 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700508 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900509 nr_free++;
510 }
Chao Yu19b2c302015-08-26 20:34:48 +0800511
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900512 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800513 pgoff_t fofs;
514 /*
515 * once we invalidate valid blkaddr in range [ofs, ofs + count],
516 * we will invalidate all blkaddr in the whole range.
517 */
518 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800519 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800520 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900521 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522 }
523 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900524
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800525 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900526 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
527 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900528}
529
530void truncate_data_blocks(struct dnode_of_data *dn)
531{
532 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
533}
534
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800535static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700536 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900537{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300538 unsigned offset = from & (PAGE_SIZE - 1);
539 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700540 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900541 struct page *page;
542
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700543 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700544 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700546 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700547 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700548 if (page && PageUptodate(page))
549 goto truncate_out;
550 f2fs_put_page(page, 1);
551 return 0;
552 }
553
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700554 page = get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900555 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800556 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700557truncate_out:
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800558 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300559 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700560
561 /* An encrypted inode should have a key and truncate the last page. */
562 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
563 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800564 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900565 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700566 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900567}
568
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700569int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900570{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700571 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900572 unsigned int blocksize = inode->i_sb->s_blocksize;
573 struct dnode_of_data dn;
574 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800575 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700576 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800577 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900578
Namjae Jeon51dd6242013-04-20 01:28:52 +0900579 trace_f2fs_truncate_blocks_enter(inode, from);
580
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800581 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900582
Chao Yu09210c92016-05-05 19:13:03 +0800583 if (free_from >= sbi->max_file_blocks)
584 goto free_partial;
585
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700586 if (lock)
587 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800588
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700589 ipage = get_node_page(sbi, inode->i_ino);
590 if (IS_ERR(ipage)) {
591 err = PTR_ERR(ipage);
592 goto out;
593 }
594
595 if (f2fs_has_inline_data(inode)) {
Kinglong Meebd4667c2017-03-10 20:43:20 +0800596 truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700597 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800598 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700599 goto out;
600 }
601
602 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700603 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900604 if (err) {
605 if (err == -ENOENT)
606 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700607 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700608 }
609
Chao Yu81ca7352016-01-26 15:39:35 +0800610 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900611
612 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700613 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900614
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900615 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
616 truncate_data_blocks_range(&dn, count);
617 free_from += count;
618 }
619
620 f2fs_put_dnode(&dn);
621free_next:
622 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800623out:
624 if (lock)
625 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800626free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700627 /* lastly zero out the first data page */
628 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800629 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900630
Namjae Jeon51dd6242013-04-20 01:28:52 +0900631 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900632 return err;
633}
634
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700635int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900636{
Chao Yub0154892015-08-24 17:39:42 +0800637 int err;
638
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200639 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
640 return -EIO;
641
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900642 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
643 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800644 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900645
Namjae Jeon51dd6242013-04-20 01:28:52 +0900646 trace_f2fs_truncate(inode);
647
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800648#ifdef CONFIG_F2FS_FAULT_INJECTION
649 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
650 f2fs_show_injection_info(FAULT_TRUNCATE);
651 return -EIO;
652 }
653#endif
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800654 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800655 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800656 err = f2fs_convert_inline_inode(inode);
657 if (err)
658 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800659 }
660
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700661 err = truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800662 if (err)
663 return err;
664
Deepa Dinamani078cd822016-09-14 07:48:04 -0700665 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700666 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800667 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668}
669
David Howellsa528d352017-01-31 16:46:22 +0000670int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800671 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900672{
David Howellsa528d352017-01-31 16:46:22 +0000673 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800674 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800675 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800676 unsigned int flags;
677
Chao Yu1c1d35d2018-01-25 14:54:42 +0800678 if (f2fs_has_extra_attr(inode) &&
679 f2fs_sb_has_inode_crtime(inode->i_sb) &&
680 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
681 stat->result_mask |= STATX_BTIME;
682 stat->btime.tv_sec = fi->i_crtime.tv_sec;
683 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
684 }
685
Chao Yu2c1d0302017-07-29 00:32:52 +0800686 flags = fi->i_flags & (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800687 if (flags & FS_APPEND_FL)
688 stat->attributes |= STATX_ATTR_APPEND;
689 if (flags & FS_COMPR_FL)
690 stat->attributes |= STATX_ATTR_COMPRESSED;
691 if (f2fs_encrypted_inode(inode))
692 stat->attributes |= STATX_ATTR_ENCRYPTED;
693 if (flags & FS_IMMUTABLE_FL)
694 stat->attributes |= STATX_ATTR_IMMUTABLE;
695 if (flags & FS_NODUMP_FL)
696 stat->attributes |= STATX_ATTR_NODUMP;
697
698 stat->attributes_mask |= (STATX_ATTR_APPEND |
699 STATX_ATTR_COMPRESSED |
700 STATX_ATTR_ENCRYPTED |
701 STATX_ATTR_IMMUTABLE |
702 STATX_ATTR_NODUMP);
703
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900704 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700705
706 /* we need to show initial sectors used for inline_data/dentries */
707 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
708 f2fs_has_inline_dentry(inode))
709 stat->blocks += (stat->size + 511) >> 9;
710
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900711 return 0;
712}
713
714#ifdef CONFIG_F2FS_FS_POSIX_ACL
715static void __setattr_copy(struct inode *inode, const struct iattr *attr)
716{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900717 unsigned int ia_valid = attr->ia_valid;
718
719 if (ia_valid & ATTR_UID)
720 inode->i_uid = attr->ia_uid;
721 if (ia_valid & ATTR_GID)
722 inode->i_gid = attr->ia_gid;
723 if (ia_valid & ATTR_ATIME)
724 inode->i_atime = timespec_trunc(attr->ia_atime,
725 inode->i_sb->s_time_gran);
726 if (ia_valid & ATTR_MTIME)
727 inode->i_mtime = timespec_trunc(attr->ia_mtime,
728 inode->i_sb->s_time_gran);
729 if (ia_valid & ATTR_CTIME)
730 inode->i_ctime = timespec_trunc(attr->ia_ctime,
731 inode->i_sb->s_time_gran);
732 if (ia_valid & ATTR_MODE) {
733 umode_t mode = attr->ia_mode;
734
735 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
736 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700737 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900738 }
739}
740#else
741#define __setattr_copy setattr_copy
742#endif
743
744int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
745{
David Howells2b0143b2015-03-17 22:25:59 +0000746 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900747 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800748 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900749
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200750 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
751 return -EIO;
752
Jan Kara31051c82016-05-26 16:55:18 +0200753 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 if (err)
755 return err;
756
Eric Biggers20bb2472017-11-29 12:35:32 -0800757 err = fscrypt_prepare_setattr(dentry, attr);
758 if (err)
759 return err;
760
Chao Yu0abd6752017-07-09 00:13:07 +0800761 if (is_quota_modification(inode, attr)) {
762 err = dquot_initialize(inode);
763 if (err)
764 return err;
765 }
766 if ((attr->ia_valid & ATTR_UID &&
767 !uid_eq(attr->ia_uid, inode->i_uid)) ||
768 (attr->ia_valid & ATTR_GID &&
769 !gid_eq(attr->ia_gid, inode->i_gid))) {
770 err = dquot_transfer(inode, attr);
771 if (err)
772 return err;
773 }
774
Chao Yu09db6a22014-09-15 18:02:09 +0800775 if (attr->ia_valid & ATTR_SIZE) {
Chao Yu3c454142015-06-05 18:34:02 +0800776 if (attr->ia_size <= i_size_read(inode)) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800777 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu09db6a22014-09-15 18:02:09 +0800778 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700779 err = f2fs_truncate(inode);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800780 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub0154892015-08-24 17:39:42 +0800781 if (err)
782 return err;
Chao Yu09db6a22014-09-15 18:02:09 +0800783 } else {
784 /*
Chao Yu3c454142015-06-05 18:34:02 +0800785 * do not trim all blocks after i_size if target size is
786 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800787 */
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800788 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3c454142015-06-05 18:34:02 +0800789 truncate_setsize(inode, attr->ia_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800790 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu0cab80e2015-12-01 11:36:16 +0800791
792 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800793 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800794 err = f2fs_convert_inline_inode(inode);
795 if (err)
796 return err;
797 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700798 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800799 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800800
Chao Yua0d00fa2017-10-09 17:55:19 +0800801 down_write(&F2FS_I(inode)->i_sem);
802 F2FS_I(inode)->last_disk_size = i_size_read(inode);
803 up_write(&F2FS_I(inode)->i_sem);
804
Yunlei Hec0ed4402016-12-11 15:35:15 +0800805 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900806 }
807
808 __setattr_copy(inode, attr);
809
810 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800811 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700812 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
813 inode->i_mode = F2FS_I(inode)->i_acl_mode;
814 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900815 }
816 }
817
Yunlei Hec0ed4402016-12-11 15:35:15 +0800818 /* file size may changed here */
819 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700820
821 /* inode change will produce dirty node pages flushed by checkpoint */
822 f2fs_balance_fs(F2FS_I_SB(inode), true);
823
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900824 return err;
825}
826
827const struct inode_operations f2fs_file_inode_operations = {
828 .getattr = f2fs_getattr,
829 .setattr = f2fs_setattr,
830 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800831 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900832#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900833 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900834#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900835 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900836};
837
Chao Yu63943282015-08-07 18:36:06 +0800838static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900839 loff_t start, loff_t len)
840{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700841 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900842 struct page *page;
843
844 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800845 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900846
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800847 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900848
Gu Zhenge4795562013-09-27 18:08:30 +0800849 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900850 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800851 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900852
Chao Yu63943282015-08-07 18:36:06 +0800853 if (IS_ERR(page))
854 return PTR_ERR(page);
855
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800856 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800857 zero_user(page, start, len);
858 set_page_dirty(page);
859 f2fs_put_page(page, 1);
860 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900861}
862
863int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
864{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900865 int err;
866
Chao Yuea587112015-09-17 20:22:44 +0800867 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900868 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800869 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900870
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900871 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yuea587112015-09-17 20:22:44 +0800872 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900873 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800874 if (err == -ENOENT) {
Weichao Guo48ab25f2017-10-14 08:13:32 +0800875 pg_start = get_next_page_offset(&dn, pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900876 continue;
Chao Yuea587112015-09-17 20:22:44 +0800877 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900878 return err;
879 }
880
Chao Yu81ca7352016-01-26 15:39:35 +0800881 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800882 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
883
884 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
885
886 truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900887 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800888
889 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900890 }
891 return 0;
892}
893
Chao Yua66c7b22013-11-22 16:52:50 +0800894static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900895{
896 pgoff_t pg_start, pg_end;
897 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800898 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900899
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800900 ret = f2fs_convert_inline_inode(inode);
901 if (ret)
902 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800903
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300904 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
905 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900906
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300907 off_start = offset & (PAGE_SIZE - 1);
908 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900909
910 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800911 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900912 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800913 if (ret)
914 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900915 } else {
Chao Yu63943282015-08-07 18:36:06 +0800916 if (off_start) {
917 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300918 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800919 if (ret)
920 return ret;
921 }
922 if (off_end) {
923 ret = fill_zero(inode, pg_end, 0, off_end);
924 if (ret)
925 return ret;
926 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900927
928 if (pg_start < pg_end) {
929 struct address_space *mapping = inode->i_mapping;
930 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700931 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500932
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800933 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900934
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300935 blk_start = (loff_t)pg_start << PAGE_SHIFT;
936 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800937 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900938 truncate_inode_pages_range(mapping, blk_start,
939 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900940
Gu Zhenge4795562013-09-27 18:08:30 +0800941 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900942 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800943 f2fs_unlock_op(sbi);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800944 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900945 }
946 }
947
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900948 return ret;
949}
950
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700951static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
952 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800953{
954 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
955 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700956 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700957
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700958next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700959 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700960 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700961 if (ret && ret != -ENOENT) {
962 return ret;
963 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700964 if (dn.max_level == 0)
965 return -ENOENT;
966 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
967 blkaddr += done;
968 do_replace += done;
969 goto next;
970 }
971
972 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
973 dn.ofs_in_node, len);
974 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800975 *blkaddr = datablock_addr(dn.inode,
976 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700977 if (!is_checkpointed_data(sbi, *blkaddr)) {
978
979 if (test_opt(sbi, LFS)) {
980 f2fs_put_dnode(&dn);
981 return -ENOTSUPP;
982 }
983
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700984 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +0800985 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700986 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700987 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700988 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700989 f2fs_put_dnode(&dn);
990next:
991 len -= done;
992 off += done;
993 if (len)
994 goto next_dnode;
995 return 0;
996}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700997
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700998static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
999 int *do_replace, pgoff_t off, int len)
1000{
1001 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1002 struct dnode_of_data dn;
1003 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001004
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001005 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1006 if (*do_replace == 0)
1007 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001008
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001009 set_new_dnode(&dn, inode, NULL, NULL, 0);
1010 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1011 if (ret) {
1012 dec_valid_block_count(sbi, inode, 1);
1013 invalidate_blocks(sbi, *blkaddr);
1014 } else {
1015 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001016 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001017 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001018 }
1019 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001020}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001021
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001022static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1023 block_t *blkaddr, int *do_replace,
1024 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1025{
1026 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1027 pgoff_t i = 0;
1028 int ret;
1029
1030 while (i < len) {
1031 if (blkaddr[i] == NULL_ADDR && !full) {
1032 i++;
1033 continue;
1034 }
1035
1036 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1037 struct dnode_of_data dn;
1038 struct node_info ni;
1039 size_t new_size;
1040 pgoff_t ilen;
1041
1042 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1043 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1044 if (ret)
1045 return ret;
1046
1047 get_node_info(sbi, dn.nid, &ni);
1048 ilen = min((pgoff_t)
1049 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1050 dn.ofs_in_node, len - i);
1051 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001052 dn.data_blkaddr = datablock_addr(dn.inode,
1053 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001054 truncate_data_blocks_range(&dn, 1);
1055
1056 if (do_replace[i]) {
1057 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001058 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001059 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001060 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001061 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1062 blkaddr[i], ni.version, true, false);
1063
1064 do_replace[i] = 0;
1065 }
1066 dn.ofs_in_node++;
1067 i++;
1068 new_size = (dst + i) << PAGE_SHIFT;
1069 if (dst_inode->i_size < new_size)
1070 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001071 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001072
1073 f2fs_put_dnode(&dn);
1074 } else {
1075 struct page *psrc, *pdst;
1076
1077 psrc = get_lock_data_page(src_inode, src + i, true);
1078 if (IS_ERR(psrc))
1079 return PTR_ERR(psrc);
1080 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1081 true);
1082 if (IS_ERR(pdst)) {
1083 f2fs_put_page(psrc, 1);
1084 return PTR_ERR(pdst);
1085 }
1086 f2fs_copy_page(psrc, pdst);
1087 set_page_dirty(pdst);
1088 f2fs_put_page(pdst, 1);
1089 f2fs_put_page(psrc, 1);
1090
1091 ret = truncate_hole(src_inode, src + i, src + i + 1);
1092 if (ret)
1093 return ret;
1094 i++;
1095 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001096 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001097 return 0;
1098}
1099
1100static int __exchange_data_block(struct inode *src_inode,
1101 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001102 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001103{
1104 block_t *src_blkaddr;
1105 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001106 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001107 int ret;
1108
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001109 while (len) {
1110 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001111
Chao Yu628b3d12017-11-30 19:28:18 +08001112 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1113 sizeof(block_t) * olen, GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001114 if (!src_blkaddr)
1115 return -ENOMEM;
1116
Chao Yu628b3d12017-11-30 19:28:18 +08001117 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1118 sizeof(int) * olen, GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001119 if (!do_replace) {
1120 kvfree(src_blkaddr);
1121 return -ENOMEM;
1122 }
1123
1124 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1125 do_replace, src, olen);
1126 if (ret)
1127 goto roll_back;
1128
1129 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1130 do_replace, src, dst, olen, full);
1131 if (ret)
1132 goto roll_back;
1133
1134 src += olen;
1135 dst += olen;
1136 len -= olen;
1137
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001138 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001139 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001140 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001141 return 0;
1142
1143roll_back:
1144 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1145 kvfree(src_blkaddr);
1146 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001147 return ret;
1148}
1149
1150static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1151{
1152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001153 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001154 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001155
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001156 f2fs_balance_fs(sbi, true);
1157 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001158
1159 f2fs_drop_extent_tree(inode);
1160
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001161 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1162 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +08001163 return ret;
1164}
1165
1166static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1167{
1168 pgoff_t pg_start, pg_end;
1169 loff_t new_size;
1170 int ret;
1171
Chao Yub4ace332015-05-06 13:09:46 +08001172 if (offset + len >= i_size_read(inode))
1173 return -EINVAL;
1174
1175 /* collapse range should be aligned to block size of f2fs. */
1176 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1177 return -EINVAL;
1178
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001179 ret = f2fs_convert_inline_inode(inode);
1180 if (ret)
1181 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001182
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001183 pg_start = offset >> PAGE_SHIFT;
1184 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yub4ace332015-05-06 13:09:46 +08001185
Chao Yu21020812017-11-23 23:26:52 +08001186 /* avoid gc operation during block exchange */
1187 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1188
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001189 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001190 /* write out all dirty pages from offset */
1191 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1192 if (ret)
Chao Yu21020812017-11-23 23:26:52 +08001193 goto out_unlock;
Chao Yubb066642017-11-03 10:21:05 +08001194
Chao Yub4ace332015-05-06 13:09:46 +08001195 truncate_pagecache(inode, offset);
1196
1197 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1198 if (ret)
Chao Yubb066642017-11-03 10:21:05 +08001199 goto out_unlock;
Chao Yub4ace332015-05-06 13:09:46 +08001200
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001201 /* write out all moved pages, if possible */
1202 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1203 truncate_pagecache(inode, offset);
1204
Chao Yub4ace332015-05-06 13:09:46 +08001205 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001206 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001207
1208 ret = truncate_blocks(inode, new_size, true);
1209 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001210 f2fs_i_size_write(inode, new_size);
Chao Yubb066642017-11-03 10:21:05 +08001211out_unlock:
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001212 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu21020812017-11-23 23:26:52 +08001213 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001214 return ret;
1215}
1216
Chao Yu6e961942016-05-09 19:56:31 +08001217static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1218 pgoff_t end)
1219{
1220 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1221 pgoff_t index = start;
1222 unsigned int ofs_in_node = dn->ofs_in_node;
1223 blkcnt_t count = 0;
1224 int ret;
1225
1226 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001227 if (datablock_addr(dn->inode, dn->node_page,
1228 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001229 count++;
1230 }
1231
1232 dn->ofs_in_node = ofs_in_node;
1233 ret = reserve_new_blocks(dn, count);
1234 if (ret)
1235 return ret;
1236
1237 dn->ofs_in_node = ofs_in_node;
1238 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001239 dn->data_blkaddr = datablock_addr(dn->inode,
1240 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001241 /*
1242 * reserve_new_blocks will not guarantee entire block
1243 * allocation.
1244 */
1245 if (dn->data_blkaddr == NULL_ADDR) {
1246 ret = -ENOSPC;
1247 break;
1248 }
1249 if (dn->data_blkaddr != NEW_ADDR) {
1250 invalidate_blocks(sbi, dn->data_blkaddr);
1251 dn->data_blkaddr = NEW_ADDR;
1252 set_data_blkaddr(dn);
1253 }
1254 }
1255
1256 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1257
1258 return ret;
1259}
1260
Chao Yu75cd4e02015-05-06 13:11:13 +08001261static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1262 int mode)
1263{
1264 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1265 struct address_space *mapping = inode->i_mapping;
1266 pgoff_t index, pg_start, pg_end;
1267 loff_t new_size = i_size_read(inode);
1268 loff_t off_start, off_end;
1269 int ret = 0;
1270
Chao Yu75cd4e02015-05-06 13:11:13 +08001271 ret = inode_newsize_ok(inode, (len + offset));
1272 if (ret)
1273 return ret;
1274
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001275 ret = f2fs_convert_inline_inode(inode);
1276 if (ret)
1277 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001278
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001279 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001280 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1281 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001282 goto out_sem;
Chao Yu75cd4e02015-05-06 13:11:13 +08001283
1284 truncate_pagecache_range(inode, offset, offset + len - 1);
1285
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001286 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1287 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001288
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001289 off_start = offset & (PAGE_SIZE - 1);
1290 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001291
1292 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001293 ret = fill_zero(inode, pg_start, off_start,
1294 off_end - off_start);
1295 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001296 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001297
Chao Yu75cd4e02015-05-06 13:11:13 +08001298 new_size = max_t(loff_t, new_size, offset + len);
1299 } else {
1300 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001301 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001302 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001303 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001304 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001305
Chao Yu75cd4e02015-05-06 13:11:13 +08001306 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001307 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001308 }
1309
Chao Yu6e961942016-05-09 19:56:31 +08001310 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001311 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001312 unsigned int end_offset;
1313 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001314
1315 f2fs_lock_op(sbi);
1316
Chao Yu6e961942016-05-09 19:56:31 +08001317 set_new_dnode(&dn, inode, NULL, NULL, 0);
1318 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001319 if (ret) {
1320 f2fs_unlock_op(sbi);
1321 goto out;
1322 }
1323
Chao Yu6e961942016-05-09 19:56:31 +08001324 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1325 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1326
1327 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001328 f2fs_put_dnode(&dn);
1329 f2fs_unlock_op(sbi);
Chao Yu9434fcd2016-10-11 22:57:02 +08001330
1331 f2fs_balance_fs(sbi, dn.node_changed);
1332
Chao Yu6e961942016-05-09 19:56:31 +08001333 if (ret)
1334 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001335
Chao Yu6e961942016-05-09 19:56:31 +08001336 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001337 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001338 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001339 }
1340
1341 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001342 ret = fill_zero(inode, pg_end, 0, off_end);
1343 if (ret)
1344 goto out;
1345
Chao Yu75cd4e02015-05-06 13:11:13 +08001346 new_size = max_t(loff_t, new_size, offset + len);
1347 }
1348 }
1349
1350out:
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001351 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001352 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001353out_sem:
1354 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001355
1356 return ret;
1357}
1358
Chao Yuf62185d2015-05-28 19:16:57 +08001359static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1360{
1361 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001362 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001363 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001364 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001365
Chao Yuf62185d2015-05-28 19:16:57 +08001366 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001367 ret = inode_newsize_ok(inode, new_size);
1368 if (ret)
1369 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001370
1371 if (offset >= i_size_read(inode))
1372 return -EINVAL;
1373
1374 /* insert range should be aligned to block size of f2fs. */
1375 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1376 return -EINVAL;
1377
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001378 ret = f2fs_convert_inline_inode(inode);
1379 if (ret)
1380 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001381
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001382 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001383
Chao Yu21020812017-11-23 23:26:52 +08001384 /* avoid gc operation during block exchange */
1385 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1386
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001387 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001388 ret = truncate_blocks(inode, i_size_read(inode), true);
1389 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001390 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001391
1392 /* write out all dirty pages from offset */
1393 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1394 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001395 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001396
1397 truncate_pagecache(inode, offset);
1398
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001399 pg_start = offset >> PAGE_SHIFT;
1400 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001401 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001402 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001403
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001404 while (!ret && idx > pg_start) {
1405 nr = idx - pg_start;
1406 if (nr > delta)
1407 nr = delta;
1408 idx -= nr;
1409
Chao Yuf62185d2015-05-28 19:16:57 +08001410 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001411 f2fs_drop_extent_tree(inode);
1412
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001413 ret = __exchange_data_block(inode, inode, idx,
1414 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001415 f2fs_unlock_op(sbi);
1416 }
1417
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001418 /* write out all moved pages, if possible */
1419 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1420 truncate_pagecache(inode, offset);
1421
1422 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001423 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001424out:
1425 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu21020812017-11-23 23:26:52 +08001426 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001427 return ret;
1428}
1429
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001430static int expand_inode_data(struct inode *inode, loff_t offset,
1431 loff_t len, int mode)
1432{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001433 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001434 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuc4020b22018-01-11 14:42:30 +08001435 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001436 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001437 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001438 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001439 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001440
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001441 err = inode_newsize_ok(inode, (len + offset));
1442 if (err)
1443 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001444
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001445 err = f2fs_convert_inline_inode(inode);
1446 if (err)
1447 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001448
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001449 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001450
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001451 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001452 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001453
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001454 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1455 map.m_len = pg_end - map.m_lblk;
1456 if (off_end)
1457 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001458
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001459 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1460 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001461 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001462
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001463 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001464 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001465
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001466 last_off = map.m_lblk + map.m_len - 1;
1467
1468 /* update new size to the failed position */
1469 new_size = (last_off == pg_end) ? offset + len:
1470 (loff_t)(last_off + 1) << PAGE_SHIFT;
1471 } else {
1472 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001473 }
1474
Chao Yue8ed90a2017-11-05 21:53:30 +08001475 if (new_size > i_size_read(inode)) {
1476 if (mode & FALLOC_FL_KEEP_SIZE)
1477 file_set_keep_isize(inode);
1478 else
1479 f2fs_i_size_write(inode, new_size);
1480 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001481
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001482 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001483}
1484
1485static long f2fs_fallocate(struct file *file, int mode,
1486 loff_t offset, loff_t len)
1487{
Al Viro6131ffa2013-02-27 16:59:05 -05001488 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001489 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001490
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001491 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1492 return -EIO;
1493
Chao Yuc9980122015-09-11 14:39:02 +08001494 /* f2fs only support ->fallocate for regular file */
1495 if (!S_ISREG(inode->i_mode))
1496 return -EINVAL;
1497
Chao Yuf62185d2015-05-28 19:16:57 +08001498 if (f2fs_encrypted_inode(inode) &&
1499 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001500 return -EOPNOTSUPP;
1501
Chao Yub4ace332015-05-06 13:09:46 +08001502 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001503 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1504 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001505 return -EOPNOTSUPP;
1506
Al Viro59551022016-01-22 15:40:57 -05001507 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001508
Taehee Yoo587c0a42015-04-21 15:59:12 +09001509 if (mode & FALLOC_FL_PUNCH_HOLE) {
1510 if (offset >= inode->i_size)
1511 goto out;
1512
Chao Yua66c7b22013-11-22 16:52:50 +08001513 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001514 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1515 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001516 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1517 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001518 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1519 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001520 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001521 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001522 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001523
Namjae Jeon3af60a42012-12-30 14:52:37 +09001524 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001525 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001526 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001527 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001528 }
Chao Yu3375f692014-01-28 10:29:26 +08001529
Taehee Yoo587c0a42015-04-21 15:59:12 +09001530out:
Al Viro59551022016-01-22 15:40:57 -05001531 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001532
Namjae Jeonc01e2852013-04-23 17:00:52 +09001533 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001534 return ret;
1535}
1536
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001537static int f2fs_release_file(struct inode *inode, struct file *filp)
1538{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001539 /*
1540 * f2fs_relase_file is called at every close calls. So we should
1541 * not drop any inmemory pages by close called by other process.
1542 */
1543 if (!(filp->f_mode & FMODE_WRITE) ||
1544 atomic_read(&inode->i_writecount) != 1)
1545 return 0;
1546
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001547 /* some remained atomic pages should discarded */
1548 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001549 drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001550 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001551 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001552 stat_dec_volatile_write(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001553 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001554 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001555 clear_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001556 }
1557 return 0;
1558}
1559
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001560static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001561{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001562 struct inode *inode = file_inode(file);
1563
1564 /*
1565 * If the process doing a transaction is crashed, we should do
1566 * roll-back. Otherwise, other reader/write can see corrupted database
1567 * until all the writers close its file. Since this should be done
1568 * before dropping file lock, it needs to do in ->flush.
1569 */
1570 if (f2fs_is_atomic_file(inode) &&
1571 F2FS_I(inode)->inmem_task == current)
1572 drop_inmem_pages(inode);
1573 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001574}
1575
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001576static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001577{
Al Viro6131ffa2013-02-27 16:59:05 -05001578 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001579 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08001580 unsigned int flags = fi->i_flags &
1581 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001582 return put_user(flags, (int __user *)arg);
1583}
1584
Chao Yu2c1d0302017-07-29 00:32:52 +08001585static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1586{
1587 struct f2fs_inode_info *fi = F2FS_I(inode);
1588 unsigned int oldflags;
1589
1590 /* Is it quota file? Do not allow user to mess with it */
1591 if (IS_NOQUOTA(inode))
1592 return -EPERM;
1593
1594 flags = f2fs_mask_flags(inode->i_mode, flags);
1595
1596 oldflags = fi->i_flags;
1597
1598 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL))
1599 if (!capable(CAP_LINUX_IMMUTABLE))
1600 return -EPERM;
1601
1602 flags = flags & (FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1603 flags |= oldflags & ~(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1604 fi->i_flags = flags;
1605
1606 if (fi->i_flags & FS_PROJINHERIT_FL)
1607 set_inode_flag(inode, FI_PROJ_INHERIT);
1608 else
1609 clear_inode_flag(inode, FI_PROJ_INHERIT);
1610
1611 inode->i_ctime = current_time(inode);
1612 f2fs_set_inode_flags(inode);
1613 f2fs_mark_inode_dirty_sync(inode, false);
1614 return 0;
1615}
1616
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001617static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1618{
1619 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001620 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001621 int ret;
1622
Chao Yu7fb17fe2016-05-09 19:56:32 +08001623 if (!inode_owner_or_capable(inode))
1624 return -EACCES;
1625
1626 if (get_user(flags, (int __user *)arg))
1627 return -EFAULT;
1628
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001629 ret = mnt_want_write_file(filp);
1630 if (ret)
1631 return ret;
1632
Al Viro59551022016-01-22 15:40:57 -05001633 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001634
Chao Yu2c1d0302017-07-29 00:32:52 +08001635 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001636
Chao Yua72d4b92017-05-03 23:59:13 +08001637 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001638 mnt_drop_write_file(filp);
1639 return ret;
1640}
1641
Chao Yud49f3e82015-01-23 20:36:04 +08001642static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1643{
1644 struct inode *inode = file_inode(filp);
1645
1646 return put_user(inode->i_generation, (int __user *)arg);
1647}
1648
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001649static int f2fs_ioc_start_atomic_write(struct file *filp)
1650{
1651 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001652 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001653
1654 if (!inode_owner_or_capable(inode))
1655 return -EACCES;
1656
Jaegeuk Kime8118982017-03-17 10:04:15 +08001657 if (!S_ISREG(inode->i_mode))
1658 return -EINVAL;
1659
Chao Yu7fb17fe2016-05-09 19:56:32 +08001660 ret = mnt_want_write_file(filp);
1661 if (ret)
1662 return ret;
1663
Chao Yu0fac5582016-05-09 19:56:33 +08001664 inode_lock(inode);
1665
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001666 if (f2fs_is_atomic_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001667 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001668
Chao Yuf4c9c742015-07-17 18:06:35 +08001669 ret = f2fs_convert_inline_inode(inode);
1670 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001671 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001672
Jaegeuk Kim91942322016-05-20 10:13:22 -07001673 set_inode_flag(inode, FI_ATOMIC_FILE);
Jaegeuk Kim6c3acd92017-04-12 10:01:33 -07001674 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001675 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1676
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001677 if (!get_dirty_pages(inode))
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001678 goto inc_stat;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001679
1680 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001681 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001682 inode->i_ino, get_dirty_pages(inode));
1683 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001684 if (ret) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001685 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu84a23fb2017-08-18 23:37:36 +08001686 clear_inode_flag(inode, FI_HOT_DATA);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001687 goto out;
1688 }
1689
1690inc_stat:
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001691 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001692 stat_inc_atomic_write(inode);
1693 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001694out:
Chao Yu0fac5582016-05-09 19:56:33 +08001695 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001696 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001697 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001698}
1699
1700static int f2fs_ioc_commit_atomic_write(struct file *filp)
1701{
1702 struct inode *inode = file_inode(filp);
1703 int ret;
1704
1705 if (!inode_owner_or_capable(inode))
1706 return -EACCES;
1707
1708 ret = mnt_want_write_file(filp);
1709 if (ret)
1710 return ret;
1711
Chao Yu0fac5582016-05-09 19:56:33 +08001712 inode_lock(inode);
1713
Chao Yu7fb17fe2016-05-09 19:56:32 +08001714 if (f2fs_is_volatile_file(inode))
1715 goto err_out;
1716
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001717 if (f2fs_is_atomic_file(inode)) {
Chao Yu29b96b52016-02-06 14:38:29 +08001718 ret = commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001719 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001720 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001721
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001722 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001723 if (!ret) {
1724 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu84a23fb2017-08-18 23:37:36 +08001725 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu5fe45742017-01-07 18:50:26 +08001726 stat_dec_atomic_write(inode);
1727 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001728 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001729 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001730 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001731err_out:
Chao Yu0fac5582016-05-09 19:56:33 +08001732 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001733 mnt_drop_write_file(filp);
1734 return ret;
1735}
1736
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001737static int f2fs_ioc_start_volatile_write(struct file *filp)
1738{
1739 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001740 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001741
1742 if (!inode_owner_or_capable(inode))
1743 return -EACCES;
1744
Chao Yu8ff09712017-03-17 15:43:57 +08001745 if (!S_ISREG(inode->i_mode))
1746 return -EINVAL;
1747
Chao Yu7fb17fe2016-05-09 19:56:32 +08001748 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001749 if (ret)
1750 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001751
Chao Yu0fac5582016-05-09 19:56:33 +08001752 inode_lock(inode);
1753
Chao Yu7fb17fe2016-05-09 19:56:32 +08001754 if (f2fs_is_volatile_file(inode))
1755 goto out;
1756
1757 ret = f2fs_convert_inline_inode(inode);
1758 if (ret)
1759 goto out;
1760
Chao Yu648d50b2017-03-22 17:23:45 +08001761 stat_inc_volatile_write(inode);
1762 stat_update_max_volatile_write(inode);
1763
Jaegeuk Kim91942322016-05-20 10:13:22 -07001764 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001765 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001766out:
Chao Yu0fac5582016-05-09 19:56:33 +08001767 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001768 mnt_drop_write_file(filp);
1769 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001770}
1771
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001772static int f2fs_ioc_release_volatile_write(struct file *filp)
1773{
1774 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001775 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001776
1777 if (!inode_owner_or_capable(inode))
1778 return -EACCES;
1779
Chao Yu7fb17fe2016-05-09 19:56:32 +08001780 ret = mnt_want_write_file(filp);
1781 if (ret)
1782 return ret;
1783
Chao Yu0fac5582016-05-09 19:56:33 +08001784 inode_lock(inode);
1785
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001786 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001787 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001788
Chao Yu7fb17fe2016-05-09 19:56:32 +08001789 if (!f2fs_is_first_block_written(inode)) {
1790 ret = truncate_partial_data_page(inode, 0, true);
1791 goto out;
1792 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001793
Chao Yu7fb17fe2016-05-09 19:56:32 +08001794 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1795out:
Chao Yu0fac5582016-05-09 19:56:33 +08001796 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001797 mnt_drop_write_file(filp);
1798 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001799}
1800
1801static int f2fs_ioc_abort_volatile_write(struct file *filp)
1802{
1803 struct inode *inode = file_inode(filp);
1804 int ret;
1805
1806 if (!inode_owner_or_capable(inode))
1807 return -EACCES;
1808
1809 ret = mnt_want_write_file(filp);
1810 if (ret)
1811 return ret;
1812
Chao Yu0fac5582016-05-09 19:56:33 +08001813 inode_lock(inode);
1814
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001815 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001816 drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001817 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001818 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001819 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001820 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001821 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001822
Chao Yu0fac5582016-05-09 19:56:33 +08001823 inode_unlock(inode);
1824
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001825 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001826 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001827 return ret;
1828}
1829
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001830static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1831{
1832 struct inode *inode = file_inode(filp);
1833 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1834 struct super_block *sb = sbi->sb;
1835 __u32 in;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001836 int ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001837
1838 if (!capable(CAP_SYS_ADMIN))
1839 return -EPERM;
1840
1841 if (get_user(in, (__u32 __user *)arg))
1842 return -EFAULT;
1843
Chao Yu7fb17fe2016-05-09 19:56:32 +08001844 ret = mnt_want_write_file(filp);
1845 if (ret)
1846 return ret;
1847
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001848 switch (in) {
1849 case F2FS_GOING_DOWN_FULLSYNC:
1850 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08001851 if (IS_ERR(sb)) {
1852 ret = PTR_ERR(sb);
1853 goto out;
1854 }
1855 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001856 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001857 thaw_bdev(sb->s_bdev, sb);
1858 }
1859 break;
1860 case F2FS_GOING_DOWN_METASYNC:
1861 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08001862 ret = f2fs_sync_fs(sb, 1);
1863 if (ret)
1864 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001865 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001866 break;
1867 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001868 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001869 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001870 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yub0af6d42017-08-02 23:21:48 +08001871 sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001872 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001873 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001874 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001875 ret = -EINVAL;
1876 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001877 }
Chao Yu7950e9a2018-01-18 17:23:29 +08001878
1879 stop_gc_thread(sbi);
1880 stop_discard_thread(sbi);
1881
1882 drop_discard_cmd(sbi);
1883 clear_opt(sbi, DISCARD);
1884
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001885 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001886out:
1887 mnt_drop_write_file(filp);
1888 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001889}
1890
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001891static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1892{
1893 struct inode *inode = file_inode(filp);
1894 struct super_block *sb = inode->i_sb;
1895 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1896 struct fstrim_range range;
1897 int ret;
1898
1899 if (!capable(CAP_SYS_ADMIN))
1900 return -EPERM;
1901
1902 if (!blk_queue_discard(q))
1903 return -EOPNOTSUPP;
1904
1905 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1906 sizeof(range)))
1907 return -EFAULT;
1908
Chao Yu7fb17fe2016-05-09 19:56:32 +08001909 ret = mnt_want_write_file(filp);
1910 if (ret)
1911 return ret;
1912
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001913 range.minlen = max((unsigned int)range.minlen,
1914 q->limits.discard_granularity);
1915 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001916 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001917 if (ret < 0)
1918 return ret;
1919
1920 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1921 sizeof(range)))
1922 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001923 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001924 return 0;
1925}
1926
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001927static bool uuid_is_nonzero(__u8 u[16])
1928{
1929 int i;
1930
1931 for (i = 0; i < 16; i++)
1932 if (u[i])
1933 return true;
1934 return false;
1935}
1936
1937static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1938{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001939 struct inode *inode = file_inode(filp);
1940
Chao Yuead710b2017-11-14 19:28:42 +08001941 if (!f2fs_sb_has_crypto(inode->i_sb))
1942 return -EOPNOTSUPP;
1943
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001944 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001945
Eric Biggersdb717d82016-11-26 19:07:49 -05001946 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001947}
1948
1949static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1950{
Chao Yuead710b2017-11-14 19:28:42 +08001951 if (!f2fs_sb_has_crypto(file_inode(filp)->i_sb))
1952 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05001953 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001954}
1955
1956static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1957{
1958 struct inode *inode = file_inode(filp);
1959 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1960 int err;
1961
1962 if (!f2fs_sb_has_crypto(inode->i_sb))
1963 return -EOPNOTSUPP;
1964
1965 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1966 goto got_it;
1967
1968 err = mnt_want_write_file(filp);
1969 if (err)
1970 return err;
1971
1972 /* update superblock with uuid */
1973 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1974
Chao Yuc5bda1c2015-06-08 13:28:03 +08001975 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001976 if (err) {
1977 /* undo new data */
1978 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yue8240f62015-12-15 17:19:26 +08001979 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001980 return err;
1981 }
Chao Yue8240f62015-12-15 17:19:26 +08001982 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001983got_it:
1984 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1985 16))
1986 return -EFAULT;
1987 return 0;
1988}
1989
Chao Yuc1c1b582015-07-10 18:08:10 +08001990static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1991{
1992 struct inode *inode = file_inode(filp);
1993 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08001994 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001995 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08001996
1997 if (!capable(CAP_SYS_ADMIN))
1998 return -EPERM;
1999
Chao Yud530d4d2015-10-05 22:22:44 +08002000 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002001 return -EFAULT;
2002
Chao Yud530d4d2015-10-05 22:22:44 +08002003 if (f2fs_readonly(sbi->sb))
2004 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002005
Chao Yu7fb17fe2016-05-09 19:56:32 +08002006 ret = mnt_want_write_file(filp);
2007 if (ret)
2008 return ret;
2009
Chao Yud530d4d2015-10-05 22:22:44 +08002010 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002011 if (!mutex_trylock(&sbi->gc_mutex)) {
2012 ret = -EBUSY;
2013 goto out;
2014 }
Chao Yud530d4d2015-10-05 22:22:44 +08002015 } else {
2016 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002017 }
2018
Jaegeuk Kime066b832017-04-13 15:17:00 -07002019 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002020out:
2021 mnt_drop_write_file(filp);
2022 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002023}
2024
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002025static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2026{
2027 struct inode *inode = file_inode(filp);
2028 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2029 struct f2fs_gc_range range;
2030 u64 end;
2031 int ret;
2032
2033 if (!capable(CAP_SYS_ADMIN))
2034 return -EPERM;
2035
2036 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2037 sizeof(range)))
2038 return -EFAULT;
2039
2040 if (f2fs_readonly(sbi->sb))
2041 return -EROFS;
2042
2043 ret = mnt_want_write_file(filp);
2044 if (ret)
2045 return ret;
2046
2047 end = range.start + range.len;
2048 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi))
2049 return -EINVAL;
2050do_more:
2051 if (!range.sync) {
2052 if (!mutex_trylock(&sbi->gc_mutex)) {
2053 ret = -EBUSY;
2054 goto out;
2055 }
2056 } else {
2057 mutex_lock(&sbi->gc_mutex);
2058 }
2059
2060 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2061 range.start += sbi->blocks_per_seg;
2062 if (range.start <= end)
2063 goto do_more;
2064out:
2065 mnt_drop_write_file(filp);
2066 return ret;
2067}
2068
Chao Yu456b88e2015-10-05 22:24:19 +08002069static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2070{
2071 struct inode *inode = file_inode(filp);
2072 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002073 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002074
2075 if (!capable(CAP_SYS_ADMIN))
2076 return -EPERM;
2077
2078 if (f2fs_readonly(sbi->sb))
2079 return -EROFS;
2080
Chao Yu7fb17fe2016-05-09 19:56:32 +08002081 ret = mnt_want_write_file(filp);
2082 if (ret)
2083 return ret;
2084
2085 ret = f2fs_sync_fs(sbi->sb, 1);
2086
2087 mnt_drop_write_file(filp);
2088 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002089}
2090
Chao Yud323d002015-10-27 09:53:45 +08002091static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2092 struct file *filp,
2093 struct f2fs_defragment *range)
2094{
2095 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002096 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2097 .m_seg_type = NO_CHECK_TYPE };
Hou Pengyange15882b2017-02-23 09:18:05 +00002098 struct extent_info ei = {0,0,0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002099 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002100 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002101 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002102 block_t blk_end = 0;
2103 bool fragmented = false;
2104 int err;
2105
2106 /* if in-place-update policy is enabled, don't waste time here */
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002107 if (should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002108 return -EINVAL;
2109
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002110 pg_start = range->start >> PAGE_SHIFT;
2111 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002112
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002113 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002114
Al Viro59551022016-01-22 15:40:57 -05002115 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002116
2117 /* writeback all dirty pages in the range */
2118 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002119 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002120 if (err)
2121 goto out;
2122
2123 /*
2124 * lookup mapping info in extent cache, skip defragmenting if physical
2125 * block addresses are continuous.
2126 */
2127 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2128 if (ei.fofs + ei.len >= pg_end)
2129 goto out;
2130 }
2131
2132 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002133 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002134
2135 /*
2136 * lookup mapping info in dnode page cache, skip defragmenting if all
2137 * physical block addresses are continuous even if there are hole(s)
2138 * in logical blocks.
2139 */
2140 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002141 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002142 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002143 if (err)
2144 goto out;
2145
2146 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002147 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002148 continue;
2149 }
2150
Chao Yu25a912e2018-01-10 18:18:51 +08002151 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002152 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002153
2154 /* record total count of block that we're going to move */
2155 total += map.m_len;
2156
Chao Yud323d002015-10-27 09:53:45 +08002157 blk_end = map.m_pblk + map.m_len;
2158
2159 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002160 }
2161
2162 if (!fragmented)
2163 goto out;
2164
Chao Yu25a912e2018-01-10 18:18:51 +08002165 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002166
2167 /*
2168 * make sure there are enough free section for LFS allocation, this can
2169 * avoid defragment running in SSR mode when free section are allocated
2170 * intensively
2171 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002172 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002173 err = -EAGAIN;
2174 goto out;
2175 }
2176
Chao Yu25a912e2018-01-10 18:18:51 +08002177 map.m_lblk = pg_start;
2178 map.m_len = pg_end - pg_start;
2179 total = 0;
2180
Chao Yud323d002015-10-27 09:53:45 +08002181 while (map.m_lblk < pg_end) {
2182 pgoff_t idx;
2183 int cnt = 0;
2184
2185do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002186 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002187 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002188 if (err)
2189 goto clear_out;
2190
2191 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002192 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002193 continue;
2194 }
2195
Jaegeuk Kim91942322016-05-20 10:13:22 -07002196 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002197
2198 idx = map.m_lblk;
2199 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2200 struct page *page;
2201
2202 page = get_lock_data_page(inode, idx, true);
2203 if (IS_ERR(page)) {
2204 err = PTR_ERR(page);
2205 goto clear_out;
2206 }
2207
2208 set_page_dirty(page);
2209 f2fs_put_page(page, 1);
2210
2211 idx++;
2212 cnt++;
2213 total++;
2214 }
2215
2216 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002217
2218 if (idx < pg_end && cnt < blk_per_seg)
2219 goto do_map;
2220
Jaegeuk Kim91942322016-05-20 10:13:22 -07002221 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002222
2223 err = filemap_fdatawrite(inode->i_mapping);
2224 if (err)
2225 goto out;
2226 }
2227clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002228 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002229out:
Al Viro59551022016-01-22 15:40:57 -05002230 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002231 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002232 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002233 return err;
2234}
2235
2236static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2237{
2238 struct inode *inode = file_inode(filp);
2239 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2240 struct f2fs_defragment range;
2241 int err;
2242
2243 if (!capable(CAP_SYS_ADMIN))
2244 return -EPERM;
2245
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002246 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002247 return -EINVAL;
2248
Kinglong Meed7563862017-03-10 17:55:07 +08002249 if (f2fs_readonly(sbi->sb))
2250 return -EROFS;
2251
2252 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2253 sizeof(range)))
2254 return -EFAULT;
2255
2256 /* verify alignment of offset & size */
2257 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2258 return -EINVAL;
2259
2260 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2261 sbi->max_file_blocks))
2262 return -EINVAL;
2263
Chao Yud323d002015-10-27 09:53:45 +08002264 err = mnt_want_write_file(filp);
2265 if (err)
2266 return err;
2267
Chao Yud323d002015-10-27 09:53:45 +08002268 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002269 mnt_drop_write_file(filp);
2270
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002271 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002272 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002273 return err;
Chao Yud323d002015-10-27 09:53:45 +08002274
2275 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2276 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002277 return -EFAULT;
2278
2279 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002280}
2281
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002282static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2283 struct file *file_out, loff_t pos_out, size_t len)
2284{
2285 struct inode *src = file_inode(file_in);
2286 struct inode *dst = file_inode(file_out);
2287 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2288 size_t olen = len, dst_max_i_size = 0;
2289 size_t dst_osize;
2290 int ret;
2291
2292 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2293 src->i_sb != dst->i_sb)
2294 return -EXDEV;
2295
2296 if (unlikely(f2fs_readonly(src->i_sb)))
2297 return -EROFS;
2298
Chao Yufe8494b2016-08-04 20:13:02 +08002299 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2300 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002301
2302 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2303 return -EOPNOTSUPP;
2304
Fan Lid95fd912016-09-13 11:35:42 +08002305 if (src == dst) {
2306 if (pos_in == pos_out)
2307 return 0;
2308 if (pos_out > pos_in && pos_out < pos_in + len)
2309 return -EINVAL;
2310 }
2311
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002312 inode_lock(src);
Chao Yubb066642017-11-03 10:21:05 +08002313 down_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Chao Yu20a3d612016-08-04 20:13:03 +08002314 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002315 ret = -EBUSY;
2316 if (!inode_trylock(dst))
2317 goto out;
2318 if (!down_write_trylock(&F2FS_I(dst)->dio_rwsem[WRITE])) {
2319 inode_unlock(dst);
Chao Yu20a3d612016-08-04 20:13:03 +08002320 goto out;
2321 }
2322 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002323
2324 ret = -EINVAL;
2325 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2326 goto out_unlock;
2327 if (len == 0)
2328 olen = len = src->i_size - pos_in;
2329 if (pos_in + len == src->i_size)
2330 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2331 if (len == 0) {
2332 ret = 0;
2333 goto out_unlock;
2334 }
2335
2336 dst_osize = dst->i_size;
2337 if (pos_out + olen > dst->i_size)
2338 dst_max_i_size = pos_out + olen;
2339
2340 /* verify the end result is block aligned */
2341 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2342 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2343 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2344 goto out_unlock;
2345
2346 ret = f2fs_convert_inline_inode(src);
2347 if (ret)
2348 goto out_unlock;
2349
2350 ret = f2fs_convert_inline_inode(dst);
2351 if (ret)
2352 goto out_unlock;
2353
2354 /* write out all dirty pages from offset */
2355 ret = filemap_write_and_wait_range(src->i_mapping,
2356 pos_in, pos_in + len);
2357 if (ret)
2358 goto out_unlock;
2359
2360 ret = filemap_write_and_wait_range(dst->i_mapping,
2361 pos_out, pos_out + len);
2362 if (ret)
2363 goto out_unlock;
2364
2365 f2fs_balance_fs(sbi, true);
2366 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002367 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2368 pos_out >> F2FS_BLKSIZE_BITS,
2369 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002370
2371 if (!ret) {
2372 if (dst_max_i_size)
2373 f2fs_i_size_write(dst, dst_max_i_size);
2374 else if (dst_osize != dst->i_size)
2375 f2fs_i_size_write(dst, dst_osize);
2376 }
2377 f2fs_unlock_op(sbi);
2378out_unlock:
Chao Yubb066642017-11-03 10:21:05 +08002379 if (src != dst) {
2380 up_write(&F2FS_I(dst)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002381 inode_unlock(dst);
Chao Yubb066642017-11-03 10:21:05 +08002382 }
Chao Yu20a3d612016-08-04 20:13:03 +08002383out:
Chao Yubb066642017-11-03 10:21:05 +08002384 up_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002385 inode_unlock(src);
2386 return ret;
2387}
2388
2389static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2390{
2391 struct f2fs_move_range range;
2392 struct fd dst;
2393 int err;
2394
2395 if (!(filp->f_mode & FMODE_READ) ||
2396 !(filp->f_mode & FMODE_WRITE))
2397 return -EBADF;
2398
2399 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2400 sizeof(range)))
2401 return -EFAULT;
2402
2403 dst = fdget(range.dst_fd);
2404 if (!dst.file)
2405 return -EBADF;
2406
2407 if (!(dst.file->f_mode & FMODE_WRITE)) {
2408 err = -EBADF;
2409 goto err_out;
2410 }
2411
2412 err = mnt_want_write_file(filp);
2413 if (err)
2414 goto err_out;
2415
2416 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2417 range.pos_out, range.len);
2418
2419 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002420 if (err)
2421 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002422
2423 if (copy_to_user((struct f2fs_move_range __user *)arg,
2424 &range, sizeof(range)))
2425 err = -EFAULT;
2426err_out:
2427 fdput(dst);
2428 return err;
2429}
2430
Jaegeuk Kime066b832017-04-13 15:17:00 -07002431static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2432{
2433 struct inode *inode = file_inode(filp);
2434 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2435 struct sit_info *sm = SIT_I(sbi);
2436 unsigned int start_segno = 0, end_segno = 0;
2437 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2438 struct f2fs_flush_device range;
2439 int ret;
2440
2441 if (!capable(CAP_SYS_ADMIN))
2442 return -EPERM;
2443
2444 if (f2fs_readonly(sbi->sb))
2445 return -EROFS;
2446
2447 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2448 sizeof(range)))
2449 return -EFAULT;
2450
2451 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2452 sbi->segs_per_sec != 1) {
2453 f2fs_msg(sbi->sb, KERN_WARNING,
2454 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2455 range.dev_num, sbi->s_ndevs,
2456 sbi->segs_per_sec);
2457 return -EINVAL;
2458 }
2459
2460 ret = mnt_want_write_file(filp);
2461 if (ret)
2462 return ret;
2463
2464 if (range.dev_num != 0)
2465 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2466 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2467
2468 start_segno = sm->last_victim[FLUSH_DEVICE];
2469 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2470 start_segno = dev_start_segno;
2471 end_segno = min(start_segno + range.segments, dev_end_segno);
2472
2473 while (start_segno < end_segno) {
2474 if (!mutex_trylock(&sbi->gc_mutex)) {
2475 ret = -EBUSY;
2476 goto out;
2477 }
2478 sm->last_victim[GC_CB] = end_segno + 1;
2479 sm->last_victim[GC_GREEDY] = end_segno + 1;
2480 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2481 ret = f2fs_gc(sbi, true, true, start_segno);
2482 if (ret == -EAGAIN)
2483 ret = 0;
2484 else if (ret < 0)
2485 break;
2486 start_segno++;
2487 }
2488out:
2489 mnt_drop_write_file(filp);
2490 return ret;
2491}
2492
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002493static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2494{
2495 struct inode *inode = file_inode(filp);
2496 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2497
2498 /* Must validate to set it with SQLite behavior in Android. */
2499 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2500
2501 return put_user(sb_feature, (u32 __user *)arg);
2502}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002503
Chao Yu2c1d0302017-07-29 00:32:52 +08002504#ifdef CONFIG_QUOTA
2505static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2506{
2507 struct inode *inode = file_inode(filp);
2508 struct f2fs_inode_info *fi = F2FS_I(inode);
2509 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2510 struct super_block *sb = sbi->sb;
2511 struct dquot *transfer_to[MAXQUOTAS] = {};
2512 struct page *ipage;
2513 kprojid_t kprojid;
2514 int err;
2515
2516 if (!f2fs_sb_has_project_quota(sb)) {
2517 if (projid != F2FS_DEF_PROJID)
2518 return -EOPNOTSUPP;
2519 else
2520 return 0;
2521 }
2522
2523 if (!f2fs_has_extra_attr(inode))
2524 return -EOPNOTSUPP;
2525
2526 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2527
2528 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2529 return 0;
2530
2531 err = mnt_want_write_file(filp);
2532 if (err)
2533 return err;
2534
2535 err = -EPERM;
2536 inode_lock(inode);
2537
2538 /* Is it quota file? Do not allow user to mess with it */
2539 if (IS_NOQUOTA(inode))
2540 goto out_unlock;
2541
2542 ipage = get_node_page(sbi, inode->i_ino);
2543 if (IS_ERR(ipage)) {
2544 err = PTR_ERR(ipage);
2545 goto out_unlock;
2546 }
2547
2548 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2549 i_projid)) {
2550 err = -EOVERFLOW;
2551 f2fs_put_page(ipage, 1);
2552 goto out_unlock;
2553 }
2554 f2fs_put_page(ipage, 1);
2555
2556 dquot_initialize(inode);
2557
2558 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2559 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2560 err = __dquot_transfer(inode, transfer_to);
2561 dqput(transfer_to[PRJQUOTA]);
2562 if (err)
2563 goto out_dirty;
2564 }
2565
2566 F2FS_I(inode)->i_projid = kprojid;
2567 inode->i_ctime = current_time(inode);
2568out_dirty:
2569 f2fs_mark_inode_dirty_sync(inode, true);
2570out_unlock:
2571 inode_unlock(inode);
2572 mnt_drop_write_file(filp);
2573 return err;
2574}
2575#else
2576static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2577{
2578 if (projid != F2FS_DEF_PROJID)
2579 return -EOPNOTSUPP;
2580 return 0;
2581}
2582#endif
2583
2584/* Transfer internal flags to xflags */
2585static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2586{
2587 __u32 xflags = 0;
2588
2589 if (iflags & FS_SYNC_FL)
2590 xflags |= FS_XFLAG_SYNC;
2591 if (iflags & FS_IMMUTABLE_FL)
2592 xflags |= FS_XFLAG_IMMUTABLE;
2593 if (iflags & FS_APPEND_FL)
2594 xflags |= FS_XFLAG_APPEND;
2595 if (iflags & FS_NODUMP_FL)
2596 xflags |= FS_XFLAG_NODUMP;
2597 if (iflags & FS_NOATIME_FL)
2598 xflags |= FS_XFLAG_NOATIME;
2599 if (iflags & FS_PROJINHERIT_FL)
2600 xflags |= FS_XFLAG_PROJINHERIT;
2601 return xflags;
2602}
2603
2604#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2605 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2606 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2607
2608/* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
2609#define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \
2610 FS_IMMUTABLE_FL | \
2611 FS_APPEND_FL | \
2612 FS_NODUMP_FL | \
2613 FS_NOATIME_FL | \
2614 FS_PROJINHERIT_FL)
2615
2616/* Transfer xflags flags to internal */
2617static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2618{
2619 unsigned long iflags = 0;
2620
2621 if (xflags & FS_XFLAG_SYNC)
2622 iflags |= FS_SYNC_FL;
2623 if (xflags & FS_XFLAG_IMMUTABLE)
2624 iflags |= FS_IMMUTABLE_FL;
2625 if (xflags & FS_XFLAG_APPEND)
2626 iflags |= FS_APPEND_FL;
2627 if (xflags & FS_XFLAG_NODUMP)
2628 iflags |= FS_NODUMP_FL;
2629 if (xflags & FS_XFLAG_NOATIME)
2630 iflags |= FS_NOATIME_FL;
2631 if (xflags & FS_XFLAG_PROJINHERIT)
2632 iflags |= FS_PROJINHERIT_FL;
2633
2634 return iflags;
2635}
2636
2637static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2638{
2639 struct inode *inode = file_inode(filp);
2640 struct f2fs_inode_info *fi = F2FS_I(inode);
2641 struct fsxattr fa;
2642
2643 memset(&fa, 0, sizeof(struct fsxattr));
2644 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
2645 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL));
2646
2647 if (f2fs_sb_has_project_quota(inode->i_sb))
2648 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2649 fi->i_projid);
2650
2651 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2652 return -EFAULT;
2653 return 0;
2654}
2655
2656static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2657{
2658 struct inode *inode = file_inode(filp);
2659 struct f2fs_inode_info *fi = F2FS_I(inode);
2660 struct fsxattr fa;
2661 unsigned int flags;
2662 int err;
2663
2664 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2665 return -EFAULT;
2666
2667 /* Make sure caller has proper permission */
2668 if (!inode_owner_or_capable(inode))
2669 return -EACCES;
2670
2671 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2672 return -EOPNOTSUPP;
2673
2674 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2675 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2676 return -EOPNOTSUPP;
2677
2678 err = mnt_want_write_file(filp);
2679 if (err)
2680 return err;
2681
2682 inode_lock(inode);
2683 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2684 (flags & F2FS_FL_XFLAG_VISIBLE);
2685 err = __f2fs_ioc_setflags(inode, flags);
2686 inode_unlock(inode);
2687 mnt_drop_write_file(filp);
2688 if (err)
2689 return err;
2690
2691 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2692 if (err)
2693 return err;
2694
2695 return 0;
2696}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002697
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002698int f2fs_pin_file_control(struct inode *inode, bool inc)
2699{
2700 struct f2fs_inode_info *fi = F2FS_I(inode);
2701 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2702
2703 /* Use i_gc_failures for normal file as a risk signal. */
2704 if (inc)
2705 f2fs_i_gc_failures_write(inode, fi->i_gc_failures + 1);
2706
2707 if (fi->i_gc_failures > sbi->gc_pin_file_threshold) {
2708 f2fs_msg(sbi->sb, KERN_WARNING,
2709 "%s: Enable GC = ino %lx after %x GC trials\n",
2710 __func__, inode->i_ino, fi->i_gc_failures);
2711 clear_inode_flag(inode, FI_PIN_FILE);
2712 return -EAGAIN;
2713 }
2714 return 0;
2715}
2716
2717static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2718{
2719 struct inode *inode = file_inode(filp);
2720 __u32 pin;
2721 int ret = 0;
2722
2723 if (!inode_owner_or_capable(inode))
2724 return -EACCES;
2725
2726 if (get_user(pin, (__u32 __user *)arg))
2727 return -EFAULT;
2728
2729 if (!S_ISREG(inode->i_mode))
2730 return -EINVAL;
2731
2732 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2733 return -EROFS;
2734
2735 ret = mnt_want_write_file(filp);
2736 if (ret)
2737 return ret;
2738
2739 inode_lock(inode);
2740
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002741 if (should_update_outplace(inode, NULL)) {
2742 ret = -EINVAL;
2743 goto out;
2744 }
2745
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002746 if (!pin) {
2747 clear_inode_flag(inode, FI_PIN_FILE);
2748 F2FS_I(inode)->i_gc_failures = 1;
2749 goto done;
2750 }
2751
2752 if (f2fs_pin_file_control(inode, false)) {
2753 ret = -EAGAIN;
2754 goto out;
2755 }
2756 ret = f2fs_convert_inline_inode(inode);
2757 if (ret)
2758 goto out;
2759
2760 set_inode_flag(inode, FI_PIN_FILE);
2761 ret = F2FS_I(inode)->i_gc_failures;
2762done:
2763 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2764out:
2765 inode_unlock(inode);
2766 mnt_drop_write_file(filp);
2767 return ret;
2768}
2769
2770static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2771{
2772 struct inode *inode = file_inode(filp);
2773 __u32 pin = 0;
2774
2775 if (is_inode_flag_set(inode, FI_PIN_FILE))
2776 pin = F2FS_I(inode)->i_gc_failures;
2777 return put_user(pin, (u32 __user *)arg);
2778}
2779
Chao Yuc4020b22018-01-11 14:42:30 +08002780int f2fs_precache_extents(struct inode *inode)
2781{
2782 struct f2fs_inode_info *fi = F2FS_I(inode);
2783 struct f2fs_map_blocks map;
2784 pgoff_t m_next_extent;
2785 loff_t end;
2786 int err;
2787
2788 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2789 return -EOPNOTSUPP;
2790
2791 map.m_lblk = 0;
2792 map.m_next_pgofs = NULL;
2793 map.m_next_extent = &m_next_extent;
2794 map.m_seg_type = NO_CHECK_TYPE;
2795 end = F2FS_I_SB(inode)->max_file_blocks;
2796
2797 while (map.m_lblk < end) {
2798 map.m_len = end - map.m_lblk;
2799
2800 down_write(&fi->dio_rwsem[WRITE]);
2801 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
2802 up_write(&fi->dio_rwsem[WRITE]);
2803 if (err)
2804 return err;
2805
2806 map.m_lblk = m_next_extent;
2807 }
2808
2809 return err;
2810}
2811
2812static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2813{
2814 return f2fs_precache_extents(file_inode(filp));
2815}
2816
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002817long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2818{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002819 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2820 return -EIO;
2821
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002822 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002823 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002824 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002825 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002826 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002827 case F2FS_IOC_GETVERSION:
2828 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002829 case F2FS_IOC_START_ATOMIC_WRITE:
2830 return f2fs_ioc_start_atomic_write(filp);
2831 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2832 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002833 case F2FS_IOC_START_VOLATILE_WRITE:
2834 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002835 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2836 return f2fs_ioc_release_volatile_write(filp);
2837 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2838 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002839 case F2FS_IOC_SHUTDOWN:
2840 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002841 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002842 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002843 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2844 return f2fs_ioc_set_encryption_policy(filp, arg);
2845 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2846 return f2fs_ioc_get_encryption_policy(filp, arg);
2847 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2848 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002849 case F2FS_IOC_GARBAGE_COLLECT:
2850 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002851 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2852 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002853 case F2FS_IOC_WRITE_CHECKPOINT:
2854 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002855 case F2FS_IOC_DEFRAGMENT:
2856 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002857 case F2FS_IOC_MOVE_RANGE:
2858 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002859 case F2FS_IOC_FLUSH_DEVICE:
2860 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002861 case F2FS_IOC_GET_FEATURES:
2862 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08002863 case F2FS_IOC_FSGETXATTR:
2864 return f2fs_ioc_fsgetxattr(filp, arg);
2865 case F2FS_IOC_FSSETXATTR:
2866 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002867 case F2FS_IOC_GET_PIN_FILE:
2868 return f2fs_ioc_get_pin_file(filp, arg);
2869 case F2FS_IOC_SET_PIN_FILE:
2870 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08002871 case F2FS_IOC_PRECACHE_EXTENTS:
2872 return f2fs_ioc_precache_extents(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002873 default:
2874 return -ENOTTY;
2875 }
2876}
2877
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002878static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2879{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002880 struct file *file = iocb->ki_filp;
2881 struct inode *inode = file_inode(file);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002882 struct blk_plug plug;
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002883 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002884
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002885 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2886 return -EIO;
2887
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002888 inode_lock(inode);
2889 ret = generic_write_checks(iocb, from);
2890 if (ret > 0) {
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002891 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002892
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002893 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2894 set_inode_flag(inode, FI_NO_PREALLOC);
2895
2896 err = f2fs_preallocate_blocks(iocb, from);
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002897 if (err) {
Chao Yu28cfafb2017-11-13 17:32:39 +08002898 clear_inode_flag(inode, FI_NO_PREALLOC);
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002899 inode_unlock(inode);
2900 return err;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002901 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002902 blk_start_plug(&plug);
2903 ret = __generic_file_write_iter(iocb, from);
2904 blk_finish_plug(&plug);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002905 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08002906
2907 if (ret > 0)
2908 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002909 }
2910 inode_unlock(inode);
2911
Christoph Hellwige2592212016-04-07 08:52:01 -07002912 if (ret > 0)
2913 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002914 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002915}
2916
Namjae Jeone9750822013-02-04 23:41:41 +09002917#ifdef CONFIG_COMPAT
2918long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2919{
2920 switch (cmd) {
2921 case F2FS_IOC32_GETFLAGS:
2922 cmd = F2FS_IOC_GETFLAGS;
2923 break;
2924 case F2FS_IOC32_SETFLAGS:
2925 cmd = F2FS_IOC_SETFLAGS;
2926 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08002927 case F2FS_IOC32_GETVERSION:
2928 cmd = F2FS_IOC_GETVERSION;
2929 break;
2930 case F2FS_IOC_START_ATOMIC_WRITE:
2931 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2932 case F2FS_IOC_START_VOLATILE_WRITE:
2933 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2934 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2935 case F2FS_IOC_SHUTDOWN:
2936 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2937 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2938 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2939 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002940 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08002941 case F2FS_IOC_WRITE_CHECKPOINT:
2942 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002943 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07002944 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002945 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08002946 case F2FS_IOC_FSGETXATTR:
2947 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002948 case F2FS_IOC_GET_PIN_FILE:
2949 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08002950 case F2FS_IOC_PRECACHE_EXTENTS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002951 break;
Namjae Jeone9750822013-02-04 23:41:41 +09002952 default:
2953 return -ENOIOCTLCMD;
2954 }
2955 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2956}
2957#endif
2958
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002959const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08002960 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04002961 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002962 .write_iter = f2fs_file_write_iter,
2963 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08002964 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002965 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002966 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002967 .fsync = f2fs_sync_file,
2968 .fallocate = f2fs_fallocate,
2969 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09002970#ifdef CONFIG_COMPAT
2971 .compat_ioctl = f2fs_compat_ioctl,
2972#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002973 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002974 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002975};