blob: 9eb0100c57fdcd34f2f45ba398dde1b8732d14cf [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"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080030#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090031#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090032
33static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
34 struct vm_fault *vmf)
35{
36 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050037 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070038 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090039 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080040 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090041
42 f2fs_balance_fs(sbi);
43
44 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070045
46 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070047
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090048 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080049 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070050 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070051 err = f2fs_reserve_block(&dn, page->index);
52 if (err) {
53 f2fs_unlock_op(sbi);
54 goto out;
55 }
56 f2fs_put_dnode(&dn);
57 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090058
Namjae Jeon9851e6e2013-04-28 09:04:18 +090059 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090060 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090061 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090062 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090063 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090064 unlock_page(page);
65 err = -EFAULT;
66 goto out;
67 }
68
69 /*
70 * check to see if the page is mapped already (no holes)
71 */
72 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090073 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090074
75 /* page is wholly or partially inside EOF */
76 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
77 unsigned offset;
78 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
79 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
80 }
81 set_page_dirty(page);
82 SetPageUptodate(page);
83
Jaegeuk Kime943a102013-10-25 14:26:31 +090084 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090085mapped:
86 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090087 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090088out:
89 sb_end_pagefault(inode->i_sb);
90 return block_page_mkwrite_return(err);
91}
92
93static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090094 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070095 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090096 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090097};
98
Jaegeuk Kim354a3392013-06-14 08:52:35 +090099static int get_parent_ino(struct inode *inode, nid_t *pino)
100{
101 struct dentry *dentry;
102
103 inode = igrab(inode);
104 dentry = d_find_any_alias(inode);
105 iput(inode);
106 if (!dentry)
107 return 0;
108
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900109 if (update_dent_inode(inode, &dentry->d_name)) {
110 dput(dentry);
111 return 0;
112 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900113
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900114 *pino = parent_ino(dentry);
115 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900116 return 1;
117}
118
Chao Yu9d1589e2014-08-20 18:37:35 +0800119static inline bool need_do_checkpoint(struct inode *inode)
120{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700121 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800122 bool need_cp = false;
123
124 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
125 need_cp = true;
126 else if (file_wrong_pino(inode))
127 need_cp = true;
128 else if (!space_for_roll_forward(sbi))
129 need_cp = true;
130 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
131 need_cp = true;
132 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
133 need_cp = true;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700134 else if (test_opt(sbi, FASTBOOT))
135 need_cp = true;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800136 else if (sbi->active_logs == 2)
137 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800138
139 return need_cp;
140}
141
Changman Lee9c7bb702014-12-08 15:29:40 +0900142static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
143{
144 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
145 bool ret = false;
146 /* But we need to avoid that there are some inode updates */
147 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
148 ret = true;
149 f2fs_put_page(i, 0);
150 return ret;
151}
152
Changman Lee51455b12014-12-08 15:29:41 +0900153static void try_to_fix_pino(struct inode *inode)
154{
155 struct f2fs_inode_info *fi = F2FS_I(inode);
156 nid_t pino;
157
158 down_write(&fi->i_sem);
159 fi->xattr_ver = 0;
160 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
161 get_parent_ino(inode, &pino)) {
162 fi->i_pino = pino;
163 file_got_pino(inode);
164 up_write(&fi->i_sem);
165
166 mark_inode_dirty_sync(inode);
167 f2fs_write_inode(inode, NULL);
168 } else {
169 up_write(&fi->i_sem);
170 }
171}
172
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900173int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
174{
175 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900176 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700177 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700178 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900179 int ret = 0;
180 bool need_cp = false;
181 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900182 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900183 .nr_to_write = LONG_MAX,
184 .for_reclaim = 0,
185 };
186
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900187 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900188 return 0;
189
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900190 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700191
192 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700193 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700194 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900195 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700196 clear_inode_flag(fi, FI_NEED_IPU);
197
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900198 if (ret) {
199 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900200 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900201 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900202
Changman Lee9c7bb702014-12-08 15:29:40 +0900203 /* if the inode is dirty, let's recover all the time */
204 if (!datasync && is_inode_flag_set(fi, FI_DIRTY_INODE)) {
205 update_inode_page(inode);
206 goto go_write;
207 }
208
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700209 /*
210 * if there is no written data, don't waste time to write recovery info.
211 */
212 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700213 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700214
Changman Lee9c7bb702014-12-08 15:29:40 +0900215 /* it may call write_inode just prior to fsync */
216 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700217 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700218
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700219 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700220 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700221 goto flush_out;
222 goto out;
223 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700224go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900225 /* guarantee free sections for fsync */
226 f2fs_balance_fs(sbi);
227
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900228 /*
229 * Both of fdatasync() and fsync() are able to be recovered from
230 * sudden-power-off.
231 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800232 down_read(&fi->i_sem);
233 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900234 up_read(&fi->i_sem);
235
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900236 if (need_cp) {
237 /* all the dirty node pages should be flushed for POR */
238 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900239
Changman Lee51455b12014-12-08 15:29:41 +0900240 /*
241 * We've secured consistency through sync_fs. Following pino
242 * will be used only for fsynced inodes after checkpoint.
243 */
244 try_to_fix_pino(inode);
Jaegeuk Kimcff28522015-03-02 10:48:56 -0800245 clear_inode_flag(fi, FI_APPEND_WRITE);
246 clear_inode_flag(fi, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900247 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900248 }
Changman Lee51455b12014-12-08 15:29:41 +0900249sync_nodes:
250 sync_node_pages(sbi, ino, &wbc);
251
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800252 /* if cp_error was enabled, we should avoid infinite loop */
253 if (unlikely(f2fs_cp_error(sbi)))
254 goto out;
255
Changman Lee51455b12014-12-08 15:29:41 +0900256 if (need_inode_block_update(sbi, ino)) {
257 mark_inode_dirty_sync(inode);
258 f2fs_write_inode(inode, NULL);
259 goto sync_nodes;
260 }
261
262 ret = wait_on_node_pages_writeback(sbi, ino);
263 if (ret)
264 goto out;
265
266 /* once recovery info is written, don't need to tack this */
267 remove_dirty_inode(sbi, ino, APPEND_INO);
268 clear_inode_flag(fi, FI_APPEND_WRITE);
269flush_out:
270 remove_dirty_inode(sbi, ino, UPDATE_INO);
271 clear_inode_flag(fi, FI_UPDATE_WRITE);
272 ret = f2fs_issue_flush(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900273out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900274 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700275 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900276 return ret;
277}
278
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900279static pgoff_t __get_first_dirty_index(struct address_space *mapping,
280 pgoff_t pgofs, int whence)
281{
282 struct pagevec pvec;
283 int nr_pages;
284
285 if (whence != SEEK_DATA)
286 return 0;
287
288 /* find first dirty page index */
289 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700290 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
291 PAGECACHE_TAG_DIRTY, 1);
292 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900293 pagevec_release(&pvec);
294 return pgofs;
295}
296
297static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
298 int whence)
299{
300 switch (whence) {
301 case SEEK_DATA:
302 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
303 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
304 return true;
305 break;
306 case SEEK_HOLE:
307 if (blkaddr == NULL_ADDR)
308 return true;
309 break;
310 }
311 return false;
312}
313
Chao Yu267378d2014-04-23 14:10:24 +0800314static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
315{
316 struct inode *inode = file->f_mapping->host;
317 loff_t maxbytes = inode->i_sb->s_maxbytes;
318 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900319 pgoff_t pgofs, end_offset, dirty;
320 loff_t data_ofs = offset;
321 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800322 int err = 0;
323
324 mutex_lock(&inode->i_mutex);
325
326 isize = i_size_read(inode);
327 if (offset >= isize)
328 goto fail;
329
330 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800331 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800332 if (whence == SEEK_HOLE)
333 data_ofs = isize;
334 goto found;
335 }
336
337 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
338
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900339 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
340
Chao Yu267378d2014-04-23 14:10:24 +0800341 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
342 set_new_dnode(&dn, inode, NULL, NULL, 0);
343 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
344 if (err && err != -ENOENT) {
345 goto fail;
346 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900347 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800348 if (whence == SEEK_DATA) {
349 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
350 F2FS_I(inode));
351 continue;
352 } else {
353 goto found;
354 }
355 }
356
Chao Yu6b2920a2014-07-07 11:21:59 +0800357 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800358
359 /* find data/hole in dnode block */
360 for (; dn.ofs_in_node < end_offset;
361 dn.ofs_in_node++, pgofs++,
Chao Yu2e023172015-02-09 11:23:58 +0800362 data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800363 block_t blkaddr;
364 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
365
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900366 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800367 f2fs_put_dnode(&dn);
368 goto found;
369 }
370 }
371 f2fs_put_dnode(&dn);
372 }
373
374 if (whence == SEEK_DATA)
375 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800376found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900377 if (whence == SEEK_HOLE && data_ofs > isize)
378 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800379 mutex_unlock(&inode->i_mutex);
380 return vfs_setpos(file, data_ofs, maxbytes);
381fail:
382 mutex_unlock(&inode->i_mutex);
383 return -ENXIO;
384}
385
386static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
387{
388 struct inode *inode = file->f_mapping->host;
389 loff_t maxbytes = inode->i_sb->s_maxbytes;
390
391 switch (whence) {
392 case SEEK_SET:
393 case SEEK_CUR:
394 case SEEK_END:
395 return generic_file_llseek_size(file, offset, whence,
396 maxbytes, i_size_read(inode));
397 case SEEK_DATA:
398 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700399 if (offset < 0)
400 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800401 return f2fs_seek_block(file, offset, whence);
402 }
403
404 return -EINVAL;
405}
406
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900407static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
408{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700409 struct inode *inode = file_inode(file);
410
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700411 if (f2fs_encrypted_inode(inode)) {
412 int err = f2fs_get_encryption_info(inode);
413 if (err)
414 return 0;
415 }
416
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700417 /* we don't need to use inline_data strictly */
418 if (f2fs_has_inline_data(inode)) {
419 int err = f2fs_convert_inline_inode(inode);
420 if (err)
421 return err;
422 }
423
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900424 file_accessed(file);
425 vma->vm_ops = &f2fs_file_vm_ops;
426 return 0;
427}
428
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700429static int f2fs_file_open(struct inode *inode, struct file *filp)
430{
431 int ret = generic_file_open(inode, filp);
432
433 if (!ret && f2fs_encrypted_inode(inode)) {
434 ret = f2fs_get_encryption_info(inode);
435 if (ret)
436 ret = -EACCES;
437 }
438 return ret;
439}
440
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900441int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900442{
443 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700444 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900445 struct f2fs_node *raw_node;
446 __le32 *addr;
447
Gu Zheng45590712013-07-15 17:57:38 +0800448 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900449 addr = blkaddr_in_node(raw_node) + ofs;
450
Chris Fries6c311ec2014-01-17 14:44:39 -0600451 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900452 block_t blkaddr = le32_to_cpu(*addr);
453 if (blkaddr == NULL_ADDR)
454 continue;
455
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800456 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800457 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800458 f2fs_update_extent_cache(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900459 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700460 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
461 clear_inode_flag(F2FS_I(dn->inode),
462 FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900463 nr_free++;
464 }
465 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900466 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467 set_page_dirty(dn->node_page);
468 sync_inode_page(dn);
469 }
470 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900471
472 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
473 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900474 return nr_free;
475}
476
477void truncate_data_blocks(struct dnode_of_data *dn)
478{
479 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
480}
481
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800482static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700483 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900484{
485 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700486 pgoff_t index = from >> PAGE_CACHE_SHIFT;
487 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900488 struct page *page;
489
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700490 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700491 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900492
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700493 if (cache_only) {
494 page = grab_cache_page(mapping, index);
495 if (page && PageUptodate(page))
496 goto truncate_out;
497 f2fs_put_page(page, 1);
498 return 0;
499 }
500
501 page = get_lock_data_page(inode, index);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900502 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700503 return 0;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700504truncate_out:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900505 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900506 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700507 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800508 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900509 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700510 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900511}
512
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700513int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900514{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700515 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900516 unsigned int blocksize = inode->i_sb->s_blocksize;
517 struct dnode_of_data dn;
518 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800519 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700520 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800521 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522
Namjae Jeon51dd6242013-04-20 01:28:52 +0900523 trace_f2fs_truncate_blocks_enter(inode, from);
524
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800525 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900526
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700527 if (lock)
528 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800529
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700530 ipage = get_node_page(sbi, inode->i_ino);
531 if (IS_ERR(ipage)) {
532 err = PTR_ERR(ipage);
533 goto out;
534 }
535
536 if (f2fs_has_inline_data(inode)) {
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800537 if (truncate_inline_inode(ipage, from))
538 set_page_dirty(ipage);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700539 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800540 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700541 goto out;
542 }
543
544 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900545 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900546 if (err) {
547 if (err == -ENOENT)
548 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700549 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700550 }
551
Chao Yu6403eb12014-04-26 19:59:52 +0800552 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900553
554 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700555 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900556
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900557 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
558 truncate_data_blocks_range(&dn, count);
559 free_from += count;
560 }
561
562 f2fs_put_dnode(&dn);
563free_next:
564 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800565out:
566 if (lock)
567 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700568
569 /* lastly zero out the first data page */
570 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800571 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900572
Namjae Jeon51dd6242013-04-20 01:28:52 +0900573 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900574 return err;
575}
576
577void f2fs_truncate(struct inode *inode)
578{
579 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
580 S_ISLNK(inode->i_mode)))
581 return;
582
Namjae Jeon51dd6242013-04-20 01:28:52 +0900583 trace_f2fs_truncate(inode);
584
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800585 /* we should check inline_data size */
Jaegeuk Kim01b960e2015-04-23 10:27:21 -0700586 if (f2fs_has_inline_data(inode) && !f2fs_may_inline_data(inode)) {
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800587 if (f2fs_convert_inline_inode(inode))
588 return;
589 }
590
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700591 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900592 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
593 mark_inode_dirty(inode);
594 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900595}
596
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900597int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900598 struct dentry *dentry, struct kstat *stat)
599{
David Howells2b0143b2015-03-17 22:25:59 +0000600 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900601 generic_fillattr(inode, stat);
602 stat->blocks <<= 3;
603 return 0;
604}
605
606#ifdef CONFIG_F2FS_FS_POSIX_ACL
607static void __setattr_copy(struct inode *inode, const struct iattr *attr)
608{
609 struct f2fs_inode_info *fi = F2FS_I(inode);
610 unsigned int ia_valid = attr->ia_valid;
611
612 if (ia_valid & ATTR_UID)
613 inode->i_uid = attr->ia_uid;
614 if (ia_valid & ATTR_GID)
615 inode->i_gid = attr->ia_gid;
616 if (ia_valid & ATTR_ATIME)
617 inode->i_atime = timespec_trunc(attr->ia_atime,
618 inode->i_sb->s_time_gran);
619 if (ia_valid & ATTR_MTIME)
620 inode->i_mtime = timespec_trunc(attr->ia_mtime,
621 inode->i_sb->s_time_gran);
622 if (ia_valid & ATTR_CTIME)
623 inode->i_ctime = timespec_trunc(attr->ia_ctime,
624 inode->i_sb->s_time_gran);
625 if (ia_valid & ATTR_MODE) {
626 umode_t mode = attr->ia_mode;
627
628 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
629 mode &= ~S_ISGID;
630 set_acl_inode(fi, mode);
631 }
632}
633#else
634#define __setattr_copy setattr_copy
635#endif
636
637int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
638{
David Howells2b0143b2015-03-17 22:25:59 +0000639 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900640 struct f2fs_inode_info *fi = F2FS_I(inode);
641 int err;
642
643 err = inode_change_ok(inode, attr);
644 if (err)
645 return err;
646
Chao Yu09db6a22014-09-15 18:02:09 +0800647 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700648 if (f2fs_encrypted_inode(inode) &&
649 f2fs_get_encryption_info(inode))
650 return -EACCES;
651
Chao Yu09db6a22014-09-15 18:02:09 +0800652 if (attr->ia_size != i_size_read(inode)) {
653 truncate_setsize(inode, attr->ia_size);
654 f2fs_truncate(inode);
655 f2fs_balance_fs(F2FS_I_SB(inode));
656 } else {
657 /*
658 * giving a chance to truncate blocks past EOF which
659 * are fallocated with FALLOC_FL_KEEP_SIZE.
660 */
661 f2fs_truncate(inode);
662 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900663 }
664
665 __setattr_copy(inode, attr);
666
667 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800668 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900669 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
670 inode->i_mode = fi->i_acl_mode;
671 clear_inode_flag(fi, FI_ACL_MODE);
672 }
673 }
674
675 mark_inode_dirty(inode);
676 return err;
677}
678
679const struct inode_operations f2fs_file_inode_operations = {
680 .getattr = f2fs_getattr,
681 .setattr = f2fs_setattr,
682 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800683 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900684#ifdef CONFIG_F2FS_FS_XATTR
685 .setxattr = generic_setxattr,
686 .getxattr = generic_getxattr,
687 .listxattr = f2fs_listxattr,
688 .removexattr = generic_removexattr,
689#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900690 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900691};
692
693static void fill_zero(struct inode *inode, pgoff_t index,
694 loff_t start, loff_t len)
695{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700696 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900697 struct page *page;
698
699 if (!len)
700 return;
701
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900702 f2fs_balance_fs(sbi);
703
Gu Zhenge4795562013-09-27 18:08:30 +0800704 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900705 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800706 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900707
708 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900709 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900710 zero_user(page, start, len);
711 set_page_dirty(page);
712 f2fs_put_page(page, 1);
713 }
714}
715
716int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
717{
718 pgoff_t index;
719 int err;
720
721 for (index = pg_start; index < pg_end; index++) {
722 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900723
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900724 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900725 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900727 if (err == -ENOENT)
728 continue;
729 return err;
730 }
731
732 if (dn.data_blkaddr != NULL_ADDR)
733 truncate_data_blocks_range(&dn, 1);
734 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900735 }
736 return 0;
737}
738
Chao Yua66c7b22013-11-22 16:52:50 +0800739static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900740{
741 pgoff_t pg_start, pg_end;
742 loff_t off_start, off_end;
743 int ret = 0;
744
Chao Yu14cecc52014-09-15 18:03:32 +0800745 if (!S_ISREG(inode->i_mode))
746 return -EOPNOTSUPP;
747
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700748 if (f2fs_has_inline_data(inode)) {
749 ret = f2fs_convert_inline_inode(inode);
750 if (ret)
751 return ret;
752 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800753
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
755 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
756
757 off_start = offset & (PAGE_CACHE_SIZE - 1);
758 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
759
760 if (pg_start == pg_end) {
761 fill_zero(inode, pg_start, off_start,
762 off_end - off_start);
763 } else {
764 if (off_start)
765 fill_zero(inode, pg_start++, off_start,
766 PAGE_CACHE_SIZE - off_start);
767 if (off_end)
768 fill_zero(inode, pg_end, 0, off_end);
769
770 if (pg_start < pg_end) {
771 struct address_space *mapping = inode->i_mapping;
772 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700773 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500774
775 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900776
777 blk_start = pg_start << PAGE_CACHE_SHIFT;
778 blk_end = pg_end << PAGE_CACHE_SHIFT;
779 truncate_inode_pages_range(mapping, blk_start,
780 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900781
Gu Zhenge4795562013-09-27 18:08:30 +0800782 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900783 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800784 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900785 }
786 }
787
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900788 return ret;
789}
790
Chao Yub4ace332015-05-06 13:09:46 +0800791static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
792{
793 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
794 struct dnode_of_data dn;
795 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
796 int ret = 0;
797
798 f2fs_lock_op(sbi);
799
800 for (; end < nrpages; start++, end++) {
801 block_t new_addr, old_addr;
802
803 set_new_dnode(&dn, inode, NULL, NULL, 0);
804 ret = get_dnode_of_data(&dn, end, LOOKUP_NODE_RA);
805 if (ret && ret != -ENOENT) {
806 goto out;
807 } else if (ret == -ENOENT) {
808 new_addr = NULL_ADDR;
809 } else {
810 new_addr = dn.data_blkaddr;
811 truncate_data_blocks_range(&dn, 1);
812 f2fs_put_dnode(&dn);
813 }
814
815 if (new_addr == NULL_ADDR) {
816 set_new_dnode(&dn, inode, NULL, NULL, 0);
817 ret = get_dnode_of_data(&dn, start, LOOKUP_NODE_RA);
818 if (ret && ret != -ENOENT)
819 goto out;
820 else if (ret == -ENOENT)
821 continue;
822
823 if (dn.data_blkaddr == NULL_ADDR) {
824 f2fs_put_dnode(&dn);
825 continue;
826 } else {
827 truncate_data_blocks_range(&dn, 1);
828 }
829
830 f2fs_put_dnode(&dn);
831 } else {
832 struct page *ipage;
833
834 ipage = get_node_page(sbi, inode->i_ino);
835 if (IS_ERR(ipage)) {
836 ret = PTR_ERR(ipage);
837 goto out;
838 }
839
840 set_new_dnode(&dn, inode, ipage, NULL, 0);
841 ret = f2fs_reserve_block(&dn, start);
842 if (ret)
843 goto out;
844
845 old_addr = dn.data_blkaddr;
846 if (old_addr != NEW_ADDR && new_addr == NEW_ADDR) {
847 dn.data_blkaddr = NULL_ADDR;
848 f2fs_update_extent_cache(&dn);
849 invalidate_blocks(sbi, old_addr);
850
851 dn.data_blkaddr = new_addr;
852 set_data_blkaddr(&dn);
853 } else if (new_addr != NEW_ADDR) {
854 struct node_info ni;
855 struct f2fs_summary sum;
856
857 get_node_info(sbi, dn.nid, &ni);
858 set_summary(&sum, dn.nid, dn.ofs_in_node,
859 ni.version);
860
861 f2fs_replace_block(sbi, &sum, old_addr,
862 new_addr, true);
863
864 dn.data_blkaddr = new_addr;
865 set_data_blkaddr(&dn);
866 f2fs_update_extent_cache(&dn);
867 }
868
869 f2fs_put_dnode(&dn);
870 }
871 }
872 ret = 0;
873out:
874 f2fs_unlock_op(sbi);
875 return ret;
876}
877
878static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
879{
880 pgoff_t pg_start, pg_end;
881 loff_t new_size;
882 int ret;
883
884 if (!S_ISREG(inode->i_mode))
885 return -EINVAL;
886
887 if (offset + len >= i_size_read(inode))
888 return -EINVAL;
889
890 /* collapse range should be aligned to block size of f2fs. */
891 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
892 return -EINVAL;
893
894 pg_start = offset >> PAGE_CACHE_SHIFT;
895 pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
896
897 /* write out all dirty pages from offset */
898 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
899 if (ret)
900 return ret;
901
902 truncate_pagecache(inode, offset);
903
904 ret = f2fs_do_collapse(inode, pg_start, pg_end);
905 if (ret)
906 return ret;
907
908 new_size = i_size_read(inode) - len;
909
910 ret = truncate_blocks(inode, new_size, true);
911 if (!ret)
912 i_size_write(inode, new_size);
913
914 return ret;
915}
916
Chao Yu75cd4e02015-05-06 13:11:13 +0800917static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
918 int mode)
919{
920 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
921 struct address_space *mapping = inode->i_mapping;
922 pgoff_t index, pg_start, pg_end;
923 loff_t new_size = i_size_read(inode);
924 loff_t off_start, off_end;
925 int ret = 0;
926
927 if (!S_ISREG(inode->i_mode))
928 return -EINVAL;
929
930 ret = inode_newsize_ok(inode, (len + offset));
931 if (ret)
932 return ret;
933
934 f2fs_balance_fs(sbi);
935
936 if (f2fs_has_inline_data(inode)) {
937 ret = f2fs_convert_inline_inode(inode);
938 if (ret)
939 return ret;
940 }
941
942 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
943 if (ret)
944 return ret;
945
946 truncate_pagecache_range(inode, offset, offset + len - 1);
947
948 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
949 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
950
951 off_start = offset & (PAGE_CACHE_SIZE - 1);
952 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
953
954 if (pg_start == pg_end) {
955 fill_zero(inode, pg_start, off_start, off_end - off_start);
956 if (offset + len > new_size)
957 new_size = offset + len;
958 new_size = max_t(loff_t, new_size, offset + len);
959 } else {
960 if (off_start) {
961 fill_zero(inode, pg_start++, off_start,
962 PAGE_CACHE_SIZE - off_start);
963 new_size = max_t(loff_t, new_size,
964 pg_start << PAGE_CACHE_SHIFT);
965 }
966
967 for (index = pg_start; index < pg_end; index++) {
968 struct dnode_of_data dn;
969 struct page *ipage;
970
971 f2fs_lock_op(sbi);
972
973 ipage = get_node_page(sbi, inode->i_ino);
974 if (IS_ERR(ipage)) {
975 ret = PTR_ERR(ipage);
976 f2fs_unlock_op(sbi);
977 goto out;
978 }
979
980 set_new_dnode(&dn, inode, ipage, NULL, 0);
981 ret = f2fs_reserve_block(&dn, index);
982 if (ret) {
983 f2fs_unlock_op(sbi);
984 goto out;
985 }
986
987 if (dn.data_blkaddr != NEW_ADDR) {
988 invalidate_blocks(sbi, dn.data_blkaddr);
989
990 dn.data_blkaddr = NEW_ADDR;
991 set_data_blkaddr(&dn);
992
993 dn.data_blkaddr = NULL_ADDR;
994 f2fs_update_extent_cache(&dn);
995 }
996 f2fs_put_dnode(&dn);
997 f2fs_unlock_op(sbi);
998
999 new_size = max_t(loff_t, new_size,
1000 (index + 1) << PAGE_CACHE_SHIFT);
1001 }
1002
1003 if (off_end) {
1004 fill_zero(inode, pg_end, 0, off_end);
1005 new_size = max_t(loff_t, new_size, offset + len);
1006 }
1007 }
1008
1009out:
1010 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) {
1011 i_size_write(inode, new_size);
1012 mark_inode_dirty(inode);
1013 update_inode_page(inode);
1014 }
1015
1016 return ret;
1017}
1018
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001019static int expand_inode_data(struct inode *inode, loff_t offset,
1020 loff_t len, int mode)
1021{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001022 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001023 pgoff_t index, pg_start, pg_end;
1024 loff_t new_size = i_size_read(inode);
1025 loff_t off_start, off_end;
1026 int ret = 0;
1027
Chao Yu497a0932014-08-04 10:11:17 +08001028 f2fs_balance_fs(sbi);
1029
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001030 ret = inode_newsize_ok(inode, (len + offset));
1031 if (ret)
1032 return ret;
1033
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001034 if (f2fs_has_inline_data(inode)) {
1035 ret = f2fs_convert_inline_inode(inode);
1036 if (ret)
1037 return ret;
1038 }
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001039
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001040 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
1041 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
1042
1043 off_start = offset & (PAGE_CACHE_SIZE - 1);
1044 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
1045
Jaegeuk Kimead43272014-06-13 13:05:55 +09001046 f2fs_lock_op(sbi);
1047
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001048 for (index = pg_start; index <= pg_end; index++) {
1049 struct dnode_of_data dn;
1050
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001051 if (index == pg_end && !off_end)
1052 goto noalloc;
1053
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001054 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001055 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +08001056 if (ret)
1057 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001058noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001059 if (pg_start == pg_end)
1060 new_size = offset + len;
1061 else if (index == pg_start && off_start)
1062 new_size = (index + 1) << PAGE_CACHE_SHIFT;
1063 else if (index == pg_end)
1064 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
1065 else
1066 new_size += PAGE_CACHE_SIZE;
1067 }
1068
1069 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
1070 i_size_read(inode) < new_size) {
1071 i_size_write(inode, new_size);
1072 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +09001073 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001074 }
Jaegeuk Kimead43272014-06-13 13:05:55 +09001075 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001076
1077 return ret;
1078}
1079
1080static long f2fs_fallocate(struct file *file, int mode,
1081 loff_t offset, loff_t len)
1082{
Al Viro6131ffa2013-02-27 16:59:05 -05001083 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001084 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001085
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001086 if (f2fs_encrypted_inode(inode) && (mode & FALLOC_FL_COLLAPSE_RANGE))
1087 return -EOPNOTSUPP;
1088
Chao Yub4ace332015-05-06 13:09:46 +08001089 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yu75cd4e02015-05-06 13:11:13 +08001090 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001091 return -EOPNOTSUPP;
1092
Chao Yu3375f692014-01-28 10:29:26 +08001093 mutex_lock(&inode->i_mutex);
1094
Taehee Yoo587c0a42015-04-21 15:59:12 +09001095 if (mode & FALLOC_FL_PUNCH_HOLE) {
1096 if (offset >= inode->i_size)
1097 goto out;
1098
Chao Yua66c7b22013-11-22 16:52:50 +08001099 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001100 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1101 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001102 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1103 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001104 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001105 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001106 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001107
Namjae Jeon3af60a42012-12-30 14:52:37 +09001108 if (!ret) {
1109 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1110 mark_inode_dirty(inode);
1111 }
Chao Yu3375f692014-01-28 10:29:26 +08001112
Taehee Yoo587c0a42015-04-21 15:59:12 +09001113out:
Chao Yu3375f692014-01-28 10:29:26 +08001114 mutex_unlock(&inode->i_mutex);
1115
Namjae Jeonc01e2852013-04-23 17:00:52 +09001116 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001117 return ret;
1118}
1119
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001120static int f2fs_release_file(struct inode *inode, struct file *filp)
1121{
1122 /* some remained atomic pages should discarded */
1123 if (f2fs_is_atomic_file(inode))
1124 commit_inmem_pages(inode, true);
1125 if (f2fs_is_volatile_file(inode)) {
1126 set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1127 filemap_fdatawrite(inode->i_mapping);
1128 clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1129 }
1130 return 0;
1131}
1132
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001133#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1134#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1135
1136static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1137{
1138 if (S_ISDIR(mode))
1139 return flags;
1140 else if (S_ISREG(mode))
1141 return flags & F2FS_REG_FLMASK;
1142 else
1143 return flags & F2FS_OTHER_FLMASK;
1144}
1145
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001146static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001147{
Al Viro6131ffa2013-02-27 16:59:05 -05001148 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001149 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001150 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1151 return put_user(flags, (int __user *)arg);
1152}
1153
1154static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1155{
1156 struct inode *inode = file_inode(filp);
1157 struct f2fs_inode_info *fi = F2FS_I(inode);
1158 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1159 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001160 int ret;
1161
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001162 ret = mnt_want_write_file(filp);
1163 if (ret)
1164 return ret;
1165
1166 if (!inode_owner_or_capable(inode)) {
1167 ret = -EACCES;
1168 goto out;
1169 }
1170
1171 if (get_user(flags, (int __user *)arg)) {
1172 ret = -EFAULT;
1173 goto out;
1174 }
1175
1176 flags = f2fs_mask_flags(inode->i_mode, flags);
1177
1178 mutex_lock(&inode->i_mutex);
1179
1180 oldflags = fi->i_flags;
1181
1182 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1183 if (!capable(CAP_LINUX_IMMUTABLE)) {
1184 mutex_unlock(&inode->i_mutex);
1185 ret = -EPERM;
1186 goto out;
1187 }
1188 }
1189
1190 flags = flags & FS_FL_USER_MODIFIABLE;
1191 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1192 fi->i_flags = flags;
1193 mutex_unlock(&inode->i_mutex);
1194
1195 f2fs_set_inode_flags(inode);
1196 inode->i_ctime = CURRENT_TIME;
1197 mark_inode_dirty(inode);
1198out:
1199 mnt_drop_write_file(filp);
1200 return ret;
1201}
1202
Chao Yud49f3e82015-01-23 20:36:04 +08001203static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1204{
1205 struct inode *inode = file_inode(filp);
1206
1207 return put_user(inode->i_generation, (int __user *)arg);
1208}
1209
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001210static int f2fs_ioc_start_atomic_write(struct file *filp)
1211{
1212 struct inode *inode = file_inode(filp);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001213
1214 if (!inode_owner_or_capable(inode))
1215 return -EACCES;
1216
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001217 f2fs_balance_fs(F2FS_I_SB(inode));
1218
1219 if (f2fs_is_atomic_file(inode))
1220 return 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001221
1222 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1223
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001224 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001225}
1226
1227static int f2fs_ioc_commit_atomic_write(struct file *filp)
1228{
1229 struct inode *inode = file_inode(filp);
1230 int ret;
1231
1232 if (!inode_owner_or_capable(inode))
1233 return -EACCES;
1234
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001235 if (f2fs_is_volatile_file(inode))
1236 return 0;
1237
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001238 ret = mnt_want_write_file(filp);
1239 if (ret)
1240 return ret;
1241
1242 if (f2fs_is_atomic_file(inode))
1243 commit_inmem_pages(inode, false);
1244
1245 ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
1246 mnt_drop_write_file(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001247 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001248 return ret;
1249}
1250
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001251static int f2fs_ioc_start_volatile_write(struct file *filp)
1252{
1253 struct inode *inode = file_inode(filp);
1254
1255 if (!inode_owner_or_capable(inode))
1256 return -EACCES;
1257
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001258 if (f2fs_is_volatile_file(inode))
1259 return 0;
1260
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001261 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001262
1263 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001264}
1265
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001266static int f2fs_ioc_release_volatile_write(struct file *filp)
1267{
1268 struct inode *inode = file_inode(filp);
1269
1270 if (!inode_owner_or_capable(inode))
1271 return -EACCES;
1272
1273 if (!f2fs_is_volatile_file(inode))
1274 return 0;
1275
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001276 if (!f2fs_is_first_block_written(inode))
1277 return truncate_partial_data_page(inode, 0, true);
1278
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001279 punch_hole(inode, 0, F2FS_BLKSIZE);
1280 return 0;
1281}
1282
1283static int f2fs_ioc_abort_volatile_write(struct file *filp)
1284{
1285 struct inode *inode = file_inode(filp);
1286 int ret;
1287
1288 if (!inode_owner_or_capable(inode))
1289 return -EACCES;
1290
1291 ret = mnt_want_write_file(filp);
1292 if (ret)
1293 return ret;
1294
1295 f2fs_balance_fs(F2FS_I_SB(inode));
1296
1297 if (f2fs_is_atomic_file(inode)) {
1298 commit_inmem_pages(inode, false);
1299 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1300 }
1301
1302 if (f2fs_is_volatile_file(inode)) {
1303 clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1304 filemap_fdatawrite(inode->i_mapping);
1305 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1306 }
1307 mnt_drop_write_file(filp);
1308 return ret;
1309}
1310
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001311static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1312{
1313 struct inode *inode = file_inode(filp);
1314 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1315 struct super_block *sb = sbi->sb;
1316 __u32 in;
1317
1318 if (!capable(CAP_SYS_ADMIN))
1319 return -EPERM;
1320
1321 if (get_user(in, (__u32 __user *)arg))
1322 return -EFAULT;
1323
1324 switch (in) {
1325 case F2FS_GOING_DOWN_FULLSYNC:
1326 sb = freeze_bdev(sb->s_bdev);
1327 if (sb && !IS_ERR(sb)) {
1328 f2fs_stop_checkpoint(sbi);
1329 thaw_bdev(sb->s_bdev, sb);
1330 }
1331 break;
1332 case F2FS_GOING_DOWN_METASYNC:
1333 /* do checkpoint only */
1334 f2fs_sync_fs(sb, 1);
1335 f2fs_stop_checkpoint(sbi);
1336 break;
1337 case F2FS_GOING_DOWN_NOSYNC:
1338 f2fs_stop_checkpoint(sbi);
1339 break;
1340 default:
1341 return -EINVAL;
1342 }
1343 return 0;
1344}
1345
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001346static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1347{
1348 struct inode *inode = file_inode(filp);
1349 struct super_block *sb = inode->i_sb;
1350 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1351 struct fstrim_range range;
1352 int ret;
1353
1354 if (!capable(CAP_SYS_ADMIN))
1355 return -EPERM;
1356
1357 if (!blk_queue_discard(q))
1358 return -EOPNOTSUPP;
1359
1360 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1361 sizeof(range)))
1362 return -EFAULT;
1363
1364 range.minlen = max((unsigned int)range.minlen,
1365 q->limits.discard_granularity);
1366 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1367 if (ret < 0)
1368 return ret;
1369
1370 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1371 sizeof(range)))
1372 return -EFAULT;
1373 return 0;
1374}
1375
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001376static bool uuid_is_nonzero(__u8 u[16])
1377{
1378 int i;
1379
1380 for (i = 0; i < 16; i++)
1381 if (u[i])
1382 return true;
1383 return false;
1384}
1385
1386static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1387{
1388#ifdef CONFIG_F2FS_FS_ENCRYPTION
1389 struct f2fs_encryption_policy policy;
1390 struct inode *inode = file_inode(filp);
1391
1392 if (copy_from_user(&policy, (struct f2fs_encryption_policy __user *)arg,
1393 sizeof(policy)))
1394 return -EFAULT;
1395
1396 if (f2fs_has_inline_data(inode)) {
1397 int ret = f2fs_convert_inline_inode(inode);
1398 if (ret)
1399 return ret;
1400 }
1401
1402 return f2fs_process_policy(&policy, inode);
1403#else
1404 return -EOPNOTSUPP;
1405#endif
1406}
1407
1408static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1409{
1410#ifdef CONFIG_F2FS_FS_ENCRYPTION
1411 struct f2fs_encryption_policy policy;
1412 struct inode *inode = file_inode(filp);
1413 int err;
1414
1415 err = f2fs_get_policy(inode, &policy);
1416 if (err)
1417 return err;
1418
1419 if (copy_to_user((struct f2fs_encryption_policy __user *)arg, &policy,
1420 sizeof(policy)))
1421 return -EFAULT;
1422 return 0;
1423#else
1424 return -EOPNOTSUPP;
1425#endif
1426}
1427
1428static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1429{
1430 struct inode *inode = file_inode(filp);
1431 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1432 int err;
1433
1434 if (!f2fs_sb_has_crypto(inode->i_sb))
1435 return -EOPNOTSUPP;
1436
1437 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1438 goto got_it;
1439
1440 err = mnt_want_write_file(filp);
1441 if (err)
1442 return err;
1443
1444 /* update superblock with uuid */
1445 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1446
1447 err = f2fs_commit_super(sbi);
1448
1449 mnt_drop_write_file(filp);
1450 if (err) {
1451 /* undo new data */
1452 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1453 return err;
1454 }
1455got_it:
1456 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1457 16))
1458 return -EFAULT;
1459 return 0;
1460}
1461
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001462long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1463{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001464 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001465 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001466 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001467 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001468 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08001469 case F2FS_IOC_GETVERSION:
1470 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001471 case F2FS_IOC_START_ATOMIC_WRITE:
1472 return f2fs_ioc_start_atomic_write(filp);
1473 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1474 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001475 case F2FS_IOC_START_VOLATILE_WRITE:
1476 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001477 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1478 return f2fs_ioc_release_volatile_write(filp);
1479 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1480 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001481 case F2FS_IOC_SHUTDOWN:
1482 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001483 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001484 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001485 case F2FS_IOC_SET_ENCRYPTION_POLICY:
1486 return f2fs_ioc_set_encryption_policy(filp, arg);
1487 case F2FS_IOC_GET_ENCRYPTION_POLICY:
1488 return f2fs_ioc_get_encryption_policy(filp, arg);
1489 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
1490 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001491 default:
1492 return -ENOTTY;
1493 }
1494}
1495
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001496static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1497{
1498 struct inode *inode = file_inode(iocb->ki_filp);
1499
1500 if (f2fs_encrypted_inode(inode) &&
1501 !f2fs_has_encryption_key(inode) &&
1502 f2fs_get_encryption_info(inode))
1503 return -EACCES;
1504
1505 return generic_file_write_iter(iocb, from);
1506}
1507
Namjae Jeone9750822013-02-04 23:41:41 +09001508#ifdef CONFIG_COMPAT
1509long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1510{
1511 switch (cmd) {
1512 case F2FS_IOC32_GETFLAGS:
1513 cmd = F2FS_IOC_GETFLAGS;
1514 break;
1515 case F2FS_IOC32_SETFLAGS:
1516 cmd = F2FS_IOC_SETFLAGS;
1517 break;
1518 default:
1519 return -ENOIOCTLCMD;
1520 }
1521 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1522}
1523#endif
1524
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001525const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001526 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001527 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001528 .write_iter = f2fs_file_write_iter,
1529 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08001530 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001531 .mmap = f2fs_file_mmap,
1532 .fsync = f2fs_sync_file,
1533 .fallocate = f2fs_fallocate,
1534 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001535#ifdef CONFIG_COMPAT
1536 .compat_ioctl = f2fs_compat_ioctl,
1537#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001538 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001539 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001540};