blob: 6880c6f78d58d0670bf28863d53de0c1e6afb3b0 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002 * fs/f2fs/file.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/stat.h>
14#include <linux/buffer_head.h>
15#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090016#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/falloc.h>
18#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090019#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090020#include <linux/uaccess.h>
21#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090022#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080023#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070024#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070025#include <linux/file.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
30#include "xattr.h"
31#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080032#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080033#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090034#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035
Souptick Joarderea4d4792018-04-15 01:40:02 +053036static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080037{
38 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053039 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080040
41 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053042 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080043 up_read(&F2FS_I(inode)->i_mmap_sem);
44
Souptick Joarderea4d4792018-04-15 01:40:02 +053045 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080046}
47
Souptick Joarderea4d4792018-04-15 01:40:02 +053048static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049{
50 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080051 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090053 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080054 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090055
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020056 if (unlikely(f2fs_cp_error(sbi))) {
57 err = -EIO;
58 goto err;
59 }
60
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090061 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070062
63 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba12014-08-07 16:32:25 -070064
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090065 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080066 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070067 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070068 err = f2fs_reserve_block(&dn, page->index);
69 if (err) {
70 f2fs_unlock_op(sbi);
71 goto out;
72 }
73 f2fs_put_dnode(&dn);
74 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090075
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -080076 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -080077
Dave Jiang11bac802017-02-24 14:56:41 -080078 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080079 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090080 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090081 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090082 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090083 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090084 unlock_page(page);
85 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080086 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090087 }
88
89 /*
90 * check to see if the page is mapped already (no holes)
91 */
92 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090093 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090094
95 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +080097 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +090098 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +090099
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300100 offset = i_size_read(inode) & ~PAGE_MASK;
101 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900102 }
103 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700104 if (!PageUptodate(page))
105 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900106
Chao Yub0af6d42017-08-02 23:21:48 +0800107 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
108
Jaegeuk Kime943a102013-10-25 14:26:31 +0900109 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900110mapped:
111 /* fill the page */
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800112 f2fs_wait_on_page_writeback(page, DATA, false);
Chao Yu08b39fb2015-10-08 13:27:34 +0800113
Eric Biggers6dbb1792018-04-18 11:09:48 -0700114 /* wait for GCed page writeback via META_MAPPING */
115 if (f2fs_post_read_required(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -0700116 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +0800117
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800118out_sem:
119 up_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900120out:
121 sb_end_pagefault(inode->i_sb);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800122 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200123err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900124 return block_page_mkwrite_return(err);
125}
126
127static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800128 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700129 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900130 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900131};
132
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900133static int get_parent_ino(struct inode *inode, nid_t *pino)
134{
135 struct dentry *dentry;
136
137 inode = igrab(inode);
138 dentry = d_find_any_alias(inode);
139 iput(inode);
140 if (!dentry)
141 return 0;
142
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900143 *pino = parent_ino(dentry);
144 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900145 return 1;
146}
147
Chao Yua5fd5052017-11-06 22:51:45 +0800148static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800149{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700150 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800151 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800152
Chao Yua5fd5052017-11-06 22:51:45 +0800153 if (!S_ISREG(inode->i_mode))
154 cp_reason = CP_NON_REGULAR;
155 else if (inode->i_nlink != 1)
156 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700157 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800158 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800159 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800160 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800161 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800162 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800163 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800164 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700165 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800166 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800167 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800168 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800169 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800170 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
171 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
172 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800173 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800174
Chao Yua5fd5052017-11-06 22:51:45 +0800175 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800176}
177
Changman Lee9c7bb702014-12-08 15:29:40 +0900178static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
179{
180 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
181 bool ret = false;
182 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800183 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900184 ret = true;
185 f2fs_put_page(i, 0);
186 return ret;
187}
188
Changman Lee51455b12014-12-08 15:29:41 +0900189static void try_to_fix_pino(struct inode *inode)
190{
191 struct f2fs_inode_info *fi = F2FS_I(inode);
192 nid_t pino;
193
194 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900195 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
196 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700197 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900198 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900199 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700200 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900201}
202
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700203static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
204 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205{
206 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700207 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700208 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900209 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800210 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900211 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900212 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900213 .nr_to_write = LONG_MAX,
214 .for_reclaim = 0,
215 };
216
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900217 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900218 return 0;
219
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900220 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700221
222 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800223 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700224 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400225 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700226 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700227
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900228 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800229 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900230 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900231 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900232
Changman Lee9c7bb702014-12-08 15:29:40 +0900233 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800234 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700235 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900236 goto go_write;
237 }
238
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700239 /*
240 * if there is no written data, don't waste time to write recovery info.
241 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700242 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800243 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700244
Changman Lee9c7bb702014-12-08 15:29:40 +0900245 /* it may call write_inode just prior to fsync */
246 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700247 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700248
Jaegeuk Kim91942322016-05-20 10:13:22 -0700249 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800250 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700251 goto flush_out;
252 goto out;
253 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700254go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900255 /*
256 * Both of fdatasync() and fsync() are able to be recovered from
257 * sudden-power-off.
258 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700259 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800260 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700261 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900262
Chao Yua5fd5052017-11-06 22:51:45 +0800263 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900264 /* all the dirty node pages should be flushed for POR */
265 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900266
Changman Lee51455b12014-12-08 15:29:41 +0900267 /*
268 * We've secured consistency through sync_fs. Following pino
269 * will be used only for fsynced inodes after checkpoint.
270 */
271 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700272 clear_inode_flag(inode, FI_APPEND_WRITE);
273 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900274 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900275 }
Changman Lee51455b12014-12-08 15:29:41 +0900276sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800277 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu4d57b862018-05-30 00:20:41 +0800278 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800279 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700280 if (ret)
281 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900282
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800283 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800284 if (unlikely(f2fs_cp_error(sbi))) {
285 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800286 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800287 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800288
Chao Yu4d57b862018-05-30 00:20:41 +0800289 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700290 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900291 f2fs_write_inode(inode, NULL);
292 goto sync_nodes;
293 }
294
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700295 /*
296 * If it's atomic_write, it's just fine to keep write ordering. So
297 * here we don't need to wait for node write completion, since we use
298 * node chain which serializes node blocks. If one of node writes are
299 * reordered, we can see simply broken chain, resulting in stopping
300 * roll-forward recovery. It means we'll recover all or none node blocks
301 * given fsync mark.
302 */
303 if (!atomic) {
Chao Yu4d57b862018-05-30 00:20:41 +0800304 ret = f2fs_wait_on_node_pages_writeback(sbi, ino);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700305 if (ret)
306 goto out;
307 }
Changman Lee51455b12014-12-08 15:29:41 +0900308
309 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800310 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700311 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900312flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700313 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800314 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800315 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800316 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800317 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800318 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800319 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800320 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900321out:
Chao Yua5fd5052017-11-06 22:51:45 +0800322 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700323 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900324 return ret;
325}
326
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700327int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
328{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200329 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
330 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700331 return f2fs_do_sync_file(file, start, end, datasync, false);
332}
333
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900334static pgoff_t __get_first_dirty_index(struct address_space *mapping,
335 pgoff_t pgofs, int whence)
336{
Jan Kara8faab642017-11-15 17:34:55 -0800337 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900338 int nr_pages;
339
340 if (whence != SEEK_DATA)
341 return 0;
342
343 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800344 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
345 1, &page);
346 if (!nr_pages)
347 return ULONG_MAX;
348 pgofs = page->index;
349 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900350 return pgofs;
351}
352
353static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
354 int whence)
355{
356 switch (whence) {
357 case SEEK_DATA:
358 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yu7b525dd2018-05-23 22:25:08 +0800359 is_valid_blkaddr(blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900360 return true;
361 break;
362 case SEEK_HOLE:
363 if (blkaddr == NULL_ADDR)
364 return true;
365 break;
366 }
367 return false;
368}
369
Chao Yu267378d2014-04-23 14:10:24 +0800370static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
371{
372 struct inode *inode = file->f_mapping->host;
373 loff_t maxbytes = inode->i_sb->s_maxbytes;
374 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900375 pgoff_t pgofs, end_offset, dirty;
376 loff_t data_ofs = offset;
377 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800378 int err = 0;
379
Al Viro59551022016-01-22 15:40:57 -0500380 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800381
382 isize = i_size_read(inode);
383 if (offset >= isize)
384 goto fail;
385
386 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800387 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800388 if (whence == SEEK_HOLE)
389 data_ofs = isize;
390 goto found;
391 }
392
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300393 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800394
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900395 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
396
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300397 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800398 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800399 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800400 if (err && err != -ENOENT) {
401 goto fail;
402 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900403 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800404 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800405 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800406 continue;
407 } else {
408 goto found;
409 }
410 }
411
Chao Yu81ca7352016-01-26 15:39:35 +0800412 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800413
414 /* find data/hole in dnode block */
415 for (; dn.ofs_in_node < end_offset;
416 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300417 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800418 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900419
Chao Yu7a2af762017-07-19 00:19:06 +0800420 blkaddr = datablock_addr(dn.inode,
421 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800422
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900423 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800424 f2fs_put_dnode(&dn);
425 goto found;
426 }
427 }
428 f2fs_put_dnode(&dn);
429 }
430
431 if (whence == SEEK_DATA)
432 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800433found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900434 if (whence == SEEK_HOLE && data_ofs > isize)
435 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500436 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800437 return vfs_setpos(file, data_ofs, maxbytes);
438fail:
Al Viro59551022016-01-22 15:40:57 -0500439 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800440 return -ENXIO;
441}
442
443static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
444{
445 struct inode *inode = file->f_mapping->host;
446 loff_t maxbytes = inode->i_sb->s_maxbytes;
447
448 switch (whence) {
449 case SEEK_SET:
450 case SEEK_CUR:
451 case SEEK_END:
452 return generic_file_llseek_size(file, offset, whence,
453 maxbytes, i_size_read(inode));
454 case SEEK_DATA:
455 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700456 if (offset < 0)
457 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800458 return f2fs_seek_block(file, offset, whence);
459 }
460
461 return -EINVAL;
462}
463
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900464static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
465{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700466 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800467 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700468
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200469 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
470 return -EIO;
471
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700472 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800473 err = f2fs_convert_inline_inode(inode);
474 if (err)
475 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700476
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900477 file_accessed(file);
478 vma->vm_ops = &f2fs_file_vm_ops;
479 return 0;
480}
481
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700482static int f2fs_file_open(struct inode *inode, struct file *filp)
483{
Eric Biggers2e168c82017-11-29 12:35:28 -0800484 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700485
Eric Biggers2e168c82017-11-29 12:35:28 -0800486 if (err)
487 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900488
489 filp->f_mode |= FMODE_NOWAIT;
490
Chao Yu0abd6752017-07-09 00:13:07 +0800491 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700492}
493
Chao Yu4d57b862018-05-30 00:20:41 +0800494void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900495{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700496 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900497 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800498 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900499 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800500 int base = 0;
501
502 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
503 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900504
Gu Zheng45590712013-07-15 17:57:38 +0800505 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800506 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900507
Chris Fries6c311ec2014-01-17 14:44:39 -0600508 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900509 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900510
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900511 if (blkaddr == NULL_ADDR)
512 continue;
513
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800514 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800515 f2fs_set_data_blkaddr(dn);
516 f2fs_invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700517 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700518 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900519 nr_free++;
520 }
Chao Yu19b2c302015-08-26 20:34:48 +0800521
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800523 pgoff_t fofs;
524 /*
525 * once we invalidate valid blkaddr in range [ofs, ofs + count],
526 * we will invalidate all blkaddr in the whole range.
527 */
Chao Yu4d57b862018-05-30 00:20:41 +0800528 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800529 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800530 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900531 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 }
533 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900534
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800535 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900536 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
537 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900538}
539
Chao Yu4d57b862018-05-30 00:20:41 +0800540void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900541{
Chao Yu4d57b862018-05-30 00:20:41 +0800542 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900543}
544
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800545static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700546 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900547{
youngjun yoo193bea12018-05-30 04:21:14 +0900548 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300549 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700550 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900551 struct page *page;
552
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700553 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700554 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900555
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700556 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700557 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700558 if (page && PageUptodate(page))
559 goto truncate_out;
560 f2fs_put_page(page, 1);
561 return 0;
562 }
563
Chao Yu4d57b862018-05-30 00:20:41 +0800564 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900565 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800566 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700567truncate_out:
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800568 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700570
571 /* An encrypted inode should have a key and truncate the last page. */
572 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
573 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800574 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900575 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700576 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900577}
578
Chao Yu4d57b862018-05-30 00:20:41 +0800579int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900580{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700581 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900582 struct dnode_of_data dn;
583 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800584 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700585 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800586 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900587
Namjae Jeon51dd6242013-04-20 01:28:52 +0900588 trace_f2fs_truncate_blocks_enter(inode, from);
589
Chao Yudf033ca2018-03-20 23:08:29 +0800590 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900591
Chao Yu09210c92016-05-05 19:13:03 +0800592 if (free_from >= sbi->max_file_blocks)
593 goto free_partial;
594
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700595 if (lock)
596 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800597
Chao Yu4d57b862018-05-30 00:20:41 +0800598 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700599 if (IS_ERR(ipage)) {
600 err = PTR_ERR(ipage);
601 goto out;
602 }
603
604 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800605 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700606 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800607 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700608 goto out;
609 }
610
611 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800612 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900613 if (err) {
614 if (err == -ENOENT)
615 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700616 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700617 }
618
Chao Yu81ca7352016-01-26 15:39:35 +0800619 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620
621 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700622 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900623
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900624 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800625 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900626 free_from += count;
627 }
628
629 f2fs_put_dnode(&dn);
630free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800631 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800632out:
633 if (lock)
634 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800635free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700636 /* lastly zero out the first data page */
637 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800638 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639
Namjae Jeon51dd6242013-04-20 01:28:52 +0900640 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900641 return err;
642}
643
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700644int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900645{
Chao Yub0154892015-08-24 17:39:42 +0800646 int err;
647
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200648 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
649 return -EIO;
650
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900651 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
652 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800653 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900654
Namjae Jeon51dd6242013-04-20 01:28:52 +0900655 trace_f2fs_truncate(inode);
656
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800657#ifdef CONFIG_F2FS_FAULT_INJECTION
658 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
659 f2fs_show_injection_info(FAULT_TRUNCATE);
660 return -EIO;
661 }
662#endif
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800663 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800664 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800665 err = f2fs_convert_inline_inode(inode);
666 if (err)
667 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800668 }
669
Chao Yu4d57b862018-05-30 00:20:41 +0800670 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800671 if (err)
672 return err;
673
Deepa Dinamani078cd822016-09-14 07:48:04 -0700674 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700675 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800676 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900677}
678
David Howellsa528d352017-01-31 16:46:22 +0000679int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800680 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900681{
David Howellsa528d352017-01-31 16:46:22 +0000682 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800683 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800684 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800685 unsigned int flags;
686
Chao Yu1c1d35d2018-01-25 14:54:42 +0800687 if (f2fs_has_extra_attr(inode) &&
688 f2fs_sb_has_inode_crtime(inode->i_sb) &&
689 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
690 stat->result_mask |= STATX_BTIME;
691 stat->btime.tv_sec = fi->i_crtime.tv_sec;
692 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
693 }
694
Chao Yuc807a7c2018-04-08 11:25:53 +0800695 flags = fi->i_flags & F2FS_FL_USER_VISIBLE;
Chao Yu59c84402018-04-03 15:08:17 +0800696 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800697 stat->attributes |= STATX_ATTR_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +0800698 if (flags & F2FS_COMPR_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800699 stat->attributes |= STATX_ATTR_COMPRESSED;
700 if (f2fs_encrypted_inode(inode))
701 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800702 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800703 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800704 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800705 stat->attributes |= STATX_ATTR_NODUMP;
706
707 stat->attributes_mask |= (STATX_ATTR_APPEND |
708 STATX_ATTR_COMPRESSED |
709 STATX_ATTR_ENCRYPTED |
710 STATX_ATTR_IMMUTABLE |
711 STATX_ATTR_NODUMP);
712
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900713 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700714
715 /* we need to show initial sectors used for inline_data/dentries */
716 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
717 f2fs_has_inline_dentry(inode))
718 stat->blocks += (stat->size + 511) >> 9;
719
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900720 return 0;
721}
722
723#ifdef CONFIG_F2FS_FS_POSIX_ACL
724static void __setattr_copy(struct inode *inode, const struct iattr *attr)
725{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726 unsigned int ia_valid = attr->ia_valid;
727
728 if (ia_valid & ATTR_UID)
729 inode->i_uid = attr->ia_uid;
730 if (ia_valid & ATTR_GID)
731 inode->i_gid = attr->ia_gid;
732 if (ia_valid & ATTR_ATIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700733 inode->i_atime = timespec64_trunc(attr->ia_atime,
734 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900735 if (ia_valid & ATTR_MTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700736 inode->i_mtime = timespec64_trunc(attr->ia_mtime,
737 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900738 if (ia_valid & ATTR_CTIME)
Deepa Dinamani95582b02018-05-08 19:36:02 -0700739 inode->i_ctime = timespec64_trunc(attr->ia_ctime,
740 inode->i_sb->s_time_gran);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900741 if (ia_valid & ATTR_MODE) {
742 umode_t mode = attr->ia_mode;
743
744 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
745 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700746 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900747 }
748}
749#else
750#define __setattr_copy setattr_copy
751#endif
752
753int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
754{
David Howells2b0143b2015-03-17 22:25:59 +0000755 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900756 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800757 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900758
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200759 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
760 return -EIO;
761
Jan Kara31051c82016-05-26 16:55:18 +0200762 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900763 if (err)
764 return err;
765
Eric Biggers20bb2472017-11-29 12:35:32 -0800766 err = fscrypt_prepare_setattr(dentry, attr);
767 if (err)
768 return err;
769
Chao Yu0abd6752017-07-09 00:13:07 +0800770 if (is_quota_modification(inode, attr)) {
771 err = dquot_initialize(inode);
772 if (err)
773 return err;
774 }
775 if ((attr->ia_valid & ATTR_UID &&
776 !uid_eq(attr->ia_uid, inode->i_uid)) ||
777 (attr->ia_valid & ATTR_GID &&
778 !gid_eq(attr->ia_gid, inode->i_gid))) {
779 err = dquot_transfer(inode, attr);
780 if (err)
781 return err;
782 }
783
Chao Yu09db6a22014-09-15 18:02:09 +0800784 if (attr->ia_valid & ATTR_SIZE) {
Chao Yu3c454142015-06-05 18:34:02 +0800785 if (attr->ia_size <= i_size_read(inode)) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800786 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu09db6a22014-09-15 18:02:09 +0800787 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700788 err = f2fs_truncate(inode);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800789 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub0154892015-08-24 17:39:42 +0800790 if (err)
791 return err;
Chao Yu09db6a22014-09-15 18:02:09 +0800792 } else {
793 /*
Chao Yu3c454142015-06-05 18:34:02 +0800794 * do not trim all blocks after i_size if target size is
795 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800796 */
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800797 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3c454142015-06-05 18:34:02 +0800798 truncate_setsize(inode, attr->ia_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800799 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu0cab80e2015-12-01 11:36:16 +0800800
801 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800802 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800803 err = f2fs_convert_inline_inode(inode);
804 if (err)
805 return err;
806 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700807 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800808 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800809
Chao Yua0d00fa2017-10-09 17:55:19 +0800810 down_write(&F2FS_I(inode)->i_sem);
811 F2FS_I(inode)->last_disk_size = i_size_read(inode);
812 up_write(&F2FS_I(inode)->i_sem);
813
Yunlei Hec0ed4402016-12-11 15:35:15 +0800814 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900815 }
816
817 __setattr_copy(inode, attr);
818
819 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800820 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700821 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
822 inode->i_mode = F2FS_I(inode)->i_acl_mode;
823 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900824 }
825 }
826
Yunlei Hec0ed4402016-12-11 15:35:15 +0800827 /* file size may changed here */
828 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700829
830 /* inode change will produce dirty node pages flushed by checkpoint */
831 f2fs_balance_fs(F2FS_I_SB(inode), true);
832
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900833 return err;
834}
835
836const struct inode_operations f2fs_file_inode_operations = {
837 .getattr = f2fs_getattr,
838 .setattr = f2fs_setattr,
839 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800840 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900841#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900842 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900843#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900844 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900845};
846
Chao Yu63943282015-08-07 18:36:06 +0800847static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900848 loff_t start, loff_t len)
849{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700850 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900851 struct page *page;
852
853 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800854 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900855
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800856 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900857
Gu Zhenge4795562013-09-27 18:08:30 +0800858 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800859 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800860 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900861
Chao Yu63943282015-08-07 18:36:06 +0800862 if (IS_ERR(page))
863 return PTR_ERR(page);
864
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800865 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800866 zero_user(page, start, len);
867 set_page_dirty(page);
868 f2fs_put_page(page, 1);
869 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900870}
871
Chao Yu4d57b862018-05-30 00:20:41 +0800872int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900873{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900874 int err;
875
Chao Yuea587112015-09-17 20:22:44 +0800876 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900877 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800878 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900879
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900880 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800881 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900882 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800883 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +0800884 pg_start = f2fs_get_next_page_offset(&dn,
885 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900886 continue;
Chao Yuea587112015-09-17 20:22:44 +0800887 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900888 return err;
889 }
890
Chao Yu81ca7352016-01-26 15:39:35 +0800891 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800892 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
893
894 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
895
Chao Yu4d57b862018-05-30 00:20:41 +0800896 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900897 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800898
899 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900900 }
901 return 0;
902}
903
Chao Yua66c7b22013-11-22 16:52:50 +0800904static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900905{
906 pgoff_t pg_start, pg_end;
907 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800908 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900909
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800910 ret = f2fs_convert_inline_inode(inode);
911 if (ret)
912 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300914 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
915 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900916
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300917 off_start = offset & (PAGE_SIZE - 1);
918 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900919
920 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800921 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900922 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800923 if (ret)
924 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900925 } else {
Chao Yu63943282015-08-07 18:36:06 +0800926 if (off_start) {
927 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300928 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800929 if (ret)
930 return ret;
931 }
932 if (off_end) {
933 ret = fill_zero(inode, pg_end, 0, off_end);
934 if (ret)
935 return ret;
936 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900937
938 if (pg_start < pg_end) {
939 struct address_space *mapping = inode->i_mapping;
940 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700941 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500942
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800943 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900944
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300945 blk_start = (loff_t)pg_start << PAGE_SHIFT;
946 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800947 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900948 truncate_inode_pages_range(mapping, blk_start,
949 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900950
Gu Zhenge4795562013-09-27 18:08:30 +0800951 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800952 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800953 f2fs_unlock_op(sbi);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800954 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900955 }
956 }
957
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900958 return ret;
959}
960
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700961static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
962 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800963{
964 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
965 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700966 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700967
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700968next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700969 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800970 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700971 if (ret && ret != -ENOENT) {
972 return ret;
973 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700974 if (dn.max_level == 0)
975 return -ENOENT;
976 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
977 blkaddr += done;
978 do_replace += done;
979 goto next;
980 }
981
982 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
983 dn.ofs_in_node, len);
984 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800985 *blkaddr = datablock_addr(dn.inode,
986 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +0800987 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700988
989 if (test_opt(sbi, LFS)) {
990 f2fs_put_dnode(&dn);
991 return -ENOTSUPP;
992 }
993
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700994 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +0800995 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700996 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700997 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700998 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700999 f2fs_put_dnode(&dn);
1000next:
1001 len -= done;
1002 off += done;
1003 if (len)
1004 goto next_dnode;
1005 return 0;
1006}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001007
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001008static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1009 int *do_replace, pgoff_t off, int len)
1010{
1011 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1012 struct dnode_of_data dn;
1013 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001014
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001015 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1016 if (*do_replace == 0)
1017 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001018
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001019 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001020 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001021 if (ret) {
1022 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001023 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001024 } else {
1025 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001026 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001027 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001028 }
1029 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001030}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001031
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001032static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1033 block_t *blkaddr, int *do_replace,
1034 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1035{
1036 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1037 pgoff_t i = 0;
1038 int ret;
1039
1040 while (i < len) {
1041 if (blkaddr[i] == NULL_ADDR && !full) {
1042 i++;
1043 continue;
1044 }
1045
1046 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1047 struct dnode_of_data dn;
1048 struct node_info ni;
1049 size_t new_size;
1050 pgoff_t ilen;
1051
1052 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001053 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001054 if (ret)
1055 return ret;
1056
Chao Yu4d57b862018-05-30 00:20:41 +08001057 f2fs_get_node_info(sbi, dn.nid, &ni);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001058 ilen = min((pgoff_t)
1059 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1060 dn.ofs_in_node, len - i);
1061 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001062 dn.data_blkaddr = datablock_addr(dn.inode,
1063 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001064 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001065
1066 if (do_replace[i]) {
1067 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001068 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001069 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001070 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001071 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1072 blkaddr[i], ni.version, true, false);
1073
1074 do_replace[i] = 0;
1075 }
1076 dn.ofs_in_node++;
1077 i++;
1078 new_size = (dst + i) << PAGE_SHIFT;
1079 if (dst_inode->i_size < new_size)
1080 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001081 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001082
1083 f2fs_put_dnode(&dn);
1084 } else {
1085 struct page *psrc, *pdst;
1086
Chao Yu4d57b862018-05-30 00:20:41 +08001087 psrc = f2fs_get_lock_data_page(src_inode,
1088 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001089 if (IS_ERR(psrc))
1090 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001091 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001092 true);
1093 if (IS_ERR(pdst)) {
1094 f2fs_put_page(psrc, 1);
1095 return PTR_ERR(pdst);
1096 }
1097 f2fs_copy_page(psrc, pdst);
1098 set_page_dirty(pdst);
1099 f2fs_put_page(pdst, 1);
1100 f2fs_put_page(psrc, 1);
1101
Chao Yu4d57b862018-05-30 00:20:41 +08001102 ret = f2fs_truncate_hole(src_inode,
1103 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001104 if (ret)
1105 return ret;
1106 i++;
1107 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001108 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001109 return 0;
1110}
1111
1112static int __exchange_data_block(struct inode *src_inode,
1113 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001114 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001115{
1116 block_t *src_blkaddr;
1117 int *do_replace;
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001118 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001119 int ret;
1120
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001121 while (len) {
1122 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001123
Chao Yu628b3d12017-11-30 19:28:18 +08001124 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001125 array_size(olen, sizeof(block_t)),
1126 GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001127 if (!src_blkaddr)
1128 return -ENOMEM;
1129
Chao Yu628b3d12017-11-30 19:28:18 +08001130 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001131 array_size(olen, sizeof(int)),
1132 GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001133 if (!do_replace) {
1134 kvfree(src_blkaddr);
1135 return -ENOMEM;
1136 }
1137
1138 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1139 do_replace, src, olen);
1140 if (ret)
1141 goto roll_back;
1142
1143 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1144 do_replace, src, dst, olen, full);
1145 if (ret)
1146 goto roll_back;
1147
1148 src += olen;
1149 dst += olen;
1150 len -= olen;
1151
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001152 kvfree(src_blkaddr);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001153 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001154 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001155 return 0;
1156
1157roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001158 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001159 kvfree(src_blkaddr);
1160 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001161 return ret;
1162}
1163
1164static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1165{
1166 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001167 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001168 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001169
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001170 f2fs_balance_fs(sbi, true);
1171 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001172
1173 f2fs_drop_extent_tree(inode);
1174
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001175 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1176 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +08001177 return ret;
1178}
1179
1180static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1181{
1182 pgoff_t pg_start, pg_end;
1183 loff_t new_size;
1184 int ret;
1185
Chao Yub4ace332015-05-06 13:09:46 +08001186 if (offset + len >= i_size_read(inode))
1187 return -EINVAL;
1188
1189 /* collapse range should be aligned to block size of f2fs. */
1190 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1191 return -EINVAL;
1192
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001193 ret = f2fs_convert_inline_inode(inode);
1194 if (ret)
1195 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001196
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001197 pg_start = offset >> PAGE_SHIFT;
1198 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yub4ace332015-05-06 13:09:46 +08001199
Chao Yu21020812017-11-23 23:26:52 +08001200 /* avoid gc operation during block exchange */
Chao Yub2532c62018-04-24 10:55:28 +08001201 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu21020812017-11-23 23:26:52 +08001202
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001203 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001204 /* write out all dirty pages from offset */
1205 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1206 if (ret)
Chao Yu21020812017-11-23 23:26:52 +08001207 goto out_unlock;
Chao Yubb066642017-11-03 10:21:05 +08001208
Chao Yub4ace332015-05-06 13:09:46 +08001209 truncate_pagecache(inode, offset);
1210
1211 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1212 if (ret)
Chao Yubb066642017-11-03 10:21:05 +08001213 goto out_unlock;
Chao Yub4ace332015-05-06 13:09:46 +08001214
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001215 /* write out all moved pages, if possible */
1216 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1217 truncate_pagecache(inode, offset);
1218
Chao Yub4ace332015-05-06 13:09:46 +08001219 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001220 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001221
Chao Yu4d57b862018-05-30 00:20:41 +08001222 ret = f2fs_truncate_blocks(inode, new_size, true);
Chao Yub4ace332015-05-06 13:09:46 +08001223 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001224 f2fs_i_size_write(inode, new_size);
Chao Yubb066642017-11-03 10:21:05 +08001225out_unlock:
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001226 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub2532c62018-04-24 10:55:28 +08001227 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001228 return ret;
1229}
1230
Chao Yu6e961942016-05-09 19:56:31 +08001231static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1232 pgoff_t end)
1233{
1234 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1235 pgoff_t index = start;
1236 unsigned int ofs_in_node = dn->ofs_in_node;
1237 blkcnt_t count = 0;
1238 int ret;
1239
1240 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001241 if (datablock_addr(dn->inode, dn->node_page,
1242 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001243 count++;
1244 }
1245
1246 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001247 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001248 if (ret)
1249 return ret;
1250
1251 dn->ofs_in_node = ofs_in_node;
1252 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001253 dn->data_blkaddr = datablock_addr(dn->inode,
1254 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001255 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001256 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001257 * allocation.
1258 */
1259 if (dn->data_blkaddr == NULL_ADDR) {
1260 ret = -ENOSPC;
1261 break;
1262 }
1263 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001264 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001265 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001266 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001267 }
1268 }
1269
1270 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1271
1272 return ret;
1273}
1274
Chao Yu75cd4e02015-05-06 13:11:13 +08001275static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1276 int mode)
1277{
1278 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1279 struct address_space *mapping = inode->i_mapping;
1280 pgoff_t index, pg_start, pg_end;
1281 loff_t new_size = i_size_read(inode);
1282 loff_t off_start, off_end;
1283 int ret = 0;
1284
Chao Yu75cd4e02015-05-06 13:11:13 +08001285 ret = inode_newsize_ok(inode, (len + offset));
1286 if (ret)
1287 return ret;
1288
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001289 ret = f2fs_convert_inline_inode(inode);
1290 if (ret)
1291 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001292
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001293 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001294 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1295 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001296 goto out_sem;
Chao Yu75cd4e02015-05-06 13:11:13 +08001297
1298 truncate_pagecache_range(inode, offset, offset + len - 1);
1299
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001300 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1301 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001302
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001303 off_start = offset & (PAGE_SIZE - 1);
1304 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001305
1306 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001307 ret = fill_zero(inode, pg_start, off_start,
1308 off_end - off_start);
1309 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001310 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001311
Chao Yu75cd4e02015-05-06 13:11:13 +08001312 new_size = max_t(loff_t, new_size, offset + len);
1313 } else {
1314 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001315 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001316 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001317 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001318 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001319
Chao Yu75cd4e02015-05-06 13:11:13 +08001320 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001321 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001322 }
1323
Chao Yu6e961942016-05-09 19:56:31 +08001324 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001325 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001326 unsigned int end_offset;
1327 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001328
1329 f2fs_lock_op(sbi);
1330
Chao Yu6e961942016-05-09 19:56:31 +08001331 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001332 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001333 if (ret) {
1334 f2fs_unlock_op(sbi);
1335 goto out;
1336 }
1337
Chao Yu6e961942016-05-09 19:56:31 +08001338 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1339 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1340
1341 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001342 f2fs_put_dnode(&dn);
1343 f2fs_unlock_op(sbi);
Chao Yu9434fcd2016-10-11 22:57:02 +08001344
1345 f2fs_balance_fs(sbi, dn.node_changed);
1346
Chao Yu6e961942016-05-09 19:56:31 +08001347 if (ret)
1348 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001349
Chao Yu6e961942016-05-09 19:56:31 +08001350 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001351 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001352 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001353 }
1354
1355 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001356 ret = fill_zero(inode, pg_end, 0, off_end);
1357 if (ret)
1358 goto out;
1359
Chao Yu75cd4e02015-05-06 13:11:13 +08001360 new_size = max_t(loff_t, new_size, offset + len);
1361 }
1362 }
1363
1364out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001365 if (new_size > i_size_read(inode)) {
1366 if (mode & FALLOC_FL_KEEP_SIZE)
1367 file_set_keep_isize(inode);
1368 else
1369 f2fs_i_size_write(inode, new_size);
1370 }
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001371out_sem:
1372 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001373
1374 return ret;
1375}
1376
Chao Yuf62185d2015-05-28 19:16:57 +08001377static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1378{
1379 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001380 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001381 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001382 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001383
Chao Yuf62185d2015-05-28 19:16:57 +08001384 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001385 ret = inode_newsize_ok(inode, new_size);
1386 if (ret)
1387 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001388
1389 if (offset >= i_size_read(inode))
1390 return -EINVAL;
1391
1392 /* insert range should be aligned to block size of f2fs. */
1393 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1394 return -EINVAL;
1395
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001396 ret = f2fs_convert_inline_inode(inode);
1397 if (ret)
1398 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001399
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001400 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001401
Chao Yu21020812017-11-23 23:26:52 +08001402 /* avoid gc operation during block exchange */
Chao Yub2532c62018-04-24 10:55:28 +08001403 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu21020812017-11-23 23:26:52 +08001404
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001405 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu4d57b862018-05-30 00:20:41 +08001406 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yuf62185d2015-05-28 19:16:57 +08001407 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001408 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001409
1410 /* write out all dirty pages from offset */
1411 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1412 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001413 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001414
1415 truncate_pagecache(inode, offset);
1416
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001417 pg_start = offset >> PAGE_SHIFT;
1418 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001419 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001420 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001421
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001422 while (!ret && idx > pg_start) {
1423 nr = idx - pg_start;
1424 if (nr > delta)
1425 nr = delta;
1426 idx -= nr;
1427
Chao Yuf62185d2015-05-28 19:16:57 +08001428 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001429 f2fs_drop_extent_tree(inode);
1430
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001431 ret = __exchange_data_block(inode, inode, idx,
1432 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001433 f2fs_unlock_op(sbi);
1434 }
1435
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001436 /* write out all moved pages, if possible */
1437 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1438 truncate_pagecache(inode, offset);
1439
1440 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001441 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001442out:
1443 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub2532c62018-04-24 10:55:28 +08001444 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001445 return ret;
1446}
1447
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001448static int expand_inode_data(struct inode *inode, loff_t offset,
1449 loff_t len, int mode)
1450{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001451 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001452 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuc4020b22018-01-11 14:42:30 +08001453 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001454 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001455 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001456 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001457 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001458
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001459 err = inode_newsize_ok(inode, (len + offset));
1460 if (err)
1461 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001462
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001463 err = f2fs_convert_inline_inode(inode);
1464 if (err)
1465 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001466
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001467 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001468
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001469 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001470 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001471
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001472 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1473 map.m_len = pg_end - map.m_lblk;
1474 if (off_end)
1475 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001476
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001477 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1478 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001479 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001480
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001481 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001482 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001483
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001484 last_off = map.m_lblk + map.m_len - 1;
1485
1486 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001487 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001488 (loff_t)(last_off + 1) << PAGE_SHIFT;
1489 } else {
1490 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001491 }
1492
Chao Yue8ed90a2017-11-05 21:53:30 +08001493 if (new_size > i_size_read(inode)) {
1494 if (mode & FALLOC_FL_KEEP_SIZE)
1495 file_set_keep_isize(inode);
1496 else
1497 f2fs_i_size_write(inode, new_size);
1498 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001499
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001500 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001501}
1502
1503static long f2fs_fallocate(struct file *file, int mode,
1504 loff_t offset, loff_t len)
1505{
Al Viro6131ffa2013-02-27 16:59:05 -05001506 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001507 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001508
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001509 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1510 return -EIO;
1511
Chao Yuc9980122015-09-11 14:39:02 +08001512 /* f2fs only support ->fallocate for regular file */
1513 if (!S_ISREG(inode->i_mode))
1514 return -EINVAL;
1515
Chao Yuf62185d2015-05-28 19:16:57 +08001516 if (f2fs_encrypted_inode(inode) &&
1517 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001518 return -EOPNOTSUPP;
1519
Chao Yub4ace332015-05-06 13:09:46 +08001520 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001521 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1522 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001523 return -EOPNOTSUPP;
1524
Al Viro59551022016-01-22 15:40:57 -05001525 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001526
Taehee Yoo587c0a42015-04-21 15:59:12 +09001527 if (mode & FALLOC_FL_PUNCH_HOLE) {
1528 if (offset >= inode->i_size)
1529 goto out;
1530
Chao Yua66c7b22013-11-22 16:52:50 +08001531 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001532 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1533 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001534 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1535 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001536 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1537 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001538 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001539 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001540 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001541
Namjae Jeon3af60a42012-12-30 14:52:37 +09001542 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001543 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001544 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001545 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001546 }
Chao Yu3375f692014-01-28 10:29:26 +08001547
Taehee Yoo587c0a42015-04-21 15:59:12 +09001548out:
Al Viro59551022016-01-22 15:40:57 -05001549 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001550
Namjae Jeonc01e2852013-04-23 17:00:52 +09001551 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001552 return ret;
1553}
1554
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001555static int f2fs_release_file(struct inode *inode, struct file *filp)
1556{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001557 /*
1558 * f2fs_relase_file is called at every close calls. So we should
1559 * not drop any inmemory pages by close called by other process.
1560 */
1561 if (!(filp->f_mode & FMODE_WRITE) ||
1562 atomic_read(&inode->i_writecount) != 1)
1563 return 0;
1564
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001565 /* some remained atomic pages should discarded */
1566 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001567 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001568 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001569 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001570 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001571 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001572 clear_inode_flag(inode, FI_VOLATILE_FILE);
1573 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001574 }
1575 return 0;
1576}
1577
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001578static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001579{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001580 struct inode *inode = file_inode(file);
1581
1582 /*
1583 * If the process doing a transaction is crashed, we should do
1584 * roll-back. Otherwise, other reader/write can see corrupted database
1585 * until all the writers close its file. Since this should be done
1586 * before dropping file lock, it needs to do in ->flush.
1587 */
1588 if (f2fs_is_atomic_file(inode) &&
1589 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001590 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001591 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001592}
1593
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001594static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001595{
Al Viro6131ffa2013-02-27 16:59:05 -05001596 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001597 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu76481152018-04-08 11:27:14 +08001598 unsigned int flags = fi->i_flags;
1599
1600 if (file_is_encrypt(inode))
1601 flags |= F2FS_ENCRYPT_FL;
1602 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1603 flags |= F2FS_INLINE_DATA_FL;
1604
1605 flags &= F2FS_FL_USER_VISIBLE;
1606
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001607 return put_user(flags, (int __user *)arg);
1608}
1609
Chao Yu2c1d0302017-07-29 00:32:52 +08001610static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1611{
1612 struct f2fs_inode_info *fi = F2FS_I(inode);
1613 unsigned int oldflags;
1614
1615 /* Is it quota file? Do not allow user to mess with it */
1616 if (IS_NOQUOTA(inode))
1617 return -EPERM;
1618
1619 flags = f2fs_mask_flags(inode->i_mode, flags);
1620
1621 oldflags = fi->i_flags;
1622
Chao Yu59c84402018-04-03 15:08:17 +08001623 if ((flags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
Chao Yu2c1d0302017-07-29 00:32:52 +08001624 if (!capable(CAP_LINUX_IMMUTABLE))
1625 return -EPERM;
1626
Chao Yuc807a7c2018-04-08 11:25:53 +08001627 flags = flags & F2FS_FL_USER_MODIFIABLE;
1628 flags |= oldflags & ~F2FS_FL_USER_MODIFIABLE;
Chao Yu2c1d0302017-07-29 00:32:52 +08001629 fi->i_flags = flags;
1630
Chao Yu59c84402018-04-03 15:08:17 +08001631 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001632 set_inode_flag(inode, FI_PROJ_INHERIT);
1633 else
1634 clear_inode_flag(inode, FI_PROJ_INHERIT);
1635
1636 inode->i_ctime = current_time(inode);
1637 f2fs_set_inode_flags(inode);
1638 f2fs_mark_inode_dirty_sync(inode, false);
1639 return 0;
1640}
1641
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001642static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1643{
1644 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001645 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001646 int ret;
1647
Chao Yu7fb17fe2016-05-09 19:56:32 +08001648 if (!inode_owner_or_capable(inode))
1649 return -EACCES;
1650
1651 if (get_user(flags, (int __user *)arg))
1652 return -EFAULT;
1653
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001654 ret = mnt_want_write_file(filp);
1655 if (ret)
1656 return ret;
1657
Al Viro59551022016-01-22 15:40:57 -05001658 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001659
Chao Yu2c1d0302017-07-29 00:32:52 +08001660 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001661
Chao Yua72d4b92017-05-03 23:59:13 +08001662 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001663 mnt_drop_write_file(filp);
1664 return ret;
1665}
1666
Chao Yud49f3e82015-01-23 20:36:04 +08001667static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1668{
1669 struct inode *inode = file_inode(filp);
1670
1671 return put_user(inode->i_generation, (int __user *)arg);
1672}
1673
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001674static int f2fs_ioc_start_atomic_write(struct file *filp)
1675{
1676 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001677 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001678
1679 if (!inode_owner_or_capable(inode))
1680 return -EACCES;
1681
Jaegeuk Kime8118982017-03-17 10:04:15 +08001682 if (!S_ISREG(inode->i_mode))
1683 return -EINVAL;
1684
Chao Yu7fb17fe2016-05-09 19:56:32 +08001685 ret = mnt_want_write_file(filp);
1686 if (ret)
1687 return ret;
1688
Chao Yu0fac5582016-05-09 19:56:33 +08001689 inode_lock(inode);
1690
Chao Yub2532c62018-04-24 10:55:28 +08001691 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu27319ba2018-04-17 17:51:28 +08001692
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001693 if (f2fs_is_atomic_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001694 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001695
Chao Yuf4c9c742015-07-17 18:06:35 +08001696 ret = f2fs_convert_inline_inode(inode);
1697 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001698 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001699
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001700 if (!get_dirty_pages(inode))
Yunlei He054afda2018-04-18 11:06:39 +08001701 goto skip_flush;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001702
1703 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001704 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001705 inode->i_ino, get_dirty_pages(inode));
1706 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Yunlei He054afda2018-04-18 11:06:39 +08001707 if (ret)
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001708 goto out;
Yunlei He054afda2018-04-18 11:06:39 +08001709skip_flush:
Yunlei He054afda2018-04-18 11:06:39 +08001710 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001711 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Yunlei He054afda2018-04-18 11:06:39 +08001712 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001713
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001714 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001715 stat_inc_atomic_write(inode);
1716 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001717out:
Chao Yub2532c62018-04-24 10:55:28 +08001718 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu0fac5582016-05-09 19:56:33 +08001719 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001720 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001721 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001722}
1723
1724static int f2fs_ioc_commit_atomic_write(struct file *filp)
1725{
1726 struct inode *inode = file_inode(filp);
1727 int ret;
1728
1729 if (!inode_owner_or_capable(inode))
1730 return -EACCES;
1731
1732 ret = mnt_want_write_file(filp);
1733 if (ret)
1734 return ret;
1735
Chao Yu0fac5582016-05-09 19:56:33 +08001736 inode_lock(inode);
1737
Chao Yub2532c62018-04-24 10:55:28 +08001738 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu1dc0f892018-02-27 22:45:24 +08001739
Chao Yub169c3c2018-04-18 17:45:02 +08001740 if (f2fs_is_volatile_file(inode)) {
1741 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001742 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08001743 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001744
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001745 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08001746 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001747 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001748 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001749
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001750 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001751 if (!ret) {
1752 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08001753 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
Chao Yu5fe45742017-01-07 18:50:26 +08001754 stat_dec_atomic_write(inode);
1755 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001756 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001757 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001758 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001759err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08001760 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1761 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1762 ret = -EINVAL;
1763 }
Chao Yub2532c62018-04-24 10:55:28 +08001764 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu0fac5582016-05-09 19:56:33 +08001765 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001766 mnt_drop_write_file(filp);
1767 return ret;
1768}
1769
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001770static int f2fs_ioc_start_volatile_write(struct file *filp)
1771{
1772 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001773 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001774
1775 if (!inode_owner_or_capable(inode))
1776 return -EACCES;
1777
Chao Yu8ff09712017-03-17 15:43:57 +08001778 if (!S_ISREG(inode->i_mode))
1779 return -EINVAL;
1780
Chao Yu7fb17fe2016-05-09 19:56:32 +08001781 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001782 if (ret)
1783 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001784
Chao Yu0fac5582016-05-09 19:56:33 +08001785 inode_lock(inode);
1786
Chao Yu7fb17fe2016-05-09 19:56:32 +08001787 if (f2fs_is_volatile_file(inode))
1788 goto out;
1789
1790 ret = f2fs_convert_inline_inode(inode);
1791 if (ret)
1792 goto out;
1793
Chao Yu648d50b2017-03-22 17:23:45 +08001794 stat_inc_volatile_write(inode);
1795 stat_update_max_volatile_write(inode);
1796
Jaegeuk Kim91942322016-05-20 10:13:22 -07001797 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001798 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001799out:
Chao Yu0fac5582016-05-09 19:56:33 +08001800 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001801 mnt_drop_write_file(filp);
1802 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001803}
1804
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001805static int f2fs_ioc_release_volatile_write(struct file *filp)
1806{
1807 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001808 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001809
1810 if (!inode_owner_or_capable(inode))
1811 return -EACCES;
1812
Chao Yu7fb17fe2016-05-09 19:56:32 +08001813 ret = mnt_want_write_file(filp);
1814 if (ret)
1815 return ret;
1816
Chao Yu0fac5582016-05-09 19:56:33 +08001817 inode_lock(inode);
1818
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001819 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001820 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001821
Chao Yu7fb17fe2016-05-09 19:56:32 +08001822 if (!f2fs_is_first_block_written(inode)) {
1823 ret = truncate_partial_data_page(inode, 0, true);
1824 goto out;
1825 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001826
Chao Yu7fb17fe2016-05-09 19:56:32 +08001827 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1828out:
Chao Yu0fac5582016-05-09 19:56:33 +08001829 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001830 mnt_drop_write_file(filp);
1831 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001832}
1833
1834static int f2fs_ioc_abort_volatile_write(struct file *filp)
1835{
1836 struct inode *inode = file_inode(filp);
1837 int ret;
1838
1839 if (!inode_owner_or_capable(inode))
1840 return -EACCES;
1841
1842 ret = mnt_want_write_file(filp);
1843 if (ret)
1844 return ret;
1845
Chao Yu0fac5582016-05-09 19:56:33 +08001846 inode_lock(inode);
1847
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001848 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001849 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001850 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001851 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001852 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001853 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001854 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001855
Chao Yu0fac5582016-05-09 19:56:33 +08001856 inode_unlock(inode);
1857
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001858 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001859 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001860 return ret;
1861}
1862
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001863static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1864{
1865 struct inode *inode = file_inode(filp);
1866 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1867 struct super_block *sb = sbi->sb;
1868 __u32 in;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001869 int ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001870
1871 if (!capable(CAP_SYS_ADMIN))
1872 return -EPERM;
1873
1874 if (get_user(in, (__u32 __user *)arg))
1875 return -EFAULT;
1876
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301877 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1878 ret = mnt_want_write_file(filp);
1879 if (ret)
1880 return ret;
1881 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08001882
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001883 switch (in) {
1884 case F2FS_GOING_DOWN_FULLSYNC:
1885 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08001886 if (IS_ERR(sb)) {
1887 ret = PTR_ERR(sb);
1888 goto out;
1889 }
1890 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001891 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001892 thaw_bdev(sb->s_bdev, sb);
1893 }
1894 break;
1895 case F2FS_GOING_DOWN_METASYNC:
1896 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08001897 ret = f2fs_sync_fs(sb, 1);
1898 if (ret)
1899 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001900 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001901 break;
1902 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001903 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001904 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001905 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08001906 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001907 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001908 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001909 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001910 ret = -EINVAL;
1911 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001912 }
Chao Yu7950e9a2018-01-18 17:23:29 +08001913
Chao Yu4d57b862018-05-30 00:20:41 +08001914 f2fs_stop_gc_thread(sbi);
1915 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001916
Chao Yu4d57b862018-05-30 00:20:41 +08001917 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08001918 clear_opt(sbi, DISCARD);
1919
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001920 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001921out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05301922 if (in != F2FS_GOING_DOWN_FULLSYNC)
1923 mnt_drop_write_file(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001924 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001925}
1926
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001927static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1928{
1929 struct inode *inode = file_inode(filp);
1930 struct super_block *sb = inode->i_sb;
1931 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1932 struct fstrim_range range;
1933 int ret;
1934
1935 if (!capable(CAP_SYS_ADMIN))
1936 return -EPERM;
1937
1938 if (!blk_queue_discard(q))
1939 return -EOPNOTSUPP;
1940
1941 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1942 sizeof(range)))
1943 return -EFAULT;
1944
Chao Yu7fb17fe2016-05-09 19:56:32 +08001945 ret = mnt_want_write_file(filp);
1946 if (ret)
1947 return ret;
1948
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001949 range.minlen = max((unsigned int)range.minlen,
1950 q->limits.discard_granularity);
1951 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001952 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001953 if (ret < 0)
1954 return ret;
1955
1956 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1957 sizeof(range)))
1958 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001959 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001960 return 0;
1961}
1962
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001963static bool uuid_is_nonzero(__u8 u[16])
1964{
1965 int i;
1966
1967 for (i = 0; i < 16; i++)
1968 if (u[i])
1969 return true;
1970 return false;
1971}
1972
1973static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1974{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001975 struct inode *inode = file_inode(filp);
1976
Sheng Yongccd31cb2018-02-06 12:31:17 +08001977 if (!f2fs_sb_has_encrypt(inode->i_sb))
Chao Yuead710b2017-11-14 19:28:42 +08001978 return -EOPNOTSUPP;
1979
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001980 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001981
Eric Biggersdb717d82016-11-26 19:07:49 -05001982 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001983}
1984
1985static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1986{
Sheng Yongccd31cb2018-02-06 12:31:17 +08001987 if (!f2fs_sb_has_encrypt(file_inode(filp)->i_sb))
Chao Yuead710b2017-11-14 19:28:42 +08001988 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05001989 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001990}
1991
1992static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1993{
1994 struct inode *inode = file_inode(filp);
1995 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1996 int err;
1997
Sheng Yongccd31cb2018-02-06 12:31:17 +08001998 if (!f2fs_sb_has_encrypt(inode->i_sb))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001999 return -EOPNOTSUPP;
2000
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002001 err = mnt_want_write_file(filp);
2002 if (err)
2003 return err;
2004
Chao Yu846ae672018-02-26 22:04:13 +08002005 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002006
2007 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2008 goto got_it;
2009
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002010 /* update superblock with uuid */
2011 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2012
Chao Yuc5bda1c2015-06-08 13:28:03 +08002013 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002014 if (err) {
2015 /* undo new data */
2016 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002017 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002018 }
2019got_it:
2020 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2021 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002022 err = -EFAULT;
2023out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002024 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002025 mnt_drop_write_file(filp);
2026 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002027}
2028
Chao Yuc1c1b582015-07-10 18:08:10 +08002029static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2030{
2031 struct inode *inode = file_inode(filp);
2032 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002033 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002034 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002035
2036 if (!capable(CAP_SYS_ADMIN))
2037 return -EPERM;
2038
Chao Yud530d4d2015-10-05 22:22:44 +08002039 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002040 return -EFAULT;
2041
Chao Yud530d4d2015-10-05 22:22:44 +08002042 if (f2fs_readonly(sbi->sb))
2043 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002044
Chao Yu7fb17fe2016-05-09 19:56:32 +08002045 ret = mnt_want_write_file(filp);
2046 if (ret)
2047 return ret;
2048
Chao Yud530d4d2015-10-05 22:22:44 +08002049 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002050 if (!mutex_trylock(&sbi->gc_mutex)) {
2051 ret = -EBUSY;
2052 goto out;
2053 }
Chao Yud530d4d2015-10-05 22:22:44 +08002054 } else {
2055 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002056 }
2057
Jaegeuk Kime066b832017-04-13 15:17:00 -07002058 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002059out:
2060 mnt_drop_write_file(filp);
2061 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002062}
2063
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002064static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2065{
2066 struct inode *inode = file_inode(filp);
2067 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2068 struct f2fs_gc_range range;
2069 u64 end;
2070 int ret;
2071
2072 if (!capable(CAP_SYS_ADMIN))
2073 return -EPERM;
2074
2075 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2076 sizeof(range)))
2077 return -EFAULT;
2078
2079 if (f2fs_readonly(sbi->sb))
2080 return -EROFS;
2081
Yunlei Heb82f6e32018-04-24 11:40:30 +08002082 end = range.start + range.len;
2083 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
2084 return -EINVAL;
2085 }
2086
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002087 ret = mnt_want_write_file(filp);
2088 if (ret)
2089 return ret;
2090
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002091do_more:
2092 if (!range.sync) {
2093 if (!mutex_trylock(&sbi->gc_mutex)) {
2094 ret = -EBUSY;
2095 goto out;
2096 }
2097 } else {
2098 mutex_lock(&sbi->gc_mutex);
2099 }
2100
2101 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2102 range.start += sbi->blocks_per_seg;
2103 if (range.start <= end)
2104 goto do_more;
2105out:
2106 mnt_drop_write_file(filp);
2107 return ret;
2108}
2109
Chao Yu4d57b862018-05-30 00:20:41 +08002110static int f2fs_ioc_f2fs_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002111{
2112 struct inode *inode = file_inode(filp);
2113 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002114 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002115
2116 if (!capable(CAP_SYS_ADMIN))
2117 return -EPERM;
2118
2119 if (f2fs_readonly(sbi->sb))
2120 return -EROFS;
2121
Chao Yu7fb17fe2016-05-09 19:56:32 +08002122 ret = mnt_want_write_file(filp);
2123 if (ret)
2124 return ret;
2125
2126 ret = f2fs_sync_fs(sbi->sb, 1);
2127
2128 mnt_drop_write_file(filp);
2129 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002130}
2131
Chao Yud323d002015-10-27 09:53:45 +08002132static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2133 struct file *filp,
2134 struct f2fs_defragment *range)
2135{
2136 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002137 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2138 .m_seg_type = NO_CHECK_TYPE };
youngjun yoo1061fd42018-05-30 04:34:58 +09002139 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002140 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002141 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002142 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002143 block_t blk_end = 0;
2144 bool fragmented = false;
2145 int err;
2146
2147 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002148 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002149 return -EINVAL;
2150
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002151 pg_start = range->start >> PAGE_SHIFT;
2152 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002153
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002154 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002155
Al Viro59551022016-01-22 15:40:57 -05002156 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002157
2158 /* writeback all dirty pages in the range */
2159 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002160 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002161 if (err)
2162 goto out;
2163
2164 /*
2165 * lookup mapping info in extent cache, skip defragmenting if physical
2166 * block addresses are continuous.
2167 */
2168 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2169 if (ei.fofs + ei.len >= pg_end)
2170 goto out;
2171 }
2172
2173 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002174 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002175
2176 /*
2177 * lookup mapping info in dnode page cache, skip defragmenting if all
2178 * physical block addresses are continuous even if there are hole(s)
2179 * in logical blocks.
2180 */
2181 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002182 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002183 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002184 if (err)
2185 goto out;
2186
2187 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002188 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002189 continue;
2190 }
2191
Chao Yu25a912e2018-01-10 18:18:51 +08002192 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002193 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002194
2195 /* record total count of block that we're going to move */
2196 total += map.m_len;
2197
Chao Yud323d002015-10-27 09:53:45 +08002198 blk_end = map.m_pblk + map.m_len;
2199
2200 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002201 }
2202
2203 if (!fragmented)
2204 goto out;
2205
Chao Yu25a912e2018-01-10 18:18:51 +08002206 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002207
2208 /*
2209 * make sure there are enough free section for LFS allocation, this can
2210 * avoid defragment running in SSR mode when free section are allocated
2211 * intensively
2212 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002213 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002214 err = -EAGAIN;
2215 goto out;
2216 }
2217
Chao Yu25a912e2018-01-10 18:18:51 +08002218 map.m_lblk = pg_start;
2219 map.m_len = pg_end - pg_start;
2220 total = 0;
2221
Chao Yud323d002015-10-27 09:53:45 +08002222 while (map.m_lblk < pg_end) {
2223 pgoff_t idx;
2224 int cnt = 0;
2225
2226do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002227 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002228 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002229 if (err)
2230 goto clear_out;
2231
2232 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002233 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002234 continue;
2235 }
2236
Jaegeuk Kim91942322016-05-20 10:13:22 -07002237 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002238
2239 idx = map.m_lblk;
2240 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2241 struct page *page;
2242
Chao Yu4d57b862018-05-30 00:20:41 +08002243 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002244 if (IS_ERR(page)) {
2245 err = PTR_ERR(page);
2246 goto clear_out;
2247 }
2248
2249 set_page_dirty(page);
2250 f2fs_put_page(page, 1);
2251
2252 idx++;
2253 cnt++;
2254 total++;
2255 }
2256
2257 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002258
2259 if (idx < pg_end && cnt < blk_per_seg)
2260 goto do_map;
2261
Jaegeuk Kim91942322016-05-20 10:13:22 -07002262 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002263
2264 err = filemap_fdatawrite(inode->i_mapping);
2265 if (err)
2266 goto out;
2267 }
2268clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002269 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002270out:
Al Viro59551022016-01-22 15:40:57 -05002271 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002272 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002273 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002274 return err;
2275}
2276
2277static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2278{
2279 struct inode *inode = file_inode(filp);
2280 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2281 struct f2fs_defragment range;
2282 int err;
2283
2284 if (!capable(CAP_SYS_ADMIN))
2285 return -EPERM;
2286
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002287 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002288 return -EINVAL;
2289
Kinglong Meed7563862017-03-10 17:55:07 +08002290 if (f2fs_readonly(sbi->sb))
2291 return -EROFS;
2292
2293 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2294 sizeof(range)))
2295 return -EFAULT;
2296
2297 /* verify alignment of offset & size */
2298 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2299 return -EINVAL;
2300
2301 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2302 sbi->max_file_blocks))
2303 return -EINVAL;
2304
Chao Yud323d002015-10-27 09:53:45 +08002305 err = mnt_want_write_file(filp);
2306 if (err)
2307 return err;
2308
Chao Yud323d002015-10-27 09:53:45 +08002309 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002310 mnt_drop_write_file(filp);
2311
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002312 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002313 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002314 return err;
Chao Yud323d002015-10-27 09:53:45 +08002315
2316 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2317 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002318 return -EFAULT;
2319
2320 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002321}
2322
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002323static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2324 struct file *file_out, loff_t pos_out, size_t len)
2325{
2326 struct inode *src = file_inode(file_in);
2327 struct inode *dst = file_inode(file_out);
2328 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2329 size_t olen = len, dst_max_i_size = 0;
2330 size_t dst_osize;
2331 int ret;
2332
2333 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2334 src->i_sb != dst->i_sb)
2335 return -EXDEV;
2336
2337 if (unlikely(f2fs_readonly(src->i_sb)))
2338 return -EROFS;
2339
Chao Yufe8494b2016-08-04 20:13:02 +08002340 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2341 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002342
2343 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2344 return -EOPNOTSUPP;
2345
Fan Lid95fd912016-09-13 11:35:42 +08002346 if (src == dst) {
2347 if (pos_in == pos_out)
2348 return 0;
2349 if (pos_out > pos_in && pos_out < pos_in + len)
2350 return -EINVAL;
2351 }
2352
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002353 inode_lock(src);
Chao Yub2532c62018-04-24 10:55:28 +08002354 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Chao Yu20a3d612016-08-04 20:13:03 +08002355 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002356 ret = -EBUSY;
2357 if (!inode_trylock(dst))
2358 goto out;
Chao Yub2532c62018-04-24 10:55:28 +08002359 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE])) {
Chao Yubb066642017-11-03 10:21:05 +08002360 inode_unlock(dst);
Chao Yu20a3d612016-08-04 20:13:03 +08002361 goto out;
2362 }
2363 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002364
2365 ret = -EINVAL;
2366 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2367 goto out_unlock;
2368 if (len == 0)
2369 olen = len = src->i_size - pos_in;
2370 if (pos_in + len == src->i_size)
2371 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2372 if (len == 0) {
2373 ret = 0;
2374 goto out_unlock;
2375 }
2376
2377 dst_osize = dst->i_size;
2378 if (pos_out + olen > dst->i_size)
2379 dst_max_i_size = pos_out + olen;
2380
2381 /* verify the end result is block aligned */
2382 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2383 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2384 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2385 goto out_unlock;
2386
2387 ret = f2fs_convert_inline_inode(src);
2388 if (ret)
2389 goto out_unlock;
2390
2391 ret = f2fs_convert_inline_inode(dst);
2392 if (ret)
2393 goto out_unlock;
2394
2395 /* write out all dirty pages from offset */
2396 ret = filemap_write_and_wait_range(src->i_mapping,
2397 pos_in, pos_in + len);
2398 if (ret)
2399 goto out_unlock;
2400
2401 ret = filemap_write_and_wait_range(dst->i_mapping,
2402 pos_out, pos_out + len);
2403 if (ret)
2404 goto out_unlock;
2405
2406 f2fs_balance_fs(sbi, true);
2407 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002408 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2409 pos_out >> F2FS_BLKSIZE_BITS,
2410 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002411
2412 if (!ret) {
2413 if (dst_max_i_size)
2414 f2fs_i_size_write(dst, dst_max_i_size);
2415 else if (dst_osize != dst->i_size)
2416 f2fs_i_size_write(dst, dst_osize);
2417 }
2418 f2fs_unlock_op(sbi);
2419out_unlock:
Chao Yubb066642017-11-03 10:21:05 +08002420 if (src != dst) {
Chao Yub2532c62018-04-24 10:55:28 +08002421 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002422 inode_unlock(dst);
Chao Yubb066642017-11-03 10:21:05 +08002423 }
Chao Yu20a3d612016-08-04 20:13:03 +08002424out:
Chao Yub2532c62018-04-24 10:55:28 +08002425 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002426 inode_unlock(src);
2427 return ret;
2428}
2429
2430static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2431{
2432 struct f2fs_move_range range;
2433 struct fd dst;
2434 int err;
2435
2436 if (!(filp->f_mode & FMODE_READ) ||
2437 !(filp->f_mode & FMODE_WRITE))
2438 return -EBADF;
2439
2440 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2441 sizeof(range)))
2442 return -EFAULT;
2443
2444 dst = fdget(range.dst_fd);
2445 if (!dst.file)
2446 return -EBADF;
2447
2448 if (!(dst.file->f_mode & FMODE_WRITE)) {
2449 err = -EBADF;
2450 goto err_out;
2451 }
2452
2453 err = mnt_want_write_file(filp);
2454 if (err)
2455 goto err_out;
2456
2457 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2458 range.pos_out, range.len);
2459
2460 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002461 if (err)
2462 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002463
2464 if (copy_to_user((struct f2fs_move_range __user *)arg,
2465 &range, sizeof(range)))
2466 err = -EFAULT;
2467err_out:
2468 fdput(dst);
2469 return err;
2470}
2471
Jaegeuk Kime066b832017-04-13 15:17:00 -07002472static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2473{
2474 struct inode *inode = file_inode(filp);
2475 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2476 struct sit_info *sm = SIT_I(sbi);
2477 unsigned int start_segno = 0, end_segno = 0;
2478 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2479 struct f2fs_flush_device range;
2480 int ret;
2481
2482 if (!capable(CAP_SYS_ADMIN))
2483 return -EPERM;
2484
2485 if (f2fs_readonly(sbi->sb))
2486 return -EROFS;
2487
2488 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2489 sizeof(range)))
2490 return -EFAULT;
2491
2492 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2493 sbi->segs_per_sec != 1) {
2494 f2fs_msg(sbi->sb, KERN_WARNING,
2495 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2496 range.dev_num, sbi->s_ndevs,
2497 sbi->segs_per_sec);
2498 return -EINVAL;
2499 }
2500
2501 ret = mnt_want_write_file(filp);
2502 if (ret)
2503 return ret;
2504
2505 if (range.dev_num != 0)
2506 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2507 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2508
2509 start_segno = sm->last_victim[FLUSH_DEVICE];
2510 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2511 start_segno = dev_start_segno;
2512 end_segno = min(start_segno + range.segments, dev_end_segno);
2513
2514 while (start_segno < end_segno) {
2515 if (!mutex_trylock(&sbi->gc_mutex)) {
2516 ret = -EBUSY;
2517 goto out;
2518 }
2519 sm->last_victim[GC_CB] = end_segno + 1;
2520 sm->last_victim[GC_GREEDY] = end_segno + 1;
2521 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2522 ret = f2fs_gc(sbi, true, true, start_segno);
2523 if (ret == -EAGAIN)
2524 ret = 0;
2525 else if (ret < 0)
2526 break;
2527 start_segno++;
2528 }
2529out:
2530 mnt_drop_write_file(filp);
2531 return ret;
2532}
2533
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002534static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2535{
2536 struct inode *inode = file_inode(filp);
2537 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2538
2539 /* Must validate to set it with SQLite behavior in Android. */
2540 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2541
2542 return put_user(sb_feature, (u32 __user *)arg);
2543}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002544
Chao Yu2c1d0302017-07-29 00:32:52 +08002545#ifdef CONFIG_QUOTA
2546static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2547{
2548 struct inode *inode = file_inode(filp);
2549 struct f2fs_inode_info *fi = F2FS_I(inode);
2550 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2551 struct super_block *sb = sbi->sb;
2552 struct dquot *transfer_to[MAXQUOTAS] = {};
2553 struct page *ipage;
2554 kprojid_t kprojid;
2555 int err;
2556
2557 if (!f2fs_sb_has_project_quota(sb)) {
2558 if (projid != F2FS_DEF_PROJID)
2559 return -EOPNOTSUPP;
2560 else
2561 return 0;
2562 }
2563
2564 if (!f2fs_has_extra_attr(inode))
2565 return -EOPNOTSUPP;
2566
2567 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2568
2569 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2570 return 0;
2571
2572 err = mnt_want_write_file(filp);
2573 if (err)
2574 return err;
2575
2576 err = -EPERM;
2577 inode_lock(inode);
2578
2579 /* Is it quota file? Do not allow user to mess with it */
2580 if (IS_NOQUOTA(inode))
2581 goto out_unlock;
2582
Chao Yu4d57b862018-05-30 00:20:41 +08002583 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Chao Yu2c1d0302017-07-29 00:32:52 +08002584 if (IS_ERR(ipage)) {
2585 err = PTR_ERR(ipage);
2586 goto out_unlock;
2587 }
2588
2589 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2590 i_projid)) {
2591 err = -EOVERFLOW;
2592 f2fs_put_page(ipage, 1);
2593 goto out_unlock;
2594 }
2595 f2fs_put_page(ipage, 1);
2596
Chao Yuc22aecd2018-04-21 17:53:52 +08002597 err = dquot_initialize(inode);
2598 if (err)
2599 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08002600
2601 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2602 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2603 err = __dquot_transfer(inode, transfer_to);
2604 dqput(transfer_to[PRJQUOTA]);
2605 if (err)
2606 goto out_dirty;
2607 }
2608
2609 F2FS_I(inode)->i_projid = kprojid;
2610 inode->i_ctime = current_time(inode);
2611out_dirty:
2612 f2fs_mark_inode_dirty_sync(inode, true);
2613out_unlock:
2614 inode_unlock(inode);
2615 mnt_drop_write_file(filp);
2616 return err;
2617}
2618#else
2619static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2620{
2621 if (projid != F2FS_DEF_PROJID)
2622 return -EOPNOTSUPP;
2623 return 0;
2624}
2625#endif
2626
2627/* Transfer internal flags to xflags */
2628static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2629{
2630 __u32 xflags = 0;
2631
Chao Yu59c84402018-04-03 15:08:17 +08002632 if (iflags & F2FS_SYNC_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002633 xflags |= FS_XFLAG_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +08002634 if (iflags & F2FS_IMMUTABLE_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002635 xflags |= FS_XFLAG_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +08002636 if (iflags & F2FS_APPEND_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002637 xflags |= FS_XFLAG_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +08002638 if (iflags & F2FS_NODUMP_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002639 xflags |= FS_XFLAG_NODUMP;
Chao Yu59c84402018-04-03 15:08:17 +08002640 if (iflags & F2FS_NOATIME_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002641 xflags |= FS_XFLAG_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +08002642 if (iflags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08002643 xflags |= FS_XFLAG_PROJINHERIT;
2644 return xflags;
2645}
2646
2647#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2648 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2649 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2650
Chao Yu2c1d0302017-07-29 00:32:52 +08002651/* Transfer xflags flags to internal */
2652static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2653{
2654 unsigned long iflags = 0;
2655
2656 if (xflags & FS_XFLAG_SYNC)
Chao Yu59c84402018-04-03 15:08:17 +08002657 iflags |= F2FS_SYNC_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002658 if (xflags & FS_XFLAG_IMMUTABLE)
Chao Yu59c84402018-04-03 15:08:17 +08002659 iflags |= F2FS_IMMUTABLE_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002660 if (xflags & FS_XFLAG_APPEND)
Chao Yu59c84402018-04-03 15:08:17 +08002661 iflags |= F2FS_APPEND_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002662 if (xflags & FS_XFLAG_NODUMP)
Chao Yu59c84402018-04-03 15:08:17 +08002663 iflags |= F2FS_NODUMP_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002664 if (xflags & FS_XFLAG_NOATIME)
Chao Yu59c84402018-04-03 15:08:17 +08002665 iflags |= F2FS_NOATIME_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002666 if (xflags & FS_XFLAG_PROJINHERIT)
Chao Yu59c84402018-04-03 15:08:17 +08002667 iflags |= F2FS_PROJINHERIT_FL;
Chao Yu2c1d0302017-07-29 00:32:52 +08002668
2669 return iflags;
2670}
2671
2672static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2673{
2674 struct inode *inode = file_inode(filp);
2675 struct f2fs_inode_info *fi = F2FS_I(inode);
2676 struct fsxattr fa;
2677
2678 memset(&fa, 0, sizeof(struct fsxattr));
2679 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
Chao Yuc807a7c2018-04-08 11:25:53 +08002680 F2FS_FL_USER_VISIBLE);
Chao Yu2c1d0302017-07-29 00:32:52 +08002681
2682 if (f2fs_sb_has_project_quota(inode->i_sb))
2683 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2684 fi->i_projid);
2685
2686 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2687 return -EFAULT;
2688 return 0;
2689}
2690
2691static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2692{
2693 struct inode *inode = file_inode(filp);
2694 struct f2fs_inode_info *fi = F2FS_I(inode);
2695 struct fsxattr fa;
2696 unsigned int flags;
2697 int err;
2698
2699 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2700 return -EFAULT;
2701
2702 /* Make sure caller has proper permission */
2703 if (!inode_owner_or_capable(inode))
2704 return -EACCES;
2705
2706 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2707 return -EOPNOTSUPP;
2708
2709 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2710 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2711 return -EOPNOTSUPP;
2712
2713 err = mnt_want_write_file(filp);
2714 if (err)
2715 return err;
2716
2717 inode_lock(inode);
2718 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2719 (flags & F2FS_FL_XFLAG_VISIBLE);
2720 err = __f2fs_ioc_setflags(inode, flags);
2721 inode_unlock(inode);
2722 mnt_drop_write_file(filp);
2723 if (err)
2724 return err;
2725
2726 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2727 if (err)
2728 return err;
2729
2730 return 0;
2731}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002732
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002733int f2fs_pin_file_control(struct inode *inode, bool inc)
2734{
2735 struct f2fs_inode_info *fi = F2FS_I(inode);
2736 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2737
2738 /* Use i_gc_failures for normal file as a risk signal. */
2739 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08002740 f2fs_i_gc_failures_write(inode,
2741 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002742
Chao Yu2ef79ec2018-05-07 20:28:54 +08002743 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002744 f2fs_msg(sbi->sb, KERN_WARNING,
2745 "%s: Enable GC = ino %lx after %x GC trials\n",
Chao Yu2ef79ec2018-05-07 20:28:54 +08002746 __func__, inode->i_ino,
2747 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002748 clear_inode_flag(inode, FI_PIN_FILE);
2749 return -EAGAIN;
2750 }
2751 return 0;
2752}
2753
2754static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2755{
2756 struct inode *inode = file_inode(filp);
2757 __u32 pin;
2758 int ret = 0;
2759
2760 if (!inode_owner_or_capable(inode))
2761 return -EACCES;
2762
2763 if (get_user(pin, (__u32 __user *)arg))
2764 return -EFAULT;
2765
2766 if (!S_ISREG(inode->i_mode))
2767 return -EINVAL;
2768
2769 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2770 return -EROFS;
2771
2772 ret = mnt_want_write_file(filp);
2773 if (ret)
2774 return ret;
2775
2776 inode_lock(inode);
2777
Chao Yu4d57b862018-05-30 00:20:41 +08002778 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb2018-01-17 16:31:38 +08002779 ret = -EINVAL;
2780 goto out;
2781 }
2782
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002783 if (!pin) {
2784 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002785 F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = 1;
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002786 goto done;
2787 }
2788
2789 if (f2fs_pin_file_control(inode, false)) {
2790 ret = -EAGAIN;
2791 goto out;
2792 }
2793 ret = f2fs_convert_inline_inode(inode);
2794 if (ret)
2795 goto out;
2796
2797 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002798 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002799done:
2800 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2801out:
2802 inode_unlock(inode);
2803 mnt_drop_write_file(filp);
2804 return ret;
2805}
2806
2807static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2808{
2809 struct inode *inode = file_inode(filp);
2810 __u32 pin = 0;
2811
2812 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08002813 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002814 return put_user(pin, (u32 __user *)arg);
2815}
2816
Chao Yuc4020b22018-01-11 14:42:30 +08002817int f2fs_precache_extents(struct inode *inode)
2818{
2819 struct f2fs_inode_info *fi = F2FS_I(inode);
2820 struct f2fs_map_blocks map;
2821 pgoff_t m_next_extent;
2822 loff_t end;
2823 int err;
2824
2825 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2826 return -EOPNOTSUPP;
2827
2828 map.m_lblk = 0;
2829 map.m_next_pgofs = NULL;
2830 map.m_next_extent = &m_next_extent;
2831 map.m_seg_type = NO_CHECK_TYPE;
2832 end = F2FS_I_SB(inode)->max_file_blocks;
2833
2834 while (map.m_lblk < end) {
2835 map.m_len = end - map.m_lblk;
2836
Chao Yub2532c62018-04-24 10:55:28 +08002837 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002838 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08002839 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08002840 if (err)
2841 return err;
2842
2843 map.m_lblk = m_next_extent;
2844 }
2845
2846 return err;
2847}
2848
2849static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2850{
2851 return f2fs_precache_extents(file_inode(filp));
2852}
2853
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002854long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2855{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002856 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2857 return -EIO;
2858
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002859 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002860 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002861 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002862 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002863 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002864 case F2FS_IOC_GETVERSION:
2865 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002866 case F2FS_IOC_START_ATOMIC_WRITE:
2867 return f2fs_ioc_start_atomic_write(filp);
2868 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2869 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002870 case F2FS_IOC_START_VOLATILE_WRITE:
2871 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002872 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2873 return f2fs_ioc_release_volatile_write(filp);
2874 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2875 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002876 case F2FS_IOC_SHUTDOWN:
2877 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002878 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002879 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002880 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2881 return f2fs_ioc_set_encryption_policy(filp, arg);
2882 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2883 return f2fs_ioc_get_encryption_policy(filp, arg);
2884 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2885 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002886 case F2FS_IOC_GARBAGE_COLLECT:
2887 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002888 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2889 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002890 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu4d57b862018-05-30 00:20:41 +08002891 return f2fs_ioc_f2fs_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002892 case F2FS_IOC_DEFRAGMENT:
2893 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002894 case F2FS_IOC_MOVE_RANGE:
2895 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002896 case F2FS_IOC_FLUSH_DEVICE:
2897 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002898 case F2FS_IOC_GET_FEATURES:
2899 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08002900 case F2FS_IOC_FSGETXATTR:
2901 return f2fs_ioc_fsgetxattr(filp, arg);
2902 case F2FS_IOC_FSSETXATTR:
2903 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08002904 case F2FS_IOC_GET_PIN_FILE:
2905 return f2fs_ioc_get_pin_file(filp, arg);
2906 case F2FS_IOC_SET_PIN_FILE:
2907 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08002908 case F2FS_IOC_PRECACHE_EXTENTS:
2909 return f2fs_ioc_precache_extents(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002910 default:
2911 return -ENOTTY;
2912 }
2913}
2914
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002915static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2916{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002917 struct file *file = iocb->ki_filp;
2918 struct inode *inode = file_inode(file);
2919 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002920
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002921 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2922 return -EIO;
2923
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002924 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2925 return -EINVAL;
2926
2927 if (!inode_trylock(inode)) {
2928 if (iocb->ki_flags & IOCB_NOWAIT)
2929 return -EAGAIN;
2930 inode_lock(inode);
2931 }
2932
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002933 ret = generic_write_checks(iocb, from);
2934 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07002935 bool preallocated = false;
2936 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002937 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002938
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002939 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2940 set_inode_flag(inode, FI_NO_PREALLOC);
2941
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002942 if ((iocb->ki_flags & IOCB_NOWAIT) &&
2943 (iocb->ki_flags & IOCB_DIRECT)) {
2944 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
2945 iov_iter_count(from)) ||
2946 f2fs_has_inline_data(inode) ||
2947 f2fs_force_buffered_io(inode, WRITE)) {
Yunlei Hecba41be2018-04-24 11:40:19 +08002948 clear_inode_flag(inode,
2949 FI_NO_PREALLOC);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002950 inode_unlock(inode);
2951 return -EAGAIN;
2952 }
2953
2954 } else {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07002955 preallocated = true;
2956 target_size = iocb->ki_pos + iov_iter_count(from);
2957
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002958 err = f2fs_preallocate_blocks(iocb, from);
2959 if (err) {
2960 clear_inode_flag(inode, FI_NO_PREALLOC);
2961 inode_unlock(inode);
2962 return err;
2963 }
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002964 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002965 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002966 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08002967
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07002968 /* if we couldn't write data, we should deallocate blocks. */
2969 if (preallocated && i_size_read(inode) < target_size)
2970 f2fs_truncate(inode);
2971
Chao Yub0af6d42017-08-02 23:21:48 +08002972 if (ret > 0)
2973 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002974 }
2975 inode_unlock(inode);
2976
Christoph Hellwige2592212016-04-07 08:52:01 -07002977 if (ret > 0)
2978 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002979 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002980}
2981
Namjae Jeone9750822013-02-04 23:41:41 +09002982#ifdef CONFIG_COMPAT
2983long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2984{
2985 switch (cmd) {
2986 case F2FS_IOC32_GETFLAGS:
2987 cmd = F2FS_IOC_GETFLAGS;
2988 break;
2989 case F2FS_IOC32_SETFLAGS:
2990 cmd = F2FS_IOC_SETFLAGS;
2991 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08002992 case F2FS_IOC32_GETVERSION:
2993 cmd = F2FS_IOC_GETVERSION;
2994 break;
2995 case F2FS_IOC_START_ATOMIC_WRITE:
2996 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2997 case F2FS_IOC_START_VOLATILE_WRITE:
2998 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2999 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3000 case F2FS_IOC_SHUTDOWN:
3001 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3002 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3003 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3004 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003005 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003006 case F2FS_IOC_WRITE_CHECKPOINT:
3007 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003008 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003009 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003010 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003011 case F2FS_IOC_FSGETXATTR:
3012 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003013 case F2FS_IOC_GET_PIN_FILE:
3014 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003015 case F2FS_IOC_PRECACHE_EXTENTS:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003016 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003017 default:
3018 return -ENOIOCTLCMD;
3019 }
3020 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3021}
3022#endif
3023
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003024const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003025 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04003026 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003027 .write_iter = f2fs_file_write_iter,
3028 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003029 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003030 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003031 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003032 .fsync = f2fs_sync_file,
3033 .fallocate = f2fs_fallocate,
3034 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003035#ifdef CONFIG_COMPAT
3036 .compat_ioctl = f2fs_compat_ioctl,
3037#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003038 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003039 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003040};