blob: 847e933e4a84cf434a2ad49622e56c2389ed641c [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002 * fs/f2fs/file.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/stat.h>
14#include <linux/buffer_head.h>
15#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090016#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/falloc.h>
18#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090019#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090020#include <linux/uaccess.h>
21#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090022#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080023#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070024#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070025#include <linux/file.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
30#include "xattr.h"
31#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080032#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080033#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090034#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080036static int f2fs_filemap_fault(struct vm_fault *vmf)
37{
38 struct inode *inode = file_inode(vmf->vma->vm_file);
39 int err;
40
41 down_read(&F2FS_I(inode)->i_mmap_sem);
42 err = filemap_fault(vmf);
43 up_read(&F2FS_I(inode)->i_mmap_sem);
44
45 return err;
46}
47
Dave Jiang11bac802017-02-24 14:56:41 -080048static int f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049{
50 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080051 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090053 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080054 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090055
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020056 if (unlikely(f2fs_cp_error(sbi))) {
57 err = -EIO;
58 goto err;
59 }
60
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090061 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070062
63 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070064
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090065 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080066 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070067 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070068 err = f2fs_reserve_block(&dn, page->index);
69 if (err) {
70 f2fs_unlock_op(sbi);
71 goto out;
72 }
73 f2fs_put_dnode(&dn);
74 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090075
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -080076 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -080077
Dave Jiang11bac802017-02-24 14:56:41 -080078 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080079 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090080 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090081 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090082 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090083 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090084 unlock_page(page);
85 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080086 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090087 }
88
89 /*
90 * check to see if the page is mapped already (no holes)
91 */
92 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090093 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090094
95 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +080097 i_size_read(inode)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090098 unsigned offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 offset = i_size_read(inode) & ~PAGE_MASK;
100 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900101 }
102 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700103 if (!PageUptodate(page))
104 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900105
Chao Yub0af6d42017-08-02 23:21:48 +0800106 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
107
Jaegeuk Kime943a102013-10-25 14:26:31 +0900108 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900109mapped:
110 /* fill the page */
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800111 f2fs_wait_on_page_writeback(page, DATA, false);
Chao Yu08b39fb2015-10-08 13:27:34 +0800112
113 /* wait for GCed encrypted page writeback */
Jaegeuk Kim19585932017-09-05 16:54:24 -0700114 if (f2fs_encrypted_file(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -0700115 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +0800116
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800117out_sem:
118 up_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900119out:
120 sb_end_pagefault(inode->i_sb);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800121 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200122err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900123 return block_page_mkwrite_return(err);
124}
125
126static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800127 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700128 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900129 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900130};
131
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900132static int get_parent_ino(struct inode *inode, nid_t *pino)
133{
134 struct dentry *dentry;
135
136 inode = igrab(inode);
137 dentry = d_find_any_alias(inode);
138 iput(inode);
139 if (!dentry)
140 return 0;
141
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900142 *pino = parent_ino(dentry);
143 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900144 return 1;
145}
146
Chao Yu9d1589e2014-08-20 18:37:35 +0800147static inline bool need_do_checkpoint(struct inode *inode)
148{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700149 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800150 bool need_cp = false;
151
152 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
153 need_cp = true;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700154 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700155 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800156 else if (file_wrong_pino(inode))
157 need_cp = true;
158 else if (!space_for_roll_forward(sbi))
159 need_cp = true;
160 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
161 need_cp = true;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700162 else if (test_opt(sbi, FASTBOOT))
163 need_cp = true;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800164 else if (sbi->active_logs == 2)
165 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800166
167 return need_cp;
168}
169
Changman Lee9c7bb702014-12-08 15:29:40 +0900170static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
171{
172 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
173 bool ret = false;
174 /* But we need to avoid that there are some inode updates */
175 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
176 ret = true;
177 f2fs_put_page(i, 0);
178 return ret;
179}
180
Changman Lee51455b12014-12-08 15:29:41 +0900181static void try_to_fix_pino(struct inode *inode)
182{
183 struct f2fs_inode_info *fi = F2FS_I(inode);
184 nid_t pino;
185
186 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900187 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
188 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700189 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900190 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900191 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700192 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900193}
194
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700195static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
196 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900197{
198 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700199 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700200 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900201 int ret = 0;
202 bool need_cp = false;
203 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900204 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205 .nr_to_write = LONG_MAX,
206 .for_reclaim = 0,
207 };
208
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900209 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900210 return 0;
211
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900212 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700213
214 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800215 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700216 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400217 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700218 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700219
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900220 if (ret) {
221 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900222 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900223 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900224
Changman Lee9c7bb702014-12-08 15:29:40 +0900225 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800226 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700227 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900228 goto go_write;
229 }
230
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700231 /*
232 * if there is no written data, don't waste time to write recovery info.
233 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700234 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700235 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700236
Changman Lee9c7bb702014-12-08 15:29:40 +0900237 /* it may call write_inode just prior to fsync */
238 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700239 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700240
Jaegeuk Kim91942322016-05-20 10:13:22 -0700241 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700242 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700243 goto flush_out;
244 goto out;
245 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700246go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900247 /*
248 * Both of fdatasync() and fsync() are able to be recovered from
249 * sudden-power-off.
250 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700251 down_read(&F2FS_I(inode)->i_sem);
Chao Yu9d1589e2014-08-20 18:37:35 +0800252 need_cp = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700253 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900254
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900255 if (need_cp) {
256 /* all the dirty node pages should be flushed for POR */
257 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900258
Changman Lee51455b12014-12-08 15:29:41 +0900259 /*
260 * We've secured consistency through sync_fs. Following pino
261 * will be used only for fsynced inodes after checkpoint.
262 */
263 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700264 clear_inode_flag(inode, FI_APPEND_WRITE);
265 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900266 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900267 }
Changman Lee51455b12014-12-08 15:29:41 +0900268sync_nodes:
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700269 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700270 if (ret)
271 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900272
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800273 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800274 if (unlikely(f2fs_cp_error(sbi))) {
275 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800276 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800277 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800278
Changman Lee51455b12014-12-08 15:29:41 +0900279 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700280 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900281 f2fs_write_inode(inode, NULL);
282 goto sync_nodes;
283 }
284
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700285 /*
286 * If it's atomic_write, it's just fine to keep write ordering. So
287 * here we don't need to wait for node write completion, since we use
288 * node chain which serializes node blocks. If one of node writes are
289 * reordered, we can see simply broken chain, resulting in stopping
290 * roll-forward recovery. It means we'll recover all or none node blocks
291 * given fsync mark.
292 */
293 if (!atomic) {
294 ret = wait_on_node_pages_writeback(sbi, ino);
295 if (ret)
296 goto out;
297 }
Changman Lee51455b12014-12-08 15:29:41 +0900298
299 /* once recovery info is written, don't need to tack this */
Chao Yua49324f2015-12-15 13:29:47 +0800300 remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700301 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900302flush_out:
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -0800303 if (!atomic)
Chao Yu39d787b2017-09-29 13:59:38 +0800304 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800305 if (!ret) {
306 remove_ino_entry(sbi, ino, UPDATE_INO);
307 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu39d787b2017-09-29 13:59:38 +0800308 remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800309 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800310 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900311out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900312 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700313 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900314 return ret;
315}
316
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700317int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
318{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200319 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
320 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700321 return f2fs_do_sync_file(file, start, end, datasync, false);
322}
323
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900324static pgoff_t __get_first_dirty_index(struct address_space *mapping,
325 pgoff_t pgofs, int whence)
326{
327 struct pagevec pvec;
328 int nr_pages;
329
330 if (whence != SEEK_DATA)
331 return 0;
332
333 /* find first dirty page index */
334 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700335 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
336 PAGECACHE_TAG_DIRTY, 1);
Chao Yu80dd9c02016-02-24 17:20:44 +0800337 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900338 pagevec_release(&pvec);
339 return pgofs;
340}
341
342static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
343 int whence)
344{
345 switch (whence) {
346 case SEEK_DATA:
347 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
348 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
349 return true;
350 break;
351 case SEEK_HOLE:
352 if (blkaddr == NULL_ADDR)
353 return true;
354 break;
355 }
356 return false;
357}
358
Chao Yu267378d2014-04-23 14:10:24 +0800359static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
360{
361 struct inode *inode = file->f_mapping->host;
362 loff_t maxbytes = inode->i_sb->s_maxbytes;
363 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900364 pgoff_t pgofs, end_offset, dirty;
365 loff_t data_ofs = offset;
366 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800367 int err = 0;
368
Al Viro59551022016-01-22 15:40:57 -0500369 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800370
371 isize = i_size_read(inode);
372 if (offset >= isize)
373 goto fail;
374
375 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800376 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800377 if (whence == SEEK_HOLE)
378 data_ofs = isize;
379 goto found;
380 }
381
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300382 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800383
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900384 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
385
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300386 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800387 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700388 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800389 if (err && err != -ENOENT) {
390 goto fail;
391 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900392 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800393 if (whence == SEEK_DATA) {
Chao Yu3cf45742016-01-26 15:40:44 +0800394 pgofs = get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800395 continue;
396 } else {
397 goto found;
398 }
399 }
400
Chao Yu81ca7352016-01-26 15:39:35 +0800401 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800402
403 /* find data/hole in dnode block */
404 for (; dn.ofs_in_node < end_offset;
405 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300406 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800407 block_t blkaddr;
Chao Yu7a2af762017-07-19 00:19:06 +0800408 blkaddr = datablock_addr(dn.inode,
409 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800410
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900411 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800412 f2fs_put_dnode(&dn);
413 goto found;
414 }
415 }
416 f2fs_put_dnode(&dn);
417 }
418
419 if (whence == SEEK_DATA)
420 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800421found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900422 if (whence == SEEK_HOLE && data_ofs > isize)
423 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500424 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800425 return vfs_setpos(file, data_ofs, maxbytes);
426fail:
Al Viro59551022016-01-22 15:40:57 -0500427 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800428 return -ENXIO;
429}
430
431static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
432{
433 struct inode *inode = file->f_mapping->host;
434 loff_t maxbytes = inode->i_sb->s_maxbytes;
435
436 switch (whence) {
437 case SEEK_SET:
438 case SEEK_CUR:
439 case SEEK_END:
440 return generic_file_llseek_size(file, offset, whence,
441 maxbytes, i_size_read(inode));
442 case SEEK_DATA:
443 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700444 if (offset < 0)
445 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800446 return f2fs_seek_block(file, offset, whence);
447 }
448
449 return -EINVAL;
450}
451
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900452static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
453{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700454 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800455 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700456
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200457 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
458 return -EIO;
459
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700460 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800461 err = f2fs_convert_inline_inode(inode);
462 if (err)
463 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700464
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900465 file_accessed(file);
466 vma->vm_ops = &f2fs_file_vm_ops;
467 return 0;
468}
469
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700470static int f2fs_file_open(struct inode *inode, struct file *filp)
471{
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700472 struct dentry *dir;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700473
Chao Yu0abd6752017-07-09 00:13:07 +0800474 if (f2fs_encrypted_inode(inode)) {
475 int ret = fscrypt_get_encryption_info(inode);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700476 if (ret)
Chao Yuae108662016-02-14 18:58:35 +0800477 return -EACCES;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700478 if (!fscrypt_has_encryption_key(inode))
Chao Yuae108662016-02-14 18:58:35 +0800479 return -ENOKEY;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700480 }
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700481 dir = dget_parent(file_dentry(filp));
482 if (f2fs_encrypted_inode(d_inode(dir)) &&
483 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
484 dput(dir);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800485 return -EPERM;
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700486 }
487 dput(dir);
Chao Yu0abd6752017-07-09 00:13:07 +0800488 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700489}
490
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900491int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900492{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700493 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900494 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800495 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900496 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800497 int base = 0;
498
499 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
500 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900501
Gu Zheng45590712013-07-15 17:57:38 +0800502 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800503 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900504
Chris Fries6c311ec2014-01-17 14:44:39 -0600505 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900506 block_t blkaddr = le32_to_cpu(*addr);
507 if (blkaddr == NULL_ADDR)
508 continue;
509
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800510 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800511 set_data_blkaddr(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900512 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700513 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700514 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900515 nr_free++;
516 }
Chao Yu19b2c302015-08-26 20:34:48 +0800517
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900518 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800519 pgoff_t fofs;
520 /*
521 * once we invalidate valid blkaddr in range [ofs, ofs + count],
522 * we will invalidate all blkaddr in the whole range.
523 */
524 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800525 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800526 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900527 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900528 }
529 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900530
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800531 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900532 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
533 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900534 return nr_free;
535}
536
537void truncate_data_blocks(struct dnode_of_data *dn)
538{
539 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
540}
541
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800542static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700543 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900544{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300545 unsigned offset = from & (PAGE_SIZE - 1);
546 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700547 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900548 struct page *page;
549
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700550 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700551 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900552
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700553 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700554 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700555 if (page && PageUptodate(page))
556 goto truncate_out;
557 f2fs_put_page(page, 1);
558 return 0;
559 }
560
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700561 page = get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900562 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800563 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700564truncate_out:
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800565 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300566 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700567
568 /* An encrypted inode should have a key and truncate the last page. */
569 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
570 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800571 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900572 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700573 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900574}
575
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700576int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900577{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700578 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900579 unsigned int blocksize = inode->i_sb->s_blocksize;
580 struct dnode_of_data dn;
581 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800582 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700583 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800584 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900585
Namjae Jeon51dd6242013-04-20 01:28:52 +0900586 trace_f2fs_truncate_blocks_enter(inode, from);
587
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800588 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900589
Chao Yu09210c92016-05-05 19:13:03 +0800590 if (free_from >= sbi->max_file_blocks)
591 goto free_partial;
592
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700593 if (lock)
594 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800595
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700596 ipage = get_node_page(sbi, inode->i_ino);
597 if (IS_ERR(ipage)) {
598 err = PTR_ERR(ipage);
599 goto out;
600 }
601
602 if (f2fs_has_inline_data(inode)) {
Kinglong Meebd4667c2017-03-10 20:43:20 +0800603 truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700604 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800605 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700606 goto out;
607 }
608
609 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700610 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900611 if (err) {
612 if (err == -ENOENT)
613 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700614 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700615 }
616
Chao Yu81ca7352016-01-26 15:39:35 +0800617 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900618
619 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700620 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900621
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900622 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
623 truncate_data_blocks_range(&dn, count);
624 free_from += count;
625 }
626
627 f2fs_put_dnode(&dn);
628free_next:
629 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800630out:
631 if (lock)
632 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800633free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700634 /* lastly zero out the first data page */
635 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800636 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900637
Namjae Jeon51dd6242013-04-20 01:28:52 +0900638 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639 return err;
640}
641
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700642int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900643{
Chao Yub0154892015-08-24 17:39:42 +0800644 int err;
645
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200646 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
647 return -EIO;
648
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900649 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
650 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800651 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900652
Namjae Jeon51dd6242013-04-20 01:28:52 +0900653 trace_f2fs_truncate(inode);
654
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800655#ifdef CONFIG_F2FS_FAULT_INJECTION
656 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
657 f2fs_show_injection_info(FAULT_TRUNCATE);
658 return -EIO;
659 }
660#endif
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800661 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800662 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800663 err = f2fs_convert_inline_inode(inode);
664 if (err)
665 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800666 }
667
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700668 err = truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800669 if (err)
670 return err;
671
Deepa Dinamani078cd822016-09-14 07:48:04 -0700672 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700673 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800674 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900675}
676
David Howellsa528d352017-01-31 16:46:22 +0000677int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800678 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900679{
David Howellsa528d352017-01-31 16:46:22 +0000680 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800681 struct f2fs_inode_info *fi = F2FS_I(inode);
682 unsigned int flags;
683
Chao Yu2c1d0302017-07-29 00:32:52 +0800684 flags = fi->i_flags & (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800685 if (flags & FS_APPEND_FL)
686 stat->attributes |= STATX_ATTR_APPEND;
687 if (flags & FS_COMPR_FL)
688 stat->attributes |= STATX_ATTR_COMPRESSED;
689 if (f2fs_encrypted_inode(inode))
690 stat->attributes |= STATX_ATTR_ENCRYPTED;
691 if (flags & FS_IMMUTABLE_FL)
692 stat->attributes |= STATX_ATTR_IMMUTABLE;
693 if (flags & FS_NODUMP_FL)
694 stat->attributes |= STATX_ATTR_NODUMP;
695
696 stat->attributes_mask |= (STATX_ATTR_APPEND |
697 STATX_ATTR_COMPRESSED |
698 STATX_ATTR_ENCRYPTED |
699 STATX_ATTR_IMMUTABLE |
700 STATX_ATTR_NODUMP);
701
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900702 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700703
704 /* we need to show initial sectors used for inline_data/dentries */
705 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
706 f2fs_has_inline_dentry(inode))
707 stat->blocks += (stat->size + 511) >> 9;
708
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900709 return 0;
710}
711
712#ifdef CONFIG_F2FS_FS_POSIX_ACL
713static void __setattr_copy(struct inode *inode, const struct iattr *attr)
714{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900715 unsigned int ia_valid = attr->ia_valid;
716
717 if (ia_valid & ATTR_UID)
718 inode->i_uid = attr->ia_uid;
719 if (ia_valid & ATTR_GID)
720 inode->i_gid = attr->ia_gid;
721 if (ia_valid & ATTR_ATIME)
722 inode->i_atime = timespec_trunc(attr->ia_atime,
723 inode->i_sb->s_time_gran);
724 if (ia_valid & ATTR_MTIME)
725 inode->i_mtime = timespec_trunc(attr->ia_mtime,
726 inode->i_sb->s_time_gran);
727 if (ia_valid & ATTR_CTIME)
728 inode->i_ctime = timespec_trunc(attr->ia_ctime,
729 inode->i_sb->s_time_gran);
730 if (ia_valid & ATTR_MODE) {
731 umode_t mode = attr->ia_mode;
732
733 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
734 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700735 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900736 }
737}
738#else
739#define __setattr_copy setattr_copy
740#endif
741
742int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
743{
David Howells2b0143b2015-03-17 22:25:59 +0000744 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900745 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800746 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900747
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200748 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
749 return -EIO;
750
Jan Kara31051c82016-05-26 16:55:18 +0200751 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900752 if (err)
753 return err;
754
Chao Yu0abd6752017-07-09 00:13:07 +0800755 if (is_quota_modification(inode, attr)) {
756 err = dquot_initialize(inode);
757 if (err)
758 return err;
759 }
760 if ((attr->ia_valid & ATTR_UID &&
761 !uid_eq(attr->ia_uid, inode->i_uid)) ||
762 (attr->ia_valid & ATTR_GID &&
763 !gid_eq(attr->ia_gid, inode->i_gid))) {
764 err = dquot_transfer(inode, attr);
765 if (err)
766 return err;
767 }
768
Chao Yu09db6a22014-09-15 18:02:09 +0800769 if (attr->ia_valid & ATTR_SIZE) {
Eric Biggers67773a12017-06-13 16:47:54 -0700770 if (f2fs_encrypted_inode(inode)) {
771 err = fscrypt_get_encryption_info(inode);
772 if (err)
773 return err;
774 if (!fscrypt_has_encryption_key(inode))
775 return -ENOKEY;
776 }
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700777
Chao Yu3c454142015-06-05 18:34:02 +0800778 if (attr->ia_size <= i_size_read(inode)) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800779 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu09db6a22014-09-15 18:02:09 +0800780 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700781 err = f2fs_truncate(inode);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800782 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub0154892015-08-24 17:39:42 +0800783 if (err)
784 return err;
Chao Yu09db6a22014-09-15 18:02:09 +0800785 } else {
786 /*
Chao Yu3c454142015-06-05 18:34:02 +0800787 * do not trim all blocks after i_size if target size is
788 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800789 */
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800790 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3c454142015-06-05 18:34:02 +0800791 truncate_setsize(inode, attr->ia_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800792 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu0cab80e2015-12-01 11:36:16 +0800793
794 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800795 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800796 err = f2fs_convert_inline_inode(inode);
797 if (err)
798 return err;
799 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700800 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800801 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800802
Chao Yua0d00fa2017-10-09 17:55:19 +0800803 down_write(&F2FS_I(inode)->i_sem);
804 F2FS_I(inode)->last_disk_size = i_size_read(inode);
805 up_write(&F2FS_I(inode)->i_sem);
806
Yunlei Hec0ed4402016-12-11 15:35:15 +0800807 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900808 }
809
810 __setattr_copy(inode, attr);
811
812 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800813 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700814 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
815 inode->i_mode = F2FS_I(inode)->i_acl_mode;
816 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900817 }
818 }
819
Yunlei Hec0ed4402016-12-11 15:35:15 +0800820 /* file size may changed here */
821 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700822
823 /* inode change will produce dirty node pages flushed by checkpoint */
824 f2fs_balance_fs(F2FS_I_SB(inode), true);
825
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900826 return err;
827}
828
829const struct inode_operations f2fs_file_inode_operations = {
830 .getattr = f2fs_getattr,
831 .setattr = f2fs_setattr,
832 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800833 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900834#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900835 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900836#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900837 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900838};
839
Chao Yu63943282015-08-07 18:36:06 +0800840static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900841 loff_t start, loff_t len)
842{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700843 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900844 struct page *page;
845
846 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800847 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900848
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800849 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900850
Gu Zhenge4795562013-09-27 18:08:30 +0800851 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900852 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800853 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900854
Chao Yu63943282015-08-07 18:36:06 +0800855 if (IS_ERR(page))
856 return PTR_ERR(page);
857
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800858 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800859 zero_user(page, start, len);
860 set_page_dirty(page);
861 f2fs_put_page(page, 1);
862 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900863}
864
865int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
866{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900867 int err;
868
Chao Yuea587112015-09-17 20:22:44 +0800869 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900870 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800871 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900872
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900873 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yuea587112015-09-17 20:22:44 +0800874 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900875 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800876 if (err == -ENOENT) {
Weichao Guo48ab25f2017-10-14 08:13:32 +0800877 pg_start = get_next_page_offset(&dn, pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900878 continue;
Chao Yuea587112015-09-17 20:22:44 +0800879 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900880 return err;
881 }
882
Chao Yu81ca7352016-01-26 15:39:35 +0800883 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800884 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
885
886 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
887
888 truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900889 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800890
891 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900892 }
893 return 0;
894}
895
Chao Yua66c7b22013-11-22 16:52:50 +0800896static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900897{
898 pgoff_t pg_start, pg_end;
899 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800900 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900901
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800902 ret = f2fs_convert_inline_inode(inode);
903 if (ret)
904 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800905
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300906 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
907 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900908
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300909 off_start = offset & (PAGE_SIZE - 1);
910 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900911
912 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800913 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900914 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800915 if (ret)
916 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900917 } else {
Chao Yu63943282015-08-07 18:36:06 +0800918 if (off_start) {
919 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300920 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800921 if (ret)
922 return ret;
923 }
924 if (off_end) {
925 ret = fill_zero(inode, pg_end, 0, off_end);
926 if (ret)
927 return ret;
928 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900929
930 if (pg_start < pg_end) {
931 struct address_space *mapping = inode->i_mapping;
932 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700933 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500934
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800935 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900936
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300937 blk_start = (loff_t)pg_start << PAGE_SHIFT;
938 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800939 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900940 truncate_inode_pages_range(mapping, blk_start,
941 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900942
Gu Zhenge4795562013-09-27 18:08:30 +0800943 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900944 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800945 f2fs_unlock_op(sbi);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800946 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900947 }
948 }
949
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900950 return ret;
951}
952
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700953static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
954 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800955{
956 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
957 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700958 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700959
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700960next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700961 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700962 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700963 if (ret && ret != -ENOENT) {
964 return ret;
965 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700966 if (dn.max_level == 0)
967 return -ENOENT;
968 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
969 blkaddr += done;
970 do_replace += done;
971 goto next;
972 }
973
974 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
975 dn.ofs_in_node, len);
976 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800977 *blkaddr = datablock_addr(dn.inode,
978 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700979 if (!is_checkpointed_data(sbi, *blkaddr)) {
980
981 if (test_opt(sbi, LFS)) {
982 f2fs_put_dnode(&dn);
983 return -ENOTSUPP;
984 }
985
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700986 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +0800987 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700988 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700989 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700990 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700991 f2fs_put_dnode(&dn);
992next:
993 len -= done;
994 off += done;
995 if (len)
996 goto next_dnode;
997 return 0;
998}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700999
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001000static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1001 int *do_replace, pgoff_t off, int len)
1002{
1003 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1004 struct dnode_of_data dn;
1005 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001006
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001007 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1008 if (*do_replace == 0)
1009 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001010
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001011 set_new_dnode(&dn, inode, NULL, NULL, 0);
1012 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1013 if (ret) {
1014 dec_valid_block_count(sbi, inode, 1);
1015 invalidate_blocks(sbi, *blkaddr);
1016 } else {
1017 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001018 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001019 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001020 }
1021 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001022}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001023
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001024static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1025 block_t *blkaddr, int *do_replace,
1026 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1027{
1028 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1029 pgoff_t i = 0;
1030 int ret;
1031
1032 while (i < len) {
1033 if (blkaddr[i] == NULL_ADDR && !full) {
1034 i++;
1035 continue;
1036 }
1037
1038 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1039 struct dnode_of_data dn;
1040 struct node_info ni;
1041 size_t new_size;
1042 pgoff_t ilen;
1043
1044 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1045 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1046 if (ret)
1047 return ret;
1048
1049 get_node_info(sbi, dn.nid, &ni);
1050 ilen = min((pgoff_t)
1051 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1052 dn.ofs_in_node, len - i);
1053 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001054 dn.data_blkaddr = datablock_addr(dn.inode,
1055 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001056 truncate_data_blocks_range(&dn, 1);
1057
1058 if (do_replace[i]) {
1059 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001060 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001061 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001062 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001063 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1064 blkaddr[i], ni.version, true, false);
1065
1066 do_replace[i] = 0;
1067 }
1068 dn.ofs_in_node++;
1069 i++;
1070 new_size = (dst + i) << PAGE_SHIFT;
1071 if (dst_inode->i_size < new_size)
1072 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001073 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001074
1075 f2fs_put_dnode(&dn);
1076 } else {
1077 struct page *psrc, *pdst;
1078
1079 psrc = get_lock_data_page(src_inode, src + i, true);
1080 if (IS_ERR(psrc))
1081 return PTR_ERR(psrc);
1082 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1083 true);
1084 if (IS_ERR(pdst)) {
1085 f2fs_put_page(psrc, 1);
1086 return PTR_ERR(pdst);
1087 }
1088 f2fs_copy_page(psrc, pdst);
1089 set_page_dirty(pdst);
1090 f2fs_put_page(pdst, 1);
1091 f2fs_put_page(psrc, 1);
1092
1093 ret = truncate_hole(src_inode, src + i, src + i + 1);
1094 if (ret)
1095 return ret;
1096 i++;
1097 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001098 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001099 return 0;
1100}
1101
1102static int __exchange_data_block(struct inode *src_inode,
1103 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001104 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001105{
1106 block_t *src_blkaddr;
1107 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001108 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001109 int ret;
1110
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001111 while (len) {
1112 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001113
Michal Hockoa7c3e902017-05-08 15:57:09 -07001114 src_blkaddr = kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001115 if (!src_blkaddr)
1116 return -ENOMEM;
1117
Michal Hockoa7c3e902017-05-08 15:57:09 -07001118 do_replace = kvzalloc(sizeof(int) * olen, GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001119 if (!do_replace) {
1120 kvfree(src_blkaddr);
1121 return -ENOMEM;
1122 }
1123
1124 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1125 do_replace, src, olen);
1126 if (ret)
1127 goto roll_back;
1128
1129 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1130 do_replace, src, dst, olen, full);
1131 if (ret)
1132 goto roll_back;
1133
1134 src += olen;
1135 dst += olen;
1136 len -= olen;
1137
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001138 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001139 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001140 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001141 return 0;
1142
1143roll_back:
1144 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1145 kvfree(src_blkaddr);
1146 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001147 return ret;
1148}
1149
1150static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1151{
1152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001153 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001154 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001155
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001156 f2fs_balance_fs(sbi, true);
1157 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001158
1159 f2fs_drop_extent_tree(inode);
1160
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001161 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1162 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +08001163 return ret;
1164}
1165
1166static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1167{
1168 pgoff_t pg_start, pg_end;
1169 loff_t new_size;
1170 int ret;
1171
Chao Yub4ace332015-05-06 13:09:46 +08001172 if (offset + len >= i_size_read(inode))
1173 return -EINVAL;
1174
1175 /* collapse range should be aligned to block size of f2fs. */
1176 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1177 return -EINVAL;
1178
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001179 ret = f2fs_convert_inline_inode(inode);
1180 if (ret)
1181 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001182
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001183 pg_start = offset >> PAGE_SHIFT;
1184 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yub4ace332015-05-06 13:09:46 +08001185
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001186 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001187 /* write out all dirty pages from offset */
1188 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1189 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001190 goto out;
Chao Yub4ace332015-05-06 13:09:46 +08001191
Chao Yubb066642017-11-03 10:21:05 +08001192 /* avoid gc operation during block exchange */
1193 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1194
Chao Yub4ace332015-05-06 13:09:46 +08001195 truncate_pagecache(inode, offset);
1196
1197 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1198 if (ret)
Chao Yubb066642017-11-03 10:21:05 +08001199 goto out_unlock;
Chao Yub4ace332015-05-06 13:09:46 +08001200
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001201 /* write out all moved pages, if possible */
1202 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1203 truncate_pagecache(inode, offset);
1204
Chao Yub4ace332015-05-06 13:09:46 +08001205 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001206 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001207
1208 ret = truncate_blocks(inode, new_size, true);
1209 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001210 f2fs_i_size_write(inode, new_size);
Chao Yubb066642017-11-03 10:21:05 +08001211out_unlock:
1212 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001213out:
1214 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001215 return ret;
1216}
1217
Chao Yu6e961942016-05-09 19:56:31 +08001218static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1219 pgoff_t end)
1220{
1221 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1222 pgoff_t index = start;
1223 unsigned int ofs_in_node = dn->ofs_in_node;
1224 blkcnt_t count = 0;
1225 int ret;
1226
1227 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001228 if (datablock_addr(dn->inode, dn->node_page,
1229 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001230 count++;
1231 }
1232
1233 dn->ofs_in_node = ofs_in_node;
1234 ret = reserve_new_blocks(dn, count);
1235 if (ret)
1236 return ret;
1237
1238 dn->ofs_in_node = ofs_in_node;
1239 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001240 dn->data_blkaddr = datablock_addr(dn->inode,
1241 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001242 /*
1243 * reserve_new_blocks will not guarantee entire block
1244 * allocation.
1245 */
1246 if (dn->data_blkaddr == NULL_ADDR) {
1247 ret = -ENOSPC;
1248 break;
1249 }
1250 if (dn->data_blkaddr != NEW_ADDR) {
1251 invalidate_blocks(sbi, dn->data_blkaddr);
1252 dn->data_blkaddr = NEW_ADDR;
1253 set_data_blkaddr(dn);
1254 }
1255 }
1256
1257 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1258
1259 return ret;
1260}
1261
Chao Yu75cd4e02015-05-06 13:11:13 +08001262static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1263 int mode)
1264{
1265 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1266 struct address_space *mapping = inode->i_mapping;
1267 pgoff_t index, pg_start, pg_end;
1268 loff_t new_size = i_size_read(inode);
1269 loff_t off_start, off_end;
1270 int ret = 0;
1271
Chao Yu75cd4e02015-05-06 13:11:13 +08001272 ret = inode_newsize_ok(inode, (len + offset));
1273 if (ret)
1274 return ret;
1275
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001276 ret = f2fs_convert_inline_inode(inode);
1277 if (ret)
1278 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001279
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001280 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001281 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1282 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001283 goto out_sem;
Chao Yu75cd4e02015-05-06 13:11:13 +08001284
1285 truncate_pagecache_range(inode, offset, offset + len - 1);
1286
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001287 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1288 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001289
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001290 off_start = offset & (PAGE_SIZE - 1);
1291 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001292
1293 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001294 ret = fill_zero(inode, pg_start, off_start,
1295 off_end - off_start);
1296 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001297 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001298
Chao Yu75cd4e02015-05-06 13:11:13 +08001299 new_size = max_t(loff_t, new_size, offset + len);
1300 } else {
1301 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001302 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001303 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001304 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001305 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001306
Chao Yu75cd4e02015-05-06 13:11:13 +08001307 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001308 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001309 }
1310
Chao Yu6e961942016-05-09 19:56:31 +08001311 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001312 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001313 unsigned int end_offset;
1314 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001315
1316 f2fs_lock_op(sbi);
1317
Chao Yu6e961942016-05-09 19:56:31 +08001318 set_new_dnode(&dn, inode, NULL, NULL, 0);
1319 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001320 if (ret) {
1321 f2fs_unlock_op(sbi);
1322 goto out;
1323 }
1324
Chao Yu6e961942016-05-09 19:56:31 +08001325 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1326 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1327
1328 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001329 f2fs_put_dnode(&dn);
1330 f2fs_unlock_op(sbi);
Chao Yu9434fcd2016-10-11 22:57:02 +08001331
1332 f2fs_balance_fs(sbi, dn.node_changed);
1333
Chao Yu6e961942016-05-09 19:56:31 +08001334 if (ret)
1335 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001336
Chao Yu6e961942016-05-09 19:56:31 +08001337 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001338 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001339 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001340 }
1341
1342 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001343 ret = fill_zero(inode, pg_end, 0, off_end);
1344 if (ret)
1345 goto out;
1346
Chao Yu75cd4e02015-05-06 13:11:13 +08001347 new_size = max_t(loff_t, new_size, offset + len);
1348 }
1349 }
1350
1351out:
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001352 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001353 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001354out_sem:
1355 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001356
1357 return ret;
1358}
1359
Chao Yuf62185d2015-05-28 19:16:57 +08001360static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1361{
1362 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001363 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001364 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001365 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001366
Chao Yuf62185d2015-05-28 19:16:57 +08001367 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001368 ret = inode_newsize_ok(inode, new_size);
1369 if (ret)
1370 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001371
1372 if (offset >= i_size_read(inode))
1373 return -EINVAL;
1374
1375 /* insert range should be aligned to block size of f2fs. */
1376 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1377 return -EINVAL;
1378
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001379 ret = f2fs_convert_inline_inode(inode);
1380 if (ret)
1381 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001382
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001383 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001384
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001385 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001386 ret = truncate_blocks(inode, i_size_read(inode), true);
1387 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001388 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001389
1390 /* write out all dirty pages from offset */
1391 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1392 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001393 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001394
Chao Yubb066642017-11-03 10:21:05 +08001395 /* avoid gc operation during block exchange */
1396 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1397
Chao Yuf62185d2015-05-28 19:16:57 +08001398 truncate_pagecache(inode, offset);
1399
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001400 pg_start = offset >> PAGE_SHIFT;
1401 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001402 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001403 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001404
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001405 while (!ret && idx > pg_start) {
1406 nr = idx - pg_start;
1407 if (nr > delta)
1408 nr = delta;
1409 idx -= nr;
1410
Chao Yuf62185d2015-05-28 19:16:57 +08001411 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001412 f2fs_drop_extent_tree(inode);
1413
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001414 ret = __exchange_data_block(inode, inode, idx,
1415 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001416 f2fs_unlock_op(sbi);
1417 }
1418
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001419 /* write out all moved pages, if possible */
1420 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1421 truncate_pagecache(inode, offset);
1422
1423 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001424 f2fs_i_size_write(inode, new_size);
Chao Yubb066642017-11-03 10:21:05 +08001425
1426 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001427out:
1428 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001429 return ret;
1430}
1431
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001432static int expand_inode_data(struct inode *inode, loff_t offset,
1433 loff_t len, int mode)
1434{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001435 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001436 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1437 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001438 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001439 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001440 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001441
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001442 err = inode_newsize_ok(inode, (len + offset));
1443 if (err)
1444 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001445
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001446 err = f2fs_convert_inline_inode(inode);
1447 if (err)
1448 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001449
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001450 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001451
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001452 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001453 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001454
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001455 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1456 map.m_len = pg_end - map.m_lblk;
1457 if (off_end)
1458 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001459
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001460 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1461 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001462 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001463
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001464 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001465 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001466
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001467 last_off = map.m_lblk + map.m_len - 1;
1468
1469 /* update new size to the failed position */
1470 new_size = (last_off == pg_end) ? offset + len:
1471 (loff_t)(last_off + 1) << PAGE_SHIFT;
1472 } else {
1473 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001474 }
1475
Chao Yue8ed90a2017-11-05 21:53:30 +08001476 if (new_size > i_size_read(inode)) {
1477 if (mode & FALLOC_FL_KEEP_SIZE)
1478 file_set_keep_isize(inode);
1479 else
1480 f2fs_i_size_write(inode, new_size);
1481 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001482
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001483 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001484}
1485
1486static long f2fs_fallocate(struct file *file, int mode,
1487 loff_t offset, loff_t len)
1488{
Al Viro6131ffa2013-02-27 16:59:05 -05001489 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001490 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001491
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001492 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1493 return -EIO;
1494
Chao Yuc9980122015-09-11 14:39:02 +08001495 /* f2fs only support ->fallocate for regular file */
1496 if (!S_ISREG(inode->i_mode))
1497 return -EINVAL;
1498
Chao Yuf62185d2015-05-28 19:16:57 +08001499 if (f2fs_encrypted_inode(inode) &&
1500 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001501 return -EOPNOTSUPP;
1502
Chao Yub4ace332015-05-06 13:09:46 +08001503 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001504 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1505 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001506 return -EOPNOTSUPP;
1507
Al Viro59551022016-01-22 15:40:57 -05001508 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001509
Taehee Yoo587c0a42015-04-21 15:59:12 +09001510 if (mode & FALLOC_FL_PUNCH_HOLE) {
1511 if (offset >= inode->i_size)
1512 goto out;
1513
Chao Yua66c7b22013-11-22 16:52:50 +08001514 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001515 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1516 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001517 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1518 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001519 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1520 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001521 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001522 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001523 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001524
Namjae Jeon3af60a42012-12-30 14:52:37 +09001525 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001526 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001527 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001528 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001529 }
Chao Yu3375f692014-01-28 10:29:26 +08001530
Taehee Yoo587c0a42015-04-21 15:59:12 +09001531out:
Al Viro59551022016-01-22 15:40:57 -05001532 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001533
Namjae Jeonc01e2852013-04-23 17:00:52 +09001534 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001535 return ret;
1536}
1537
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001538static int f2fs_release_file(struct inode *inode, struct file *filp)
1539{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001540 /*
1541 * f2fs_relase_file is called at every close calls. So we should
1542 * not drop any inmemory pages by close called by other process.
1543 */
1544 if (!(filp->f_mode & FMODE_WRITE) ||
1545 atomic_read(&inode->i_writecount) != 1)
1546 return 0;
1547
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001548 /* some remained atomic pages should discarded */
1549 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001550 drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001551 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001552 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001553 stat_dec_volatile_write(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001554 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001555 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001556 clear_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001557 }
1558 return 0;
1559}
1560
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001561static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001562{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001563 struct inode *inode = file_inode(file);
1564
1565 /*
1566 * If the process doing a transaction is crashed, we should do
1567 * roll-back. Otherwise, other reader/write can see corrupted database
1568 * until all the writers close its file. Since this should be done
1569 * before dropping file lock, it needs to do in ->flush.
1570 */
1571 if (f2fs_is_atomic_file(inode) &&
1572 F2FS_I(inode)->inmem_task == current)
1573 drop_inmem_pages(inode);
1574 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001575}
1576
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001577static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001578{
Al Viro6131ffa2013-02-27 16:59:05 -05001579 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001580 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08001581 unsigned int flags = fi->i_flags &
1582 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001583 return put_user(flags, (int __user *)arg);
1584}
1585
Chao Yu2c1d0302017-07-29 00:32:52 +08001586static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1587{
1588 struct f2fs_inode_info *fi = F2FS_I(inode);
1589 unsigned int oldflags;
1590
1591 /* Is it quota file? Do not allow user to mess with it */
1592 if (IS_NOQUOTA(inode))
1593 return -EPERM;
1594
1595 flags = f2fs_mask_flags(inode->i_mode, flags);
1596
1597 oldflags = fi->i_flags;
1598
1599 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL))
1600 if (!capable(CAP_LINUX_IMMUTABLE))
1601 return -EPERM;
1602
1603 flags = flags & (FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1604 flags |= oldflags & ~(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1605 fi->i_flags = flags;
1606
1607 if (fi->i_flags & FS_PROJINHERIT_FL)
1608 set_inode_flag(inode, FI_PROJ_INHERIT);
1609 else
1610 clear_inode_flag(inode, FI_PROJ_INHERIT);
1611
1612 inode->i_ctime = current_time(inode);
1613 f2fs_set_inode_flags(inode);
1614 f2fs_mark_inode_dirty_sync(inode, false);
1615 return 0;
1616}
1617
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001618static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1619{
1620 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001621 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001622 int ret;
1623
Chao Yu7fb17fe2016-05-09 19:56:32 +08001624 if (!inode_owner_or_capable(inode))
1625 return -EACCES;
1626
1627 if (get_user(flags, (int __user *)arg))
1628 return -EFAULT;
1629
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001630 ret = mnt_want_write_file(filp);
1631 if (ret)
1632 return ret;
1633
Al Viro59551022016-01-22 15:40:57 -05001634 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001635
Chao Yu2c1d0302017-07-29 00:32:52 +08001636 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001637
Chao Yua72d4b92017-05-03 23:59:13 +08001638 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001639 mnt_drop_write_file(filp);
1640 return ret;
1641}
1642
Chao Yud49f3e82015-01-23 20:36:04 +08001643static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1644{
1645 struct inode *inode = file_inode(filp);
1646
1647 return put_user(inode->i_generation, (int __user *)arg);
1648}
1649
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001650static int f2fs_ioc_start_atomic_write(struct file *filp)
1651{
1652 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001653 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001654
1655 if (!inode_owner_or_capable(inode))
1656 return -EACCES;
1657
Jaegeuk Kime8118982017-03-17 10:04:15 +08001658 if (!S_ISREG(inode->i_mode))
1659 return -EINVAL;
1660
Chao Yu7fb17fe2016-05-09 19:56:32 +08001661 ret = mnt_want_write_file(filp);
1662 if (ret)
1663 return ret;
1664
Chao Yu0fac5582016-05-09 19:56:33 +08001665 inode_lock(inode);
1666
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001667 if (f2fs_is_atomic_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001668 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001669
Chao Yuf4c9c742015-07-17 18:06:35 +08001670 ret = f2fs_convert_inline_inode(inode);
1671 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001672 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001673
Jaegeuk Kim91942322016-05-20 10:13:22 -07001674 set_inode_flag(inode, FI_ATOMIC_FILE);
Jaegeuk Kim6c3acd92017-04-12 10:01:33 -07001675 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001676 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1677
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001678 if (!get_dirty_pages(inode))
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001679 goto inc_stat;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001680
1681 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001682 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001683 inode->i_ino, get_dirty_pages(inode));
1684 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001685 if (ret) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001686 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu84a23fb2017-08-18 23:37:36 +08001687 clear_inode_flag(inode, FI_HOT_DATA);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001688 goto out;
1689 }
1690
1691inc_stat:
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001692 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001693 stat_inc_atomic_write(inode);
1694 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001695out:
Chao Yu0fac5582016-05-09 19:56:33 +08001696 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001697 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001698 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001699}
1700
1701static int f2fs_ioc_commit_atomic_write(struct file *filp)
1702{
1703 struct inode *inode = file_inode(filp);
1704 int ret;
1705
1706 if (!inode_owner_or_capable(inode))
1707 return -EACCES;
1708
1709 ret = mnt_want_write_file(filp);
1710 if (ret)
1711 return ret;
1712
Chao Yu0fac5582016-05-09 19:56:33 +08001713 inode_lock(inode);
1714
Chao Yu7fb17fe2016-05-09 19:56:32 +08001715 if (f2fs_is_volatile_file(inode))
1716 goto err_out;
1717
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001718 if (f2fs_is_atomic_file(inode)) {
Chao Yu29b96b52016-02-06 14:38:29 +08001719 ret = commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001720 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001721 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001722
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001723 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001724 if (!ret) {
1725 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu84a23fb2017-08-18 23:37:36 +08001726 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu5fe45742017-01-07 18:50:26 +08001727 stat_dec_atomic_write(inode);
1728 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001729 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001730 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001731 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001732err_out:
Chao Yu0fac5582016-05-09 19:56:33 +08001733 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001734 mnt_drop_write_file(filp);
1735 return ret;
1736}
1737
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001738static int f2fs_ioc_start_volatile_write(struct file *filp)
1739{
1740 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001741 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001742
1743 if (!inode_owner_or_capable(inode))
1744 return -EACCES;
1745
Chao Yu8ff09712017-03-17 15:43:57 +08001746 if (!S_ISREG(inode->i_mode))
1747 return -EINVAL;
1748
Chao Yu7fb17fe2016-05-09 19:56:32 +08001749 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001750 if (ret)
1751 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001752
Chao Yu0fac5582016-05-09 19:56:33 +08001753 inode_lock(inode);
1754
Chao Yu7fb17fe2016-05-09 19:56:32 +08001755 if (f2fs_is_volatile_file(inode))
1756 goto out;
1757
1758 ret = f2fs_convert_inline_inode(inode);
1759 if (ret)
1760 goto out;
1761
Chao Yu648d50b2017-03-22 17:23:45 +08001762 stat_inc_volatile_write(inode);
1763 stat_update_max_volatile_write(inode);
1764
Jaegeuk Kim91942322016-05-20 10:13:22 -07001765 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001766 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001767out:
Chao Yu0fac5582016-05-09 19:56:33 +08001768 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001769 mnt_drop_write_file(filp);
1770 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001771}
1772
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001773static int f2fs_ioc_release_volatile_write(struct file *filp)
1774{
1775 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001776 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001777
1778 if (!inode_owner_or_capable(inode))
1779 return -EACCES;
1780
Chao Yu7fb17fe2016-05-09 19:56:32 +08001781 ret = mnt_want_write_file(filp);
1782 if (ret)
1783 return ret;
1784
Chao Yu0fac5582016-05-09 19:56:33 +08001785 inode_lock(inode);
1786
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001787 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001788 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001789
Chao Yu7fb17fe2016-05-09 19:56:32 +08001790 if (!f2fs_is_first_block_written(inode)) {
1791 ret = truncate_partial_data_page(inode, 0, true);
1792 goto out;
1793 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001794
Chao Yu7fb17fe2016-05-09 19:56:32 +08001795 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1796out:
Chao Yu0fac5582016-05-09 19:56:33 +08001797 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001798 mnt_drop_write_file(filp);
1799 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001800}
1801
1802static int f2fs_ioc_abort_volatile_write(struct file *filp)
1803{
1804 struct inode *inode = file_inode(filp);
1805 int ret;
1806
1807 if (!inode_owner_or_capable(inode))
1808 return -EACCES;
1809
1810 ret = mnt_want_write_file(filp);
1811 if (ret)
1812 return ret;
1813
Chao Yu0fac5582016-05-09 19:56:33 +08001814 inode_lock(inode);
1815
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001816 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001817 drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001818 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001819 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001820 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001821 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001822 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001823
Chao Yu0fac5582016-05-09 19:56:33 +08001824 inode_unlock(inode);
1825
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001826 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001827 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001828 return ret;
1829}
1830
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001831static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1832{
1833 struct inode *inode = file_inode(filp);
1834 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1835 struct super_block *sb = sbi->sb;
1836 __u32 in;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001837 int ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001838
1839 if (!capable(CAP_SYS_ADMIN))
1840 return -EPERM;
1841
1842 if (get_user(in, (__u32 __user *)arg))
1843 return -EFAULT;
1844
Chao Yu7fb17fe2016-05-09 19:56:32 +08001845 ret = mnt_want_write_file(filp);
1846 if (ret)
1847 return ret;
1848
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001849 switch (in) {
1850 case F2FS_GOING_DOWN_FULLSYNC:
1851 sb = freeze_bdev(sb->s_bdev);
1852 if (sb && !IS_ERR(sb)) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001853 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001854 thaw_bdev(sb->s_bdev, sb);
1855 }
1856 break;
1857 case F2FS_GOING_DOWN_METASYNC:
1858 /* do checkpoint only */
1859 f2fs_sync_fs(sb, 1);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001860 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001861 break;
1862 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001863 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001864 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001865 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yub0af6d42017-08-02 23:21:48 +08001866 sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001867 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001868 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001869 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001870 ret = -EINVAL;
1871 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001872 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001873 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001874out:
1875 mnt_drop_write_file(filp);
1876 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001877}
1878
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001879static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1880{
1881 struct inode *inode = file_inode(filp);
1882 struct super_block *sb = inode->i_sb;
1883 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1884 struct fstrim_range range;
1885 int ret;
1886
1887 if (!capable(CAP_SYS_ADMIN))
1888 return -EPERM;
1889
1890 if (!blk_queue_discard(q))
1891 return -EOPNOTSUPP;
1892
1893 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1894 sizeof(range)))
1895 return -EFAULT;
1896
Chao Yu7fb17fe2016-05-09 19:56:32 +08001897 ret = mnt_want_write_file(filp);
1898 if (ret)
1899 return ret;
1900
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001901 range.minlen = max((unsigned int)range.minlen,
1902 q->limits.discard_granularity);
1903 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001904 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001905 if (ret < 0)
1906 return ret;
1907
1908 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1909 sizeof(range)))
1910 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001911 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001912 return 0;
1913}
1914
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001915static bool uuid_is_nonzero(__u8 u[16])
1916{
1917 int i;
1918
1919 for (i = 0; i < 16; i++)
1920 if (u[i])
1921 return true;
1922 return false;
1923}
1924
1925static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1926{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001927 struct inode *inode = file_inode(filp);
1928
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001929 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001930
Eric Biggersdb717d82016-11-26 19:07:49 -05001931 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001932}
1933
1934static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1935{
Eric Biggersdb717d82016-11-26 19:07:49 -05001936 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001937}
1938
1939static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1940{
1941 struct inode *inode = file_inode(filp);
1942 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1943 int err;
1944
1945 if (!f2fs_sb_has_crypto(inode->i_sb))
1946 return -EOPNOTSUPP;
1947
1948 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1949 goto got_it;
1950
1951 err = mnt_want_write_file(filp);
1952 if (err)
1953 return err;
1954
1955 /* update superblock with uuid */
1956 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1957
Chao Yuc5bda1c2015-06-08 13:28:03 +08001958 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001959 if (err) {
1960 /* undo new data */
1961 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yue8240f62015-12-15 17:19:26 +08001962 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001963 return err;
1964 }
Chao Yue8240f62015-12-15 17:19:26 +08001965 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001966got_it:
1967 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1968 16))
1969 return -EFAULT;
1970 return 0;
1971}
1972
Chao Yuc1c1b582015-07-10 18:08:10 +08001973static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1974{
1975 struct inode *inode = file_inode(filp);
1976 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08001977 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001978 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08001979
1980 if (!capable(CAP_SYS_ADMIN))
1981 return -EPERM;
1982
Chao Yud530d4d2015-10-05 22:22:44 +08001983 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08001984 return -EFAULT;
1985
Chao Yud530d4d2015-10-05 22:22:44 +08001986 if (f2fs_readonly(sbi->sb))
1987 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08001988
Chao Yu7fb17fe2016-05-09 19:56:32 +08001989 ret = mnt_want_write_file(filp);
1990 if (ret)
1991 return ret;
1992
Chao Yud530d4d2015-10-05 22:22:44 +08001993 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08001994 if (!mutex_trylock(&sbi->gc_mutex)) {
1995 ret = -EBUSY;
1996 goto out;
1997 }
Chao Yud530d4d2015-10-05 22:22:44 +08001998 } else {
1999 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002000 }
2001
Jaegeuk Kime066b832017-04-13 15:17:00 -07002002 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002003out:
2004 mnt_drop_write_file(filp);
2005 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002006}
2007
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002008static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2009{
2010 struct inode *inode = file_inode(filp);
2011 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2012 struct f2fs_gc_range range;
2013 u64 end;
2014 int ret;
2015
2016 if (!capable(CAP_SYS_ADMIN))
2017 return -EPERM;
2018
2019 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2020 sizeof(range)))
2021 return -EFAULT;
2022
2023 if (f2fs_readonly(sbi->sb))
2024 return -EROFS;
2025
2026 ret = mnt_want_write_file(filp);
2027 if (ret)
2028 return ret;
2029
2030 end = range.start + range.len;
2031 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi))
2032 return -EINVAL;
2033do_more:
2034 if (!range.sync) {
2035 if (!mutex_trylock(&sbi->gc_mutex)) {
2036 ret = -EBUSY;
2037 goto out;
2038 }
2039 } else {
2040 mutex_lock(&sbi->gc_mutex);
2041 }
2042
2043 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2044 range.start += sbi->blocks_per_seg;
2045 if (range.start <= end)
2046 goto do_more;
2047out:
2048 mnt_drop_write_file(filp);
2049 return ret;
2050}
2051
Chao Yu456b88e2015-10-05 22:24:19 +08002052static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2053{
2054 struct inode *inode = file_inode(filp);
2055 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002056 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002057
2058 if (!capable(CAP_SYS_ADMIN))
2059 return -EPERM;
2060
2061 if (f2fs_readonly(sbi->sb))
2062 return -EROFS;
2063
Chao Yu7fb17fe2016-05-09 19:56:32 +08002064 ret = mnt_want_write_file(filp);
2065 if (ret)
2066 return ret;
2067
2068 ret = f2fs_sync_fs(sbi->sb, 1);
2069
2070 mnt_drop_write_file(filp);
2071 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002072}
2073
Chao Yud323d002015-10-27 09:53:45 +08002074static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2075 struct file *filp,
2076 struct f2fs_defragment *range)
2077{
2078 struct inode *inode = file_inode(filp);
Chao Yuda859852016-01-26 15:42:58 +08002079 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
Hou Pengyange15882b2017-02-23 09:18:05 +00002080 struct extent_info ei = {0,0,0};
Chao Yud323d002015-10-27 09:53:45 +08002081 pgoff_t pg_start, pg_end;
Chao Yu3519e3f2015-12-01 11:56:52 +08002082 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002083 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002084 block_t blk_end = 0;
2085 bool fragmented = false;
2086 int err;
2087
2088 /* if in-place-update policy is enabled, don't waste time here */
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002089 if (need_inplace_update_policy(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002090 return -EINVAL;
2091
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002092 pg_start = range->start >> PAGE_SHIFT;
2093 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002094
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002095 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002096
Al Viro59551022016-01-22 15:40:57 -05002097 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002098
2099 /* writeback all dirty pages in the range */
2100 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002101 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002102 if (err)
2103 goto out;
2104
2105 /*
2106 * lookup mapping info in extent cache, skip defragmenting if physical
2107 * block addresses are continuous.
2108 */
2109 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2110 if (ei.fofs + ei.len >= pg_end)
2111 goto out;
2112 }
2113
2114 map.m_lblk = pg_start;
Chao Yud323d002015-10-27 09:53:45 +08002115
2116 /*
2117 * lookup mapping info in dnode page cache, skip defragmenting if all
2118 * physical block addresses are continuous even if there are hole(s)
2119 * in logical blocks.
2120 */
2121 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002122 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002123 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002124 if (err)
2125 goto out;
2126
2127 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2128 map.m_lblk++;
Chao Yud323d002015-10-27 09:53:45 +08002129 continue;
2130 }
2131
2132 if (blk_end && blk_end != map.m_pblk) {
2133 fragmented = true;
2134 break;
2135 }
2136 blk_end = map.m_pblk + map.m_len;
2137
2138 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002139 }
2140
2141 if (!fragmented)
2142 goto out;
2143
2144 map.m_lblk = pg_start;
2145 map.m_len = pg_end - pg_start;
2146
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002147 sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002148
2149 /*
2150 * make sure there are enough free section for LFS allocation, this can
2151 * avoid defragment running in SSR mode when free section are allocated
2152 * intensively
2153 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002154 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002155 err = -EAGAIN;
2156 goto out;
2157 }
2158
2159 while (map.m_lblk < pg_end) {
2160 pgoff_t idx;
2161 int cnt = 0;
2162
2163do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002164 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002165 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002166 if (err)
2167 goto clear_out;
2168
2169 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2170 map.m_lblk++;
2171 continue;
2172 }
2173
Jaegeuk Kim91942322016-05-20 10:13:22 -07002174 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002175
2176 idx = map.m_lblk;
2177 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2178 struct page *page;
2179
2180 page = get_lock_data_page(inode, idx, true);
2181 if (IS_ERR(page)) {
2182 err = PTR_ERR(page);
2183 goto clear_out;
2184 }
2185
2186 set_page_dirty(page);
2187 f2fs_put_page(page, 1);
2188
2189 idx++;
2190 cnt++;
2191 total++;
2192 }
2193
2194 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002195
2196 if (idx < pg_end && cnt < blk_per_seg)
2197 goto do_map;
2198
Jaegeuk Kim91942322016-05-20 10:13:22 -07002199 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002200
2201 err = filemap_fdatawrite(inode->i_mapping);
2202 if (err)
2203 goto out;
2204 }
2205clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002206 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002207out:
Al Viro59551022016-01-22 15:40:57 -05002208 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002209 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002210 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002211 return err;
2212}
2213
2214static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2215{
2216 struct inode *inode = file_inode(filp);
2217 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2218 struct f2fs_defragment range;
2219 int err;
2220
2221 if (!capable(CAP_SYS_ADMIN))
2222 return -EPERM;
2223
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002224 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002225 return -EINVAL;
2226
Kinglong Meed7563862017-03-10 17:55:07 +08002227 if (f2fs_readonly(sbi->sb))
2228 return -EROFS;
2229
2230 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2231 sizeof(range)))
2232 return -EFAULT;
2233
2234 /* verify alignment of offset & size */
2235 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2236 return -EINVAL;
2237
2238 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2239 sbi->max_file_blocks))
2240 return -EINVAL;
2241
Chao Yud323d002015-10-27 09:53:45 +08002242 err = mnt_want_write_file(filp);
2243 if (err)
2244 return err;
2245
Chao Yud323d002015-10-27 09:53:45 +08002246 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002247 mnt_drop_write_file(filp);
2248
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002249 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002250 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002251 return err;
Chao Yud323d002015-10-27 09:53:45 +08002252
2253 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2254 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002255 return -EFAULT;
2256
2257 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002258}
2259
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002260static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2261 struct file *file_out, loff_t pos_out, size_t len)
2262{
2263 struct inode *src = file_inode(file_in);
2264 struct inode *dst = file_inode(file_out);
2265 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2266 size_t olen = len, dst_max_i_size = 0;
2267 size_t dst_osize;
2268 int ret;
2269
2270 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2271 src->i_sb != dst->i_sb)
2272 return -EXDEV;
2273
2274 if (unlikely(f2fs_readonly(src->i_sb)))
2275 return -EROFS;
2276
Chao Yufe8494b2016-08-04 20:13:02 +08002277 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2278 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002279
2280 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2281 return -EOPNOTSUPP;
2282
Fan Lid95fd912016-09-13 11:35:42 +08002283 if (src == dst) {
2284 if (pos_in == pos_out)
2285 return 0;
2286 if (pos_out > pos_in && pos_out < pos_in + len)
2287 return -EINVAL;
2288 }
2289
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002290 inode_lock(src);
Chao Yubb066642017-11-03 10:21:05 +08002291 down_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Chao Yu20a3d612016-08-04 20:13:03 +08002292 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002293 ret = -EBUSY;
2294 if (!inode_trylock(dst))
2295 goto out;
2296 if (!down_write_trylock(&F2FS_I(dst)->dio_rwsem[WRITE])) {
2297 inode_unlock(dst);
Chao Yu20a3d612016-08-04 20:13:03 +08002298 goto out;
2299 }
2300 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002301
2302 ret = -EINVAL;
2303 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2304 goto out_unlock;
2305 if (len == 0)
2306 olen = len = src->i_size - pos_in;
2307 if (pos_in + len == src->i_size)
2308 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2309 if (len == 0) {
2310 ret = 0;
2311 goto out_unlock;
2312 }
2313
2314 dst_osize = dst->i_size;
2315 if (pos_out + olen > dst->i_size)
2316 dst_max_i_size = pos_out + olen;
2317
2318 /* verify the end result is block aligned */
2319 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2320 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2321 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2322 goto out_unlock;
2323
2324 ret = f2fs_convert_inline_inode(src);
2325 if (ret)
2326 goto out_unlock;
2327
2328 ret = f2fs_convert_inline_inode(dst);
2329 if (ret)
2330 goto out_unlock;
2331
2332 /* write out all dirty pages from offset */
2333 ret = filemap_write_and_wait_range(src->i_mapping,
2334 pos_in, pos_in + len);
2335 if (ret)
2336 goto out_unlock;
2337
2338 ret = filemap_write_and_wait_range(dst->i_mapping,
2339 pos_out, pos_out + len);
2340 if (ret)
2341 goto out_unlock;
2342
2343 f2fs_balance_fs(sbi, true);
2344 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002345 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2346 pos_out >> F2FS_BLKSIZE_BITS,
2347 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002348
2349 if (!ret) {
2350 if (dst_max_i_size)
2351 f2fs_i_size_write(dst, dst_max_i_size);
2352 else if (dst_osize != dst->i_size)
2353 f2fs_i_size_write(dst, dst_osize);
2354 }
2355 f2fs_unlock_op(sbi);
2356out_unlock:
Chao Yubb066642017-11-03 10:21:05 +08002357 if (src != dst) {
2358 up_write(&F2FS_I(dst)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002359 inode_unlock(dst);
Chao Yubb066642017-11-03 10:21:05 +08002360 }
Chao Yu20a3d612016-08-04 20:13:03 +08002361out:
Chao Yubb066642017-11-03 10:21:05 +08002362 up_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002363 inode_unlock(src);
2364 return ret;
2365}
2366
2367static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2368{
2369 struct f2fs_move_range range;
2370 struct fd dst;
2371 int err;
2372
2373 if (!(filp->f_mode & FMODE_READ) ||
2374 !(filp->f_mode & FMODE_WRITE))
2375 return -EBADF;
2376
2377 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2378 sizeof(range)))
2379 return -EFAULT;
2380
2381 dst = fdget(range.dst_fd);
2382 if (!dst.file)
2383 return -EBADF;
2384
2385 if (!(dst.file->f_mode & FMODE_WRITE)) {
2386 err = -EBADF;
2387 goto err_out;
2388 }
2389
2390 err = mnt_want_write_file(filp);
2391 if (err)
2392 goto err_out;
2393
2394 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2395 range.pos_out, range.len);
2396
2397 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002398 if (err)
2399 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002400
2401 if (copy_to_user((struct f2fs_move_range __user *)arg,
2402 &range, sizeof(range)))
2403 err = -EFAULT;
2404err_out:
2405 fdput(dst);
2406 return err;
2407}
2408
Jaegeuk Kime066b832017-04-13 15:17:00 -07002409static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2410{
2411 struct inode *inode = file_inode(filp);
2412 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2413 struct sit_info *sm = SIT_I(sbi);
2414 unsigned int start_segno = 0, end_segno = 0;
2415 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2416 struct f2fs_flush_device range;
2417 int ret;
2418
2419 if (!capable(CAP_SYS_ADMIN))
2420 return -EPERM;
2421
2422 if (f2fs_readonly(sbi->sb))
2423 return -EROFS;
2424
2425 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2426 sizeof(range)))
2427 return -EFAULT;
2428
2429 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2430 sbi->segs_per_sec != 1) {
2431 f2fs_msg(sbi->sb, KERN_WARNING,
2432 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2433 range.dev_num, sbi->s_ndevs,
2434 sbi->segs_per_sec);
2435 return -EINVAL;
2436 }
2437
2438 ret = mnt_want_write_file(filp);
2439 if (ret)
2440 return ret;
2441
2442 if (range.dev_num != 0)
2443 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2444 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2445
2446 start_segno = sm->last_victim[FLUSH_DEVICE];
2447 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2448 start_segno = dev_start_segno;
2449 end_segno = min(start_segno + range.segments, dev_end_segno);
2450
2451 while (start_segno < end_segno) {
2452 if (!mutex_trylock(&sbi->gc_mutex)) {
2453 ret = -EBUSY;
2454 goto out;
2455 }
2456 sm->last_victim[GC_CB] = end_segno + 1;
2457 sm->last_victim[GC_GREEDY] = end_segno + 1;
2458 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2459 ret = f2fs_gc(sbi, true, true, start_segno);
2460 if (ret == -EAGAIN)
2461 ret = 0;
2462 else if (ret < 0)
2463 break;
2464 start_segno++;
2465 }
2466out:
2467 mnt_drop_write_file(filp);
2468 return ret;
2469}
2470
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002471static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2472{
2473 struct inode *inode = file_inode(filp);
2474 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2475
2476 /* Must validate to set it with SQLite behavior in Android. */
2477 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2478
2479 return put_user(sb_feature, (u32 __user *)arg);
2480}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002481
Chao Yu2c1d0302017-07-29 00:32:52 +08002482#ifdef CONFIG_QUOTA
2483static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2484{
2485 struct inode *inode = file_inode(filp);
2486 struct f2fs_inode_info *fi = F2FS_I(inode);
2487 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2488 struct super_block *sb = sbi->sb;
2489 struct dquot *transfer_to[MAXQUOTAS] = {};
2490 struct page *ipage;
2491 kprojid_t kprojid;
2492 int err;
2493
2494 if (!f2fs_sb_has_project_quota(sb)) {
2495 if (projid != F2FS_DEF_PROJID)
2496 return -EOPNOTSUPP;
2497 else
2498 return 0;
2499 }
2500
2501 if (!f2fs_has_extra_attr(inode))
2502 return -EOPNOTSUPP;
2503
2504 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2505
2506 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2507 return 0;
2508
2509 err = mnt_want_write_file(filp);
2510 if (err)
2511 return err;
2512
2513 err = -EPERM;
2514 inode_lock(inode);
2515
2516 /* Is it quota file? Do not allow user to mess with it */
2517 if (IS_NOQUOTA(inode))
2518 goto out_unlock;
2519
2520 ipage = get_node_page(sbi, inode->i_ino);
2521 if (IS_ERR(ipage)) {
2522 err = PTR_ERR(ipage);
2523 goto out_unlock;
2524 }
2525
2526 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2527 i_projid)) {
2528 err = -EOVERFLOW;
2529 f2fs_put_page(ipage, 1);
2530 goto out_unlock;
2531 }
2532 f2fs_put_page(ipage, 1);
2533
2534 dquot_initialize(inode);
2535
2536 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2537 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2538 err = __dquot_transfer(inode, transfer_to);
2539 dqput(transfer_to[PRJQUOTA]);
2540 if (err)
2541 goto out_dirty;
2542 }
2543
2544 F2FS_I(inode)->i_projid = kprojid;
2545 inode->i_ctime = current_time(inode);
2546out_dirty:
2547 f2fs_mark_inode_dirty_sync(inode, true);
2548out_unlock:
2549 inode_unlock(inode);
2550 mnt_drop_write_file(filp);
2551 return err;
2552}
2553#else
2554static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2555{
2556 if (projid != F2FS_DEF_PROJID)
2557 return -EOPNOTSUPP;
2558 return 0;
2559}
2560#endif
2561
2562/* Transfer internal flags to xflags */
2563static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2564{
2565 __u32 xflags = 0;
2566
2567 if (iflags & FS_SYNC_FL)
2568 xflags |= FS_XFLAG_SYNC;
2569 if (iflags & FS_IMMUTABLE_FL)
2570 xflags |= FS_XFLAG_IMMUTABLE;
2571 if (iflags & FS_APPEND_FL)
2572 xflags |= FS_XFLAG_APPEND;
2573 if (iflags & FS_NODUMP_FL)
2574 xflags |= FS_XFLAG_NODUMP;
2575 if (iflags & FS_NOATIME_FL)
2576 xflags |= FS_XFLAG_NOATIME;
2577 if (iflags & FS_PROJINHERIT_FL)
2578 xflags |= FS_XFLAG_PROJINHERIT;
2579 return xflags;
2580}
2581
2582#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2583 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2584 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2585
2586/* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
2587#define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \
2588 FS_IMMUTABLE_FL | \
2589 FS_APPEND_FL | \
2590 FS_NODUMP_FL | \
2591 FS_NOATIME_FL | \
2592 FS_PROJINHERIT_FL)
2593
2594/* Transfer xflags flags to internal */
2595static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2596{
2597 unsigned long iflags = 0;
2598
2599 if (xflags & FS_XFLAG_SYNC)
2600 iflags |= FS_SYNC_FL;
2601 if (xflags & FS_XFLAG_IMMUTABLE)
2602 iflags |= FS_IMMUTABLE_FL;
2603 if (xflags & FS_XFLAG_APPEND)
2604 iflags |= FS_APPEND_FL;
2605 if (xflags & FS_XFLAG_NODUMP)
2606 iflags |= FS_NODUMP_FL;
2607 if (xflags & FS_XFLAG_NOATIME)
2608 iflags |= FS_NOATIME_FL;
2609 if (xflags & FS_XFLAG_PROJINHERIT)
2610 iflags |= FS_PROJINHERIT_FL;
2611
2612 return iflags;
2613}
2614
2615static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2616{
2617 struct inode *inode = file_inode(filp);
2618 struct f2fs_inode_info *fi = F2FS_I(inode);
2619 struct fsxattr fa;
2620
2621 memset(&fa, 0, sizeof(struct fsxattr));
2622 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
2623 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL));
2624
2625 if (f2fs_sb_has_project_quota(inode->i_sb))
2626 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2627 fi->i_projid);
2628
2629 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2630 return -EFAULT;
2631 return 0;
2632}
2633
2634static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2635{
2636 struct inode *inode = file_inode(filp);
2637 struct f2fs_inode_info *fi = F2FS_I(inode);
2638 struct fsxattr fa;
2639 unsigned int flags;
2640 int err;
2641
2642 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2643 return -EFAULT;
2644
2645 /* Make sure caller has proper permission */
2646 if (!inode_owner_or_capable(inode))
2647 return -EACCES;
2648
2649 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2650 return -EOPNOTSUPP;
2651
2652 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2653 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2654 return -EOPNOTSUPP;
2655
2656 err = mnt_want_write_file(filp);
2657 if (err)
2658 return err;
2659
2660 inode_lock(inode);
2661 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2662 (flags & F2FS_FL_XFLAG_VISIBLE);
2663 err = __f2fs_ioc_setflags(inode, flags);
2664 inode_unlock(inode);
2665 mnt_drop_write_file(filp);
2666 if (err)
2667 return err;
2668
2669 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2670 if (err)
2671 return err;
2672
2673 return 0;
2674}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002675
2676long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2677{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002678 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2679 return -EIO;
2680
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002681 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002682 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002683 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002684 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002685 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002686 case F2FS_IOC_GETVERSION:
2687 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002688 case F2FS_IOC_START_ATOMIC_WRITE:
2689 return f2fs_ioc_start_atomic_write(filp);
2690 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2691 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002692 case F2FS_IOC_START_VOLATILE_WRITE:
2693 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002694 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2695 return f2fs_ioc_release_volatile_write(filp);
2696 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2697 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002698 case F2FS_IOC_SHUTDOWN:
2699 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002700 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002701 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002702 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2703 return f2fs_ioc_set_encryption_policy(filp, arg);
2704 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2705 return f2fs_ioc_get_encryption_policy(filp, arg);
2706 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2707 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002708 case F2FS_IOC_GARBAGE_COLLECT:
2709 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002710 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2711 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002712 case F2FS_IOC_WRITE_CHECKPOINT:
2713 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002714 case F2FS_IOC_DEFRAGMENT:
2715 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002716 case F2FS_IOC_MOVE_RANGE:
2717 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002718 case F2FS_IOC_FLUSH_DEVICE:
2719 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002720 case F2FS_IOC_GET_FEATURES:
2721 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08002722 case F2FS_IOC_FSGETXATTR:
2723 return f2fs_ioc_fsgetxattr(filp, arg);
2724 case F2FS_IOC_FSSETXATTR:
2725 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002726 default:
2727 return -ENOTTY;
2728 }
2729}
2730
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002731static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2732{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002733 struct file *file = iocb->ki_filp;
2734 struct inode *inode = file_inode(file);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002735 struct blk_plug plug;
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002736 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002737
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002738 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2739 return -EIO;
2740
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002741 inode_lock(inode);
2742 ret = generic_write_checks(iocb, from);
2743 if (ret > 0) {
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002744 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002745
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002746 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2747 set_inode_flag(inode, FI_NO_PREALLOC);
2748
2749 err = f2fs_preallocate_blocks(iocb, from);
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002750 if (err) {
2751 inode_unlock(inode);
2752 return err;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002753 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002754 blk_start_plug(&plug);
2755 ret = __generic_file_write_iter(iocb, from);
2756 blk_finish_plug(&plug);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002757 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08002758
2759 if (ret > 0)
2760 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002761 }
2762 inode_unlock(inode);
2763
Christoph Hellwige2592212016-04-07 08:52:01 -07002764 if (ret > 0)
2765 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002766 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002767}
2768
Namjae Jeone9750822013-02-04 23:41:41 +09002769#ifdef CONFIG_COMPAT
2770long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2771{
2772 switch (cmd) {
2773 case F2FS_IOC32_GETFLAGS:
2774 cmd = F2FS_IOC_GETFLAGS;
2775 break;
2776 case F2FS_IOC32_SETFLAGS:
2777 cmd = F2FS_IOC_SETFLAGS;
2778 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08002779 case F2FS_IOC32_GETVERSION:
2780 cmd = F2FS_IOC_GETVERSION;
2781 break;
2782 case F2FS_IOC_START_ATOMIC_WRITE:
2783 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2784 case F2FS_IOC_START_VOLATILE_WRITE:
2785 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2786 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2787 case F2FS_IOC_SHUTDOWN:
2788 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2789 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2790 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2791 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002792 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08002793 case F2FS_IOC_WRITE_CHECKPOINT:
2794 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002795 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07002796 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002797 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08002798 case F2FS_IOC_FSGETXATTR:
2799 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002800 break;
Namjae Jeone9750822013-02-04 23:41:41 +09002801 default:
2802 return -ENOIOCTLCMD;
2803 }
2804 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2805}
2806#endif
2807
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002808const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08002809 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04002810 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002811 .write_iter = f2fs_file_write_iter,
2812 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08002813 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002814 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002815 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002816 .fsync = f2fs_sync_file,
2817 .fallocate = f2fs_fallocate,
2818 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09002819#ifdef CONFIG_COMPAT
2820 .compat_ioctl = f2fs_compat_ioctl,
2821#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002822 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002823 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002824};