blob: bb365c932555ab53f2e4ae739d2599f69dec42cd [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 */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090079 f2fs_wait_on_page_writeback(page, DATA);
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,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070087 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090088 .page_mkwrite = f2fs_vm_page_mkwrite,
89 .remap_pages = generic_file_remap_pages,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090090};
91
Jaegeuk Kim354a3392013-06-14 08:52:35 +090092static int get_parent_ino(struct inode *inode, nid_t *pino)
93{
94 struct dentry *dentry;
95
96 inode = igrab(inode);
97 dentry = d_find_any_alias(inode);
98 iput(inode);
99 if (!dentry)
100 return 0;
101
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900102 if (update_dent_inode(inode, &dentry->d_name)) {
103 dput(dentry);
104 return 0;
105 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900106
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900107 *pino = parent_ino(dentry);
108 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900109 return 1;
110}
111
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900112int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
113{
114 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900115 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900116 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900117 int ret = 0;
118 bool need_cp = false;
119 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900120 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900121 .nr_to_write = LONG_MAX,
122 .for_reclaim = 0,
123 };
124
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900125 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900126 return 0;
127
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900128 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900129 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900130 if (ret) {
131 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900132 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900133 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900134
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900135 /* guarantee free sections for fsync */
136 f2fs_balance_fs(sbi);
137
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900138 down_read(&fi->i_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900139
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900140 /*
141 * Both of fdatasync() and fsync() are able to be recovered from
142 * sudden-power-off.
143 */
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900144 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
145 need_cp = true;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900146 else if (file_wrong_pino(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900147 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900148 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900149 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900150 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900151 need_cp = true;
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900152 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900153 need_cp = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900154
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900155 up_read(&fi->i_sem);
156
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900157 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900158 nid_t pino;
159
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900160 /* all the dirty node pages should be flushed for POR */
161 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900162
163 down_write(&fi->i_sem);
164 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900165 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
166 get_parent_ino(inode, &pino)) {
167 F2FS_I(inode)->i_pino = pino;
168 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900169 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900170 mark_inode_dirty_sync(inode);
171 ret = f2fs_write_inode(inode, NULL);
172 if (ret)
173 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900174 } else {
175 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900176 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900177 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900178 /* if there is no written node page, write its inode page */
179 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900180 if (fsync_mark_done(sbi, inode->i_ino))
181 goto out;
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900182 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900183 ret = f2fs_write_inode(inode, NULL);
184 if (ret)
185 goto out;
186 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900187 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
188 if (ret)
189 goto out;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900190 ret = f2fs_issue_flush(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900191 }
192out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900193 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900194 return ret;
195}
196
197static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
198{
199 file_accessed(file);
200 vma->vm_ops = &f2fs_file_vm_ops;
201 return 0;
202}
203
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900204int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205{
206 int nr_free = 0, ofs = dn->ofs_in_node;
207 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
208 struct f2fs_node *raw_node;
209 __le32 *addr;
210
Gu Zheng45590712013-07-15 17:57:38 +0800211 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900212 addr = blkaddr_in_node(raw_node) + ofs;
213
Chris Fries6c311ec2014-01-17 14:44:39 -0600214 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900215 block_t blkaddr = le32_to_cpu(*addr);
216 if (blkaddr == NULL_ADDR)
217 continue;
218
219 update_extent_cache(NULL_ADDR, dn);
220 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900221 nr_free++;
222 }
223 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900224 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900225 set_page_dirty(dn->node_page);
226 sync_inode_page(dn);
227 }
228 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900229
230 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
231 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900232 return nr_free;
233}
234
235void truncate_data_blocks(struct dnode_of_data *dn)
236{
237 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
238}
239
240static void truncate_partial_data_page(struct inode *inode, u64 from)
241{
242 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
243 struct page *page;
244
245 if (!offset)
246 return;
247
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900248 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900249 if (IS_ERR(page))
250 return;
251
252 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900253 if (unlikely(page->mapping != inode->i_mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900254 f2fs_put_page(page, 1);
255 return;
256 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900257 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900258 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
259 set_page_dirty(page);
260 f2fs_put_page(page, 1);
261}
262
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900263int truncate_blocks(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900264{
265 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
266 unsigned int blocksize = inode->i_sb->s_blocksize;
267 struct dnode_of_data dn;
268 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800269 int count = 0, err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900270
Namjae Jeon51dd6242013-04-20 01:28:52 +0900271 trace_f2fs_truncate_blocks_enter(inode, from);
272
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900273 if (f2fs_has_inline_data(inode))
274 goto done;
275
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900276 free_from = (pgoff_t)
277 ((from + blocksize - 1) >> (sbi->log_blocksize));
278
Gu Zhenge4795562013-09-27 18:08:30 +0800279 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800280
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900281 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900282 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900283 if (err) {
284 if (err == -ENOENT)
285 goto free_next;
Gu Zhenge4795562013-09-27 18:08:30 +0800286 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900287 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900288 return err;
289 }
290
Chao Yu6403eb12014-04-26 19:59:52 +0800291 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900292
293 count -= dn.ofs_in_node;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900294 f2fs_bug_on(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900295
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900296 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
297 truncate_data_blocks_range(&dn, count);
298 free_from += count;
299 }
300
301 f2fs_put_dnode(&dn);
302free_next:
303 err = truncate_inode_blocks(inode, free_from);
Gu Zhenge4795562013-09-27 18:08:30 +0800304 f2fs_unlock_op(sbi);
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900305done:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900306 /* lastly zero out the first data page */
307 truncate_partial_data_page(inode, from);
308
Namjae Jeon51dd6242013-04-20 01:28:52 +0900309 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900310 return err;
311}
312
313void f2fs_truncate(struct inode *inode)
314{
315 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
316 S_ISLNK(inode->i_mode)))
317 return;
318
Namjae Jeon51dd6242013-04-20 01:28:52 +0900319 trace_f2fs_truncate(inode);
320
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900321 if (!truncate_blocks(inode, i_size_read(inode))) {
322 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
323 mark_inode_dirty(inode);
324 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900325}
326
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900327int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900328 struct dentry *dentry, struct kstat *stat)
329{
330 struct inode *inode = dentry->d_inode;
331 generic_fillattr(inode, stat);
332 stat->blocks <<= 3;
333 return 0;
334}
335
336#ifdef CONFIG_F2FS_FS_POSIX_ACL
337static void __setattr_copy(struct inode *inode, const struct iattr *attr)
338{
339 struct f2fs_inode_info *fi = F2FS_I(inode);
340 unsigned int ia_valid = attr->ia_valid;
341
342 if (ia_valid & ATTR_UID)
343 inode->i_uid = attr->ia_uid;
344 if (ia_valid & ATTR_GID)
345 inode->i_gid = attr->ia_gid;
346 if (ia_valid & ATTR_ATIME)
347 inode->i_atime = timespec_trunc(attr->ia_atime,
348 inode->i_sb->s_time_gran);
349 if (ia_valid & ATTR_MTIME)
350 inode->i_mtime = timespec_trunc(attr->ia_mtime,
351 inode->i_sb->s_time_gran);
352 if (ia_valid & ATTR_CTIME)
353 inode->i_ctime = timespec_trunc(attr->ia_ctime,
354 inode->i_sb->s_time_gran);
355 if (ia_valid & ATTR_MODE) {
356 umode_t mode = attr->ia_mode;
357
358 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
359 mode &= ~S_ISGID;
360 set_acl_inode(fi, mode);
361 }
362}
363#else
364#define __setattr_copy setattr_copy
365#endif
366
367int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
368{
369 struct inode *inode = dentry->d_inode;
370 struct f2fs_inode_info *fi = F2FS_I(inode);
371 int err;
372
373 err = inode_change_ok(inode, attr);
374 if (err)
375 return err;
376
377 if ((attr->ia_valid & ATTR_SIZE) &&
378 attr->ia_size != i_size_read(inode)) {
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900379 err = f2fs_convert_inline_data(inode, attr->ia_size);
380 if (err)
381 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800382
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900383 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900384 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) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800391 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900392 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,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800406 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900407#ifdef CONFIG_F2FS_FS_XATTR
408 .setxattr = generic_setxattr,
409 .getxattr = generic_getxattr,
410 .listxattr = f2fs_listxattr,
411 .removexattr = generic_removexattr,
412#endif
413};
414
415static void fill_zero(struct inode *inode, pgoff_t index,
416 loff_t start, loff_t len)
417{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900418 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900419 struct page *page;
420
421 if (!len)
422 return;
423
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900424 f2fs_balance_fs(sbi);
425
Gu Zhenge4795562013-09-27 18:08:30 +0800426 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900427 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800428 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900429
430 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900431 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900432 zero_user(page, start, len);
433 set_page_dirty(page);
434 f2fs_put_page(page, 1);
435 }
436}
437
438int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
439{
440 pgoff_t index;
441 int err;
442
443 for (index = pg_start; index < pg_end; index++) {
444 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900445
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900446 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900447 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900448 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900449 if (err == -ENOENT)
450 continue;
451 return err;
452 }
453
454 if (dn.data_blkaddr != NULL_ADDR)
455 truncate_data_blocks_range(&dn, 1);
456 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900457 }
458 return 0;
459}
460
Chao Yua66c7b22013-11-22 16:52:50 +0800461static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900462{
463 pgoff_t pg_start, pg_end;
464 loff_t off_start, off_end;
465 int ret = 0;
466
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900467 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1);
Jaegeuk Kim8230a0a2013-12-27 11:13:21 +0900468 if (ret)
469 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800470
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900471 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
472 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
473
474 off_start = offset & (PAGE_CACHE_SIZE - 1);
475 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
476
477 if (pg_start == pg_end) {
478 fill_zero(inode, pg_start, off_start,
479 off_end - off_start);
480 } else {
481 if (off_start)
482 fill_zero(inode, pg_start++, off_start,
483 PAGE_CACHE_SIZE - off_start);
484 if (off_end)
485 fill_zero(inode, pg_end, 0, off_end);
486
487 if (pg_start < pg_end) {
488 struct address_space *mapping = inode->i_mapping;
489 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500490 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
491
492 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900493
494 blk_start = pg_start << PAGE_CACHE_SHIFT;
495 blk_end = pg_end << PAGE_CACHE_SHIFT;
496 truncate_inode_pages_range(mapping, blk_start,
497 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900498
Gu Zhenge4795562013-09-27 18:08:30 +0800499 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900500 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800501 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900502 }
503 }
504
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900505 return ret;
506}
507
508static int expand_inode_data(struct inode *inode, loff_t offset,
509 loff_t len, int mode)
510{
511 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
512 pgoff_t index, pg_start, pg_end;
513 loff_t new_size = i_size_read(inode);
514 loff_t off_start, off_end;
515 int ret = 0;
516
517 ret = inode_newsize_ok(inode, (len + offset));
518 if (ret)
519 return ret;
520
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900521 ret = f2fs_convert_inline_data(inode, offset + len);
522 if (ret)
523 return ret;
524
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900525 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
526 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
527
528 off_start = offset & (PAGE_CACHE_SIZE - 1);
529 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
530
531 for (index = pg_start; index <= pg_end; index++) {
532 struct dnode_of_data dn;
533
Gu Zhenge4795562013-09-27 18:08:30 +0800534 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900535 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800536 ret = f2fs_reserve_block(&dn, index);
Gu Zhenge4795562013-09-27 18:08:30 +0800537 f2fs_unlock_op(sbi);
Huajun Lib6009652013-11-10 23:13:18 +0800538 if (ret)
539 break;
540
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900541 if (pg_start == pg_end)
542 new_size = offset + len;
543 else if (index == pg_start && off_start)
544 new_size = (index + 1) << PAGE_CACHE_SHIFT;
545 else if (index == pg_end)
546 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
547 else
548 new_size += PAGE_CACHE_SIZE;
549 }
550
551 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
552 i_size_read(inode) < new_size) {
553 i_size_write(inode, new_size);
554 mark_inode_dirty(inode);
555 }
556
557 return ret;
558}
559
560static long f2fs_fallocate(struct file *file, int mode,
561 loff_t offset, loff_t len)
562{
Al Viro6131ffa2013-02-27 16:59:05 -0500563 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900564 long ret;
565
566 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
567 return -EOPNOTSUPP;
568
Chao Yu3375f692014-01-28 10:29:26 +0800569 mutex_lock(&inode->i_mutex);
570
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900571 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800572 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900573 else
574 ret = expand_inode_data(inode, offset, len, mode);
575
Namjae Jeon3af60a42012-12-30 14:52:37 +0900576 if (!ret) {
577 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
578 mark_inode_dirty(inode);
579 }
Chao Yu3375f692014-01-28 10:29:26 +0800580
581 mutex_unlock(&inode->i_mutex);
582
Namjae Jeonc01e2852013-04-23 17:00:52 +0900583 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900584 return ret;
585}
586
587#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
588#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
589
590static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
591{
592 if (S_ISDIR(mode))
593 return flags;
594 else if (S_ISREG(mode))
595 return flags & F2FS_REG_FLMASK;
596 else
597 return flags & F2FS_OTHER_FLMASK;
598}
599
600long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
601{
Al Viro6131ffa2013-02-27 16:59:05 -0500602 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900603 struct f2fs_inode_info *fi = F2FS_I(inode);
604 unsigned int flags;
605 int ret;
606
607 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900608 case F2FS_IOC_GETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900609 flags = fi->i_flags & FS_FL_USER_VISIBLE;
610 return put_user(flags, (int __user *) arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900611 case F2FS_IOC_SETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900612 {
613 unsigned int oldflags;
614
Al Virobdaec332013-03-20 09:33:23 -0400615 ret = mnt_want_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900616 if (ret)
617 return ret;
618
619 if (!inode_owner_or_capable(inode)) {
620 ret = -EACCES;
621 goto out;
622 }
623
624 if (get_user(flags, (int __user *) arg)) {
625 ret = -EFAULT;
626 goto out;
627 }
628
629 flags = f2fs_mask_flags(inode->i_mode, flags);
630
631 mutex_lock(&inode->i_mutex);
632
633 oldflags = fi->i_flags;
634
635 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
636 if (!capable(CAP_LINUX_IMMUTABLE)) {
637 mutex_unlock(&inode->i_mutex);
638 ret = -EPERM;
639 goto out;
640 }
641 }
642
643 flags = flags & FS_FL_USER_MODIFIABLE;
644 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
645 fi->i_flags = flags;
646 mutex_unlock(&inode->i_mutex);
647
648 f2fs_set_inode_flags(inode);
649 inode->i_ctime = CURRENT_TIME;
650 mark_inode_dirty(inode);
651out:
Al Virobdaec332013-03-20 09:33:23 -0400652 mnt_drop_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900653 return ret;
654 }
655 default:
656 return -ENOTTY;
657 }
658}
659
Namjae Jeone9750822013-02-04 23:41:41 +0900660#ifdef CONFIG_COMPAT
661long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
662{
663 switch (cmd) {
664 case F2FS_IOC32_GETFLAGS:
665 cmd = F2FS_IOC_GETFLAGS;
666 break;
667 case F2FS_IOC32_SETFLAGS:
668 cmd = F2FS_IOC_SETFLAGS;
669 break;
670 default:
671 return -ENOIOCTLCMD;
672 }
673 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
674}
675#endif
676
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900677const struct file_operations f2fs_file_operations = {
678 .llseek = generic_file_llseek,
679 .read = do_sync_read,
680 .write = do_sync_write,
681 .aio_read = generic_file_aio_read,
682 .aio_write = generic_file_aio_write,
683 .open = generic_file_open,
684 .mmap = f2fs_file_mmap,
685 .fsync = f2fs_sync_file,
686 .fallocate = f2fs_fallocate,
687 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900688#ifdef CONFIG_COMPAT
689 .compat_ioctl = f2fs_compat_ioctl,
690#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900691 .splice_read = generic_file_splice_read,
692 .splice_write = generic_file_splice_write,
693};