blob: f5d19e54edd2a27e996fdc43f1550a32765c44c8 [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
2 * 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>
16#include <linux/blkdev.h>
17#include <linux/falloc.h>
18#include <linux/types.h>
19#include <linux/compat.h>
20#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#include <trace/events/f2fs.h>
29
30static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
31 struct vm_fault *vmf)
32{
33 struct page *page = vmf->page;
34 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
35 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080036 struct dnode_of_data dn;
37 int err;
38
39 f2fs_balance_fs(sbi);
40
Evan McClain1a808fb2014-03-20 09:12:16 -040041 /* F2FS backport: We replace in old kernels sb_start_pagefault(inode->i_sb) with vfs_check_frozen()
42 * and remove the original sb_end_pagefault(inode->i_sb) after the out label
43 *
44 * The introduction of sb_{start,end}_pagefault() was made post-3.2 kernels by Jan Kara
45 * and merged in commit a0e881b7c189fa2bd76c024dbff91e79511c971d.
46 * Discussed at https://lkml.org/lkml/2012/3/5/278
47 *
48 * - Alex
49 */
50 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080051
52 /* block allocation */
53 f2fs_lock_op(sbi);
54 set_new_dnode(&dn, inode, NULL, NULL, 0);
Changman Leeb1a94e82013-11-15 10:42:51 +090055 err = f2fs_reserve_block(&dn, page->index);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080056 f2fs_unlock_op(sbi);
Changman Leeb1a94e82013-11-15 10:42:51 +090057 if (err)
58 goto out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -080059
60 file_update_time(vma->vm_file);
61 lock_page(page);
Changman Leeb1a94e82013-11-15 10:42:51 +090062 if (unlikely(page->mapping != inode->i_mapping ||
Linus Torvalds8005ecc2012-12-20 13:54:51 -080063 page_offset(page) > i_size_read(inode) ||
Changman Leeb1a94e82013-11-15 10:42:51 +090064 !PageUptodate(page))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -080065 unlock_page(page);
66 err = -EFAULT;
67 goto out;
68 }
69
70 /*
71 * check to see if the page is mapped already (no holes)
72 */
73 if (PageMappedToDisk(page))
74 goto mapped;
75
76 /* page is wholly or partially inside EOF */
77 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
78 unsigned offset;
79 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
80 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
81 }
82 set_page_dirty(page);
83 SetPageUptodate(page);
84
85 trace_f2fs_vm_page_mkwrite(page, DATA);
86mapped:
87 /* fill the page */
88 wait_on_page_writeback(page);
89out:
Linus Torvalds8005ecc2012-12-20 13:54:51 -080090 return block_page_mkwrite_return(err);
91}
92
93static const struct vm_operations_struct f2fs_file_vm_ops = {
Evan McClain1a808fb2014-03-20 09:12:16 -040094 .fault = filemap_fault,
95 .page_mkwrite = f2fs_vm_page_mkwrite,
Linus Torvalds8005ecc2012-12-20 13:54:51 -080096};
97
98static int get_parent_ino(struct inode *inode, nid_t *pino)
99{
100 struct dentry *dentry;
101
102 inode = igrab(inode);
103 dentry = d_find_any_alias(inode);
104 iput(inode);
105 if (!dentry)
106 return 0;
107
108 if (update_dent_inode(inode, &dentry->d_name)) {
109 dput(dentry);
110 return 0;
111 }
112
113 *pino = parent_ino(dentry);
114 dput(dentry);
115 return 1;
116}
117
118int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
119{
120 struct inode *inode = file->f_mapping->host;
121 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
122 int ret = 0;
123 bool need_cp = false;
124 struct writeback_control wbc = {
Changman Leeb1a94e82013-11-15 10:42:51 +0900125 .sync_mode = WB_SYNC_NONE,
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800126 .nr_to_write = LONG_MAX,
127 .for_reclaim = 0,
128 };
129
Changman Leeb1a94e82013-11-15 10:42:51 +0900130 if (unlikely(f2fs_readonly(inode->i_sb)))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800131 return 0;
132
133 trace_f2fs_sync_file_enter(inode);
134 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
135 if (ret) {
136 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
137 return ret;
138 }
139
140 /* guarantee free sections for fsync */
141 f2fs_balance_fs(sbi);
142
143 mutex_lock(&inode->i_mutex);
144
145 /*
146 * Both of fdatasync() and fsync() are able to be recovered from
147 * sudden-power-off.
148 */
149 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
150 need_cp = true;
151 else if (file_wrong_pino(inode))
152 need_cp = true;
153 else if (!space_for_roll_forward(sbi))
154 need_cp = true;
155 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
156 need_cp = true;
157 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
158 need_cp = true;
159
160 if (need_cp) {
161 nid_t pino;
162
163 F2FS_I(inode)->xattr_ver = 0;
164
165 /* all the dirty node pages should be flushed for POR */
166 ret = f2fs_sync_fs(inode->i_sb, 1);
167 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 }
176 } else {
177 /* if there is no written node page, write its inode page */
178 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
179 mark_inode_dirty_sync(inode);
180 ret = f2fs_write_inode(inode, NULL);
181 if (ret)
182 goto out;
183 }
184 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
185 if (ret)
186 goto out;
187 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
188 }
189out:
190 mutex_unlock(&inode->i_mutex);
191 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
192 return ret;
193}
194
195static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
196{
197 file_accessed(file);
198 vma->vm_ops = &f2fs_file_vm_ops;
Evan McClain1a808fb2014-03-20 09:12:16 -0400199 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800200 return 0;
201}
202
203int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
204{
205 int nr_free = 0, ofs = dn->ofs_in_node;
206 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
207 struct f2fs_node *raw_node;
208 __le32 *addr;
209
210 raw_node = F2FS_NODE(dn->node_page);
211 addr = blkaddr_in_node(raw_node) + ofs;
212
Changman Leeb1a94e82013-11-15 10:42:51 +0900213 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800214 block_t blkaddr = le32_to_cpu(*addr);
215 if (blkaddr == NULL_ADDR)
216 continue;
217
218 update_extent_cache(NULL_ADDR, dn);
219 invalidate_blocks(sbi, blkaddr);
220 nr_free++;
221 }
222 if (nr_free) {
223 dec_valid_block_count(sbi, dn->inode, nr_free);
224 set_page_dirty(dn->node_page);
225 sync_inode_page(dn);
226 }
227 dn->ofs_in_node = ofs;
228
229 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
230 dn->ofs_in_node, nr_free);
231 return nr_free;
232}
233
234void truncate_data_blocks(struct dnode_of_data *dn)
235{
236 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
237}
238
239static void truncate_partial_data_page(struct inode *inode, u64 from)
240{
241 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
242 struct page *page;
243
244 if (!offset)
245 return;
246
247 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
248 if (IS_ERR(page))
249 return;
250
251 lock_page(page);
Changman Leeb1a94e82013-11-15 10:42:51 +0900252 if (unlikely(page->mapping != inode->i_mapping)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800253 f2fs_put_page(page, 1);
254 return;
255 }
256 wait_on_page_writeback(page);
257 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
258 set_page_dirty(page);
259 f2fs_put_page(page, 1);
260}
261
Changman Leeb1a94e82013-11-15 10:42:51 +0900262int truncate_blocks(struct inode *inode, u64 from)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800263{
264 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
265 unsigned int blocksize = inode->i_sb->s_blocksize;
266 struct dnode_of_data dn;
267 pgoff_t free_from;
Changman Leeb1a94e82013-11-15 10:42:51 +0900268 int count = 0, err = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800269
270 trace_f2fs_truncate_blocks_enter(inode, from);
271
Changman Leeb1a94e82013-11-15 10:42:51 +0900272 if (f2fs_has_inline_data(inode))
273 goto done;
274
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800275 free_from = (pgoff_t)
276 ((from + blocksize - 1) >> (sbi->log_blocksize));
277
278 f2fs_lock_op(sbi);
Changman Leeb1a94e82013-11-15 10:42:51 +0900279
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800280 set_new_dnode(&dn, inode, NULL, NULL, 0);
281 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
282 if (err) {
283 if (err == -ENOENT)
284 goto free_next;
285 f2fs_unlock_op(sbi);
286 trace_f2fs_truncate_blocks_exit(inode, err);
287 return err;
288 }
289
290 if (IS_INODE(dn.node_page))
291 count = ADDRS_PER_INODE(F2FS_I(inode));
292 else
293 count = ADDRS_PER_BLOCK;
294
295 count -= dn.ofs_in_node;
296 f2fs_bug_on(count < 0);
297
298 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
299 truncate_data_blocks_range(&dn, count);
300 free_from += count;
301 }
302
303 f2fs_put_dnode(&dn);
304free_next:
305 err = truncate_inode_blocks(inode, free_from);
306 f2fs_unlock_op(sbi);
Changman Leeb1a94e82013-11-15 10:42:51 +0900307done:
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800308 /* lastly zero out the first data page */
309 truncate_partial_data_page(inode, from);
310
311 trace_f2fs_truncate_blocks_exit(inode, err);
312 return err;
313}
314
315void f2fs_truncate(struct inode *inode)
316{
317 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
318 S_ISLNK(inode->i_mode)))
319 return;
320
321 trace_f2fs_truncate(inode);
322
323 if (!truncate_blocks(inode, i_size_read(inode))) {
324 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
325 mark_inode_dirty(inode);
326 }
327}
328
329int f2fs_getattr(struct vfsmount *mnt,
330 struct dentry *dentry, struct kstat *stat)
331{
332 struct inode *inode = dentry->d_inode;
333 generic_fillattr(inode, stat);
334 stat->blocks <<= 3;
335 return 0;
336}
337
338#ifdef CONFIG_F2FS_FS_POSIX_ACL
339static void __setattr_copy(struct inode *inode, const struct iattr *attr)
340{
341 struct f2fs_inode_info *fi = F2FS_I(inode);
342 unsigned int ia_valid = attr->ia_valid;
343
344 if (ia_valid & ATTR_UID)
345 inode->i_uid = attr->ia_uid;
346 if (ia_valid & ATTR_GID)
347 inode->i_gid = attr->ia_gid;
348 if (ia_valid & ATTR_ATIME)
349 inode->i_atime = timespec_trunc(attr->ia_atime,
350 inode->i_sb->s_time_gran);
351 if (ia_valid & ATTR_MTIME)
352 inode->i_mtime = timespec_trunc(attr->ia_mtime,
353 inode->i_sb->s_time_gran);
354 if (ia_valid & ATTR_CTIME)
355 inode->i_ctime = timespec_trunc(attr->ia_ctime,
356 inode->i_sb->s_time_gran);
357 if (ia_valid & ATTR_MODE) {
358 umode_t mode = attr->ia_mode;
359
360 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
361 mode &= ~S_ISGID;
362 set_acl_inode(fi, mode);
363 }
364}
365#else
366#define __setattr_copy setattr_copy
367#endif
368
369int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
370{
371 struct inode *inode = dentry->d_inode;
372 struct f2fs_inode_info *fi = F2FS_I(inode);
373 int err;
374
375 err = inode_change_ok(inode, attr);
376 if (err)
377 return err;
378
379 if ((attr->ia_valid & ATTR_SIZE) &&
380 attr->ia_size != i_size_read(inode)) {
Changman Leeb1a94e82013-11-15 10:42:51 +0900381 err = f2fs_convert_inline_data(inode, attr->ia_size);
382 if (err)
383 return err;
384
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800385 truncate_setsize(inode, attr->ia_size);
386 f2fs_truncate(inode);
387 f2fs_balance_fs(F2FS_SB(inode->i_sb));
388 }
389
390 __setattr_copy(inode, attr);
391
392 if (attr->ia_valid & ATTR_MODE) {
393 err = f2fs_acl_chmod(inode);
394 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
395 inode->i_mode = fi->i_acl_mode;
396 clear_inode_flag(fi, FI_ACL_MODE);
397 }
398 }
399
400 mark_inode_dirty(inode);
401 return err;
402}
403
404const struct inode_operations f2fs_file_inode_operations = {
405 .getattr = f2fs_getattr,
406 .setattr = f2fs_setattr,
407 .get_acl = f2fs_get_acl,
408#ifdef CONFIG_F2FS_FS_XATTR
409 .setxattr = generic_setxattr,
410 .getxattr = generic_getxattr,
411 .listxattr = f2fs_listxattr,
412 .removexattr = generic_removexattr,
413#endif
414};
415
416static void fill_zero(struct inode *inode, pgoff_t index,
417 loff_t start, loff_t len)
418{
419 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
420 struct page *page;
421
422 if (!len)
423 return;
424
425 f2fs_balance_fs(sbi);
426
427 f2fs_lock_op(sbi);
428 page = get_new_data_page(inode, NULL, index, false);
429 f2fs_unlock_op(sbi);
430
431 if (!IS_ERR(page)) {
432 wait_on_page_writeback(page);
433 zero_user(page, start, len);
434 set_page_dirty(page);
435 f2fs_put_page(page, 1);
436 }
437}
438
439int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
440{
441 pgoff_t index;
442 int err;
443
444 for (index = pg_start; index < pg_end; index++) {
445 struct dnode_of_data dn;
446
447 set_new_dnode(&dn, inode, NULL, NULL, 0);
448 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
449 if (err) {
450 if (err == -ENOENT)
451 continue;
452 return err;
453 }
454
455 if (dn.data_blkaddr != NULL_ADDR)
456 truncate_data_blocks_range(&dn, 1);
457 f2fs_put_dnode(&dn);
458 }
459 return 0;
460}
461
Changman Leeb1a94e82013-11-15 10:42:51 +0900462static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800463{
464 pgoff_t pg_start, pg_end;
465 loff_t off_start, off_end;
466 int ret = 0;
467
Changman Leeb1a94e82013-11-15 10:42:51 +0900468 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1);
469 if (ret)
470 return ret;
471
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800472 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
473 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
474
475 off_start = offset & (PAGE_CACHE_SIZE - 1);
476 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
477
478 if (pg_start == pg_end) {
479 fill_zero(inode, pg_start, off_start,
480 off_end - off_start);
481 } else {
482 if (off_start)
483 fill_zero(inode, pg_start++, off_start,
484 PAGE_CACHE_SIZE - off_start);
485 if (off_end)
486 fill_zero(inode, pg_end, 0, off_end);
487
488 if (pg_start < pg_end) {
489 struct address_space *mapping = inode->i_mapping;
490 loff_t blk_start, blk_end;
491 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
492
493 f2fs_balance_fs(sbi);
494
495 blk_start = pg_start << PAGE_CACHE_SHIFT;
496 blk_end = pg_end << PAGE_CACHE_SHIFT;
497 truncate_inode_pages_range(mapping, blk_start,
498 blk_end - 1);
499
500 f2fs_lock_op(sbi);
501 ret = truncate_hole(inode, pg_start, pg_end);
502 f2fs_unlock_op(sbi);
503 }
504 }
505
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800506 return ret;
507}
508
509static int expand_inode_data(struct inode *inode, loff_t offset,
510 loff_t len, int mode)
511{
512 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
513 pgoff_t index, pg_start, pg_end;
514 loff_t new_size = i_size_read(inode);
515 loff_t off_start, off_end;
516 int ret = 0;
517
518 ret = inode_newsize_ok(inode, (len + offset));
519 if (ret)
520 return ret;
521
Changman Leeb1a94e82013-11-15 10:42:51 +0900522 ret = f2fs_convert_inline_data(inode, offset + len);
523 if (ret)
524 return ret;
525
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800526 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
527 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
528
529 off_start = offset & (PAGE_CACHE_SIZE - 1);
530 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
531
532 for (index = pg_start; index <= pg_end; index++) {
533 struct dnode_of_data dn;
534
535 f2fs_lock_op(sbi);
536 set_new_dnode(&dn, inode, NULL, NULL, 0);
Changman Leeb1a94e82013-11-15 10:42:51 +0900537 ret = f2fs_reserve_block(&dn, index);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800538 f2fs_unlock_op(sbi);
Changman Leeb1a94e82013-11-15 10:42:51 +0900539 if (ret)
540 break;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800541
542 if (pg_start == pg_end)
543 new_size = offset + len;
544 else if (index == pg_start && off_start)
545 new_size = (index + 1) << PAGE_CACHE_SHIFT;
546 else if (index == pg_end)
547 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
548 else
549 new_size += PAGE_CACHE_SIZE;
550 }
551
552 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
553 i_size_read(inode) < new_size) {
554 i_size_write(inode, new_size);
555 mark_inode_dirty(inode);
556 }
557
558 return ret;
559}
560
561static long f2fs_fallocate(struct file *file, int mode,
562 loff_t offset, loff_t len)
563{
564 struct inode *inode = file->f_path.dentry->d_inode;
565 long ret;
566
567 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
568 return -EOPNOTSUPP;
569
570 if (mode & FALLOC_FL_PUNCH_HOLE)
Changman Leeb1a94e82013-11-15 10:42:51 +0900571 ret = punch_hole(inode, offset, len);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800572 else
573 ret = expand_inode_data(inode, offset, len, mode);
574
575 if (!ret) {
576 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
577 mark_inode_dirty(inode);
578 }
579 trace_f2fs_fallocate(inode, mode, offset, len, ret);
580 return ret;
581}
582
583#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
584#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
585
586static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
587{
588 if (S_ISDIR(mode))
589 return flags;
590 else if (S_ISREG(mode))
591 return flags & F2FS_REG_FLMASK;
592 else
593 return flags & F2FS_OTHER_FLMASK;
594}
595
596long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
597{
598 struct inode *inode = filp->f_dentry->d_inode;
599 struct f2fs_inode_info *fi = F2FS_I(inode);
600 unsigned int flags;
601 int ret;
602
603 switch (cmd) {
604 case F2FS_IOC_GETFLAGS:
605 flags = fi->i_flags & FS_FL_USER_VISIBLE;
606 return put_user(flags, (int __user *) arg);
607 case F2FS_IOC_SETFLAGS:
608 {
609 unsigned int oldflags;
610
611 ret = mnt_want_write(filp->f_path.mnt);
612 if (ret)
613 return ret;
614
615 if (!inode_owner_or_capable(inode)) {
616 ret = -EACCES;
617 goto out;
618 }
619
620 if (get_user(flags, (int __user *) arg)) {
621 ret = -EFAULT;
622 goto out;
623 }
624
625 flags = f2fs_mask_flags(inode->i_mode, flags);
626
627 mutex_lock(&inode->i_mutex);
628
629 oldflags = fi->i_flags;
630
631 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
632 if (!capable(CAP_LINUX_IMMUTABLE)) {
633 mutex_unlock(&inode->i_mutex);
634 ret = -EPERM;
635 goto out;
636 }
637 }
638
639 flags = flags & FS_FL_USER_MODIFIABLE;
640 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
641 fi->i_flags = flags;
642 mutex_unlock(&inode->i_mutex);
643
644 f2fs_set_inode_flags(inode);
645 inode->i_ctime = CURRENT_TIME;
646 mark_inode_dirty(inode);
647out:
648 mnt_drop_write(filp->f_path.mnt);
649 return ret;
650 }
651 default:
652 return -ENOTTY;
653 }
654}
655
656#ifdef CONFIG_COMPAT
657long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
658{
659 switch (cmd) {
660 case F2FS_IOC32_GETFLAGS:
661 cmd = F2FS_IOC_GETFLAGS;
662 break;
663 case F2FS_IOC32_SETFLAGS:
664 cmd = F2FS_IOC_SETFLAGS;
665 break;
666 default:
667 return -ENOIOCTLCMD;
668 }
669 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
670}
671#endif
672
673const struct file_operations f2fs_file_operations = {
674 .llseek = generic_file_llseek,
675 .read = do_sync_read,
676 .write = do_sync_write,
677 .aio_read = generic_file_aio_read,
678 .aio_write = generic_file_aio_write,
679 .open = generic_file_open,
680 .mmap = f2fs_file_mmap,
681 .fsync = f2fs_sync_file,
682 .fallocate = f2fs_fallocate,
683 .unlocked_ioctl = f2fs_ioctl,
684#ifdef CONFIG_COMPAT
685 .compat_ioctl = f2fs_compat_ioctl,
686#endif
687 .splice_read = generic_file_splice_read,
688 .splice_write = generic_file_splice_write,
689};