blob: c80faa2fff39ee09556d73bc0c567c15329a5cb4 [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 block_t old_blk_addr;
37 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);
43
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090044 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080045 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090046 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +090047 err = get_dnode_of_data(&dn, page->index, ALLOC_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090048 if (err) {
Gu Zhenge4795562013-09-27 18:08:30 +080049 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090050 goto out;
51 }
52
53 old_blk_addr = dn.data_blkaddr;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090054
55 if (old_blk_addr == NULL_ADDR) {
56 err = reserve_new_block(&dn);
57 if (err) {
58 f2fs_put_dnode(&dn);
Gu Zhenge4795562013-09-27 18:08:30 +080059 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090060 goto out;
61 }
62 }
63 f2fs_put_dnode(&dn);
Gu Zhenge4795562013-09-27 18:08:30 +080064 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090065
Namjae Jeon9851e6e2013-04-28 09:04:18 +090066 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090067 lock_page(page);
68 if (page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090069 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090070 !PageUptodate(page)) {
71 unlock_page(page);
72 err = -EFAULT;
73 goto out;
74 }
75
76 /*
77 * check to see if the page is mapped already (no holes)
78 */
79 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090080 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090081
82 /* page is wholly or partially inside EOF */
83 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
84 unsigned offset;
85 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
86 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
87 }
88 set_page_dirty(page);
89 SetPageUptodate(page);
90
Namjae Jeon9851e6e2013-04-28 09:04:18 +090091mapped:
92 /* fill the page */
93 wait_on_page_writeback(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090094out:
95 sb_end_pagefault(inode->i_sb);
96 return block_page_mkwrite_return(err);
97}
98
99static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900100 .fault = filemap_fault,
101 .page_mkwrite = f2fs_vm_page_mkwrite,
102 .remap_pages = generic_file_remap_pages,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900103};
104
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900105static int get_parent_ino(struct inode *inode, nid_t *pino)
106{
107 struct dentry *dentry;
108
109 inode = igrab(inode);
110 dentry = d_find_any_alias(inode);
111 iput(inode);
112 if (!dentry)
113 return 0;
114
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900115 if (update_dent_inode(inode, &dentry->d_name)) {
116 dput(dentry);
117 return 0;
118 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900119
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900120 *pino = parent_ino(dentry);
121 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900122 return 1;
123}
124
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900125int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
126{
127 struct inode *inode = file->f_mapping->host;
128 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900129 int ret = 0;
130 bool need_cp = false;
131 struct writeback_control wbc = {
132 .sync_mode = WB_SYNC_ALL,
133 .nr_to_write = LONG_MAX,
134 .for_reclaim = 0,
135 };
136
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900137 if (f2fs_readonly(inode->i_sb))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900138 return 0;
139
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900140 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900141 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900142 if (ret) {
143 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900144 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900145 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900146
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900147 /* guarantee free sections for fsync */
148 f2fs_balance_fs(sbi);
149
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900150 mutex_lock(&inode->i_mutex);
151
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900152 /*
153 * Both of fdatasync() and fsync() are able to be recovered from
154 * sudden-power-off.
155 */
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900156 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
157 need_cp = true;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900158 else if (file_wrong_pino(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900159 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900160 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900161 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900162 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900163 need_cp = true;
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900164 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900165 need_cp = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900166
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900167 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900168 nid_t pino;
169
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900170 F2FS_I(inode)->xattr_ver = 0;
171
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900172 /* all the dirty node pages should be flushed for POR */
173 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900174 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
175 get_parent_ino(inode, &pino)) {
176 F2FS_I(inode)->i_pino = pino;
177 file_got_pino(inode);
178 mark_inode_dirty_sync(inode);
179 ret = f2fs_write_inode(inode, NULL);
180 if (ret)
181 goto out;
182 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900183 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900184 /* if there is no written node page, write its inode page */
185 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900186 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900187 ret = f2fs_write_inode(inode, NULL);
188 if (ret)
189 goto out;
190 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900191 filemap_fdatawait_range(sbi->node_inode->i_mapping,
192 0, LONG_MAX);
Jaegeuk Kimae51fb32013-03-16 11:13:04 +0900193 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900194 }
195out:
196 mutex_unlock(&inode->i_mutex);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900197 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900198 return ret;
199}
200
201static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
202{
203 file_accessed(file);
204 vma->vm_ops = &f2fs_file_vm_ops;
205 return 0;
206}
207
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900208int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900209{
210 int nr_free = 0, ofs = dn->ofs_in_node;
211 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
212 struct f2fs_node *raw_node;
213 __le32 *addr;
214
Gu Zheng45590712013-07-15 17:57:38 +0800215 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900216 addr = blkaddr_in_node(raw_node) + ofs;
217
218 for ( ; count > 0; count--, addr++, dn->ofs_in_node++) {
219 block_t blkaddr = le32_to_cpu(*addr);
220 if (blkaddr == NULL_ADDR)
221 continue;
222
223 update_extent_cache(NULL_ADDR, dn);
224 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900225 nr_free++;
226 }
227 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900228 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900229 set_page_dirty(dn->node_page);
230 sync_inode_page(dn);
231 }
232 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900233
234 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
235 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900236 return nr_free;
237}
238
239void truncate_data_blocks(struct dnode_of_data *dn)
240{
241 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
242}
243
244static void truncate_partial_data_page(struct inode *inode, u64 from)
245{
246 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
247 struct page *page;
248
249 if (!offset)
250 return;
251
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900252 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900253 if (IS_ERR(page))
254 return;
255
256 lock_page(page);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900257 if (page->mapping != inode->i_mapping) {
258 f2fs_put_page(page, 1);
259 return;
260 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900261 wait_on_page_writeback(page);
262 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
263 set_page_dirty(page);
264 f2fs_put_page(page, 1);
265}
266
267static int truncate_blocks(struct inode *inode, u64 from)
268{
269 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
270 unsigned int blocksize = inode->i_sb->s_blocksize;
271 struct dnode_of_data dn;
272 pgoff_t free_from;
Gu Zhenge4795562013-09-27 18:08:30 +0800273 int count = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900274 int err;
275
Namjae Jeon51dd6242013-04-20 01:28:52 +0900276 trace_f2fs_truncate_blocks_enter(inode, from);
277
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900278 free_from = (pgoff_t)
279 ((from + blocksize - 1) >> (sbi->log_blocksize));
280
Gu Zhenge4795562013-09-27 18:08:30 +0800281 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900282 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900283 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900284 if (err) {
285 if (err == -ENOENT)
286 goto free_next;
Gu Zhenge4795562013-09-27 18:08:30 +0800287 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900288 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900289 return err;
290 }
291
292 if (IS_INODE(dn.node_page))
Jaegeuk Kimde936532013-08-12 21:08:03 +0900293 count = ADDRS_PER_INODE(F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900294 else
295 count = ADDRS_PER_BLOCK;
296
297 count -= dn.ofs_in_node;
298 BUG_ON(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900299
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900300 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
301 truncate_data_blocks_range(&dn, count);
302 free_from += count;
303 }
304
305 f2fs_put_dnode(&dn);
306free_next:
307 err = truncate_inode_blocks(inode, free_from);
Gu Zhenge4795562013-09-27 18:08:30 +0800308 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900309
310 /* lastly zero out the first data page */
311 truncate_partial_data_page(inode, from);
312
Namjae Jeon51dd6242013-04-20 01:28:52 +0900313 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900314 return err;
315}
316
317void f2fs_truncate(struct inode *inode)
318{
319 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
320 S_ISLNK(inode->i_mode)))
321 return;
322
Namjae Jeon51dd6242013-04-20 01:28:52 +0900323 trace_f2fs_truncate(inode);
324
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900325 if (!truncate_blocks(inode, i_size_read(inode))) {
326 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
327 mark_inode_dirty(inode);
328 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900329}
330
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900331int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900332 struct dentry *dentry, struct kstat *stat)
333{
334 struct inode *inode = dentry->d_inode;
335 generic_fillattr(inode, stat);
336 stat->blocks <<= 3;
337 return 0;
338}
339
340#ifdef CONFIG_F2FS_FS_POSIX_ACL
341static void __setattr_copy(struct inode *inode, const struct iattr *attr)
342{
343 struct f2fs_inode_info *fi = F2FS_I(inode);
344 unsigned int ia_valid = attr->ia_valid;
345
346 if (ia_valid & ATTR_UID)
347 inode->i_uid = attr->ia_uid;
348 if (ia_valid & ATTR_GID)
349 inode->i_gid = attr->ia_gid;
350 if (ia_valid & ATTR_ATIME)
351 inode->i_atime = timespec_trunc(attr->ia_atime,
352 inode->i_sb->s_time_gran);
353 if (ia_valid & ATTR_MTIME)
354 inode->i_mtime = timespec_trunc(attr->ia_mtime,
355 inode->i_sb->s_time_gran);
356 if (ia_valid & ATTR_CTIME)
357 inode->i_ctime = timespec_trunc(attr->ia_ctime,
358 inode->i_sb->s_time_gran);
359 if (ia_valid & ATTR_MODE) {
360 umode_t mode = attr->ia_mode;
361
362 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
363 mode &= ~S_ISGID;
364 set_acl_inode(fi, mode);
365 }
366}
367#else
368#define __setattr_copy setattr_copy
369#endif
370
371int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
372{
373 struct inode *inode = dentry->d_inode;
374 struct f2fs_inode_info *fi = F2FS_I(inode);
375 int err;
376
377 err = inode_change_ok(inode, attr);
378 if (err)
379 return err;
380
381 if ((attr->ia_valid & ATTR_SIZE) &&
382 attr->ia_size != i_size_read(inode)) {
383 truncate_setsize(inode, attr->ia_size);
384 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900385 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900386 }
387
388 __setattr_copy(inode, attr);
389
390 if (attr->ia_valid & ATTR_MODE) {
391 err = f2fs_acl_chmod(inode);
392 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
393 inode->i_mode = fi->i_acl_mode;
394 clear_inode_flag(fi, FI_ACL_MODE);
395 }
396 }
397
398 mark_inode_dirty(inode);
399 return err;
400}
401
402const struct inode_operations f2fs_file_inode_operations = {
403 .getattr = f2fs_getattr,
404 .setattr = f2fs_setattr,
405 .get_acl = f2fs_get_acl,
406#ifdef CONFIG_F2FS_FS_XATTR
407 .setxattr = generic_setxattr,
408 .getxattr = generic_getxattr,
409 .listxattr = f2fs_listxattr,
410 .removexattr = generic_removexattr,
411#endif
412};
413
414static void fill_zero(struct inode *inode, pgoff_t index,
415 loff_t start, loff_t len)
416{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900417 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900418 struct page *page;
419
420 if (!len)
421 return;
422
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900423 f2fs_balance_fs(sbi);
424
Gu Zhenge4795562013-09-27 18:08:30 +0800425 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900426 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800427 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900428
429 if (!IS_ERR(page)) {
430 wait_on_page_writeback(page);
431 zero_user(page, start, len);
432 set_page_dirty(page);
433 f2fs_put_page(page, 1);
434 }
435}
436
437int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
438{
439 pgoff_t index;
440 int err;
441
442 for (index = pg_start; index < pg_end; index++) {
443 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900444
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900445 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900446 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900447 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900448 if (err == -ENOENT)
449 continue;
450 return err;
451 }
452
453 if (dn.data_blkaddr != NULL_ADDR)
454 truncate_data_blocks_range(&dn, 1);
455 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900456 }
457 return 0;
458}
459
460static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode)
461{
462 pgoff_t pg_start, pg_end;
463 loff_t off_start, off_end;
464 int ret = 0;
465
466 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
467 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
468
469 off_start = offset & (PAGE_CACHE_SIZE - 1);
470 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
471
472 if (pg_start == pg_end) {
473 fill_zero(inode, pg_start, off_start,
474 off_end - off_start);
475 } else {
476 if (off_start)
477 fill_zero(inode, pg_start++, off_start,
478 PAGE_CACHE_SIZE - off_start);
479 if (off_end)
480 fill_zero(inode, pg_end, 0, off_end);
481
482 if (pg_start < pg_end) {
483 struct address_space *mapping = inode->i_mapping;
484 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500485 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
486
487 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900488
489 blk_start = pg_start << PAGE_CACHE_SHIFT;
490 blk_end = pg_end << PAGE_CACHE_SHIFT;
491 truncate_inode_pages_range(mapping, blk_start,
492 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900493
Gu Zhenge4795562013-09-27 18:08:30 +0800494 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900495 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800496 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900497 }
498 }
499
500 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
501 i_size_read(inode) <= (offset + len)) {
502 i_size_write(inode, offset);
503 mark_inode_dirty(inode);
504 }
505
506 return ret;
507}
508
509static int expand_inode_data(struct inode *inode, loff_t offset,
510 loff_t len, int mode)
511{
512 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
513 pgoff_t index, pg_start, pg_end;
514 loff_t new_size = i_size_read(inode);
515 loff_t off_start, off_end;
516 int ret = 0;
517
518 ret = inode_newsize_ok(inode, (len + offset));
519 if (ret)
520 return ret;
521
522 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
523 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
524
525 off_start = offset & (PAGE_CACHE_SIZE - 1);
526 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
527
528 for (index = pg_start; index <= pg_end; index++) {
529 struct dnode_of_data dn;
530
Gu Zhenge4795562013-09-27 18:08:30 +0800531 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900533 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900534 if (ret) {
Gu Zhenge4795562013-09-27 18:08:30 +0800535 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900536 break;
537 }
538
539 if (dn.data_blkaddr == NULL_ADDR) {
540 ret = reserve_new_block(&dn);
541 if (ret) {
542 f2fs_put_dnode(&dn);
Gu Zhenge4795562013-09-27 18:08:30 +0800543 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900544 break;
545 }
546 }
547 f2fs_put_dnode(&dn);
Gu Zhenge4795562013-09-27 18:08:30 +0800548 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900549
550 if (pg_start == pg_end)
551 new_size = offset + len;
552 else if (index == pg_start && off_start)
553 new_size = (index + 1) << PAGE_CACHE_SHIFT;
554 else if (index == pg_end)
555 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
556 else
557 new_size += PAGE_CACHE_SIZE;
558 }
559
560 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
561 i_size_read(inode) < new_size) {
562 i_size_write(inode, new_size);
563 mark_inode_dirty(inode);
564 }
565
566 return ret;
567}
568
569static long f2fs_fallocate(struct file *file, int mode,
570 loff_t offset, loff_t len)
571{
Al Viro6131ffa2013-02-27 16:59:05 -0500572 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900573 long ret;
574
575 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
576 return -EOPNOTSUPP;
577
578 if (mode & FALLOC_FL_PUNCH_HOLE)
579 ret = punch_hole(inode, offset, len, mode);
580 else
581 ret = expand_inode_data(inode, offset, len, mode);
582
Namjae Jeon3af60a42012-12-30 14:52:37 +0900583 if (!ret) {
584 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
585 mark_inode_dirty(inode);
586 }
Namjae Jeonc01e2852013-04-23 17:00:52 +0900587 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900588 return ret;
589}
590
591#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
592#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
593
594static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
595{
596 if (S_ISDIR(mode))
597 return flags;
598 else if (S_ISREG(mode))
599 return flags & F2FS_REG_FLMASK;
600 else
601 return flags & F2FS_OTHER_FLMASK;
602}
603
604long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
605{
Al Viro6131ffa2013-02-27 16:59:05 -0500606 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900607 struct f2fs_inode_info *fi = F2FS_I(inode);
608 unsigned int flags;
609 int ret;
610
611 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900612 case F2FS_IOC_GETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900613 flags = fi->i_flags & FS_FL_USER_VISIBLE;
614 return put_user(flags, (int __user *) arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900615 case F2FS_IOC_SETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900616 {
617 unsigned int oldflags;
618
Al Virobdaec332013-03-20 09:33:23 -0400619 ret = mnt_want_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620 if (ret)
621 return ret;
622
623 if (!inode_owner_or_capable(inode)) {
624 ret = -EACCES;
625 goto out;
626 }
627
628 if (get_user(flags, (int __user *) arg)) {
629 ret = -EFAULT;
630 goto out;
631 }
632
633 flags = f2fs_mask_flags(inode->i_mode, flags);
634
635 mutex_lock(&inode->i_mutex);
636
637 oldflags = fi->i_flags;
638
639 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
640 if (!capable(CAP_LINUX_IMMUTABLE)) {
641 mutex_unlock(&inode->i_mutex);
642 ret = -EPERM;
643 goto out;
644 }
645 }
646
647 flags = flags & FS_FL_USER_MODIFIABLE;
648 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
649 fi->i_flags = flags;
650 mutex_unlock(&inode->i_mutex);
651
652 f2fs_set_inode_flags(inode);
653 inode->i_ctime = CURRENT_TIME;
654 mark_inode_dirty(inode);
655out:
Al Virobdaec332013-03-20 09:33:23 -0400656 mnt_drop_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900657 return ret;
658 }
659 default:
660 return -ENOTTY;
661 }
662}
663
Namjae Jeone9750822013-02-04 23:41:41 +0900664#ifdef CONFIG_COMPAT
665long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
666{
667 switch (cmd) {
668 case F2FS_IOC32_GETFLAGS:
669 cmd = F2FS_IOC_GETFLAGS;
670 break;
671 case F2FS_IOC32_SETFLAGS:
672 cmd = F2FS_IOC_SETFLAGS;
673 break;
674 default:
675 return -ENOIOCTLCMD;
676 }
677 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
678}
679#endif
680
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900681const struct file_operations f2fs_file_operations = {
682 .llseek = generic_file_llseek,
683 .read = do_sync_read,
684 .write = do_sync_write,
685 .aio_read = generic_file_aio_read,
686 .aio_write = generic_file_aio_write,
687 .open = generic_file_open,
688 .mmap = f2fs_file_mmap,
689 .fsync = f2fs_sync_file,
690 .fallocate = f2fs_fallocate,
691 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900692#ifdef CONFIG_COMPAT
693 .compat_ioctl = f2fs_compat_ioctl,
694#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900695 .splice_read = generic_file_splice_read,
696 .splice_write = generic_file_splice_write,
697};