blob: 0b97002163035fc7c7ce63933dd17ff705e2f823 [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"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090029#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090030
31static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
32 struct vm_fault *vmf)
33{
34 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050035 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070036 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090037 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080038 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090039
40 f2fs_balance_fs(sbi);
41
42 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070043
44 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070045
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090046 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080047 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070048 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070049 err = f2fs_reserve_block(&dn, page->index);
50 if (err) {
51 f2fs_unlock_op(sbi);
52 goto out;
53 }
54 f2fs_put_dnode(&dn);
55 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090056
Namjae Jeon9851e6e2013-04-28 09:04:18 +090057 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090058 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090059 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090060 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090061 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090062 unlock_page(page);
63 err = -EFAULT;
64 goto out;
65 }
66
67 /*
68 * check to see if the page is mapped already (no holes)
69 */
70 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090071 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090072
73 /* page is wholly or partially inside EOF */
74 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
75 unsigned offset;
76 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
77 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
78 }
79 set_page_dirty(page);
80 SetPageUptodate(page);
81
Jaegeuk Kime943a102013-10-25 14:26:31 +090082 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090083mapped:
84 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090085 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090086out:
87 sb_end_pagefault(inode->i_sb);
88 return block_page_mkwrite_return(err);
89}
90
91static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090092 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070093 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090094 .page_mkwrite = f2fs_vm_page_mkwrite,
95 .remap_pages = generic_file_remap_pages,
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
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900152int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
153{
154 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900155 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700156 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700157 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900158 int ret = 0;
159 bool need_cp = false;
160 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900161 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900162 .nr_to_write = LONG_MAX,
163 .for_reclaim = 0,
164 };
165
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900166 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900167 return 0;
168
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900169 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700170
171 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700172 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700173 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900174 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700175 clear_inode_flag(fi, FI_NEED_IPU);
176
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900177 if (ret) {
178 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900179 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900180 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900181
Changman Lee9c7bb702014-12-08 15:29:40 +0900182 /* if the inode is dirty, let's recover all the time */
183 if (!datasync && is_inode_flag_set(fi, FI_DIRTY_INODE)) {
184 update_inode_page(inode);
185 goto go_write;
186 }
187
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700188 /*
189 * if there is no written data, don't waste time to write recovery info.
190 */
191 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700192 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700193
Changman Lee9c7bb702014-12-08 15:29:40 +0900194 /* it may call write_inode just prior to fsync */
195 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700196 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700197
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700198 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700199 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700200 goto flush_out;
201 goto out;
202 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700203go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900204 /* guarantee free sections for fsync */
205 f2fs_balance_fs(sbi);
206
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900207 /*
208 * Both of fdatasync() and fsync() are able to be recovered from
209 * sudden-power-off.
210 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800211 down_read(&fi->i_sem);
212 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900213 up_read(&fi->i_sem);
214
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900215 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900216 nid_t pino;
217
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900218 /* all the dirty node pages should be flushed for POR */
219 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900220
221 down_write(&fi->i_sem);
Changman Lee9c015032014-12-01 16:29:58 +0900222 fi->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900223 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
224 get_parent_ino(inode, &pino)) {
Changman Lee9c015032014-12-01 16:29:58 +0900225 fi->i_pino = pino;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900226 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900227 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900228 mark_inode_dirty_sync(inode);
229 ret = f2fs_write_inode(inode, NULL);
230 if (ret)
231 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900232 } else {
233 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900234 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900235 } else {
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700236sync_nodes:
237 sync_node_pages(sbi, ino, &wbc);
238
239 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900240 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900241 ret = f2fs_write_inode(inode, NULL);
242 if (ret)
243 goto out;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700244 goto sync_nodes;
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900245 }
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700246
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700247 ret = wait_on_node_pages_writeback(sbi, ino);
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900248 if (ret)
249 goto out;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700250
251 /* once recovery info is written, don't need to tack this */
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700252 remove_dirty_inode(sbi, ino, APPEND_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700253 clear_inode_flag(fi, FI_APPEND_WRITE);
254flush_out:
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700255 remove_dirty_inode(sbi, ino, UPDATE_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700256 clear_inode_flag(fi, FI_UPDATE_WRITE);
Changman Lee9c015032014-12-01 16:29:58 +0900257 ret = f2fs_issue_flush(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900258 }
259out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900260 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900261 return ret;
262}
263
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900264static pgoff_t __get_first_dirty_index(struct address_space *mapping,
265 pgoff_t pgofs, int whence)
266{
267 struct pagevec pvec;
268 int nr_pages;
269
270 if (whence != SEEK_DATA)
271 return 0;
272
273 /* find first dirty page index */
274 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700275 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
276 PAGECACHE_TAG_DIRTY, 1);
277 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900278 pagevec_release(&pvec);
279 return pgofs;
280}
281
282static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
283 int whence)
284{
285 switch (whence) {
286 case SEEK_DATA:
287 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
288 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
289 return true;
290 break;
291 case SEEK_HOLE:
292 if (blkaddr == NULL_ADDR)
293 return true;
294 break;
295 }
296 return false;
297}
298
Chao Yu267378d2014-04-23 14:10:24 +0800299static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
300{
301 struct inode *inode = file->f_mapping->host;
302 loff_t maxbytes = inode->i_sb->s_maxbytes;
303 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900304 pgoff_t pgofs, end_offset, dirty;
305 loff_t data_ofs = offset;
306 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800307 int err = 0;
308
309 mutex_lock(&inode->i_mutex);
310
311 isize = i_size_read(inode);
312 if (offset >= isize)
313 goto fail;
314
315 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800316 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800317 if (whence == SEEK_HOLE)
318 data_ofs = isize;
319 goto found;
320 }
321
322 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
323
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900324 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
325
Chao Yu267378d2014-04-23 14:10:24 +0800326 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
327 set_new_dnode(&dn, inode, NULL, NULL, 0);
328 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
329 if (err && err != -ENOENT) {
330 goto fail;
331 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900332 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800333 if (whence == SEEK_DATA) {
334 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
335 F2FS_I(inode));
336 continue;
337 } else {
338 goto found;
339 }
340 }
341
Chao Yu6b2920a2014-07-07 11:21:59 +0800342 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800343
344 /* find data/hole in dnode block */
345 for (; dn.ofs_in_node < end_offset;
346 dn.ofs_in_node++, pgofs++,
347 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
348 block_t blkaddr;
349 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
350
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900351 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800352 f2fs_put_dnode(&dn);
353 goto found;
354 }
355 }
356 f2fs_put_dnode(&dn);
357 }
358
359 if (whence == SEEK_DATA)
360 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800361found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900362 if (whence == SEEK_HOLE && data_ofs > isize)
363 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800364 mutex_unlock(&inode->i_mutex);
365 return vfs_setpos(file, data_ofs, maxbytes);
366fail:
367 mutex_unlock(&inode->i_mutex);
368 return -ENXIO;
369}
370
371static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
372{
373 struct inode *inode = file->f_mapping->host;
374 loff_t maxbytes = inode->i_sb->s_maxbytes;
375
376 switch (whence) {
377 case SEEK_SET:
378 case SEEK_CUR:
379 case SEEK_END:
380 return generic_file_llseek_size(file, offset, whence,
381 maxbytes, i_size_read(inode));
382 case SEEK_DATA:
383 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700384 if (offset < 0)
385 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800386 return f2fs_seek_block(file, offset, whence);
387 }
388
389 return -EINVAL;
390}
391
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900392static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
393{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700394 struct inode *inode = file_inode(file);
395
396 /* we don't need to use inline_data strictly */
397 if (f2fs_has_inline_data(inode)) {
398 int err = f2fs_convert_inline_inode(inode);
399 if (err)
400 return err;
401 }
402
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900403 file_accessed(file);
404 vma->vm_ops = &f2fs_file_vm_ops;
405 return 0;
406}
407
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900408int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900409{
410 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700411 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900412 struct f2fs_node *raw_node;
413 __le32 *addr;
414
Gu Zheng45590712013-07-15 17:57:38 +0800415 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900416 addr = blkaddr_in_node(raw_node) + ofs;
417
Chris Fries6c311ec2014-01-17 14:44:39 -0600418 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900419 block_t blkaddr = le32_to_cpu(*addr);
420 if (blkaddr == NULL_ADDR)
421 continue;
422
423 update_extent_cache(NULL_ADDR, dn);
424 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900425 nr_free++;
426 }
427 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900428 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900429 set_page_dirty(dn->node_page);
430 sync_inode_page(dn);
431 }
432 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900433
434 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
435 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900436 return nr_free;
437}
438
439void truncate_data_blocks(struct dnode_of_data *dn)
440{
441 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
442}
443
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700444static int truncate_partial_data_page(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900445{
446 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
447 struct page *page;
448
449 if (!offset)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700450 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900451
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900452 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900453 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700454 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900455
456 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800457 if (unlikely(!PageUptodate(page) ||
458 page->mapping != inode->i_mapping))
459 goto out;
460
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900461 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900462 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
463 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800464out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900465 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700466 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467}
468
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700469int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900470{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700471 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900472 unsigned int blocksize = inode->i_sb->s_blocksize;
473 struct dnode_of_data dn;
474 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800475 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700476 struct page *ipage;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900477
Namjae Jeon51dd6242013-04-20 01:28:52 +0900478 trace_f2fs_truncate_blocks_enter(inode, from);
479
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900480 free_from = (pgoff_t)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700481 ((from + blocksize - 1) >> (sbi->log_blocksize));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900482
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700483 if (lock)
484 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800485
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700486 ipage = get_node_page(sbi, inode->i_ino);
487 if (IS_ERR(ipage)) {
488 err = PTR_ERR(ipage);
489 goto out;
490 }
491
492 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700493 f2fs_put_page(ipage, 1);
494 goto out;
495 }
496
497 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900498 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900499 if (err) {
500 if (err == -ENOENT)
501 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700502 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700503 }
504
Chao Yu6403eb12014-04-26 19:59:52 +0800505 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900506
507 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700508 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900509
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900510 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
511 truncate_data_blocks_range(&dn, count);
512 free_from += count;
513 }
514
515 f2fs_put_dnode(&dn);
516free_next:
517 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800518out:
519 if (lock)
520 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700521
522 /* lastly zero out the first data page */
523 if (!err)
524 err = truncate_partial_data_page(inode, from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900525
Namjae Jeon51dd6242013-04-20 01:28:52 +0900526 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900527 return err;
528}
529
530void f2fs_truncate(struct inode *inode)
531{
532 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
533 S_ISLNK(inode->i_mode)))
534 return;
535
Namjae Jeon51dd6242013-04-20 01:28:52 +0900536 trace_f2fs_truncate(inode);
537
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800538 /* we should check inline_data size */
539 if (f2fs_has_inline_data(inode) && !f2fs_may_inline(inode)) {
540 if (f2fs_convert_inline_inode(inode))
541 return;
542 }
543
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700544 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
546 mark_inode_dirty(inode);
547 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900548}
549
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900550int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900551 struct dentry *dentry, struct kstat *stat)
552{
553 struct inode *inode = dentry->d_inode;
554 generic_fillattr(inode, stat);
555 stat->blocks <<= 3;
556 return 0;
557}
558
559#ifdef CONFIG_F2FS_FS_POSIX_ACL
560static void __setattr_copy(struct inode *inode, const struct iattr *attr)
561{
562 struct f2fs_inode_info *fi = F2FS_I(inode);
563 unsigned int ia_valid = attr->ia_valid;
564
565 if (ia_valid & ATTR_UID)
566 inode->i_uid = attr->ia_uid;
567 if (ia_valid & ATTR_GID)
568 inode->i_gid = attr->ia_gid;
569 if (ia_valid & ATTR_ATIME)
570 inode->i_atime = timespec_trunc(attr->ia_atime,
571 inode->i_sb->s_time_gran);
572 if (ia_valid & ATTR_MTIME)
573 inode->i_mtime = timespec_trunc(attr->ia_mtime,
574 inode->i_sb->s_time_gran);
575 if (ia_valid & ATTR_CTIME)
576 inode->i_ctime = timespec_trunc(attr->ia_ctime,
577 inode->i_sb->s_time_gran);
578 if (ia_valid & ATTR_MODE) {
579 umode_t mode = attr->ia_mode;
580
581 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
582 mode &= ~S_ISGID;
583 set_acl_inode(fi, mode);
584 }
585}
586#else
587#define __setattr_copy setattr_copy
588#endif
589
590int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
591{
592 struct inode *inode = dentry->d_inode;
593 struct f2fs_inode_info *fi = F2FS_I(inode);
594 int err;
595
596 err = inode_change_ok(inode, attr);
597 if (err)
598 return err;
599
Chao Yu09db6a22014-09-15 18:02:09 +0800600 if (attr->ia_valid & ATTR_SIZE) {
Chao Yu09db6a22014-09-15 18:02:09 +0800601 if (attr->ia_size != i_size_read(inode)) {
602 truncate_setsize(inode, attr->ia_size);
603 f2fs_truncate(inode);
604 f2fs_balance_fs(F2FS_I_SB(inode));
605 } else {
606 /*
607 * giving a chance to truncate blocks past EOF which
608 * are fallocated with FALLOC_FL_KEEP_SIZE.
609 */
610 f2fs_truncate(inode);
611 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900612 }
613
614 __setattr_copy(inode, attr);
615
616 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800617 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900618 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
619 inode->i_mode = fi->i_acl_mode;
620 clear_inode_flag(fi, FI_ACL_MODE);
621 }
622 }
623
624 mark_inode_dirty(inode);
625 return err;
626}
627
628const struct inode_operations f2fs_file_inode_operations = {
629 .getattr = f2fs_getattr,
630 .setattr = f2fs_setattr,
631 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800632 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900633#ifdef CONFIG_F2FS_FS_XATTR
634 .setxattr = generic_setxattr,
635 .getxattr = generic_getxattr,
636 .listxattr = f2fs_listxattr,
637 .removexattr = generic_removexattr,
638#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900639 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900640};
641
642static void fill_zero(struct inode *inode, pgoff_t index,
643 loff_t start, loff_t len)
644{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700645 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900646 struct page *page;
647
648 if (!len)
649 return;
650
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900651 f2fs_balance_fs(sbi);
652
Gu Zhenge4795562013-09-27 18:08:30 +0800653 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900654 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800655 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656
657 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900658 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900659 zero_user(page, start, len);
660 set_page_dirty(page);
661 f2fs_put_page(page, 1);
662 }
663}
664
665int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
666{
667 pgoff_t index;
668 int err;
669
670 for (index = pg_start; index < pg_end; index++) {
671 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900672
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900673 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900674 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900675 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900676 if (err == -ENOENT)
677 continue;
678 return err;
679 }
680
681 if (dn.data_blkaddr != NULL_ADDR)
682 truncate_data_blocks_range(&dn, 1);
683 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900684 }
685 return 0;
686}
687
Chao Yua66c7b22013-11-22 16:52:50 +0800688static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900689{
690 pgoff_t pg_start, pg_end;
691 loff_t off_start, off_end;
692 int ret = 0;
693
Chao Yu14cecc52014-09-15 18:03:32 +0800694 if (!S_ISREG(inode->i_mode))
695 return -EOPNOTSUPP;
696
697 /* skip punching hole beyond i_size */
698 if (offset >= inode->i_size)
699 return ret;
700
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700701 if (f2fs_has_inline_data(inode)) {
702 ret = f2fs_convert_inline_inode(inode);
703 if (ret)
704 return ret;
705 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800706
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900707 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
708 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
709
710 off_start = offset & (PAGE_CACHE_SIZE - 1);
711 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
712
713 if (pg_start == pg_end) {
714 fill_zero(inode, pg_start, off_start,
715 off_end - off_start);
716 } else {
717 if (off_start)
718 fill_zero(inode, pg_start++, off_start,
719 PAGE_CACHE_SIZE - off_start);
720 if (off_end)
721 fill_zero(inode, pg_end, 0, off_end);
722
723 if (pg_start < pg_end) {
724 struct address_space *mapping = inode->i_mapping;
725 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700726 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500727
728 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900729
730 blk_start = pg_start << PAGE_CACHE_SHIFT;
731 blk_end = pg_end << PAGE_CACHE_SHIFT;
732 truncate_inode_pages_range(mapping, blk_start,
733 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900734
Gu Zhenge4795562013-09-27 18:08:30 +0800735 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900736 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800737 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900738 }
739 }
740
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900741 return ret;
742}
743
744static int expand_inode_data(struct inode *inode, loff_t offset,
745 loff_t len, int mode)
746{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700747 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900748 pgoff_t index, pg_start, pg_end;
749 loff_t new_size = i_size_read(inode);
750 loff_t off_start, off_end;
751 int ret = 0;
752
Chao Yu497a0932014-08-04 10:11:17 +0800753 f2fs_balance_fs(sbi);
754
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900755 ret = inode_newsize_ok(inode, (len + offset));
756 if (ret)
757 return ret;
758
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700759 if (f2fs_has_inline_data(inode)) {
760 ret = f2fs_convert_inline_inode(inode);
761 if (ret)
762 return ret;
763 }
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900764
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900765 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
766 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
767
768 off_start = offset & (PAGE_CACHE_SIZE - 1);
769 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
770
Jaegeuk Kimead43272014-06-13 13:05:55 +0900771 f2fs_lock_op(sbi);
772
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900773 for (index = pg_start; index <= pg_end; index++) {
774 struct dnode_of_data dn;
775
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900776 if (index == pg_end && !off_end)
777 goto noalloc;
778
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900779 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800780 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800781 if (ret)
782 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900783noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900784 if (pg_start == pg_end)
785 new_size = offset + len;
786 else if (index == pg_start && off_start)
787 new_size = (index + 1) << PAGE_CACHE_SHIFT;
788 else if (index == pg_end)
789 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
790 else
791 new_size += PAGE_CACHE_SIZE;
792 }
793
794 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
795 i_size_read(inode) < new_size) {
796 i_size_write(inode, new_size);
797 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900798 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900799 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900800 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900801
802 return ret;
803}
804
805static long f2fs_fallocate(struct file *file, int mode,
806 loff_t offset, loff_t len)
807{
Al Viro6131ffa2013-02-27 16:59:05 -0500808 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900809 long ret;
810
811 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
812 return -EOPNOTSUPP;
813
Chao Yu3375f692014-01-28 10:29:26 +0800814 mutex_lock(&inode->i_mutex);
815
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900816 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800817 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900818 else
819 ret = expand_inode_data(inode, offset, len, mode);
820
Namjae Jeon3af60a42012-12-30 14:52:37 +0900821 if (!ret) {
822 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
823 mark_inode_dirty(inode);
824 }
Chao Yu3375f692014-01-28 10:29:26 +0800825
826 mutex_unlock(&inode->i_mutex);
827
Namjae Jeonc01e2852013-04-23 17:00:52 +0900828 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900829 return ret;
830}
831
832#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
833#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
834
835static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
836{
837 if (S_ISDIR(mode))
838 return flags;
839 else if (S_ISREG(mode))
840 return flags & F2FS_REG_FLMASK;
841 else
842 return flags & F2FS_OTHER_FLMASK;
843}
844
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700845static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900846{
Al Viro6131ffa2013-02-27 16:59:05 -0500847 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900848 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700849 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
850 return put_user(flags, (int __user *)arg);
851}
852
853static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
854{
855 struct inode *inode = file_inode(filp);
856 struct f2fs_inode_info *fi = F2FS_I(inode);
857 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
858 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900859 int ret;
860
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700861 ret = mnt_want_write_file(filp);
862 if (ret)
863 return ret;
864
865 if (!inode_owner_or_capable(inode)) {
866 ret = -EACCES;
867 goto out;
868 }
869
870 if (get_user(flags, (int __user *)arg)) {
871 ret = -EFAULT;
872 goto out;
873 }
874
875 flags = f2fs_mask_flags(inode->i_mode, flags);
876
877 mutex_lock(&inode->i_mutex);
878
879 oldflags = fi->i_flags;
880
881 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
882 if (!capable(CAP_LINUX_IMMUTABLE)) {
883 mutex_unlock(&inode->i_mutex);
884 ret = -EPERM;
885 goto out;
886 }
887 }
888
889 flags = flags & FS_FL_USER_MODIFIABLE;
890 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
891 fi->i_flags = flags;
892 mutex_unlock(&inode->i_mutex);
893
894 f2fs_set_inode_flags(inode);
895 inode->i_ctime = CURRENT_TIME;
896 mark_inode_dirty(inode);
897out:
898 mnt_drop_write_file(filp);
899 return ret;
900}
901
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700902static int f2fs_ioc_start_atomic_write(struct file *filp)
903{
904 struct inode *inode = file_inode(filp);
905 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
906
907 if (!inode_owner_or_capable(inode))
908 return -EACCES;
909
910 f2fs_balance_fs(sbi);
911
912 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
913
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700914 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700915}
916
Jaegeuk Kim12662232014-12-05 14:37:37 -0800917static int f2fs_release_file(struct inode *inode, struct file *filp)
918{
919 /* some remained atomic pages should discarded */
920 if (f2fs_is_atomic_file(inode) || f2fs_is_volatile_file(inode))
921 commit_inmem_pages(inode, true);
922 return 0;
923}
924
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700925static int f2fs_ioc_commit_atomic_write(struct file *filp)
926{
927 struct inode *inode = file_inode(filp);
928 int ret;
929
930 if (!inode_owner_or_capable(inode))
931 return -EACCES;
932
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700933 if (f2fs_is_volatile_file(inode))
934 return 0;
935
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700936 ret = mnt_want_write_file(filp);
937 if (ret)
938 return ret;
939
940 if (f2fs_is_atomic_file(inode))
941 commit_inmem_pages(inode, false);
942
943 ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
944 mnt_drop_write_file(filp);
945 return ret;
946}
947
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700948static int f2fs_ioc_start_volatile_write(struct file *filp)
949{
950 struct inode *inode = file_inode(filp);
951
952 if (!inode_owner_or_capable(inode))
953 return -EACCES;
954
955 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700956
957 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700958}
959
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700960static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
961{
962 struct inode *inode = file_inode(filp);
963 struct super_block *sb = inode->i_sb;
964 struct request_queue *q = bdev_get_queue(sb->s_bdev);
965 struct fstrim_range range;
966 int ret;
967
968 if (!capable(CAP_SYS_ADMIN))
969 return -EPERM;
970
971 if (!blk_queue_discard(q))
972 return -EOPNOTSUPP;
973
974 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
975 sizeof(range)))
976 return -EFAULT;
977
978 range.minlen = max((unsigned int)range.minlen,
979 q->limits.discard_granularity);
980 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
981 if (ret < 0)
982 return ret;
983
984 if (copy_to_user((struct fstrim_range __user *)arg, &range,
985 sizeof(range)))
986 return -EFAULT;
987 return 0;
988}
989
990long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
991{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900992 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900993 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700994 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900995 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700996 return f2fs_ioc_setflags(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700997 case F2FS_IOC_START_ATOMIC_WRITE:
998 return f2fs_ioc_start_atomic_write(filp);
999 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1000 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001001 case F2FS_IOC_START_VOLATILE_WRITE:
1002 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001003 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001004 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001005 default:
1006 return -ENOTTY;
1007 }
1008}
1009
Namjae Jeone9750822013-02-04 23:41:41 +09001010#ifdef CONFIG_COMPAT
1011long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1012{
1013 switch (cmd) {
1014 case F2FS_IOC32_GETFLAGS:
1015 cmd = F2FS_IOC_GETFLAGS;
1016 break;
1017 case F2FS_IOC32_SETFLAGS:
1018 cmd = F2FS_IOC_SETFLAGS;
1019 break;
1020 default:
1021 return -ENOIOCTLCMD;
1022 }
1023 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1024}
1025#endif
1026
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001027const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001028 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001029 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -04001030 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -04001031 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04001032 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001033 .open = generic_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08001034 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001035 .mmap = f2fs_file_mmap,
1036 .fsync = f2fs_sync_file,
1037 .fallocate = f2fs_fallocate,
1038 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001039#ifdef CONFIG_COMPAT
1040 .compat_ioctl = f2fs_compat_ioctl,
1041#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001042 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001043 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001044};