blob: 46311e7b388a3ae84e54cb7ffc5ca72e147c158d [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;
Chao Yu9d1589e2014-08-20 18:37:35 +0800135
136 return need_cp;
137}
138
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900139int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
140{
141 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900142 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700143 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700144 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900145 int ret = 0;
146 bool need_cp = false;
147 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900148 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900149 .nr_to_write = LONG_MAX,
150 .for_reclaim = 0,
151 };
152
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900153 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900154 return 0;
155
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900156 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700157
158 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700159 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700160 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900161 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700162 clear_inode_flag(fi, FI_NEED_IPU);
163
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900164 if (ret) {
165 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900166 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900167 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900168
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700169 /*
170 * if there is no written data, don't waste time to write recovery info.
171 */
172 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700173 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700174 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
175
176 /* But we need to avoid that there are some inode updates */
177 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) {
178 f2fs_put_page(i, 0);
179 goto go_write;
180 }
181 f2fs_put_page(i, 0);
182
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700183 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700184 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700185 goto flush_out;
186 goto out;
187 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700188go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900189 /* guarantee free sections for fsync */
190 f2fs_balance_fs(sbi);
191
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900192 /*
193 * Both of fdatasync() and fsync() are able to be recovered from
194 * sudden-power-off.
195 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800196 down_read(&fi->i_sem);
197 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900198 up_read(&fi->i_sem);
199
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900200 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900201 nid_t pino;
202
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900203 /* all the dirty node pages should be flushed for POR */
204 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900205
206 down_write(&fi->i_sem);
207 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900208 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
209 get_parent_ino(inode, &pino)) {
210 F2FS_I(inode)->i_pino = pino;
211 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900212 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900213 mark_inode_dirty_sync(inode);
214 ret = f2fs_write_inode(inode, NULL);
215 if (ret)
216 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900217 } else {
218 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900219 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900220 } else {
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700221sync_nodes:
222 sync_node_pages(sbi, ino, &wbc);
223
224 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900225 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900226 ret = f2fs_write_inode(inode, NULL);
227 if (ret)
228 goto out;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700229 goto sync_nodes;
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900230 }
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700231
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700232 ret = wait_on_node_pages_writeback(sbi, ino);
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900233 if (ret)
234 goto out;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700235
236 /* once recovery info is written, don't need to tack this */
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700237 remove_dirty_inode(sbi, ino, APPEND_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700238 clear_inode_flag(fi, FI_APPEND_WRITE);
239flush_out:
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700240 remove_dirty_inode(sbi, ino, UPDATE_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700241 clear_inode_flag(fi, FI_UPDATE_WRITE);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700242 ret = f2fs_issue_flush(F2FS_I_SB(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900243 }
244out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900245 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900246 return ret;
247}
248
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900249static pgoff_t __get_first_dirty_index(struct address_space *mapping,
250 pgoff_t pgofs, int whence)
251{
252 struct pagevec pvec;
253 int nr_pages;
254
255 if (whence != SEEK_DATA)
256 return 0;
257
258 /* find first dirty page index */
259 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700260 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
261 PAGECACHE_TAG_DIRTY, 1);
262 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900263 pagevec_release(&pvec);
264 return pgofs;
265}
266
267static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
268 int whence)
269{
270 switch (whence) {
271 case SEEK_DATA:
272 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
273 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
274 return true;
275 break;
276 case SEEK_HOLE:
277 if (blkaddr == NULL_ADDR)
278 return true;
279 break;
280 }
281 return false;
282}
283
Chao Yu267378d2014-04-23 14:10:24 +0800284static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
285{
286 struct inode *inode = file->f_mapping->host;
287 loff_t maxbytes = inode->i_sb->s_maxbytes;
288 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900289 pgoff_t pgofs, end_offset, dirty;
290 loff_t data_ofs = offset;
291 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800292 int err = 0;
293
294 mutex_lock(&inode->i_mutex);
295
296 isize = i_size_read(inode);
297 if (offset >= isize)
298 goto fail;
299
300 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800301 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800302 if (whence == SEEK_HOLE)
303 data_ofs = isize;
304 goto found;
305 }
306
307 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
308
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900309 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
310
Chao Yu267378d2014-04-23 14:10:24 +0800311 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
312 set_new_dnode(&dn, inode, NULL, NULL, 0);
313 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
314 if (err && err != -ENOENT) {
315 goto fail;
316 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900317 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800318 if (whence == SEEK_DATA) {
319 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
320 F2FS_I(inode));
321 continue;
322 } else {
323 goto found;
324 }
325 }
326
Chao Yu6b2920a2014-07-07 11:21:59 +0800327 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800328
329 /* find data/hole in dnode block */
330 for (; dn.ofs_in_node < end_offset;
331 dn.ofs_in_node++, pgofs++,
332 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
333 block_t blkaddr;
334 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
335
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900336 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800337 f2fs_put_dnode(&dn);
338 goto found;
339 }
340 }
341 f2fs_put_dnode(&dn);
342 }
343
344 if (whence == SEEK_DATA)
345 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800346found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900347 if (whence == SEEK_HOLE && data_ofs > isize)
348 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800349 mutex_unlock(&inode->i_mutex);
350 return vfs_setpos(file, data_ofs, maxbytes);
351fail:
352 mutex_unlock(&inode->i_mutex);
353 return -ENXIO;
354}
355
356static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
357{
358 struct inode *inode = file->f_mapping->host;
359 loff_t maxbytes = inode->i_sb->s_maxbytes;
360
361 switch (whence) {
362 case SEEK_SET:
363 case SEEK_CUR:
364 case SEEK_END:
365 return generic_file_llseek_size(file, offset, whence,
366 maxbytes, i_size_read(inode));
367 case SEEK_DATA:
368 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700369 if (offset < 0)
370 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800371 return f2fs_seek_block(file, offset, whence);
372 }
373
374 return -EINVAL;
375}
376
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900377static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
378{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700379 struct inode *inode = file_inode(file);
380
381 /* we don't need to use inline_data strictly */
382 if (f2fs_has_inline_data(inode)) {
383 int err = f2fs_convert_inline_inode(inode);
384 if (err)
385 return err;
386 }
387
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900388 file_accessed(file);
389 vma->vm_ops = &f2fs_file_vm_ops;
390 return 0;
391}
392
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900393int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900394{
395 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700396 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900397 struct f2fs_node *raw_node;
398 __le32 *addr;
399
Gu Zheng45590712013-07-15 17:57:38 +0800400 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900401 addr = blkaddr_in_node(raw_node) + ofs;
402
Chris Fries6c311ec2014-01-17 14:44:39 -0600403 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900404 block_t blkaddr = le32_to_cpu(*addr);
405 if (blkaddr == NULL_ADDR)
406 continue;
407
408 update_extent_cache(NULL_ADDR, dn);
409 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900410 nr_free++;
411 }
412 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900413 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900414 set_page_dirty(dn->node_page);
415 sync_inode_page(dn);
416 }
417 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900418
419 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
420 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900421 return nr_free;
422}
423
424void truncate_data_blocks(struct dnode_of_data *dn)
425{
426 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
427}
428
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700429static int truncate_partial_data_page(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900430{
431 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
432 struct page *page;
433
434 if (!offset)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700435 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900436
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900437 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900438 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700439 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900440
441 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800442 if (unlikely(!PageUptodate(page) ||
443 page->mapping != inode->i_mapping))
444 goto out;
445
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900446 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900447 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
448 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800449out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900450 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700451 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900452}
453
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700454int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900455{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700456 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900457 unsigned int blocksize = inode->i_sb->s_blocksize;
458 struct dnode_of_data dn;
459 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800460 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700461 struct page *ipage;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900462
Namjae Jeon51dd6242013-04-20 01:28:52 +0900463 trace_f2fs_truncate_blocks_enter(inode, from);
464
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900465 free_from = (pgoff_t)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700466 ((from + blocksize - 1) >> (sbi->log_blocksize));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700468 if (lock)
469 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800470
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700471 ipage = get_node_page(sbi, inode->i_ino);
472 if (IS_ERR(ipage)) {
473 err = PTR_ERR(ipage);
474 goto out;
475 }
476
477 if (f2fs_has_inline_data(inode)) {
478 truncate_inline_data(ipage, from);
479 update_inode(inode, ipage);
480 f2fs_put_page(ipage, 1);
481 goto out;
482 }
483
484 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900485 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900486 if (err) {
487 if (err == -ENOENT)
488 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700489 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700490 }
491
Chao Yu6403eb12014-04-26 19:59:52 +0800492 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900493
494 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700495 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900496
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900497 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
498 truncate_data_blocks_range(&dn, count);
499 free_from += count;
500 }
501
502 f2fs_put_dnode(&dn);
503free_next:
504 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700505
506 /* lastly zero out the first data page */
507 if (!err)
508 err = truncate_partial_data_page(inode, from);
509out:
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700510 if (lock)
511 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900512
Namjae Jeon51dd6242013-04-20 01:28:52 +0900513 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900514 return err;
515}
516
517void f2fs_truncate(struct inode *inode)
518{
519 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
520 S_ISLNK(inode->i_mode)))
521 return;
522
Namjae Jeon51dd6242013-04-20 01:28:52 +0900523 trace_f2fs_truncate(inode);
524
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700525 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900526 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
527 mark_inode_dirty(inode);
528 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900529}
530
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900531int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 struct dentry *dentry, struct kstat *stat)
533{
534 struct inode *inode = dentry->d_inode;
535 generic_fillattr(inode, stat);
536 stat->blocks <<= 3;
537 return 0;
538}
539
540#ifdef CONFIG_F2FS_FS_POSIX_ACL
541static void __setattr_copy(struct inode *inode, const struct iattr *attr)
542{
543 struct f2fs_inode_info *fi = F2FS_I(inode);
544 unsigned int ia_valid = attr->ia_valid;
545
546 if (ia_valid & ATTR_UID)
547 inode->i_uid = attr->ia_uid;
548 if (ia_valid & ATTR_GID)
549 inode->i_gid = attr->ia_gid;
550 if (ia_valid & ATTR_ATIME)
551 inode->i_atime = timespec_trunc(attr->ia_atime,
552 inode->i_sb->s_time_gran);
553 if (ia_valid & ATTR_MTIME)
554 inode->i_mtime = timespec_trunc(attr->ia_mtime,
555 inode->i_sb->s_time_gran);
556 if (ia_valid & ATTR_CTIME)
557 inode->i_ctime = timespec_trunc(attr->ia_ctime,
558 inode->i_sb->s_time_gran);
559 if (ia_valid & ATTR_MODE) {
560 umode_t mode = attr->ia_mode;
561
562 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
563 mode &= ~S_ISGID;
564 set_acl_inode(fi, mode);
565 }
566}
567#else
568#define __setattr_copy setattr_copy
569#endif
570
571int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
572{
573 struct inode *inode = dentry->d_inode;
574 struct f2fs_inode_info *fi = F2FS_I(inode);
575 int err;
576
577 err = inode_change_ok(inode, attr);
578 if (err)
579 return err;
580
Chao Yu09db6a22014-09-15 18:02:09 +0800581 if (attr->ia_valid & ATTR_SIZE) {
Chao Yu09db6a22014-09-15 18:02:09 +0800582 if (attr->ia_size != i_size_read(inode)) {
583 truncate_setsize(inode, attr->ia_size);
584 f2fs_truncate(inode);
585 f2fs_balance_fs(F2FS_I_SB(inode));
586 } else {
587 /*
588 * giving a chance to truncate blocks past EOF which
589 * are fallocated with FALLOC_FL_KEEP_SIZE.
590 */
591 f2fs_truncate(inode);
592 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900593 }
594
595 __setattr_copy(inode, attr);
596
597 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800598 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900599 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
600 inode->i_mode = fi->i_acl_mode;
601 clear_inode_flag(fi, FI_ACL_MODE);
602 }
603 }
604
605 mark_inode_dirty(inode);
606 return err;
607}
608
609const struct inode_operations f2fs_file_inode_operations = {
610 .getattr = f2fs_getattr,
611 .setattr = f2fs_setattr,
612 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800613 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900614#ifdef CONFIG_F2FS_FS_XATTR
615 .setxattr = generic_setxattr,
616 .getxattr = generic_getxattr,
617 .listxattr = f2fs_listxattr,
618 .removexattr = generic_removexattr,
619#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900620 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900621};
622
623static void fill_zero(struct inode *inode, pgoff_t index,
624 loff_t start, loff_t len)
625{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700626 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900627 struct page *page;
628
629 if (!len)
630 return;
631
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900632 f2fs_balance_fs(sbi);
633
Gu Zhenge4795562013-09-27 18:08:30 +0800634 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900635 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800636 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900637
638 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900639 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900640 zero_user(page, start, len);
641 set_page_dirty(page);
642 f2fs_put_page(page, 1);
643 }
644}
645
646int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
647{
648 pgoff_t index;
649 int err;
650
651 for (index = pg_start; index < pg_end; index++) {
652 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900653
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900654 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900655 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900657 if (err == -ENOENT)
658 continue;
659 return err;
660 }
661
662 if (dn.data_blkaddr != NULL_ADDR)
663 truncate_data_blocks_range(&dn, 1);
664 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665 }
666 return 0;
667}
668
Chao Yua66c7b22013-11-22 16:52:50 +0800669static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900670{
671 pgoff_t pg_start, pg_end;
672 loff_t off_start, off_end;
673 int ret = 0;
674
Chao Yu14cecc52014-09-15 18:03:32 +0800675 if (!S_ISREG(inode->i_mode))
676 return -EOPNOTSUPP;
677
678 /* skip punching hole beyond i_size */
679 if (offset >= inode->i_size)
680 return ret;
681
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700682 if (f2fs_has_inline_data(inode)) {
683 ret = f2fs_convert_inline_inode(inode);
684 if (ret)
685 return ret;
686 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800687
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900688 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
689 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
690
691 off_start = offset & (PAGE_CACHE_SIZE - 1);
692 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
693
694 if (pg_start == pg_end) {
695 fill_zero(inode, pg_start, off_start,
696 off_end - off_start);
697 } else {
698 if (off_start)
699 fill_zero(inode, pg_start++, off_start,
700 PAGE_CACHE_SIZE - off_start);
701 if (off_end)
702 fill_zero(inode, pg_end, 0, off_end);
703
704 if (pg_start < pg_end) {
705 struct address_space *mapping = inode->i_mapping;
706 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700707 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500708
709 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900710
711 blk_start = pg_start << PAGE_CACHE_SHIFT;
712 blk_end = pg_end << PAGE_CACHE_SHIFT;
713 truncate_inode_pages_range(mapping, blk_start,
714 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900715
Gu Zhenge4795562013-09-27 18:08:30 +0800716 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900717 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800718 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719 }
720 }
721
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900722 return ret;
723}
724
725static int expand_inode_data(struct inode *inode, loff_t offset,
726 loff_t len, int mode)
727{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700728 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900729 pgoff_t index, pg_start, pg_end;
730 loff_t new_size = i_size_read(inode);
731 loff_t off_start, off_end;
732 int ret = 0;
733
Chao Yu497a0932014-08-04 10:11:17 +0800734 f2fs_balance_fs(sbi);
735
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900736 ret = inode_newsize_ok(inode, (len + offset));
737 if (ret)
738 return ret;
739
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700740 if (f2fs_has_inline_data(inode)) {
741 ret = f2fs_convert_inline_inode(inode);
742 if (ret)
743 return ret;
744 }
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900745
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900746 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
747 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
748
749 off_start = offset & (PAGE_CACHE_SIZE - 1);
750 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
751
Jaegeuk Kimead43272014-06-13 13:05:55 +0900752 f2fs_lock_op(sbi);
753
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 for (index = pg_start; index <= pg_end; index++) {
755 struct dnode_of_data dn;
756
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900757 if (index == pg_end && !off_end)
758 goto noalloc;
759
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900760 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800761 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800762 if (ret)
763 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900764noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900765 if (pg_start == pg_end)
766 new_size = offset + len;
767 else if (index == pg_start && off_start)
768 new_size = (index + 1) << PAGE_CACHE_SHIFT;
769 else if (index == pg_end)
770 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
771 else
772 new_size += PAGE_CACHE_SIZE;
773 }
774
775 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
776 i_size_read(inode) < new_size) {
777 i_size_write(inode, new_size);
778 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900779 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900780 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900781 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900782
783 return ret;
784}
785
786static long f2fs_fallocate(struct file *file, int mode,
787 loff_t offset, loff_t len)
788{
Al Viro6131ffa2013-02-27 16:59:05 -0500789 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900790 long ret;
791
792 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
793 return -EOPNOTSUPP;
794
Chao Yu3375f692014-01-28 10:29:26 +0800795 mutex_lock(&inode->i_mutex);
796
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900797 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800798 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900799 else
800 ret = expand_inode_data(inode, offset, len, mode);
801
Namjae Jeon3af60a42012-12-30 14:52:37 +0900802 if (!ret) {
803 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
804 mark_inode_dirty(inode);
805 }
Chao Yu3375f692014-01-28 10:29:26 +0800806
807 mutex_unlock(&inode->i_mutex);
808
Namjae Jeonc01e2852013-04-23 17:00:52 +0900809 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900810 return ret;
811}
812
813#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
814#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
815
816static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
817{
818 if (S_ISDIR(mode))
819 return flags;
820 else if (S_ISREG(mode))
821 return flags & F2FS_REG_FLMASK;
822 else
823 return flags & F2FS_OTHER_FLMASK;
824}
825
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700826static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900827{
Al Viro6131ffa2013-02-27 16:59:05 -0500828 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900829 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700830 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
831 return put_user(flags, (int __user *)arg);
832}
833
834static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
835{
836 struct inode *inode = file_inode(filp);
837 struct f2fs_inode_info *fi = F2FS_I(inode);
838 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
839 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900840 int ret;
841
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700842 ret = mnt_want_write_file(filp);
843 if (ret)
844 return ret;
845
846 if (!inode_owner_or_capable(inode)) {
847 ret = -EACCES;
848 goto out;
849 }
850
851 if (get_user(flags, (int __user *)arg)) {
852 ret = -EFAULT;
853 goto out;
854 }
855
856 flags = f2fs_mask_flags(inode->i_mode, flags);
857
858 mutex_lock(&inode->i_mutex);
859
860 oldflags = fi->i_flags;
861
862 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
863 if (!capable(CAP_LINUX_IMMUTABLE)) {
864 mutex_unlock(&inode->i_mutex);
865 ret = -EPERM;
866 goto out;
867 }
868 }
869
870 flags = flags & FS_FL_USER_MODIFIABLE;
871 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
872 fi->i_flags = flags;
873 mutex_unlock(&inode->i_mutex);
874
875 f2fs_set_inode_flags(inode);
876 inode->i_ctime = CURRENT_TIME;
877 mark_inode_dirty(inode);
878out:
879 mnt_drop_write_file(filp);
880 return ret;
881}
882
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700883static int f2fs_ioc_start_atomic_write(struct file *filp)
884{
885 struct inode *inode = file_inode(filp);
886 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
887
888 if (!inode_owner_or_capable(inode))
889 return -EACCES;
890
891 f2fs_balance_fs(sbi);
892
893 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
894
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700895 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700896}
897
898static int f2fs_ioc_commit_atomic_write(struct file *filp)
899{
900 struct inode *inode = file_inode(filp);
901 int ret;
902
903 if (!inode_owner_or_capable(inode))
904 return -EACCES;
905
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700906 if (f2fs_is_volatile_file(inode))
907 return 0;
908
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700909 ret = mnt_want_write_file(filp);
910 if (ret)
911 return ret;
912
913 if (f2fs_is_atomic_file(inode))
914 commit_inmem_pages(inode, false);
915
916 ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
917 mnt_drop_write_file(filp);
918 return ret;
919}
920
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700921static int f2fs_ioc_start_volatile_write(struct file *filp)
922{
923 struct inode *inode = file_inode(filp);
924
925 if (!inode_owner_or_capable(inode))
926 return -EACCES;
927
928 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700929
930 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700931}
932
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700933static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
934{
935 struct inode *inode = file_inode(filp);
936 struct super_block *sb = inode->i_sb;
937 struct request_queue *q = bdev_get_queue(sb->s_bdev);
938 struct fstrim_range range;
939 int ret;
940
941 if (!capable(CAP_SYS_ADMIN))
942 return -EPERM;
943
944 if (!blk_queue_discard(q))
945 return -EOPNOTSUPP;
946
947 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
948 sizeof(range)))
949 return -EFAULT;
950
951 range.minlen = max((unsigned int)range.minlen,
952 q->limits.discard_granularity);
953 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
954 if (ret < 0)
955 return ret;
956
957 if (copy_to_user((struct fstrim_range __user *)arg, &range,
958 sizeof(range)))
959 return -EFAULT;
960 return 0;
961}
962
963long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
964{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900965 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900966 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700967 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900968 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700969 return f2fs_ioc_setflags(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700970 case F2FS_IOC_START_ATOMIC_WRITE:
971 return f2fs_ioc_start_atomic_write(filp);
972 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
973 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700974 case F2FS_IOC_START_VOLATILE_WRITE:
975 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700976 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700977 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900978 default:
979 return -ENOTTY;
980 }
981}
982
Namjae Jeone9750822013-02-04 23:41:41 +0900983#ifdef CONFIG_COMPAT
984long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
985{
986 switch (cmd) {
987 case F2FS_IOC32_GETFLAGS:
988 cmd = F2FS_IOC_GETFLAGS;
989 break;
990 case F2FS_IOC32_SETFLAGS:
991 cmd = F2FS_IOC_SETFLAGS;
992 break;
993 default:
994 return -ENOIOCTLCMD;
995 }
996 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
997}
998#endif
999
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001000const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001001 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001002 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -04001003 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -04001004 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04001005 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001006 .open = generic_file_open,
1007 .mmap = f2fs_file_mmap,
1008 .fsync = f2fs_sync_file,
1009 .fallocate = f2fs_fallocate,
1010 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001011#ifdef CONFIG_COMPAT
1012 .compat_ioctl = f2fs_compat_ioctl,
1013#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001014 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001015 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001016};