blob: 016ed3ba2ca46fbfffc940b7b45b1859afa0cc63 [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
43 f2fs_balance_fs(sbi);
44
45 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070046
47 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070048
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080050 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070051 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070052 err = f2fs_reserve_block(&dn, page->index);
53 if (err) {
54 f2fs_unlock_op(sbi);
55 goto out;
56 }
57 f2fs_put_dnode(&dn);
58 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090059
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 */
77 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
78 unsigned offset;
79 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
80 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
81 }
82 set_page_dirty(page);
83 SetPageUptodate(page);
84
Jaegeuk Kime943a102013-10-25 14:26:31 +090085 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090086mapped:
87 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090088 f2fs_wait_on_page_writeback(page, DATA);
Chao Yu5b339122015-07-13 17:43:19 +080089 /* if gced page is attached, don't write to cold segment */
90 clear_cold_data(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090091out:
92 sb_end_pagefault(inode->i_sb);
93 return block_page_mkwrite_return(err);
94}
95
96static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090097 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070098 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090099 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900100};
101
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900102static int get_parent_ino(struct inode *inode, nid_t *pino)
103{
104 struct dentry *dentry;
105
106 inode = igrab(inode);
107 dentry = d_find_any_alias(inode);
108 iput(inode);
109 if (!dentry)
110 return 0;
111
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700112 if (update_dent_inode(inode, inode, &dentry->d_name)) {
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900113 dput(dentry);
114 return 0;
115 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900116
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900117 *pino = parent_ino(dentry);
118 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900119 return 1;
120}
121
Chao Yu9d1589e2014-08-20 18:37:35 +0800122static inline bool need_do_checkpoint(struct inode *inode)
123{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700124 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800125 bool need_cp = false;
126
127 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
128 need_cp = true;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700129 else if (file_enc_name(inode) && need_dentry_mark(sbi, inode->i_ino))
130 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800131 else if (file_wrong_pino(inode))
132 need_cp = true;
133 else if (!space_for_roll_forward(sbi))
134 need_cp = true;
135 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
136 need_cp = true;
137 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
138 need_cp = true;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700139 else if (test_opt(sbi, FASTBOOT))
140 need_cp = true;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800141 else if (sbi->active_logs == 2)
142 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800143
144 return need_cp;
145}
146
Changman Lee9c7bb702014-12-08 15:29:40 +0900147static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
148{
149 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
150 bool ret = false;
151 /* But we need to avoid that there are some inode updates */
152 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
153 ret = true;
154 f2fs_put_page(i, 0);
155 return ret;
156}
157
Changman Lee51455b12014-12-08 15:29:41 +0900158static void try_to_fix_pino(struct inode *inode)
159{
160 struct f2fs_inode_info *fi = F2FS_I(inode);
161 nid_t pino;
162
163 down_write(&fi->i_sem);
164 fi->xattr_ver = 0;
165 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
166 get_parent_ino(inode, &pino)) {
167 fi->i_pino = pino;
168 file_got_pino(inode);
169 up_write(&fi->i_sem);
170
171 mark_inode_dirty_sync(inode);
172 f2fs_write_inode(inode, NULL);
173 } else {
174 up_write(&fi->i_sem);
175 }
176}
177
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900178int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
179{
180 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900181 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700182 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700183 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900184 int ret = 0;
185 bool need_cp = false;
186 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900187 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900188 .nr_to_write = LONG_MAX,
189 .for_reclaim = 0,
190 };
191
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900192 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900193 return 0;
194
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900195 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700196
197 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700198 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700199 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900200 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700201 clear_inode_flag(fi, FI_NEED_IPU);
202
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900203 if (ret) {
204 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900206 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900207
Changman Lee9c7bb702014-12-08 15:29:40 +0900208 /* if the inode is dirty, let's recover all the time */
209 if (!datasync && is_inode_flag_set(fi, FI_DIRTY_INODE)) {
210 update_inode_page(inode);
211 goto go_write;
212 }
213
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700214 /*
215 * if there is no written data, don't waste time to write recovery info.
216 */
217 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700218 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700219
Changman Lee9c7bb702014-12-08 15:29:40 +0900220 /* it may call write_inode just prior to fsync */
221 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700222 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700223
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700224 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700225 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700226 goto flush_out;
227 goto out;
228 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700229go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900230 /* guarantee free sections for fsync */
231 f2fs_balance_fs(sbi);
232
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900233 /*
234 * Both of fdatasync() and fsync() are able to be recovered from
235 * sudden-power-off.
236 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800237 down_read(&fi->i_sem);
238 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900239 up_read(&fi->i_sem);
240
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900241 if (need_cp) {
242 /* all the dirty node pages should be flushed for POR */
243 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900244
Changman Lee51455b12014-12-08 15:29:41 +0900245 /*
246 * We've secured consistency through sync_fs. Following pino
247 * will be used only for fsynced inodes after checkpoint.
248 */
249 try_to_fix_pino(inode);
Jaegeuk Kimcff28522015-03-02 10:48:56 -0800250 clear_inode_flag(fi, FI_APPEND_WRITE);
251 clear_inode_flag(fi, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900252 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900253 }
Changman Lee51455b12014-12-08 15:29:41 +0900254sync_nodes:
255 sync_node_pages(sbi, ino, &wbc);
256
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800257 /* if cp_error was enabled, we should avoid infinite loop */
258 if (unlikely(f2fs_cp_error(sbi)))
259 goto out;
260
Changman Lee51455b12014-12-08 15:29:41 +0900261 if (need_inode_block_update(sbi, ino)) {
262 mark_inode_dirty_sync(inode);
263 f2fs_write_inode(inode, NULL);
264 goto sync_nodes;
265 }
266
267 ret = wait_on_node_pages_writeback(sbi, ino);
268 if (ret)
269 goto out;
270
271 /* once recovery info is written, don't need to tack this */
272 remove_dirty_inode(sbi, ino, APPEND_INO);
273 clear_inode_flag(fi, FI_APPEND_WRITE);
274flush_out:
275 remove_dirty_inode(sbi, ino, UPDATE_INO);
276 clear_inode_flag(fi, FI_UPDATE_WRITE);
277 ret = f2fs_issue_flush(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900278out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900279 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700280 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900281 return ret;
282}
283
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900284static pgoff_t __get_first_dirty_index(struct address_space *mapping,
285 pgoff_t pgofs, int whence)
286{
287 struct pagevec pvec;
288 int nr_pages;
289
290 if (whence != SEEK_DATA)
291 return 0;
292
293 /* find first dirty page index */
294 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700295 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
296 PAGECACHE_TAG_DIRTY, 1);
297 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900298 pagevec_release(&pvec);
299 return pgofs;
300}
301
302static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
303 int whence)
304{
305 switch (whence) {
306 case SEEK_DATA:
307 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
308 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
309 return true;
310 break;
311 case SEEK_HOLE:
312 if (blkaddr == NULL_ADDR)
313 return true;
314 break;
315 }
316 return false;
317}
318
Chao Yu267378d2014-04-23 14:10:24 +0800319static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
320{
321 struct inode *inode = file->f_mapping->host;
322 loff_t maxbytes = inode->i_sb->s_maxbytes;
323 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900324 pgoff_t pgofs, end_offset, dirty;
325 loff_t data_ofs = offset;
326 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800327 int err = 0;
328
329 mutex_lock(&inode->i_mutex);
330
331 isize = i_size_read(inode);
332 if (offset >= isize)
333 goto fail;
334
335 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800336 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800337 if (whence == SEEK_HOLE)
338 data_ofs = isize;
339 goto found;
340 }
341
342 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
343
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900344 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
345
Chao Yu267378d2014-04-23 14:10:24 +0800346 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
347 set_new_dnode(&dn, inode, NULL, NULL, 0);
348 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
349 if (err && err != -ENOENT) {
350 goto fail;
351 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900352 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800353 if (whence == SEEK_DATA) {
354 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
355 F2FS_I(inode));
356 continue;
357 } else {
358 goto found;
359 }
360 }
361
Chao Yu6b2920a2014-07-07 11:21:59 +0800362 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800363
364 /* find data/hole in dnode block */
365 for (; dn.ofs_in_node < end_offset;
366 dn.ofs_in_node++, pgofs++,
Chao Yu2e023172015-02-09 11:23:58 +0800367 data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800368 block_t blkaddr;
369 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
370
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900371 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800372 f2fs_put_dnode(&dn);
373 goto found;
374 }
375 }
376 f2fs_put_dnode(&dn);
377 }
378
379 if (whence == SEEK_DATA)
380 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800381found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900382 if (whence == SEEK_HOLE && data_ofs > isize)
383 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800384 mutex_unlock(&inode->i_mutex);
385 return vfs_setpos(file, data_ofs, maxbytes);
386fail:
387 mutex_unlock(&inode->i_mutex);
388 return -ENXIO;
389}
390
391static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
392{
393 struct inode *inode = file->f_mapping->host;
394 loff_t maxbytes = inode->i_sb->s_maxbytes;
395
396 switch (whence) {
397 case SEEK_SET:
398 case SEEK_CUR:
399 case SEEK_END:
400 return generic_file_llseek_size(file, offset, whence,
401 maxbytes, i_size_read(inode));
402 case SEEK_DATA:
403 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700404 if (offset < 0)
405 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800406 return f2fs_seek_block(file, offset, whence);
407 }
408
409 return -EINVAL;
410}
411
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900412static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
413{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700414 struct inode *inode = file_inode(file);
415
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700416 if (f2fs_encrypted_inode(inode)) {
417 int err = f2fs_get_encryption_info(inode);
418 if (err)
419 return 0;
420 }
421
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700422 /* we don't need to use inline_data strictly */
423 if (f2fs_has_inline_data(inode)) {
424 int err = f2fs_convert_inline_inode(inode);
425 if (err)
426 return err;
427 }
428
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900429 file_accessed(file);
430 vma->vm_ops = &f2fs_file_vm_ops;
431 return 0;
432}
433
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700434static int f2fs_file_open(struct inode *inode, struct file *filp)
435{
436 int ret = generic_file_open(inode, filp);
437
438 if (!ret && f2fs_encrypted_inode(inode)) {
439 ret = f2fs_get_encryption_info(inode);
440 if (ret)
441 ret = -EACCES;
442 }
443 return ret;
444}
445
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900446int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900447{
448 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700449 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900450 struct f2fs_node *raw_node;
451 __le32 *addr;
452
Gu Zheng45590712013-07-15 17:57:38 +0800453 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900454 addr = blkaddr_in_node(raw_node) + ofs;
455
Chris Fries6c311ec2014-01-17 14:44:39 -0600456 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900457 block_t blkaddr = le32_to_cpu(*addr);
458 if (blkaddr == NULL_ADDR)
459 continue;
460
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800461 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800462 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800463 f2fs_update_extent_cache(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900464 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700465 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
466 clear_inode_flag(F2FS_I(dn->inode),
467 FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900468 nr_free++;
469 }
470 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900471 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900472 set_page_dirty(dn->node_page);
473 sync_inode_page(dn);
474 }
475 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900476
477 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
478 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900479 return nr_free;
480}
481
482void truncate_data_blocks(struct dnode_of_data *dn)
483{
484 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
485}
486
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800487static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700488 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900489{
490 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700491 pgoff_t index = from >> PAGE_CACHE_SHIFT;
492 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900493 struct page *page;
494
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700495 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700496 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900497
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700498 if (cache_only) {
499 page = grab_cache_page(mapping, index);
500 if (page && PageUptodate(page))
501 goto truncate_out;
502 f2fs_put_page(page, 1);
503 return 0;
504 }
505
506 page = get_lock_data_page(inode, index);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900507 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700508 return 0;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700509truncate_out:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900510 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900511 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700512 if (!cache_only || !f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800513 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900514 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700515 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900516}
517
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700518int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900519{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700520 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900521 unsigned int blocksize = inode->i_sb->s_blocksize;
522 struct dnode_of_data dn;
523 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800524 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700525 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800526 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900527
Namjae Jeon51dd6242013-04-20 01:28:52 +0900528 trace_f2fs_truncate_blocks_enter(inode, from);
529
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800530 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900531
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700532 if (lock)
533 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800534
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700535 ipage = get_node_page(sbi, inode->i_ino);
536 if (IS_ERR(ipage)) {
537 err = PTR_ERR(ipage);
538 goto out;
539 }
540
541 if (f2fs_has_inline_data(inode)) {
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800542 if (truncate_inline_inode(ipage, from))
543 set_page_dirty(ipage);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700544 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800545 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700546 goto out;
547 }
548
549 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900550 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900551 if (err) {
552 if (err == -ENOENT)
553 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700554 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700555 }
556
Chao Yu6403eb12014-04-26 19:59:52 +0800557 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900558
559 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700560 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900561
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900562 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
563 truncate_data_blocks_range(&dn, count);
564 free_from += count;
565 }
566
567 f2fs_put_dnode(&dn);
568free_next:
569 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800570out:
571 if (lock)
572 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700573
574 /* lastly zero out the first data page */
575 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800576 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900577
Namjae Jeon51dd6242013-04-20 01:28:52 +0900578 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900579 return err;
580}
581
Chao Yu55f57d22015-07-16 18:19:02 +0800582void f2fs_truncate(struct inode *inode, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900583{
584 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
585 S_ISLNK(inode->i_mode)))
586 return;
587
Namjae Jeon51dd6242013-04-20 01:28:52 +0900588 trace_f2fs_truncate(inode);
589
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800590 /* we should check inline_data size */
Jaegeuk Kim01b960e2015-04-23 10:27:21 -0700591 if (f2fs_has_inline_data(inode) && !f2fs_may_inline_data(inode)) {
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800592 if (f2fs_convert_inline_inode(inode))
593 return;
594 }
595
Chao Yu55f57d22015-07-16 18:19:02 +0800596 if (!truncate_blocks(inode, i_size_read(inode), lock)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900597 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
598 mark_inode_dirty(inode);
599 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900600}
601
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900602int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900603 struct dentry *dentry, struct kstat *stat)
604{
David Howells2b0143b2015-03-17 22:25:59 +0000605 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900606 generic_fillattr(inode, stat);
607 stat->blocks <<= 3;
608 return 0;
609}
610
611#ifdef CONFIG_F2FS_FS_POSIX_ACL
612static void __setattr_copy(struct inode *inode, const struct iattr *attr)
613{
614 struct f2fs_inode_info *fi = F2FS_I(inode);
615 unsigned int ia_valid = attr->ia_valid;
616
617 if (ia_valid & ATTR_UID)
618 inode->i_uid = attr->ia_uid;
619 if (ia_valid & ATTR_GID)
620 inode->i_gid = attr->ia_gid;
621 if (ia_valid & ATTR_ATIME)
622 inode->i_atime = timespec_trunc(attr->ia_atime,
623 inode->i_sb->s_time_gran);
624 if (ia_valid & ATTR_MTIME)
625 inode->i_mtime = timespec_trunc(attr->ia_mtime,
626 inode->i_sb->s_time_gran);
627 if (ia_valid & ATTR_CTIME)
628 inode->i_ctime = timespec_trunc(attr->ia_ctime,
629 inode->i_sb->s_time_gran);
630 if (ia_valid & ATTR_MODE) {
631 umode_t mode = attr->ia_mode;
632
633 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
634 mode &= ~S_ISGID;
635 set_acl_inode(fi, mode);
636 }
637}
638#else
639#define __setattr_copy setattr_copy
640#endif
641
642int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
643{
David Howells2b0143b2015-03-17 22:25:59 +0000644 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900645 struct f2fs_inode_info *fi = F2FS_I(inode);
646 int err;
647
648 err = inode_change_ok(inode, attr);
649 if (err)
650 return err;
651
Chao Yu09db6a22014-09-15 18:02:09 +0800652 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700653 if (f2fs_encrypted_inode(inode) &&
654 f2fs_get_encryption_info(inode))
655 return -EACCES;
656
Chao Yu3c454142015-06-05 18:34:02 +0800657 if (attr->ia_size <= i_size_read(inode)) {
Chao Yu09db6a22014-09-15 18:02:09 +0800658 truncate_setsize(inode, attr->ia_size);
Chao Yu55f57d22015-07-16 18:19:02 +0800659 f2fs_truncate(inode, true);
Chao Yu09db6a22014-09-15 18:02:09 +0800660 f2fs_balance_fs(F2FS_I_SB(inode));
661 } else {
662 /*
Chao Yu3c454142015-06-05 18:34:02 +0800663 * do not trim all blocks after i_size if target size is
664 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800665 */
Chao Yu3c454142015-06-05 18:34:02 +0800666 truncate_setsize(inode, attr->ia_size);
Chao Yu09db6a22014-09-15 18:02:09 +0800667 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668 }
669
670 __setattr_copy(inode, attr);
671
672 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800673 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900674 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
675 inode->i_mode = fi->i_acl_mode;
676 clear_inode_flag(fi, FI_ACL_MODE);
677 }
678 }
679
680 mark_inode_dirty(inode);
681 return err;
682}
683
684const struct inode_operations f2fs_file_inode_operations = {
685 .getattr = f2fs_getattr,
686 .setattr = f2fs_setattr,
687 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800688 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900689#ifdef CONFIG_F2FS_FS_XATTR
690 .setxattr = generic_setxattr,
691 .getxattr = generic_getxattr,
692 .listxattr = f2fs_listxattr,
693 .removexattr = generic_removexattr,
694#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900695 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900696};
697
Chao Yu63943282015-08-07 18:36:06 +0800698static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900699 loff_t start, loff_t len)
700{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700701 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900702 struct page *page;
703
704 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800705 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900706
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900707 f2fs_balance_fs(sbi);
708
Gu Zhenge4795562013-09-27 18:08:30 +0800709 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900710 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800711 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900712
Chao Yu63943282015-08-07 18:36:06 +0800713 if (IS_ERR(page))
714 return PTR_ERR(page);
715
716 f2fs_wait_on_page_writeback(page, DATA);
717 zero_user(page, start, len);
718 set_page_dirty(page);
719 f2fs_put_page(page, 1);
720 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900721}
722
723int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
724{
725 pgoff_t index;
726 int err;
727
728 for (index = pg_start; index < pg_end; index++) {
729 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900730
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900731 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900732 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900733 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900734 if (err == -ENOENT)
735 continue;
736 return err;
737 }
738
739 if (dn.data_blkaddr != NULL_ADDR)
740 truncate_data_blocks_range(&dn, 1);
741 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900742 }
743 return 0;
744}
745
Chao Yua66c7b22013-11-22 16:52:50 +0800746static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900747{
748 pgoff_t pg_start, pg_end;
749 loff_t off_start, off_end;
750 int ret = 0;
751
Chao Yu14cecc52014-09-15 18:03:32 +0800752 if (!S_ISREG(inode->i_mode))
753 return -EOPNOTSUPP;
754
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700755 if (f2fs_has_inline_data(inode)) {
756 ret = f2fs_convert_inline_inode(inode);
757 if (ret)
758 return ret;
759 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800760
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900761 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
762 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
763
764 off_start = offset & (PAGE_CACHE_SIZE - 1);
765 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
766
767 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800768 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900769 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800770 if (ret)
771 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900772 } else {
Chao Yu63943282015-08-07 18:36:06 +0800773 if (off_start) {
774 ret = fill_zero(inode, pg_start++, off_start,
775 PAGE_CACHE_SIZE - off_start);
776 if (ret)
777 return ret;
778 }
779 if (off_end) {
780 ret = fill_zero(inode, pg_end, 0, off_end);
781 if (ret)
782 return ret;
783 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900784
785 if (pg_start < pg_end) {
786 struct address_space *mapping = inode->i_mapping;
787 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700788 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500789
790 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900791
792 blk_start = pg_start << PAGE_CACHE_SHIFT;
793 blk_end = pg_end << PAGE_CACHE_SHIFT;
794 truncate_inode_pages_range(mapping, blk_start,
795 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900796
Gu Zhenge4795562013-09-27 18:08:30 +0800797 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900798 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800799 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900800 }
801 }
802
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900803 return ret;
804}
805
Chao Yub4ace332015-05-06 13:09:46 +0800806static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
807{
808 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
809 struct dnode_of_data dn;
810 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
811 int ret = 0;
812
Chao Yub4ace332015-05-06 13:09:46 +0800813 for (; end < nrpages; start++, end++) {
814 block_t new_addr, old_addr;
815
Chao Yuecbaa402015-07-16 18:18:11 +0800816 f2fs_lock_op(sbi);
817
Chao Yub4ace332015-05-06 13:09:46 +0800818 set_new_dnode(&dn, inode, NULL, NULL, 0);
819 ret = get_dnode_of_data(&dn, end, LOOKUP_NODE_RA);
820 if (ret && ret != -ENOENT) {
821 goto out;
822 } else if (ret == -ENOENT) {
823 new_addr = NULL_ADDR;
824 } else {
825 new_addr = dn.data_blkaddr;
826 truncate_data_blocks_range(&dn, 1);
827 f2fs_put_dnode(&dn);
828 }
829
830 if (new_addr == NULL_ADDR) {
831 set_new_dnode(&dn, inode, NULL, NULL, 0);
832 ret = get_dnode_of_data(&dn, start, LOOKUP_NODE_RA);
Chao Yuecbaa402015-07-16 18:18:11 +0800833 if (ret && ret != -ENOENT) {
Chao Yub4ace332015-05-06 13:09:46 +0800834 goto out;
Chao Yuecbaa402015-07-16 18:18:11 +0800835 } else if (ret == -ENOENT) {
836 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +0800837 continue;
Chao Yuecbaa402015-07-16 18:18:11 +0800838 }
Chao Yub4ace332015-05-06 13:09:46 +0800839
840 if (dn.data_blkaddr == NULL_ADDR) {
841 f2fs_put_dnode(&dn);
Chao Yuecbaa402015-07-16 18:18:11 +0800842 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +0800843 continue;
844 } else {
845 truncate_data_blocks_range(&dn, 1);
846 }
847
848 f2fs_put_dnode(&dn);
849 } else {
850 struct page *ipage;
851
852 ipage = get_node_page(sbi, inode->i_ino);
853 if (IS_ERR(ipage)) {
854 ret = PTR_ERR(ipage);
855 goto out;
856 }
857
858 set_new_dnode(&dn, inode, ipage, NULL, 0);
859 ret = f2fs_reserve_block(&dn, start);
860 if (ret)
861 goto out;
862
863 old_addr = dn.data_blkaddr;
864 if (old_addr != NEW_ADDR && new_addr == NEW_ADDR) {
865 dn.data_blkaddr = NULL_ADDR;
866 f2fs_update_extent_cache(&dn);
867 invalidate_blocks(sbi, old_addr);
868
869 dn.data_blkaddr = new_addr;
870 set_data_blkaddr(&dn);
871 } else if (new_addr != NEW_ADDR) {
872 struct node_info ni;
Chao Yub4ace332015-05-06 13:09:46 +0800873
874 get_node_info(sbi, dn.nid, &ni);
Chao Yu528e3452015-05-28 19:15:35 +0800875 f2fs_replace_block(sbi, &dn, old_addr, new_addr,
876 ni.version, true);
Chao Yub4ace332015-05-06 13:09:46 +0800877 }
878
879 f2fs_put_dnode(&dn);
880 }
Chao Yuecbaa402015-07-16 18:18:11 +0800881 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +0800882 }
Chao Yuecbaa402015-07-16 18:18:11 +0800883 return 0;
Chao Yub4ace332015-05-06 13:09:46 +0800884out:
885 f2fs_unlock_op(sbi);
886 return ret;
887}
888
889static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
890{
891 pgoff_t pg_start, pg_end;
892 loff_t new_size;
893 int ret;
894
895 if (!S_ISREG(inode->i_mode))
896 return -EINVAL;
897
898 if (offset + len >= i_size_read(inode))
899 return -EINVAL;
900
901 /* collapse range should be aligned to block size of f2fs. */
902 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
903 return -EINVAL;
904
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -0700905 f2fs_balance_fs(F2FS_I_SB(inode));
906
907 if (f2fs_has_inline_data(inode)) {
908 ret = f2fs_convert_inline_inode(inode);
909 if (ret)
910 return ret;
911 }
912
Chao Yub4ace332015-05-06 13:09:46 +0800913 pg_start = offset >> PAGE_CACHE_SHIFT;
914 pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
915
916 /* write out all dirty pages from offset */
917 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
918 if (ret)
919 return ret;
920
921 truncate_pagecache(inode, offset);
922
923 ret = f2fs_do_collapse(inode, pg_start, pg_end);
924 if (ret)
925 return ret;
926
927 new_size = i_size_read(inode) - len;
928
929 ret = truncate_blocks(inode, new_size, true);
930 if (!ret)
931 i_size_write(inode, new_size);
932
933 return ret;
934}
935
Chao Yu75cd4e02015-05-06 13:11:13 +0800936static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
937 int mode)
938{
939 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
940 struct address_space *mapping = inode->i_mapping;
941 pgoff_t index, pg_start, pg_end;
942 loff_t new_size = i_size_read(inode);
943 loff_t off_start, off_end;
944 int ret = 0;
945
946 if (!S_ISREG(inode->i_mode))
947 return -EINVAL;
948
949 ret = inode_newsize_ok(inode, (len + offset));
950 if (ret)
951 return ret;
952
953 f2fs_balance_fs(sbi);
954
955 if (f2fs_has_inline_data(inode)) {
956 ret = f2fs_convert_inline_inode(inode);
957 if (ret)
958 return ret;
959 }
960
961 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
962 if (ret)
963 return ret;
964
965 truncate_pagecache_range(inode, offset, offset + len - 1);
966
967 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
968 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
969
970 off_start = offset & (PAGE_CACHE_SIZE - 1);
971 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
972
973 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800974 ret = fill_zero(inode, pg_start, off_start,
975 off_end - off_start);
976 if (ret)
977 return ret;
978
Chao Yu75cd4e02015-05-06 13:11:13 +0800979 if (offset + len > new_size)
980 new_size = offset + len;
981 new_size = max_t(loff_t, new_size, offset + len);
982 } else {
983 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +0800984 ret = fill_zero(inode, pg_start++, off_start,
985 PAGE_CACHE_SIZE - off_start);
986 if (ret)
987 return ret;
988
Chao Yu75cd4e02015-05-06 13:11:13 +0800989 new_size = max_t(loff_t, new_size,
990 pg_start << PAGE_CACHE_SHIFT);
991 }
992
993 for (index = pg_start; index < pg_end; index++) {
994 struct dnode_of_data dn;
995 struct page *ipage;
996
997 f2fs_lock_op(sbi);
998
999 ipage = get_node_page(sbi, inode->i_ino);
1000 if (IS_ERR(ipage)) {
1001 ret = PTR_ERR(ipage);
1002 f2fs_unlock_op(sbi);
1003 goto out;
1004 }
1005
1006 set_new_dnode(&dn, inode, ipage, NULL, 0);
1007 ret = f2fs_reserve_block(&dn, index);
1008 if (ret) {
1009 f2fs_unlock_op(sbi);
1010 goto out;
1011 }
1012
1013 if (dn.data_blkaddr != NEW_ADDR) {
1014 invalidate_blocks(sbi, dn.data_blkaddr);
1015
1016 dn.data_blkaddr = NEW_ADDR;
1017 set_data_blkaddr(&dn);
1018
1019 dn.data_blkaddr = NULL_ADDR;
1020 f2fs_update_extent_cache(&dn);
1021 }
1022 f2fs_put_dnode(&dn);
1023 f2fs_unlock_op(sbi);
1024
1025 new_size = max_t(loff_t, new_size,
1026 (index + 1) << PAGE_CACHE_SHIFT);
1027 }
1028
1029 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001030 ret = fill_zero(inode, pg_end, 0, off_end);
1031 if (ret)
1032 goto out;
1033
Chao Yu75cd4e02015-05-06 13:11:13 +08001034 new_size = max_t(loff_t, new_size, offset + len);
1035 }
1036 }
1037
1038out:
1039 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) {
1040 i_size_write(inode, new_size);
1041 mark_inode_dirty(inode);
1042 update_inode_page(inode);
1043 }
1044
1045 return ret;
1046}
1047
Chao Yuf62185d2015-05-28 19:16:57 +08001048static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1049{
1050 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1051 pgoff_t pg_start, pg_end, delta, nrpages, idx;
1052 loff_t new_size;
1053 int ret;
1054
1055 if (!S_ISREG(inode->i_mode))
1056 return -EINVAL;
1057
1058 new_size = i_size_read(inode) + len;
1059 if (new_size > inode->i_sb->s_maxbytes)
1060 return -EFBIG;
1061
1062 if (offset >= i_size_read(inode))
1063 return -EINVAL;
1064
1065 /* insert range should be aligned to block size of f2fs. */
1066 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1067 return -EINVAL;
1068
1069 f2fs_balance_fs(sbi);
1070
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001071 if (f2fs_has_inline_data(inode)) {
1072 ret = f2fs_convert_inline_inode(inode);
1073 if (ret)
1074 return ret;
1075 }
1076
Chao Yuf62185d2015-05-28 19:16:57 +08001077 ret = truncate_blocks(inode, i_size_read(inode), true);
1078 if (ret)
1079 return ret;
1080
1081 /* write out all dirty pages from offset */
1082 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1083 if (ret)
1084 return ret;
1085
1086 truncate_pagecache(inode, offset);
1087
1088 pg_start = offset >> PAGE_CACHE_SHIFT;
1089 pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
1090 delta = pg_end - pg_start;
1091 nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1092
1093 for (idx = nrpages - 1; idx >= pg_start && idx != -1; idx--) {
1094 struct dnode_of_data dn;
1095 struct page *ipage;
1096 block_t new_addr, old_addr;
1097
1098 f2fs_lock_op(sbi);
1099
1100 set_new_dnode(&dn, inode, NULL, NULL, 0);
1101 ret = get_dnode_of_data(&dn, idx, LOOKUP_NODE_RA);
1102 if (ret && ret != -ENOENT) {
1103 goto out;
1104 } else if (ret == -ENOENT) {
1105 goto next;
1106 } else if (dn.data_blkaddr == NULL_ADDR) {
1107 f2fs_put_dnode(&dn);
1108 goto next;
1109 } else {
1110 new_addr = dn.data_blkaddr;
1111 truncate_data_blocks_range(&dn, 1);
1112 f2fs_put_dnode(&dn);
1113 }
1114
1115 ipage = get_node_page(sbi, inode->i_ino);
1116 if (IS_ERR(ipage)) {
1117 ret = PTR_ERR(ipage);
1118 goto out;
1119 }
1120
1121 set_new_dnode(&dn, inode, ipage, NULL, 0);
1122 ret = f2fs_reserve_block(&dn, idx + delta);
1123 if (ret)
1124 goto out;
1125
1126 old_addr = dn.data_blkaddr;
1127 f2fs_bug_on(sbi, old_addr != NEW_ADDR);
1128
1129 if (new_addr != NEW_ADDR) {
1130 struct node_info ni;
1131
1132 get_node_info(sbi, dn.nid, &ni);
1133 f2fs_replace_block(sbi, &dn, old_addr, new_addr,
1134 ni.version, true);
1135 }
1136 f2fs_put_dnode(&dn);
1137next:
1138 f2fs_unlock_op(sbi);
1139 }
1140
1141 i_size_write(inode, new_size);
1142 return 0;
1143out:
1144 f2fs_unlock_op(sbi);
1145 return ret;
1146}
1147
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001148static int expand_inode_data(struct inode *inode, loff_t offset,
1149 loff_t len, int mode)
1150{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001151 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001152 pgoff_t index, pg_start, pg_end;
1153 loff_t new_size = i_size_read(inode);
1154 loff_t off_start, off_end;
1155 int ret = 0;
1156
Chao Yu497a0932014-08-04 10:11:17 +08001157 f2fs_balance_fs(sbi);
1158
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001159 ret = inode_newsize_ok(inode, (len + offset));
1160 if (ret)
1161 return ret;
1162
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001163 if (f2fs_has_inline_data(inode)) {
1164 ret = f2fs_convert_inline_inode(inode);
1165 if (ret)
1166 return ret;
1167 }
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001168
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001169 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
1170 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
1171
1172 off_start = offset & (PAGE_CACHE_SIZE - 1);
1173 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
1174
Jaegeuk Kimead43272014-06-13 13:05:55 +09001175 f2fs_lock_op(sbi);
1176
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001177 for (index = pg_start; index <= pg_end; index++) {
1178 struct dnode_of_data dn;
1179
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001180 if (index == pg_end && !off_end)
1181 goto noalloc;
1182
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001183 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001184 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +08001185 if (ret)
1186 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001187noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001188 if (pg_start == pg_end)
1189 new_size = offset + len;
1190 else if (index == pg_start && off_start)
1191 new_size = (index + 1) << PAGE_CACHE_SHIFT;
1192 else if (index == pg_end)
1193 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
1194 else
1195 new_size += PAGE_CACHE_SIZE;
1196 }
1197
1198 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
1199 i_size_read(inode) < new_size) {
1200 i_size_write(inode, new_size);
1201 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +09001202 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001203 }
Jaegeuk Kimead43272014-06-13 13:05:55 +09001204 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001205
1206 return ret;
1207}
1208
1209static long f2fs_fallocate(struct file *file, int mode,
1210 loff_t offset, loff_t len)
1211{
Al Viro6131ffa2013-02-27 16:59:05 -05001212 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001213 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001214
Chao Yuf62185d2015-05-28 19:16:57 +08001215 if (f2fs_encrypted_inode(inode) &&
1216 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001217 return -EOPNOTSUPP;
1218
Chao Yub4ace332015-05-06 13:09:46 +08001219 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001220 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1221 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001222 return -EOPNOTSUPP;
1223
Chao Yu3375f692014-01-28 10:29:26 +08001224 mutex_lock(&inode->i_mutex);
1225
Taehee Yoo587c0a42015-04-21 15:59:12 +09001226 if (mode & FALLOC_FL_PUNCH_HOLE) {
1227 if (offset >= inode->i_size)
1228 goto out;
1229
Chao Yua66c7b22013-11-22 16:52:50 +08001230 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001231 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1232 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001233 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1234 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001235 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1236 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001237 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001238 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001239 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001240
Namjae Jeon3af60a42012-12-30 14:52:37 +09001241 if (!ret) {
1242 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1243 mark_inode_dirty(inode);
1244 }
Chao Yu3375f692014-01-28 10:29:26 +08001245
Taehee Yoo587c0a42015-04-21 15:59:12 +09001246out:
Chao Yu3375f692014-01-28 10:29:26 +08001247 mutex_unlock(&inode->i_mutex);
1248
Namjae Jeonc01e2852013-04-23 17:00:52 +09001249 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001250 return ret;
1251}
1252
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001253static int f2fs_release_file(struct inode *inode, struct file *filp)
1254{
1255 /* some remained atomic pages should discarded */
1256 if (f2fs_is_atomic_file(inode))
1257 commit_inmem_pages(inode, true);
1258 if (f2fs_is_volatile_file(inode)) {
1259 set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1260 filemap_fdatawrite(inode->i_mapping);
1261 clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1262 }
1263 return 0;
1264}
1265
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001266#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1267#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1268
1269static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1270{
1271 if (S_ISDIR(mode))
1272 return flags;
1273 else if (S_ISREG(mode))
1274 return flags & F2FS_REG_FLMASK;
1275 else
1276 return flags & F2FS_OTHER_FLMASK;
1277}
1278
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001279static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001280{
Al Viro6131ffa2013-02-27 16:59:05 -05001281 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001282 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001283 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1284 return put_user(flags, (int __user *)arg);
1285}
1286
1287static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1288{
1289 struct inode *inode = file_inode(filp);
1290 struct f2fs_inode_info *fi = F2FS_I(inode);
1291 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1292 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001293 int ret;
1294
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001295 ret = mnt_want_write_file(filp);
1296 if (ret)
1297 return ret;
1298
1299 if (!inode_owner_or_capable(inode)) {
1300 ret = -EACCES;
1301 goto out;
1302 }
1303
1304 if (get_user(flags, (int __user *)arg)) {
1305 ret = -EFAULT;
1306 goto out;
1307 }
1308
1309 flags = f2fs_mask_flags(inode->i_mode, flags);
1310
1311 mutex_lock(&inode->i_mutex);
1312
1313 oldflags = fi->i_flags;
1314
1315 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1316 if (!capable(CAP_LINUX_IMMUTABLE)) {
1317 mutex_unlock(&inode->i_mutex);
1318 ret = -EPERM;
1319 goto out;
1320 }
1321 }
1322
1323 flags = flags & FS_FL_USER_MODIFIABLE;
1324 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1325 fi->i_flags = flags;
1326 mutex_unlock(&inode->i_mutex);
1327
1328 f2fs_set_inode_flags(inode);
1329 inode->i_ctime = CURRENT_TIME;
1330 mark_inode_dirty(inode);
1331out:
1332 mnt_drop_write_file(filp);
1333 return ret;
1334}
1335
Chao Yud49f3e82015-01-23 20:36:04 +08001336static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1337{
1338 struct inode *inode = file_inode(filp);
1339
1340 return put_user(inode->i_generation, (int __user *)arg);
1341}
1342
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001343static int f2fs_ioc_start_atomic_write(struct file *filp)
1344{
1345 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001346 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001347
1348 if (!inode_owner_or_capable(inode))
1349 return -EACCES;
1350
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001351 f2fs_balance_fs(F2FS_I_SB(inode));
1352
1353 if (f2fs_is_atomic_file(inode))
1354 return 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001355
Chao Yuf4c9c742015-07-17 18:06:35 +08001356 ret = f2fs_convert_inline_inode(inode);
1357 if (ret)
1358 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001359
Chao Yuf4c9c742015-07-17 18:06:35 +08001360 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1361 return 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001362}
1363
1364static int f2fs_ioc_commit_atomic_write(struct file *filp)
1365{
1366 struct inode *inode = file_inode(filp);
1367 int ret;
1368
1369 if (!inode_owner_or_capable(inode))
1370 return -EACCES;
1371
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001372 if (f2fs_is_volatile_file(inode))
1373 return 0;
1374
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001375 ret = mnt_want_write_file(filp);
1376 if (ret)
1377 return ret;
1378
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001379 if (f2fs_is_atomic_file(inode)) {
1380 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001381 ret = commit_inmem_pages(inode, false);
1382 if (ret)
1383 goto err_out;
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001384 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001385
Chao Yua5f64b62015-07-17 18:05:21 +08001386 ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001387err_out:
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001388 mnt_drop_write_file(filp);
1389 return ret;
1390}
1391
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001392static int f2fs_ioc_start_volatile_write(struct file *filp)
1393{
1394 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001395 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001396
1397 if (!inode_owner_or_capable(inode))
1398 return -EACCES;
1399
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001400 if (f2fs_is_volatile_file(inode))
1401 return 0;
1402
Chao Yuf4c9c742015-07-17 18:06:35 +08001403 ret = f2fs_convert_inline_inode(inode);
1404 if (ret)
1405 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001406
Chao Yuf4c9c742015-07-17 18:06:35 +08001407 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1408 return 0;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001409}
1410
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001411static int f2fs_ioc_release_volatile_write(struct file *filp)
1412{
1413 struct inode *inode = file_inode(filp);
1414
1415 if (!inode_owner_or_capable(inode))
1416 return -EACCES;
1417
1418 if (!f2fs_is_volatile_file(inode))
1419 return 0;
1420
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001421 if (!f2fs_is_first_block_written(inode))
1422 return truncate_partial_data_page(inode, 0, true);
1423
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001424 punch_hole(inode, 0, F2FS_BLKSIZE);
1425 return 0;
1426}
1427
1428static int f2fs_ioc_abort_volatile_write(struct file *filp)
1429{
1430 struct inode *inode = file_inode(filp);
1431 int ret;
1432
1433 if (!inode_owner_or_capable(inode))
1434 return -EACCES;
1435
1436 ret = mnt_want_write_file(filp);
1437 if (ret)
1438 return ret;
1439
1440 f2fs_balance_fs(F2FS_I_SB(inode));
1441
1442 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001443 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001444 commit_inmem_pages(inode, true);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001445 }
1446
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001447 if (f2fs_is_volatile_file(inode))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001448 clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001449
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001450 mnt_drop_write_file(filp);
1451 return ret;
1452}
1453
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001454static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1455{
1456 struct inode *inode = file_inode(filp);
1457 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1458 struct super_block *sb = sbi->sb;
1459 __u32 in;
1460
1461 if (!capable(CAP_SYS_ADMIN))
1462 return -EPERM;
1463
1464 if (get_user(in, (__u32 __user *)arg))
1465 return -EFAULT;
1466
1467 switch (in) {
1468 case F2FS_GOING_DOWN_FULLSYNC:
1469 sb = freeze_bdev(sb->s_bdev);
1470 if (sb && !IS_ERR(sb)) {
1471 f2fs_stop_checkpoint(sbi);
1472 thaw_bdev(sb->s_bdev, sb);
1473 }
1474 break;
1475 case F2FS_GOING_DOWN_METASYNC:
1476 /* do checkpoint only */
1477 f2fs_sync_fs(sb, 1);
1478 f2fs_stop_checkpoint(sbi);
1479 break;
1480 case F2FS_GOING_DOWN_NOSYNC:
1481 f2fs_stop_checkpoint(sbi);
1482 break;
1483 default:
1484 return -EINVAL;
1485 }
1486 return 0;
1487}
1488
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001489static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1490{
1491 struct inode *inode = file_inode(filp);
1492 struct super_block *sb = inode->i_sb;
1493 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1494 struct fstrim_range range;
1495 int ret;
1496
1497 if (!capable(CAP_SYS_ADMIN))
1498 return -EPERM;
1499
1500 if (!blk_queue_discard(q))
1501 return -EOPNOTSUPP;
1502
1503 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1504 sizeof(range)))
1505 return -EFAULT;
1506
1507 range.minlen = max((unsigned int)range.minlen,
1508 q->limits.discard_granularity);
1509 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1510 if (ret < 0)
1511 return ret;
1512
1513 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1514 sizeof(range)))
1515 return -EFAULT;
1516 return 0;
1517}
1518
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001519static bool uuid_is_nonzero(__u8 u[16])
1520{
1521 int i;
1522
1523 for (i = 0; i < 16; i++)
1524 if (u[i])
1525 return true;
1526 return false;
1527}
1528
1529static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1530{
1531#ifdef CONFIG_F2FS_FS_ENCRYPTION
1532 struct f2fs_encryption_policy policy;
1533 struct inode *inode = file_inode(filp);
1534
1535 if (copy_from_user(&policy, (struct f2fs_encryption_policy __user *)arg,
1536 sizeof(policy)))
1537 return -EFAULT;
1538
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001539 return f2fs_process_policy(&policy, inode);
1540#else
1541 return -EOPNOTSUPP;
1542#endif
1543}
1544
1545static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1546{
1547#ifdef CONFIG_F2FS_FS_ENCRYPTION
1548 struct f2fs_encryption_policy policy;
1549 struct inode *inode = file_inode(filp);
1550 int err;
1551
1552 err = f2fs_get_policy(inode, &policy);
1553 if (err)
1554 return err;
1555
1556 if (copy_to_user((struct f2fs_encryption_policy __user *)arg, &policy,
1557 sizeof(policy)))
1558 return -EFAULT;
1559 return 0;
1560#else
1561 return -EOPNOTSUPP;
1562#endif
1563}
1564
1565static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1566{
1567 struct inode *inode = file_inode(filp);
1568 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1569 int err;
1570
1571 if (!f2fs_sb_has_crypto(inode->i_sb))
1572 return -EOPNOTSUPP;
1573
1574 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1575 goto got_it;
1576
1577 err = mnt_want_write_file(filp);
1578 if (err)
1579 return err;
1580
1581 /* update superblock with uuid */
1582 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1583
Chao Yuc5bda1c2015-06-08 13:28:03 +08001584 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001585
1586 mnt_drop_write_file(filp);
1587 if (err) {
1588 /* undo new data */
1589 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1590 return err;
1591 }
1592got_it:
1593 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1594 16))
1595 return -EFAULT;
1596 return 0;
1597}
1598
Chao Yuc1c1b582015-07-10 18:08:10 +08001599static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1600{
1601 struct inode *inode = file_inode(filp);
1602 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1603 __u32 i, count;
1604
1605 if (!capable(CAP_SYS_ADMIN))
1606 return -EPERM;
1607
1608 if (get_user(count, (__u32 __user *)arg))
1609 return -EFAULT;
1610
1611 if (!count || count > F2FS_BATCH_GC_MAX_NUM)
1612 return -EINVAL;
1613
1614 for (i = 0; i < count; i++) {
1615 if (!mutex_trylock(&sbi->gc_mutex))
1616 break;
1617
1618 if (f2fs_gc(sbi))
1619 break;
1620 }
1621
1622 if (put_user(i, (__u32 __user *)arg))
1623 return -EFAULT;
1624
1625 return 0;
1626}
1627
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001628long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1629{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001630 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001631 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001632 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001633 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001634 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08001635 case F2FS_IOC_GETVERSION:
1636 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001637 case F2FS_IOC_START_ATOMIC_WRITE:
1638 return f2fs_ioc_start_atomic_write(filp);
1639 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1640 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001641 case F2FS_IOC_START_VOLATILE_WRITE:
1642 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001643 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1644 return f2fs_ioc_release_volatile_write(filp);
1645 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1646 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001647 case F2FS_IOC_SHUTDOWN:
1648 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001649 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001650 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001651 case F2FS_IOC_SET_ENCRYPTION_POLICY:
1652 return f2fs_ioc_set_encryption_policy(filp, arg);
1653 case F2FS_IOC_GET_ENCRYPTION_POLICY:
1654 return f2fs_ioc_get_encryption_policy(filp, arg);
1655 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
1656 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08001657 case F2FS_IOC_GARBAGE_COLLECT:
1658 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001659 default:
1660 return -ENOTTY;
1661 }
1662}
1663
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001664static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1665{
1666 struct inode *inode = file_inode(iocb->ki_filp);
1667
1668 if (f2fs_encrypted_inode(inode) &&
1669 !f2fs_has_encryption_key(inode) &&
1670 f2fs_get_encryption_info(inode))
1671 return -EACCES;
1672
1673 return generic_file_write_iter(iocb, from);
1674}
1675
Namjae Jeone9750822013-02-04 23:41:41 +09001676#ifdef CONFIG_COMPAT
1677long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1678{
1679 switch (cmd) {
1680 case F2FS_IOC32_GETFLAGS:
1681 cmd = F2FS_IOC_GETFLAGS;
1682 break;
1683 case F2FS_IOC32_SETFLAGS:
1684 cmd = F2FS_IOC_SETFLAGS;
1685 break;
1686 default:
1687 return -ENOIOCTLCMD;
1688 }
1689 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1690}
1691#endif
1692
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001693const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001694 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001695 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001696 .write_iter = f2fs_file_write_iter,
1697 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08001698 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001699 .mmap = f2fs_file_mmap,
1700 .fsync = f2fs_sync_file,
1701 .fallocate = f2fs_fallocate,
1702 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001703#ifdef CONFIG_COMPAT
1704 .compat_ioctl = f2fs_compat_ioctl,
1705#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001706 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001707 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001708};