4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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 #include <linux/pagevec.h>
29 #include <trace/events/f2fs.h>
31 static int f2fs_vm_page_mkwrite(struct vm_area_struct
*vma
,
34 struct page
*page
= vmf
->page
;
35 struct inode
*inode
= file_inode(vma
->vm_file
);
36 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
37 struct dnode_of_data dn
;
42 sb_start_pagefault(inode
->i_sb
);
44 /* force to convert with normal data indices */
45 err
= f2fs_convert_inline_data(inode
, MAX_INLINE_DATA
+ 1, page
);
49 /* block allocation */
51 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
52 err
= f2fs_reserve_block(&dn
, page
->index
);
57 file_update_time(vma
->vm_file
);
59 if (unlikely(page
->mapping
!= inode
->i_mapping
||
60 page_offset(page
) > i_size_read(inode
) ||
61 !PageUptodate(page
))) {
68 * check to see if the page is mapped already (no holes)
70 if (PageMappedToDisk(page
))
73 /* page is wholly or partially inside EOF */
74 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > i_size_read(inode
)) {
76 offset
= i_size_read(inode
) & ~PAGE_CACHE_MASK
;
77 zero_user_segment(page
, offset
, PAGE_CACHE_SIZE
);
80 SetPageUptodate(page
);
82 trace_f2fs_vm_page_mkwrite(page
, DATA
);
85 f2fs_wait_on_page_writeback(page
, DATA
);
87 sb_end_pagefault(inode
->i_sb
);
88 return block_page_mkwrite_return(err
);
91 static const struct vm_operations_struct f2fs_file_vm_ops
= {
92 .fault
= filemap_fault
,
93 .map_pages
= filemap_map_pages
,
94 .page_mkwrite
= f2fs_vm_page_mkwrite
,
95 .remap_pages
= generic_file_remap_pages
,
98 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
100 struct dentry
*dentry
;
102 inode
= igrab(inode
);
103 dentry
= d_find_any_alias(inode
);
108 if (update_dent_inode(inode
, &dentry
->d_name
)) {
113 *pino
= parent_ino(dentry
);
118 static inline bool need_do_checkpoint(struct inode
*inode
)
120 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
121 bool need_cp
= false;
123 if (!S_ISREG(inode
->i_mode
) || inode
->i_nlink
!= 1)
125 else if (file_wrong_pino(inode
))
127 else if (!space_for_roll_forward(sbi
))
129 else if (!is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
131 else if (F2FS_I(inode
)->xattr_ver
== cur_cp_version(F2FS_CKPT(sbi
)))
137 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
139 struct inode
*inode
= file
->f_mapping
->host
;
140 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
141 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
142 nid_t ino
= inode
->i_ino
;
144 bool need_cp
= false;
145 struct writeback_control wbc
= {
146 .sync_mode
= WB_SYNC_ALL
,
147 .nr_to_write
= LONG_MAX
,
151 if (unlikely(f2fs_readonly(inode
->i_sb
)))
154 trace_f2fs_sync_file_enter(inode
);
156 /* if fdatasync is triggered, let's do in-place-update */
157 if (get_dirty_pages(inode
) <= SM_I(sbi
)->min_fsync_blocks
)
158 set_inode_flag(fi
, FI_NEED_IPU
);
159 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
160 clear_inode_flag(fi
, FI_NEED_IPU
);
163 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
168 * if there is no written data, don't waste time to write recovery info.
170 if (!is_inode_flag_set(fi
, FI_APPEND_WRITE
) &&
171 !exist_written_data(sbi
, ino
, APPEND_INO
)) {
172 struct page
*i
= find_get_page(NODE_MAPPING(sbi
), ino
);
174 /* But we need to avoid that there are some inode updates */
175 if ((i
&& PageDirty(i
)) || need_inode_block_update(sbi
, ino
)) {
181 if (is_inode_flag_set(fi
, FI_UPDATE_WRITE
) ||
182 exist_written_data(sbi
, ino
, UPDATE_INO
))
187 /* guarantee free sections for fsync */
188 f2fs_balance_fs(sbi
);
191 * Both of fdatasync() and fsync() are able to be recovered from
194 down_read(&fi
->i_sem
);
195 need_cp
= need_do_checkpoint(inode
);
201 /* all the dirty node pages should be flushed for POR */
202 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
204 down_write(&fi
->i_sem
);
205 F2FS_I(inode
)->xattr_ver
= 0;
206 if (file_wrong_pino(inode
) && inode
->i_nlink
== 1 &&
207 get_parent_ino(inode
, &pino
)) {
208 F2FS_I(inode
)->i_pino
= pino
;
209 file_got_pino(inode
);
210 up_write(&fi
->i_sem
);
211 mark_inode_dirty_sync(inode
);
212 ret
= f2fs_write_inode(inode
, NULL
);
216 up_write(&fi
->i_sem
);
220 sync_node_pages(sbi
, ino
, &wbc
);
222 if (need_inode_block_update(sbi
, ino
)) {
223 mark_inode_dirty_sync(inode
);
224 ret
= f2fs_write_inode(inode
, NULL
);
230 ret
= wait_on_node_pages_writeback(sbi
, ino
);
234 /* once recovery info is written, don't need to tack this */
235 remove_dirty_inode(sbi
, ino
, APPEND_INO
);
236 clear_inode_flag(fi
, FI_APPEND_WRITE
);
238 remove_dirty_inode(sbi
, ino
, UPDATE_INO
);
239 clear_inode_flag(fi
, FI_UPDATE_WRITE
);
240 ret
= f2fs_issue_flush(F2FS_I_SB(inode
));
243 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
247 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
248 pgoff_t pgofs
, int whence
)
253 if (whence
!= SEEK_DATA
)
256 /* find first dirty page index */
257 pagevec_init(&pvec
, 0);
258 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &pgofs
,
259 PAGECACHE_TAG_DIRTY
, 1);
260 pgofs
= nr_pages
? pvec
.pages
[0]->index
: LONG_MAX
;
261 pagevec_release(&pvec
);
265 static bool __found_offset(block_t blkaddr
, pgoff_t dirty
, pgoff_t pgofs
,
270 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
271 (blkaddr
!= NEW_ADDR
&& blkaddr
!= NULL_ADDR
))
275 if (blkaddr
== NULL_ADDR
)
282 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
284 struct inode
*inode
= file
->f_mapping
->host
;
285 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
286 struct dnode_of_data dn
;
287 pgoff_t pgofs
, end_offset
, dirty
;
288 loff_t data_ofs
= offset
;
292 mutex_lock(&inode
->i_mutex
);
294 isize
= i_size_read(inode
);
298 /* handle inline data case */
299 if (f2fs_has_inline_data(inode
)) {
300 if (whence
== SEEK_HOLE
)
305 pgofs
= (pgoff_t
)(offset
>> PAGE_CACHE_SHIFT
);
307 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
309 for (; data_ofs
< isize
; data_ofs
= pgofs
<< PAGE_CACHE_SHIFT
) {
310 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
311 err
= get_dnode_of_data(&dn
, pgofs
, LOOKUP_NODE_RA
);
312 if (err
&& err
!= -ENOENT
) {
314 } else if (err
== -ENOENT
) {
315 /* direct node does not exists */
316 if (whence
== SEEK_DATA
) {
317 pgofs
= PGOFS_OF_NEXT_DNODE(pgofs
,
325 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
327 /* find data/hole in dnode block */
328 for (; dn
.ofs_in_node
< end_offset
;
329 dn
.ofs_in_node
++, pgofs
++,
330 data_ofs
= pgofs
<< PAGE_CACHE_SHIFT
) {
332 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
334 if (__found_offset(blkaddr
, dirty
, pgofs
, whence
)) {
342 if (whence
== SEEK_DATA
)
345 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
347 mutex_unlock(&inode
->i_mutex
);
348 return vfs_setpos(file
, data_ofs
, maxbytes
);
350 mutex_unlock(&inode
->i_mutex
);
354 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
356 struct inode
*inode
= file
->f_mapping
->host
;
357 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
363 return generic_file_llseek_size(file
, offset
, whence
,
364 maxbytes
, i_size_read(inode
));
369 return f2fs_seek_block(file
, offset
, whence
);
375 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
378 vma
->vm_ops
= &f2fs_file_vm_ops
;
382 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
384 int nr_free
= 0, ofs
= dn
->ofs_in_node
;
385 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
386 struct f2fs_node
*raw_node
;
389 raw_node
= F2FS_NODE(dn
->node_page
);
390 addr
= blkaddr_in_node(raw_node
) + ofs
;
392 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
393 block_t blkaddr
= le32_to_cpu(*addr
);
394 if (blkaddr
== NULL_ADDR
)
397 update_extent_cache(NULL_ADDR
, dn
);
398 invalidate_blocks(sbi
, blkaddr
);
402 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
403 set_page_dirty(dn
->node_page
);
406 dn
->ofs_in_node
= ofs
;
408 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
409 dn
->ofs_in_node
, nr_free
);
413 void truncate_data_blocks(struct dnode_of_data
*dn
)
415 truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK
);
418 static void truncate_partial_data_page(struct inode
*inode
, u64 from
)
420 unsigned offset
= from
& (PAGE_CACHE_SIZE
- 1);
423 if (f2fs_has_inline_data(inode
))
424 return truncate_inline_data(inode
, from
);
429 page
= find_data_page(inode
, from
>> PAGE_CACHE_SHIFT
, false);
434 if (unlikely(!PageUptodate(page
) ||
435 page
->mapping
!= inode
->i_mapping
))
438 f2fs_wait_on_page_writeback(page
, DATA
);
439 zero_user(page
, offset
, PAGE_CACHE_SIZE
- offset
);
440 set_page_dirty(page
);
443 f2fs_put_page(page
, 1);
446 int truncate_blocks(struct inode
*inode
, u64 from
, bool lock
)
448 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
449 unsigned int blocksize
= inode
->i_sb
->s_blocksize
;
450 struct dnode_of_data dn
;
452 int count
= 0, err
= 0;
454 trace_f2fs_truncate_blocks_enter(inode
, from
);
456 if (f2fs_has_inline_data(inode
))
459 free_from
= (pgoff_t
)
460 ((from
+ blocksize
- 1) >> (sbi
->log_blocksize
));
465 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
466 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE
);
472 trace_f2fs_truncate_blocks_exit(inode
, err
);
476 count
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
478 count
-= dn
.ofs_in_node
;
479 f2fs_bug_on(sbi
, count
< 0);
481 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
482 truncate_data_blocks_range(&dn
, count
);
488 err
= truncate_inode_blocks(inode
, free_from
);
492 /* lastly zero out the first data page */
493 truncate_partial_data_page(inode
, from
);
495 trace_f2fs_truncate_blocks_exit(inode
, err
);
499 void f2fs_truncate(struct inode
*inode
)
501 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
502 S_ISLNK(inode
->i_mode
)))
505 trace_f2fs_truncate(inode
);
507 if (!truncate_blocks(inode
, i_size_read(inode
), true)) {
508 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
509 mark_inode_dirty(inode
);
513 int f2fs_getattr(struct vfsmount
*mnt
,
514 struct dentry
*dentry
, struct kstat
*stat
)
516 struct inode
*inode
= dentry
->d_inode
;
517 generic_fillattr(inode
, stat
);
522 #ifdef CONFIG_F2FS_FS_POSIX_ACL
523 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
525 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
526 unsigned int ia_valid
= attr
->ia_valid
;
528 if (ia_valid
& ATTR_UID
)
529 inode
->i_uid
= attr
->ia_uid
;
530 if (ia_valid
& ATTR_GID
)
531 inode
->i_gid
= attr
->ia_gid
;
532 if (ia_valid
& ATTR_ATIME
)
533 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
534 inode
->i_sb
->s_time_gran
);
535 if (ia_valid
& ATTR_MTIME
)
536 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
537 inode
->i_sb
->s_time_gran
);
538 if (ia_valid
& ATTR_CTIME
)
539 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
540 inode
->i_sb
->s_time_gran
);
541 if (ia_valid
& ATTR_MODE
) {
542 umode_t mode
= attr
->ia_mode
;
544 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
546 set_acl_inode(fi
, mode
);
550 #define __setattr_copy setattr_copy
553 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
555 struct inode
*inode
= dentry
->d_inode
;
556 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
559 err
= inode_change_ok(inode
, attr
);
563 if (attr
->ia_valid
& ATTR_SIZE
) {
564 err
= f2fs_convert_inline_data(inode
, attr
->ia_size
, NULL
);
568 if (attr
->ia_size
!= i_size_read(inode
)) {
569 truncate_setsize(inode
, attr
->ia_size
);
570 f2fs_truncate(inode
);
571 f2fs_balance_fs(F2FS_I_SB(inode
));
574 * giving a chance to truncate blocks past EOF which
575 * are fallocated with FALLOC_FL_KEEP_SIZE.
577 f2fs_truncate(inode
);
581 __setattr_copy(inode
, attr
);
583 if (attr
->ia_valid
& ATTR_MODE
) {
584 err
= posix_acl_chmod(inode
, get_inode_mode(inode
));
585 if (err
|| is_inode_flag_set(fi
, FI_ACL_MODE
)) {
586 inode
->i_mode
= fi
->i_acl_mode
;
587 clear_inode_flag(fi
, FI_ACL_MODE
);
591 mark_inode_dirty(inode
);
595 const struct inode_operations f2fs_file_inode_operations
= {
596 .getattr
= f2fs_getattr
,
597 .setattr
= f2fs_setattr
,
598 .get_acl
= f2fs_get_acl
,
599 .set_acl
= f2fs_set_acl
,
600 #ifdef CONFIG_F2FS_FS_XATTR
601 .setxattr
= generic_setxattr
,
602 .getxattr
= generic_getxattr
,
603 .listxattr
= f2fs_listxattr
,
604 .removexattr
= generic_removexattr
,
606 .fiemap
= f2fs_fiemap
,
609 static void fill_zero(struct inode
*inode
, pgoff_t index
,
610 loff_t start
, loff_t len
)
612 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
618 f2fs_balance_fs(sbi
);
621 page
= get_new_data_page(inode
, NULL
, index
, false);
625 f2fs_wait_on_page_writeback(page
, DATA
);
626 zero_user(page
, start
, len
);
627 set_page_dirty(page
);
628 f2fs_put_page(page
, 1);
632 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
637 for (index
= pg_start
; index
< pg_end
; index
++) {
638 struct dnode_of_data dn
;
640 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
641 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
648 if (dn
.data_blkaddr
!= NULL_ADDR
)
649 truncate_data_blocks_range(&dn
, 1);
655 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
657 pgoff_t pg_start
, pg_end
;
658 loff_t off_start
, off_end
;
661 if (!S_ISREG(inode
->i_mode
))
664 /* skip punching hole beyond i_size */
665 if (offset
>= inode
->i_size
)
668 ret
= f2fs_convert_inline_data(inode
, MAX_INLINE_DATA
+ 1, NULL
);
672 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
673 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
675 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
676 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
678 if (pg_start
== pg_end
) {
679 fill_zero(inode
, pg_start
, off_start
,
680 off_end
- off_start
);
683 fill_zero(inode
, pg_start
++, off_start
,
684 PAGE_CACHE_SIZE
- off_start
);
686 fill_zero(inode
, pg_end
, 0, off_end
);
688 if (pg_start
< pg_end
) {
689 struct address_space
*mapping
= inode
->i_mapping
;
690 loff_t blk_start
, blk_end
;
691 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
693 f2fs_balance_fs(sbi
);
695 blk_start
= pg_start
<< PAGE_CACHE_SHIFT
;
696 blk_end
= pg_end
<< PAGE_CACHE_SHIFT
;
697 truncate_inode_pages_range(mapping
, blk_start
,
701 ret
= truncate_hole(inode
, pg_start
, pg_end
);
709 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
710 loff_t len
, int mode
)
712 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
713 pgoff_t index
, pg_start
, pg_end
;
714 loff_t new_size
= i_size_read(inode
);
715 loff_t off_start
, off_end
;
718 f2fs_balance_fs(sbi
);
720 ret
= inode_newsize_ok(inode
, (len
+ offset
));
724 ret
= f2fs_convert_inline_data(inode
, offset
+ len
, NULL
);
728 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
729 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
731 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
732 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
736 for (index
= pg_start
; index
<= pg_end
; index
++) {
737 struct dnode_of_data dn
;
739 if (index
== pg_end
&& !off_end
)
742 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
743 ret
= f2fs_reserve_block(&dn
, index
);
747 if (pg_start
== pg_end
)
748 new_size
= offset
+ len
;
749 else if (index
== pg_start
&& off_start
)
750 new_size
= (index
+ 1) << PAGE_CACHE_SHIFT
;
751 else if (index
== pg_end
)
752 new_size
= (index
<< PAGE_CACHE_SHIFT
) + off_end
;
754 new_size
+= PAGE_CACHE_SIZE
;
757 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
758 i_size_read(inode
) < new_size
) {
759 i_size_write(inode
, new_size
);
760 mark_inode_dirty(inode
);
761 update_inode_page(inode
);
768 static long f2fs_fallocate(struct file
*file
, int mode
,
769 loff_t offset
, loff_t len
)
771 struct inode
*inode
= file_inode(file
);
774 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
777 mutex_lock(&inode
->i_mutex
);
779 if (mode
& FALLOC_FL_PUNCH_HOLE
)
780 ret
= punch_hole(inode
, offset
, len
);
782 ret
= expand_inode_data(inode
, offset
, len
, mode
);
785 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
786 mark_inode_dirty(inode
);
789 mutex_unlock(&inode
->i_mutex
);
791 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
795 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
796 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
798 static inline __u32
f2fs_mask_flags(umode_t mode
, __u32 flags
)
802 else if (S_ISREG(mode
))
803 return flags
& F2FS_REG_FLMASK
;
805 return flags
& F2FS_OTHER_FLMASK
;
808 static int f2fs_ioc_getflags(struct file
*filp
, unsigned long arg
)
810 struct inode
*inode
= file_inode(filp
);
811 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
812 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
813 return put_user(flags
, (int __user
*)arg
);
816 static int f2fs_ioc_setflags(struct file
*filp
, unsigned long arg
)
818 struct inode
*inode
= file_inode(filp
);
819 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
820 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
821 unsigned int oldflags
;
824 ret
= mnt_want_write_file(filp
);
828 if (!inode_owner_or_capable(inode
)) {
833 if (get_user(flags
, (int __user
*)arg
)) {
838 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
840 mutex_lock(&inode
->i_mutex
);
842 oldflags
= fi
->i_flags
;
844 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
845 if (!capable(CAP_LINUX_IMMUTABLE
)) {
846 mutex_unlock(&inode
->i_mutex
);
852 flags
= flags
& FS_FL_USER_MODIFIABLE
;
853 flags
|= oldflags
& ~FS_FL_USER_MODIFIABLE
;
855 mutex_unlock(&inode
->i_mutex
);
857 f2fs_set_inode_flags(inode
);
858 inode
->i_ctime
= CURRENT_TIME
;
859 mark_inode_dirty(inode
);
861 mnt_drop_write_file(filp
);
865 static int f2fs_ioc_start_atomic_write(struct file
*filp
)
867 struct inode
*inode
= file_inode(filp
);
868 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
870 if (!inode_owner_or_capable(inode
))
873 f2fs_balance_fs(sbi
);
875 set_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
877 return f2fs_convert_inline_data(inode
, MAX_INLINE_DATA
+ 1, NULL
);
880 static int f2fs_ioc_commit_atomic_write(struct file
*filp
)
882 struct inode
*inode
= file_inode(filp
);
885 if (!inode_owner_or_capable(inode
))
888 if (f2fs_is_volatile_file(inode
))
891 ret
= mnt_want_write_file(filp
);
895 if (f2fs_is_atomic_file(inode
))
896 commit_inmem_pages(inode
, false);
898 ret
= f2fs_sync_file(filp
, 0, LONG_MAX
, 0);
899 mnt_drop_write_file(filp
);
903 static int f2fs_ioc_start_volatile_write(struct file
*filp
)
905 struct inode
*inode
= file_inode(filp
);
907 if (!inode_owner_or_capable(inode
))
910 set_inode_flag(F2FS_I(inode
), FI_VOLATILE_FILE
);
914 static int f2fs_ioc_fitrim(struct file
*filp
, unsigned long arg
)
916 struct inode
*inode
= file_inode(filp
);
917 struct super_block
*sb
= inode
->i_sb
;
918 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
919 struct fstrim_range range
;
922 if (!capable(CAP_SYS_ADMIN
))
925 if (!blk_queue_discard(q
))
928 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
932 range
.minlen
= max((unsigned int)range
.minlen
,
933 q
->limits
.discard_granularity
);
934 ret
= f2fs_trim_fs(F2FS_SB(sb
), &range
);
938 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
944 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
947 case F2FS_IOC_GETFLAGS
:
948 return f2fs_ioc_getflags(filp
, arg
);
949 case F2FS_IOC_SETFLAGS
:
950 return f2fs_ioc_setflags(filp
, arg
);
951 case F2FS_IOC_START_ATOMIC_WRITE
:
952 return f2fs_ioc_start_atomic_write(filp
);
953 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
954 return f2fs_ioc_commit_atomic_write(filp
);
955 case F2FS_IOC_START_VOLATILE_WRITE
:
956 return f2fs_ioc_start_volatile_write(filp
);
958 return f2fs_ioc_fitrim(filp
, arg
);
965 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
968 case F2FS_IOC32_GETFLAGS
:
969 cmd
= F2FS_IOC_GETFLAGS
;
971 case F2FS_IOC32_SETFLAGS
:
972 cmd
= F2FS_IOC_SETFLAGS
;
977 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
981 const struct file_operations f2fs_file_operations
= {
982 .llseek
= f2fs_llseek
,
983 .read
= new_sync_read
,
984 .write
= new_sync_write
,
985 .read_iter
= generic_file_read_iter
,
986 .write_iter
= generic_file_write_iter
,
987 .open
= generic_file_open
,
988 .mmap
= f2fs_file_mmap
,
989 .fsync
= f2fs_sync_file
,
990 .fallocate
= f2fs_fallocate
,
991 .unlocked_ioctl
= f2fs_ioctl
,
993 .compat_ioctl
= f2fs_compat_ioctl
,
995 .splice_read
= generic_file_splice_read
,
996 .splice_write
= iter_file_splice_write
,