blob: d054e0ebfe240d40f4d44ec0cd592825f4acc149 [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;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070038 struct page *ipage;
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 Kim9ba69cf2014-10-17 20:33:55 -070044retry:
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070045 /* force to convert with normal data indices */
46 err = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, page);
47 if (err)
48 goto out;
49
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090050 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080051 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070052
53 /* check inline_data */
54 ipage = get_node_page(sbi, inode->i_ino);
55 if (IS_ERR(ipage)) {
56 f2fs_unlock_op(sbi);
Huajun Lib6009652013-11-10 23:13:18 +080057 goto out;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070058 }
59
60 if (f2fs_has_inline_data(inode)) {
61 f2fs_put_page(ipage, 1);
62 f2fs_unlock_op(sbi);
63 goto retry;
64 }
65
66 set_new_dnode(&dn, inode, ipage, NULL, 0);
67 err = f2fs_reserve_block(&dn, page->index);
68 if (err) {
69 f2fs_unlock_op(sbi);
70 goto out;
71 }
72 f2fs_put_dnode(&dn);
73 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090074
Namjae Jeon9851e6e2013-04-28 09:04:18 +090075 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090076 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090077 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090078 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090079 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090080 unlock_page(page);
81 err = -EFAULT;
82 goto out;
83 }
84
85 /*
86 * check to see if the page is mapped already (no holes)
87 */
88 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090089 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090090
91 /* page is wholly or partially inside EOF */
92 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
93 unsigned offset;
94 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
95 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
96 }
97 set_page_dirty(page);
98 SetPageUptodate(page);
99
Jaegeuk Kime943a102013-10-25 14:26:31 +0900100 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900101mapped:
102 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900103 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900104out:
105 sb_end_pagefault(inode->i_sb);
106 return block_page_mkwrite_return(err);
107}
108
109static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900110 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700111 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900112 .page_mkwrite = f2fs_vm_page_mkwrite,
113 .remap_pages = generic_file_remap_pages,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900114};
115
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900116static int get_parent_ino(struct inode *inode, nid_t *pino)
117{
118 struct dentry *dentry;
119
120 inode = igrab(inode);
121 dentry = d_find_any_alias(inode);
122 iput(inode);
123 if (!dentry)
124 return 0;
125
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900126 if (update_dent_inode(inode, &dentry->d_name)) {
127 dput(dentry);
128 return 0;
129 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900130
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900131 *pino = parent_ino(dentry);
132 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900133 return 1;
134}
135
Chao Yu9d1589e2014-08-20 18:37:35 +0800136static inline bool need_do_checkpoint(struct inode *inode)
137{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700138 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800139 bool need_cp = false;
140
141 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
142 need_cp = true;
143 else if (file_wrong_pino(inode))
144 need_cp = true;
145 else if (!space_for_roll_forward(sbi))
146 need_cp = true;
147 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
148 need_cp = true;
149 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
150 need_cp = true;
151
152 return need_cp;
153}
154
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900155int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
156{
157 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900158 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700159 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700160 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900161 int ret = 0;
162 bool need_cp = false;
163 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900164 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900165 .nr_to_write = LONG_MAX,
166 .for_reclaim = 0,
167 };
168
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900169 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900170 return 0;
171
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900172 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700173
174 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700175 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700176 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900177 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700178 clear_inode_flag(fi, FI_NEED_IPU);
179
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900180 if (ret) {
181 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900182 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900183 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900184
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700185 /*
186 * if there is no written data, don't waste time to write recovery info.
187 */
188 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700189 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700190 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
191
192 /* But we need to avoid that there are some inode updates */
193 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) {
194 f2fs_put_page(i, 0);
195 goto go_write;
196 }
197 f2fs_put_page(i, 0);
198
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700199 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700200 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700201 goto flush_out;
202 goto out;
203 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700204go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900205 /* guarantee free sections for fsync */
206 f2fs_balance_fs(sbi);
207
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900208 /*
209 * Both of fdatasync() and fsync() are able to be recovered from
210 * sudden-power-off.
211 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800212 down_read(&fi->i_sem);
213 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900214 up_read(&fi->i_sem);
215
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900216 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900217 nid_t pino;
218
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900219 /* all the dirty node pages should be flushed for POR */
220 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900221
222 down_write(&fi->i_sem);
223 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900224 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
225 get_parent_ino(inode, &pino)) {
226 F2FS_I(inode)->i_pino = pino;
227 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900228 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900229 mark_inode_dirty_sync(inode);
230 ret = f2fs_write_inode(inode, NULL);
231 if (ret)
232 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900233 } else {
234 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900235 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900236 } else {
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700237sync_nodes:
238 sync_node_pages(sbi, ino, &wbc);
239
240 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900241 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900242 ret = f2fs_write_inode(inode, NULL);
243 if (ret)
244 goto out;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700245 goto sync_nodes;
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900246 }
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700247
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700248 ret = wait_on_node_pages_writeback(sbi, ino);
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900249 if (ret)
250 goto out;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700251
252 /* once recovery info is written, don't need to tack this */
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700253 remove_dirty_inode(sbi, ino, APPEND_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700254 clear_inode_flag(fi, FI_APPEND_WRITE);
255flush_out:
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700256 remove_dirty_inode(sbi, ino, UPDATE_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700257 clear_inode_flag(fi, FI_UPDATE_WRITE);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700258 ret = f2fs_issue_flush(F2FS_I_SB(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900259 }
260out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900261 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900262 return ret;
263}
264
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900265static pgoff_t __get_first_dirty_index(struct address_space *mapping,
266 pgoff_t pgofs, int whence)
267{
268 struct pagevec pvec;
269 int nr_pages;
270
271 if (whence != SEEK_DATA)
272 return 0;
273
274 /* find first dirty page index */
275 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700276 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
277 PAGECACHE_TAG_DIRTY, 1);
278 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900279 pagevec_release(&pvec);
280 return pgofs;
281}
282
283static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
284 int whence)
285{
286 switch (whence) {
287 case SEEK_DATA:
288 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
289 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
290 return true;
291 break;
292 case SEEK_HOLE:
293 if (blkaddr == NULL_ADDR)
294 return true;
295 break;
296 }
297 return false;
298}
299
Chao Yu267378d2014-04-23 14:10:24 +0800300static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
301{
302 struct inode *inode = file->f_mapping->host;
303 loff_t maxbytes = inode->i_sb->s_maxbytes;
304 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900305 pgoff_t pgofs, end_offset, dirty;
306 loff_t data_ofs = offset;
307 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800308 int err = 0;
309
310 mutex_lock(&inode->i_mutex);
311
312 isize = i_size_read(inode);
313 if (offset >= isize)
314 goto fail;
315
316 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800317 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800318 if (whence == SEEK_HOLE)
319 data_ofs = isize;
320 goto found;
321 }
322
323 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
324
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900325 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
326
Chao Yu267378d2014-04-23 14:10:24 +0800327 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
328 set_new_dnode(&dn, inode, NULL, NULL, 0);
329 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
330 if (err && err != -ENOENT) {
331 goto fail;
332 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900333 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800334 if (whence == SEEK_DATA) {
335 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
336 F2FS_I(inode));
337 continue;
338 } else {
339 goto found;
340 }
341 }
342
Chao Yu6b2920a2014-07-07 11:21:59 +0800343 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800344
345 /* find data/hole in dnode block */
346 for (; dn.ofs_in_node < end_offset;
347 dn.ofs_in_node++, pgofs++,
348 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
349 block_t blkaddr;
350 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
351
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900352 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800353 f2fs_put_dnode(&dn);
354 goto found;
355 }
356 }
357 f2fs_put_dnode(&dn);
358 }
359
360 if (whence == SEEK_DATA)
361 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800362found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900363 if (whence == SEEK_HOLE && data_ofs > isize)
364 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800365 mutex_unlock(&inode->i_mutex);
366 return vfs_setpos(file, data_ofs, maxbytes);
367fail:
368 mutex_unlock(&inode->i_mutex);
369 return -ENXIO;
370}
371
372static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
373{
374 struct inode *inode = file->f_mapping->host;
375 loff_t maxbytes = inode->i_sb->s_maxbytes;
376
377 switch (whence) {
378 case SEEK_SET:
379 case SEEK_CUR:
380 case SEEK_END:
381 return generic_file_llseek_size(file, offset, whence,
382 maxbytes, i_size_read(inode));
383 case SEEK_DATA:
384 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700385 if (offset < 0)
386 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800387 return f2fs_seek_block(file, offset, whence);
388 }
389
390 return -EINVAL;
391}
392
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900393static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
394{
395 file_accessed(file);
396 vma->vm_ops = &f2fs_file_vm_ops;
397 return 0;
398}
399
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900400int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900401{
402 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700403 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900404 struct f2fs_node *raw_node;
405 __le32 *addr;
406
Gu Zheng45590712013-07-15 17:57:38 +0800407 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900408 addr = blkaddr_in_node(raw_node) + ofs;
409
Chris Fries6c311ec2014-01-17 14:44:39 -0600410 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900411 block_t blkaddr = le32_to_cpu(*addr);
412 if (blkaddr == NULL_ADDR)
413 continue;
414
415 update_extent_cache(NULL_ADDR, dn);
416 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900417 nr_free++;
418 }
419 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900420 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900421 set_page_dirty(dn->node_page);
422 sync_inode_page(dn);
423 }
424 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900425
426 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
427 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900428 return nr_free;
429}
430
431void truncate_data_blocks(struct dnode_of_data *dn)
432{
433 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
434}
435
436static void truncate_partial_data_page(struct inode *inode, u64 from)
437{
438 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
439 struct page *page;
440
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800441 if (f2fs_has_inline_data(inode))
442 return truncate_inline_data(inode, from);
443
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900444 if (!offset)
445 return;
446
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900447 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900448 if (IS_ERR(page))
449 return;
450
451 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800452 if (unlikely(!PageUptodate(page) ||
453 page->mapping != inode->i_mapping))
454 goto out;
455
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900456 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900457 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
458 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800459
460out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900461 f2fs_put_page(page, 1);
462}
463
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700464int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900465{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700466 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467 unsigned int blocksize = inode->i_sb->s_blocksize;
468 struct dnode_of_data dn;
469 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800470 int count = 0, err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900471
Namjae Jeon51dd6242013-04-20 01:28:52 +0900472 trace_f2fs_truncate_blocks_enter(inode, from);
473
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900474 if (f2fs_has_inline_data(inode))
475 goto done;
476
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900477 free_from = (pgoff_t)
478 ((from + blocksize - 1) >> (sbi->log_blocksize));
479
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700480 if (lock)
481 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800482
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900483 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900484 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900485 if (err) {
486 if (err == -ENOENT)
487 goto free_next;
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700488 if (lock)
489 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900490 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900491 return err;
492 }
493
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700494 /* writepage can convert inline_data under get_donde_of_data */
495 if (f2fs_has_inline_data(inode)) {
496 f2fs_put_dnode(&dn);
Jaegeuk Kim13fd8f82014-10-19 01:43:23 -0700497 goto unlock_done;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700498 }
499
Chao Yu6403eb12014-04-26 19:59:52 +0800500 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900501
502 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700503 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900504
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900505 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
506 truncate_data_blocks_range(&dn, count);
507 free_from += count;
508 }
509
510 f2fs_put_dnode(&dn);
511free_next:
512 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim13fd8f82014-10-19 01:43:23 -0700513unlock_done:
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700514 if (lock)
515 f2fs_unlock_op(sbi);
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900516done:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900517 /* lastly zero out the first data page */
518 truncate_partial_data_page(inode, from);
519
Namjae Jeon51dd6242013-04-20 01:28:52 +0900520 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900521 return err;
522}
523
524void f2fs_truncate(struct inode *inode)
525{
526 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
527 S_ISLNK(inode->i_mode)))
528 return;
529
Namjae Jeon51dd6242013-04-20 01:28:52 +0900530 trace_f2fs_truncate(inode);
531
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700532 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900533 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
534 mark_inode_dirty(inode);
535 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900536}
537
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900538int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900539 struct dentry *dentry, struct kstat *stat)
540{
541 struct inode *inode = dentry->d_inode;
542 generic_fillattr(inode, stat);
543 stat->blocks <<= 3;
544 return 0;
545}
546
547#ifdef CONFIG_F2FS_FS_POSIX_ACL
548static void __setattr_copy(struct inode *inode, const struct iattr *attr)
549{
550 struct f2fs_inode_info *fi = F2FS_I(inode);
551 unsigned int ia_valid = attr->ia_valid;
552
553 if (ia_valid & ATTR_UID)
554 inode->i_uid = attr->ia_uid;
555 if (ia_valid & ATTR_GID)
556 inode->i_gid = attr->ia_gid;
557 if (ia_valid & ATTR_ATIME)
558 inode->i_atime = timespec_trunc(attr->ia_atime,
559 inode->i_sb->s_time_gran);
560 if (ia_valid & ATTR_MTIME)
561 inode->i_mtime = timespec_trunc(attr->ia_mtime,
562 inode->i_sb->s_time_gran);
563 if (ia_valid & ATTR_CTIME)
564 inode->i_ctime = timespec_trunc(attr->ia_ctime,
565 inode->i_sb->s_time_gran);
566 if (ia_valid & ATTR_MODE) {
567 umode_t mode = attr->ia_mode;
568
569 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
570 mode &= ~S_ISGID;
571 set_acl_inode(fi, mode);
572 }
573}
574#else
575#define __setattr_copy setattr_copy
576#endif
577
578int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
579{
580 struct inode *inode = dentry->d_inode;
581 struct f2fs_inode_info *fi = F2FS_I(inode);
582 int err;
583
584 err = inode_change_ok(inode, attr);
585 if (err)
586 return err;
587
Chao Yu09db6a22014-09-15 18:02:09 +0800588 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700589 err = f2fs_convert_inline_data(inode, attr->ia_size, NULL);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900590 if (err)
591 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800592
Chao Yu09db6a22014-09-15 18:02:09 +0800593 if (attr->ia_size != i_size_read(inode)) {
594 truncate_setsize(inode, attr->ia_size);
595 f2fs_truncate(inode);
596 f2fs_balance_fs(F2FS_I_SB(inode));
597 } else {
598 /*
599 * giving a chance to truncate blocks past EOF which
600 * are fallocated with FALLOC_FL_KEEP_SIZE.
601 */
602 f2fs_truncate(inode);
603 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900604 }
605
606 __setattr_copy(inode, attr);
607
608 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800609 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900610 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
611 inode->i_mode = fi->i_acl_mode;
612 clear_inode_flag(fi, FI_ACL_MODE);
613 }
614 }
615
616 mark_inode_dirty(inode);
617 return err;
618}
619
620const struct inode_operations f2fs_file_inode_operations = {
621 .getattr = f2fs_getattr,
622 .setattr = f2fs_setattr,
623 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800624 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900625#ifdef CONFIG_F2FS_FS_XATTR
626 .setxattr = generic_setxattr,
627 .getxattr = generic_getxattr,
628 .listxattr = f2fs_listxattr,
629 .removexattr = generic_removexattr,
630#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900631 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900632};
633
634static void fill_zero(struct inode *inode, pgoff_t index,
635 loff_t start, loff_t len)
636{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700637 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900638 struct page *page;
639
640 if (!len)
641 return;
642
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900643 f2fs_balance_fs(sbi);
644
Gu Zhenge4795562013-09-27 18:08:30 +0800645 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900646 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800647 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900648
649 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900650 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900651 zero_user(page, start, len);
652 set_page_dirty(page);
653 f2fs_put_page(page, 1);
654 }
655}
656
657int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
658{
659 pgoff_t index;
660 int err;
661
662 for (index = pg_start; index < pg_end; index++) {
663 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900664
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900666 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900667 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668 if (err == -ENOENT)
669 continue;
670 return err;
671 }
672
673 if (dn.data_blkaddr != NULL_ADDR)
674 truncate_data_blocks_range(&dn, 1);
675 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900676 }
677 return 0;
678}
679
Chao Yua66c7b22013-11-22 16:52:50 +0800680static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900681{
682 pgoff_t pg_start, pg_end;
683 loff_t off_start, off_end;
684 int ret = 0;
685
Chao Yu14cecc52014-09-15 18:03:32 +0800686 if (!S_ISREG(inode->i_mode))
687 return -EOPNOTSUPP;
688
689 /* skip punching hole beyond i_size */
690 if (offset >= inode->i_size)
691 return ret;
692
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700693 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, NULL);
Jaegeuk Kim8230a0a2013-12-27 11:13:21 +0900694 if (ret)
695 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800696
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900697 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
698 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
699
700 off_start = offset & (PAGE_CACHE_SIZE - 1);
701 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
702
703 if (pg_start == pg_end) {
704 fill_zero(inode, pg_start, off_start,
705 off_end - off_start);
706 } else {
707 if (off_start)
708 fill_zero(inode, pg_start++, off_start,
709 PAGE_CACHE_SIZE - off_start);
710 if (off_end)
711 fill_zero(inode, pg_end, 0, off_end);
712
713 if (pg_start < pg_end) {
714 struct address_space *mapping = inode->i_mapping;
715 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700716 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500717
718 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719
720 blk_start = pg_start << PAGE_CACHE_SHIFT;
721 blk_end = pg_end << PAGE_CACHE_SHIFT;
722 truncate_inode_pages_range(mapping, blk_start,
723 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900724
Gu Zhenge4795562013-09-27 18:08:30 +0800725 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800727 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900728 }
729 }
730
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900731 return ret;
732}
733
734static int expand_inode_data(struct inode *inode, loff_t offset,
735 loff_t len, int mode)
736{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700737 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900738 pgoff_t index, pg_start, pg_end;
739 loff_t new_size = i_size_read(inode);
740 loff_t off_start, off_end;
741 int ret = 0;
742
Chao Yu497a0932014-08-04 10:11:17 +0800743 f2fs_balance_fs(sbi);
744
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900745 ret = inode_newsize_ok(inode, (len + offset));
746 if (ret)
747 return ret;
748
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700749 ret = f2fs_convert_inline_data(inode, offset + len, NULL);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900750 if (ret)
751 return ret;
752
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900753 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
754 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
755
756 off_start = offset & (PAGE_CACHE_SIZE - 1);
757 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
758
Jaegeuk Kimead43272014-06-13 13:05:55 +0900759 f2fs_lock_op(sbi);
760
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900761 for (index = pg_start; index <= pg_end; index++) {
762 struct dnode_of_data dn;
763
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900764 if (index == pg_end && !off_end)
765 goto noalloc;
766
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900767 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800768 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800769 if (ret)
770 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900771noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900772 if (pg_start == pg_end)
773 new_size = offset + len;
774 else if (index == pg_start && off_start)
775 new_size = (index + 1) << PAGE_CACHE_SHIFT;
776 else if (index == pg_end)
777 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
778 else
779 new_size += PAGE_CACHE_SIZE;
780 }
781
782 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
783 i_size_read(inode) < new_size) {
784 i_size_write(inode, new_size);
785 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900786 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900787 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900788 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900789
790 return ret;
791}
792
793static long f2fs_fallocate(struct file *file, int mode,
794 loff_t offset, loff_t len)
795{
Al Viro6131ffa2013-02-27 16:59:05 -0500796 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900797 long ret;
798
799 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
800 return -EOPNOTSUPP;
801
Chao Yu3375f692014-01-28 10:29:26 +0800802 mutex_lock(&inode->i_mutex);
803
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900804 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800805 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900806 else
807 ret = expand_inode_data(inode, offset, len, mode);
808
Namjae Jeon3af60a42012-12-30 14:52:37 +0900809 if (!ret) {
810 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
811 mark_inode_dirty(inode);
812 }
Chao Yu3375f692014-01-28 10:29:26 +0800813
814 mutex_unlock(&inode->i_mutex);
815
Namjae Jeonc01e2852013-04-23 17:00:52 +0900816 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900817 return ret;
818}
819
820#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
821#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
822
823static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
824{
825 if (S_ISDIR(mode))
826 return flags;
827 else if (S_ISREG(mode))
828 return flags & F2FS_REG_FLMASK;
829 else
830 return flags & F2FS_OTHER_FLMASK;
831}
832
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700833static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900834{
Al Viro6131ffa2013-02-27 16:59:05 -0500835 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900836 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700837 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
838 return put_user(flags, (int __user *)arg);
839}
840
841static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
842{
843 struct inode *inode = file_inode(filp);
844 struct f2fs_inode_info *fi = F2FS_I(inode);
845 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
846 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900847 int ret;
848
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700849 ret = mnt_want_write_file(filp);
850 if (ret)
851 return ret;
852
853 if (!inode_owner_or_capable(inode)) {
854 ret = -EACCES;
855 goto out;
856 }
857
858 if (get_user(flags, (int __user *)arg)) {
859 ret = -EFAULT;
860 goto out;
861 }
862
863 flags = f2fs_mask_flags(inode->i_mode, flags);
864
865 mutex_lock(&inode->i_mutex);
866
867 oldflags = fi->i_flags;
868
869 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
870 if (!capable(CAP_LINUX_IMMUTABLE)) {
871 mutex_unlock(&inode->i_mutex);
872 ret = -EPERM;
873 goto out;
874 }
875 }
876
877 flags = flags & FS_FL_USER_MODIFIABLE;
878 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
879 fi->i_flags = flags;
880 mutex_unlock(&inode->i_mutex);
881
882 f2fs_set_inode_flags(inode);
883 inode->i_ctime = CURRENT_TIME;
884 mark_inode_dirty(inode);
885out:
886 mnt_drop_write_file(filp);
887 return ret;
888}
889
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700890static int f2fs_ioc_start_atomic_write(struct file *filp)
891{
892 struct inode *inode = file_inode(filp);
893 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
894
895 if (!inode_owner_or_capable(inode))
896 return -EACCES;
897
898 f2fs_balance_fs(sbi);
899
900 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
901
902 return f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, NULL);
903}
904
905static int f2fs_ioc_commit_atomic_write(struct file *filp)
906{
907 struct inode *inode = file_inode(filp);
908 int ret;
909
910 if (!inode_owner_or_capable(inode))
911 return -EACCES;
912
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700913 if (f2fs_is_volatile_file(inode))
914 return 0;
915
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700916 ret = mnt_want_write_file(filp);
917 if (ret)
918 return ret;
919
920 if (f2fs_is_atomic_file(inode))
921 commit_inmem_pages(inode, false);
922
923 ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
924 mnt_drop_write_file(filp);
925 return ret;
926}
927
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700928static int f2fs_ioc_start_volatile_write(struct file *filp)
929{
930 struct inode *inode = file_inode(filp);
931
932 if (!inode_owner_or_capable(inode))
933 return -EACCES;
934
935 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
936 return 0;
937}
938
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700939static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
940{
941 struct inode *inode = file_inode(filp);
942 struct super_block *sb = inode->i_sb;
943 struct request_queue *q = bdev_get_queue(sb->s_bdev);
944 struct fstrim_range range;
945 int ret;
946
947 if (!capable(CAP_SYS_ADMIN))
948 return -EPERM;
949
950 if (!blk_queue_discard(q))
951 return -EOPNOTSUPP;
952
953 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
954 sizeof(range)))
955 return -EFAULT;
956
957 range.minlen = max((unsigned int)range.minlen,
958 q->limits.discard_granularity);
959 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
960 if (ret < 0)
961 return ret;
962
963 if (copy_to_user((struct fstrim_range __user *)arg, &range,
964 sizeof(range)))
965 return -EFAULT;
966 return 0;
967}
968
969long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
970{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900971 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900972 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700973 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900974 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700975 return f2fs_ioc_setflags(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700976 case F2FS_IOC_START_ATOMIC_WRITE:
977 return f2fs_ioc_start_atomic_write(filp);
978 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
979 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700980 case F2FS_IOC_START_VOLATILE_WRITE:
981 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700982 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700983 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900984 default:
985 return -ENOTTY;
986 }
987}
988
Namjae Jeone9750822013-02-04 23:41:41 +0900989#ifdef CONFIG_COMPAT
990long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
991{
992 switch (cmd) {
993 case F2FS_IOC32_GETFLAGS:
994 cmd = F2FS_IOC_GETFLAGS;
995 break;
996 case F2FS_IOC32_SETFLAGS:
997 cmd = F2FS_IOC_SETFLAGS;
998 break;
999 default:
1000 return -ENOIOCTLCMD;
1001 }
1002 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1003}
1004#endif
1005
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001006const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001007 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001008 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -04001009 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -04001010 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04001011 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001012 .open = generic_file_open,
1013 .mmap = f2fs_file_mmap,
1014 .fsync = f2fs_sync_file,
1015 .fallocate = f2fs_fallocate,
1016 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001017#ifdef CONFIG_COMPAT
1018 .compat_ioctl = f2fs_compat_ioctl,
1019#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001020 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001021 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001022};