blob: 8742c028acc69ca560bc3176bc3e7700389891ef [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002 * fs/f2fs/file.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/stat.h>
14#include <linux/buffer_head.h>
15#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090016#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/falloc.h>
18#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090019#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090020#include <linux/uaccess.h>
21#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090022#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080023#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070024#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070025#include <linux/file.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
30#include "xattr.h"
31#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080032#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080033#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090034#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080036static int f2fs_filemap_fault(struct vm_fault *vmf)
37{
38 struct inode *inode = file_inode(vmf->vma->vm_file);
39 int err;
40
41 down_read(&F2FS_I(inode)->i_mmap_sem);
42 err = filemap_fault(vmf);
43 up_read(&F2FS_I(inode)->i_mmap_sem);
44
45 return err;
46}
47
Dave Jiang11bac802017-02-24 14:56:41 -080048static int f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049{
50 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080051 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090053 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080054 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090055
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020056 if (unlikely(f2fs_cp_error(sbi))) {
57 err = -EIO;
58 goto err;
59 }
60
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090061 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070062
63 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070064
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090065 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080066 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070067 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070068 err = f2fs_reserve_block(&dn, page->index);
69 if (err) {
70 f2fs_unlock_op(sbi);
71 goto out;
72 }
73 f2fs_put_dnode(&dn);
74 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090075
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -080076 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -080077
Dave Jiang11bac802017-02-24 14:56:41 -080078 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080079 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090080 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090081 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090082 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090083 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090084 unlock_page(page);
85 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080086 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090087 }
88
89 /*
90 * check to see if the page is mapped already (no holes)
91 */
92 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090093 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090094
95 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +080097 i_size_read(inode)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090098 unsigned offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 offset = i_size_read(inode) & ~PAGE_MASK;
100 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900101 }
102 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700103 if (!PageUptodate(page))
104 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900105
Chao Yub0af6d42017-08-02 23:21:48 +0800106 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
107
Jaegeuk Kime943a102013-10-25 14:26:31 +0900108 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900109mapped:
110 /* fill the page */
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800111 f2fs_wait_on_page_writeback(page, DATA, false);
Chao Yu08b39fb2015-10-08 13:27:34 +0800112
113 /* wait for GCed encrypted page writeback */
Jaegeuk Kim19585932017-09-05 16:54:24 -0700114 if (f2fs_encrypted_file(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -0700115 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +0800116
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800117out_sem:
118 up_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900119out:
120 sb_end_pagefault(inode->i_sb);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800121 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200122err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900123 return block_page_mkwrite_return(err);
124}
125
126static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800127 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700128 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900129 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900130};
131
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900132static int get_parent_ino(struct inode *inode, nid_t *pino)
133{
134 struct dentry *dentry;
135
136 inode = igrab(inode);
137 dentry = d_find_any_alias(inode);
138 iput(inode);
139 if (!dentry)
140 return 0;
141
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900142 *pino = parent_ino(dentry);
143 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900144 return 1;
145}
146
Chao Yua5fd5052017-11-06 22:51:45 +0800147static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800148{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700149 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800150 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800151
Chao Yua5fd5052017-11-06 22:51:45 +0800152 if (!S_ISREG(inode->i_mode))
153 cp_reason = CP_NON_REGULAR;
154 else if (inode->i_nlink != 1)
155 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700156 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800157 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800158 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800159 cp_reason = CP_WRONG_PINO;
Chao Yu9d1589e2014-08-20 18:37:35 +0800160 else if (!space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800161 cp_reason = CP_NO_SPC_ROLL;
Chao Yu9d1589e2014-08-20 18:37:35 +0800162 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800163 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700164 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800165 cp_reason = CP_FASTBOOT_MODE;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800166 else if (sbi->active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800167 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu9d1589e2014-08-20 18:37:35 +0800168
Chao Yua5fd5052017-11-06 22:51:45 +0800169 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800170}
171
Changman Lee9c7bb702014-12-08 15:29:40 +0900172static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
173{
174 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
175 bool ret = false;
176 /* But we need to avoid that there are some inode updates */
177 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
178 ret = true;
179 f2fs_put_page(i, 0);
180 return ret;
181}
182
Changman Lee51455b12014-12-08 15:29:41 +0900183static void try_to_fix_pino(struct inode *inode)
184{
185 struct f2fs_inode_info *fi = F2FS_I(inode);
186 nid_t pino;
187
188 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900189 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
190 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700191 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900192 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900193 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700194 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900195}
196
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700197static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
198 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900199{
200 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700201 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700202 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900203 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800204 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900206 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900207 .nr_to_write = LONG_MAX,
208 .for_reclaim = 0,
209 };
210
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900211 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900212 return 0;
213
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900214 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700215
216 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800217 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700218 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400219 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700220 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700221
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900222 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800223 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900224 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900225 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900226
Changman Lee9c7bb702014-12-08 15:29:40 +0900227 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800228 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700229 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900230 goto go_write;
231 }
232
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700233 /*
234 * if there is no written data, don't waste time to write recovery info.
235 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700236 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700237 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700238
Changman Lee9c7bb702014-12-08 15:29:40 +0900239 /* it may call write_inode just prior to fsync */
240 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700241 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700242
Jaegeuk Kim91942322016-05-20 10:13:22 -0700243 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700244 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700245 goto flush_out;
246 goto out;
247 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700248go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900249 /*
250 * Both of fdatasync() and fsync() are able to be recovered from
251 * sudden-power-off.
252 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700253 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800254 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700255 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900256
Chao Yua5fd5052017-11-06 22:51:45 +0800257 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900258 /* all the dirty node pages should be flushed for POR */
259 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900260
Changman Lee51455b12014-12-08 15:29:41 +0900261 /*
262 * We've secured consistency through sync_fs. Following pino
263 * will be used only for fsynced inodes after checkpoint.
264 */
265 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700266 clear_inode_flag(inode, FI_APPEND_WRITE);
267 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900268 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900269 }
Changman Lee51455b12014-12-08 15:29:41 +0900270sync_nodes:
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700271 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700272 if (ret)
273 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900274
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800275 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800276 if (unlikely(f2fs_cp_error(sbi))) {
277 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800278 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800279 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800280
Changman Lee51455b12014-12-08 15:29:41 +0900281 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700282 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900283 f2fs_write_inode(inode, NULL);
284 goto sync_nodes;
285 }
286
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700287 /*
288 * If it's atomic_write, it's just fine to keep write ordering. So
289 * here we don't need to wait for node write completion, since we use
290 * node chain which serializes node blocks. If one of node writes are
291 * reordered, we can see simply broken chain, resulting in stopping
292 * roll-forward recovery. It means we'll recover all or none node blocks
293 * given fsync mark.
294 */
295 if (!atomic) {
296 ret = wait_on_node_pages_writeback(sbi, ino);
297 if (ret)
298 goto out;
299 }
Changman Lee51455b12014-12-08 15:29:41 +0900300
301 /* once recovery info is written, don't need to tack this */
Chao Yua49324f2015-12-15 13:29:47 +0800302 remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700303 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900304flush_out:
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -0800305 if (!atomic)
Chao Yu39d787b2017-09-29 13:59:38 +0800306 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800307 if (!ret) {
308 remove_ino_entry(sbi, ino, UPDATE_INO);
309 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu39d787b2017-09-29 13:59:38 +0800310 remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800311 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800312 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900313out:
Chao Yua5fd5052017-11-06 22:51:45 +0800314 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700315 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900316 return ret;
317}
318
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700319int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
320{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200321 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
322 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700323 return f2fs_do_sync_file(file, start, end, datasync, false);
324}
325
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900326static pgoff_t __get_first_dirty_index(struct address_space *mapping,
327 pgoff_t pgofs, int whence)
328{
329 struct pagevec pvec;
330 int nr_pages;
331
332 if (whence != SEEK_DATA)
333 return 0;
334
335 /* find first dirty page index */
336 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700337 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
338 PAGECACHE_TAG_DIRTY, 1);
Chao Yu80dd9c02016-02-24 17:20:44 +0800339 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900340 pagevec_release(&pvec);
341 return pgofs;
342}
343
344static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
345 int whence)
346{
347 switch (whence) {
348 case SEEK_DATA:
349 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
350 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
351 return true;
352 break;
353 case SEEK_HOLE:
354 if (blkaddr == NULL_ADDR)
355 return true;
356 break;
357 }
358 return false;
359}
360
Chao Yu267378d2014-04-23 14:10:24 +0800361static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
362{
363 struct inode *inode = file->f_mapping->host;
364 loff_t maxbytes = inode->i_sb->s_maxbytes;
365 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900366 pgoff_t pgofs, end_offset, dirty;
367 loff_t data_ofs = offset;
368 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800369 int err = 0;
370
Al Viro59551022016-01-22 15:40:57 -0500371 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800372
373 isize = i_size_read(inode);
374 if (offset >= isize)
375 goto fail;
376
377 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800378 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800379 if (whence == SEEK_HOLE)
380 data_ofs = isize;
381 goto found;
382 }
383
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300384 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800385
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900386 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
387
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300388 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800389 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700390 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800391 if (err && err != -ENOENT) {
392 goto fail;
393 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900394 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800395 if (whence == SEEK_DATA) {
Chao Yu3cf45742016-01-26 15:40:44 +0800396 pgofs = get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800397 continue;
398 } else {
399 goto found;
400 }
401 }
402
Chao Yu81ca7352016-01-26 15:39:35 +0800403 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800404
405 /* find data/hole in dnode block */
406 for (; dn.ofs_in_node < end_offset;
407 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300408 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800409 block_t blkaddr;
Chao Yu7a2af762017-07-19 00:19:06 +0800410 blkaddr = datablock_addr(dn.inode,
411 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800412
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900413 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800414 f2fs_put_dnode(&dn);
415 goto found;
416 }
417 }
418 f2fs_put_dnode(&dn);
419 }
420
421 if (whence == SEEK_DATA)
422 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800423found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900424 if (whence == SEEK_HOLE && data_ofs > isize)
425 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500426 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800427 return vfs_setpos(file, data_ofs, maxbytes);
428fail:
Al Viro59551022016-01-22 15:40:57 -0500429 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800430 return -ENXIO;
431}
432
433static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
434{
435 struct inode *inode = file->f_mapping->host;
436 loff_t maxbytes = inode->i_sb->s_maxbytes;
437
438 switch (whence) {
439 case SEEK_SET:
440 case SEEK_CUR:
441 case SEEK_END:
442 return generic_file_llseek_size(file, offset, whence,
443 maxbytes, i_size_read(inode));
444 case SEEK_DATA:
445 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700446 if (offset < 0)
447 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800448 return f2fs_seek_block(file, offset, whence);
449 }
450
451 return -EINVAL;
452}
453
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900454static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
455{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700456 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800457 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700458
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200459 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
460 return -EIO;
461
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700462 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800463 err = f2fs_convert_inline_inode(inode);
464 if (err)
465 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700466
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467 file_accessed(file);
468 vma->vm_ops = &f2fs_file_vm_ops;
469 return 0;
470}
471
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700472static int f2fs_file_open(struct inode *inode, struct file *filp)
473{
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700474 struct dentry *dir;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700475
Chao Yu0abd6752017-07-09 00:13:07 +0800476 if (f2fs_encrypted_inode(inode)) {
477 int ret = fscrypt_get_encryption_info(inode);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700478 if (ret)
Chao Yuae108662016-02-14 18:58:35 +0800479 return -EACCES;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700480 if (!fscrypt_has_encryption_key(inode))
Chao Yuae108662016-02-14 18:58:35 +0800481 return -ENOKEY;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700482 }
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700483 dir = dget_parent(file_dentry(filp));
484 if (f2fs_encrypted_inode(d_inode(dir)) &&
485 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
486 dput(dir);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800487 return -EPERM;
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700488 }
489 dput(dir);
Chao Yu0abd6752017-07-09 00:13:07 +0800490 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700491}
492
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900493int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900494{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700495 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900496 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800497 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900498 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800499 int base = 0;
500
501 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
502 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900503
Gu Zheng45590712013-07-15 17:57:38 +0800504 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800505 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900506
Chris Fries6c311ec2014-01-17 14:44:39 -0600507 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900508 block_t blkaddr = le32_to_cpu(*addr);
509 if (blkaddr == NULL_ADDR)
510 continue;
511
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800512 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800513 set_data_blkaddr(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900514 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700515 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700516 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900517 nr_free++;
518 }
Chao Yu19b2c302015-08-26 20:34:48 +0800519
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900520 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800521 pgoff_t fofs;
522 /*
523 * once we invalidate valid blkaddr in range [ofs, ofs + count],
524 * we will invalidate all blkaddr in the whole range.
525 */
526 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800527 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800528 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900529 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900530 }
531 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900532
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800533 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900534 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
535 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900536 return nr_free;
537}
538
539void truncate_data_blocks(struct dnode_of_data *dn)
540{
541 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
542}
543
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800544static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700545 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900546{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300547 unsigned offset = from & (PAGE_SIZE - 1);
548 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700549 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900550 struct page *page;
551
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700552 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700553 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900554
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700555 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700556 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700557 if (page && PageUptodate(page))
558 goto truncate_out;
559 f2fs_put_page(page, 1);
560 return 0;
561 }
562
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700563 page = get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900564 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800565 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700566truncate_out:
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800567 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300568 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700569
570 /* An encrypted inode should have a key and truncate the last page. */
571 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
572 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800573 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900574 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700575 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900576}
577
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700578int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900579{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700580 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900581 unsigned int blocksize = inode->i_sb->s_blocksize;
582 struct dnode_of_data dn;
583 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800584 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700585 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800586 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900587
Namjae Jeon51dd6242013-04-20 01:28:52 +0900588 trace_f2fs_truncate_blocks_enter(inode, from);
589
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800590 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900591
Chao Yu09210c92016-05-05 19:13:03 +0800592 if (free_from >= sbi->max_file_blocks)
593 goto free_partial;
594
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700595 if (lock)
596 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800597
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700598 ipage = get_node_page(sbi, inode->i_ino);
599 if (IS_ERR(ipage)) {
600 err = PTR_ERR(ipage);
601 goto out;
602 }
603
604 if (f2fs_has_inline_data(inode)) {
Kinglong Meebd4667c2017-03-10 20:43:20 +0800605 truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700606 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800607 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700608 goto out;
609 }
610
611 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700612 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900613 if (err) {
614 if (err == -ENOENT)
615 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700616 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700617 }
618
Chao Yu81ca7352016-01-26 15:39:35 +0800619 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620
621 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700622 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900623
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900624 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
625 truncate_data_blocks_range(&dn, count);
626 free_from += count;
627 }
628
629 f2fs_put_dnode(&dn);
630free_next:
631 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800632out:
633 if (lock)
634 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800635free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700636 /* lastly zero out the first data page */
637 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800638 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639
Namjae Jeon51dd6242013-04-20 01:28:52 +0900640 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900641 return err;
642}
643
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700644int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900645{
Chao Yub0154892015-08-24 17:39:42 +0800646 int err;
647
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200648 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
649 return -EIO;
650
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900651 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
652 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800653 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900654
Namjae Jeon51dd6242013-04-20 01:28:52 +0900655 trace_f2fs_truncate(inode);
656
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800657#ifdef CONFIG_F2FS_FAULT_INJECTION
658 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
659 f2fs_show_injection_info(FAULT_TRUNCATE);
660 return -EIO;
661 }
662#endif
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800663 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800664 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800665 err = f2fs_convert_inline_inode(inode);
666 if (err)
667 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800668 }
669
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700670 err = truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800671 if (err)
672 return err;
673
Deepa Dinamani078cd822016-09-14 07:48:04 -0700674 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700675 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800676 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900677}
678
David Howellsa528d352017-01-31 16:46:22 +0000679int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800680 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900681{
David Howellsa528d352017-01-31 16:46:22 +0000682 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800683 struct f2fs_inode_info *fi = F2FS_I(inode);
684 unsigned int flags;
685
Chao Yu2c1d0302017-07-29 00:32:52 +0800686 flags = fi->i_flags & (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800687 if (flags & FS_APPEND_FL)
688 stat->attributes |= STATX_ATTR_APPEND;
689 if (flags & FS_COMPR_FL)
690 stat->attributes |= STATX_ATTR_COMPRESSED;
691 if (f2fs_encrypted_inode(inode))
692 stat->attributes |= STATX_ATTR_ENCRYPTED;
693 if (flags & FS_IMMUTABLE_FL)
694 stat->attributes |= STATX_ATTR_IMMUTABLE;
695 if (flags & FS_NODUMP_FL)
696 stat->attributes |= STATX_ATTR_NODUMP;
697
698 stat->attributes_mask |= (STATX_ATTR_APPEND |
699 STATX_ATTR_COMPRESSED |
700 STATX_ATTR_ENCRYPTED |
701 STATX_ATTR_IMMUTABLE |
702 STATX_ATTR_NODUMP);
703
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900704 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700705
706 /* we need to show initial sectors used for inline_data/dentries */
707 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
708 f2fs_has_inline_dentry(inode))
709 stat->blocks += (stat->size + 511) >> 9;
710
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900711 return 0;
712}
713
714#ifdef CONFIG_F2FS_FS_POSIX_ACL
715static void __setattr_copy(struct inode *inode, const struct iattr *attr)
716{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900717 unsigned int ia_valid = attr->ia_valid;
718
719 if (ia_valid & ATTR_UID)
720 inode->i_uid = attr->ia_uid;
721 if (ia_valid & ATTR_GID)
722 inode->i_gid = attr->ia_gid;
723 if (ia_valid & ATTR_ATIME)
724 inode->i_atime = timespec_trunc(attr->ia_atime,
725 inode->i_sb->s_time_gran);
726 if (ia_valid & ATTR_MTIME)
727 inode->i_mtime = timespec_trunc(attr->ia_mtime,
728 inode->i_sb->s_time_gran);
729 if (ia_valid & ATTR_CTIME)
730 inode->i_ctime = timespec_trunc(attr->ia_ctime,
731 inode->i_sb->s_time_gran);
732 if (ia_valid & ATTR_MODE) {
733 umode_t mode = attr->ia_mode;
734
735 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
736 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700737 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900738 }
739}
740#else
741#define __setattr_copy setattr_copy
742#endif
743
744int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
745{
David Howells2b0143b2015-03-17 22:25:59 +0000746 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900747 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800748 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900749
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200750 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
751 return -EIO;
752
Jan Kara31051c82016-05-26 16:55:18 +0200753 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 if (err)
755 return err;
756
Chao Yu0abd6752017-07-09 00:13:07 +0800757 if (is_quota_modification(inode, attr)) {
758 err = dquot_initialize(inode);
759 if (err)
760 return err;
761 }
762 if ((attr->ia_valid & ATTR_UID &&
763 !uid_eq(attr->ia_uid, inode->i_uid)) ||
764 (attr->ia_valid & ATTR_GID &&
765 !gid_eq(attr->ia_gid, inode->i_gid))) {
766 err = dquot_transfer(inode, attr);
767 if (err)
768 return err;
769 }
770
Chao Yu09db6a22014-09-15 18:02:09 +0800771 if (attr->ia_valid & ATTR_SIZE) {
Eric Biggers67773a12017-06-13 16:47:54 -0700772 if (f2fs_encrypted_inode(inode)) {
773 err = fscrypt_get_encryption_info(inode);
774 if (err)
775 return err;
776 if (!fscrypt_has_encryption_key(inode))
777 return -ENOKEY;
778 }
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700779
Chao Yu3c454142015-06-05 18:34:02 +0800780 if (attr->ia_size <= i_size_read(inode)) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800781 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu09db6a22014-09-15 18:02:09 +0800782 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700783 err = f2fs_truncate(inode);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800784 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub0154892015-08-24 17:39:42 +0800785 if (err)
786 return err;
Chao Yu09db6a22014-09-15 18:02:09 +0800787 } else {
788 /*
Chao Yu3c454142015-06-05 18:34:02 +0800789 * do not trim all blocks after i_size if target size is
790 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800791 */
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800792 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3c454142015-06-05 18:34:02 +0800793 truncate_setsize(inode, attr->ia_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800794 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu0cab80e2015-12-01 11:36:16 +0800795
796 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800797 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800798 err = f2fs_convert_inline_inode(inode);
799 if (err)
800 return err;
801 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700802 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800803 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800804
Chao Yua0d00fa2017-10-09 17:55:19 +0800805 down_write(&F2FS_I(inode)->i_sem);
806 F2FS_I(inode)->last_disk_size = i_size_read(inode);
807 up_write(&F2FS_I(inode)->i_sem);
808
Yunlei Hec0ed4402016-12-11 15:35:15 +0800809 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900810 }
811
812 __setattr_copy(inode, attr);
813
814 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800815 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700816 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
817 inode->i_mode = F2FS_I(inode)->i_acl_mode;
818 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900819 }
820 }
821
Yunlei Hec0ed4402016-12-11 15:35:15 +0800822 /* file size may changed here */
823 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700824
825 /* inode change will produce dirty node pages flushed by checkpoint */
826 f2fs_balance_fs(F2FS_I_SB(inode), true);
827
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900828 return err;
829}
830
831const struct inode_operations f2fs_file_inode_operations = {
832 .getattr = f2fs_getattr,
833 .setattr = f2fs_setattr,
834 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800835 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900836#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900837 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900838#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900839 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900840};
841
Chao Yu63943282015-08-07 18:36:06 +0800842static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900843 loff_t start, loff_t len)
844{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700845 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900846 struct page *page;
847
848 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800849 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900850
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800851 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900852
Gu Zhenge4795562013-09-27 18:08:30 +0800853 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900854 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800855 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900856
Chao Yu63943282015-08-07 18:36:06 +0800857 if (IS_ERR(page))
858 return PTR_ERR(page);
859
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800860 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800861 zero_user(page, start, len);
862 set_page_dirty(page);
863 f2fs_put_page(page, 1);
864 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900865}
866
867int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
868{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900869 int err;
870
Chao Yuea587112015-09-17 20:22:44 +0800871 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900872 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800873 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900874
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900875 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yuea587112015-09-17 20:22:44 +0800876 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900877 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800878 if (err == -ENOENT) {
Weichao Guo48ab25f2017-10-14 08:13:32 +0800879 pg_start = get_next_page_offset(&dn, pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900880 continue;
Chao Yuea587112015-09-17 20:22:44 +0800881 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900882 return err;
883 }
884
Chao Yu81ca7352016-01-26 15:39:35 +0800885 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800886 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
887
888 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
889
890 truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900891 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800892
893 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900894 }
895 return 0;
896}
897
Chao Yua66c7b22013-11-22 16:52:50 +0800898static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900899{
900 pgoff_t pg_start, pg_end;
901 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800902 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900903
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800904 ret = f2fs_convert_inline_inode(inode);
905 if (ret)
906 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800907
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300908 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
909 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900910
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300911 off_start = offset & (PAGE_SIZE - 1);
912 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900913
914 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800915 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900916 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800917 if (ret)
918 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900919 } else {
Chao Yu63943282015-08-07 18:36:06 +0800920 if (off_start) {
921 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300922 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800923 if (ret)
924 return ret;
925 }
926 if (off_end) {
927 ret = fill_zero(inode, pg_end, 0, off_end);
928 if (ret)
929 return ret;
930 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900931
932 if (pg_start < pg_end) {
933 struct address_space *mapping = inode->i_mapping;
934 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700935 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500936
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800937 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900938
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300939 blk_start = (loff_t)pg_start << PAGE_SHIFT;
940 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800941 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900942 truncate_inode_pages_range(mapping, blk_start,
943 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900944
Gu Zhenge4795562013-09-27 18:08:30 +0800945 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900946 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800947 f2fs_unlock_op(sbi);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800948 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900949 }
950 }
951
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900952 return ret;
953}
954
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700955static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
956 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800957{
958 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
959 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700960 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700961
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700962next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700963 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700964 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700965 if (ret && ret != -ENOENT) {
966 return ret;
967 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700968 if (dn.max_level == 0)
969 return -ENOENT;
970 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
971 blkaddr += done;
972 do_replace += done;
973 goto next;
974 }
975
976 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
977 dn.ofs_in_node, len);
978 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800979 *blkaddr = datablock_addr(dn.inode,
980 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700981 if (!is_checkpointed_data(sbi, *blkaddr)) {
982
983 if (test_opt(sbi, LFS)) {
984 f2fs_put_dnode(&dn);
985 return -ENOTSUPP;
986 }
987
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700988 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +0800989 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700990 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700991 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700992 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700993 f2fs_put_dnode(&dn);
994next:
995 len -= done;
996 off += done;
997 if (len)
998 goto next_dnode;
999 return 0;
1000}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001001
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001002static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1003 int *do_replace, pgoff_t off, int len)
1004{
1005 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1006 struct dnode_of_data dn;
1007 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001008
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001009 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1010 if (*do_replace == 0)
1011 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001012
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001013 set_new_dnode(&dn, inode, NULL, NULL, 0);
1014 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1015 if (ret) {
1016 dec_valid_block_count(sbi, inode, 1);
1017 invalidate_blocks(sbi, *blkaddr);
1018 } else {
1019 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001020 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001021 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001022 }
1023 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001024}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001025
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001026static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1027 block_t *blkaddr, int *do_replace,
1028 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1029{
1030 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1031 pgoff_t i = 0;
1032 int ret;
1033
1034 while (i < len) {
1035 if (blkaddr[i] == NULL_ADDR && !full) {
1036 i++;
1037 continue;
1038 }
1039
1040 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1041 struct dnode_of_data dn;
1042 struct node_info ni;
1043 size_t new_size;
1044 pgoff_t ilen;
1045
1046 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1047 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1048 if (ret)
1049 return ret;
1050
1051 get_node_info(sbi, dn.nid, &ni);
1052 ilen = min((pgoff_t)
1053 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1054 dn.ofs_in_node, len - i);
1055 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001056 dn.data_blkaddr = datablock_addr(dn.inode,
1057 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001058 truncate_data_blocks_range(&dn, 1);
1059
1060 if (do_replace[i]) {
1061 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001062 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001063 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001064 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001065 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1066 blkaddr[i], ni.version, true, false);
1067
1068 do_replace[i] = 0;
1069 }
1070 dn.ofs_in_node++;
1071 i++;
1072 new_size = (dst + i) << PAGE_SHIFT;
1073 if (dst_inode->i_size < new_size)
1074 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001075 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001076
1077 f2fs_put_dnode(&dn);
1078 } else {
1079 struct page *psrc, *pdst;
1080
1081 psrc = get_lock_data_page(src_inode, src + i, true);
1082 if (IS_ERR(psrc))
1083 return PTR_ERR(psrc);
1084 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1085 true);
1086 if (IS_ERR(pdst)) {
1087 f2fs_put_page(psrc, 1);
1088 return PTR_ERR(pdst);
1089 }
1090 f2fs_copy_page(psrc, pdst);
1091 set_page_dirty(pdst);
1092 f2fs_put_page(pdst, 1);
1093 f2fs_put_page(psrc, 1);
1094
1095 ret = truncate_hole(src_inode, src + i, src + i + 1);
1096 if (ret)
1097 return ret;
1098 i++;
1099 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001100 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001101 return 0;
1102}
1103
1104static int __exchange_data_block(struct inode *src_inode,
1105 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001106 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001107{
1108 block_t *src_blkaddr;
1109 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001110 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001111 int ret;
1112
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001113 while (len) {
1114 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001115
Michal Hockoa7c3e902017-05-08 15:57:09 -07001116 src_blkaddr = kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001117 if (!src_blkaddr)
1118 return -ENOMEM;
1119
Michal Hockoa7c3e902017-05-08 15:57:09 -07001120 do_replace = kvzalloc(sizeof(int) * olen, GFP_KERNEL);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001121 if (!do_replace) {
1122 kvfree(src_blkaddr);
1123 return -ENOMEM;
1124 }
1125
1126 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1127 do_replace, src, olen);
1128 if (ret)
1129 goto roll_back;
1130
1131 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1132 do_replace, src, dst, olen, full);
1133 if (ret)
1134 goto roll_back;
1135
1136 src += olen;
1137 dst += olen;
1138 len -= olen;
1139
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001140 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001141 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001142 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001143 return 0;
1144
1145roll_back:
1146 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1147 kvfree(src_blkaddr);
1148 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001149 return ret;
1150}
1151
1152static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1153{
1154 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001155 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001156 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001157
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001158 f2fs_balance_fs(sbi, true);
1159 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001160
1161 f2fs_drop_extent_tree(inode);
1162
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001163 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1164 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +08001165 return ret;
1166}
1167
1168static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1169{
1170 pgoff_t pg_start, pg_end;
1171 loff_t new_size;
1172 int ret;
1173
Chao Yub4ace332015-05-06 13:09:46 +08001174 if (offset + len >= i_size_read(inode))
1175 return -EINVAL;
1176
1177 /* collapse range should be aligned to block size of f2fs. */
1178 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1179 return -EINVAL;
1180
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001181 ret = f2fs_convert_inline_inode(inode);
1182 if (ret)
1183 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001184
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001185 pg_start = offset >> PAGE_SHIFT;
1186 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yub4ace332015-05-06 13:09:46 +08001187
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001188 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001189 /* write out all dirty pages from offset */
1190 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1191 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001192 goto out;
Chao Yub4ace332015-05-06 13:09:46 +08001193
Chao Yubb066642017-11-03 10:21:05 +08001194 /* avoid gc operation during block exchange */
1195 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1196
Chao Yub4ace332015-05-06 13:09:46 +08001197 truncate_pagecache(inode, offset);
1198
1199 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1200 if (ret)
Chao Yubb066642017-11-03 10:21:05 +08001201 goto out_unlock;
Chao Yub4ace332015-05-06 13:09:46 +08001202
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001203 /* write out all moved pages, if possible */
1204 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1205 truncate_pagecache(inode, offset);
1206
Chao Yub4ace332015-05-06 13:09:46 +08001207 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001208 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001209
1210 ret = truncate_blocks(inode, new_size, true);
1211 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001212 f2fs_i_size_write(inode, new_size);
Chao Yubb066642017-11-03 10:21:05 +08001213out_unlock:
1214 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001215out:
1216 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001217 return ret;
1218}
1219
Chao Yu6e961942016-05-09 19:56:31 +08001220static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1221 pgoff_t end)
1222{
1223 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1224 pgoff_t index = start;
1225 unsigned int ofs_in_node = dn->ofs_in_node;
1226 blkcnt_t count = 0;
1227 int ret;
1228
1229 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001230 if (datablock_addr(dn->inode, dn->node_page,
1231 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001232 count++;
1233 }
1234
1235 dn->ofs_in_node = ofs_in_node;
1236 ret = reserve_new_blocks(dn, count);
1237 if (ret)
1238 return ret;
1239
1240 dn->ofs_in_node = ofs_in_node;
1241 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001242 dn->data_blkaddr = datablock_addr(dn->inode,
1243 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001244 /*
1245 * reserve_new_blocks will not guarantee entire block
1246 * allocation.
1247 */
1248 if (dn->data_blkaddr == NULL_ADDR) {
1249 ret = -ENOSPC;
1250 break;
1251 }
1252 if (dn->data_blkaddr != NEW_ADDR) {
1253 invalidate_blocks(sbi, dn->data_blkaddr);
1254 dn->data_blkaddr = NEW_ADDR;
1255 set_data_blkaddr(dn);
1256 }
1257 }
1258
1259 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1260
1261 return ret;
1262}
1263
Chao Yu75cd4e02015-05-06 13:11:13 +08001264static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1265 int mode)
1266{
1267 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1268 struct address_space *mapping = inode->i_mapping;
1269 pgoff_t index, pg_start, pg_end;
1270 loff_t new_size = i_size_read(inode);
1271 loff_t off_start, off_end;
1272 int ret = 0;
1273
Chao Yu75cd4e02015-05-06 13:11:13 +08001274 ret = inode_newsize_ok(inode, (len + offset));
1275 if (ret)
1276 return ret;
1277
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001278 ret = f2fs_convert_inline_inode(inode);
1279 if (ret)
1280 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001281
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001282 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001283 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1284 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001285 goto out_sem;
Chao Yu75cd4e02015-05-06 13:11:13 +08001286
1287 truncate_pagecache_range(inode, offset, offset + len - 1);
1288
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001289 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1290 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001291
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001292 off_start = offset & (PAGE_SIZE - 1);
1293 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001294
1295 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001296 ret = fill_zero(inode, pg_start, off_start,
1297 off_end - off_start);
1298 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001299 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001300
Chao Yu75cd4e02015-05-06 13:11:13 +08001301 new_size = max_t(loff_t, new_size, offset + len);
1302 } else {
1303 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001304 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001305 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001306 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001307 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001308
Chao Yu75cd4e02015-05-06 13:11:13 +08001309 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001310 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001311 }
1312
Chao Yu6e961942016-05-09 19:56:31 +08001313 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001314 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001315 unsigned int end_offset;
1316 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001317
1318 f2fs_lock_op(sbi);
1319
Chao Yu6e961942016-05-09 19:56:31 +08001320 set_new_dnode(&dn, inode, NULL, NULL, 0);
1321 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001322 if (ret) {
1323 f2fs_unlock_op(sbi);
1324 goto out;
1325 }
1326
Chao Yu6e961942016-05-09 19:56:31 +08001327 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1328 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1329
1330 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001331 f2fs_put_dnode(&dn);
1332 f2fs_unlock_op(sbi);
Chao Yu9434fcd2016-10-11 22:57:02 +08001333
1334 f2fs_balance_fs(sbi, dn.node_changed);
1335
Chao Yu6e961942016-05-09 19:56:31 +08001336 if (ret)
1337 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001338
Chao Yu6e961942016-05-09 19:56:31 +08001339 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001340 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001341 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001342 }
1343
1344 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001345 ret = fill_zero(inode, pg_end, 0, off_end);
1346 if (ret)
1347 goto out;
1348
Chao Yu75cd4e02015-05-06 13:11:13 +08001349 new_size = max_t(loff_t, new_size, offset + len);
1350 }
1351 }
1352
1353out:
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001354 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001355 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001356out_sem:
1357 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001358
1359 return ret;
1360}
1361
Chao Yuf62185d2015-05-28 19:16:57 +08001362static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1363{
1364 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001365 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001366 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001367 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001368
Chao Yuf62185d2015-05-28 19:16:57 +08001369 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001370 ret = inode_newsize_ok(inode, new_size);
1371 if (ret)
1372 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001373
1374 if (offset >= i_size_read(inode))
1375 return -EINVAL;
1376
1377 /* insert range should be aligned to block size of f2fs. */
1378 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1379 return -EINVAL;
1380
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001381 ret = f2fs_convert_inline_inode(inode);
1382 if (ret)
1383 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001384
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001385 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001386
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001387 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001388 ret = truncate_blocks(inode, i_size_read(inode), true);
1389 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001390 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001391
1392 /* write out all dirty pages from offset */
1393 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1394 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001395 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001396
Chao Yubb066642017-11-03 10:21:05 +08001397 /* avoid gc operation during block exchange */
1398 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1399
Chao Yuf62185d2015-05-28 19:16:57 +08001400 truncate_pagecache(inode, offset);
1401
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001402 pg_start = offset >> PAGE_SHIFT;
1403 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001404 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001405 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001406
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001407 while (!ret && idx > pg_start) {
1408 nr = idx - pg_start;
1409 if (nr > delta)
1410 nr = delta;
1411 idx -= nr;
1412
Chao Yuf62185d2015-05-28 19:16:57 +08001413 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001414 f2fs_drop_extent_tree(inode);
1415
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001416 ret = __exchange_data_block(inode, inode, idx,
1417 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001418 f2fs_unlock_op(sbi);
1419 }
1420
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001421 /* write out all moved pages, if possible */
1422 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1423 truncate_pagecache(inode, offset);
1424
1425 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001426 f2fs_i_size_write(inode, new_size);
Chao Yubb066642017-11-03 10:21:05 +08001427
1428 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001429out:
1430 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001431 return ret;
1432}
1433
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001434static int expand_inode_data(struct inode *inode, loff_t offset,
1435 loff_t len, int mode)
1436{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001437 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001438 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1439 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001440 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001441 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001442 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001443
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001444 err = inode_newsize_ok(inode, (len + offset));
1445 if (err)
1446 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001447
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001448 err = f2fs_convert_inline_inode(inode);
1449 if (err)
1450 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001451
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001452 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001453
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001454 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001455 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001456
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001457 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1458 map.m_len = pg_end - map.m_lblk;
1459 if (off_end)
1460 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001461
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001462 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1463 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001464 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001465
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001466 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001467 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001468
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001469 last_off = map.m_lblk + map.m_len - 1;
1470
1471 /* update new size to the failed position */
1472 new_size = (last_off == pg_end) ? offset + len:
1473 (loff_t)(last_off + 1) << PAGE_SHIFT;
1474 } else {
1475 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001476 }
1477
Chao Yue8ed90a2017-11-05 21:53:30 +08001478 if (new_size > i_size_read(inode)) {
1479 if (mode & FALLOC_FL_KEEP_SIZE)
1480 file_set_keep_isize(inode);
1481 else
1482 f2fs_i_size_write(inode, new_size);
1483 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001484
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001485 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001486}
1487
1488static long f2fs_fallocate(struct file *file, int mode,
1489 loff_t offset, loff_t len)
1490{
Al Viro6131ffa2013-02-27 16:59:05 -05001491 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001492 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001493
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001494 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1495 return -EIO;
1496
Chao Yuc9980122015-09-11 14:39:02 +08001497 /* f2fs only support ->fallocate for regular file */
1498 if (!S_ISREG(inode->i_mode))
1499 return -EINVAL;
1500
Chao Yuf62185d2015-05-28 19:16:57 +08001501 if (f2fs_encrypted_inode(inode) &&
1502 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001503 return -EOPNOTSUPP;
1504
Chao Yub4ace332015-05-06 13:09:46 +08001505 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001506 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1507 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001508 return -EOPNOTSUPP;
1509
Al Viro59551022016-01-22 15:40:57 -05001510 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001511
Taehee Yoo587c0a42015-04-21 15:59:12 +09001512 if (mode & FALLOC_FL_PUNCH_HOLE) {
1513 if (offset >= inode->i_size)
1514 goto out;
1515
Chao Yua66c7b22013-11-22 16:52:50 +08001516 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001517 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1518 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001519 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1520 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001521 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1522 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001523 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001524 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001525 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001526
Namjae Jeon3af60a42012-12-30 14:52:37 +09001527 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001528 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001529 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001530 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001531 }
Chao Yu3375f692014-01-28 10:29:26 +08001532
Taehee Yoo587c0a42015-04-21 15:59:12 +09001533out:
Al Viro59551022016-01-22 15:40:57 -05001534 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001535
Namjae Jeonc01e2852013-04-23 17:00:52 +09001536 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001537 return ret;
1538}
1539
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001540static int f2fs_release_file(struct inode *inode, struct file *filp)
1541{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001542 /*
1543 * f2fs_relase_file is called at every close calls. So we should
1544 * not drop any inmemory pages by close called by other process.
1545 */
1546 if (!(filp->f_mode & FMODE_WRITE) ||
1547 atomic_read(&inode->i_writecount) != 1)
1548 return 0;
1549
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001550 /* some remained atomic pages should discarded */
1551 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001552 drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001553 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001554 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001555 stat_dec_volatile_write(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001556 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001557 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001558 clear_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001559 }
1560 return 0;
1561}
1562
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001563static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001564{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001565 struct inode *inode = file_inode(file);
1566
1567 /*
1568 * If the process doing a transaction is crashed, we should do
1569 * roll-back. Otherwise, other reader/write can see corrupted database
1570 * until all the writers close its file. Since this should be done
1571 * before dropping file lock, it needs to do in ->flush.
1572 */
1573 if (f2fs_is_atomic_file(inode) &&
1574 F2FS_I(inode)->inmem_task == current)
1575 drop_inmem_pages(inode);
1576 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001577}
1578
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001579static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001580{
Al Viro6131ffa2013-02-27 16:59:05 -05001581 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001582 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08001583 unsigned int flags = fi->i_flags &
1584 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001585 return put_user(flags, (int __user *)arg);
1586}
1587
Chao Yu2c1d0302017-07-29 00:32:52 +08001588static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1589{
1590 struct f2fs_inode_info *fi = F2FS_I(inode);
1591 unsigned int oldflags;
1592
1593 /* Is it quota file? Do not allow user to mess with it */
1594 if (IS_NOQUOTA(inode))
1595 return -EPERM;
1596
1597 flags = f2fs_mask_flags(inode->i_mode, flags);
1598
1599 oldflags = fi->i_flags;
1600
1601 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL))
1602 if (!capable(CAP_LINUX_IMMUTABLE))
1603 return -EPERM;
1604
1605 flags = flags & (FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1606 flags |= oldflags & ~(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1607 fi->i_flags = flags;
1608
1609 if (fi->i_flags & FS_PROJINHERIT_FL)
1610 set_inode_flag(inode, FI_PROJ_INHERIT);
1611 else
1612 clear_inode_flag(inode, FI_PROJ_INHERIT);
1613
1614 inode->i_ctime = current_time(inode);
1615 f2fs_set_inode_flags(inode);
1616 f2fs_mark_inode_dirty_sync(inode, false);
1617 return 0;
1618}
1619
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001620static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1621{
1622 struct inode *inode = file_inode(filp);
Sheng Yong69494222016-08-23 20:10:47 +08001623 unsigned int flags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001624 int ret;
1625
Chao Yu7fb17fe2016-05-09 19:56:32 +08001626 if (!inode_owner_or_capable(inode))
1627 return -EACCES;
1628
1629 if (get_user(flags, (int __user *)arg))
1630 return -EFAULT;
1631
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001632 ret = mnt_want_write_file(filp);
1633 if (ret)
1634 return ret;
1635
Al Viro59551022016-01-22 15:40:57 -05001636 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001637
Chao Yu2c1d0302017-07-29 00:32:52 +08001638 ret = __f2fs_ioc_setflags(inode, flags);
Chao Yu0abd6752017-07-09 00:13:07 +08001639
Chao Yua72d4b92017-05-03 23:59:13 +08001640 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001641 mnt_drop_write_file(filp);
1642 return ret;
1643}
1644
Chao Yud49f3e82015-01-23 20:36:04 +08001645static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1646{
1647 struct inode *inode = file_inode(filp);
1648
1649 return put_user(inode->i_generation, (int __user *)arg);
1650}
1651
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001652static int f2fs_ioc_start_atomic_write(struct file *filp)
1653{
1654 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001655 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001656
1657 if (!inode_owner_or_capable(inode))
1658 return -EACCES;
1659
Jaegeuk Kime8118982017-03-17 10:04:15 +08001660 if (!S_ISREG(inode->i_mode))
1661 return -EINVAL;
1662
Chao Yu7fb17fe2016-05-09 19:56:32 +08001663 ret = mnt_want_write_file(filp);
1664 if (ret)
1665 return ret;
1666
Chao Yu0fac5582016-05-09 19:56:33 +08001667 inode_lock(inode);
1668
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001669 if (f2fs_is_atomic_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001670 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001671
Chao Yuf4c9c742015-07-17 18:06:35 +08001672 ret = f2fs_convert_inline_inode(inode);
1673 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001674 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001675
Jaegeuk Kim91942322016-05-20 10:13:22 -07001676 set_inode_flag(inode, FI_ATOMIC_FILE);
Jaegeuk Kim6c3acd92017-04-12 10:01:33 -07001677 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001678 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1679
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001680 if (!get_dirty_pages(inode))
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001681 goto inc_stat;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001682
1683 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001684 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001685 inode->i_ino, get_dirty_pages(inode));
1686 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001687 if (ret) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001688 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu84a23fb2017-08-18 23:37:36 +08001689 clear_inode_flag(inode, FI_HOT_DATA);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001690 goto out;
1691 }
1692
1693inc_stat:
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001694 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001695 stat_inc_atomic_write(inode);
1696 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001697out:
Chao Yu0fac5582016-05-09 19:56:33 +08001698 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001699 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001700 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001701}
1702
1703static int f2fs_ioc_commit_atomic_write(struct file *filp)
1704{
1705 struct inode *inode = file_inode(filp);
1706 int ret;
1707
1708 if (!inode_owner_or_capable(inode))
1709 return -EACCES;
1710
1711 ret = mnt_want_write_file(filp);
1712 if (ret)
1713 return ret;
1714
Chao Yu0fac5582016-05-09 19:56:33 +08001715 inode_lock(inode);
1716
Chao Yu7fb17fe2016-05-09 19:56:32 +08001717 if (f2fs_is_volatile_file(inode))
1718 goto err_out;
1719
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001720 if (f2fs_is_atomic_file(inode)) {
Chao Yu29b96b52016-02-06 14:38:29 +08001721 ret = commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001722 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001723 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001724
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001725 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001726 if (!ret) {
1727 clear_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu84a23fb2017-08-18 23:37:36 +08001728 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu5fe45742017-01-07 18:50:26 +08001729 stat_dec_atomic_write(inode);
1730 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001731 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08001732 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001733 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001734err_out:
Chao Yu0fac5582016-05-09 19:56:33 +08001735 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001736 mnt_drop_write_file(filp);
1737 return ret;
1738}
1739
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001740static int f2fs_ioc_start_volatile_write(struct file *filp)
1741{
1742 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001743 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001744
1745 if (!inode_owner_or_capable(inode))
1746 return -EACCES;
1747
Chao Yu8ff09712017-03-17 15:43:57 +08001748 if (!S_ISREG(inode->i_mode))
1749 return -EINVAL;
1750
Chao Yu7fb17fe2016-05-09 19:56:32 +08001751 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001752 if (ret)
1753 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001754
Chao Yu0fac5582016-05-09 19:56:33 +08001755 inode_lock(inode);
1756
Chao Yu7fb17fe2016-05-09 19:56:32 +08001757 if (f2fs_is_volatile_file(inode))
1758 goto out;
1759
1760 ret = f2fs_convert_inline_inode(inode);
1761 if (ret)
1762 goto out;
1763
Chao Yu648d50b2017-03-22 17:23:45 +08001764 stat_inc_volatile_write(inode);
1765 stat_update_max_volatile_write(inode);
1766
Jaegeuk Kim91942322016-05-20 10:13:22 -07001767 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001768 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001769out:
Chao Yu0fac5582016-05-09 19:56:33 +08001770 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001771 mnt_drop_write_file(filp);
1772 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001773}
1774
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001775static int f2fs_ioc_release_volatile_write(struct file *filp)
1776{
1777 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001778 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001779
1780 if (!inode_owner_or_capable(inode))
1781 return -EACCES;
1782
Chao Yu7fb17fe2016-05-09 19:56:32 +08001783 ret = mnt_want_write_file(filp);
1784 if (ret)
1785 return ret;
1786
Chao Yu0fac5582016-05-09 19:56:33 +08001787 inode_lock(inode);
1788
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001789 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001790 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001791
Chao Yu7fb17fe2016-05-09 19:56:32 +08001792 if (!f2fs_is_first_block_written(inode)) {
1793 ret = truncate_partial_data_page(inode, 0, true);
1794 goto out;
1795 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001796
Chao Yu7fb17fe2016-05-09 19:56:32 +08001797 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1798out:
Chao Yu0fac5582016-05-09 19:56:33 +08001799 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001800 mnt_drop_write_file(filp);
1801 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001802}
1803
1804static int f2fs_ioc_abort_volatile_write(struct file *filp)
1805{
1806 struct inode *inode = file_inode(filp);
1807 int ret;
1808
1809 if (!inode_owner_or_capable(inode))
1810 return -EACCES;
1811
1812 ret = mnt_want_write_file(filp);
1813 if (ret)
1814 return ret;
1815
Chao Yu0fac5582016-05-09 19:56:33 +08001816 inode_lock(inode);
1817
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001818 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001819 drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001820 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001821 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001822 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001823 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001824 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001825
Chao Yu0fac5582016-05-09 19:56:33 +08001826 inode_unlock(inode);
1827
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001828 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001829 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001830 return ret;
1831}
1832
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001833static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1834{
1835 struct inode *inode = file_inode(filp);
1836 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1837 struct super_block *sb = sbi->sb;
1838 __u32 in;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001839 int ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001840
1841 if (!capable(CAP_SYS_ADMIN))
1842 return -EPERM;
1843
1844 if (get_user(in, (__u32 __user *)arg))
1845 return -EFAULT;
1846
Chao Yu7fb17fe2016-05-09 19:56:32 +08001847 ret = mnt_want_write_file(filp);
1848 if (ret)
1849 return ret;
1850
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001851 switch (in) {
1852 case F2FS_GOING_DOWN_FULLSYNC:
1853 sb = freeze_bdev(sb->s_bdev);
1854 if (sb && !IS_ERR(sb)) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001855 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001856 thaw_bdev(sb->s_bdev, sb);
1857 }
1858 break;
1859 case F2FS_GOING_DOWN_METASYNC:
1860 /* do checkpoint only */
1861 f2fs_sync_fs(sb, 1);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001862 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001863 break;
1864 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001865 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001866 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001867 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yub0af6d42017-08-02 23:21:48 +08001868 sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001869 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001870 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001871 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001872 ret = -EINVAL;
1873 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001874 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001875 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001876out:
1877 mnt_drop_write_file(filp);
1878 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001879}
1880
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001881static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1882{
1883 struct inode *inode = file_inode(filp);
1884 struct super_block *sb = inode->i_sb;
1885 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1886 struct fstrim_range range;
1887 int ret;
1888
1889 if (!capable(CAP_SYS_ADMIN))
1890 return -EPERM;
1891
1892 if (!blk_queue_discard(q))
1893 return -EOPNOTSUPP;
1894
1895 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1896 sizeof(range)))
1897 return -EFAULT;
1898
Chao Yu7fb17fe2016-05-09 19:56:32 +08001899 ret = mnt_want_write_file(filp);
1900 if (ret)
1901 return ret;
1902
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001903 range.minlen = max((unsigned int)range.minlen,
1904 q->limits.discard_granularity);
1905 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001906 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001907 if (ret < 0)
1908 return ret;
1909
1910 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1911 sizeof(range)))
1912 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001913 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001914 return 0;
1915}
1916
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001917static bool uuid_is_nonzero(__u8 u[16])
1918{
1919 int i;
1920
1921 for (i = 0; i < 16; i++)
1922 if (u[i])
1923 return true;
1924 return false;
1925}
1926
1927static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1928{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001929 struct inode *inode = file_inode(filp);
1930
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001931 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001932
Eric Biggersdb717d82016-11-26 19:07:49 -05001933 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001934}
1935
1936static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1937{
Eric Biggersdb717d82016-11-26 19:07:49 -05001938 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001939}
1940
1941static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1942{
1943 struct inode *inode = file_inode(filp);
1944 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1945 int err;
1946
1947 if (!f2fs_sb_has_crypto(inode->i_sb))
1948 return -EOPNOTSUPP;
1949
1950 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1951 goto got_it;
1952
1953 err = mnt_want_write_file(filp);
1954 if (err)
1955 return err;
1956
1957 /* update superblock with uuid */
1958 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1959
Chao Yuc5bda1c2015-06-08 13:28:03 +08001960 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001961 if (err) {
1962 /* undo new data */
1963 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yue8240f62015-12-15 17:19:26 +08001964 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001965 return err;
1966 }
Chao Yue8240f62015-12-15 17:19:26 +08001967 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001968got_it:
1969 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1970 16))
1971 return -EFAULT;
1972 return 0;
1973}
1974
Chao Yuc1c1b582015-07-10 18:08:10 +08001975static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1976{
1977 struct inode *inode = file_inode(filp);
1978 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08001979 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001980 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08001981
1982 if (!capable(CAP_SYS_ADMIN))
1983 return -EPERM;
1984
Chao Yud530d4d2015-10-05 22:22:44 +08001985 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08001986 return -EFAULT;
1987
Chao Yud530d4d2015-10-05 22:22:44 +08001988 if (f2fs_readonly(sbi->sb))
1989 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08001990
Chao Yu7fb17fe2016-05-09 19:56:32 +08001991 ret = mnt_want_write_file(filp);
1992 if (ret)
1993 return ret;
1994
Chao Yud530d4d2015-10-05 22:22:44 +08001995 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08001996 if (!mutex_trylock(&sbi->gc_mutex)) {
1997 ret = -EBUSY;
1998 goto out;
1999 }
Chao Yud530d4d2015-10-05 22:22:44 +08002000 } else {
2001 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002002 }
2003
Jaegeuk Kime066b832017-04-13 15:17:00 -07002004 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002005out:
2006 mnt_drop_write_file(filp);
2007 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002008}
2009
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002010static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2011{
2012 struct inode *inode = file_inode(filp);
2013 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2014 struct f2fs_gc_range range;
2015 u64 end;
2016 int ret;
2017
2018 if (!capable(CAP_SYS_ADMIN))
2019 return -EPERM;
2020
2021 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2022 sizeof(range)))
2023 return -EFAULT;
2024
2025 if (f2fs_readonly(sbi->sb))
2026 return -EROFS;
2027
2028 ret = mnt_want_write_file(filp);
2029 if (ret)
2030 return ret;
2031
2032 end = range.start + range.len;
2033 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi))
2034 return -EINVAL;
2035do_more:
2036 if (!range.sync) {
2037 if (!mutex_trylock(&sbi->gc_mutex)) {
2038 ret = -EBUSY;
2039 goto out;
2040 }
2041 } else {
2042 mutex_lock(&sbi->gc_mutex);
2043 }
2044
2045 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2046 range.start += sbi->blocks_per_seg;
2047 if (range.start <= end)
2048 goto do_more;
2049out:
2050 mnt_drop_write_file(filp);
2051 return ret;
2052}
2053
Chao Yu456b88e2015-10-05 22:24:19 +08002054static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2055{
2056 struct inode *inode = file_inode(filp);
2057 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002058 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002059
2060 if (!capable(CAP_SYS_ADMIN))
2061 return -EPERM;
2062
2063 if (f2fs_readonly(sbi->sb))
2064 return -EROFS;
2065
Chao Yu7fb17fe2016-05-09 19:56:32 +08002066 ret = mnt_want_write_file(filp);
2067 if (ret)
2068 return ret;
2069
2070 ret = f2fs_sync_fs(sbi->sb, 1);
2071
2072 mnt_drop_write_file(filp);
2073 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002074}
2075
Chao Yud323d002015-10-27 09:53:45 +08002076static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2077 struct file *filp,
2078 struct f2fs_defragment *range)
2079{
2080 struct inode *inode = file_inode(filp);
Chao Yuda859852016-01-26 15:42:58 +08002081 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
Hou Pengyange15882b2017-02-23 09:18:05 +00002082 struct extent_info ei = {0,0,0};
Chao Yud323d002015-10-27 09:53:45 +08002083 pgoff_t pg_start, pg_end;
Chao Yu3519e3f2015-12-01 11:56:52 +08002084 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002085 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002086 block_t blk_end = 0;
2087 bool fragmented = false;
2088 int err;
2089
2090 /* if in-place-update policy is enabled, don't waste time here */
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002091 if (need_inplace_update_policy(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002092 return -EINVAL;
2093
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002094 pg_start = range->start >> PAGE_SHIFT;
2095 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002096
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002097 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002098
Al Viro59551022016-01-22 15:40:57 -05002099 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002100
2101 /* writeback all dirty pages in the range */
2102 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002103 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002104 if (err)
2105 goto out;
2106
2107 /*
2108 * lookup mapping info in extent cache, skip defragmenting if physical
2109 * block addresses are continuous.
2110 */
2111 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2112 if (ei.fofs + ei.len >= pg_end)
2113 goto out;
2114 }
2115
2116 map.m_lblk = pg_start;
Chao Yud323d002015-10-27 09:53:45 +08002117
2118 /*
2119 * lookup mapping info in dnode page cache, skip defragmenting if all
2120 * physical block addresses are continuous even if there are hole(s)
2121 * in logical blocks.
2122 */
2123 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002124 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002125 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002126 if (err)
2127 goto out;
2128
2129 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2130 map.m_lblk++;
Chao Yud323d002015-10-27 09:53:45 +08002131 continue;
2132 }
2133
2134 if (blk_end && blk_end != map.m_pblk) {
2135 fragmented = true;
2136 break;
2137 }
2138 blk_end = map.m_pblk + map.m_len;
2139
2140 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002141 }
2142
2143 if (!fragmented)
2144 goto out;
2145
2146 map.m_lblk = pg_start;
2147 map.m_len = pg_end - pg_start;
2148
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002149 sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002150
2151 /*
2152 * make sure there are enough free section for LFS allocation, this can
2153 * avoid defragment running in SSR mode when free section are allocated
2154 * intensively
2155 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002156 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002157 err = -EAGAIN;
2158 goto out;
2159 }
2160
2161 while (map.m_lblk < pg_end) {
2162 pgoff_t idx;
2163 int cnt = 0;
2164
2165do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002166 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002167 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002168 if (err)
2169 goto clear_out;
2170
2171 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2172 map.m_lblk++;
2173 continue;
2174 }
2175
Jaegeuk Kim91942322016-05-20 10:13:22 -07002176 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002177
2178 idx = map.m_lblk;
2179 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2180 struct page *page;
2181
2182 page = get_lock_data_page(inode, idx, true);
2183 if (IS_ERR(page)) {
2184 err = PTR_ERR(page);
2185 goto clear_out;
2186 }
2187
2188 set_page_dirty(page);
2189 f2fs_put_page(page, 1);
2190
2191 idx++;
2192 cnt++;
2193 total++;
2194 }
2195
2196 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002197
2198 if (idx < pg_end && cnt < blk_per_seg)
2199 goto do_map;
2200
Jaegeuk Kim91942322016-05-20 10:13:22 -07002201 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002202
2203 err = filemap_fdatawrite(inode->i_mapping);
2204 if (err)
2205 goto out;
2206 }
2207clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002208 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002209out:
Al Viro59551022016-01-22 15:40:57 -05002210 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002211 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002212 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002213 return err;
2214}
2215
2216static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2217{
2218 struct inode *inode = file_inode(filp);
2219 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2220 struct f2fs_defragment range;
2221 int err;
2222
2223 if (!capable(CAP_SYS_ADMIN))
2224 return -EPERM;
2225
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002226 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002227 return -EINVAL;
2228
Kinglong Meed7563862017-03-10 17:55:07 +08002229 if (f2fs_readonly(sbi->sb))
2230 return -EROFS;
2231
2232 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2233 sizeof(range)))
2234 return -EFAULT;
2235
2236 /* verify alignment of offset & size */
2237 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2238 return -EINVAL;
2239
2240 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2241 sbi->max_file_blocks))
2242 return -EINVAL;
2243
Chao Yud323d002015-10-27 09:53:45 +08002244 err = mnt_want_write_file(filp);
2245 if (err)
2246 return err;
2247
Chao Yud323d002015-10-27 09:53:45 +08002248 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002249 mnt_drop_write_file(filp);
2250
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002251 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002252 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002253 return err;
Chao Yud323d002015-10-27 09:53:45 +08002254
2255 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2256 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002257 return -EFAULT;
2258
2259 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002260}
2261
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002262static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2263 struct file *file_out, loff_t pos_out, size_t len)
2264{
2265 struct inode *src = file_inode(file_in);
2266 struct inode *dst = file_inode(file_out);
2267 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2268 size_t olen = len, dst_max_i_size = 0;
2269 size_t dst_osize;
2270 int ret;
2271
2272 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2273 src->i_sb != dst->i_sb)
2274 return -EXDEV;
2275
2276 if (unlikely(f2fs_readonly(src->i_sb)))
2277 return -EROFS;
2278
Chao Yufe8494b2016-08-04 20:13:02 +08002279 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2280 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002281
2282 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2283 return -EOPNOTSUPP;
2284
Fan Lid95fd912016-09-13 11:35:42 +08002285 if (src == dst) {
2286 if (pos_in == pos_out)
2287 return 0;
2288 if (pos_out > pos_in && pos_out < pos_in + len)
2289 return -EINVAL;
2290 }
2291
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002292 inode_lock(src);
Chao Yubb066642017-11-03 10:21:05 +08002293 down_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Chao Yu20a3d612016-08-04 20:13:03 +08002294 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002295 ret = -EBUSY;
2296 if (!inode_trylock(dst))
2297 goto out;
2298 if (!down_write_trylock(&F2FS_I(dst)->dio_rwsem[WRITE])) {
2299 inode_unlock(dst);
Chao Yu20a3d612016-08-04 20:13:03 +08002300 goto out;
2301 }
2302 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002303
2304 ret = -EINVAL;
2305 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2306 goto out_unlock;
2307 if (len == 0)
2308 olen = len = src->i_size - pos_in;
2309 if (pos_in + len == src->i_size)
2310 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2311 if (len == 0) {
2312 ret = 0;
2313 goto out_unlock;
2314 }
2315
2316 dst_osize = dst->i_size;
2317 if (pos_out + olen > dst->i_size)
2318 dst_max_i_size = pos_out + olen;
2319
2320 /* verify the end result is block aligned */
2321 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2322 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2323 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2324 goto out_unlock;
2325
2326 ret = f2fs_convert_inline_inode(src);
2327 if (ret)
2328 goto out_unlock;
2329
2330 ret = f2fs_convert_inline_inode(dst);
2331 if (ret)
2332 goto out_unlock;
2333
2334 /* write out all dirty pages from offset */
2335 ret = filemap_write_and_wait_range(src->i_mapping,
2336 pos_in, pos_in + len);
2337 if (ret)
2338 goto out_unlock;
2339
2340 ret = filemap_write_and_wait_range(dst->i_mapping,
2341 pos_out, pos_out + len);
2342 if (ret)
2343 goto out_unlock;
2344
2345 f2fs_balance_fs(sbi, true);
2346 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002347 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2348 pos_out >> F2FS_BLKSIZE_BITS,
2349 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002350
2351 if (!ret) {
2352 if (dst_max_i_size)
2353 f2fs_i_size_write(dst, dst_max_i_size);
2354 else if (dst_osize != dst->i_size)
2355 f2fs_i_size_write(dst, dst_osize);
2356 }
2357 f2fs_unlock_op(sbi);
2358out_unlock:
Chao Yubb066642017-11-03 10:21:05 +08002359 if (src != dst) {
2360 up_write(&F2FS_I(dst)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002361 inode_unlock(dst);
Chao Yubb066642017-11-03 10:21:05 +08002362 }
Chao Yu20a3d612016-08-04 20:13:03 +08002363out:
Chao Yubb066642017-11-03 10:21:05 +08002364 up_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002365 inode_unlock(src);
2366 return ret;
2367}
2368
2369static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2370{
2371 struct f2fs_move_range range;
2372 struct fd dst;
2373 int err;
2374
2375 if (!(filp->f_mode & FMODE_READ) ||
2376 !(filp->f_mode & FMODE_WRITE))
2377 return -EBADF;
2378
2379 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2380 sizeof(range)))
2381 return -EFAULT;
2382
2383 dst = fdget(range.dst_fd);
2384 if (!dst.file)
2385 return -EBADF;
2386
2387 if (!(dst.file->f_mode & FMODE_WRITE)) {
2388 err = -EBADF;
2389 goto err_out;
2390 }
2391
2392 err = mnt_want_write_file(filp);
2393 if (err)
2394 goto err_out;
2395
2396 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2397 range.pos_out, range.len);
2398
2399 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002400 if (err)
2401 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002402
2403 if (copy_to_user((struct f2fs_move_range __user *)arg,
2404 &range, sizeof(range)))
2405 err = -EFAULT;
2406err_out:
2407 fdput(dst);
2408 return err;
2409}
2410
Jaegeuk Kime066b832017-04-13 15:17:00 -07002411static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2412{
2413 struct inode *inode = file_inode(filp);
2414 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2415 struct sit_info *sm = SIT_I(sbi);
2416 unsigned int start_segno = 0, end_segno = 0;
2417 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2418 struct f2fs_flush_device range;
2419 int ret;
2420
2421 if (!capable(CAP_SYS_ADMIN))
2422 return -EPERM;
2423
2424 if (f2fs_readonly(sbi->sb))
2425 return -EROFS;
2426
2427 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2428 sizeof(range)))
2429 return -EFAULT;
2430
2431 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2432 sbi->segs_per_sec != 1) {
2433 f2fs_msg(sbi->sb, KERN_WARNING,
2434 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2435 range.dev_num, sbi->s_ndevs,
2436 sbi->segs_per_sec);
2437 return -EINVAL;
2438 }
2439
2440 ret = mnt_want_write_file(filp);
2441 if (ret)
2442 return ret;
2443
2444 if (range.dev_num != 0)
2445 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2446 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2447
2448 start_segno = sm->last_victim[FLUSH_DEVICE];
2449 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2450 start_segno = dev_start_segno;
2451 end_segno = min(start_segno + range.segments, dev_end_segno);
2452
2453 while (start_segno < end_segno) {
2454 if (!mutex_trylock(&sbi->gc_mutex)) {
2455 ret = -EBUSY;
2456 goto out;
2457 }
2458 sm->last_victim[GC_CB] = end_segno + 1;
2459 sm->last_victim[GC_GREEDY] = end_segno + 1;
2460 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2461 ret = f2fs_gc(sbi, true, true, start_segno);
2462 if (ret == -EAGAIN)
2463 ret = 0;
2464 else if (ret < 0)
2465 break;
2466 start_segno++;
2467 }
2468out:
2469 mnt_drop_write_file(filp);
2470 return ret;
2471}
2472
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002473static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2474{
2475 struct inode *inode = file_inode(filp);
2476 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2477
2478 /* Must validate to set it with SQLite behavior in Android. */
2479 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2480
2481 return put_user(sb_feature, (u32 __user *)arg);
2482}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002483
Chao Yu2c1d0302017-07-29 00:32:52 +08002484#ifdef CONFIG_QUOTA
2485static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2486{
2487 struct inode *inode = file_inode(filp);
2488 struct f2fs_inode_info *fi = F2FS_I(inode);
2489 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2490 struct super_block *sb = sbi->sb;
2491 struct dquot *transfer_to[MAXQUOTAS] = {};
2492 struct page *ipage;
2493 kprojid_t kprojid;
2494 int err;
2495
2496 if (!f2fs_sb_has_project_quota(sb)) {
2497 if (projid != F2FS_DEF_PROJID)
2498 return -EOPNOTSUPP;
2499 else
2500 return 0;
2501 }
2502
2503 if (!f2fs_has_extra_attr(inode))
2504 return -EOPNOTSUPP;
2505
2506 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2507
2508 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2509 return 0;
2510
2511 err = mnt_want_write_file(filp);
2512 if (err)
2513 return err;
2514
2515 err = -EPERM;
2516 inode_lock(inode);
2517
2518 /* Is it quota file? Do not allow user to mess with it */
2519 if (IS_NOQUOTA(inode))
2520 goto out_unlock;
2521
2522 ipage = get_node_page(sbi, inode->i_ino);
2523 if (IS_ERR(ipage)) {
2524 err = PTR_ERR(ipage);
2525 goto out_unlock;
2526 }
2527
2528 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2529 i_projid)) {
2530 err = -EOVERFLOW;
2531 f2fs_put_page(ipage, 1);
2532 goto out_unlock;
2533 }
2534 f2fs_put_page(ipage, 1);
2535
2536 dquot_initialize(inode);
2537
2538 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2539 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2540 err = __dquot_transfer(inode, transfer_to);
2541 dqput(transfer_to[PRJQUOTA]);
2542 if (err)
2543 goto out_dirty;
2544 }
2545
2546 F2FS_I(inode)->i_projid = kprojid;
2547 inode->i_ctime = current_time(inode);
2548out_dirty:
2549 f2fs_mark_inode_dirty_sync(inode, true);
2550out_unlock:
2551 inode_unlock(inode);
2552 mnt_drop_write_file(filp);
2553 return err;
2554}
2555#else
2556static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2557{
2558 if (projid != F2FS_DEF_PROJID)
2559 return -EOPNOTSUPP;
2560 return 0;
2561}
2562#endif
2563
2564/* Transfer internal flags to xflags */
2565static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2566{
2567 __u32 xflags = 0;
2568
2569 if (iflags & FS_SYNC_FL)
2570 xflags |= FS_XFLAG_SYNC;
2571 if (iflags & FS_IMMUTABLE_FL)
2572 xflags |= FS_XFLAG_IMMUTABLE;
2573 if (iflags & FS_APPEND_FL)
2574 xflags |= FS_XFLAG_APPEND;
2575 if (iflags & FS_NODUMP_FL)
2576 xflags |= FS_XFLAG_NODUMP;
2577 if (iflags & FS_NOATIME_FL)
2578 xflags |= FS_XFLAG_NOATIME;
2579 if (iflags & FS_PROJINHERIT_FL)
2580 xflags |= FS_XFLAG_PROJINHERIT;
2581 return xflags;
2582}
2583
2584#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2585 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2586 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2587
2588/* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
2589#define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \
2590 FS_IMMUTABLE_FL | \
2591 FS_APPEND_FL | \
2592 FS_NODUMP_FL | \
2593 FS_NOATIME_FL | \
2594 FS_PROJINHERIT_FL)
2595
2596/* Transfer xflags flags to internal */
2597static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2598{
2599 unsigned long iflags = 0;
2600
2601 if (xflags & FS_XFLAG_SYNC)
2602 iflags |= FS_SYNC_FL;
2603 if (xflags & FS_XFLAG_IMMUTABLE)
2604 iflags |= FS_IMMUTABLE_FL;
2605 if (xflags & FS_XFLAG_APPEND)
2606 iflags |= FS_APPEND_FL;
2607 if (xflags & FS_XFLAG_NODUMP)
2608 iflags |= FS_NODUMP_FL;
2609 if (xflags & FS_XFLAG_NOATIME)
2610 iflags |= FS_NOATIME_FL;
2611 if (xflags & FS_XFLAG_PROJINHERIT)
2612 iflags |= FS_PROJINHERIT_FL;
2613
2614 return iflags;
2615}
2616
2617static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2618{
2619 struct inode *inode = file_inode(filp);
2620 struct f2fs_inode_info *fi = F2FS_I(inode);
2621 struct fsxattr fa;
2622
2623 memset(&fa, 0, sizeof(struct fsxattr));
2624 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
2625 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL));
2626
2627 if (f2fs_sb_has_project_quota(inode->i_sb))
2628 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2629 fi->i_projid);
2630
2631 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2632 return -EFAULT;
2633 return 0;
2634}
2635
2636static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2637{
2638 struct inode *inode = file_inode(filp);
2639 struct f2fs_inode_info *fi = F2FS_I(inode);
2640 struct fsxattr fa;
2641 unsigned int flags;
2642 int err;
2643
2644 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2645 return -EFAULT;
2646
2647 /* Make sure caller has proper permission */
2648 if (!inode_owner_or_capable(inode))
2649 return -EACCES;
2650
2651 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2652 return -EOPNOTSUPP;
2653
2654 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2655 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2656 return -EOPNOTSUPP;
2657
2658 err = mnt_want_write_file(filp);
2659 if (err)
2660 return err;
2661
2662 inode_lock(inode);
2663 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2664 (flags & F2FS_FL_XFLAG_VISIBLE);
2665 err = __f2fs_ioc_setflags(inode, flags);
2666 inode_unlock(inode);
2667 mnt_drop_write_file(filp);
2668 if (err)
2669 return err;
2670
2671 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2672 if (err)
2673 return err;
2674
2675 return 0;
2676}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002677
2678long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2679{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002680 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2681 return -EIO;
2682
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002683 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002684 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002685 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002686 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002687 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002688 case F2FS_IOC_GETVERSION:
2689 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002690 case F2FS_IOC_START_ATOMIC_WRITE:
2691 return f2fs_ioc_start_atomic_write(filp);
2692 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2693 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002694 case F2FS_IOC_START_VOLATILE_WRITE:
2695 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002696 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2697 return f2fs_ioc_release_volatile_write(filp);
2698 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2699 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002700 case F2FS_IOC_SHUTDOWN:
2701 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002702 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002703 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002704 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2705 return f2fs_ioc_set_encryption_policy(filp, arg);
2706 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2707 return f2fs_ioc_get_encryption_policy(filp, arg);
2708 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2709 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002710 case F2FS_IOC_GARBAGE_COLLECT:
2711 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002712 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2713 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002714 case F2FS_IOC_WRITE_CHECKPOINT:
2715 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002716 case F2FS_IOC_DEFRAGMENT:
2717 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002718 case F2FS_IOC_MOVE_RANGE:
2719 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002720 case F2FS_IOC_FLUSH_DEVICE:
2721 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002722 case F2FS_IOC_GET_FEATURES:
2723 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08002724 case F2FS_IOC_FSGETXATTR:
2725 return f2fs_ioc_fsgetxattr(filp, arg);
2726 case F2FS_IOC_FSSETXATTR:
2727 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002728 default:
2729 return -ENOTTY;
2730 }
2731}
2732
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002733static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2734{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002735 struct file *file = iocb->ki_filp;
2736 struct inode *inode = file_inode(file);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002737 struct blk_plug plug;
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002738 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002739
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02002740 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2741 return -EIO;
2742
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002743 inode_lock(inode);
2744 ret = generic_write_checks(iocb, from);
2745 if (ret > 0) {
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002746 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002747
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002748 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2749 set_inode_flag(inode, FI_NO_PREALLOC);
2750
2751 err = f2fs_preallocate_blocks(iocb, from);
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002752 if (err) {
2753 inode_unlock(inode);
2754 return err;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002755 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002756 blk_start_plug(&plug);
2757 ret = __generic_file_write_iter(iocb, from);
2758 blk_finish_plug(&plug);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002759 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08002760
2761 if (ret > 0)
2762 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002763 }
2764 inode_unlock(inode);
2765
Christoph Hellwige2592212016-04-07 08:52:01 -07002766 if (ret > 0)
2767 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002768 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002769}
2770
Namjae Jeone9750822013-02-04 23:41:41 +09002771#ifdef CONFIG_COMPAT
2772long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2773{
2774 switch (cmd) {
2775 case F2FS_IOC32_GETFLAGS:
2776 cmd = F2FS_IOC_GETFLAGS;
2777 break;
2778 case F2FS_IOC32_SETFLAGS:
2779 cmd = F2FS_IOC_SETFLAGS;
2780 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08002781 case F2FS_IOC32_GETVERSION:
2782 cmd = F2FS_IOC_GETVERSION;
2783 break;
2784 case F2FS_IOC_START_ATOMIC_WRITE:
2785 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2786 case F2FS_IOC_START_VOLATILE_WRITE:
2787 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2788 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2789 case F2FS_IOC_SHUTDOWN:
2790 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2791 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2792 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2793 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002794 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08002795 case F2FS_IOC_WRITE_CHECKPOINT:
2796 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002797 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07002798 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002799 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08002800 case F2FS_IOC_FSGETXATTR:
2801 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002802 break;
Namjae Jeone9750822013-02-04 23:41:41 +09002803 default:
2804 return -ENOIOCTLCMD;
2805 }
2806 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2807}
2808#endif
2809
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002810const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08002811 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04002812 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002813 .write_iter = f2fs_file_write_iter,
2814 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08002815 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002816 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002817 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002818 .fsync = f2fs_sync_file,
2819 .fallocate = f2fs_fallocate,
2820 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09002821#ifdef CONFIG_COMPAT
2822 .compat_ioctl = f2fs_compat_ioctl,
2823#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002824 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002825 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002826};