blob: 9dfcdab5ea7c1e1438fbb33c441bf0b3b31be78c [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
66 lock_page(page);
67 if (page->mapping != inode->i_mapping ||
68 page_offset(page) >= i_size_read(inode) ||
69 !PageUptodate(page)) {
70 unlock_page(page);
71 err = -EFAULT;
72 goto out;
73 }
74
75 /*
76 * check to see if the page is mapped already (no holes)
77 */
78 if (PageMappedToDisk(page))
79 goto out;
80
81 /* fill the page */
82 wait_on_page_writeback(page);
83
84 /* page is wholly or partially inside EOF */
85 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
86 unsigned offset;
87 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
88 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
89 }
90 set_page_dirty(page);
91 SetPageUptodate(page);
92
93 file_update_time(vma->vm_file);
94out:
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 Kimfbfa2cc2012-11-02 17:09:44 +0900105int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
106{
107 struct inode *inode = file->f_mapping->host;
108 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900109 int ret = 0;
110 bool need_cp = false;
111 struct writeback_control wbc = {
112 .sync_mode = WB_SYNC_ALL,
113 .nr_to_write = LONG_MAX,
114 .for_reclaim = 0,
115 };
116
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900117 if (inode->i_sb->s_flags & MS_RDONLY)
118 return 0;
119
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900120 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900121 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900122 if (ret) {
123 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900124 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900125 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900126
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900127 /* guarantee free sections for fsync */
128 f2fs_balance_fs(sbi);
129
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900130 mutex_lock(&inode->i_mutex);
131
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900132 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
133 goto out;
134
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900135 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
136 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900137 else if (is_cp_file(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900138 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900139 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900140 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900141 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900142 need_cp = true;
143
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900144 if (need_cp) {
145 /* all the dirty node pages should be flushed for POR */
146 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900147 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900148 /* if there is no written node page, write its inode page */
149 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
150 ret = f2fs_write_inode(inode, NULL);
151 if (ret)
152 goto out;
153 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900154 filemap_fdatawait_range(sbi->node_inode->i_mapping,
155 0, LONG_MAX);
Jaegeuk Kimae51fb32013-03-16 11:13:04 +0900156 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900157 }
158out:
159 mutex_unlock(&inode->i_mutex);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900160 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900161 return ret;
162}
163
164static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
165{
166 file_accessed(file);
167 vma->vm_ops = &f2fs_file_vm_ops;
168 return 0;
169}
170
171static int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
172{
173 int nr_free = 0, ofs = dn->ofs_in_node;
174 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
175 struct f2fs_node *raw_node;
176 __le32 *addr;
177
178 raw_node = page_address(dn->node_page);
179 addr = blkaddr_in_node(raw_node) + ofs;
180
181 for ( ; count > 0; count--, addr++, dn->ofs_in_node++) {
182 block_t blkaddr = le32_to_cpu(*addr);
183 if (blkaddr == NULL_ADDR)
184 continue;
185
186 update_extent_cache(NULL_ADDR, dn);
187 invalidate_blocks(sbi, blkaddr);
188 dec_valid_block_count(sbi, dn->inode, 1);
189 nr_free++;
190 }
191 if (nr_free) {
192 set_page_dirty(dn->node_page);
193 sync_inode_page(dn);
194 }
195 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900196
197 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
198 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900199 return nr_free;
200}
201
202void truncate_data_blocks(struct dnode_of_data *dn)
203{
204 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
205}
206
207static void truncate_partial_data_page(struct inode *inode, u64 from)
208{
209 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
210 struct page *page;
211
212 if (!offset)
213 return;
214
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900215 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900216 if (IS_ERR(page))
217 return;
218
219 lock_page(page);
220 wait_on_page_writeback(page);
221 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
222 set_page_dirty(page);
223 f2fs_put_page(page, 1);
224}
225
226static int truncate_blocks(struct inode *inode, u64 from)
227{
228 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
229 unsigned int blocksize = inode->i_sb->s_blocksize;
230 struct dnode_of_data dn;
231 pgoff_t free_from;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900232 int count = 0, ilock = -1;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900233 int err;
234
Namjae Jeon51dd6242013-04-20 01:28:52 +0900235 trace_f2fs_truncate_blocks_enter(inode, from);
236
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900237 free_from = (pgoff_t)
238 ((from + blocksize - 1) >> (sbi->log_blocksize));
239
Jaegeuk Kim39936832012-11-22 16:21:29 +0900240 ilock = mutex_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900241 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900242 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900243 if (err) {
244 if (err == -ENOENT)
245 goto free_next;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900246 mutex_unlock_op(sbi, ilock);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900247 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900248 return err;
249 }
250
251 if (IS_INODE(dn.node_page))
252 count = ADDRS_PER_INODE;
253 else
254 count = ADDRS_PER_BLOCK;
255
256 count -= dn.ofs_in_node;
257 BUG_ON(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900258
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900259 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
260 truncate_data_blocks_range(&dn, count);
261 free_from += count;
262 }
263
264 f2fs_put_dnode(&dn);
265free_next:
266 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900267 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900268
269 /* lastly zero out the first data page */
270 truncate_partial_data_page(inode, from);
271
Namjae Jeon51dd6242013-04-20 01:28:52 +0900272 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900273 return err;
274}
275
276void f2fs_truncate(struct inode *inode)
277{
278 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
279 S_ISLNK(inode->i_mode)))
280 return;
281
Namjae Jeon51dd6242013-04-20 01:28:52 +0900282 trace_f2fs_truncate(inode);
283
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900284 if (!truncate_blocks(inode, i_size_read(inode))) {
285 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
286 mark_inode_dirty(inode);
287 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900288}
289
290static int f2fs_getattr(struct vfsmount *mnt,
291 struct dentry *dentry, struct kstat *stat)
292{
293 struct inode *inode = dentry->d_inode;
294 generic_fillattr(inode, stat);
295 stat->blocks <<= 3;
296 return 0;
297}
298
299#ifdef CONFIG_F2FS_FS_POSIX_ACL
300static void __setattr_copy(struct inode *inode, const struct iattr *attr)
301{
302 struct f2fs_inode_info *fi = F2FS_I(inode);
303 unsigned int ia_valid = attr->ia_valid;
304
305 if (ia_valid & ATTR_UID)
306 inode->i_uid = attr->ia_uid;
307 if (ia_valid & ATTR_GID)
308 inode->i_gid = attr->ia_gid;
309 if (ia_valid & ATTR_ATIME)
310 inode->i_atime = timespec_trunc(attr->ia_atime,
311 inode->i_sb->s_time_gran);
312 if (ia_valid & ATTR_MTIME)
313 inode->i_mtime = timespec_trunc(attr->ia_mtime,
314 inode->i_sb->s_time_gran);
315 if (ia_valid & ATTR_CTIME)
316 inode->i_ctime = timespec_trunc(attr->ia_ctime,
317 inode->i_sb->s_time_gran);
318 if (ia_valid & ATTR_MODE) {
319 umode_t mode = attr->ia_mode;
320
321 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
322 mode &= ~S_ISGID;
323 set_acl_inode(fi, mode);
324 }
325}
326#else
327#define __setattr_copy setattr_copy
328#endif
329
330int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
331{
332 struct inode *inode = dentry->d_inode;
333 struct f2fs_inode_info *fi = F2FS_I(inode);
334 int err;
335
336 err = inode_change_ok(inode, attr);
337 if (err)
338 return err;
339
340 if ((attr->ia_valid & ATTR_SIZE) &&
341 attr->ia_size != i_size_read(inode)) {
342 truncate_setsize(inode, attr->ia_size);
343 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900344 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900345 }
346
347 __setattr_copy(inode, attr);
348
349 if (attr->ia_valid & ATTR_MODE) {
350 err = f2fs_acl_chmod(inode);
351 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
352 inode->i_mode = fi->i_acl_mode;
353 clear_inode_flag(fi, FI_ACL_MODE);
354 }
355 }
356
357 mark_inode_dirty(inode);
358 return err;
359}
360
361const struct inode_operations f2fs_file_inode_operations = {
362 .getattr = f2fs_getattr,
363 .setattr = f2fs_setattr,
364 .get_acl = f2fs_get_acl,
365#ifdef CONFIG_F2FS_FS_XATTR
366 .setxattr = generic_setxattr,
367 .getxattr = generic_getxattr,
368 .listxattr = f2fs_listxattr,
369 .removexattr = generic_removexattr,
370#endif
371};
372
373static void fill_zero(struct inode *inode, pgoff_t index,
374 loff_t start, loff_t len)
375{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900376 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900377 struct page *page;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900378 int ilock;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900379
380 if (!len)
381 return;
382
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900383 f2fs_balance_fs(sbi);
384
Jaegeuk Kim39936832012-11-22 16:21:29 +0900385 ilock = mutex_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900386 page = get_new_data_page(inode, index, false);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900387 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900388
389 if (!IS_ERR(page)) {
390 wait_on_page_writeback(page);
391 zero_user(page, start, len);
392 set_page_dirty(page);
393 f2fs_put_page(page, 1);
394 }
395}
396
397int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
398{
399 pgoff_t index;
400 int err;
401
402 for (index = pg_start; index < pg_end; index++) {
403 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900404
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900405 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900406 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900407 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900408 if (err == -ENOENT)
409 continue;
410 return err;
411 }
412
413 if (dn.data_blkaddr != NULL_ADDR)
414 truncate_data_blocks_range(&dn, 1);
415 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900416 }
417 return 0;
418}
419
420static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode)
421{
422 pgoff_t pg_start, pg_end;
423 loff_t off_start, off_end;
424 int ret = 0;
425
426 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
427 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
428
429 off_start = offset & (PAGE_CACHE_SIZE - 1);
430 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
431
432 if (pg_start == pg_end) {
433 fill_zero(inode, pg_start, off_start,
434 off_end - off_start);
435 } else {
436 if (off_start)
437 fill_zero(inode, pg_start++, off_start,
438 PAGE_CACHE_SIZE - off_start);
439 if (off_end)
440 fill_zero(inode, pg_end, 0, off_end);
441
442 if (pg_start < pg_end) {
443 struct address_space *mapping = inode->i_mapping;
444 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500445 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900446 int ilock;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500447
448 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900449
450 blk_start = pg_start << PAGE_CACHE_SHIFT;
451 blk_end = pg_end << PAGE_CACHE_SHIFT;
452 truncate_inode_pages_range(mapping, blk_start,
453 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900454
455 ilock = mutex_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900456 ret = truncate_hole(inode, pg_start, pg_end);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900457 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900458 }
459 }
460
461 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
462 i_size_read(inode) <= (offset + len)) {
463 i_size_write(inode, offset);
464 mark_inode_dirty(inode);
465 }
466
467 return ret;
468}
469
470static int expand_inode_data(struct inode *inode, loff_t offset,
471 loff_t len, int mode)
472{
473 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
474 pgoff_t index, pg_start, pg_end;
475 loff_t new_size = i_size_read(inode);
476 loff_t off_start, off_end;
477 int ret = 0;
478
479 ret = inode_newsize_ok(inode, (len + offset));
480 if (ret)
481 return ret;
482
483 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
484 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
485
486 off_start = offset & (PAGE_CACHE_SIZE - 1);
487 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
488
489 for (index = pg_start; index <= pg_end; index++) {
490 struct dnode_of_data dn;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900491 int ilock;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900492
Jaegeuk Kim39936832012-11-22 16:21:29 +0900493 ilock = mutex_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900494 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900495 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900496 if (ret) {
Jaegeuk Kim39936832012-11-22 16:21:29 +0900497 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900498 break;
499 }
500
501 if (dn.data_blkaddr == NULL_ADDR) {
502 ret = reserve_new_block(&dn);
503 if (ret) {
504 f2fs_put_dnode(&dn);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900505 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900506 break;
507 }
508 }
509 f2fs_put_dnode(&dn);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900510 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900511
512 if (pg_start == pg_end)
513 new_size = offset + len;
514 else if (index == pg_start && off_start)
515 new_size = (index + 1) << PAGE_CACHE_SHIFT;
516 else if (index == pg_end)
517 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
518 else
519 new_size += PAGE_CACHE_SIZE;
520 }
521
522 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
523 i_size_read(inode) < new_size) {
524 i_size_write(inode, new_size);
525 mark_inode_dirty(inode);
526 }
527
528 return ret;
529}
530
531static long f2fs_fallocate(struct file *file, int mode,
532 loff_t offset, loff_t len)
533{
Al Viro6131ffa2013-02-27 16:59:05 -0500534 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900535 long ret;
536
537 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
538 return -EOPNOTSUPP;
539
540 if (mode & FALLOC_FL_PUNCH_HOLE)
541 ret = punch_hole(inode, offset, len, mode);
542 else
543 ret = expand_inode_data(inode, offset, len, mode);
544
Namjae Jeon3af60a42012-12-30 14:52:37 +0900545 if (!ret) {
546 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
547 mark_inode_dirty(inode);
548 }
Namjae Jeonc01e2852013-04-23 17:00:52 +0900549 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900550 return ret;
551}
552
553#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
554#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
555
556static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
557{
558 if (S_ISDIR(mode))
559 return flags;
560 else if (S_ISREG(mode))
561 return flags & F2FS_REG_FLMASK;
562 else
563 return flags & F2FS_OTHER_FLMASK;
564}
565
566long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
567{
Al Viro6131ffa2013-02-27 16:59:05 -0500568 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900569 struct f2fs_inode_info *fi = F2FS_I(inode);
570 unsigned int flags;
571 int ret;
572
573 switch (cmd) {
574 case FS_IOC_GETFLAGS:
575 flags = fi->i_flags & FS_FL_USER_VISIBLE;
576 return put_user(flags, (int __user *) arg);
577 case FS_IOC_SETFLAGS:
578 {
579 unsigned int oldflags;
580
581 ret = mnt_want_write(filp->f_path.mnt);
582 if (ret)
583 return ret;
584
585 if (!inode_owner_or_capable(inode)) {
586 ret = -EACCES;
587 goto out;
588 }
589
590 if (get_user(flags, (int __user *) arg)) {
591 ret = -EFAULT;
592 goto out;
593 }
594
595 flags = f2fs_mask_flags(inode->i_mode, flags);
596
597 mutex_lock(&inode->i_mutex);
598
599 oldflags = fi->i_flags;
600
601 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
602 if (!capable(CAP_LINUX_IMMUTABLE)) {
603 mutex_unlock(&inode->i_mutex);
604 ret = -EPERM;
605 goto out;
606 }
607 }
608
609 flags = flags & FS_FL_USER_MODIFIABLE;
610 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
611 fi->i_flags = flags;
612 mutex_unlock(&inode->i_mutex);
613
614 f2fs_set_inode_flags(inode);
615 inode->i_ctime = CURRENT_TIME;
616 mark_inode_dirty(inode);
617out:
618 mnt_drop_write(filp->f_path.mnt);
619 return ret;
620 }
621 default:
622 return -ENOTTY;
623 }
624}
625
Namjae Jeone9750822013-02-04 23:41:41 +0900626#ifdef CONFIG_COMPAT
627long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
628{
629 switch (cmd) {
630 case F2FS_IOC32_GETFLAGS:
631 cmd = F2FS_IOC_GETFLAGS;
632 break;
633 case F2FS_IOC32_SETFLAGS:
634 cmd = F2FS_IOC_SETFLAGS;
635 break;
636 default:
637 return -ENOIOCTLCMD;
638 }
639 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
640}
641#endif
642
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900643const struct file_operations f2fs_file_operations = {
644 .llseek = generic_file_llseek,
645 .read = do_sync_read,
646 .write = do_sync_write,
647 .aio_read = generic_file_aio_read,
648 .aio_write = generic_file_aio_write,
649 .open = generic_file_open,
650 .mmap = f2fs_file_mmap,
651 .fsync = f2fs_sync_file,
652 .fallocate = f2fs_fallocate,
653 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900654#ifdef CONFIG_COMPAT
655 .compat_ioctl = f2fs_compat_ioctl,
656#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900657 .splice_read = generic_file_splice_read,
658 .splice_write = generic_file_splice_write,
659};