blob: ff018a42e435438e3f34010eb7022e0b27b38749 [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"
28
29static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
30 struct vm_fault *vmf)
31{
32 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050033 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090034 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035 block_t old_blk_addr;
36 struct dnode_of_data dn;
37 int err;
38
39 f2fs_balance_fs(sbi);
40
41 sb_start_pagefault(inode->i_sb);
42
43 mutex_lock_op(sbi, DATA_NEW);
44
45 /* block allocation */
46 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) {
49 mutex_unlock_op(sbi, DATA_NEW);
50 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);
59 mutex_unlock_op(sbi, DATA_NEW);
60 goto out;
61 }
62 }
63 f2fs_put_dnode(&dn);
64
65 mutex_unlock_op(sbi, DATA_NEW);
66
67 lock_page(page);
68 if (page->mapping != inode->i_mapping ||
69 page_offset(page) >= i_size_read(inode) ||
70 !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))
80 goto out;
81
82 /* fill the page */
83 wait_on_page_writeback(page);
84
85 /* page is wholly or partially inside EOF */
86 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
87 unsigned offset;
88 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
89 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
90 }
91 set_page_dirty(page);
92 SetPageUptodate(page);
93
94 file_update_time(vma->vm_file);
95out:
96 sb_end_pagefault(inode->i_sb);
97 return block_page_mkwrite_return(err);
98}
99
100static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900101 .fault = filemap_fault,
102 .page_mkwrite = f2fs_vm_page_mkwrite,
103 .remap_pages = generic_file_remap_pages,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900104};
105
106static int need_to_sync_dir(struct f2fs_sb_info *sbi, struct inode *inode)
107{
108 struct dentry *dentry;
109 nid_t pino;
110
111 inode = igrab(inode);
112 dentry = d_find_any_alias(inode);
113 if (!dentry) {
114 iput(inode);
115 return 0;
116 }
117 pino = dentry->d_parent->d_inode->i_ino;
118 dput(dentry);
119 iput(inode);
120 return !is_checkpointed_node(sbi, pino);
121}
122
123int 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);
127 unsigned long long cur_version;
128 int ret = 0;
129 bool need_cp = false;
130 struct writeback_control wbc = {
131 .sync_mode = WB_SYNC_ALL,
132 .nr_to_write = LONG_MAX,
133 .for_reclaim = 0,
134 };
135
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900136 if (inode->i_sb->s_flags & MS_RDONLY)
137 return 0;
138
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900139 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
140 if (ret)
141 return ret;
142
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900143 /* guarantee free sections for fsync */
144 f2fs_balance_fs(sbi);
145
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900146 mutex_lock(&inode->i_mutex);
147
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900148 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
149 goto out;
150
151 mutex_lock(&sbi->cp_mutex);
152 cur_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver);
153 mutex_unlock(&sbi->cp_mutex);
154
155 if (F2FS_I(inode)->data_version != cur_version &&
156 !(inode->i_state & I_DIRTY))
157 goto out;
158 F2FS_I(inode)->data_version--;
159
160 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
161 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900162 else if (is_inode_flag_set(F2FS_I(inode), FI_NEED_CP))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900163 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900164 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900165 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900166 else if (need_to_sync_dir(sbi, inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900167 need_cp = true;
168
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900169 if (need_cp) {
170 /* all the dirty node pages should be flushed for POR */
171 ret = f2fs_sync_fs(inode->i_sb, 1);
172 clear_inode_flag(F2FS_I(inode), FI_NEED_CP);
173 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900174 /* if there is no written node page, write its inode page */
175 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
176 ret = f2fs_write_inode(inode, NULL);
177 if (ret)
178 goto out;
179 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900180 filemap_fdatawait_range(sbi->node_inode->i_mapping,
181 0, LONG_MAX);
Jaegeuk Kimae51fb32013-03-16 11:13:04 +0900182 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900183 }
184out:
185 mutex_unlock(&inode->i_mutex);
186 return ret;
187}
188
189static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
190{
191 file_accessed(file);
192 vma->vm_ops = &f2fs_file_vm_ops;
193 return 0;
194}
195
196static int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
197{
198 int nr_free = 0, ofs = dn->ofs_in_node;
199 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
200 struct f2fs_node *raw_node;
201 __le32 *addr;
202
203 raw_node = page_address(dn->node_page);
204 addr = blkaddr_in_node(raw_node) + ofs;
205
206 for ( ; count > 0; count--, addr++, dn->ofs_in_node++) {
207 block_t blkaddr = le32_to_cpu(*addr);
208 if (blkaddr == NULL_ADDR)
209 continue;
210
211 update_extent_cache(NULL_ADDR, dn);
212 invalidate_blocks(sbi, blkaddr);
213 dec_valid_block_count(sbi, dn->inode, 1);
214 nr_free++;
215 }
216 if (nr_free) {
217 set_page_dirty(dn->node_page);
218 sync_inode_page(dn);
219 }
220 dn->ofs_in_node = ofs;
221 return nr_free;
222}
223
224void truncate_data_blocks(struct dnode_of_data *dn)
225{
226 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
227}
228
229static void truncate_partial_data_page(struct inode *inode, u64 from)
230{
231 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
232 struct page *page;
233
234 if (!offset)
235 return;
236
237 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT);
238 if (IS_ERR(page))
239 return;
240
241 lock_page(page);
242 wait_on_page_writeback(page);
243 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
244 set_page_dirty(page);
245 f2fs_put_page(page, 1);
246}
247
248static int truncate_blocks(struct inode *inode, u64 from)
249{
250 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
251 unsigned int blocksize = inode->i_sb->s_blocksize;
252 struct dnode_of_data dn;
253 pgoff_t free_from;
254 int count = 0;
255 int err;
256
257 free_from = (pgoff_t)
258 ((from + blocksize - 1) >> (sbi->log_blocksize));
259
260 mutex_lock_op(sbi, DATA_TRUNC);
261
262 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900263 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900264 if (err) {
265 if (err == -ENOENT)
266 goto free_next;
267 mutex_unlock_op(sbi, DATA_TRUNC);
268 return err;
269 }
270
271 if (IS_INODE(dn.node_page))
272 count = ADDRS_PER_INODE;
273 else
274 count = ADDRS_PER_BLOCK;
275
276 count -= dn.ofs_in_node;
277 BUG_ON(count < 0);
278 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
279 truncate_data_blocks_range(&dn, count);
280 free_from += count;
281 }
282
283 f2fs_put_dnode(&dn);
284free_next:
285 err = truncate_inode_blocks(inode, free_from);
286 mutex_unlock_op(sbi, DATA_TRUNC);
287
288 /* lastly zero out the first data page */
289 truncate_partial_data_page(inode, from);
290
291 return err;
292}
293
294void f2fs_truncate(struct inode *inode)
295{
296 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
297 S_ISLNK(inode->i_mode)))
298 return;
299
300 if (!truncate_blocks(inode, i_size_read(inode))) {
301 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
302 mark_inode_dirty(inode);
303 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900304}
305
306static int f2fs_getattr(struct vfsmount *mnt,
307 struct dentry *dentry, struct kstat *stat)
308{
309 struct inode *inode = dentry->d_inode;
310 generic_fillattr(inode, stat);
311 stat->blocks <<= 3;
312 return 0;
313}
314
315#ifdef CONFIG_F2FS_FS_POSIX_ACL
316static void __setattr_copy(struct inode *inode, const struct iattr *attr)
317{
318 struct f2fs_inode_info *fi = F2FS_I(inode);
319 unsigned int ia_valid = attr->ia_valid;
320
321 if (ia_valid & ATTR_UID)
322 inode->i_uid = attr->ia_uid;
323 if (ia_valid & ATTR_GID)
324 inode->i_gid = attr->ia_gid;
325 if (ia_valid & ATTR_ATIME)
326 inode->i_atime = timespec_trunc(attr->ia_atime,
327 inode->i_sb->s_time_gran);
328 if (ia_valid & ATTR_MTIME)
329 inode->i_mtime = timespec_trunc(attr->ia_mtime,
330 inode->i_sb->s_time_gran);
331 if (ia_valid & ATTR_CTIME)
332 inode->i_ctime = timespec_trunc(attr->ia_ctime,
333 inode->i_sb->s_time_gran);
334 if (ia_valid & ATTR_MODE) {
335 umode_t mode = attr->ia_mode;
336
337 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
338 mode &= ~S_ISGID;
339 set_acl_inode(fi, mode);
340 }
341}
342#else
343#define __setattr_copy setattr_copy
344#endif
345
346int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
347{
348 struct inode *inode = dentry->d_inode;
349 struct f2fs_inode_info *fi = F2FS_I(inode);
350 int err;
351
352 err = inode_change_ok(inode, attr);
353 if (err)
354 return err;
355
356 if ((attr->ia_valid & ATTR_SIZE) &&
357 attr->ia_size != i_size_read(inode)) {
358 truncate_setsize(inode, attr->ia_size);
359 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900360 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900361 }
362
363 __setattr_copy(inode, attr);
364
365 if (attr->ia_valid & ATTR_MODE) {
366 err = f2fs_acl_chmod(inode);
367 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
368 inode->i_mode = fi->i_acl_mode;
369 clear_inode_flag(fi, FI_ACL_MODE);
370 }
371 }
372
373 mark_inode_dirty(inode);
374 return err;
375}
376
377const struct inode_operations f2fs_file_inode_operations = {
378 .getattr = f2fs_getattr,
379 .setattr = f2fs_setattr,
380 .get_acl = f2fs_get_acl,
381#ifdef CONFIG_F2FS_FS_XATTR
382 .setxattr = generic_setxattr,
383 .getxattr = generic_getxattr,
384 .listxattr = f2fs_listxattr,
385 .removexattr = generic_removexattr,
386#endif
387};
388
389static void fill_zero(struct inode *inode, pgoff_t index,
390 loff_t start, loff_t len)
391{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900392 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900393 struct page *page;
394
395 if (!len)
396 return;
397
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900398 f2fs_balance_fs(sbi);
399
400 mutex_lock_op(sbi, DATA_NEW);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900401 page = get_new_data_page(inode, index, false);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900402 mutex_unlock_op(sbi, DATA_NEW);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900403
404 if (!IS_ERR(page)) {
405 wait_on_page_writeback(page);
406 zero_user(page, start, len);
407 set_page_dirty(page);
408 f2fs_put_page(page, 1);
409 }
410}
411
412int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
413{
414 pgoff_t index;
415 int err;
416
417 for (index = pg_start; index < pg_end; index++) {
418 struct dnode_of_data dn;
419 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
420
Jaegeuk Kim9eaeba72013-01-11 14:09:38 +0900421 f2fs_balance_fs(sbi);
422
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900423 mutex_lock_op(sbi, DATA_TRUNC);
424 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900425 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900426 if (err) {
427 mutex_unlock_op(sbi, DATA_TRUNC);
428 if (err == -ENOENT)
429 continue;
430 return err;
431 }
432
433 if (dn.data_blkaddr != NULL_ADDR)
434 truncate_data_blocks_range(&dn, 1);
435 f2fs_put_dnode(&dn);
436 mutex_unlock_op(sbi, DATA_TRUNC);
437 }
438 return 0;
439}
440
441static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode)
442{
443 pgoff_t pg_start, pg_end;
444 loff_t off_start, off_end;
445 int ret = 0;
446
447 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
448 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
449
450 off_start = offset & (PAGE_CACHE_SIZE - 1);
451 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
452
453 if (pg_start == pg_end) {
454 fill_zero(inode, pg_start, off_start,
455 off_end - off_start);
456 } else {
457 if (off_start)
458 fill_zero(inode, pg_start++, off_start,
459 PAGE_CACHE_SIZE - off_start);
460 if (off_end)
461 fill_zero(inode, pg_end, 0, off_end);
462
463 if (pg_start < pg_end) {
464 struct address_space *mapping = inode->i_mapping;
465 loff_t blk_start, blk_end;
466
467 blk_start = pg_start << PAGE_CACHE_SHIFT;
468 blk_end = pg_end << PAGE_CACHE_SHIFT;
469 truncate_inode_pages_range(mapping, blk_start,
470 blk_end - 1);
471 ret = truncate_hole(inode, pg_start, pg_end);
472 }
473 }
474
475 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
476 i_size_read(inode) <= (offset + len)) {
477 i_size_write(inode, offset);
478 mark_inode_dirty(inode);
479 }
480
481 return ret;
482}
483
484static int expand_inode_data(struct inode *inode, loff_t offset,
485 loff_t len, int mode)
486{
487 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
488 pgoff_t index, pg_start, pg_end;
489 loff_t new_size = i_size_read(inode);
490 loff_t off_start, off_end;
491 int ret = 0;
492
493 ret = inode_newsize_ok(inode, (len + offset));
494 if (ret)
495 return ret;
496
497 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
498 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
499
500 off_start = offset & (PAGE_CACHE_SIZE - 1);
501 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
502
503 for (index = pg_start; index <= pg_end; index++) {
504 struct dnode_of_data dn;
505
506 mutex_lock_op(sbi, DATA_NEW);
507
508 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900509 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900510 if (ret) {
511 mutex_unlock_op(sbi, DATA_NEW);
512 break;
513 }
514
515 if (dn.data_blkaddr == NULL_ADDR) {
516 ret = reserve_new_block(&dn);
517 if (ret) {
518 f2fs_put_dnode(&dn);
519 mutex_unlock_op(sbi, DATA_NEW);
520 break;
521 }
522 }
523 f2fs_put_dnode(&dn);
524
525 mutex_unlock_op(sbi, DATA_NEW);
526
527 if (pg_start == pg_end)
528 new_size = offset + len;
529 else if (index == pg_start && off_start)
530 new_size = (index + 1) << PAGE_CACHE_SHIFT;
531 else if (index == pg_end)
532 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
533 else
534 new_size += PAGE_CACHE_SIZE;
535 }
536
537 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
538 i_size_read(inode) < new_size) {
539 i_size_write(inode, new_size);
540 mark_inode_dirty(inode);
541 }
542
543 return ret;
544}
545
546static long f2fs_fallocate(struct file *file, int mode,
547 loff_t offset, loff_t len)
548{
Al Viro6131ffa2013-02-27 16:59:05 -0500549 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900550 long ret;
551
552 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
553 return -EOPNOTSUPP;
554
555 if (mode & FALLOC_FL_PUNCH_HOLE)
556 ret = punch_hole(inode, offset, len, mode);
557 else
558 ret = expand_inode_data(inode, offset, len, mode);
559
Namjae Jeon3af60a42012-12-30 14:52:37 +0900560 if (!ret) {
561 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
562 mark_inode_dirty(inode);
563 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900564 return ret;
565}
566
567#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
568#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
569
570static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
571{
572 if (S_ISDIR(mode))
573 return flags;
574 else if (S_ISREG(mode))
575 return flags & F2FS_REG_FLMASK;
576 else
577 return flags & F2FS_OTHER_FLMASK;
578}
579
580long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
581{
Al Viro6131ffa2013-02-27 16:59:05 -0500582 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900583 struct f2fs_inode_info *fi = F2FS_I(inode);
584 unsigned int flags;
585 int ret;
586
587 switch (cmd) {
588 case FS_IOC_GETFLAGS:
589 flags = fi->i_flags & FS_FL_USER_VISIBLE;
590 return put_user(flags, (int __user *) arg);
591 case FS_IOC_SETFLAGS:
592 {
593 unsigned int oldflags;
594
595 ret = mnt_want_write(filp->f_path.mnt);
596 if (ret)
597 return ret;
598
599 if (!inode_owner_or_capable(inode)) {
600 ret = -EACCES;
601 goto out;
602 }
603
604 if (get_user(flags, (int __user *) arg)) {
605 ret = -EFAULT;
606 goto out;
607 }
608
609 flags = f2fs_mask_flags(inode->i_mode, flags);
610
611 mutex_lock(&inode->i_mutex);
612
613 oldflags = fi->i_flags;
614
615 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
616 if (!capable(CAP_LINUX_IMMUTABLE)) {
617 mutex_unlock(&inode->i_mutex);
618 ret = -EPERM;
619 goto out;
620 }
621 }
622
623 flags = flags & FS_FL_USER_MODIFIABLE;
624 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
625 fi->i_flags = flags;
626 mutex_unlock(&inode->i_mutex);
627
628 f2fs_set_inode_flags(inode);
629 inode->i_ctime = CURRENT_TIME;
630 mark_inode_dirty(inode);
631out:
632 mnt_drop_write(filp->f_path.mnt);
633 return ret;
634 }
635 default:
636 return -ENOTTY;
637 }
638}
639
Namjae Jeone9750822013-02-04 23:41:41 +0900640#ifdef CONFIG_COMPAT
641long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
642{
643 switch (cmd) {
644 case F2FS_IOC32_GETFLAGS:
645 cmd = F2FS_IOC_GETFLAGS;
646 break;
647 case F2FS_IOC32_SETFLAGS:
648 cmd = F2FS_IOC_SETFLAGS;
649 break;
650 default:
651 return -ENOIOCTLCMD;
652 }
653 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
654}
655#endif
656
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900657const struct file_operations f2fs_file_operations = {
658 .llseek = generic_file_llseek,
659 .read = do_sync_read,
660 .write = do_sync_write,
661 .aio_read = generic_file_aio_read,
662 .aio_write = generic_file_aio_write,
663 .open = generic_file_open,
664 .mmap = f2fs_file_mmap,
665 .fsync = f2fs_sync_file,
666 .fallocate = f2fs_fallocate,
667 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900668#ifdef CONFIG_COMPAT
669 .compat_ioctl = f2fs_compat_ioctl,
670#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900671 .splice_read = generic_file_splice_read,
672 .splice_write = generic_file_splice_write,
673};