blob: ff06827aa3695662655756edf37f393306b075eb [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 Kimf424f662015-04-20 15:19:06 -070023#include <linux/random.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090024
25#include "f2fs.h"
26#include "node.h"
27#include "segment.h"
28#include "xattr.h"
29#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080030#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080031#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090032#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090033
34static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
35 struct vm_fault *vmf)
36{
37 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050038 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070039 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090040 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080041 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090042
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090043 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070044
45 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070046
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090047 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080048 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070049 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070050 err = f2fs_reserve_block(&dn, page->index);
51 if (err) {
52 f2fs_unlock_op(sbi);
53 goto out;
54 }
55 f2fs_put_dnode(&dn);
56 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090057
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -080058 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -080059
Namjae Jeon9851e6e2013-04-28 09:04:18 +090060 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090061 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090062 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090063 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090064 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090065 unlock_page(page);
66 err = -EFAULT;
67 goto out;
68 }
69
70 /*
71 * check to see if the page is mapped already (no holes)
72 */
73 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090074 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090075
76 /* page is wholly or partially inside EOF */
Chao Yu9edcdab2015-09-11 14:43:52 +080077 if (((loff_t)(page->index + 1) << PAGE_CACHE_SHIFT) >
78 i_size_read(inode)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090079 unsigned offset;
80 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
81 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
82 }
83 set_page_dirty(page);
84 SetPageUptodate(page);
85
Jaegeuk Kime943a102013-10-25 14:26:31 +090086 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090087mapped:
88 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090089 f2fs_wait_on_page_writeback(page, DATA);
Chao Yu08b39fb2015-10-08 13:27:34 +080090
91 /* wait for GCed encrypted page writeback */
92 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
93 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
94
Chao Yu5b339122015-07-13 17:43:19 +080095 /* if gced page is attached, don't write to cold segment */
96 clear_cold_data(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090097out:
98 sb_end_pagefault(inode->i_sb);
99 return block_page_mkwrite_return(err);
100}
101
102static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900103 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700104 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900105 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900106};
107
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900108static int get_parent_ino(struct inode *inode, nid_t *pino)
109{
110 struct dentry *dentry;
111
112 inode = igrab(inode);
113 dentry = d_find_any_alias(inode);
114 iput(inode);
115 if (!dentry)
116 return 0;
117
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700118 if (update_dent_inode(inode, inode, &dentry->d_name)) {
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900119 dput(dentry);
120 return 0;
121 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900122
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900123 *pino = parent_ino(dentry);
124 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900125 return 1;
126}
127
Chao Yu9d1589e2014-08-20 18:37:35 +0800128static inline bool need_do_checkpoint(struct inode *inode)
129{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700130 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800131 bool need_cp = false;
132
133 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
134 need_cp = true;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700135 else if (file_enc_name(inode) && need_dentry_mark(sbi, inode->i_ino))
136 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800137 else if (file_wrong_pino(inode))
138 need_cp = true;
139 else if (!space_for_roll_forward(sbi))
140 need_cp = true;
141 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
142 need_cp = true;
143 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
144 need_cp = true;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700145 else if (test_opt(sbi, FASTBOOT))
146 need_cp = true;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800147 else if (sbi->active_logs == 2)
148 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800149
150 return need_cp;
151}
152
Changman Lee9c7bb702014-12-08 15:29:40 +0900153static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
154{
155 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
156 bool ret = false;
157 /* But we need to avoid that there are some inode updates */
158 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
159 ret = true;
160 f2fs_put_page(i, 0);
161 return ret;
162}
163
Changman Lee51455b12014-12-08 15:29:41 +0900164static void try_to_fix_pino(struct inode *inode)
165{
166 struct f2fs_inode_info *fi = F2FS_I(inode);
167 nid_t pino;
168
169 down_write(&fi->i_sem);
170 fi->xattr_ver = 0;
171 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
172 get_parent_ino(inode, &pino)) {
173 fi->i_pino = pino;
174 file_got_pino(inode);
175 up_write(&fi->i_sem);
176
177 mark_inode_dirty_sync(inode);
178 f2fs_write_inode(inode, NULL);
179 } else {
180 up_write(&fi->i_sem);
181 }
182}
183
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900184int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
185{
186 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900187 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700188 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700189 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900190 int ret = 0;
191 bool need_cp = false;
192 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900193 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900194 .nr_to_write = LONG_MAX,
195 .for_reclaim = 0,
196 };
197
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900198 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900199 return 0;
200
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900201 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700202
203 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800204 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700205 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900206 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700207 clear_inode_flag(fi, FI_NEED_IPU);
208
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900209 if (ret) {
210 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900211 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900212 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900213
Changman Lee9c7bb702014-12-08 15:29:40 +0900214 /* if the inode is dirty, let's recover all the time */
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700215 if (!datasync) {
216 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900217 goto go_write;
218 }
219
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700220 /*
221 * if there is no written data, don't waste time to write recovery info.
222 */
223 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700224 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700225
Changman Lee9c7bb702014-12-08 15:29:40 +0900226 /* it may call write_inode just prior to fsync */
227 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700228 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700229
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700230 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700231 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700232 goto flush_out;
233 goto out;
234 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700235go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900236 /*
237 * Both of fdatasync() and fsync() are able to be recovered from
238 * sudden-power-off.
239 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800240 down_read(&fi->i_sem);
241 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900242 up_read(&fi->i_sem);
243
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900244 if (need_cp) {
245 /* all the dirty node pages should be flushed for POR */
246 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900247
Changman Lee51455b12014-12-08 15:29:41 +0900248 /*
249 * We've secured consistency through sync_fs. Following pino
250 * will be used only for fsynced inodes after checkpoint.
251 */
252 try_to_fix_pino(inode);
Jaegeuk Kimcff28522015-03-02 10:48:56 -0800253 clear_inode_flag(fi, FI_APPEND_WRITE);
254 clear_inode_flag(fi, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900255 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900256 }
Changman Lee51455b12014-12-08 15:29:41 +0900257sync_nodes:
258 sync_node_pages(sbi, ino, &wbc);
259
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800260 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800261 if (unlikely(f2fs_cp_error(sbi))) {
262 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800263 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800264 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800265
Changman Lee51455b12014-12-08 15:29:41 +0900266 if (need_inode_block_update(sbi, ino)) {
267 mark_inode_dirty_sync(inode);
268 f2fs_write_inode(inode, NULL);
269 goto sync_nodes;
270 }
271
272 ret = wait_on_node_pages_writeback(sbi, ino);
273 if (ret)
274 goto out;
275
276 /* once recovery info is written, don't need to tack this */
Chao Yua49324f2015-12-15 13:29:47 +0800277 remove_ino_entry(sbi, ino, APPEND_INO);
Changman Lee51455b12014-12-08 15:29:41 +0900278 clear_inode_flag(fi, FI_APPEND_WRITE);
279flush_out:
Chao Yua49324f2015-12-15 13:29:47 +0800280 remove_ino_entry(sbi, ino, UPDATE_INO);
Changman Lee51455b12014-12-08 15:29:41 +0900281 clear_inode_flag(fi, FI_UPDATE_WRITE);
282 ret = f2fs_issue_flush(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900283out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900284 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700285 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900286 return ret;
287}
288
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900289static pgoff_t __get_first_dirty_index(struct address_space *mapping,
290 pgoff_t pgofs, int whence)
291{
292 struct pagevec pvec;
293 int nr_pages;
294
295 if (whence != SEEK_DATA)
296 return 0;
297
298 /* find first dirty page index */
299 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700300 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
301 PAGECACHE_TAG_DIRTY, 1);
302 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900303 pagevec_release(&pvec);
304 return pgofs;
305}
306
307static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
308 int whence)
309{
310 switch (whence) {
311 case SEEK_DATA:
312 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
313 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
314 return true;
315 break;
316 case SEEK_HOLE:
317 if (blkaddr == NULL_ADDR)
318 return true;
319 break;
320 }
321 return false;
322}
323
Chao Yu267378d2014-04-23 14:10:24 +0800324static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
325{
326 struct inode *inode = file->f_mapping->host;
327 loff_t maxbytes = inode->i_sb->s_maxbytes;
328 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900329 pgoff_t pgofs, end_offset, dirty;
330 loff_t data_ofs = offset;
331 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800332 int err = 0;
333
334 mutex_lock(&inode->i_mutex);
335
336 isize = i_size_read(inode);
337 if (offset >= isize)
338 goto fail;
339
340 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800341 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800342 if (whence == SEEK_HOLE)
343 data_ofs = isize;
344 goto found;
345 }
346
347 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
348
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900349 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
350
Chao Yu9edcdab2015-09-11 14:43:52 +0800351 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800352 set_new_dnode(&dn, inode, NULL, NULL, 0);
353 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
354 if (err && err != -ENOENT) {
355 goto fail;
356 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900357 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800358 if (whence == SEEK_DATA) {
359 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
360 F2FS_I(inode));
361 continue;
362 } else {
363 goto found;
364 }
365 }
366
Chao Yu6b2920a2014-07-07 11:21:59 +0800367 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800368
369 /* find data/hole in dnode block */
370 for (; dn.ofs_in_node < end_offset;
371 dn.ofs_in_node++, pgofs++,
Chao Yu2e023172015-02-09 11:23:58 +0800372 data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800373 block_t blkaddr;
374 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
375
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900376 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800377 f2fs_put_dnode(&dn);
378 goto found;
379 }
380 }
381 f2fs_put_dnode(&dn);
382 }
383
384 if (whence == SEEK_DATA)
385 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800386found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900387 if (whence == SEEK_HOLE && data_ofs > isize)
388 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800389 mutex_unlock(&inode->i_mutex);
390 return vfs_setpos(file, data_ofs, maxbytes);
391fail:
392 mutex_unlock(&inode->i_mutex);
393 return -ENXIO;
394}
395
396static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
397{
398 struct inode *inode = file->f_mapping->host;
399 loff_t maxbytes = inode->i_sb->s_maxbytes;
400
401 switch (whence) {
402 case SEEK_SET:
403 case SEEK_CUR:
404 case SEEK_END:
405 return generic_file_llseek_size(file, offset, whence,
406 maxbytes, i_size_read(inode));
407 case SEEK_DATA:
408 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700409 if (offset < 0)
410 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800411 return f2fs_seek_block(file, offset, whence);
412 }
413
414 return -EINVAL;
415}
416
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900417static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
418{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700419 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800420 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700421
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700422 if (f2fs_encrypted_inode(inode)) {
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800423 err = f2fs_get_encryption_info(inode);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700424 if (err)
425 return 0;
426 }
427
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700428 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800429 err = f2fs_convert_inline_inode(inode);
430 if (err)
431 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700432
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900433 file_accessed(file);
434 vma->vm_ops = &f2fs_file_vm_ops;
435 return 0;
436}
437
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700438static int f2fs_file_open(struct inode *inode, struct file *filp)
439{
440 int ret = generic_file_open(inode, filp);
441
442 if (!ret && f2fs_encrypted_inode(inode)) {
443 ret = f2fs_get_encryption_info(inode);
444 if (ret)
445 ret = -EACCES;
446 }
447 return ret;
448}
449
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900450int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900451{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700452 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900453 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800454 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900455 __le32 *addr;
456
Gu Zheng45590712013-07-15 17:57:38 +0800457 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900458 addr = blkaddr_in_node(raw_node) + ofs;
459
Chris Fries6c311ec2014-01-17 14:44:39 -0600460 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900461 block_t blkaddr = le32_to_cpu(*addr);
462 if (blkaddr == NULL_ADDR)
463 continue;
464
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800465 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800466 set_data_blkaddr(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700468 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
469 clear_inode_flag(F2FS_I(dn->inode),
470 FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900471 nr_free++;
472 }
Chao Yu19b2c302015-08-26 20:34:48 +0800473
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900474 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800475 pgoff_t fofs;
476 /*
477 * once we invalidate valid blkaddr in range [ofs, ofs + count],
478 * we will invalidate all blkaddr in the whole range.
479 */
480 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
481 F2FS_I(dn->inode)) + ofs;
482 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900483 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900484 sync_inode_page(dn);
485 }
486 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900487
488 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
489 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900490 return nr_free;
491}
492
493void truncate_data_blocks(struct dnode_of_data *dn)
494{
495 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
496}
497
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800498static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700499 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900500{
501 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700502 pgoff_t index = from >> PAGE_CACHE_SHIFT;
503 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900504 struct page *page;
505
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700506 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700507 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900508
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700509 if (cache_only) {
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700510 page = f2fs_grab_cache_page(mapping, index, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700511 if (page && PageUptodate(page))
512 goto truncate_out;
513 f2fs_put_page(page, 1);
514 return 0;
515 }
516
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700517 page = get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900518 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700519 return 0;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700520truncate_out:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900521 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700523 if (!cache_only || !f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800524 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900525 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700526 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900527}
528
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700529int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900530{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700531 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 unsigned int blocksize = inode->i_sb->s_blocksize;
533 struct dnode_of_data dn;
534 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800535 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700536 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800537 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900538
Namjae Jeon51dd6242013-04-20 01:28:52 +0900539 trace_f2fs_truncate_blocks_enter(inode, from);
540
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800541 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900542
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700543 if (lock)
544 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800545
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700546 ipage = get_node_page(sbi, inode->i_ino);
547 if (IS_ERR(ipage)) {
548 err = PTR_ERR(ipage);
549 goto out;
550 }
551
552 if (f2fs_has_inline_data(inode)) {
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800553 if (truncate_inline_inode(ipage, from))
554 set_page_dirty(ipage);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700555 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800556 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700557 goto out;
558 }
559
560 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900561 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900562 if (err) {
563 if (err == -ENOENT)
564 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700565 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700566 }
567
Chao Yu6403eb12014-04-26 19:59:52 +0800568 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900569
570 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700571 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900572
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900573 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
574 truncate_data_blocks_range(&dn, count);
575 free_from += count;
576 }
577
578 f2fs_put_dnode(&dn);
579free_next:
580 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800581out:
582 if (lock)
583 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700584
585 /* lastly zero out the first data page */
586 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800587 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900588
Namjae Jeon51dd6242013-04-20 01:28:52 +0900589 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900590 return err;
591}
592
Chao Yub0154892015-08-24 17:39:42 +0800593int f2fs_truncate(struct inode *inode, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900594{
Chao Yub0154892015-08-24 17:39:42 +0800595 int err;
596
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900597 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
598 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800599 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900600
Namjae Jeon51dd6242013-04-20 01:28:52 +0900601 trace_f2fs_truncate(inode);
602
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800603 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800604 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800605 err = f2fs_convert_inline_inode(inode);
606 if (err)
607 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800608 }
609
Chao Yub0154892015-08-24 17:39:42 +0800610 err = truncate_blocks(inode, i_size_read(inode), lock);
611 if (err)
612 return err;
613
614 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
615 mark_inode_dirty(inode);
616 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900617}
618
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900619int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620 struct dentry *dentry, struct kstat *stat)
621{
David Howells2b0143b2015-03-17 22:25:59 +0000622 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900623 generic_fillattr(inode, stat);
624 stat->blocks <<= 3;
625 return 0;
626}
627
628#ifdef CONFIG_F2FS_FS_POSIX_ACL
629static void __setattr_copy(struct inode *inode, const struct iattr *attr)
630{
631 struct f2fs_inode_info *fi = F2FS_I(inode);
632 unsigned int ia_valid = attr->ia_valid;
633
634 if (ia_valid & ATTR_UID)
635 inode->i_uid = attr->ia_uid;
636 if (ia_valid & ATTR_GID)
637 inode->i_gid = attr->ia_gid;
638 if (ia_valid & ATTR_ATIME)
639 inode->i_atime = timespec_trunc(attr->ia_atime,
640 inode->i_sb->s_time_gran);
641 if (ia_valid & ATTR_MTIME)
642 inode->i_mtime = timespec_trunc(attr->ia_mtime,
643 inode->i_sb->s_time_gran);
644 if (ia_valid & ATTR_CTIME)
645 inode->i_ctime = timespec_trunc(attr->ia_ctime,
646 inode->i_sb->s_time_gran);
647 if (ia_valid & ATTR_MODE) {
648 umode_t mode = attr->ia_mode;
649
650 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
651 mode &= ~S_ISGID;
652 set_acl_inode(fi, mode);
653 }
654}
655#else
656#define __setattr_copy setattr_copy
657#endif
658
659int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
660{
David Howells2b0143b2015-03-17 22:25:59 +0000661 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900662 struct f2fs_inode_info *fi = F2FS_I(inode);
663 int err;
664
665 err = inode_change_ok(inode, attr);
666 if (err)
667 return err;
668
Chao Yu09db6a22014-09-15 18:02:09 +0800669 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700670 if (f2fs_encrypted_inode(inode) &&
671 f2fs_get_encryption_info(inode))
672 return -EACCES;
673
Chao Yu3c454142015-06-05 18:34:02 +0800674 if (attr->ia_size <= i_size_read(inode)) {
Chao Yu09db6a22014-09-15 18:02:09 +0800675 truncate_setsize(inode, attr->ia_size);
Chao Yub0154892015-08-24 17:39:42 +0800676 err = f2fs_truncate(inode, true);
677 if (err)
678 return err;
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800679 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu09db6a22014-09-15 18:02:09 +0800680 } else {
681 /*
Chao Yu3c454142015-06-05 18:34:02 +0800682 * do not trim all blocks after i_size if target size is
683 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800684 */
Chao Yu3c454142015-06-05 18:34:02 +0800685 truncate_setsize(inode, attr->ia_size);
Chao Yu0cab80e2015-12-01 11:36:16 +0800686
687 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800688 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800689 err = f2fs_convert_inline_inode(inode);
690 if (err)
691 return err;
692 }
Chao Yu345a6b22015-09-28 17:46:01 +0800693 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Chao Yu09db6a22014-09-15 18:02:09 +0800694 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900695 }
696
697 __setattr_copy(inode, attr);
698
699 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800700 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900701 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
702 inode->i_mode = fi->i_acl_mode;
703 clear_inode_flag(fi, FI_ACL_MODE);
704 }
705 }
706
707 mark_inode_dirty(inode);
708 return err;
709}
710
711const struct inode_operations f2fs_file_inode_operations = {
712 .getattr = f2fs_getattr,
713 .setattr = f2fs_setattr,
714 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800715 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900716#ifdef CONFIG_F2FS_FS_XATTR
717 .setxattr = generic_setxattr,
718 .getxattr = generic_getxattr,
719 .listxattr = f2fs_listxattr,
720 .removexattr = generic_removexattr,
721#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900722 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900723};
724
Chao Yu63943282015-08-07 18:36:06 +0800725static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726 loff_t start, loff_t len)
727{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700728 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900729 struct page *page;
730
731 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800732 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900733
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800734 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900735
Gu Zhenge4795562013-09-27 18:08:30 +0800736 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900737 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800738 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900739
Chao Yu63943282015-08-07 18:36:06 +0800740 if (IS_ERR(page))
741 return PTR_ERR(page);
742
743 f2fs_wait_on_page_writeback(page, DATA);
744 zero_user(page, start, len);
745 set_page_dirty(page);
746 f2fs_put_page(page, 1);
747 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900748}
749
750int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
751{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900752 int err;
753
Chao Yuea587112015-09-17 20:22:44 +0800754 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900755 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800756 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900757
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900758 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yuea587112015-09-17 20:22:44 +0800759 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900760 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800761 if (err == -ENOENT) {
762 pg_start++;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900763 continue;
Chao Yuea587112015-09-17 20:22:44 +0800764 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900765 return err;
766 }
767
Chao Yuea587112015-09-17 20:22:44 +0800768 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
769 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
770
771 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
772
773 truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900774 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800775
776 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900777 }
778 return 0;
779}
780
Chao Yua66c7b22013-11-22 16:52:50 +0800781static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900782{
783 pgoff_t pg_start, pg_end;
784 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800785 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900786
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800787 ret = f2fs_convert_inline_inode(inode);
788 if (ret)
789 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800790
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900791 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
792 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
793
794 off_start = offset & (PAGE_CACHE_SIZE - 1);
795 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
796
797 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800798 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900799 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800800 if (ret)
801 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900802 } else {
Chao Yu63943282015-08-07 18:36:06 +0800803 if (off_start) {
804 ret = fill_zero(inode, pg_start++, off_start,
805 PAGE_CACHE_SIZE - off_start);
806 if (ret)
807 return ret;
808 }
809 if (off_end) {
810 ret = fill_zero(inode, pg_end, 0, off_end);
811 if (ret)
812 return ret;
813 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900814
815 if (pg_start < pg_end) {
816 struct address_space *mapping = inode->i_mapping;
817 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700818 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500819
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800820 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900821
Chao Yu9edcdab2015-09-11 14:43:52 +0800822 blk_start = (loff_t)pg_start << PAGE_CACHE_SHIFT;
823 blk_end = (loff_t)pg_end << PAGE_CACHE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900824 truncate_inode_pages_range(mapping, blk_start,
825 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900826
Gu Zhenge4795562013-09-27 18:08:30 +0800827 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900828 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800829 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900830 }
831 }
832
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900833 return ret;
834}
835
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700836static int __exchange_data_block(struct inode *inode, pgoff_t src,
837 pgoff_t dst, bool full)
Chao Yub4ace332015-05-06 13:09:46 +0800838{
839 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
840 struct dnode_of_data dn;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700841 block_t new_addr;
842 bool do_replace = false;
843 int ret;
844
845 set_new_dnode(&dn, inode, NULL, NULL, 0);
846 ret = get_dnode_of_data(&dn, src, LOOKUP_NODE_RA);
847 if (ret && ret != -ENOENT) {
848 return ret;
849 } else if (ret == -ENOENT) {
850 new_addr = NULL_ADDR;
851 } else {
852 new_addr = dn.data_blkaddr;
853 if (!is_checkpointed_data(sbi, new_addr)) {
854 dn.data_blkaddr = NULL_ADDR;
855 /* do not invalidate this block address */
856 set_data_blkaddr(&dn);
857 f2fs_update_extent_cache(&dn);
858 do_replace = true;
859 }
860 f2fs_put_dnode(&dn);
861 }
862
863 if (new_addr == NULL_ADDR)
864 return full ? truncate_hole(inode, dst, dst + 1) : 0;
865
866 if (do_replace) {
867 struct page *ipage = get_node_page(sbi, inode->i_ino);
868 struct node_info ni;
869
870 if (IS_ERR(ipage)) {
871 ret = PTR_ERR(ipage);
872 goto err_out;
873 }
874
875 set_new_dnode(&dn, inode, ipage, NULL, 0);
876 ret = f2fs_reserve_block(&dn, dst);
877 if (ret)
878 goto err_out;
879
880 truncate_data_blocks_range(&dn, 1);
881
882 get_node_info(sbi, dn.nid, &ni);
883 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr,
884 ni.version, true);
885 f2fs_put_dnode(&dn);
886 } else {
887 struct page *psrc, *pdst;
888
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700889 psrc = get_lock_data_page(inode, src, true);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700890 if (IS_ERR(psrc))
891 return PTR_ERR(psrc);
892 pdst = get_new_data_page(inode, NULL, dst, false);
893 if (IS_ERR(pdst)) {
894 f2fs_put_page(psrc, 1);
895 return PTR_ERR(pdst);
896 }
897 f2fs_copy_page(psrc, pdst);
898 set_page_dirty(pdst);
899 f2fs_put_page(pdst, 1);
900 f2fs_put_page(psrc, 1);
901
902 return truncate_hole(inode, src, src + 1);
903 }
904 return 0;
905
906err_out:
907 if (!get_dnode_of_data(&dn, src, LOOKUP_NODE)) {
908 dn.data_blkaddr = new_addr;
909 set_data_blkaddr(&dn);
910 f2fs_update_extent_cache(&dn);
911 f2fs_put_dnode(&dn);
912 }
913 return ret;
914}
915
916static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
917{
918 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +0800919 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
920 int ret = 0;
921
Chao Yub4ace332015-05-06 13:09:46 +0800922 for (; end < nrpages; start++, end++) {
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800923 f2fs_balance_fs(sbi, true);
Chao Yuecbaa402015-07-16 18:18:11 +0800924 f2fs_lock_op(sbi);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700925 ret = __exchange_data_block(inode, end, start, true);
Chao Yuecbaa402015-07-16 18:18:11 +0800926 f2fs_unlock_op(sbi);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700927 if (ret)
928 break;
Chao Yub4ace332015-05-06 13:09:46 +0800929 }
Chao Yub4ace332015-05-06 13:09:46 +0800930 return ret;
931}
932
933static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
934{
935 pgoff_t pg_start, pg_end;
936 loff_t new_size;
937 int ret;
938
Chao Yub4ace332015-05-06 13:09:46 +0800939 if (offset + len >= i_size_read(inode))
940 return -EINVAL;
941
942 /* collapse range should be aligned to block size of f2fs. */
943 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
944 return -EINVAL;
945
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800946 ret = f2fs_convert_inline_inode(inode);
947 if (ret)
948 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -0700949
Chao Yub4ace332015-05-06 13:09:46 +0800950 pg_start = offset >> PAGE_CACHE_SHIFT;
951 pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
952
953 /* write out all dirty pages from offset */
954 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
955 if (ret)
956 return ret;
957
958 truncate_pagecache(inode, offset);
959
960 ret = f2fs_do_collapse(inode, pg_start, pg_end);
961 if (ret)
962 return ret;
963
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700964 /* write out all moved pages, if possible */
965 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
966 truncate_pagecache(inode, offset);
967
Chao Yub4ace332015-05-06 13:09:46 +0800968 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700969 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +0800970
971 ret = truncate_blocks(inode, new_size, true);
972 if (!ret)
973 i_size_write(inode, new_size);
974
975 return ret;
976}
977
Chao Yu75cd4e02015-05-06 13:11:13 +0800978static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
979 int mode)
980{
981 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
982 struct address_space *mapping = inode->i_mapping;
983 pgoff_t index, pg_start, pg_end;
984 loff_t new_size = i_size_read(inode);
985 loff_t off_start, off_end;
986 int ret = 0;
987
Chao Yu75cd4e02015-05-06 13:11:13 +0800988 ret = inode_newsize_ok(inode, (len + offset));
989 if (ret)
990 return ret;
991
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800992 ret = f2fs_convert_inline_inode(inode);
993 if (ret)
994 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +0800995
996 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
997 if (ret)
998 return ret;
999
1000 truncate_pagecache_range(inode, offset, offset + len - 1);
1001
1002 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
1003 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
1004
1005 off_start = offset & (PAGE_CACHE_SIZE - 1);
1006 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
1007
1008 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001009 ret = fill_zero(inode, pg_start, off_start,
1010 off_end - off_start);
1011 if (ret)
1012 return ret;
1013
Chao Yu75cd4e02015-05-06 13:11:13 +08001014 if (offset + len > new_size)
1015 new_size = offset + len;
1016 new_size = max_t(loff_t, new_size, offset + len);
1017 } else {
1018 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001019 ret = fill_zero(inode, pg_start++, off_start,
1020 PAGE_CACHE_SIZE - off_start);
1021 if (ret)
1022 return ret;
1023
Chao Yu75cd4e02015-05-06 13:11:13 +08001024 new_size = max_t(loff_t, new_size,
Chao Yu9edcdab2015-09-11 14:43:52 +08001025 (loff_t)pg_start << PAGE_CACHE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001026 }
1027
1028 for (index = pg_start; index < pg_end; index++) {
1029 struct dnode_of_data dn;
1030 struct page *ipage;
1031
1032 f2fs_lock_op(sbi);
1033
1034 ipage = get_node_page(sbi, inode->i_ino);
1035 if (IS_ERR(ipage)) {
1036 ret = PTR_ERR(ipage);
1037 f2fs_unlock_op(sbi);
1038 goto out;
1039 }
1040
1041 set_new_dnode(&dn, inode, ipage, NULL, 0);
1042 ret = f2fs_reserve_block(&dn, index);
1043 if (ret) {
1044 f2fs_unlock_op(sbi);
1045 goto out;
1046 }
1047
1048 if (dn.data_blkaddr != NEW_ADDR) {
1049 invalidate_blocks(sbi, dn.data_blkaddr);
1050
1051 dn.data_blkaddr = NEW_ADDR;
1052 set_data_blkaddr(&dn);
1053
1054 dn.data_blkaddr = NULL_ADDR;
1055 f2fs_update_extent_cache(&dn);
1056 }
1057 f2fs_put_dnode(&dn);
1058 f2fs_unlock_op(sbi);
1059
1060 new_size = max_t(loff_t, new_size,
Chao Yu9edcdab2015-09-11 14:43:52 +08001061 (loff_t)(index + 1) << PAGE_CACHE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001062 }
1063
1064 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001065 ret = fill_zero(inode, pg_end, 0, off_end);
1066 if (ret)
1067 goto out;
1068
Chao Yu75cd4e02015-05-06 13:11:13 +08001069 new_size = max_t(loff_t, new_size, offset + len);
1070 }
1071 }
1072
1073out:
1074 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) {
1075 i_size_write(inode, new_size);
1076 mark_inode_dirty(inode);
1077 update_inode_page(inode);
1078 }
1079
1080 return ret;
1081}
1082
Chao Yuf62185d2015-05-28 19:16:57 +08001083static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1084{
1085 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1086 pgoff_t pg_start, pg_end, delta, nrpages, idx;
1087 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001088 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001089
Chao Yuf62185d2015-05-28 19:16:57 +08001090 new_size = i_size_read(inode) + len;
1091 if (new_size > inode->i_sb->s_maxbytes)
1092 return -EFBIG;
1093
1094 if (offset >= i_size_read(inode))
1095 return -EINVAL;
1096
1097 /* insert range should be aligned to block size of f2fs. */
1098 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1099 return -EINVAL;
1100
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001101 ret = f2fs_convert_inline_inode(inode);
1102 if (ret)
1103 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001104
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001105 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001106
Chao Yuf62185d2015-05-28 19:16:57 +08001107 ret = truncate_blocks(inode, i_size_read(inode), true);
1108 if (ret)
1109 return ret;
1110
1111 /* write out all dirty pages from offset */
1112 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1113 if (ret)
1114 return ret;
1115
1116 truncate_pagecache(inode, offset);
1117
1118 pg_start = offset >> PAGE_CACHE_SHIFT;
1119 pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
1120 delta = pg_end - pg_start;
1121 nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1122
1123 for (idx = nrpages - 1; idx >= pg_start && idx != -1; idx--) {
Chao Yuf62185d2015-05-28 19:16:57 +08001124 f2fs_lock_op(sbi);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001125 ret = __exchange_data_block(inode, idx, idx + delta, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001126 f2fs_unlock_op(sbi);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001127 if (ret)
1128 break;
Chao Yuf62185d2015-05-28 19:16:57 +08001129 }
1130
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001131 /* write out all moved pages, if possible */
1132 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1133 truncate_pagecache(inode, offset);
1134
1135 if (!ret)
1136 i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001137 return ret;
1138}
1139
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001140static int expand_inode_data(struct inode *inode, loff_t offset,
1141 loff_t len, int mode)
1142{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001143 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001144 pgoff_t index, pg_start, pg_end;
1145 loff_t new_size = i_size_read(inode);
1146 loff_t off_start, off_end;
1147 int ret = 0;
1148
1149 ret = inode_newsize_ok(inode, (len + offset));
1150 if (ret)
1151 return ret;
1152
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001153 ret = f2fs_convert_inline_inode(inode);
1154 if (ret)
1155 return ret;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001156
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001157 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001158
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001159 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
1160 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
1161
1162 off_start = offset & (PAGE_CACHE_SIZE - 1);
1163 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
1164
Jaegeuk Kimead43272014-06-13 13:05:55 +09001165 f2fs_lock_op(sbi);
1166
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001167 for (index = pg_start; index <= pg_end; index++) {
1168 struct dnode_of_data dn;
1169
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001170 if (index == pg_end && !off_end)
1171 goto noalloc;
1172
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001173 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001174 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +08001175 if (ret)
1176 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001177noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001178 if (pg_start == pg_end)
1179 new_size = offset + len;
1180 else if (index == pg_start && off_start)
Chao Yu9edcdab2015-09-11 14:43:52 +08001181 new_size = (loff_t)(index + 1) << PAGE_CACHE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001182 else if (index == pg_end)
Chao Yu9edcdab2015-09-11 14:43:52 +08001183 new_size = ((loff_t)index << PAGE_CACHE_SHIFT) +
1184 off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001185 else
1186 new_size += PAGE_CACHE_SIZE;
1187 }
1188
1189 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
1190 i_size_read(inode) < new_size) {
1191 i_size_write(inode, new_size);
1192 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +09001193 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001194 }
Jaegeuk Kimead43272014-06-13 13:05:55 +09001195 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001196
1197 return ret;
1198}
1199
1200static long f2fs_fallocate(struct file *file, int mode,
1201 loff_t offset, loff_t len)
1202{
Al Viro6131ffa2013-02-27 16:59:05 -05001203 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001204 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001205
Chao Yuc9980122015-09-11 14:39:02 +08001206 /* f2fs only support ->fallocate for regular file */
1207 if (!S_ISREG(inode->i_mode))
1208 return -EINVAL;
1209
Chao Yuf62185d2015-05-28 19:16:57 +08001210 if (f2fs_encrypted_inode(inode) &&
1211 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001212 return -EOPNOTSUPP;
1213
Chao Yub4ace332015-05-06 13:09:46 +08001214 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001215 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1216 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001217 return -EOPNOTSUPP;
1218
Chao Yu3375f692014-01-28 10:29:26 +08001219 mutex_lock(&inode->i_mutex);
1220
Taehee Yoo587c0a42015-04-21 15:59:12 +09001221 if (mode & FALLOC_FL_PUNCH_HOLE) {
1222 if (offset >= inode->i_size)
1223 goto out;
1224
Chao Yua66c7b22013-11-22 16:52:50 +08001225 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001226 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1227 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001228 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1229 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001230 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1231 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001232 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001233 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001234 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001235
Namjae Jeon3af60a42012-12-30 14:52:37 +09001236 if (!ret) {
1237 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1238 mark_inode_dirty(inode);
1239 }
Chao Yu3375f692014-01-28 10:29:26 +08001240
Taehee Yoo587c0a42015-04-21 15:59:12 +09001241out:
Chao Yu3375f692014-01-28 10:29:26 +08001242 mutex_unlock(&inode->i_mutex);
1243
Namjae Jeonc01e2852013-04-23 17:00:52 +09001244 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001245 return ret;
1246}
1247
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001248static int f2fs_release_file(struct inode *inode, struct file *filp)
1249{
1250 /* some remained atomic pages should discarded */
1251 if (f2fs_is_atomic_file(inode))
1252 commit_inmem_pages(inode, true);
1253 if (f2fs_is_volatile_file(inode)) {
1254 set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1255 filemap_fdatawrite(inode->i_mapping);
1256 clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1257 }
1258 return 0;
1259}
1260
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001261#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1262#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1263
1264static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1265{
1266 if (S_ISDIR(mode))
1267 return flags;
1268 else if (S_ISREG(mode))
1269 return flags & F2FS_REG_FLMASK;
1270 else
1271 return flags & F2FS_OTHER_FLMASK;
1272}
1273
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001274static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001275{
Al Viro6131ffa2013-02-27 16:59:05 -05001276 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001277 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001278 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1279 return put_user(flags, (int __user *)arg);
1280}
1281
1282static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1283{
1284 struct inode *inode = file_inode(filp);
1285 struct f2fs_inode_info *fi = F2FS_I(inode);
1286 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1287 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001288 int ret;
1289
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001290 ret = mnt_want_write_file(filp);
1291 if (ret)
1292 return ret;
1293
1294 if (!inode_owner_or_capable(inode)) {
1295 ret = -EACCES;
1296 goto out;
1297 }
1298
1299 if (get_user(flags, (int __user *)arg)) {
1300 ret = -EFAULT;
1301 goto out;
1302 }
1303
1304 flags = f2fs_mask_flags(inode->i_mode, flags);
1305
1306 mutex_lock(&inode->i_mutex);
1307
1308 oldflags = fi->i_flags;
1309
1310 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1311 if (!capable(CAP_LINUX_IMMUTABLE)) {
1312 mutex_unlock(&inode->i_mutex);
1313 ret = -EPERM;
1314 goto out;
1315 }
1316 }
1317
1318 flags = flags & FS_FL_USER_MODIFIABLE;
1319 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1320 fi->i_flags = flags;
1321 mutex_unlock(&inode->i_mutex);
1322
1323 f2fs_set_inode_flags(inode);
1324 inode->i_ctime = CURRENT_TIME;
1325 mark_inode_dirty(inode);
1326out:
1327 mnt_drop_write_file(filp);
1328 return ret;
1329}
1330
Chao Yud49f3e82015-01-23 20:36:04 +08001331static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1332{
1333 struct inode *inode = file_inode(filp);
1334
1335 return put_user(inode->i_generation, (int __user *)arg);
1336}
1337
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001338static int f2fs_ioc_start_atomic_write(struct file *filp)
1339{
1340 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001341 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001342
1343 if (!inode_owner_or_capable(inode))
1344 return -EACCES;
1345
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001346 if (f2fs_is_atomic_file(inode))
1347 return 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001348
Chao Yuf4c9c742015-07-17 18:06:35 +08001349 ret = f2fs_convert_inline_inode(inode);
1350 if (ret)
1351 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001352
Chao Yuf4c9c742015-07-17 18:06:35 +08001353 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1354 return 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001355}
1356
1357static int f2fs_ioc_commit_atomic_write(struct file *filp)
1358{
1359 struct inode *inode = file_inode(filp);
1360 int ret;
1361
1362 if (!inode_owner_or_capable(inode))
1363 return -EACCES;
1364
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001365 if (f2fs_is_volatile_file(inode))
1366 return 0;
1367
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001368 ret = mnt_want_write_file(filp);
1369 if (ret)
1370 return ret;
1371
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001372 if (f2fs_is_atomic_file(inode)) {
1373 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001374 ret = commit_inmem_pages(inode, false);
1375 if (ret)
1376 goto err_out;
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001377 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001378
Chao Yua5f64b62015-07-17 18:05:21 +08001379 ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001380err_out:
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001381 mnt_drop_write_file(filp);
1382 return ret;
1383}
1384
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001385static int f2fs_ioc_start_volatile_write(struct file *filp)
1386{
1387 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001388 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001389
1390 if (!inode_owner_or_capable(inode))
1391 return -EACCES;
1392
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001393 if (f2fs_is_volatile_file(inode))
1394 return 0;
1395
Chao Yuf4c9c742015-07-17 18:06:35 +08001396 ret = f2fs_convert_inline_inode(inode);
1397 if (ret)
1398 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001399
Chao Yuf4c9c742015-07-17 18:06:35 +08001400 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1401 return 0;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001402}
1403
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001404static int f2fs_ioc_release_volatile_write(struct file *filp)
1405{
1406 struct inode *inode = file_inode(filp);
1407
1408 if (!inode_owner_or_capable(inode))
1409 return -EACCES;
1410
1411 if (!f2fs_is_volatile_file(inode))
1412 return 0;
1413
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001414 if (!f2fs_is_first_block_written(inode))
1415 return truncate_partial_data_page(inode, 0, true);
1416
Nicholas Krause538e17e2015-09-06 08:28:46 -04001417 return punch_hole(inode, 0, F2FS_BLKSIZE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001418}
1419
1420static int f2fs_ioc_abort_volatile_write(struct file *filp)
1421{
1422 struct inode *inode = file_inode(filp);
1423 int ret;
1424
1425 if (!inode_owner_or_capable(inode))
1426 return -EACCES;
1427
1428 ret = mnt_want_write_file(filp);
1429 if (ret)
1430 return ret;
1431
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001432 if (f2fs_is_atomic_file(inode)) {
1433 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1434 commit_inmem_pages(inode, true);
1435 }
1436 if (f2fs_is_volatile_file(inode)) {
1437 clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1438 ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0);
1439 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001440
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001441 mnt_drop_write_file(filp);
1442 return ret;
1443}
1444
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001445static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1446{
1447 struct inode *inode = file_inode(filp);
1448 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1449 struct super_block *sb = sbi->sb;
1450 __u32 in;
1451
1452 if (!capable(CAP_SYS_ADMIN))
1453 return -EPERM;
1454
1455 if (get_user(in, (__u32 __user *)arg))
1456 return -EFAULT;
1457
1458 switch (in) {
1459 case F2FS_GOING_DOWN_FULLSYNC:
1460 sb = freeze_bdev(sb->s_bdev);
1461 if (sb && !IS_ERR(sb)) {
1462 f2fs_stop_checkpoint(sbi);
1463 thaw_bdev(sb->s_bdev, sb);
1464 }
1465 break;
1466 case F2FS_GOING_DOWN_METASYNC:
1467 /* do checkpoint only */
1468 f2fs_sync_fs(sb, 1);
1469 f2fs_stop_checkpoint(sbi);
1470 break;
1471 case F2FS_GOING_DOWN_NOSYNC:
1472 f2fs_stop_checkpoint(sbi);
1473 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001474 case F2FS_GOING_DOWN_METAFLUSH:
1475 sync_meta_pages(sbi, META, LONG_MAX);
1476 f2fs_stop_checkpoint(sbi);
1477 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001478 default:
1479 return -EINVAL;
1480 }
1481 return 0;
1482}
1483
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001484static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1485{
1486 struct inode *inode = file_inode(filp);
1487 struct super_block *sb = inode->i_sb;
1488 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1489 struct fstrim_range range;
1490 int ret;
1491
1492 if (!capable(CAP_SYS_ADMIN))
1493 return -EPERM;
1494
1495 if (!blk_queue_discard(q))
1496 return -EOPNOTSUPP;
1497
1498 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1499 sizeof(range)))
1500 return -EFAULT;
1501
1502 range.minlen = max((unsigned int)range.minlen,
1503 q->limits.discard_granularity);
1504 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1505 if (ret < 0)
1506 return ret;
1507
1508 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1509 sizeof(range)))
1510 return -EFAULT;
1511 return 0;
1512}
1513
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001514static bool uuid_is_nonzero(__u8 u[16])
1515{
1516 int i;
1517
1518 for (i = 0; i < 16; i++)
1519 if (u[i])
1520 return true;
1521 return false;
1522}
1523
1524static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1525{
1526#ifdef CONFIG_F2FS_FS_ENCRYPTION
1527 struct f2fs_encryption_policy policy;
1528 struct inode *inode = file_inode(filp);
1529
1530 if (copy_from_user(&policy, (struct f2fs_encryption_policy __user *)arg,
1531 sizeof(policy)))
1532 return -EFAULT;
1533
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001534 return f2fs_process_policy(&policy, inode);
1535#else
1536 return -EOPNOTSUPP;
1537#endif
1538}
1539
1540static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1541{
1542#ifdef CONFIG_F2FS_FS_ENCRYPTION
1543 struct f2fs_encryption_policy policy;
1544 struct inode *inode = file_inode(filp);
1545 int err;
1546
1547 err = f2fs_get_policy(inode, &policy);
1548 if (err)
1549 return err;
1550
1551 if (copy_to_user((struct f2fs_encryption_policy __user *)arg, &policy,
1552 sizeof(policy)))
1553 return -EFAULT;
1554 return 0;
1555#else
1556 return -EOPNOTSUPP;
1557#endif
1558}
1559
1560static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1561{
1562 struct inode *inode = file_inode(filp);
1563 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1564 int err;
1565
1566 if (!f2fs_sb_has_crypto(inode->i_sb))
1567 return -EOPNOTSUPP;
1568
1569 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1570 goto got_it;
1571
1572 err = mnt_want_write_file(filp);
1573 if (err)
1574 return err;
1575
1576 /* update superblock with uuid */
1577 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1578
Chao Yuc5bda1c2015-06-08 13:28:03 +08001579 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001580 if (err) {
1581 /* undo new data */
1582 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yue8240f62015-12-15 17:19:26 +08001583 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001584 return err;
1585 }
Chao Yue8240f62015-12-15 17:19:26 +08001586 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001587got_it:
1588 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1589 16))
1590 return -EFAULT;
1591 return 0;
1592}
1593
Chao Yuc1c1b582015-07-10 18:08:10 +08001594static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1595{
1596 struct inode *inode = file_inode(filp);
1597 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08001598 __u32 sync;
Chao Yuc1c1b582015-07-10 18:08:10 +08001599
1600 if (!capable(CAP_SYS_ADMIN))
1601 return -EPERM;
1602
Chao Yud530d4d2015-10-05 22:22:44 +08001603 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08001604 return -EFAULT;
1605
Chao Yud530d4d2015-10-05 22:22:44 +08001606 if (f2fs_readonly(sbi->sb))
1607 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08001608
Chao Yud530d4d2015-10-05 22:22:44 +08001609 if (!sync) {
Chao Yuc1c1b582015-07-10 18:08:10 +08001610 if (!mutex_trylock(&sbi->gc_mutex))
Chao Yud530d4d2015-10-05 22:22:44 +08001611 return -EBUSY;
1612 } else {
1613 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08001614 }
1615
Chao Yud530d4d2015-10-05 22:22:44 +08001616 return f2fs_gc(sbi, sync);
Chao Yuc1c1b582015-07-10 18:08:10 +08001617}
1618
Chao Yu456b88e2015-10-05 22:24:19 +08001619static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
1620{
1621 struct inode *inode = file_inode(filp);
1622 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu456b88e2015-10-05 22:24:19 +08001623
1624 if (!capable(CAP_SYS_ADMIN))
1625 return -EPERM;
1626
1627 if (f2fs_readonly(sbi->sb))
1628 return -EROFS;
1629
Chao Yue96248b2015-12-24 18:11:32 +08001630 return f2fs_sync_fs(sbi->sb, 1);
Chao Yu456b88e2015-10-05 22:24:19 +08001631}
1632
Chao Yud323d002015-10-27 09:53:45 +08001633static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
1634 struct file *filp,
1635 struct f2fs_defragment *range)
1636{
1637 struct inode *inode = file_inode(filp);
1638 struct f2fs_map_blocks map;
1639 struct extent_info ei;
1640 pgoff_t pg_start, pg_end;
Chao Yu3519e3f2015-12-01 11:56:52 +08001641 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08001642 unsigned int total = 0, sec_num;
Chao Yu3519e3f2015-12-01 11:56:52 +08001643 unsigned int pages_per_sec = sbi->segs_per_sec * blk_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08001644 block_t blk_end = 0;
1645 bool fragmented = false;
1646 int err;
1647
1648 /* if in-place-update policy is enabled, don't waste time here */
1649 if (need_inplace_update(inode))
1650 return -EINVAL;
1651
1652 pg_start = range->start >> PAGE_CACHE_SHIFT;
1653 pg_end = (range->start + range->len) >> PAGE_CACHE_SHIFT;
1654
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001655 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08001656
1657 mutex_lock(&inode->i_mutex);
1658
1659 /* writeback all dirty pages in the range */
1660 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08001661 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08001662 if (err)
1663 goto out;
1664
1665 /*
1666 * lookup mapping info in extent cache, skip defragmenting if physical
1667 * block addresses are continuous.
1668 */
1669 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
1670 if (ei.fofs + ei.len >= pg_end)
1671 goto out;
1672 }
1673
1674 map.m_lblk = pg_start;
Chao Yud323d002015-10-27 09:53:45 +08001675
1676 /*
1677 * lookup mapping info in dnode page cache, skip defragmenting if all
1678 * physical block addresses are continuous even if there are hole(s)
1679 * in logical blocks.
1680 */
1681 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08001682 map.m_len = pg_end - map.m_lblk;
Chao Yud323d002015-10-27 09:53:45 +08001683 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1684 if (err)
1685 goto out;
1686
1687 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1688 map.m_lblk++;
Chao Yud323d002015-10-27 09:53:45 +08001689 continue;
1690 }
1691
1692 if (blk_end && blk_end != map.m_pblk) {
1693 fragmented = true;
1694 break;
1695 }
1696 blk_end = map.m_pblk + map.m_len;
1697
1698 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08001699 }
1700
1701 if (!fragmented)
1702 goto out;
1703
1704 map.m_lblk = pg_start;
1705 map.m_len = pg_end - pg_start;
1706
1707 sec_num = (map.m_len + pages_per_sec - 1) / pages_per_sec;
1708
1709 /*
1710 * make sure there are enough free section for LFS allocation, this can
1711 * avoid defragment running in SSR mode when free section are allocated
1712 * intensively
1713 */
1714 if (has_not_enough_free_secs(sbi, sec_num)) {
1715 err = -EAGAIN;
1716 goto out;
1717 }
1718
1719 while (map.m_lblk < pg_end) {
1720 pgoff_t idx;
1721 int cnt = 0;
1722
1723do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08001724 map.m_len = pg_end - map.m_lblk;
Chao Yud323d002015-10-27 09:53:45 +08001725 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1726 if (err)
1727 goto clear_out;
1728
1729 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1730 map.m_lblk++;
1731 continue;
1732 }
1733
1734 set_inode_flag(F2FS_I(inode), FI_DO_DEFRAG);
1735
1736 idx = map.m_lblk;
1737 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
1738 struct page *page;
1739
1740 page = get_lock_data_page(inode, idx, true);
1741 if (IS_ERR(page)) {
1742 err = PTR_ERR(page);
1743 goto clear_out;
1744 }
1745
1746 set_page_dirty(page);
1747 f2fs_put_page(page, 1);
1748
1749 idx++;
1750 cnt++;
1751 total++;
1752 }
1753
1754 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08001755
1756 if (idx < pg_end && cnt < blk_per_seg)
1757 goto do_map;
1758
1759 clear_inode_flag(F2FS_I(inode), FI_DO_DEFRAG);
1760
1761 err = filemap_fdatawrite(inode->i_mapping);
1762 if (err)
1763 goto out;
1764 }
1765clear_out:
1766 clear_inode_flag(F2FS_I(inode), FI_DO_DEFRAG);
1767out:
1768 mutex_unlock(&inode->i_mutex);
1769 if (!err)
1770 range->len = (u64)total << PAGE_CACHE_SHIFT;
1771 return err;
1772}
1773
1774static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
1775{
1776 struct inode *inode = file_inode(filp);
1777 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1778 struct f2fs_defragment range;
1779 int err;
1780
1781 if (!capable(CAP_SYS_ADMIN))
1782 return -EPERM;
1783
1784 if (!S_ISREG(inode->i_mode))
1785 return -EINVAL;
1786
1787 err = mnt_want_write_file(filp);
1788 if (err)
1789 return err;
1790
1791 if (f2fs_readonly(sbi->sb)) {
1792 err = -EROFS;
1793 goto out;
1794 }
1795
1796 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
1797 sizeof(range))) {
1798 err = -EFAULT;
1799 goto out;
1800 }
1801
1802 /* verify alignment of offset & size */
1803 if (range.start & (F2FS_BLKSIZE - 1) ||
1804 range.len & (F2FS_BLKSIZE - 1)) {
1805 err = -EINVAL;
1806 goto out;
1807 }
1808
1809 err = f2fs_defragment_range(sbi, filp, &range);
1810 if (err < 0)
1811 goto out;
1812
1813 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
1814 sizeof(range)))
1815 err = -EFAULT;
1816out:
1817 mnt_drop_write_file(filp);
1818 return err;
1819}
1820
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001821long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1822{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001823 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001824 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001825 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001826 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001827 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08001828 case F2FS_IOC_GETVERSION:
1829 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001830 case F2FS_IOC_START_ATOMIC_WRITE:
1831 return f2fs_ioc_start_atomic_write(filp);
1832 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1833 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001834 case F2FS_IOC_START_VOLATILE_WRITE:
1835 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001836 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1837 return f2fs_ioc_release_volatile_write(filp);
1838 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1839 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001840 case F2FS_IOC_SHUTDOWN:
1841 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001842 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001843 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001844 case F2FS_IOC_SET_ENCRYPTION_POLICY:
1845 return f2fs_ioc_set_encryption_policy(filp, arg);
1846 case F2FS_IOC_GET_ENCRYPTION_POLICY:
1847 return f2fs_ioc_get_encryption_policy(filp, arg);
1848 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
1849 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08001850 case F2FS_IOC_GARBAGE_COLLECT:
1851 return f2fs_ioc_gc(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08001852 case F2FS_IOC_WRITE_CHECKPOINT:
1853 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08001854 case F2FS_IOC_DEFRAGMENT:
1855 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001856 default:
1857 return -ENOTTY;
1858 }
1859}
1860
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001861static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1862{
1863 struct inode *inode = file_inode(iocb->ki_filp);
1864
1865 if (f2fs_encrypted_inode(inode) &&
1866 !f2fs_has_encryption_key(inode) &&
1867 f2fs_get_encryption_info(inode))
1868 return -EACCES;
1869
1870 return generic_file_write_iter(iocb, from);
1871}
1872
Namjae Jeone9750822013-02-04 23:41:41 +09001873#ifdef CONFIG_COMPAT
1874long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1875{
1876 switch (cmd) {
1877 case F2FS_IOC32_GETFLAGS:
1878 cmd = F2FS_IOC_GETFLAGS;
1879 break;
1880 case F2FS_IOC32_SETFLAGS:
1881 cmd = F2FS_IOC_SETFLAGS;
1882 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08001883 case F2FS_IOC32_GETVERSION:
1884 cmd = F2FS_IOC_GETVERSION;
1885 break;
1886 case F2FS_IOC_START_ATOMIC_WRITE:
1887 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1888 case F2FS_IOC_START_VOLATILE_WRITE:
1889 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1890 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1891 case F2FS_IOC_SHUTDOWN:
1892 case F2FS_IOC_SET_ENCRYPTION_POLICY:
1893 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
1894 case F2FS_IOC_GET_ENCRYPTION_POLICY:
1895 case F2FS_IOC_GARBAGE_COLLECT:
1896 case F2FS_IOC_WRITE_CHECKPOINT:
1897 case F2FS_IOC_DEFRAGMENT:
1898 break;
Namjae Jeone9750822013-02-04 23:41:41 +09001899 default:
1900 return -ENOIOCTLCMD;
1901 }
1902 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1903}
1904#endif
1905
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001906const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001907 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001908 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001909 .write_iter = f2fs_file_write_iter,
1910 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08001911 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001912 .mmap = f2fs_file_mmap,
1913 .fsync = f2fs_sync_file,
1914 .fallocate = f2fs_fallocate,
1915 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001916#ifdef CONFIG_COMPAT
1917 .compat_ioctl = f2fs_compat_ioctl,
1918#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001919 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001920 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001921};