blob: 0dfcef53a6ed830e442f8c174ea166b0d1b2eabd [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>
22
23#include "f2fs.h"
24#include "node.h"
25#include "segment.h"
26#include "xattr.h"
27#include "acl.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090029
30static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
31 struct vm_fault *vmf)
32{
33 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050034 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090036 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080037 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090038
39 f2fs_balance_fs(sbi);
40
41 sb_start_pagefault(inode->i_sb);
42
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090043 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080044 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090045 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +080046 err = f2fs_reserve_block(&dn, page->index);
Gu Zhenge4795562013-09-27 18:08:30 +080047 f2fs_unlock_op(sbi);
Huajun Lib6009652013-11-10 23:13:18 +080048 if (err)
49 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090050
Namjae Jeon9851e6e2013-04-28 09:04:18 +090051 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090052 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090053 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090054 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090055 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090056 unlock_page(page);
57 err = -EFAULT;
58 goto out;
59 }
60
61 /*
62 * check to see if the page is mapped already (no holes)
63 */
64 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090065 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090066
67 /* page is wholly or partially inside EOF */
68 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
69 unsigned offset;
70 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
71 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
72 }
73 set_page_dirty(page);
74 SetPageUptodate(page);
75
Jaegeuk Kime943a102013-10-25 14:26:31 +090076 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090077mapped:
78 /* fill the page */
79 wait_on_page_writeback(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090080out:
81 sb_end_pagefault(inode->i_sb);
82 return block_page_mkwrite_return(err);
83}
84
85static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090086 .fault = filemap_fault,
87 .page_mkwrite = f2fs_vm_page_mkwrite,
88 .remap_pages = generic_file_remap_pages,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090089};
90
Jaegeuk Kim354a3392013-06-14 08:52:35 +090091static int get_parent_ino(struct inode *inode, nid_t *pino)
92{
93 struct dentry *dentry;
94
95 inode = igrab(inode);
96 dentry = d_find_any_alias(inode);
97 iput(inode);
98 if (!dentry)
99 return 0;
100
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900101 if (update_dent_inode(inode, &dentry->d_name)) {
102 dput(dentry);
103 return 0;
104 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900105
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900106 *pino = parent_ino(dentry);
107 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900108 return 1;
109}
110
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900111int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
112{
113 struct inode *inode = file->f_mapping->host;
114 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900115 int ret = 0;
116 bool need_cp = false;
117 struct writeback_control wbc = {
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +0900118 .sync_mode = WB_SYNC_NONE,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900119 .nr_to_write = LONG_MAX,
120 .for_reclaim = 0,
121 };
122
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900123 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900124 return 0;
125
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900126 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900127 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900128 if (ret) {
129 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900130 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900131 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900132
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900133 /* guarantee free sections for fsync */
134 f2fs_balance_fs(sbi);
135
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900136 mutex_lock(&inode->i_mutex);
137
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900138 /*
139 * Both of fdatasync() and fsync() are able to be recovered from
140 * sudden-power-off.
141 */
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900142 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
143 need_cp = true;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900144 else if (file_wrong_pino(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900145 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900146 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900147 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900148 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900149 need_cp = true;
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900150 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900151 need_cp = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900152
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900153 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900154 nid_t pino;
155
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900156 F2FS_I(inode)->xattr_ver = 0;
157
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900158 /* all the dirty node pages should be flushed for POR */
159 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900160 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
161 get_parent_ino(inode, &pino)) {
162 F2FS_I(inode)->i_pino = pino;
163 file_got_pino(inode);
164 mark_inode_dirty_sync(inode);
165 ret = f2fs_write_inode(inode, NULL);
166 if (ret)
167 goto out;
168 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900169 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900170 /* if there is no written node page, write its inode page */
171 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900172 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900173 ret = f2fs_write_inode(inode, NULL);
174 if (ret)
175 goto out;
176 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900177 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
178 if (ret)
179 goto out;
Jaegeuk Kimae51fb32013-03-16 11:13:04 +0900180 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900181 }
182out:
183 mutex_unlock(&inode->i_mutex);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900184 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900185 return ret;
186}
187
188static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
189{
190 file_accessed(file);
191 vma->vm_ops = &f2fs_file_vm_ops;
192 return 0;
193}
194
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900195int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900196{
197 int nr_free = 0, ofs = dn->ofs_in_node;
198 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
199 struct f2fs_node *raw_node;
200 __le32 *addr;
201
Gu Zheng45590712013-07-15 17:57:38 +0800202 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900203 addr = blkaddr_in_node(raw_node) + ofs;
204
Chris Fries6c311ec2014-01-17 14:44:39 -0600205 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900206 block_t blkaddr = le32_to_cpu(*addr);
207 if (blkaddr == NULL_ADDR)
208 continue;
209
210 update_extent_cache(NULL_ADDR, dn);
211 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900212 nr_free++;
213 }
214 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900215 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900216 set_page_dirty(dn->node_page);
217 sync_inode_page(dn);
218 }
219 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900220
221 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
222 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900223 return nr_free;
224}
225
226void truncate_data_blocks(struct dnode_of_data *dn)
227{
228 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
229}
230
231static void truncate_partial_data_page(struct inode *inode, u64 from)
232{
233 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
234 struct page *page;
235
236 if (!offset)
237 return;
238
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900239 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900240 if (IS_ERR(page))
241 return;
242
243 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900244 if (unlikely(page->mapping != inode->i_mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900245 f2fs_put_page(page, 1);
246 return;
247 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900248 wait_on_page_writeback(page);
249 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
250 set_page_dirty(page);
251 f2fs_put_page(page, 1);
252}
253
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900254int truncate_blocks(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900255{
256 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
257 unsigned int blocksize = inode->i_sb->s_blocksize;
258 struct dnode_of_data dn;
259 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800260 int count = 0, err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900261
Namjae Jeon51dd6242013-04-20 01:28:52 +0900262 trace_f2fs_truncate_blocks_enter(inode, from);
263
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900264 if (f2fs_has_inline_data(inode))
265 goto done;
266
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900267 free_from = (pgoff_t)
268 ((from + blocksize - 1) >> (sbi->log_blocksize));
269
Gu Zhenge4795562013-09-27 18:08:30 +0800270 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800271
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900272 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900273 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900274 if (err) {
275 if (err == -ENOENT)
276 goto free_next;
Gu Zhenge4795562013-09-27 18:08:30 +0800277 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900278 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900279 return err;
280 }
281
282 if (IS_INODE(dn.node_page))
Jaegeuk Kimde936532013-08-12 21:08:03 +0900283 count = ADDRS_PER_INODE(F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900284 else
285 count = ADDRS_PER_BLOCK;
286
287 count -= dn.ofs_in_node;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900288 f2fs_bug_on(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900289
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900290 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
291 truncate_data_blocks_range(&dn, count);
292 free_from += count;
293 }
294
295 f2fs_put_dnode(&dn);
296free_next:
297 err = truncate_inode_blocks(inode, free_from);
Gu Zhenge4795562013-09-27 18:08:30 +0800298 f2fs_unlock_op(sbi);
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900299done:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900300 /* lastly zero out the first data page */
301 truncate_partial_data_page(inode, from);
302
Namjae Jeon51dd6242013-04-20 01:28:52 +0900303 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900304 return err;
305}
306
307void f2fs_truncate(struct inode *inode)
308{
309 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
310 S_ISLNK(inode->i_mode)))
311 return;
312
Namjae Jeon51dd6242013-04-20 01:28:52 +0900313 trace_f2fs_truncate(inode);
314
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900315 if (!truncate_blocks(inode, i_size_read(inode))) {
316 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
317 mark_inode_dirty(inode);
318 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900319}
320
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900321int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900322 struct dentry *dentry, struct kstat *stat)
323{
324 struct inode *inode = dentry->d_inode;
325 generic_fillattr(inode, stat);
326 stat->blocks <<= 3;
327 return 0;
328}
329
330#ifdef CONFIG_F2FS_FS_POSIX_ACL
331static void __setattr_copy(struct inode *inode, const struct iattr *attr)
332{
333 struct f2fs_inode_info *fi = F2FS_I(inode);
334 unsigned int ia_valid = attr->ia_valid;
335
336 if (ia_valid & ATTR_UID)
337 inode->i_uid = attr->ia_uid;
338 if (ia_valid & ATTR_GID)
339 inode->i_gid = attr->ia_gid;
340 if (ia_valid & ATTR_ATIME)
341 inode->i_atime = timespec_trunc(attr->ia_atime,
342 inode->i_sb->s_time_gran);
343 if (ia_valid & ATTR_MTIME)
344 inode->i_mtime = timespec_trunc(attr->ia_mtime,
345 inode->i_sb->s_time_gran);
346 if (ia_valid & ATTR_CTIME)
347 inode->i_ctime = timespec_trunc(attr->ia_ctime,
348 inode->i_sb->s_time_gran);
349 if (ia_valid & ATTR_MODE) {
350 umode_t mode = attr->ia_mode;
351
352 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
353 mode &= ~S_ISGID;
354 set_acl_inode(fi, mode);
355 }
356}
357#else
358#define __setattr_copy setattr_copy
359#endif
360
361int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
362{
363 struct inode *inode = dentry->d_inode;
364 struct f2fs_inode_info *fi = F2FS_I(inode);
365 int err;
366
367 err = inode_change_ok(inode, attr);
368 if (err)
369 return err;
370
371 if ((attr->ia_valid & ATTR_SIZE) &&
372 attr->ia_size != i_size_read(inode)) {
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900373 err = f2fs_convert_inline_data(inode, attr->ia_size);
374 if (err)
375 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800376
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900377 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900378 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900379 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900380 }
381
382 __setattr_copy(inode, attr);
383
384 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800385 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900386 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
387 inode->i_mode = fi->i_acl_mode;
388 clear_inode_flag(fi, FI_ACL_MODE);
389 }
390 }
391
392 mark_inode_dirty(inode);
393 return err;
394}
395
396const struct inode_operations f2fs_file_inode_operations = {
397 .getattr = f2fs_getattr,
398 .setattr = f2fs_setattr,
399 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800400 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900401#ifdef CONFIG_F2FS_FS_XATTR
402 .setxattr = generic_setxattr,
403 .getxattr = generic_getxattr,
404 .listxattr = f2fs_listxattr,
405 .removexattr = generic_removexattr,
406#endif
407};
408
409static void fill_zero(struct inode *inode, pgoff_t index,
410 loff_t start, loff_t len)
411{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900412 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900413 struct page *page;
414
415 if (!len)
416 return;
417
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900418 f2fs_balance_fs(sbi);
419
Gu Zhenge4795562013-09-27 18:08:30 +0800420 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900421 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800422 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900423
424 if (!IS_ERR(page)) {
425 wait_on_page_writeback(page);
426 zero_user(page, start, len);
427 set_page_dirty(page);
428 f2fs_put_page(page, 1);
429 }
430}
431
432int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
433{
434 pgoff_t index;
435 int err;
436
437 for (index = pg_start; index < pg_end; index++) {
438 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900439
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900440 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900441 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900442 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900443 if (err == -ENOENT)
444 continue;
445 return err;
446 }
447
448 if (dn.data_blkaddr != NULL_ADDR)
449 truncate_data_blocks_range(&dn, 1);
450 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900451 }
452 return 0;
453}
454
Chao Yua66c7b22013-11-22 16:52:50 +0800455static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900456{
457 pgoff_t pg_start, pg_end;
458 loff_t off_start, off_end;
459 int ret = 0;
460
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900461 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1);
Jaegeuk Kim8230a0a2013-12-27 11:13:21 +0900462 if (ret)
463 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800464
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900465 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
466 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
467
468 off_start = offset & (PAGE_CACHE_SIZE - 1);
469 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
470
471 if (pg_start == pg_end) {
472 fill_zero(inode, pg_start, off_start,
473 off_end - off_start);
474 } else {
475 if (off_start)
476 fill_zero(inode, pg_start++, off_start,
477 PAGE_CACHE_SIZE - off_start);
478 if (off_end)
479 fill_zero(inode, pg_end, 0, off_end);
480
481 if (pg_start < pg_end) {
482 struct address_space *mapping = inode->i_mapping;
483 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500484 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
485
486 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900487
488 blk_start = pg_start << PAGE_CACHE_SHIFT;
489 blk_end = pg_end << PAGE_CACHE_SHIFT;
490 truncate_inode_pages_range(mapping, blk_start,
491 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900492
Gu Zhenge4795562013-09-27 18:08:30 +0800493 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900494 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800495 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900496 }
497 }
498
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900499 return ret;
500}
501
502static int expand_inode_data(struct inode *inode, loff_t offset,
503 loff_t len, int mode)
504{
505 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
506 pgoff_t index, pg_start, pg_end;
507 loff_t new_size = i_size_read(inode);
508 loff_t off_start, off_end;
509 int ret = 0;
510
511 ret = inode_newsize_ok(inode, (len + offset));
512 if (ret)
513 return ret;
514
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900515 ret = f2fs_convert_inline_data(inode, offset + len);
516 if (ret)
517 return ret;
518
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900519 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
520 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
521
522 off_start = offset & (PAGE_CACHE_SIZE - 1);
523 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
524
525 for (index = pg_start; index <= pg_end; index++) {
526 struct dnode_of_data dn;
527
Gu Zhenge4795562013-09-27 18:08:30 +0800528 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900529 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800530 ret = f2fs_reserve_block(&dn, index);
Gu Zhenge4795562013-09-27 18:08:30 +0800531 f2fs_unlock_op(sbi);
Huajun Lib6009652013-11-10 23:13:18 +0800532 if (ret)
533 break;
534
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900535 if (pg_start == pg_end)
536 new_size = offset + len;
537 else if (index == pg_start && off_start)
538 new_size = (index + 1) << PAGE_CACHE_SHIFT;
539 else if (index == pg_end)
540 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
541 else
542 new_size += PAGE_CACHE_SIZE;
543 }
544
545 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
546 i_size_read(inode) < new_size) {
547 i_size_write(inode, new_size);
548 mark_inode_dirty(inode);
549 }
550
551 return ret;
552}
553
554static long f2fs_fallocate(struct file *file, int mode,
555 loff_t offset, loff_t len)
556{
Al Viro6131ffa2013-02-27 16:59:05 -0500557 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900558 long ret;
559
560 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
561 return -EOPNOTSUPP;
562
563 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800564 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900565 else
566 ret = expand_inode_data(inode, offset, len, mode);
567
Namjae Jeon3af60a42012-12-30 14:52:37 +0900568 if (!ret) {
569 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
570 mark_inode_dirty(inode);
571 }
Namjae Jeonc01e2852013-04-23 17:00:52 +0900572 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900573 return ret;
574}
575
576#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
577#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
578
579static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
580{
581 if (S_ISDIR(mode))
582 return flags;
583 else if (S_ISREG(mode))
584 return flags & F2FS_REG_FLMASK;
585 else
586 return flags & F2FS_OTHER_FLMASK;
587}
588
589long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
590{
Al Viro6131ffa2013-02-27 16:59:05 -0500591 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900592 struct f2fs_inode_info *fi = F2FS_I(inode);
593 unsigned int flags;
594 int ret;
595
596 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900597 case F2FS_IOC_GETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900598 flags = fi->i_flags & FS_FL_USER_VISIBLE;
599 return put_user(flags, (int __user *) arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900600 case F2FS_IOC_SETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900601 {
602 unsigned int oldflags;
603
Al Virobdaec332013-03-20 09:33:23 -0400604 ret = mnt_want_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900605 if (ret)
606 return ret;
607
608 if (!inode_owner_or_capable(inode)) {
609 ret = -EACCES;
610 goto out;
611 }
612
613 if (get_user(flags, (int __user *) arg)) {
614 ret = -EFAULT;
615 goto out;
616 }
617
618 flags = f2fs_mask_flags(inode->i_mode, flags);
619
620 mutex_lock(&inode->i_mutex);
621
622 oldflags = fi->i_flags;
623
624 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
625 if (!capable(CAP_LINUX_IMMUTABLE)) {
626 mutex_unlock(&inode->i_mutex);
627 ret = -EPERM;
628 goto out;
629 }
630 }
631
632 flags = flags & FS_FL_USER_MODIFIABLE;
633 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
634 fi->i_flags = flags;
635 mutex_unlock(&inode->i_mutex);
636
637 f2fs_set_inode_flags(inode);
638 inode->i_ctime = CURRENT_TIME;
639 mark_inode_dirty(inode);
640out:
Al Virobdaec332013-03-20 09:33:23 -0400641 mnt_drop_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900642 return ret;
643 }
644 default:
645 return -ENOTTY;
646 }
647}
648
Namjae Jeone9750822013-02-04 23:41:41 +0900649#ifdef CONFIG_COMPAT
650long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
651{
652 switch (cmd) {
653 case F2FS_IOC32_GETFLAGS:
654 cmd = F2FS_IOC_GETFLAGS;
655 break;
656 case F2FS_IOC32_SETFLAGS:
657 cmd = F2FS_IOC_SETFLAGS;
658 break;
659 default:
660 return -ENOIOCTLCMD;
661 }
662 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
663}
664#endif
665
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900666const struct file_operations f2fs_file_operations = {
667 .llseek = generic_file_llseek,
668 .read = do_sync_read,
669 .write = do_sync_write,
670 .aio_read = generic_file_aio_read,
671 .aio_write = generic_file_aio_write,
672 .open = generic_file_open,
673 .mmap = f2fs_file_mmap,
674 .fsync = f2fs_sync_file,
675 .fallocate = f2fs_fallocate,
676 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900677#ifdef CONFIG_COMPAT
678 .compat_ioctl = f2fs_compat_ioctl,
679#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900680 .splice_read = generic_file_splice_read,
681 .splice_write = generic_file_splice_write,
682};