blob: d2d2b7dbdcc12b348ca89899f500748716c2104c [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;
Jaegeuk Kim39936832012-11-22 16:21:29 +090038 int err, ilock;
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 */
Jaegeuk Kim39936832012-11-22 16:21:29 +090045 ilock = mutex_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) {
Jaegeuk Kim39936832012-11-22 16:21:29 +090049 mutex_unlock_op(sbi, ilock);
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);
Jaegeuk Kim39936832012-11-22 16:21:29 +090059 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090060 goto out;
61 }
62 }
63 f2fs_put_dnode(&dn);
Jaegeuk Kim39936832012-11-22 16:21:29 +090064 mutex_unlock_op(sbi, ilock);
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
115 inode = igrab(dentry->d_parent->d_inode);
116 dput(dentry);
117
118 *pino = inode->i_ino;
119 iput(inode);
120 return 1;
121}
122
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900123int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
124{
125 struct inode *inode = file->f_mapping->host;
126 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900127 int ret = 0;
128 bool need_cp = false;
129 struct writeback_control wbc = {
130 .sync_mode = WB_SYNC_ALL,
131 .nr_to_write = LONG_MAX,
132 .for_reclaim = 0,
133 };
134
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900135 if (f2fs_readonly(inode->i_sb))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900136 return 0;
137
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900138 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900139 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900140 if (ret) {
141 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900142 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900143 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900144
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900145 /* guarantee free sections for fsync */
146 f2fs_balance_fs(sbi);
147
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900148 mutex_lock(&inode->i_mutex);
149
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900150 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
151 goto out;
152
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900153 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
154 need_cp = true;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900155 else if (file_wrong_pino(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900156 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900157 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900158 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900159 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900160 need_cp = true;
161
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900162 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900163 nid_t pino;
164
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900165 /* all the dirty node pages should be flushed for POR */
166 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900167 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
168 get_parent_ino(inode, &pino)) {
169 F2FS_I(inode)->i_pino = pino;
170 file_got_pino(inode);
171 mark_inode_dirty_sync(inode);
172 ret = f2fs_write_inode(inode, NULL);
173 if (ret)
174 goto out;
175 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900176 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900177 /* if there is no written node page, write its inode page */
178 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900179 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900180 ret = f2fs_write_inode(inode, NULL);
181 if (ret)
182 goto out;
183 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900184 filemap_fdatawait_range(sbi->node_inode->i_mapping,
185 0, LONG_MAX);
Jaegeuk Kimae51fb32013-03-16 11:13:04 +0900186 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900187 }
188out:
189 mutex_unlock(&inode->i_mutex);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900190 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900191 return ret;
192}
193
194static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
195{
196 file_accessed(file);
197 vma->vm_ops = &f2fs_file_vm_ops;
198 return 0;
199}
200
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900201int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900202{
203 int nr_free = 0, ofs = dn->ofs_in_node;
204 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
205 struct f2fs_node *raw_node;
206 __le32 *addr;
207
208 raw_node = page_address(dn->node_page);
209 addr = blkaddr_in_node(raw_node) + ofs;
210
211 for ( ; count > 0; count--, addr++, dn->ofs_in_node++) {
212 block_t blkaddr = le32_to_cpu(*addr);
213 if (blkaddr == NULL_ADDR)
214 continue;
215
216 update_extent_cache(NULL_ADDR, dn);
217 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900218 nr_free++;
219 }
220 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900221 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900222 set_page_dirty(dn->node_page);
223 sync_inode_page(dn);
224 }
225 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900226
227 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
228 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900229 return nr_free;
230}
231
232void truncate_data_blocks(struct dnode_of_data *dn)
233{
234 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
235}
236
237static void truncate_partial_data_page(struct inode *inode, u64 from)
238{
239 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
240 struct page *page;
241
242 if (!offset)
243 return;
244
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900245 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900246 if (IS_ERR(page))
247 return;
248
249 lock_page(page);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900250 if (page->mapping != inode->i_mapping) {
251 f2fs_put_page(page, 1);
252 return;
253 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900254 wait_on_page_writeback(page);
255 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
256 set_page_dirty(page);
257 f2fs_put_page(page, 1);
258}
259
260static int truncate_blocks(struct inode *inode, u64 from)
261{
262 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
263 unsigned int blocksize = inode->i_sb->s_blocksize;
264 struct dnode_of_data dn;
265 pgoff_t free_from;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900266 int count = 0, ilock = -1;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900267 int err;
268
Namjae Jeon51dd6242013-04-20 01:28:52 +0900269 trace_f2fs_truncate_blocks_enter(inode, from);
270
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900271 free_from = (pgoff_t)
272 ((from + blocksize - 1) >> (sbi->log_blocksize));
273
Jaegeuk Kim39936832012-11-22 16:21:29 +0900274 ilock = mutex_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900275 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900276 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900277 if (err) {
278 if (err == -ENOENT)
279 goto free_next;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900280 mutex_unlock_op(sbi, ilock);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900281 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900282 return err;
283 }
284
285 if (IS_INODE(dn.node_page))
286 count = ADDRS_PER_INODE;
287 else
288 count = ADDRS_PER_BLOCK;
289
290 count -= dn.ofs_in_node;
291 BUG_ON(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900292
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900293 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
294 truncate_data_blocks_range(&dn, count);
295 free_from += count;
296 }
297
298 f2fs_put_dnode(&dn);
299free_next:
300 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900301 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900302
303 /* lastly zero out the first data page */
304 truncate_partial_data_page(inode, from);
305
Namjae Jeon51dd6242013-04-20 01:28:52 +0900306 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900307 return err;
308}
309
310void f2fs_truncate(struct inode *inode)
311{
312 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
313 S_ISLNK(inode->i_mode)))
314 return;
315
Namjae Jeon51dd6242013-04-20 01:28:52 +0900316 trace_f2fs_truncate(inode);
317
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900318 if (!truncate_blocks(inode, i_size_read(inode))) {
319 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
320 mark_inode_dirty(inode);
321 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900322}
323
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900324int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900325 struct dentry *dentry, struct kstat *stat)
326{
327 struct inode *inode = dentry->d_inode;
328 generic_fillattr(inode, stat);
329 stat->blocks <<= 3;
330 return 0;
331}
332
333#ifdef CONFIG_F2FS_FS_POSIX_ACL
334static void __setattr_copy(struct inode *inode, const struct iattr *attr)
335{
336 struct f2fs_inode_info *fi = F2FS_I(inode);
337 unsigned int ia_valid = attr->ia_valid;
338
339 if (ia_valid & ATTR_UID)
340 inode->i_uid = attr->ia_uid;
341 if (ia_valid & ATTR_GID)
342 inode->i_gid = attr->ia_gid;
343 if (ia_valid & ATTR_ATIME)
344 inode->i_atime = timespec_trunc(attr->ia_atime,
345 inode->i_sb->s_time_gran);
346 if (ia_valid & ATTR_MTIME)
347 inode->i_mtime = timespec_trunc(attr->ia_mtime,
348 inode->i_sb->s_time_gran);
349 if (ia_valid & ATTR_CTIME)
350 inode->i_ctime = timespec_trunc(attr->ia_ctime,
351 inode->i_sb->s_time_gran);
352 if (ia_valid & ATTR_MODE) {
353 umode_t mode = attr->ia_mode;
354
355 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
356 mode &= ~S_ISGID;
357 set_acl_inode(fi, mode);
358 }
359}
360#else
361#define __setattr_copy setattr_copy
362#endif
363
364int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
365{
366 struct inode *inode = dentry->d_inode;
367 struct f2fs_inode_info *fi = F2FS_I(inode);
368 int err;
369
370 err = inode_change_ok(inode, attr);
371 if (err)
372 return err;
373
374 if ((attr->ia_valid & ATTR_SIZE) &&
375 attr->ia_size != i_size_read(inode)) {
376 truncate_setsize(inode, attr->ia_size);
377 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900378 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900379 }
380
381 __setattr_copy(inode, attr);
382
383 if (attr->ia_valid & ATTR_MODE) {
384 err = f2fs_acl_chmod(inode);
385 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
386 inode->i_mode = fi->i_acl_mode;
387 clear_inode_flag(fi, FI_ACL_MODE);
388 }
389 }
390
391 mark_inode_dirty(inode);
392 return err;
393}
394
395const struct inode_operations f2fs_file_inode_operations = {
396 .getattr = f2fs_getattr,
397 .setattr = f2fs_setattr,
398 .get_acl = f2fs_get_acl,
399#ifdef CONFIG_F2FS_FS_XATTR
400 .setxattr = generic_setxattr,
401 .getxattr = generic_getxattr,
402 .listxattr = f2fs_listxattr,
403 .removexattr = generic_removexattr,
404#endif
405};
406
407static void fill_zero(struct inode *inode, pgoff_t index,
408 loff_t start, loff_t len)
409{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900410 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900411 struct page *page;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900412 int ilock;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900413
414 if (!len)
415 return;
416
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900417 f2fs_balance_fs(sbi);
418
Jaegeuk Kim39936832012-11-22 16:21:29 +0900419 ilock = mutex_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900420 page = get_new_data_page(inode, NULL, index, false);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900421 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900422
423 if (!IS_ERR(page)) {
424 wait_on_page_writeback(page);
425 zero_user(page, start, len);
426 set_page_dirty(page);
427 f2fs_put_page(page, 1);
428 }
429}
430
431int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
432{
433 pgoff_t index;
434 int err;
435
436 for (index = pg_start; index < pg_end; index++) {
437 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900438
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900439 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900440 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900441 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900442 if (err == -ENOENT)
443 continue;
444 return err;
445 }
446
447 if (dn.data_blkaddr != NULL_ADDR)
448 truncate_data_blocks_range(&dn, 1);
449 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900450 }
451 return 0;
452}
453
454static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode)
455{
456 pgoff_t pg_start, pg_end;
457 loff_t off_start, off_end;
458 int ret = 0;
459
460 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
461 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
462
463 off_start = offset & (PAGE_CACHE_SIZE - 1);
464 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
465
466 if (pg_start == pg_end) {
467 fill_zero(inode, pg_start, off_start,
468 off_end - off_start);
469 } else {
470 if (off_start)
471 fill_zero(inode, pg_start++, off_start,
472 PAGE_CACHE_SIZE - off_start);
473 if (off_end)
474 fill_zero(inode, pg_end, 0, off_end);
475
476 if (pg_start < pg_end) {
477 struct address_space *mapping = inode->i_mapping;
478 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500479 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900480 int ilock;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500481
482 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900483
484 blk_start = pg_start << PAGE_CACHE_SHIFT;
485 blk_end = pg_end << PAGE_CACHE_SHIFT;
486 truncate_inode_pages_range(mapping, blk_start,
487 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900488
489 ilock = mutex_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900490 ret = truncate_hole(inode, pg_start, pg_end);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900491 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900492 }
493 }
494
495 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
496 i_size_read(inode) <= (offset + len)) {
497 i_size_write(inode, offset);
498 mark_inode_dirty(inode);
499 }
500
501 return ret;
502}
503
504static int expand_inode_data(struct inode *inode, loff_t offset,
505 loff_t len, int mode)
506{
507 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
508 pgoff_t index, pg_start, pg_end;
509 loff_t new_size = i_size_read(inode);
510 loff_t off_start, off_end;
511 int ret = 0;
512
513 ret = inode_newsize_ok(inode, (len + offset));
514 if (ret)
515 return ret;
516
517 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
518 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
519
520 off_start = offset & (PAGE_CACHE_SIZE - 1);
521 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
522
523 for (index = pg_start; index <= pg_end; index++) {
524 struct dnode_of_data dn;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900525 int ilock;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900526
Jaegeuk Kim39936832012-11-22 16:21:29 +0900527 ilock = mutex_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900528 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900529 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900530 if (ret) {
Jaegeuk Kim39936832012-11-22 16:21:29 +0900531 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 break;
533 }
534
535 if (dn.data_blkaddr == NULL_ADDR) {
536 ret = reserve_new_block(&dn);
537 if (ret) {
538 f2fs_put_dnode(&dn);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900539 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900540 break;
541 }
542 }
543 f2fs_put_dnode(&dn);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900544 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545
546 if (pg_start == pg_end)
547 new_size = offset + len;
548 else if (index == pg_start && off_start)
549 new_size = (index + 1) << PAGE_CACHE_SHIFT;
550 else if (index == pg_end)
551 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
552 else
553 new_size += PAGE_CACHE_SIZE;
554 }
555
556 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
557 i_size_read(inode) < new_size) {
558 i_size_write(inode, new_size);
559 mark_inode_dirty(inode);
560 }
561
562 return ret;
563}
564
565static long f2fs_fallocate(struct file *file, int mode,
566 loff_t offset, loff_t len)
567{
Al Viro6131ffa2013-02-27 16:59:05 -0500568 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900569 long ret;
570
571 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
572 return -EOPNOTSUPP;
573
574 if (mode & FALLOC_FL_PUNCH_HOLE)
575 ret = punch_hole(inode, offset, len, mode);
576 else
577 ret = expand_inode_data(inode, offset, len, mode);
578
Namjae Jeon3af60a42012-12-30 14:52:37 +0900579 if (!ret) {
580 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
581 mark_inode_dirty(inode);
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};