blob: 54722a0a5be58c6b8e83fb447c70c23b5110aa85 [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)) {
480 truncate_inline_data(ipage, from);
481 update_inode(inode, ipage);
482 f2fs_put_page(ipage, 1);
483 goto out;
484 }
485
486 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900487 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900488 if (err) {
489 if (err == -ENOENT)
490 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700491 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700492 }
493
Chao Yu6403eb12014-04-26 19:59:52 +0800494 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900495
496 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700497 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900498
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900499 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
500 truncate_data_blocks_range(&dn, count);
501 free_from += count;
502 }
503
504 f2fs_put_dnode(&dn);
505free_next:
506 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700507
508 /* lastly zero out the first data page */
509 if (!err)
510 err = truncate_partial_data_page(inode, from);
511out:
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700512 if (lock)
513 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900514
Namjae Jeon51dd6242013-04-20 01:28:52 +0900515 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900516 return err;
517}
518
519void f2fs_truncate(struct inode *inode)
520{
521 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
522 S_ISLNK(inode->i_mode)))
523 return;
524
Namjae Jeon51dd6242013-04-20 01:28:52 +0900525 trace_f2fs_truncate(inode);
526
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700527 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900528 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
529 mark_inode_dirty(inode);
530 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900531}
532
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900533int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900534 struct dentry *dentry, struct kstat *stat)
535{
536 struct inode *inode = dentry->d_inode;
537 generic_fillattr(inode, stat);
538 stat->blocks <<= 3;
539 return 0;
540}
541
542#ifdef CONFIG_F2FS_FS_POSIX_ACL
543static void __setattr_copy(struct inode *inode, const struct iattr *attr)
544{
545 struct f2fs_inode_info *fi = F2FS_I(inode);
546 unsigned int ia_valid = attr->ia_valid;
547
548 if (ia_valid & ATTR_UID)
549 inode->i_uid = attr->ia_uid;
550 if (ia_valid & ATTR_GID)
551 inode->i_gid = attr->ia_gid;
552 if (ia_valid & ATTR_ATIME)
553 inode->i_atime = timespec_trunc(attr->ia_atime,
554 inode->i_sb->s_time_gran);
555 if (ia_valid & ATTR_MTIME)
556 inode->i_mtime = timespec_trunc(attr->ia_mtime,
557 inode->i_sb->s_time_gran);
558 if (ia_valid & ATTR_CTIME)
559 inode->i_ctime = timespec_trunc(attr->ia_ctime,
560 inode->i_sb->s_time_gran);
561 if (ia_valid & ATTR_MODE) {
562 umode_t mode = attr->ia_mode;
563
564 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
565 mode &= ~S_ISGID;
566 set_acl_inode(fi, mode);
567 }
568}
569#else
570#define __setattr_copy setattr_copy
571#endif
572
573int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
574{
575 struct inode *inode = dentry->d_inode;
576 struct f2fs_inode_info *fi = F2FS_I(inode);
577 int err;
578
579 err = inode_change_ok(inode, attr);
580 if (err)
581 return err;
582
Chao Yu09db6a22014-09-15 18:02:09 +0800583 if (attr->ia_valid & ATTR_SIZE) {
Chao Yu09db6a22014-09-15 18:02:09 +0800584 if (attr->ia_size != i_size_read(inode)) {
585 truncate_setsize(inode, attr->ia_size);
586 f2fs_truncate(inode);
587 f2fs_balance_fs(F2FS_I_SB(inode));
588 } else {
589 /*
590 * giving a chance to truncate blocks past EOF which
591 * are fallocated with FALLOC_FL_KEEP_SIZE.
592 */
593 f2fs_truncate(inode);
594 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900595 }
596
597 __setattr_copy(inode, attr);
598
599 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800600 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900601 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
602 inode->i_mode = fi->i_acl_mode;
603 clear_inode_flag(fi, FI_ACL_MODE);
604 }
605 }
606
607 mark_inode_dirty(inode);
608 return err;
609}
610
611const struct inode_operations f2fs_file_inode_operations = {
612 .getattr = f2fs_getattr,
613 .setattr = f2fs_setattr,
614 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800615 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900616#ifdef CONFIG_F2FS_FS_XATTR
617 .setxattr = generic_setxattr,
618 .getxattr = generic_getxattr,
619 .listxattr = f2fs_listxattr,
620 .removexattr = generic_removexattr,
621#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900622 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900623};
624
625static void fill_zero(struct inode *inode, pgoff_t index,
626 loff_t start, loff_t len)
627{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700628 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900629 struct page *page;
630
631 if (!len)
632 return;
633
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900634 f2fs_balance_fs(sbi);
635
Gu Zhenge4795562013-09-27 18:08:30 +0800636 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900637 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800638 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900639
640 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900641 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900642 zero_user(page, start, len);
643 set_page_dirty(page);
644 f2fs_put_page(page, 1);
645 }
646}
647
648int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
649{
650 pgoff_t index;
651 int err;
652
653 for (index = pg_start; index < pg_end; index++) {
654 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900655
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900657 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900658 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900659 if (err == -ENOENT)
660 continue;
661 return err;
662 }
663
664 if (dn.data_blkaddr != NULL_ADDR)
665 truncate_data_blocks_range(&dn, 1);
666 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900667 }
668 return 0;
669}
670
Chao Yua66c7b22013-11-22 16:52:50 +0800671static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900672{
673 pgoff_t pg_start, pg_end;
674 loff_t off_start, off_end;
675 int ret = 0;
676
Chao Yu14cecc52014-09-15 18:03:32 +0800677 if (!S_ISREG(inode->i_mode))
678 return -EOPNOTSUPP;
679
680 /* skip punching hole beyond i_size */
681 if (offset >= inode->i_size)
682 return ret;
683
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700684 if (f2fs_has_inline_data(inode)) {
685 ret = f2fs_convert_inline_inode(inode);
686 if (ret)
687 return ret;
688 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800689
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900690 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
691 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
692
693 off_start = offset & (PAGE_CACHE_SIZE - 1);
694 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
695
696 if (pg_start == pg_end) {
697 fill_zero(inode, pg_start, off_start,
698 off_end - off_start);
699 } else {
700 if (off_start)
701 fill_zero(inode, pg_start++, off_start,
702 PAGE_CACHE_SIZE - off_start);
703 if (off_end)
704 fill_zero(inode, pg_end, 0, off_end);
705
706 if (pg_start < pg_end) {
707 struct address_space *mapping = inode->i_mapping;
708 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700709 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500710
711 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900712
713 blk_start = pg_start << PAGE_CACHE_SHIFT;
714 blk_end = pg_end << PAGE_CACHE_SHIFT;
715 truncate_inode_pages_range(mapping, blk_start,
716 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900717
Gu Zhenge4795562013-09-27 18:08:30 +0800718 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800720 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900721 }
722 }
723
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900724 return ret;
725}
726
727static int expand_inode_data(struct inode *inode, loff_t offset,
728 loff_t len, int mode)
729{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700730 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900731 pgoff_t index, pg_start, pg_end;
732 loff_t new_size = i_size_read(inode);
733 loff_t off_start, off_end;
734 int ret = 0;
735
Chao Yu497a0932014-08-04 10:11:17 +0800736 f2fs_balance_fs(sbi);
737
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900738 ret = inode_newsize_ok(inode, (len + offset));
739 if (ret)
740 return ret;
741
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700742 if (f2fs_has_inline_data(inode)) {
743 ret = f2fs_convert_inline_inode(inode);
744 if (ret)
745 return ret;
746 }
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900747
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900748 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
749 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
750
751 off_start = offset & (PAGE_CACHE_SIZE - 1);
752 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
753
Jaegeuk Kimead43272014-06-13 13:05:55 +0900754 f2fs_lock_op(sbi);
755
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900756 for (index = pg_start; index <= pg_end; index++) {
757 struct dnode_of_data dn;
758
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900759 if (index == pg_end && !off_end)
760 goto noalloc;
761
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900762 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800763 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800764 if (ret)
765 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900766noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900767 if (pg_start == pg_end)
768 new_size = offset + len;
769 else if (index == pg_start && off_start)
770 new_size = (index + 1) << PAGE_CACHE_SHIFT;
771 else if (index == pg_end)
772 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
773 else
774 new_size += PAGE_CACHE_SIZE;
775 }
776
777 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
778 i_size_read(inode) < new_size) {
779 i_size_write(inode, new_size);
780 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900781 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900782 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900783 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900784
785 return ret;
786}
787
788static long f2fs_fallocate(struct file *file, int mode,
789 loff_t offset, loff_t len)
790{
Al Viro6131ffa2013-02-27 16:59:05 -0500791 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900792 long ret;
793
794 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
795 return -EOPNOTSUPP;
796
Chao Yu3375f692014-01-28 10:29:26 +0800797 mutex_lock(&inode->i_mutex);
798
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900799 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800800 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900801 else
802 ret = expand_inode_data(inode, offset, len, mode);
803
Namjae Jeon3af60a42012-12-30 14:52:37 +0900804 if (!ret) {
805 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
806 mark_inode_dirty(inode);
807 }
Chao Yu3375f692014-01-28 10:29:26 +0800808
809 mutex_unlock(&inode->i_mutex);
810
Namjae Jeonc01e2852013-04-23 17:00:52 +0900811 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900812 return ret;
813}
814
815#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
816#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
817
818static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
819{
820 if (S_ISDIR(mode))
821 return flags;
822 else if (S_ISREG(mode))
823 return flags & F2FS_REG_FLMASK;
824 else
825 return flags & F2FS_OTHER_FLMASK;
826}
827
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700828static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900829{
Al Viro6131ffa2013-02-27 16:59:05 -0500830 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900831 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700832 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
833 return put_user(flags, (int __user *)arg);
834}
835
836static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
837{
838 struct inode *inode = file_inode(filp);
839 struct f2fs_inode_info *fi = F2FS_I(inode);
840 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
841 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900842 int ret;
843
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700844 ret = mnt_want_write_file(filp);
845 if (ret)
846 return ret;
847
848 if (!inode_owner_or_capable(inode)) {
849 ret = -EACCES;
850 goto out;
851 }
852
853 if (get_user(flags, (int __user *)arg)) {
854 ret = -EFAULT;
855 goto out;
856 }
857
858 flags = f2fs_mask_flags(inode->i_mode, flags);
859
860 mutex_lock(&inode->i_mutex);
861
862 oldflags = fi->i_flags;
863
864 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
865 if (!capable(CAP_LINUX_IMMUTABLE)) {
866 mutex_unlock(&inode->i_mutex);
867 ret = -EPERM;
868 goto out;
869 }
870 }
871
872 flags = flags & FS_FL_USER_MODIFIABLE;
873 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
874 fi->i_flags = flags;
875 mutex_unlock(&inode->i_mutex);
876
877 f2fs_set_inode_flags(inode);
878 inode->i_ctime = CURRENT_TIME;
879 mark_inode_dirty(inode);
880out:
881 mnt_drop_write_file(filp);
882 return ret;
883}
884
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700885static int f2fs_ioc_start_atomic_write(struct file *filp)
886{
887 struct inode *inode = file_inode(filp);
888 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
889
890 if (!inode_owner_or_capable(inode))
891 return -EACCES;
892
893 f2fs_balance_fs(sbi);
894
895 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
896
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700897 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700898}
899
900static int f2fs_ioc_commit_atomic_write(struct file *filp)
901{
902 struct inode *inode = file_inode(filp);
903 int ret;
904
905 if (!inode_owner_or_capable(inode))
906 return -EACCES;
907
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700908 if (f2fs_is_volatile_file(inode))
909 return 0;
910
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700911 ret = mnt_want_write_file(filp);
912 if (ret)
913 return ret;
914
915 if (f2fs_is_atomic_file(inode))
916 commit_inmem_pages(inode, false);
917
918 ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
919 mnt_drop_write_file(filp);
920 return ret;
921}
922
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700923static int f2fs_ioc_start_volatile_write(struct file *filp)
924{
925 struct inode *inode = file_inode(filp);
926
927 if (!inode_owner_or_capable(inode))
928 return -EACCES;
929
930 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700931
932 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700933}
934
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700935static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
936{
937 struct inode *inode = file_inode(filp);
938 struct super_block *sb = inode->i_sb;
939 struct request_queue *q = bdev_get_queue(sb->s_bdev);
940 struct fstrim_range range;
941 int ret;
942
943 if (!capable(CAP_SYS_ADMIN))
944 return -EPERM;
945
946 if (!blk_queue_discard(q))
947 return -EOPNOTSUPP;
948
949 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
950 sizeof(range)))
951 return -EFAULT;
952
953 range.minlen = max((unsigned int)range.minlen,
954 q->limits.discard_granularity);
955 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
956 if (ret < 0)
957 return ret;
958
959 if (copy_to_user((struct fstrim_range __user *)arg, &range,
960 sizeof(range)))
961 return -EFAULT;
962 return 0;
963}
964
965long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
966{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900967 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900968 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700969 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900970 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700971 return f2fs_ioc_setflags(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700972 case F2FS_IOC_START_ATOMIC_WRITE:
973 return f2fs_ioc_start_atomic_write(filp);
974 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
975 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700976 case F2FS_IOC_START_VOLATILE_WRITE:
977 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700978 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -0700979 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900980 default:
981 return -ENOTTY;
982 }
983}
984
Namjae Jeone9750822013-02-04 23:41:41 +0900985#ifdef CONFIG_COMPAT
986long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
987{
988 switch (cmd) {
989 case F2FS_IOC32_GETFLAGS:
990 cmd = F2FS_IOC_GETFLAGS;
991 break;
992 case F2FS_IOC32_SETFLAGS:
993 cmd = F2FS_IOC_SETFLAGS;
994 break;
995 default:
996 return -ENOIOCTLCMD;
997 }
998 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
999}
1000#endif
1001
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001002const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001003 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001004 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -04001005 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -04001006 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04001007 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001008 .open = generic_file_open,
1009 .mmap = f2fs_file_mmap,
1010 .fsync = f2fs_sync_file,
1011 .fallocate = f2fs_fallocate,
1012 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001013#ifdef CONFIG_COMPAT
1014 .compat_ioctl = f2fs_compat_ioctl,
1015#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001016 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001017 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001018};