blob: 98dac27bc3f733975fabc96404ab30a916b20e7c [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 Kimfbfa2cc2012-11-02 17:09:44 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
27#include "xattr.h"
28#include "acl.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080029#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090030#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090031
32static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
33 struct vm_fault *vmf)
34{
35 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050036 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070037 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090038 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080039 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090040
41 f2fs_balance_fs(sbi);
42
43 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070044
45 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba12014-08-07 16:32:25 -070046
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090047 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080048 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070049 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070050 err = f2fs_reserve_block(&dn, page->index);
51 if (err) {
52 f2fs_unlock_op(sbi);
53 goto out;
54 }
55 f2fs_put_dnode(&dn);
56 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090057
Namjae Jeon9851e6e2013-04-28 09:04:18 +090058 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090059 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090060 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090061 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090062 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090063 unlock_page(page);
64 err = -EFAULT;
65 goto out;
66 }
67
68 /*
69 * check to see if the page is mapped already (no holes)
70 */
71 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090072 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090073
74 /* page is wholly or partially inside EOF */
75 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
76 unsigned offset;
77 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
78 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
79 }
80 set_page_dirty(page);
81 SetPageUptodate(page);
82
Jaegeuk Kime943a102013-10-25 14:26:31 +090083 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090084mapped:
85 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090086 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090087out:
88 sb_end_pagefault(inode->i_sb);
89 return block_page_mkwrite_return(err);
90}
91
92static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090093 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070094 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090095 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090096};
97
Jaegeuk Kim354a3392013-06-14 08:52:35 +090098static int get_parent_ino(struct inode *inode, nid_t *pino)
99{
100 struct dentry *dentry;
101
102 inode = igrab(inode);
103 dentry = d_find_any_alias(inode);
104 iput(inode);
105 if (!dentry)
106 return 0;
107
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900108 if (update_dent_inode(inode, &dentry->d_name)) {
109 dput(dentry);
110 return 0;
111 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900112
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900113 *pino = parent_ino(dentry);
114 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900115 return 1;
116}
117
Chao Yu9d1589e2014-08-20 18:37:35 +0800118static inline bool need_do_checkpoint(struct inode *inode)
119{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700120 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800121 bool need_cp = false;
122
123 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
124 need_cp = true;
125 else if (file_wrong_pino(inode))
126 need_cp = true;
127 else if (!space_for_roll_forward(sbi))
128 need_cp = true;
129 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
130 need_cp = true;
131 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
132 need_cp = true;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700133 else if (test_opt(sbi, FASTBOOT))
134 need_cp = true;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800135 else if (sbi->active_logs == 2)
136 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800137
138 return need_cp;
139}
140
Changman Lee9c7bb702014-12-08 15:29:40 +0900141static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
142{
143 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
144 bool ret = false;
145 /* But we need to avoid that there are some inode updates */
146 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
147 ret = true;
148 f2fs_put_page(i, 0);
149 return ret;
150}
151
Changman Lee51455b12014-12-08 15:29:41 +0900152static void try_to_fix_pino(struct inode *inode)
153{
154 struct f2fs_inode_info *fi = F2FS_I(inode);
155 nid_t pino;
156
157 down_write(&fi->i_sem);
158 fi->xattr_ver = 0;
159 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
160 get_parent_ino(inode, &pino)) {
161 fi->i_pino = pino;
162 file_got_pino(inode);
163 up_write(&fi->i_sem);
164
165 mark_inode_dirty_sync(inode);
166 f2fs_write_inode(inode, NULL);
167 } else {
168 up_write(&fi->i_sem);
169 }
170}
171
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900172int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
173{
174 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900175 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700176 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700177 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900178 int ret = 0;
179 bool need_cp = false;
180 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900181 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900182 .nr_to_write = LONG_MAX,
183 .for_reclaim = 0,
184 };
185
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900186 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900187 return 0;
188
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900189 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700190
191 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700192 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700193 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900194 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700195 clear_inode_flag(fi, FI_NEED_IPU);
196
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900197 if (ret) {
198 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900199 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900200 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900201
Changman Lee9c7bb702014-12-08 15:29:40 +0900202 /* if the inode is dirty, let's recover all the time */
203 if (!datasync && is_inode_flag_set(fi, FI_DIRTY_INODE)) {
204 update_inode_page(inode);
205 goto go_write;
206 }
207
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700208 /*
209 * if there is no written data, don't waste time to write recovery info.
210 */
211 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700212 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700213
Changman Lee9c7bb702014-12-08 15:29:40 +0900214 /* it may call write_inode just prior to fsync */
215 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700216 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700217
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700218 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700219 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700220 goto flush_out;
221 goto out;
222 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700223go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900224 /* guarantee free sections for fsync */
225 f2fs_balance_fs(sbi);
226
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900227 /*
228 * Both of fdatasync() and fsync() are able to be recovered from
229 * sudden-power-off.
230 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800231 down_read(&fi->i_sem);
232 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900233 up_read(&fi->i_sem);
234
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900235 if (need_cp) {
236 /* all the dirty node pages should be flushed for POR */
237 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900238
Changman Lee51455b12014-12-08 15:29:41 +0900239 /*
240 * We've secured consistency through sync_fs. Following pino
241 * will be used only for fsynced inodes after checkpoint.
242 */
243 try_to_fix_pino(inode);
244 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900245 }
Changman Lee51455b12014-12-08 15:29:41 +0900246sync_nodes:
247 sync_node_pages(sbi, ino, &wbc);
248
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800249 /* if cp_error was enabled, we should avoid infinite loop */
250 if (unlikely(f2fs_cp_error(sbi)))
251 goto out;
252
Changman Lee51455b12014-12-08 15:29:41 +0900253 if (need_inode_block_update(sbi, ino)) {
254 mark_inode_dirty_sync(inode);
255 f2fs_write_inode(inode, NULL);
256 goto sync_nodes;
257 }
258
259 ret = wait_on_node_pages_writeback(sbi, ino);
260 if (ret)
261 goto out;
262
263 /* once recovery info is written, don't need to tack this */
264 remove_dirty_inode(sbi, ino, APPEND_INO);
265 clear_inode_flag(fi, FI_APPEND_WRITE);
266flush_out:
267 remove_dirty_inode(sbi, ino, UPDATE_INO);
268 clear_inode_flag(fi, FI_UPDATE_WRITE);
269 ret = f2fs_issue_flush(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900270out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900271 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -0800272 f2fs_trace_ios(NULL, NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900273 return ret;
274}
275
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900276static pgoff_t __get_first_dirty_index(struct address_space *mapping,
277 pgoff_t pgofs, int whence)
278{
279 struct pagevec pvec;
280 int nr_pages;
281
282 if (whence != SEEK_DATA)
283 return 0;
284
285 /* find first dirty page index */
286 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700287 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
288 PAGECACHE_TAG_DIRTY, 1);
289 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900290 pagevec_release(&pvec);
291 return pgofs;
292}
293
294static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
295 int whence)
296{
297 switch (whence) {
298 case SEEK_DATA:
299 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
300 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
301 return true;
302 break;
303 case SEEK_HOLE:
304 if (blkaddr == NULL_ADDR)
305 return true;
306 break;
307 }
308 return false;
309}
310
Chao Yu267378d2014-04-23 14:10:24 +0800311static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
312{
313 struct inode *inode = file->f_mapping->host;
314 loff_t maxbytes = inode->i_sb->s_maxbytes;
315 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900316 pgoff_t pgofs, end_offset, dirty;
317 loff_t data_ofs = offset;
318 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800319 int err = 0;
320
321 mutex_lock(&inode->i_mutex);
322
323 isize = i_size_read(inode);
324 if (offset >= isize)
325 goto fail;
326
327 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800328 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800329 if (whence == SEEK_HOLE)
330 data_ofs = isize;
331 goto found;
332 }
333
334 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
335
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900336 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
337
Chao Yu267378d2014-04-23 14:10:24 +0800338 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
339 set_new_dnode(&dn, inode, NULL, NULL, 0);
340 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
341 if (err && err != -ENOENT) {
342 goto fail;
343 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900344 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800345 if (whence == SEEK_DATA) {
346 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
347 F2FS_I(inode));
348 continue;
349 } else {
350 goto found;
351 }
352 }
353
Chao Yu6b2920a2014-07-07 11:21:59 +0800354 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800355
356 /* find data/hole in dnode block */
357 for (; dn.ofs_in_node < end_offset;
358 dn.ofs_in_node++, pgofs++,
Chao Yu2e023172015-02-09 11:23:58 +0800359 data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800360 block_t blkaddr;
361 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
362
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900363 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800364 f2fs_put_dnode(&dn);
365 goto found;
366 }
367 }
368 f2fs_put_dnode(&dn);
369 }
370
371 if (whence == SEEK_DATA)
372 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800373found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900374 if (whence == SEEK_HOLE && data_ofs > isize)
375 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800376 mutex_unlock(&inode->i_mutex);
377 return vfs_setpos(file, data_ofs, maxbytes);
378fail:
379 mutex_unlock(&inode->i_mutex);
380 return -ENXIO;
381}
382
383static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
384{
385 struct inode *inode = file->f_mapping->host;
386 loff_t maxbytes = inode->i_sb->s_maxbytes;
387
388 switch (whence) {
389 case SEEK_SET:
390 case SEEK_CUR:
391 case SEEK_END:
392 return generic_file_llseek_size(file, offset, whence,
393 maxbytes, i_size_read(inode));
394 case SEEK_DATA:
395 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700396 if (offset < 0)
397 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800398 return f2fs_seek_block(file, offset, whence);
399 }
400
401 return -EINVAL;
402}
403
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900404static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
405{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700406 struct inode *inode = file_inode(file);
407
408 /* we don't need to use inline_data strictly */
409 if (f2fs_has_inline_data(inode)) {
410 int err = f2fs_convert_inline_inode(inode);
411 if (err)
412 return err;
413 }
414
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900415 file_accessed(file);
416 vma->vm_ops = &f2fs_file_vm_ops;
417 return 0;
418}
419
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900420int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900421{
422 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700423 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900424 struct f2fs_node *raw_node;
425 __le32 *addr;
426
Gu Zheng45590712013-07-15 17:57:38 +0800427 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900428 addr = blkaddr_in_node(raw_node) + ofs;
429
Chris Fries6c311ec2014-01-17 14:44:39 -0600430 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900431 block_t blkaddr = le32_to_cpu(*addr);
432 if (blkaddr == NULL_ADDR)
433 continue;
434
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800435 dn->data_blkaddr = NULL_ADDR;
436 update_extent_cache(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900437 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900438 nr_free++;
439 }
440 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900441 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900442 set_page_dirty(dn->node_page);
443 sync_inode_page(dn);
444 }
445 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900446
447 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
448 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900449 return nr_free;
450}
451
452void truncate_data_blocks(struct dnode_of_data *dn)
453{
454 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
455}
456
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700457static int truncate_partial_data_page(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900458{
459 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
460 struct page *page;
461
462 if (!offset)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700463 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900464
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900465 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900466 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700467 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900468
469 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800470 if (unlikely(!PageUptodate(page) ||
471 page->mapping != inode->i_mapping))
472 goto out;
473
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900474 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900475 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
476 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800477out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900478 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700479 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900480}
481
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700482int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900483{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700484 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900485 unsigned int blocksize = inode->i_sb->s_blocksize;
486 struct dnode_of_data dn;
487 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800488 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700489 struct page *ipage;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900490
Namjae Jeon51dd6242013-04-20 01:28:52 +0900491 trace_f2fs_truncate_blocks_enter(inode, from);
492
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800493 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900494
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700495 if (lock)
496 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800497
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700498 ipage = get_node_page(sbi, inode->i_ino);
499 if (IS_ERR(ipage)) {
500 err = PTR_ERR(ipage);
501 goto out;
502 }
503
504 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700505 f2fs_put_page(ipage, 1);
506 goto out;
507 }
508
509 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900510 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900511 if (err) {
512 if (err == -ENOENT)
513 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700514 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700515 }
516
Chao Yu6403eb12014-04-26 19:59:52 +0800517 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900518
519 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700520 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900521
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
523 truncate_data_blocks_range(&dn, count);
524 free_from += count;
525 }
526
527 f2fs_put_dnode(&dn);
528free_next:
529 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800530out:
531 if (lock)
532 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700533
534 /* lastly zero out the first data page */
535 if (!err)
536 err = truncate_partial_data_page(inode, from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900537
Namjae Jeon51dd6242013-04-20 01:28:52 +0900538 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900539 return err;
540}
541
542void f2fs_truncate(struct inode *inode)
543{
544 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
545 S_ISLNK(inode->i_mode)))
546 return;
547
Namjae Jeon51dd6242013-04-20 01:28:52 +0900548 trace_f2fs_truncate(inode);
549
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800550 /* we should check inline_data size */
551 if (f2fs_has_inline_data(inode) && !f2fs_may_inline(inode)) {
552 if (f2fs_convert_inline_inode(inode))
553 return;
554 }
555
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700556 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900557 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
558 mark_inode_dirty(inode);
559 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900560}
561
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900562int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900563 struct dentry *dentry, struct kstat *stat)
564{
565 struct inode *inode = dentry->d_inode;
566 generic_fillattr(inode, stat);
567 stat->blocks <<= 3;
568 return 0;
569}
570
571#ifdef CONFIG_F2FS_FS_POSIX_ACL
572static void __setattr_copy(struct inode *inode, const struct iattr *attr)
573{
574 struct f2fs_inode_info *fi = F2FS_I(inode);
575 unsigned int ia_valid = attr->ia_valid;
576
577 if (ia_valid & ATTR_UID)
578 inode->i_uid = attr->ia_uid;
579 if (ia_valid & ATTR_GID)
580 inode->i_gid = attr->ia_gid;
581 if (ia_valid & ATTR_ATIME)
582 inode->i_atime = timespec_trunc(attr->ia_atime,
583 inode->i_sb->s_time_gran);
584 if (ia_valid & ATTR_MTIME)
585 inode->i_mtime = timespec_trunc(attr->ia_mtime,
586 inode->i_sb->s_time_gran);
587 if (ia_valid & ATTR_CTIME)
588 inode->i_ctime = timespec_trunc(attr->ia_ctime,
589 inode->i_sb->s_time_gran);
590 if (ia_valid & ATTR_MODE) {
591 umode_t mode = attr->ia_mode;
592
593 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
594 mode &= ~S_ISGID;
595 set_acl_inode(fi, mode);
596 }
597}
598#else
599#define __setattr_copy setattr_copy
600#endif
601
602int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
603{
604 struct inode *inode = dentry->d_inode;
605 struct f2fs_inode_info *fi = F2FS_I(inode);
606 int err;
607
608 err = inode_change_ok(inode, attr);
609 if (err)
610 return err;
611
Chao Yu09db6a22014-09-15 18:02:09 +0800612 if (attr->ia_valid & ATTR_SIZE) {
Chao Yu09db6a22014-09-15 18:02:09 +0800613 if (attr->ia_size != i_size_read(inode)) {
614 truncate_setsize(inode, attr->ia_size);
615 f2fs_truncate(inode);
616 f2fs_balance_fs(F2FS_I_SB(inode));
617 } else {
618 /*
619 * giving a chance to truncate blocks past EOF which
620 * are fallocated with FALLOC_FL_KEEP_SIZE.
621 */
622 f2fs_truncate(inode);
623 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900624 }
625
626 __setattr_copy(inode, attr);
627
628 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800629 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900630 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
631 inode->i_mode = fi->i_acl_mode;
632 clear_inode_flag(fi, FI_ACL_MODE);
633 }
634 }
635
636 mark_inode_dirty(inode);
637 return err;
638}
639
640const struct inode_operations f2fs_file_inode_operations = {
641 .getattr = f2fs_getattr,
642 .setattr = f2fs_setattr,
643 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800644 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900645#ifdef CONFIG_F2FS_FS_XATTR
646 .setxattr = generic_setxattr,
647 .getxattr = generic_getxattr,
648 .listxattr = f2fs_listxattr,
649 .removexattr = generic_removexattr,
650#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900651 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900652};
653
654static void fill_zero(struct inode *inode, pgoff_t index,
655 loff_t start, loff_t len)
656{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700657 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900658 struct page *page;
659
660 if (!len)
661 return;
662
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900663 f2fs_balance_fs(sbi);
664
Gu Zhenge4795562013-09-27 18:08:30 +0800665 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900666 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800667 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668
669 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900670 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900671 zero_user(page, start, len);
672 set_page_dirty(page);
673 f2fs_put_page(page, 1);
674 }
675}
676
677int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
678{
679 pgoff_t index;
680 int err;
681
682 for (index = pg_start; index < pg_end; index++) {
683 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900684
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900685 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900686 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900687 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900688 if (err == -ENOENT)
689 continue;
690 return err;
691 }
692
693 if (dn.data_blkaddr != NULL_ADDR)
694 truncate_data_blocks_range(&dn, 1);
695 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900696 }
697 return 0;
698}
699
Chao Yua66c7b22013-11-22 16:52:50 +0800700static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900701{
702 pgoff_t pg_start, pg_end;
703 loff_t off_start, off_end;
704 int ret = 0;
705
Chao Yu14cecc52014-09-15 18:03:32 +0800706 if (!S_ISREG(inode->i_mode))
707 return -EOPNOTSUPP;
708
709 /* skip punching hole beyond i_size */
710 if (offset >= inode->i_size)
711 return ret;
712
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700713 if (f2fs_has_inline_data(inode)) {
714 ret = f2fs_convert_inline_inode(inode);
715 if (ret)
716 return ret;
717 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800718
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
720 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
721
722 off_start = offset & (PAGE_CACHE_SIZE - 1);
723 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
724
725 if (pg_start == pg_end) {
726 fill_zero(inode, pg_start, off_start,
727 off_end - off_start);
728 } else {
729 if (off_start)
730 fill_zero(inode, pg_start++, off_start,
731 PAGE_CACHE_SIZE - off_start);
732 if (off_end)
733 fill_zero(inode, pg_end, 0, off_end);
734
735 if (pg_start < pg_end) {
736 struct address_space *mapping = inode->i_mapping;
737 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700738 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500739
740 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900741
742 blk_start = pg_start << PAGE_CACHE_SHIFT;
743 blk_end = pg_end << PAGE_CACHE_SHIFT;
744 truncate_inode_pages_range(mapping, blk_start,
745 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900746
Gu Zhenge4795562013-09-27 18:08:30 +0800747 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900748 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800749 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900750 }
751 }
752
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900753 return ret;
754}
755
756static int expand_inode_data(struct inode *inode, loff_t offset,
757 loff_t len, int mode)
758{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700759 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900760 pgoff_t index, pg_start, pg_end;
761 loff_t new_size = i_size_read(inode);
762 loff_t off_start, off_end;
763 int ret = 0;
764
Chao Yu497a0932014-08-04 10:11:17 +0800765 f2fs_balance_fs(sbi);
766
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900767 ret = inode_newsize_ok(inode, (len + offset));
768 if (ret)
769 return ret;
770
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700771 if (f2fs_has_inline_data(inode)) {
772 ret = f2fs_convert_inline_inode(inode);
773 if (ret)
774 return ret;
775 }
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900776
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900777 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
778 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
779
780 off_start = offset & (PAGE_CACHE_SIZE - 1);
781 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
782
Jaegeuk Kimead43272014-06-13 13:05:55 +0900783 f2fs_lock_op(sbi);
784
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900785 for (index = pg_start; index <= pg_end; index++) {
786 struct dnode_of_data dn;
787
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900788 if (index == pg_end && !off_end)
789 goto noalloc;
790
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900791 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800792 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800793 if (ret)
794 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900795noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900796 if (pg_start == pg_end)
797 new_size = offset + len;
798 else if (index == pg_start && off_start)
799 new_size = (index + 1) << PAGE_CACHE_SHIFT;
800 else if (index == pg_end)
801 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
802 else
803 new_size += PAGE_CACHE_SIZE;
804 }
805
806 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
807 i_size_read(inode) < new_size) {
808 i_size_write(inode, new_size);
809 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900810 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900811 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900812 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900813
814 return ret;
815}
816
817static long f2fs_fallocate(struct file *file, int mode,
818 loff_t offset, loff_t len)
819{
Al Viro6131ffa2013-02-27 16:59:05 -0500820 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900821 long ret;
822
823 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
824 return -EOPNOTSUPP;
825
Chao Yu3375f692014-01-28 10:29:26 +0800826 mutex_lock(&inode->i_mutex);
827
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900828 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800829 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900830 else
831 ret = expand_inode_data(inode, offset, len, mode);
832
Namjae Jeon3af60a42012-12-30 14:52:37 +0900833 if (!ret) {
834 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
835 mark_inode_dirty(inode);
836 }
Chao Yu3375f692014-01-28 10:29:26 +0800837
838 mutex_unlock(&inode->i_mutex);
839
Namjae Jeonc01e2852013-04-23 17:00:52 +0900840 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900841 return ret;
842}
843
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800844static int f2fs_release_file(struct inode *inode, struct file *filp)
845{
846 /* some remained atomic pages should discarded */
847 if (f2fs_is_atomic_file(inode))
848 commit_inmem_pages(inode, true);
849 if (f2fs_is_volatile_file(inode)) {
850 set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
851 filemap_fdatawrite(inode->i_mapping);
852 clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
853 }
854 return 0;
855}
856
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900857#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
858#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
859
860static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
861{
862 if (S_ISDIR(mode))
863 return flags;
864 else if (S_ISREG(mode))
865 return flags & F2FS_REG_FLMASK;
866 else
867 return flags & F2FS_OTHER_FLMASK;
868}
869
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700870static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900871{
Al Viro6131ffa2013-02-27 16:59:05 -0500872 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900873 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700874 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
875 return put_user(flags, (int __user *)arg);
876}
877
878static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
879{
880 struct inode *inode = file_inode(filp);
881 struct f2fs_inode_info *fi = F2FS_I(inode);
882 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
883 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900884 int ret;
885
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700886 ret = mnt_want_write_file(filp);
887 if (ret)
888 return ret;
889
890 if (!inode_owner_or_capable(inode)) {
891 ret = -EACCES;
892 goto out;
893 }
894
895 if (get_user(flags, (int __user *)arg)) {
896 ret = -EFAULT;
897 goto out;
898 }
899
900 flags = f2fs_mask_flags(inode->i_mode, flags);
901
902 mutex_lock(&inode->i_mutex);
903
904 oldflags = fi->i_flags;
905
906 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
907 if (!capable(CAP_LINUX_IMMUTABLE)) {
908 mutex_unlock(&inode->i_mutex);
909 ret = -EPERM;
910 goto out;
911 }
912 }
913
914 flags = flags & FS_FL_USER_MODIFIABLE;
915 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
916 fi->i_flags = flags;
917 mutex_unlock(&inode->i_mutex);
918
919 f2fs_set_inode_flags(inode);
920 inode->i_ctime = CURRENT_TIME;
921 mark_inode_dirty(inode);
922out:
923 mnt_drop_write_file(filp);
924 return ret;
925}
926
Chao Yud49f3e82015-01-23 20:36:04 +0800927static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
928{
929 struct inode *inode = file_inode(filp);
930
931 return put_user(inode->i_generation, (int __user *)arg);
932}
933
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700934static int f2fs_ioc_start_atomic_write(struct file *filp)
935{
936 struct inode *inode = file_inode(filp);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700937
938 if (!inode_owner_or_capable(inode))
939 return -EACCES;
940
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800941 f2fs_balance_fs(F2FS_I_SB(inode));
942
943 if (f2fs_is_atomic_file(inode))
944 return 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700945
946 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
947
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700948 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700949}
950
951static int f2fs_ioc_commit_atomic_write(struct file *filp)
952{
953 struct inode *inode = file_inode(filp);
954 int ret;
955
956 if (!inode_owner_or_capable(inode))
957 return -EACCES;
958
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700959 if (f2fs_is_volatile_file(inode))
960 return 0;
961
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700962 ret = mnt_want_write_file(filp);
963 if (ret)
964 return ret;
965
966 if (f2fs_is_atomic_file(inode))
967 commit_inmem_pages(inode, false);
968
969 ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
970 mnt_drop_write_file(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800971 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700972 return ret;
973}
974
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700975static int f2fs_ioc_start_volatile_write(struct file *filp)
976{
977 struct inode *inode = file_inode(filp);
978
979 if (!inode_owner_or_capable(inode))
980 return -EACCES;
981
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800982 if (f2fs_is_volatile_file(inode))
983 return 0;
984
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700985 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700986
987 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700988}
989
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800990static int f2fs_ioc_release_volatile_write(struct file *filp)
991{
992 struct inode *inode = file_inode(filp);
993
994 if (!inode_owner_or_capable(inode))
995 return -EACCES;
996
997 if (!f2fs_is_volatile_file(inode))
998 return 0;
999
1000 punch_hole(inode, 0, F2FS_BLKSIZE);
1001 return 0;
1002}
1003
1004static int f2fs_ioc_abort_volatile_write(struct file *filp)
1005{
1006 struct inode *inode = file_inode(filp);
1007 int ret;
1008
1009 if (!inode_owner_or_capable(inode))
1010 return -EACCES;
1011
1012 ret = mnt_want_write_file(filp);
1013 if (ret)
1014 return ret;
1015
1016 f2fs_balance_fs(F2FS_I_SB(inode));
1017
1018 if (f2fs_is_atomic_file(inode)) {
1019 commit_inmem_pages(inode, false);
1020 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1021 }
1022
1023 if (f2fs_is_volatile_file(inode)) {
1024 clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1025 filemap_fdatawrite(inode->i_mapping);
1026 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1027 }
1028 mnt_drop_write_file(filp);
1029 return ret;
1030}
1031
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001032static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1033{
1034 struct inode *inode = file_inode(filp);
1035 struct super_block *sb = inode->i_sb;
1036 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1037 struct fstrim_range range;
1038 int ret;
1039
1040 if (!capable(CAP_SYS_ADMIN))
1041 return -EPERM;
1042
1043 if (!blk_queue_discard(q))
1044 return -EOPNOTSUPP;
1045
1046 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1047 sizeof(range)))
1048 return -EFAULT;
1049
1050 range.minlen = max((unsigned int)range.minlen,
1051 q->limits.discard_granularity);
1052 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1053 if (ret < 0)
1054 return ret;
1055
1056 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1057 sizeof(range)))
1058 return -EFAULT;
1059 return 0;
1060}
1061
1062long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1063{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001064 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001065 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001066 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001067 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001068 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08001069 case F2FS_IOC_GETVERSION:
1070 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001071 case F2FS_IOC_START_ATOMIC_WRITE:
1072 return f2fs_ioc_start_atomic_write(filp);
1073 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1074 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001075 case F2FS_IOC_START_VOLATILE_WRITE:
1076 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001077 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1078 return f2fs_ioc_release_volatile_write(filp);
1079 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1080 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001081 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001082 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001083 default:
1084 return -ENOTTY;
1085 }
1086}
1087
Namjae Jeone9750822013-02-04 23:41:41 +09001088#ifdef CONFIG_COMPAT
1089long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1090{
1091 switch (cmd) {
1092 case F2FS_IOC32_GETFLAGS:
1093 cmd = F2FS_IOC_GETFLAGS;
1094 break;
1095 case F2FS_IOC32_SETFLAGS:
1096 cmd = F2FS_IOC_SETFLAGS;
1097 break;
1098 default:
1099 return -ENOIOCTLCMD;
1100 }
1101 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1102}
1103#endif
1104
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001105const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001106 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001107 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -04001108 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -04001109 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04001110 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001111 .open = generic_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08001112 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001113 .mmap = f2fs_file_mmap,
1114 .fsync = f2fs_sync_file,
1115 .fallocate = f2fs_fallocate,
1116 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001117#ifdef CONFIG_COMPAT
1118 .compat_ioctl = f2fs_compat_ioctl,
1119#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001120 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001121 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001122};