blob: edc3ce867e9c12430948ddc3f40f33a4cbaed00e [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
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900141int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
142{
143 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900144 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700145 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700146 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900147 int ret = 0;
148 bool need_cp = false;
149 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900150 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900151 .nr_to_write = LONG_MAX,
152 .for_reclaim = 0,
153 };
154
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900155 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900156 return 0;
157
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900158 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700159
160 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700161 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700162 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900163 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700164 clear_inode_flag(fi, FI_NEED_IPU);
165
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900166 if (ret) {
167 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900168 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900169 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900170
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700171 /*
172 * if there is no written data, don't waste time to write recovery info.
173 */
174 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700175 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700176 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
177
178 /* But we need to avoid that there are some inode updates */
179 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) {
180 f2fs_put_page(i, 0);
181 goto go_write;
182 }
183 f2fs_put_page(i, 0);
184
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700185 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700186 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700187 goto flush_out;
188 goto out;
189 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700190go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900191 /* guarantee free sections for fsync */
192 f2fs_balance_fs(sbi);
193
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900194 /*
195 * Both of fdatasync() and fsync() are able to be recovered from
196 * sudden-power-off.
197 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800198 down_read(&fi->i_sem);
199 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900200 up_read(&fi->i_sem);
201
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900202 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900203 nid_t pino;
204
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205 /* all the dirty node pages should be flushed for POR */
206 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900207
208 down_write(&fi->i_sem);
209 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900210 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
211 get_parent_ino(inode, &pino)) {
212 F2FS_I(inode)->i_pino = pino;
213 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900214 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900215 mark_inode_dirty_sync(inode);
216 ret = f2fs_write_inode(inode, NULL);
217 if (ret)
218 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900219 } else {
220 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900221 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900222 } else {
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700223sync_nodes:
224 sync_node_pages(sbi, ino, &wbc);
225
226 if (need_inode_block_update(sbi, ino)) {
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900227 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900228 ret = f2fs_write_inode(inode, NULL);
229 if (ret)
230 goto out;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700231 goto sync_nodes;
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900232 }
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700233
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700234 ret = wait_on_node_pages_writeback(sbi, ino);
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900235 if (ret)
236 goto out;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700237
238 /* once recovery info is written, don't need to tack this */
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700239 remove_dirty_inode(sbi, ino, APPEND_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700240 clear_inode_flag(fi, FI_APPEND_WRITE);
241flush_out:
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700242 remove_dirty_inode(sbi, ino, UPDATE_INO);
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700243 clear_inode_flag(fi, FI_UPDATE_WRITE);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700244 ret = f2fs_issue_flush(F2FS_I_SB(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900245 }
246out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900247 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900248 return ret;
249}
250
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900251static pgoff_t __get_first_dirty_index(struct address_space *mapping,
252 pgoff_t pgofs, int whence)
253{
254 struct pagevec pvec;
255 int nr_pages;
256
257 if (whence != SEEK_DATA)
258 return 0;
259
260 /* find first dirty page index */
261 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700262 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
263 PAGECACHE_TAG_DIRTY, 1);
264 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900265 pagevec_release(&pvec);
266 return pgofs;
267}
268
269static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
270 int whence)
271{
272 switch (whence) {
273 case SEEK_DATA:
274 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
275 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
276 return true;
277 break;
278 case SEEK_HOLE:
279 if (blkaddr == NULL_ADDR)
280 return true;
281 break;
282 }
283 return false;
284}
285
Chao Yu267378d2014-04-23 14:10:24 +0800286static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
287{
288 struct inode *inode = file->f_mapping->host;
289 loff_t maxbytes = inode->i_sb->s_maxbytes;
290 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900291 pgoff_t pgofs, end_offset, dirty;
292 loff_t data_ofs = offset;
293 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800294 int err = 0;
295
296 mutex_lock(&inode->i_mutex);
297
298 isize = i_size_read(inode);
299 if (offset >= isize)
300 goto fail;
301
302 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800303 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800304 if (whence == SEEK_HOLE)
305 data_ofs = isize;
306 goto found;
307 }
308
309 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
310
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900311 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
312
Chao Yu267378d2014-04-23 14:10:24 +0800313 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
314 set_new_dnode(&dn, inode, NULL, NULL, 0);
315 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
316 if (err && err != -ENOENT) {
317 goto fail;
318 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900319 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800320 if (whence == SEEK_DATA) {
321 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
322 F2FS_I(inode));
323 continue;
324 } else {
325 goto found;
326 }
327 }
328
Chao Yu6b2920a2014-07-07 11:21:59 +0800329 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800330
331 /* find data/hole in dnode block */
332 for (; dn.ofs_in_node < end_offset;
333 dn.ofs_in_node++, pgofs++,
334 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
335 block_t blkaddr;
336 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
337
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900338 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800339 f2fs_put_dnode(&dn);
340 goto found;
341 }
342 }
343 f2fs_put_dnode(&dn);
344 }
345
346 if (whence == SEEK_DATA)
347 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800348found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900349 if (whence == SEEK_HOLE && data_ofs > isize)
350 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800351 mutex_unlock(&inode->i_mutex);
352 return vfs_setpos(file, data_ofs, maxbytes);
353fail:
354 mutex_unlock(&inode->i_mutex);
355 return -ENXIO;
356}
357
358static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
359{
360 struct inode *inode = file->f_mapping->host;
361 loff_t maxbytes = inode->i_sb->s_maxbytes;
362
363 switch (whence) {
364 case SEEK_SET:
365 case SEEK_CUR:
366 case SEEK_END:
367 return generic_file_llseek_size(file, offset, whence,
368 maxbytes, i_size_read(inode));
369 case SEEK_DATA:
370 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700371 if (offset < 0)
372 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800373 return f2fs_seek_block(file, offset, whence);
374 }
375
376 return -EINVAL;
377}
378
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900379static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
380{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700381 struct inode *inode = file_inode(file);
382
383 /* we don't need to use inline_data strictly */
384 if (f2fs_has_inline_data(inode)) {
385 int err = f2fs_convert_inline_inode(inode);
386 if (err)
387 return err;
388 }
389
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900390 file_accessed(file);
391 vma->vm_ops = &f2fs_file_vm_ops;
392 return 0;
393}
394
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900395int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900396{
397 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700398 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900399 struct f2fs_node *raw_node;
400 __le32 *addr;
401
Gu Zheng45590712013-07-15 17:57:38 +0800402 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900403 addr = blkaddr_in_node(raw_node) + ofs;
404
Chris Fries6c311ec2014-01-17 14:44:39 -0600405 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900406 block_t blkaddr = le32_to_cpu(*addr);
407 if (blkaddr == NULL_ADDR)
408 continue;
409
410 update_extent_cache(NULL_ADDR, dn);
411 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900412 nr_free++;
413 }
414 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900415 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900416 set_page_dirty(dn->node_page);
417 sync_inode_page(dn);
418 }
419 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900420
421 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
422 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900423 return nr_free;
424}
425
426void truncate_data_blocks(struct dnode_of_data *dn)
427{
428 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
429}
430
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700431static int truncate_partial_data_page(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900432{
433 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
434 struct page *page;
435
436 if (!offset)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700437 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900438
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900439 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900440 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700441 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900442
443 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800444 if (unlikely(!PageUptodate(page) ||
445 page->mapping != inode->i_mapping))
446 goto out;
447
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900448 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900449 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
450 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800451out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900452 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700453 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900454}
455
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700456int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900457{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700458 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900459 unsigned int blocksize = inode->i_sb->s_blocksize;
460 struct dnode_of_data dn;
461 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800462 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700463 struct page *ipage;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900464
Namjae Jeon51dd6242013-04-20 01:28:52 +0900465 trace_f2fs_truncate_blocks_enter(inode, from);
466
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467 free_from = (pgoff_t)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700468 ((from + blocksize - 1) >> (sbi->log_blocksize));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900469
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700470 if (lock)
471 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800472
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700473 ipage = get_node_page(sbi, inode->i_ino);
474 if (IS_ERR(ipage)) {
475 err = PTR_ERR(ipage);
476 goto out;
477 }
478
479 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700480 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 Kim764d2c82014-11-11 11:01:01 -0800505out:
506 if (lock)
507 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700508
509 /* lastly zero out the first data page */
510 if (!err)
511 err = truncate_partial_data_page(inode, from);
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};