blob: 6a201c61eef546a3ff7ab61a11e9a9ed91782ec5 [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 Kimfbfa2cc2012-11-02 17:09:44 +090056 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070057
58 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070059
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090060 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080061 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070062 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070063 err = f2fs_reserve_block(&dn, page->index);
64 if (err) {
65 f2fs_unlock_op(sbi);
66 goto out;
67 }
68 f2fs_put_dnode(&dn);
69 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090070
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -080071 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -080072
Dave Jiang11bac802017-02-24 14:56:41 -080073 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080074 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090075 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090076 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090077 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090078 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090079 unlock_page(page);
80 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080081 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090082 }
83
84 /*
85 * check to see if the page is mapped already (no holes)
86 */
87 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090088 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090089
90 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030091 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +080092 i_size_read(inode)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090093 unsigned offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030094 offset = i_size_read(inode) & ~PAGE_MASK;
95 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090096 }
97 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -070098 if (!PageUptodate(page))
99 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900100
Jaegeuk Kime943a102013-10-25 14:26:31 +0900101 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900102mapped:
103 /* fill the page */
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800104 f2fs_wait_on_page_writeback(page, DATA, false);
Chao Yu08b39fb2015-10-08 13:27:34 +0800105
106 /* wait for GCed encrypted page writeback */
107 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
108 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
109
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800110out_sem:
111 up_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900112out:
113 sb_end_pagefault(inode->i_sb);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800114 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900115 return block_page_mkwrite_return(err);
116}
117
118static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800119 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700120 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900121 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900122};
123
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900124static int get_parent_ino(struct inode *inode, nid_t *pino)
125{
126 struct dentry *dentry;
127
128 inode = igrab(inode);
129 dentry = d_find_any_alias(inode);
130 iput(inode);
131 if (!dentry)
132 return 0;
133
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900134 *pino = parent_ino(dentry);
135 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900136 return 1;
137}
138
Chao Yu9d1589e2014-08-20 18:37:35 +0800139static inline bool need_do_checkpoint(struct inode *inode)
140{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700141 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800142 bool need_cp = false;
143
144 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
145 need_cp = true;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700146 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700147 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800148 else if (file_wrong_pino(inode))
149 need_cp = true;
150 else if (!space_for_roll_forward(sbi))
151 need_cp = true;
152 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
153 need_cp = true;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700154 else if (test_opt(sbi, FASTBOOT))
155 need_cp = true;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800156 else if (sbi->active_logs == 2)
157 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800158
159 return need_cp;
160}
161
Changman Lee9c7bb702014-12-08 15:29:40 +0900162static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
163{
164 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
165 bool ret = false;
166 /* But we need to avoid that there are some inode updates */
167 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
168 ret = true;
169 f2fs_put_page(i, 0);
170 return ret;
171}
172
Changman Lee51455b12014-12-08 15:29:41 +0900173static void try_to_fix_pino(struct inode *inode)
174{
175 struct f2fs_inode_info *fi = F2FS_I(inode);
176 nid_t pino;
177
178 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900179 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
180 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700181 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900182 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900183 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700184 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900185}
186
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700187static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
188 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900189{
190 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700191 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700192 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900193 int ret = 0;
194 bool need_cp = false;
195 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900196 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900197 .nr_to_write = LONG_MAX,
198 .for_reclaim = 0,
199 };
200
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900201 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900202 return 0;
203
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900204 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700205
206 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800207 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700208 set_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900209 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700210 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700211
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900212 if (ret) {
213 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900214 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900215 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900216
Changman Lee9c7bb702014-12-08 15:29:40 +0900217 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800218 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700219 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900220 goto go_write;
221 }
222
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700223 /*
224 * if there is no written data, don't waste time to write recovery info.
225 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700226 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700227 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700228
Changman Lee9c7bb702014-12-08 15:29:40 +0900229 /* it may call write_inode just prior to fsync */
230 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700231 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700232
Jaegeuk Kim91942322016-05-20 10:13:22 -0700233 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700234 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700235 goto flush_out;
236 goto out;
237 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700238go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900239 /*
240 * Both of fdatasync() and fsync() are able to be recovered from
241 * sudden-power-off.
242 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700243 down_read(&F2FS_I(inode)->i_sem);
Chao Yu9d1589e2014-08-20 18:37:35 +0800244 need_cp = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700245 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900246
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900247 if (need_cp) {
248 /* all the dirty node pages should be flushed for POR */
249 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900250
Changman Lee51455b12014-12-08 15:29:41 +0900251 /*
252 * We've secured consistency through sync_fs. Following pino
253 * will be used only for fsynced inodes after checkpoint.
254 */
255 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700256 clear_inode_flag(inode, FI_APPEND_WRITE);
257 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900258 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900259 }
Changman Lee51455b12014-12-08 15:29:41 +0900260sync_nodes:
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700261 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700262 if (ret)
263 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900264
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800265 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800266 if (unlikely(f2fs_cp_error(sbi))) {
267 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800268 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800269 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800270
Changman Lee51455b12014-12-08 15:29:41 +0900271 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700272 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900273 f2fs_write_inode(inode, NULL);
274 goto sync_nodes;
275 }
276
277 ret = wait_on_node_pages_writeback(sbi, ino);
278 if (ret)
279 goto out;
280
281 /* once recovery info is written, don't need to tack this */
Chao Yua49324f2015-12-15 13:29:47 +0800282 remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700283 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900284flush_out:
Chao Yua49324f2015-12-15 13:29:47 +0800285 remove_ino_entry(sbi, ino, UPDATE_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700286 clear_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -0800287 if (!atomic)
288 ret = f2fs_issue_flush(sbi);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800289 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900290out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900291 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700292 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900293 return ret;
294}
295
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700296int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
297{
298 return f2fs_do_sync_file(file, start, end, datasync, false);
299}
300
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900301static pgoff_t __get_first_dirty_index(struct address_space *mapping,
302 pgoff_t pgofs, int whence)
303{
304 struct pagevec pvec;
305 int nr_pages;
306
307 if (whence != SEEK_DATA)
308 return 0;
309
310 /* find first dirty page index */
311 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700312 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
313 PAGECACHE_TAG_DIRTY, 1);
Chao Yu80dd9c02016-02-24 17:20:44 +0800314 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900315 pagevec_release(&pvec);
316 return pgofs;
317}
318
319static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
320 int whence)
321{
322 switch (whence) {
323 case SEEK_DATA:
324 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
325 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
326 return true;
327 break;
328 case SEEK_HOLE:
329 if (blkaddr == NULL_ADDR)
330 return true;
331 break;
332 }
333 return false;
334}
335
Chao Yu267378d2014-04-23 14:10:24 +0800336static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
337{
338 struct inode *inode = file->f_mapping->host;
339 loff_t maxbytes = inode->i_sb->s_maxbytes;
340 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900341 pgoff_t pgofs, end_offset, dirty;
342 loff_t data_ofs = offset;
343 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800344 int err = 0;
345
Al Viro59551022016-01-22 15:40:57 -0500346 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800347
348 isize = i_size_read(inode);
349 if (offset >= isize)
350 goto fail;
351
352 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800353 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800354 if (whence == SEEK_HOLE)
355 data_ofs = isize;
356 goto found;
357 }
358
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300359 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800360
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900361 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
362
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300363 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800364 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700365 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800366 if (err && err != -ENOENT) {
367 goto fail;
368 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900369 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800370 if (whence == SEEK_DATA) {
Chao Yu3cf45742016-01-26 15:40:44 +0800371 pgofs = get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800372 continue;
373 } else {
374 goto found;
375 }
376 }
377
Chao Yu81ca7352016-01-26 15:39:35 +0800378 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800379
380 /* find data/hole in dnode block */
381 for (; dn.ofs_in_node < end_offset;
382 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300383 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800384 block_t blkaddr;
385 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
386
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900387 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800388 f2fs_put_dnode(&dn);
389 goto found;
390 }
391 }
392 f2fs_put_dnode(&dn);
393 }
394
395 if (whence == SEEK_DATA)
396 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800397found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900398 if (whence == SEEK_HOLE && data_ofs > isize)
399 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500400 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800401 return vfs_setpos(file, data_ofs, maxbytes);
402fail:
Al Viro59551022016-01-22 15:40:57 -0500403 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800404 return -ENXIO;
405}
406
407static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
408{
409 struct inode *inode = file->f_mapping->host;
410 loff_t maxbytes = inode->i_sb->s_maxbytes;
411
412 switch (whence) {
413 case SEEK_SET:
414 case SEEK_CUR:
415 case SEEK_END:
416 return generic_file_llseek_size(file, offset, whence,
417 maxbytes, i_size_read(inode));
418 case SEEK_DATA:
419 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700420 if (offset < 0)
421 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800422 return f2fs_seek_block(file, offset, whence);
423 }
424
425 return -EINVAL;
426}
427
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900428static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
429{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700430 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800431 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700432
433 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800434 err = f2fs_convert_inline_inode(inode);
435 if (err)
436 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700437
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900438 file_accessed(file);
439 vma->vm_ops = &f2fs_file_vm_ops;
440 return 0;
441}
442
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700443static int f2fs_file_open(struct inode *inode, struct file *filp)
444{
445 int ret = generic_file_open(inode, filp);
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700446 struct dentry *dir;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700447
448 if (!ret && f2fs_encrypted_inode(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700449 ret = fscrypt_get_encryption_info(inode);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700450 if (ret)
Chao Yuae108662016-02-14 18:58:35 +0800451 return -EACCES;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700452 if (!fscrypt_has_encryption_key(inode))
Chao Yuae108662016-02-14 18:58:35 +0800453 return -ENOKEY;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700454 }
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700455 dir = dget_parent(file_dentry(filp));
456 if (f2fs_encrypted_inode(d_inode(dir)) &&
457 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
458 dput(dir);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800459 return -EPERM;
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700460 }
461 dput(dir);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700462 return ret;
463}
464
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900465int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900466{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700467 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900468 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800469 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900470 __le32 *addr;
471
Gu Zheng45590712013-07-15 17:57:38 +0800472 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900473 addr = blkaddr_in_node(raw_node) + ofs;
474
Chris Fries6c311ec2014-01-17 14:44:39 -0600475 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900476 block_t blkaddr = le32_to_cpu(*addr);
477 if (blkaddr == NULL_ADDR)
478 continue;
479
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800480 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800481 set_data_blkaddr(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900482 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700483 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700484 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900485 nr_free++;
486 }
Chao Yu19b2c302015-08-26 20:34:48 +0800487
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900488 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800489 pgoff_t fofs;
490 /*
491 * once we invalidate valid blkaddr in range [ofs, ofs + count],
492 * we will invalidate all blkaddr in the whole range.
493 */
494 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800495 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800496 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900497 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900498 }
499 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900500
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800501 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900502 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
503 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900504 return nr_free;
505}
506
507void truncate_data_blocks(struct dnode_of_data *dn)
508{
509 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
510}
511
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800512static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700513 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900514{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300515 unsigned offset = from & (PAGE_SIZE - 1);
516 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700517 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900518 struct page *page;
519
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700520 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700521 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700523 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700524 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700525 if (page && PageUptodate(page))
526 goto truncate_out;
527 f2fs_put_page(page, 1);
528 return 0;
529 }
530
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700531 page = get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800533 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700534truncate_out:
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800535 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300536 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700537
538 /* An encrypted inode should have a key and truncate the last page. */
539 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
540 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800541 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900542 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700543 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900544}
545
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700546int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900547{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700548 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900549 unsigned int blocksize = inode->i_sb->s_blocksize;
550 struct dnode_of_data dn;
551 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800552 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700553 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800554 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900555
Namjae Jeon51dd6242013-04-20 01:28:52 +0900556 trace_f2fs_truncate_blocks_enter(inode, from);
557
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800558 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900559
Chao Yu09210c92016-05-05 19:13:03 +0800560 if (free_from >= sbi->max_file_blocks)
561 goto free_partial;
562
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700563 if (lock)
564 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800565
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700566 ipage = get_node_page(sbi, inode->i_ino);
567 if (IS_ERR(ipage)) {
568 err = PTR_ERR(ipage);
569 goto out;
570 }
571
572 if (f2fs_has_inline_data(inode)) {
Kinglong Meebd4667c2017-03-10 20:43:20 +0800573 truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700574 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800575 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700576 goto out;
577 }
578
579 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700580 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900581 if (err) {
582 if (err == -ENOENT)
583 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700584 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700585 }
586
Chao Yu81ca7352016-01-26 15:39:35 +0800587 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900588
589 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700590 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900591
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900592 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
593 truncate_data_blocks_range(&dn, count);
594 free_from += count;
595 }
596
597 f2fs_put_dnode(&dn);
598free_next:
599 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800600out:
601 if (lock)
602 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800603free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700604 /* lastly zero out the first data page */
605 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800606 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900607
Namjae Jeon51dd6242013-04-20 01:28:52 +0900608 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900609 return err;
610}
611
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700612int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900613{
Chao Yub0154892015-08-24 17:39:42 +0800614 int err;
615
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900616 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
617 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800618 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900619
Namjae Jeon51dd6242013-04-20 01:28:52 +0900620 trace_f2fs_truncate(inode);
621
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800622#ifdef CONFIG_F2FS_FAULT_INJECTION
623 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
624 f2fs_show_injection_info(FAULT_TRUNCATE);
625 return -EIO;
626 }
627#endif
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800628 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800629 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800630 err = f2fs_convert_inline_inode(inode);
631 if (err)
632 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800633 }
634
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700635 err = truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800636 if (err)
637 return err;
638
Deepa Dinamani078cd822016-09-14 07:48:04 -0700639 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700640 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800641 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900642}
643
David Howellsa528d352017-01-31 16:46:22 +0000644int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800645 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900646{
David Howellsa528d352017-01-31 16:46:22 +0000647 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800648 struct f2fs_inode_info *fi = F2FS_I(inode);
649 unsigned int flags;
650
651 flags = fi->i_flags & FS_FL_USER_VISIBLE;
652 if (flags & FS_APPEND_FL)
653 stat->attributes |= STATX_ATTR_APPEND;
654 if (flags & FS_COMPR_FL)
655 stat->attributes |= STATX_ATTR_COMPRESSED;
656 if (f2fs_encrypted_inode(inode))
657 stat->attributes |= STATX_ATTR_ENCRYPTED;
658 if (flags & FS_IMMUTABLE_FL)
659 stat->attributes |= STATX_ATTR_IMMUTABLE;
660 if (flags & FS_NODUMP_FL)
661 stat->attributes |= STATX_ATTR_NODUMP;
662
663 stat->attributes_mask |= (STATX_ATTR_APPEND |
664 STATX_ATTR_COMPRESSED |
665 STATX_ATTR_ENCRYPTED |
666 STATX_ATTR_IMMUTABLE |
667 STATX_ATTR_NODUMP);
668
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900669 generic_fillattr(inode, stat);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900670 return 0;
671}
672
673#ifdef CONFIG_F2FS_FS_POSIX_ACL
674static void __setattr_copy(struct inode *inode, const struct iattr *attr)
675{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900676 unsigned int ia_valid = attr->ia_valid;
677
678 if (ia_valid & ATTR_UID)
679 inode->i_uid = attr->ia_uid;
680 if (ia_valid & ATTR_GID)
681 inode->i_gid = attr->ia_gid;
682 if (ia_valid & ATTR_ATIME)
683 inode->i_atime = timespec_trunc(attr->ia_atime,
684 inode->i_sb->s_time_gran);
685 if (ia_valid & ATTR_MTIME)
686 inode->i_mtime = timespec_trunc(attr->ia_mtime,
687 inode->i_sb->s_time_gran);
688 if (ia_valid & ATTR_CTIME)
689 inode->i_ctime = timespec_trunc(attr->ia_ctime,
690 inode->i_sb->s_time_gran);
691 if (ia_valid & ATTR_MODE) {
692 umode_t mode = attr->ia_mode;
693
694 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
695 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700696 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900697 }
698}
699#else
700#define __setattr_copy setattr_copy
701#endif
702
703int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
704{
David Howells2b0143b2015-03-17 22:25:59 +0000705 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900706 int err;
Yunlei Hec0ed4402016-12-11 15:35:15 +0800707 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900708
Jan Kara31051c82016-05-26 16:55:18 +0200709 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900710 if (err)
711 return err;
712
Chao Yu09db6a22014-09-15 18:02:09 +0800713 if (attr->ia_valid & ATTR_SIZE) {
Eric Biggers67773a12017-06-13 16:47:54 -0700714 if (f2fs_encrypted_inode(inode)) {
715 err = fscrypt_get_encryption_info(inode);
716 if (err)
717 return err;
718 if (!fscrypt_has_encryption_key(inode))
719 return -ENOKEY;
720 }
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700721
Chao Yu3c454142015-06-05 18:34:02 +0800722 if (attr->ia_size <= i_size_read(inode)) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800723 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu09db6a22014-09-15 18:02:09 +0800724 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700725 err = f2fs_truncate(inode);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800726 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub0154892015-08-24 17:39:42 +0800727 if (err)
728 return err;
Chao Yu09db6a22014-09-15 18:02:09 +0800729 } else {
730 /*
Chao Yu3c454142015-06-05 18:34:02 +0800731 * do not trim all blocks after i_size if target size is
732 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800733 */
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800734 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3c454142015-06-05 18:34:02 +0800735 truncate_setsize(inode, attr->ia_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800736 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu0cab80e2015-12-01 11:36:16 +0800737
738 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800739 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800740 err = f2fs_convert_inline_inode(inode);
741 if (err)
742 return err;
743 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700744 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800745 }
Yunlei Hec0ed4402016-12-11 15:35:15 +0800746
747 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900748 }
749
750 __setattr_copy(inode, attr);
751
752 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800753 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700754 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
755 inode->i_mode = F2FS_I(inode)->i_acl_mode;
756 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900757 }
758 }
759
Yunlei Hec0ed4402016-12-11 15:35:15 +0800760 /* file size may changed here */
761 f2fs_mark_inode_dirty_sync(inode, size_changed);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700762
763 /* inode change will produce dirty node pages flushed by checkpoint */
764 f2fs_balance_fs(F2FS_I_SB(inode), true);
765
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900766 return err;
767}
768
769const struct inode_operations f2fs_file_inode_operations = {
770 .getattr = f2fs_getattr,
771 .setattr = f2fs_setattr,
772 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800773 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900774#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900775 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900776#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900777 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900778};
779
Chao Yu63943282015-08-07 18:36:06 +0800780static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900781 loff_t start, loff_t len)
782{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700783 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900784 struct page *page;
785
786 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800787 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900788
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800789 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900790
Gu Zhenge4795562013-09-27 18:08:30 +0800791 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900792 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800793 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900794
Chao Yu63943282015-08-07 18:36:06 +0800795 if (IS_ERR(page))
796 return PTR_ERR(page);
797
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800798 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800799 zero_user(page, start, len);
800 set_page_dirty(page);
801 f2fs_put_page(page, 1);
802 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900803}
804
805int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
806{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900807 int err;
808
Chao Yuea587112015-09-17 20:22:44 +0800809 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900810 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800811 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900812
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900813 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yuea587112015-09-17 20:22:44 +0800814 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900815 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800816 if (err == -ENOENT) {
817 pg_start++;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900818 continue;
Chao Yuea587112015-09-17 20:22:44 +0800819 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900820 return err;
821 }
822
Chao Yu81ca7352016-01-26 15:39:35 +0800823 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800824 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
825
826 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
827
828 truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900829 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800830
831 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900832 }
833 return 0;
834}
835
Chao Yua66c7b22013-11-22 16:52:50 +0800836static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900837{
838 pgoff_t pg_start, pg_end;
839 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800840 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900841
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800842 ret = f2fs_convert_inline_inode(inode);
843 if (ret)
844 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800845
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300846 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
847 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900848
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300849 off_start = offset & (PAGE_SIZE - 1);
850 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900851
852 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800853 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900854 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800855 if (ret)
856 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900857 } else {
Chao Yu63943282015-08-07 18:36:06 +0800858 if (off_start) {
859 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300860 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800861 if (ret)
862 return ret;
863 }
864 if (off_end) {
865 ret = fill_zero(inode, pg_end, 0, off_end);
866 if (ret)
867 return ret;
868 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900869
870 if (pg_start < pg_end) {
871 struct address_space *mapping = inode->i_mapping;
872 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700873 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500874
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800875 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900876
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300877 blk_start = (loff_t)pg_start << PAGE_SHIFT;
878 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800879 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900880 truncate_inode_pages_range(mapping, blk_start,
881 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900882
Gu Zhenge4795562013-09-27 18:08:30 +0800883 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900884 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800885 f2fs_unlock_op(sbi);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800886 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900887 }
888 }
889
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900890 return ret;
891}
892
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700893static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
894 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800895{
896 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
897 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700898 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700899
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700900next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700901 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700902 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700903 if (ret && ret != -ENOENT) {
904 return ret;
905 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700906 if (dn.max_level == 0)
907 return -ENOENT;
908 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
909 blkaddr += done;
910 do_replace += done;
911 goto next;
912 }
913
914 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
915 dn.ofs_in_node, len);
916 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
917 *blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
918 if (!is_checkpointed_data(sbi, *blkaddr)) {
919
920 if (test_opt(sbi, LFS)) {
921 f2fs_put_dnode(&dn);
922 return -ENOTSUPP;
923 }
924
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700925 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +0800926 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700927 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700928 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700929 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700930 f2fs_put_dnode(&dn);
931next:
932 len -= done;
933 off += done;
934 if (len)
935 goto next_dnode;
936 return 0;
937}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700938
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700939static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
940 int *do_replace, pgoff_t off, int len)
941{
942 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
943 struct dnode_of_data dn;
944 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700945
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700946 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
947 if (*do_replace == 0)
948 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700949
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700950 set_new_dnode(&dn, inode, NULL, NULL, 0);
951 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
952 if (ret) {
953 dec_valid_block_count(sbi, inode, 1);
954 invalidate_blocks(sbi, *blkaddr);
955 } else {
956 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700957 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700958 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700959 }
960 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700961}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700962
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700963static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
964 block_t *blkaddr, int *do_replace,
965 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
966{
967 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
968 pgoff_t i = 0;
969 int ret;
970
971 while (i < len) {
972 if (blkaddr[i] == NULL_ADDR && !full) {
973 i++;
974 continue;
975 }
976
977 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
978 struct dnode_of_data dn;
979 struct node_info ni;
980 size_t new_size;
981 pgoff_t ilen;
982
983 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
984 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
985 if (ret)
986 return ret;
987
988 get_node_info(sbi, dn.nid, &ni);
989 ilen = min((pgoff_t)
990 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
991 dn.ofs_in_node, len - i);
992 do {
993 dn.data_blkaddr = datablock_addr(dn.node_page,
994 dn.ofs_in_node);
995 truncate_data_blocks_range(&dn, 1);
996
997 if (do_replace[i]) {
998 f2fs_i_blocks_write(src_inode,
999 1, false);
1000 f2fs_i_blocks_write(dst_inode,
1001 1, true);
1002 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1003 blkaddr[i], ni.version, true, false);
1004
1005 do_replace[i] = 0;
1006 }
1007 dn.ofs_in_node++;
1008 i++;
1009 new_size = (dst + i) << PAGE_SHIFT;
1010 if (dst_inode->i_size < new_size)
1011 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001012 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001013
1014 f2fs_put_dnode(&dn);
1015 } else {
1016 struct page *psrc, *pdst;
1017
1018 psrc = get_lock_data_page(src_inode, src + i, true);
1019 if (IS_ERR(psrc))
1020 return PTR_ERR(psrc);
1021 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1022 true);
1023 if (IS_ERR(pdst)) {
1024 f2fs_put_page(psrc, 1);
1025 return PTR_ERR(pdst);
1026 }
1027 f2fs_copy_page(psrc, pdst);
1028 set_page_dirty(pdst);
1029 f2fs_put_page(pdst, 1);
1030 f2fs_put_page(psrc, 1);
1031
1032 ret = truncate_hole(src_inode, src + i, src + i + 1);
1033 if (ret)
1034 return ret;
1035 i++;
1036 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001037 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001038 return 0;
1039}
1040
1041static int __exchange_data_block(struct inode *src_inode,
1042 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001043 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001044{
1045 block_t *src_blkaddr;
1046 int *do_replace;
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001047 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001048 int ret;
1049
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001050 while (len) {
1051 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001052
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001053 src_blkaddr = f2fs_kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
1054 if (!src_blkaddr)
1055 return -ENOMEM;
1056
1057 do_replace = f2fs_kvzalloc(sizeof(int) * olen, GFP_KERNEL);
1058 if (!do_replace) {
1059 kvfree(src_blkaddr);
1060 return -ENOMEM;
1061 }
1062
1063 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1064 do_replace, src, olen);
1065 if (ret)
1066 goto roll_back;
1067
1068 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1069 do_replace, src, dst, olen, full);
1070 if (ret)
1071 goto roll_back;
1072
1073 src += olen;
1074 dst += olen;
1075 len -= olen;
1076
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001077 kvfree(src_blkaddr);
Jaegeuk Kim363cad7f2016-07-16 21:59:22 -07001078 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001079 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001080 return 0;
1081
1082roll_back:
1083 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1084 kvfree(src_blkaddr);
1085 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001086 return ret;
1087}
1088
1089static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1090{
1091 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001092 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001093 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001094
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001095 f2fs_balance_fs(sbi, true);
1096 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001097
1098 f2fs_drop_extent_tree(inode);
1099
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001100 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1101 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +08001102 return ret;
1103}
1104
1105static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1106{
1107 pgoff_t pg_start, pg_end;
1108 loff_t new_size;
1109 int ret;
1110
Chao Yub4ace332015-05-06 13:09:46 +08001111 if (offset + len >= i_size_read(inode))
1112 return -EINVAL;
1113
1114 /* collapse range should be aligned to block size of f2fs. */
1115 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1116 return -EINVAL;
1117
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001118 ret = f2fs_convert_inline_inode(inode);
1119 if (ret)
1120 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001121
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001122 pg_start = offset >> PAGE_SHIFT;
1123 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yub4ace332015-05-06 13:09:46 +08001124
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001125 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001126 /* write out all dirty pages from offset */
1127 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1128 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001129 goto out;
Chao Yub4ace332015-05-06 13:09:46 +08001130
1131 truncate_pagecache(inode, offset);
1132
1133 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1134 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001135 goto out;
Chao Yub4ace332015-05-06 13:09:46 +08001136
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001137 /* write out all moved pages, if possible */
1138 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1139 truncate_pagecache(inode, offset);
1140
Chao Yub4ace332015-05-06 13:09:46 +08001141 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001142 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001143
1144 ret = truncate_blocks(inode, new_size, true);
1145 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001146 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001147
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001148out:
1149 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001150 return ret;
1151}
1152
Chao Yu6e961942016-05-09 19:56:31 +08001153static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1154 pgoff_t end)
1155{
1156 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1157 pgoff_t index = start;
1158 unsigned int ofs_in_node = dn->ofs_in_node;
1159 blkcnt_t count = 0;
1160 int ret;
1161
1162 for (; index < end; index++, dn->ofs_in_node++) {
1163 if (datablock_addr(dn->node_page, dn->ofs_in_node) == NULL_ADDR)
1164 count++;
1165 }
1166
1167 dn->ofs_in_node = ofs_in_node;
1168 ret = reserve_new_blocks(dn, count);
1169 if (ret)
1170 return ret;
1171
1172 dn->ofs_in_node = ofs_in_node;
1173 for (index = start; index < end; index++, dn->ofs_in_node++) {
1174 dn->data_blkaddr =
1175 datablock_addr(dn->node_page, dn->ofs_in_node);
1176 /*
1177 * reserve_new_blocks will not guarantee entire block
1178 * allocation.
1179 */
1180 if (dn->data_blkaddr == NULL_ADDR) {
1181 ret = -ENOSPC;
1182 break;
1183 }
1184 if (dn->data_blkaddr != NEW_ADDR) {
1185 invalidate_blocks(sbi, dn->data_blkaddr);
1186 dn->data_blkaddr = NEW_ADDR;
1187 set_data_blkaddr(dn);
1188 }
1189 }
1190
1191 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1192
1193 return ret;
1194}
1195
Chao Yu75cd4e02015-05-06 13:11:13 +08001196static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1197 int mode)
1198{
1199 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1200 struct address_space *mapping = inode->i_mapping;
1201 pgoff_t index, pg_start, pg_end;
1202 loff_t new_size = i_size_read(inode);
1203 loff_t off_start, off_end;
1204 int ret = 0;
1205
Chao Yu75cd4e02015-05-06 13:11:13 +08001206 ret = inode_newsize_ok(inode, (len + offset));
1207 if (ret)
1208 return ret;
1209
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001210 ret = f2fs_convert_inline_inode(inode);
1211 if (ret)
1212 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001213
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001214 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001215 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1216 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001217 goto out_sem;
Chao Yu75cd4e02015-05-06 13:11:13 +08001218
1219 truncate_pagecache_range(inode, offset, offset + len - 1);
1220
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001221 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1222 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001223
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001224 off_start = offset & (PAGE_SIZE - 1);
1225 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001226
1227 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001228 ret = fill_zero(inode, pg_start, off_start,
1229 off_end - off_start);
1230 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001231 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001232
Chao Yu75cd4e02015-05-06 13:11:13 +08001233 new_size = max_t(loff_t, new_size, offset + len);
1234 } else {
1235 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001236 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001237 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001238 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001239 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001240
Chao Yu75cd4e02015-05-06 13:11:13 +08001241 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001242 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001243 }
1244
Chao Yu6e961942016-05-09 19:56:31 +08001245 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001246 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001247 unsigned int end_offset;
1248 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001249
1250 f2fs_lock_op(sbi);
1251
Chao Yu6e961942016-05-09 19:56:31 +08001252 set_new_dnode(&dn, inode, NULL, NULL, 0);
1253 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001254 if (ret) {
1255 f2fs_unlock_op(sbi);
1256 goto out;
1257 }
1258
Chao Yu6e961942016-05-09 19:56:31 +08001259 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1260 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1261
1262 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001263 f2fs_put_dnode(&dn);
1264 f2fs_unlock_op(sbi);
Chao Yu9434fcd2016-10-11 22:57:02 +08001265
1266 f2fs_balance_fs(sbi, dn.node_changed);
1267
Chao Yu6e961942016-05-09 19:56:31 +08001268 if (ret)
1269 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001270
Chao Yu6e961942016-05-09 19:56:31 +08001271 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001272 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001273 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001274 }
1275
1276 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001277 ret = fill_zero(inode, pg_end, 0, off_end);
1278 if (ret)
1279 goto out;
1280
Chao Yu75cd4e02015-05-06 13:11:13 +08001281 new_size = max_t(loff_t, new_size, offset + len);
1282 }
1283 }
1284
1285out:
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001286 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001287 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001288out_sem:
1289 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001290
1291 return ret;
1292}
1293
Chao Yuf62185d2015-05-28 19:16:57 +08001294static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1295{
1296 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001297 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001298 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001299 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001300
Chao Yuf62185d2015-05-28 19:16:57 +08001301 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001302 ret = inode_newsize_ok(inode, new_size);
1303 if (ret)
1304 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001305
1306 if (offset >= i_size_read(inode))
1307 return -EINVAL;
1308
1309 /* insert range should be aligned to block size of f2fs. */
1310 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1311 return -EINVAL;
1312
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001313 ret = f2fs_convert_inline_inode(inode);
1314 if (ret)
1315 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001316
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001317 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001318
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001319 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001320 ret = truncate_blocks(inode, i_size_read(inode), true);
1321 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001322 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001323
1324 /* write out all dirty pages from offset */
1325 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1326 if (ret)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001327 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001328
1329 truncate_pagecache(inode, offset);
1330
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001331 pg_start = offset >> PAGE_SHIFT;
1332 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001333 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001334 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001335
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001336 while (!ret && idx > pg_start) {
1337 nr = idx - pg_start;
1338 if (nr > delta)
1339 nr = delta;
1340 idx -= nr;
1341
Chao Yuf62185d2015-05-28 19:16:57 +08001342 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001343 f2fs_drop_extent_tree(inode);
1344
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001345 ret = __exchange_data_block(inode, inode, idx,
1346 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001347 f2fs_unlock_op(sbi);
1348 }
1349
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001350 /* write out all moved pages, if possible */
1351 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1352 truncate_pagecache(inode, offset);
1353
1354 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001355 f2fs_i_size_write(inode, new_size);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001356out:
1357 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001358 return ret;
1359}
1360
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001361static int expand_inode_data(struct inode *inode, loff_t offset,
1362 loff_t len, int mode)
1363{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001364 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001365 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1366 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001367 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001368 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001369 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001370
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001371 err = inode_newsize_ok(inode, (len + offset));
1372 if (err)
1373 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001374
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001375 err = f2fs_convert_inline_inode(inode);
1376 if (err)
1377 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001378
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001379 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001380
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001381 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001382 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001383
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001384 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1385 map.m_len = pg_end - map.m_lblk;
1386 if (off_end)
1387 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001388
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001389 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1390 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001391 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001392
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001393 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001394 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001395
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001396 last_off = map.m_lblk + map.m_len - 1;
1397
1398 /* update new size to the failed position */
1399 new_size = (last_off == pg_end) ? offset + len:
1400 (loff_t)(last_off + 1) << PAGE_SHIFT;
1401 } else {
1402 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001403 }
1404
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001405 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001406 f2fs_i_size_write(inode, new_size);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001407
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001408 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001409}
1410
1411static long f2fs_fallocate(struct file *file, int mode,
1412 loff_t offset, loff_t len)
1413{
Al Viro6131ffa2013-02-27 16:59:05 -05001414 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001415 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001416
Chao Yuc9980122015-09-11 14:39:02 +08001417 /* f2fs only support ->fallocate for regular file */
1418 if (!S_ISREG(inode->i_mode))
1419 return -EINVAL;
1420
Chao Yuf62185d2015-05-28 19:16:57 +08001421 if (f2fs_encrypted_inode(inode) &&
1422 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001423 return -EOPNOTSUPP;
1424
Chao Yub4ace332015-05-06 13:09:46 +08001425 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001426 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1427 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001428 return -EOPNOTSUPP;
1429
Al Viro59551022016-01-22 15:40:57 -05001430 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001431
Taehee Yoo587c0a42015-04-21 15:59:12 +09001432 if (mode & FALLOC_FL_PUNCH_HOLE) {
1433 if (offset >= inode->i_size)
1434 goto out;
1435
Chao Yua66c7b22013-11-22 16:52:50 +08001436 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001437 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1438 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001439 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1440 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001441 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1442 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001443 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001444 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001445 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001446
Namjae Jeon3af60a42012-12-30 14:52:37 +09001447 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001448 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001449 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kim26787232016-11-28 15:33:38 -08001450 if (mode & FALLOC_FL_KEEP_SIZE)
1451 file_set_keep_isize(inode);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001452 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001453 }
Chao Yu3375f692014-01-28 10:29:26 +08001454
Taehee Yoo587c0a42015-04-21 15:59:12 +09001455out:
Al Viro59551022016-01-22 15:40:57 -05001456 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001457
Namjae Jeonc01e2852013-04-23 17:00:52 +09001458 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001459 return ret;
1460}
1461
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001462static int f2fs_release_file(struct inode *inode, struct file *filp)
1463{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001464 /*
1465 * f2fs_relase_file is called at every close calls. So we should
1466 * not drop any inmemory pages by close called by other process.
1467 */
1468 if (!(filp->f_mode & FMODE_WRITE) ||
1469 atomic_read(&inode->i_writecount) != 1)
1470 return 0;
1471
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001472 /* some remained atomic pages should discarded */
1473 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001474 drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001475 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001476 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001477 stat_dec_volatile_write(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001478 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001479 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001480 clear_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001481 }
1482 return 0;
1483}
1484
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001485#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1486#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1487
1488static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1489{
1490 if (S_ISDIR(mode))
1491 return flags;
1492 else if (S_ISREG(mode))
1493 return flags & F2FS_REG_FLMASK;
1494 else
1495 return flags & F2FS_OTHER_FLMASK;
1496}
1497
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001498static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001499{
Al Viro6131ffa2013-02-27 16:59:05 -05001500 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001501 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001502 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1503 return put_user(flags, (int __user *)arg);
1504}
1505
1506static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1507{
1508 struct inode *inode = file_inode(filp);
1509 struct f2fs_inode_info *fi = F2FS_I(inode);
Sheng Yong69494222016-08-23 20:10:47 +08001510 unsigned int flags;
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001511 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001512 int ret;
1513
Chao Yu7fb17fe2016-05-09 19:56:32 +08001514 if (!inode_owner_or_capable(inode))
1515 return -EACCES;
1516
1517 if (get_user(flags, (int __user *)arg))
1518 return -EFAULT;
1519
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001520 ret = mnt_want_write_file(filp);
1521 if (ret)
1522 return ret;
1523
Al Viro59551022016-01-22 15:40:57 -05001524 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001525
Chao Yua72d4b92017-05-03 23:59:13 +08001526 flags = f2fs_mask_flags(inode->i_mode, flags);
1527
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001528 oldflags = fi->i_flags;
1529
1530 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1531 if (!capable(CAP_LINUX_IMMUTABLE)) {
Al Viro59551022016-01-22 15:40:57 -05001532 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001533 ret = -EPERM;
1534 goto out;
1535 }
1536 }
1537
1538 flags = flags & FS_FL_USER_MODIFIABLE;
1539 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1540 fi->i_flags = flags;
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001541
Deepa Dinamani078cd822016-09-14 07:48:04 -07001542 inode->i_ctime = current_time(inode);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001543 f2fs_set_inode_flags(inode);
Jaegeuk Kim93607122017-05-16 13:20:16 -07001544 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yua72d4b92017-05-03 23:59:13 +08001545
1546 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001547out:
1548 mnt_drop_write_file(filp);
1549 return ret;
1550}
1551
Chao Yud49f3e82015-01-23 20:36:04 +08001552static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1553{
1554 struct inode *inode = file_inode(filp);
1555
1556 return put_user(inode->i_generation, (int __user *)arg);
1557}
1558
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001559static int f2fs_ioc_start_atomic_write(struct file *filp)
1560{
1561 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001562 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001563
1564 if (!inode_owner_or_capable(inode))
1565 return -EACCES;
1566
Jaegeuk Kime8118982017-03-17 10:04:15 +08001567 if (!S_ISREG(inode->i_mode))
1568 return -EINVAL;
1569
Chao Yu7fb17fe2016-05-09 19:56:32 +08001570 ret = mnt_want_write_file(filp);
1571 if (ret)
1572 return ret;
1573
Chao Yu0fac5582016-05-09 19:56:33 +08001574 inode_lock(inode);
1575
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001576 if (f2fs_is_atomic_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001577 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001578
Chao Yuf4c9c742015-07-17 18:06:35 +08001579 ret = f2fs_convert_inline_inode(inode);
1580 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001581 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001582
Jaegeuk Kim91942322016-05-20 10:13:22 -07001583 set_inode_flag(inode, FI_ATOMIC_FILE);
Jaegeuk Kim6c3acd92017-04-12 10:01:33 -07001584 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001585 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1586
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001587 if (!get_dirty_pages(inode))
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001588 goto inc_stat;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001589
1590 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001591 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001592 inode->i_ino, get_dirty_pages(inode));
1593 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001594 if (ret) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001595 clear_inode_flag(inode, FI_ATOMIC_FILE);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001596 goto out;
1597 }
1598
1599inc_stat:
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001600 stat_inc_atomic_write(inode);
1601 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08001602out:
Chao Yu0fac5582016-05-09 19:56:33 +08001603 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001604 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001605 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001606}
1607
1608static int f2fs_ioc_commit_atomic_write(struct file *filp)
1609{
1610 struct inode *inode = file_inode(filp);
1611 int ret;
1612
1613 if (!inode_owner_or_capable(inode))
1614 return -EACCES;
1615
1616 ret = mnt_want_write_file(filp);
1617 if (ret)
1618 return ret;
1619
Chao Yu0fac5582016-05-09 19:56:33 +08001620 inode_lock(inode);
1621
Chao Yu7fb17fe2016-05-09 19:56:32 +08001622 if (f2fs_is_volatile_file(inode))
1623 goto err_out;
1624
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001625 if (f2fs_is_atomic_file(inode)) {
Chao Yu29b96b52016-02-06 14:38:29 +08001626 ret = commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08001627 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001628 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001629
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001630 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Chao Yu5fe45742017-01-07 18:50:26 +08001631 if (!ret) {
1632 clear_inode_flag(inode, FI_ATOMIC_FILE);
1633 stat_dec_atomic_write(inode);
1634 }
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08001635 } else {
1636 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001637 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001638err_out:
Chao Yu0fac5582016-05-09 19:56:33 +08001639 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001640 mnt_drop_write_file(filp);
1641 return ret;
1642}
1643
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001644static int f2fs_ioc_start_volatile_write(struct file *filp)
1645{
1646 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001647 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001648
1649 if (!inode_owner_or_capable(inode))
1650 return -EACCES;
1651
Chao Yu8ff09712017-03-17 15:43:57 +08001652 if (!S_ISREG(inode->i_mode))
1653 return -EINVAL;
1654
Chao Yu7fb17fe2016-05-09 19:56:32 +08001655 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001656 if (ret)
1657 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001658
Chao Yu0fac5582016-05-09 19:56:33 +08001659 inode_lock(inode);
1660
Chao Yu7fb17fe2016-05-09 19:56:32 +08001661 if (f2fs_is_volatile_file(inode))
1662 goto out;
1663
1664 ret = f2fs_convert_inline_inode(inode);
1665 if (ret)
1666 goto out;
1667
Chao Yu648d50b2017-03-22 17:23:45 +08001668 stat_inc_volatile_write(inode);
1669 stat_update_max_volatile_write(inode);
1670
Jaegeuk Kim91942322016-05-20 10:13:22 -07001671 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001672 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001673out:
Chao Yu0fac5582016-05-09 19:56:33 +08001674 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001675 mnt_drop_write_file(filp);
1676 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001677}
1678
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001679static int f2fs_ioc_release_volatile_write(struct file *filp)
1680{
1681 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001682 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001683
1684 if (!inode_owner_or_capable(inode))
1685 return -EACCES;
1686
Chao Yu7fb17fe2016-05-09 19:56:32 +08001687 ret = mnt_want_write_file(filp);
1688 if (ret)
1689 return ret;
1690
Chao Yu0fac5582016-05-09 19:56:33 +08001691 inode_lock(inode);
1692
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001693 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001694 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001695
Chao Yu7fb17fe2016-05-09 19:56:32 +08001696 if (!f2fs_is_first_block_written(inode)) {
1697 ret = truncate_partial_data_page(inode, 0, true);
1698 goto out;
1699 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001700
Chao Yu7fb17fe2016-05-09 19:56:32 +08001701 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1702out:
Chao Yu0fac5582016-05-09 19:56:33 +08001703 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001704 mnt_drop_write_file(filp);
1705 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001706}
1707
1708static int f2fs_ioc_abort_volatile_write(struct file *filp)
1709{
1710 struct inode *inode = file_inode(filp);
1711 int ret;
1712
1713 if (!inode_owner_or_capable(inode))
1714 return -EACCES;
1715
1716 ret = mnt_want_write_file(filp);
1717 if (ret)
1718 return ret;
1719
Chao Yu0fac5582016-05-09 19:56:33 +08001720 inode_lock(inode);
1721
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001722 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001723 drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001724 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001725 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08001726 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001727 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001728 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001729
Chao Yu0fac5582016-05-09 19:56:33 +08001730 inode_unlock(inode);
1731
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001732 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001733 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001734 return ret;
1735}
1736
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001737static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1738{
1739 struct inode *inode = file_inode(filp);
1740 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1741 struct super_block *sb = sbi->sb;
1742 __u32 in;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001743 int ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001744
1745 if (!capable(CAP_SYS_ADMIN))
1746 return -EPERM;
1747
1748 if (get_user(in, (__u32 __user *)arg))
1749 return -EFAULT;
1750
Chao Yu7fb17fe2016-05-09 19:56:32 +08001751 ret = mnt_want_write_file(filp);
1752 if (ret)
1753 return ret;
1754
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001755 switch (in) {
1756 case F2FS_GOING_DOWN_FULLSYNC:
1757 sb = freeze_bdev(sb->s_bdev);
1758 if (sb && !IS_ERR(sb)) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001759 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001760 thaw_bdev(sb->s_bdev, sb);
1761 }
1762 break;
1763 case F2FS_GOING_DOWN_METASYNC:
1764 /* do checkpoint only */
1765 f2fs_sync_fs(sb, 1);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001766 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001767 break;
1768 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001769 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001770 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001771 case F2FS_GOING_DOWN_METAFLUSH:
1772 sync_meta_pages(sbi, META, LONG_MAX);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001773 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001774 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001775 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001776 ret = -EINVAL;
1777 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001778 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001779 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001780out:
1781 mnt_drop_write_file(filp);
1782 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001783}
1784
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001785static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1786{
1787 struct inode *inode = file_inode(filp);
1788 struct super_block *sb = inode->i_sb;
1789 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1790 struct fstrim_range range;
1791 int ret;
1792
1793 if (!capable(CAP_SYS_ADMIN))
1794 return -EPERM;
1795
1796 if (!blk_queue_discard(q))
1797 return -EOPNOTSUPP;
1798
1799 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1800 sizeof(range)))
1801 return -EFAULT;
1802
Chao Yu7fb17fe2016-05-09 19:56:32 +08001803 ret = mnt_want_write_file(filp);
1804 if (ret)
1805 return ret;
1806
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001807 range.minlen = max((unsigned int)range.minlen,
1808 q->limits.discard_granularity);
1809 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001810 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001811 if (ret < 0)
1812 return ret;
1813
1814 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1815 sizeof(range)))
1816 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001817 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001818 return 0;
1819}
1820
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001821static bool uuid_is_nonzero(__u8 u[16])
1822{
1823 int i;
1824
1825 for (i = 0; i < 16; i++)
1826 if (u[i])
1827 return true;
1828 return false;
1829}
1830
1831static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1832{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001833 struct inode *inode = file_inode(filp);
1834
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001835 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001836
Eric Biggersdb717d82016-11-26 19:07:49 -05001837 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001838}
1839
1840static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1841{
Eric Biggersdb717d82016-11-26 19:07:49 -05001842 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001843}
1844
1845static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1846{
1847 struct inode *inode = file_inode(filp);
1848 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1849 int err;
1850
1851 if (!f2fs_sb_has_crypto(inode->i_sb))
1852 return -EOPNOTSUPP;
1853
1854 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1855 goto got_it;
1856
1857 err = mnt_want_write_file(filp);
1858 if (err)
1859 return err;
1860
1861 /* update superblock with uuid */
1862 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1863
Chao Yuc5bda1c2015-06-08 13:28:03 +08001864 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001865 if (err) {
1866 /* undo new data */
1867 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yue8240f62015-12-15 17:19:26 +08001868 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001869 return err;
1870 }
Chao Yue8240f62015-12-15 17:19:26 +08001871 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001872got_it:
1873 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1874 16))
1875 return -EFAULT;
1876 return 0;
1877}
1878
Chao Yuc1c1b582015-07-10 18:08:10 +08001879static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1880{
1881 struct inode *inode = file_inode(filp);
1882 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08001883 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001884 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08001885
1886 if (!capable(CAP_SYS_ADMIN))
1887 return -EPERM;
1888
Chao Yud530d4d2015-10-05 22:22:44 +08001889 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08001890 return -EFAULT;
1891
Chao Yud530d4d2015-10-05 22:22:44 +08001892 if (f2fs_readonly(sbi->sb))
1893 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08001894
Chao Yu7fb17fe2016-05-09 19:56:32 +08001895 ret = mnt_want_write_file(filp);
1896 if (ret)
1897 return ret;
1898
Chao Yud530d4d2015-10-05 22:22:44 +08001899 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08001900 if (!mutex_trylock(&sbi->gc_mutex)) {
1901 ret = -EBUSY;
1902 goto out;
1903 }
Chao Yud530d4d2015-10-05 22:22:44 +08001904 } else {
1905 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08001906 }
1907
Jaegeuk Kime066b832017-04-13 15:17:00 -07001908 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001909out:
1910 mnt_drop_write_file(filp);
1911 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08001912}
1913
Chao Yu456b88e2015-10-05 22:24:19 +08001914static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
1915{
1916 struct inode *inode = file_inode(filp);
1917 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001918 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08001919
1920 if (!capable(CAP_SYS_ADMIN))
1921 return -EPERM;
1922
1923 if (f2fs_readonly(sbi->sb))
1924 return -EROFS;
1925
Chao Yu7fb17fe2016-05-09 19:56:32 +08001926 ret = mnt_want_write_file(filp);
1927 if (ret)
1928 return ret;
1929
1930 ret = f2fs_sync_fs(sbi->sb, 1);
1931
1932 mnt_drop_write_file(filp);
1933 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08001934}
1935
Chao Yud323d002015-10-27 09:53:45 +08001936static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
1937 struct file *filp,
1938 struct f2fs_defragment *range)
1939{
1940 struct inode *inode = file_inode(filp);
Chao Yuda859852016-01-26 15:42:58 +08001941 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
Hou Pengyange15882b2017-02-23 09:18:05 +00001942 struct extent_info ei = {0,0,0};
Chao Yud323d002015-10-27 09:53:45 +08001943 pgoff_t pg_start, pg_end;
Chao Yu3519e3f2015-12-01 11:56:52 +08001944 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08001945 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08001946 block_t blk_end = 0;
1947 bool fragmented = false;
1948 int err;
1949
1950 /* if in-place-update policy is enabled, don't waste time here */
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001951 if (need_inplace_update_policy(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08001952 return -EINVAL;
1953
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001954 pg_start = range->start >> PAGE_SHIFT;
1955 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08001956
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001957 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08001958
Al Viro59551022016-01-22 15:40:57 -05001959 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08001960
1961 /* writeback all dirty pages in the range */
1962 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08001963 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08001964 if (err)
1965 goto out;
1966
1967 /*
1968 * lookup mapping info in extent cache, skip defragmenting if physical
1969 * block addresses are continuous.
1970 */
1971 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
1972 if (ei.fofs + ei.len >= pg_end)
1973 goto out;
1974 }
1975
1976 map.m_lblk = pg_start;
Chao Yud323d002015-10-27 09:53:45 +08001977
1978 /*
1979 * lookup mapping info in dnode page cache, skip defragmenting if all
1980 * physical block addresses are continuous even if there are hole(s)
1981 * in logical blocks.
1982 */
1983 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08001984 map.m_len = pg_end - map.m_lblk;
Chao Yud323d002015-10-27 09:53:45 +08001985 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1986 if (err)
1987 goto out;
1988
1989 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1990 map.m_lblk++;
Chao Yud323d002015-10-27 09:53:45 +08001991 continue;
1992 }
1993
1994 if (blk_end && blk_end != map.m_pblk) {
1995 fragmented = true;
1996 break;
1997 }
1998 blk_end = map.m_pblk + map.m_len;
1999
2000 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002001 }
2002
2003 if (!fragmented)
2004 goto out;
2005
2006 map.m_lblk = pg_start;
2007 map.m_len = pg_end - pg_start;
2008
Jaegeuk Kim4ddb1a42017-04-07 15:08:17 -07002009 sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002010
2011 /*
2012 * make sure there are enough free section for LFS allocation, this can
2013 * avoid defragment running in SSR mode when free section are allocated
2014 * intensively
2015 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002016 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002017 err = -EAGAIN;
2018 goto out;
2019 }
2020
2021 while (map.m_lblk < pg_end) {
2022 pgoff_t idx;
2023 int cnt = 0;
2024
2025do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002026 map.m_len = pg_end - map.m_lblk;
Chao Yud323d002015-10-27 09:53:45 +08002027 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
2028 if (err)
2029 goto clear_out;
2030
2031 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2032 map.m_lblk++;
2033 continue;
2034 }
2035
Jaegeuk Kim91942322016-05-20 10:13:22 -07002036 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002037
2038 idx = map.m_lblk;
2039 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2040 struct page *page;
2041
2042 page = get_lock_data_page(inode, idx, true);
2043 if (IS_ERR(page)) {
2044 err = PTR_ERR(page);
2045 goto clear_out;
2046 }
2047
2048 set_page_dirty(page);
2049 f2fs_put_page(page, 1);
2050
2051 idx++;
2052 cnt++;
2053 total++;
2054 }
2055
2056 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002057
2058 if (idx < pg_end && cnt < blk_per_seg)
2059 goto do_map;
2060
Jaegeuk Kim91942322016-05-20 10:13:22 -07002061 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002062
2063 err = filemap_fdatawrite(inode->i_mapping);
2064 if (err)
2065 goto out;
2066 }
2067clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002068 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002069out:
Al Viro59551022016-01-22 15:40:57 -05002070 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002071 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002073 return err;
2074}
2075
2076static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2077{
2078 struct inode *inode = file_inode(filp);
2079 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2080 struct f2fs_defragment range;
2081 int err;
2082
2083 if (!capable(CAP_SYS_ADMIN))
2084 return -EPERM;
2085
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002086 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002087 return -EINVAL;
2088
Kinglong Meed7563862017-03-10 17:55:07 +08002089 if (f2fs_readonly(sbi->sb))
2090 return -EROFS;
2091
2092 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2093 sizeof(range)))
2094 return -EFAULT;
2095
2096 /* verify alignment of offset & size */
2097 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2098 return -EINVAL;
2099
2100 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2101 sbi->max_file_blocks))
2102 return -EINVAL;
2103
Chao Yud323d002015-10-27 09:53:45 +08002104 err = mnt_want_write_file(filp);
2105 if (err)
2106 return err;
2107
Chao Yud323d002015-10-27 09:53:45 +08002108 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002109 mnt_drop_write_file(filp);
2110
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002111 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002112 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002113 return err;
Chao Yud323d002015-10-27 09:53:45 +08002114
2115 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2116 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002117 return -EFAULT;
2118
2119 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002120}
2121
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002122static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2123 struct file *file_out, loff_t pos_out, size_t len)
2124{
2125 struct inode *src = file_inode(file_in);
2126 struct inode *dst = file_inode(file_out);
2127 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2128 size_t olen = len, dst_max_i_size = 0;
2129 size_t dst_osize;
2130 int ret;
2131
2132 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2133 src->i_sb != dst->i_sb)
2134 return -EXDEV;
2135
2136 if (unlikely(f2fs_readonly(src->i_sb)))
2137 return -EROFS;
2138
Chao Yufe8494b2016-08-04 20:13:02 +08002139 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2140 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002141
2142 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2143 return -EOPNOTSUPP;
2144
Fan Lid95fd912016-09-13 11:35:42 +08002145 if (src == dst) {
2146 if (pos_in == pos_out)
2147 return 0;
2148 if (pos_out > pos_in && pos_out < pos_in + len)
2149 return -EINVAL;
2150 }
2151
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002152 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002153 if (src != dst) {
2154 if (!inode_trylock(dst)) {
2155 ret = -EBUSY;
2156 goto out;
2157 }
2158 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002159
2160 ret = -EINVAL;
2161 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2162 goto out_unlock;
2163 if (len == 0)
2164 olen = len = src->i_size - pos_in;
2165 if (pos_in + len == src->i_size)
2166 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2167 if (len == 0) {
2168 ret = 0;
2169 goto out_unlock;
2170 }
2171
2172 dst_osize = dst->i_size;
2173 if (pos_out + olen > dst->i_size)
2174 dst_max_i_size = pos_out + olen;
2175
2176 /* verify the end result is block aligned */
2177 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2178 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2179 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2180 goto out_unlock;
2181
2182 ret = f2fs_convert_inline_inode(src);
2183 if (ret)
2184 goto out_unlock;
2185
2186 ret = f2fs_convert_inline_inode(dst);
2187 if (ret)
2188 goto out_unlock;
2189
2190 /* write out all dirty pages from offset */
2191 ret = filemap_write_and_wait_range(src->i_mapping,
2192 pos_in, pos_in + len);
2193 if (ret)
2194 goto out_unlock;
2195
2196 ret = filemap_write_and_wait_range(dst->i_mapping,
2197 pos_out, pos_out + len);
2198 if (ret)
2199 goto out_unlock;
2200
2201 f2fs_balance_fs(sbi, true);
2202 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002203 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2204 pos_out >> F2FS_BLKSIZE_BITS,
2205 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002206
2207 if (!ret) {
2208 if (dst_max_i_size)
2209 f2fs_i_size_write(dst, dst_max_i_size);
2210 else if (dst_osize != dst->i_size)
2211 f2fs_i_size_write(dst, dst_osize);
2212 }
2213 f2fs_unlock_op(sbi);
2214out_unlock:
2215 if (src != dst)
2216 inode_unlock(dst);
Chao Yu20a3d612016-08-04 20:13:03 +08002217out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002218 inode_unlock(src);
2219 return ret;
2220}
2221
2222static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2223{
2224 struct f2fs_move_range range;
2225 struct fd dst;
2226 int err;
2227
2228 if (!(filp->f_mode & FMODE_READ) ||
2229 !(filp->f_mode & FMODE_WRITE))
2230 return -EBADF;
2231
2232 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2233 sizeof(range)))
2234 return -EFAULT;
2235
2236 dst = fdget(range.dst_fd);
2237 if (!dst.file)
2238 return -EBADF;
2239
2240 if (!(dst.file->f_mode & FMODE_WRITE)) {
2241 err = -EBADF;
2242 goto err_out;
2243 }
2244
2245 err = mnt_want_write_file(filp);
2246 if (err)
2247 goto err_out;
2248
2249 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2250 range.pos_out, range.len);
2251
2252 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002253 if (err)
2254 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002255
2256 if (copy_to_user((struct f2fs_move_range __user *)arg,
2257 &range, sizeof(range)))
2258 err = -EFAULT;
2259err_out:
2260 fdput(dst);
2261 return err;
2262}
2263
Jaegeuk Kime066b832017-04-13 15:17:00 -07002264static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2265{
2266 struct inode *inode = file_inode(filp);
2267 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2268 struct sit_info *sm = SIT_I(sbi);
2269 unsigned int start_segno = 0, end_segno = 0;
2270 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2271 struct f2fs_flush_device range;
2272 int ret;
2273
2274 if (!capable(CAP_SYS_ADMIN))
2275 return -EPERM;
2276
2277 if (f2fs_readonly(sbi->sb))
2278 return -EROFS;
2279
2280 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2281 sizeof(range)))
2282 return -EFAULT;
2283
2284 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2285 sbi->segs_per_sec != 1) {
2286 f2fs_msg(sbi->sb, KERN_WARNING,
2287 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2288 range.dev_num, sbi->s_ndevs,
2289 sbi->segs_per_sec);
2290 return -EINVAL;
2291 }
2292
2293 ret = mnt_want_write_file(filp);
2294 if (ret)
2295 return ret;
2296
2297 if (range.dev_num != 0)
2298 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2299 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2300
2301 start_segno = sm->last_victim[FLUSH_DEVICE];
2302 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2303 start_segno = dev_start_segno;
2304 end_segno = min(start_segno + range.segments, dev_end_segno);
2305
2306 while (start_segno < end_segno) {
2307 if (!mutex_trylock(&sbi->gc_mutex)) {
2308 ret = -EBUSY;
2309 goto out;
2310 }
2311 sm->last_victim[GC_CB] = end_segno + 1;
2312 sm->last_victim[GC_GREEDY] = end_segno + 1;
2313 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2314 ret = f2fs_gc(sbi, true, true, start_segno);
2315 if (ret == -EAGAIN)
2316 ret = 0;
2317 else if (ret < 0)
2318 break;
2319 start_segno++;
2320 }
2321out:
2322 mnt_drop_write_file(filp);
2323 return ret;
2324}
2325
2326
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002327long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2328{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002329 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002330 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002331 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002332 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002333 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002334 case F2FS_IOC_GETVERSION:
2335 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002336 case F2FS_IOC_START_ATOMIC_WRITE:
2337 return f2fs_ioc_start_atomic_write(filp);
2338 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2339 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002340 case F2FS_IOC_START_VOLATILE_WRITE:
2341 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002342 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2343 return f2fs_ioc_release_volatile_write(filp);
2344 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2345 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002346 case F2FS_IOC_SHUTDOWN:
2347 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002348 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002349 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002350 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2351 return f2fs_ioc_set_encryption_policy(filp, arg);
2352 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2353 return f2fs_ioc_get_encryption_policy(filp, arg);
2354 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2355 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002356 case F2FS_IOC_GARBAGE_COLLECT:
2357 return f2fs_ioc_gc(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002358 case F2FS_IOC_WRITE_CHECKPOINT:
2359 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002360 case F2FS_IOC_DEFRAGMENT:
2361 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002362 case F2FS_IOC_MOVE_RANGE:
2363 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002364 case F2FS_IOC_FLUSH_DEVICE:
2365 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002366 default:
2367 return -ENOTTY;
2368 }
2369}
2370
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002371static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2372{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002373 struct file *file = iocb->ki_filp;
2374 struct inode *inode = file_inode(file);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002375 struct blk_plug plug;
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002376 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002377
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002378 inode_lock(inode);
2379 ret = generic_write_checks(iocb, from);
2380 if (ret > 0) {
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002381 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002382
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002383 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2384 set_inode_flag(inode, FI_NO_PREALLOC);
2385
2386 err = f2fs_preallocate_blocks(iocb, from);
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002387 if (err) {
2388 inode_unlock(inode);
2389 return err;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002390 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08002391 blk_start_plug(&plug);
2392 ret = __generic_file_write_iter(iocb, from);
2393 blk_finish_plug(&plug);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002394 clear_inode_flag(inode, FI_NO_PREALLOC);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002395 }
2396 inode_unlock(inode);
2397
Christoph Hellwige2592212016-04-07 08:52:01 -07002398 if (ret > 0)
2399 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002400 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002401}
2402
Namjae Jeone9750822013-02-04 23:41:41 +09002403#ifdef CONFIG_COMPAT
2404long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2405{
2406 switch (cmd) {
2407 case F2FS_IOC32_GETFLAGS:
2408 cmd = F2FS_IOC_GETFLAGS;
2409 break;
2410 case F2FS_IOC32_SETFLAGS:
2411 cmd = F2FS_IOC_SETFLAGS;
2412 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08002413 case F2FS_IOC32_GETVERSION:
2414 cmd = F2FS_IOC_GETVERSION;
2415 break;
2416 case F2FS_IOC_START_ATOMIC_WRITE:
2417 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2418 case F2FS_IOC_START_VOLATILE_WRITE:
2419 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2420 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2421 case F2FS_IOC_SHUTDOWN:
2422 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2423 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2424 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2425 case F2FS_IOC_GARBAGE_COLLECT:
2426 case F2FS_IOC_WRITE_CHECKPOINT:
2427 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002428 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07002429 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002430 break;
Namjae Jeone9750822013-02-04 23:41:41 +09002431 default:
2432 return -ENOIOCTLCMD;
2433 }
2434 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2435}
2436#endif
2437
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002438const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08002439 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04002440 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002441 .write_iter = f2fs_file_write_iter,
2442 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08002443 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002444 .mmap = f2fs_file_mmap,
2445 .fsync = f2fs_sync_file,
2446 .fallocate = f2fs_fallocate,
2447 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09002448#ifdef CONFIG_COMPAT
2449 .compat_ioctl = f2fs_compat_ioctl,
2450#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002451 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002452 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002453};