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>
23 #include <linux/uuid.h>
24 #include <linux/file.h>
33 #include <trace/events/f2fs.h>
35 static int f2fs_vm_page_mkwrite(struct vm_area_struct
*vma
,
38 struct page
*page
= vmf
->page
;
39 struct inode
*inode
= file_inode(vma
->vm_file
);
40 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
41 struct dnode_of_data dn
;
44 sb_start_pagefault(inode
->i_sb
);
46 f2fs_bug_on(sbi
, f2fs_has_inline_data(inode
));
48 /* block allocation */
50 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
51 err
= f2fs_reserve_block(&dn
, page
->index
);
59 f2fs_balance_fs(sbi
, dn
.node_changed
);
61 file_update_time(vma
->vm_file
);
63 if (unlikely(page
->mapping
!= inode
->i_mapping
||
64 page_offset(page
) > i_size_read(inode
) ||
65 !PageUptodate(page
))) {
72 * check to see if the page is mapped already (no holes)
74 if (PageMappedToDisk(page
))
77 /* page is wholly or partially inside EOF */
78 if (((loff_t
)(page
->index
+ 1) << PAGE_SHIFT
) >
81 offset
= i_size_read(inode
) & ~PAGE_MASK
;
82 zero_user_segment(page
, offset
, PAGE_SIZE
);
85 if (!PageUptodate(page
))
86 SetPageUptodate(page
);
88 trace_f2fs_vm_page_mkwrite(page
, DATA
);
91 f2fs_wait_on_page_writeback(page
, DATA
, false);
93 /* wait for GCed encrypted page writeback */
94 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
95 f2fs_wait_on_encrypted_page_writeback(sbi
, dn
.data_blkaddr
);
97 /* if gced page is attached, don't write to cold segment */
98 clear_cold_data(page
);
100 sb_end_pagefault(inode
->i_sb
);
101 f2fs_update_time(sbi
, REQ_TIME
);
102 return block_page_mkwrite_return(err
);
105 static const struct vm_operations_struct f2fs_file_vm_ops
= {
106 .fault
= filemap_fault
,
107 .map_pages
= filemap_map_pages
,
108 .page_mkwrite
= f2fs_vm_page_mkwrite
,
111 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
113 struct dentry
*dentry
;
115 inode
= igrab(inode
);
116 dentry
= d_find_any_alias(inode
);
121 if (update_dent_inode(inode
, inode
, &dentry
->d_name
)) {
126 *pino
= parent_ino(dentry
);
131 static inline bool need_do_checkpoint(struct inode
*inode
)
133 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
134 bool need_cp
= false;
136 if (!S_ISREG(inode
->i_mode
) || inode
->i_nlink
!= 1)
138 else if (is_sbi_flag_set(sbi
, SBI_NEED_CP
))
140 else if (file_wrong_pino(inode
))
142 else if (!space_for_roll_forward(sbi
))
144 else if (!is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
146 else if (F2FS_I(inode
)->xattr_ver
== cur_cp_version(F2FS_CKPT(sbi
)))
148 else if (test_opt(sbi
, FASTBOOT
))
150 else if (sbi
->active_logs
== 2)
156 static bool need_inode_page_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
158 struct page
*i
= find_get_page(NODE_MAPPING(sbi
), ino
);
160 /* But we need to avoid that there are some inode updates */
161 if ((i
&& PageDirty(i
)) || need_inode_block_update(sbi
, ino
))
167 static void try_to_fix_pino(struct inode
*inode
)
169 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
172 down_write(&fi
->i_sem
);
174 if (file_wrong_pino(inode
) && inode
->i_nlink
== 1 &&
175 get_parent_ino(inode
, &pino
)) {
176 f2fs_i_pino_write(inode
, pino
);
177 file_got_pino(inode
);
179 up_write(&fi
->i_sem
);
182 static int f2fs_do_sync_file(struct file
*file
, loff_t start
, loff_t end
,
183 int datasync
, bool atomic
)
185 struct inode
*inode
= file
->f_mapping
->host
;
186 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
187 nid_t ino
= inode
->i_ino
;
189 bool need_cp
= false;
190 struct writeback_control wbc
= {
191 .sync_mode
= WB_SYNC_ALL
,
192 .nr_to_write
= LONG_MAX
,
196 if (unlikely(f2fs_readonly(inode
->i_sb
)))
199 trace_f2fs_sync_file_enter(inode
);
201 if (S_ISDIR(inode
->i_mode
))
204 /* if fdatasync is triggered, let's do in-place-update */
205 if (datasync
|| get_dirty_pages(inode
) <= SM_I(sbi
)->min_fsync_blocks
)
206 set_inode_flag(inode
, FI_NEED_IPU
);
207 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
208 clear_inode_flag(inode
, FI_NEED_IPU
);
211 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
215 /* if the inode is dirty, let's recover all the time */
216 if (!datasync
&& !f2fs_skip_inode_update(inode
)) {
217 f2fs_write_inode(inode
, NULL
);
222 * if there is no written data, don't waste time to write recovery info.
224 if (!is_inode_flag_set(inode
, FI_APPEND_WRITE
) &&
225 !exist_written_data(sbi
, ino
, APPEND_INO
)) {
227 /* it may call write_inode just prior to fsync */
228 if (need_inode_page_update(sbi
, ino
))
231 if (is_inode_flag_set(inode
, FI_UPDATE_WRITE
) ||
232 exist_written_data(sbi
, ino
, UPDATE_INO
))
238 * Both of fdatasync() and fsync() are able to be recovered from
241 down_read(&F2FS_I(inode
)->i_sem
);
242 need_cp
= need_do_checkpoint(inode
);
243 up_read(&F2FS_I(inode
)->i_sem
);
246 /* all the dirty node pages should be flushed for POR */
247 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
250 * We've secured consistency through sync_fs. Following pino
251 * will be used only for fsynced inodes after checkpoint.
253 try_to_fix_pino(inode
);
254 clear_inode_flag(inode
, FI_APPEND_WRITE
);
255 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
259 ret
= fsync_node_pages(sbi
, inode
, &wbc
, atomic
);
263 /* if cp_error was enabled, we should avoid infinite loop */
264 if (unlikely(f2fs_cp_error(sbi
))) {
269 if (need_inode_block_update(sbi
, ino
)) {
270 f2fs_mark_inode_dirty_sync(inode
);
271 f2fs_write_inode(inode
, NULL
);
275 ret
= wait_on_node_pages_writeback(sbi
, ino
);
279 /* once recovery info is written, don't need to tack this */
280 remove_ino_entry(sbi
, ino
, APPEND_INO
);
281 clear_inode_flag(inode
, FI_APPEND_WRITE
);
283 remove_ino_entry(sbi
, ino
, UPDATE_INO
);
284 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
285 ret
= f2fs_issue_flush(sbi
);
286 f2fs_update_time(sbi
, REQ_TIME
);
288 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
289 f2fs_trace_ios(NULL
, 1);
293 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
295 return f2fs_do_sync_file(file
, start
, end
, datasync
, false);
298 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
299 pgoff_t pgofs
, int whence
)
304 if (whence
!= SEEK_DATA
)
307 /* find first dirty page index */
308 pagevec_init(&pvec
, 0);
309 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &pgofs
,
310 PAGECACHE_TAG_DIRTY
, 1);
311 pgofs
= nr_pages
? pvec
.pages
[0]->index
: ULONG_MAX
;
312 pagevec_release(&pvec
);
316 static bool __found_offset(struct f2fs_sb_info
*sbi
, block_t blkaddr
,
317 pgoff_t dirty
, pgoff_t pgofs
, int whence
)
321 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
322 is_valid_data_blkaddr(sbi
, blkaddr
))
326 if (blkaddr
== NULL_ADDR
)
333 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
335 struct inode
*inode
= file
->f_mapping
->host
;
336 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
337 struct dnode_of_data dn
;
338 pgoff_t pgofs
, end_offset
, dirty
;
339 loff_t data_ofs
= offset
;
345 isize
= i_size_read(inode
);
349 /* handle inline data case */
350 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
351 if (whence
== SEEK_HOLE
)
356 pgofs
= (pgoff_t
)(offset
>> PAGE_SHIFT
);
358 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
360 for (; data_ofs
< isize
; data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
361 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
362 err
= get_dnode_of_data(&dn
, pgofs
, LOOKUP_NODE
);
363 if (err
&& err
!= -ENOENT
) {
365 } else if (err
== -ENOENT
) {
366 /* direct node does not exists */
367 if (whence
== SEEK_DATA
) {
368 pgofs
= get_next_page_offset(&dn
, pgofs
);
375 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
377 /* find data/hole in dnode block */
378 for (; dn
.ofs_in_node
< end_offset
;
379 dn
.ofs_in_node
++, pgofs
++,
380 data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
382 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
384 if (__is_valid_data_blkaddr(blkaddr
) &&
385 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode
),
386 blkaddr
, DATA_GENERIC
)) {
391 if (__found_offset(F2FS_I_SB(inode
), blkaddr
, dirty
,
400 if (whence
== SEEK_DATA
)
403 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
406 return vfs_setpos(file
, data_ofs
, maxbytes
);
412 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
414 struct inode
*inode
= file
->f_mapping
->host
;
415 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
421 return generic_file_llseek_size(file
, offset
, whence
,
422 maxbytes
, i_size_read(inode
));
427 return f2fs_seek_block(file
, offset
, whence
);
433 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
435 struct inode
*inode
= file_inode(file
);
438 if (f2fs_encrypted_inode(inode
)) {
439 err
= fscrypt_get_encryption_info(inode
);
442 if (!f2fs_encrypted_inode(inode
))
446 /* we don't need to use inline_data strictly */
447 err
= f2fs_convert_inline_inode(inode
);
452 vma
->vm_ops
= &f2fs_file_vm_ops
;
456 static int f2fs_file_open(struct inode
*inode
, struct file
*filp
)
458 int ret
= generic_file_open(inode
, filp
);
461 if (!ret
&& f2fs_encrypted_inode(inode
)) {
462 ret
= fscrypt_get_encryption_info(inode
);
465 if (!fscrypt_has_encryption_key(inode
))
468 dir
= dget_parent(file_dentry(filp
));
469 if (f2fs_encrypted_inode(d_inode(dir
)) &&
470 !fscrypt_has_permitted_context(d_inode(dir
), inode
)) {
478 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
480 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
481 struct f2fs_node
*raw_node
;
482 int nr_free
= 0, ofs
= dn
->ofs_in_node
, len
= count
;
485 raw_node
= F2FS_NODE(dn
->node_page
);
486 addr
= blkaddr_in_node(raw_node
) + ofs
;
488 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
489 block_t blkaddr
= le32_to_cpu(*addr
);
490 if (blkaddr
== NULL_ADDR
)
493 dn
->data_blkaddr
= NULL_ADDR
;
494 set_data_blkaddr(dn
);
496 if (__is_valid_data_blkaddr(blkaddr
) &&
497 !f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
))
500 invalidate_blocks(sbi
, blkaddr
);
501 if (dn
->ofs_in_node
== 0 && IS_INODE(dn
->node_page
))
502 clear_inode_flag(dn
->inode
, FI_FIRST_BLOCK_WRITTEN
);
509 * once we invalidate valid blkaddr in range [ofs, ofs + count],
510 * we will invalidate all blkaddr in the whole range.
512 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
),
514 f2fs_update_extent_cache_range(dn
, fofs
, 0, len
);
515 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
517 dn
->ofs_in_node
= ofs
;
519 f2fs_update_time(sbi
, REQ_TIME
);
520 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
521 dn
->ofs_in_node
, nr_free
);
525 void truncate_data_blocks(struct dnode_of_data
*dn
)
527 truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK
);
530 static int truncate_partial_data_page(struct inode
*inode
, u64 from
,
533 unsigned offset
= from
& (PAGE_SIZE
- 1);
534 pgoff_t index
= from
>> PAGE_SHIFT
;
535 struct address_space
*mapping
= inode
->i_mapping
;
538 if (!offset
&& !cache_only
)
542 page
= find_lock_page(mapping
, index
);
543 if (page
&& PageUptodate(page
))
545 f2fs_put_page(page
, 1);
549 page
= get_lock_data_page(inode
, index
, true);
553 f2fs_wait_on_page_writeback(page
, DATA
, true);
554 zero_user(page
, offset
, PAGE_SIZE
- offset
);
555 if (!cache_only
|| !f2fs_encrypted_inode(inode
) ||
556 !S_ISREG(inode
->i_mode
))
557 set_page_dirty(page
);
558 f2fs_put_page(page
, 1);
562 int truncate_blocks(struct inode
*inode
, u64 from
, bool lock
)
564 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
565 unsigned int blocksize
= inode
->i_sb
->s_blocksize
;
566 struct dnode_of_data dn
;
568 int count
= 0, err
= 0;
570 bool truncate_page
= false;
572 trace_f2fs_truncate_blocks_enter(inode
, from
);
574 free_from
= (pgoff_t
)F2FS_BYTES_TO_BLK(from
+ blocksize
- 1);
576 if (free_from
>= sbi
->max_file_blocks
)
582 ipage
= get_node_page(sbi
, inode
->i_ino
);
584 err
= PTR_ERR(ipage
);
588 if (f2fs_has_inline_data(inode
)) {
589 if (truncate_inline_inode(ipage
, from
))
590 set_page_dirty(ipage
);
591 f2fs_put_page(ipage
, 1);
592 truncate_page
= true;
596 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
597 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE_RA
);
604 count
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
606 count
-= dn
.ofs_in_node
;
607 f2fs_bug_on(sbi
, count
< 0);
609 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
610 truncate_data_blocks_range(&dn
, count
);
616 err
= truncate_inode_blocks(inode
, free_from
);
621 /* lastly zero out the first data page */
623 err
= truncate_partial_data_page(inode
, from
, truncate_page
);
625 trace_f2fs_truncate_blocks_exit(inode
, err
);
629 int f2fs_truncate(struct inode
*inode
)
633 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
634 S_ISLNK(inode
->i_mode
)))
637 trace_f2fs_truncate(inode
);
639 /* we should check inline_data size */
640 if (!f2fs_may_inline_data(inode
)) {
641 err
= f2fs_convert_inline_inode(inode
);
646 err
= truncate_blocks(inode
, i_size_read(inode
), true);
650 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
651 f2fs_mark_inode_dirty_sync(inode
);
655 int f2fs_getattr(struct vfsmount
*mnt
,
656 struct dentry
*dentry
, struct kstat
*stat
)
658 struct inode
*inode
= d_inode(dentry
);
659 generic_fillattr(inode
, stat
);
664 #ifdef CONFIG_F2FS_FS_POSIX_ACL
665 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
667 unsigned int ia_valid
= attr
->ia_valid
;
669 if (ia_valid
& ATTR_UID
)
670 inode
->i_uid
= attr
->ia_uid
;
671 if (ia_valid
& ATTR_GID
)
672 inode
->i_gid
= attr
->ia_gid
;
673 if (ia_valid
& ATTR_ATIME
)
674 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
675 inode
->i_sb
->s_time_gran
);
676 if (ia_valid
& ATTR_MTIME
)
677 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
678 inode
->i_sb
->s_time_gran
);
679 if (ia_valid
& ATTR_CTIME
)
680 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
681 inode
->i_sb
->s_time_gran
);
682 if (ia_valid
& ATTR_MODE
) {
683 umode_t mode
= attr
->ia_mode
;
685 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
687 set_acl_inode(inode
, mode
);
691 #define __setattr_copy setattr_copy
694 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
696 struct inode
*inode
= d_inode(dentry
);
699 err
= setattr_prepare(dentry
, attr
);
703 if (attr
->ia_valid
& ATTR_SIZE
) {
704 if (f2fs_encrypted_inode(inode
) &&
705 fscrypt_get_encryption_info(inode
))
708 if (attr
->ia_size
<= i_size_read(inode
)) {
709 truncate_setsize(inode
, attr
->ia_size
);
710 err
= f2fs_truncate(inode
);
713 f2fs_balance_fs(F2FS_I_SB(inode
), true);
716 * do not trim all blocks after i_size if target size is
717 * larger than i_size.
719 truncate_setsize(inode
, attr
->ia_size
);
721 /* should convert inline inode here */
722 if (!f2fs_may_inline_data(inode
)) {
723 err
= f2fs_convert_inline_inode(inode
);
727 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
731 __setattr_copy(inode
, attr
);
733 if (attr
->ia_valid
& ATTR_MODE
) {
734 err
= posix_acl_chmod(inode
, get_inode_mode(inode
));
735 if (err
|| is_inode_flag_set(inode
, FI_ACL_MODE
)) {
736 inode
->i_mode
= F2FS_I(inode
)->i_acl_mode
;
737 clear_inode_flag(inode
, FI_ACL_MODE
);
741 f2fs_mark_inode_dirty_sync(inode
);
745 const struct inode_operations f2fs_file_inode_operations
= {
746 .getattr
= f2fs_getattr
,
747 .setattr
= f2fs_setattr
,
748 .get_acl
= f2fs_get_acl
,
749 .set_acl
= f2fs_set_acl
,
750 #ifdef CONFIG_F2FS_FS_XATTR
751 .listxattr
= f2fs_listxattr
,
753 .fiemap
= f2fs_fiemap
,
756 static int fill_zero(struct inode
*inode
, pgoff_t index
,
757 loff_t start
, loff_t len
)
759 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
765 f2fs_balance_fs(sbi
, true);
768 page
= get_new_data_page(inode
, NULL
, index
, false);
772 return PTR_ERR(page
);
774 f2fs_wait_on_page_writeback(page
, DATA
, true);
775 zero_user(page
, start
, len
);
776 set_page_dirty(page
);
777 f2fs_put_page(page
, 1);
781 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
785 while (pg_start
< pg_end
) {
786 struct dnode_of_data dn
;
787 pgoff_t end_offset
, count
;
789 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
790 err
= get_dnode_of_data(&dn
, pg_start
, LOOKUP_NODE
);
792 if (err
== -ENOENT
) {
799 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
800 count
= min(end_offset
- dn
.ofs_in_node
, pg_end
- pg_start
);
802 f2fs_bug_on(F2FS_I_SB(inode
), count
== 0 || count
> end_offset
);
804 truncate_data_blocks_range(&dn
, count
);
812 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
814 pgoff_t pg_start
, pg_end
;
815 loff_t off_start
, off_end
;
818 ret
= f2fs_convert_inline_inode(inode
);
822 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
823 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
825 off_start
= offset
& (PAGE_SIZE
- 1);
826 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
828 if (pg_start
== pg_end
) {
829 ret
= fill_zero(inode
, pg_start
, off_start
,
830 off_end
- off_start
);
835 ret
= fill_zero(inode
, pg_start
++, off_start
,
836 PAGE_SIZE
- off_start
);
841 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
846 if (pg_start
< pg_end
) {
847 struct address_space
*mapping
= inode
->i_mapping
;
848 loff_t blk_start
, blk_end
;
849 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
851 f2fs_balance_fs(sbi
, true);
853 blk_start
= (loff_t
)pg_start
<< PAGE_SHIFT
;
854 blk_end
= (loff_t
)pg_end
<< PAGE_SHIFT
;
855 truncate_inode_pages_range(mapping
, blk_start
,
859 ret
= truncate_hole(inode
, pg_start
, pg_end
);
867 static int __read_out_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
868 int *do_replace
, pgoff_t off
, pgoff_t len
)
870 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
871 struct dnode_of_data dn
;
875 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
876 ret
= get_dnode_of_data(&dn
, off
, LOOKUP_NODE_RA
);
877 if (ret
&& ret
!= -ENOENT
) {
879 } else if (ret
== -ENOENT
) {
880 if (dn
.max_level
== 0)
882 done
= min((pgoff_t
)ADDRS_PER_BLOCK
- dn
.ofs_in_node
, len
);
888 done
= min((pgoff_t
)ADDRS_PER_PAGE(dn
.node_page
, inode
) -
889 dn
.ofs_in_node
, len
);
890 for (i
= 0; i
< done
; i
++, blkaddr
++, do_replace
++, dn
.ofs_in_node
++) {
891 *blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
892 if (!is_checkpointed_data(sbi
, *blkaddr
)) {
894 if (test_opt(sbi
, LFS
)) {
899 /* do not invalidate this block address */
900 f2fs_update_data_blkaddr(&dn
, NULL_ADDR
);
913 static int __roll_back_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
914 int *do_replace
, pgoff_t off
, int len
)
916 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
917 struct dnode_of_data dn
;
920 for (i
= 0; i
< len
; i
++, do_replace
++, blkaddr
++) {
921 if (*do_replace
== 0)
924 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
925 ret
= get_dnode_of_data(&dn
, off
+ i
, LOOKUP_NODE_RA
);
927 dec_valid_block_count(sbi
, inode
, 1);
928 invalidate_blocks(sbi
, *blkaddr
);
930 f2fs_update_data_blkaddr(&dn
, *blkaddr
);
937 static int __clone_blkaddrs(struct inode
*src_inode
, struct inode
*dst_inode
,
938 block_t
*blkaddr
, int *do_replace
,
939 pgoff_t src
, pgoff_t dst
, pgoff_t len
, bool full
)
941 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src_inode
);
946 if (blkaddr
[i
] == NULL_ADDR
&& !full
) {
951 if (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
) {
952 struct dnode_of_data dn
;
957 set_new_dnode(&dn
, dst_inode
, NULL
, NULL
, 0);
958 ret
= get_dnode_of_data(&dn
, dst
+ i
, ALLOC_NODE
);
962 get_node_info(sbi
, dn
.nid
, &ni
);
964 ADDRS_PER_PAGE(dn
.node_page
, dst_inode
) -
965 dn
.ofs_in_node
, len
- i
);
967 dn
.data_blkaddr
= datablock_addr(dn
.node_page
,
969 truncate_data_blocks_range(&dn
, 1);
972 f2fs_i_blocks_write(src_inode
,
974 f2fs_i_blocks_write(dst_inode
,
976 f2fs_replace_block(sbi
, &dn
, dn
.data_blkaddr
,
977 blkaddr
[i
], ni
.version
, true, false);
983 new_size
= (loff_t
)(dst
+ i
) << PAGE_SHIFT
;
984 if (dst_inode
->i_size
< new_size
)
985 f2fs_i_size_write(dst_inode
, new_size
);
986 } while (--ilen
&& (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
));
990 struct page
*psrc
, *pdst
;
992 psrc
= get_lock_data_page(src_inode
, src
+ i
, true);
994 return PTR_ERR(psrc
);
995 pdst
= get_new_data_page(dst_inode
, NULL
, dst
+ i
,
998 f2fs_put_page(psrc
, 1);
999 return PTR_ERR(pdst
);
1001 f2fs_copy_page(psrc
, pdst
);
1002 set_page_dirty(pdst
);
1003 f2fs_put_page(pdst
, 1);
1004 f2fs_put_page(psrc
, 1);
1006 ret
= truncate_hole(src_inode
, src
+ i
, src
+ i
+ 1);
1015 static int __exchange_data_block(struct inode
*src_inode
,
1016 struct inode
*dst_inode
, pgoff_t src
, pgoff_t dst
,
1017 pgoff_t len
, bool full
)
1019 block_t
*src_blkaddr
;
1025 olen
= min((pgoff_t
)4 * ADDRS_PER_BLOCK
, len
);
1027 src_blkaddr
= f2fs_kvzalloc(sizeof(block_t
) * olen
, GFP_KERNEL
);
1031 do_replace
= f2fs_kvzalloc(sizeof(int) * olen
, GFP_KERNEL
);
1033 kvfree(src_blkaddr
);
1037 ret
= __read_out_blkaddrs(src_inode
, src_blkaddr
,
1038 do_replace
, src
, olen
);
1042 ret
= __clone_blkaddrs(src_inode
, dst_inode
, src_blkaddr
,
1043 do_replace
, src
, dst
, olen
, full
);
1051 kvfree(src_blkaddr
);
1057 __roll_back_blkaddrs(src_inode
, src_blkaddr
, do_replace
, src
, len
);
1058 kvfree(src_blkaddr
);
1063 static int f2fs_do_collapse(struct inode
*inode
, pgoff_t start
, pgoff_t end
)
1065 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1066 pgoff_t nrpages
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1069 f2fs_balance_fs(sbi
, true);
1072 f2fs_drop_extent_tree(inode
);
1074 ret
= __exchange_data_block(inode
, inode
, end
, start
, nrpages
- end
, true);
1075 f2fs_unlock_op(sbi
);
1079 static int f2fs_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1081 pgoff_t pg_start
, pg_end
;
1085 if (offset
+ len
>= i_size_read(inode
))
1088 /* collapse range should be aligned to block size of f2fs. */
1089 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1092 ret
= f2fs_convert_inline_inode(inode
);
1096 pg_start
= offset
>> PAGE_SHIFT
;
1097 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1099 /* write out all dirty pages from offset */
1100 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1104 truncate_pagecache(inode
, offset
);
1106 ret
= f2fs_do_collapse(inode
, pg_start
, pg_end
);
1110 /* write out all moved pages, if possible */
1111 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1112 truncate_pagecache(inode
, offset
);
1114 new_size
= i_size_read(inode
) - len
;
1115 truncate_pagecache(inode
, new_size
);
1117 ret
= truncate_blocks(inode
, new_size
, true);
1119 f2fs_i_size_write(inode
, new_size
);
1124 static int f2fs_do_zero_range(struct dnode_of_data
*dn
, pgoff_t start
,
1127 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1128 pgoff_t index
= start
;
1129 unsigned int ofs_in_node
= dn
->ofs_in_node
;
1133 for (; index
< end
; index
++, dn
->ofs_in_node
++) {
1134 if (datablock_addr(dn
->node_page
, dn
->ofs_in_node
) == NULL_ADDR
)
1138 dn
->ofs_in_node
= ofs_in_node
;
1139 ret
= reserve_new_blocks(dn
, count
);
1143 dn
->ofs_in_node
= ofs_in_node
;
1144 for (index
= start
; index
< end
; index
++, dn
->ofs_in_node
++) {
1146 datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
1148 * reserve_new_blocks will not guarantee entire block
1151 if (dn
->data_blkaddr
== NULL_ADDR
) {
1155 if (dn
->data_blkaddr
!= NEW_ADDR
) {
1156 invalidate_blocks(sbi
, dn
->data_blkaddr
);
1157 dn
->data_blkaddr
= NEW_ADDR
;
1158 set_data_blkaddr(dn
);
1162 f2fs_update_extent_cache_range(dn
, start
, 0, index
- start
);
1167 static int f2fs_zero_range(struct inode
*inode
, loff_t offset
, loff_t len
,
1170 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1171 struct address_space
*mapping
= inode
->i_mapping
;
1172 pgoff_t index
, pg_start
, pg_end
;
1173 loff_t new_size
= i_size_read(inode
);
1174 loff_t off_start
, off_end
;
1177 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1181 ret
= f2fs_convert_inline_inode(inode
);
1185 ret
= filemap_write_and_wait_range(mapping
, offset
, offset
+ len
- 1);
1189 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
1191 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
1192 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
1194 off_start
= offset
& (PAGE_SIZE
- 1);
1195 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1197 if (pg_start
== pg_end
) {
1198 ret
= fill_zero(inode
, pg_start
, off_start
,
1199 off_end
- off_start
);
1203 if (offset
+ len
> new_size
)
1204 new_size
= offset
+ len
;
1205 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1208 ret
= fill_zero(inode
, pg_start
++, off_start
,
1209 PAGE_SIZE
- off_start
);
1213 new_size
= max_t(loff_t
, new_size
,
1214 (loff_t
)pg_start
<< PAGE_SHIFT
);
1217 for (index
= pg_start
; index
< pg_end
;) {
1218 struct dnode_of_data dn
;
1219 unsigned int end_offset
;
1224 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1225 ret
= get_dnode_of_data(&dn
, index
, ALLOC_NODE
);
1227 f2fs_unlock_op(sbi
);
1231 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1232 end
= min(pg_end
, end_offset
- dn
.ofs_in_node
+ index
);
1234 ret
= f2fs_do_zero_range(&dn
, index
, end
);
1235 f2fs_put_dnode(&dn
);
1236 f2fs_unlock_op(sbi
);
1241 new_size
= max_t(loff_t
, new_size
,
1242 (loff_t
)index
<< PAGE_SHIFT
);
1246 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
1250 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1255 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && i_size_read(inode
) < new_size
)
1256 f2fs_i_size_write(inode
, new_size
);
1261 static int f2fs_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1263 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1264 pgoff_t nr
, pg_start
, pg_end
, delta
, idx
;
1268 new_size
= i_size_read(inode
) + len
;
1269 if (new_size
> inode
->i_sb
->s_maxbytes
)
1272 if (offset
>= i_size_read(inode
))
1275 /* insert range should be aligned to block size of f2fs. */
1276 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1279 ret
= f2fs_convert_inline_inode(inode
);
1283 f2fs_balance_fs(sbi
, true);
1285 ret
= truncate_blocks(inode
, i_size_read(inode
), true);
1289 /* write out all dirty pages from offset */
1290 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1294 truncate_pagecache(inode
, offset
);
1296 pg_start
= offset
>> PAGE_SHIFT
;
1297 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1298 delta
= pg_end
- pg_start
;
1299 idx
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1301 while (!ret
&& idx
> pg_start
) {
1302 nr
= idx
- pg_start
;
1308 f2fs_drop_extent_tree(inode
);
1310 ret
= __exchange_data_block(inode
, inode
, idx
,
1311 idx
+ delta
, nr
, false);
1312 f2fs_unlock_op(sbi
);
1315 /* write out all moved pages, if possible */
1316 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1317 truncate_pagecache(inode
, offset
);
1320 f2fs_i_size_write(inode
, new_size
);
1324 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
1325 loff_t len
, int mode
)
1327 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1328 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
};
1330 loff_t new_size
= i_size_read(inode
);
1334 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1338 ret
= f2fs_convert_inline_inode(inode
);
1342 f2fs_balance_fs(sbi
, true);
1344 pg_end
= ((unsigned long long)offset
+ len
) >> PAGE_SHIFT
;
1345 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1347 map
.m_lblk
= ((unsigned long long)offset
) >> PAGE_SHIFT
;
1348 map
.m_len
= pg_end
- map
.m_lblk
;
1352 ret
= f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_AIO
);
1359 last_off
= map
.m_lblk
+ map
.m_len
- 1;
1361 /* update new size to the failed position */
1362 new_size
= (last_off
== pg_end
) ? offset
+ len
:
1363 (loff_t
)(last_off
+ 1) << PAGE_SHIFT
;
1365 new_size
= ((loff_t
)pg_end
<< PAGE_SHIFT
) + off_end
;
1368 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && i_size_read(inode
) < new_size
)
1369 f2fs_i_size_write(inode
, new_size
);
1374 static long f2fs_fallocate(struct file
*file
, int mode
,
1375 loff_t offset
, loff_t len
)
1377 struct inode
*inode
= file_inode(file
);
1380 /* f2fs only support ->fallocate for regular file */
1381 if (!S_ISREG(inode
->i_mode
))
1384 if (f2fs_encrypted_inode(inode
) &&
1385 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
)))
1388 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
1389 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
1390 FALLOC_FL_INSERT_RANGE
))
1395 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
1396 if (offset
>= inode
->i_size
)
1399 ret
= punch_hole(inode
, offset
, len
);
1400 } else if (mode
& FALLOC_FL_COLLAPSE_RANGE
) {
1401 ret
= f2fs_collapse_range(inode
, offset
, len
);
1402 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
1403 ret
= f2fs_zero_range(inode
, offset
, len
, mode
);
1404 } else if (mode
& FALLOC_FL_INSERT_RANGE
) {
1405 ret
= f2fs_insert_range(inode
, offset
, len
);
1407 ret
= expand_inode_data(inode
, offset
, len
, mode
);
1411 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
1412 f2fs_mark_inode_dirty_sync(inode
);
1413 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1417 inode_unlock(inode
);
1419 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
1423 static int f2fs_release_file(struct inode
*inode
, struct file
*filp
)
1426 * f2fs_relase_file is called at every close calls. So we should
1427 * not drop any inmemory pages by close called by other process.
1429 if (!(filp
->f_mode
& FMODE_WRITE
) ||
1430 atomic_read(&inode
->i_writecount
) != 1)
1433 /* some remained atomic pages should discarded */
1434 if (f2fs_is_atomic_file(inode
))
1435 drop_inmem_pages(inode
);
1436 if (f2fs_is_volatile_file(inode
)) {
1437 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1438 set_inode_flag(inode
, FI_DROP_CACHE
);
1439 filemap_fdatawrite(inode
->i_mapping
);
1440 clear_inode_flag(inode
, FI_DROP_CACHE
);
1445 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1446 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1448 static inline __u32
f2fs_mask_flags(umode_t mode
, __u32 flags
)
1452 else if (S_ISREG(mode
))
1453 return flags
& F2FS_REG_FLMASK
;
1455 return flags
& F2FS_OTHER_FLMASK
;
1458 static int f2fs_ioc_getflags(struct file
*filp
, unsigned long arg
)
1460 struct inode
*inode
= file_inode(filp
);
1461 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1462 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
1463 return put_user(flags
, (int __user
*)arg
);
1466 static int f2fs_ioc_setflags(struct file
*filp
, unsigned long arg
)
1468 struct inode
*inode
= file_inode(filp
);
1469 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1471 unsigned int oldflags
;
1474 if (!inode_owner_or_capable(inode
))
1477 if (get_user(flags
, (int __user
*)arg
))
1480 ret
= mnt_want_write_file(filp
);
1484 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
1488 oldflags
= fi
->i_flags
;
1490 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
1491 if (!capable(CAP_LINUX_IMMUTABLE
)) {
1492 inode_unlock(inode
);
1498 flags
= flags
& FS_FL_USER_MODIFIABLE
;
1499 flags
|= oldflags
& ~FS_FL_USER_MODIFIABLE
;
1500 fi
->i_flags
= flags
;
1501 inode_unlock(inode
);
1503 inode
->i_ctime
= current_time(inode
);
1504 f2fs_set_inode_flags(inode
);
1506 mnt_drop_write_file(filp
);
1510 static int f2fs_ioc_getversion(struct file
*filp
, unsigned long arg
)
1512 struct inode
*inode
= file_inode(filp
);
1514 return put_user(inode
->i_generation
, (int __user
*)arg
);
1517 static int f2fs_ioc_start_atomic_write(struct file
*filp
)
1519 struct inode
*inode
= file_inode(filp
);
1522 if (!inode_owner_or_capable(inode
))
1525 ret
= mnt_want_write_file(filp
);
1531 down_write(&F2FS_I(inode
)->dio_rwsem
[WRITE
]);
1533 if (f2fs_is_atomic_file(inode
))
1536 ret
= f2fs_convert_inline_inode(inode
);
1540 set_inode_flag(inode
, FI_ATOMIC_FILE
);
1541 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1543 if (!get_dirty_pages(inode
))
1546 f2fs_msg(F2FS_I_SB(inode
)->sb
, KERN_WARNING
,
1547 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1548 inode
->i_ino
, get_dirty_pages(inode
));
1549 ret
= filemap_write_and_wait_range(inode
->i_mapping
, 0, LLONG_MAX
);
1551 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
1553 up_write(&F2FS_I(inode
)->dio_rwsem
[WRITE
]);
1554 inode_unlock(inode
);
1555 mnt_drop_write_file(filp
);
1559 static int f2fs_ioc_commit_atomic_write(struct file
*filp
)
1561 struct inode
*inode
= file_inode(filp
);
1564 if (!inode_owner_or_capable(inode
))
1567 ret
= mnt_want_write_file(filp
);
1573 if (f2fs_is_volatile_file(inode
))
1576 if (f2fs_is_atomic_file(inode
)) {
1577 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
1578 ret
= commit_inmem_pages(inode
);
1580 set_inode_flag(inode
, FI_ATOMIC_FILE
);
1585 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1587 inode_unlock(inode
);
1588 mnt_drop_write_file(filp
);
1592 static int f2fs_ioc_start_volatile_write(struct file
*filp
)
1594 struct inode
*inode
= file_inode(filp
);
1597 if (!inode_owner_or_capable(inode
))
1600 ret
= mnt_want_write_file(filp
);
1606 if (f2fs_is_volatile_file(inode
))
1609 ret
= f2fs_convert_inline_inode(inode
);
1613 set_inode_flag(inode
, FI_VOLATILE_FILE
);
1614 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1616 inode_unlock(inode
);
1617 mnt_drop_write_file(filp
);
1621 static int f2fs_ioc_release_volatile_write(struct file
*filp
)
1623 struct inode
*inode
= file_inode(filp
);
1626 if (!inode_owner_or_capable(inode
))
1629 ret
= mnt_want_write_file(filp
);
1635 if (!f2fs_is_volatile_file(inode
))
1638 if (!f2fs_is_first_block_written(inode
)) {
1639 ret
= truncate_partial_data_page(inode
, 0, true);
1643 ret
= punch_hole(inode
, 0, F2FS_BLKSIZE
);
1645 inode_unlock(inode
);
1646 mnt_drop_write_file(filp
);
1650 static int f2fs_ioc_abort_volatile_write(struct file
*filp
)
1652 struct inode
*inode
= file_inode(filp
);
1655 if (!inode_owner_or_capable(inode
))
1658 ret
= mnt_want_write_file(filp
);
1664 if (f2fs_is_atomic_file(inode
))
1665 drop_inmem_pages(inode
);
1666 if (f2fs_is_volatile_file(inode
)) {
1667 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1668 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1671 inode_unlock(inode
);
1673 mnt_drop_write_file(filp
);
1674 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1678 static int f2fs_ioc_shutdown(struct file
*filp
, unsigned long arg
)
1680 struct inode
*inode
= file_inode(filp
);
1681 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1682 struct super_block
*sb
= sbi
->sb
;
1686 if (!capable(CAP_SYS_ADMIN
))
1689 if (get_user(in
, (__u32 __user
*)arg
))
1692 if (in
!= F2FS_GOING_DOWN_FULLSYNC
) {
1693 ret
= mnt_want_write_file(filp
);
1699 case F2FS_GOING_DOWN_FULLSYNC
:
1700 sb
= freeze_bdev(sb
->s_bdev
);
1701 if (sb
&& !IS_ERR(sb
)) {
1702 f2fs_stop_checkpoint(sbi
, false);
1703 thaw_bdev(sb
->s_bdev
, sb
);
1706 case F2FS_GOING_DOWN_METASYNC
:
1707 /* do checkpoint only */
1708 f2fs_sync_fs(sb
, 1);
1709 f2fs_stop_checkpoint(sbi
, false);
1711 case F2FS_GOING_DOWN_NOSYNC
:
1712 f2fs_stop_checkpoint(sbi
, false);
1714 case F2FS_GOING_DOWN_METAFLUSH
:
1715 sync_meta_pages(sbi
, META
, LONG_MAX
);
1716 f2fs_stop_checkpoint(sbi
, false);
1722 f2fs_update_time(sbi
, REQ_TIME
);
1724 if (in
!= F2FS_GOING_DOWN_FULLSYNC
)
1725 mnt_drop_write_file(filp
);
1729 static int f2fs_ioc_fitrim(struct file
*filp
, unsigned long arg
)
1731 struct inode
*inode
= file_inode(filp
);
1732 struct super_block
*sb
= inode
->i_sb
;
1733 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
1734 struct fstrim_range range
;
1737 if (!capable(CAP_SYS_ADMIN
))
1740 if (!blk_queue_discard(q
))
1743 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
1747 ret
= mnt_want_write_file(filp
);
1751 range
.minlen
= max((unsigned int)range
.minlen
,
1752 q
->limits
.discard_granularity
);
1753 ret
= f2fs_trim_fs(F2FS_SB(sb
), &range
);
1754 mnt_drop_write_file(filp
);
1758 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
1761 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1765 static bool uuid_is_nonzero(__u8 u
[16])
1769 for (i
= 0; i
< 16; i
++)
1775 static int f2fs_ioc_set_encryption_policy(struct file
*filp
, unsigned long arg
)
1777 struct fscrypt_policy policy
;
1778 struct inode
*inode
= file_inode(filp
);
1780 if (copy_from_user(&policy
, (struct fscrypt_policy __user
*)arg
,
1784 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1786 return fscrypt_process_policy(filp
, &policy
);
1789 static int f2fs_ioc_get_encryption_policy(struct file
*filp
, unsigned long arg
)
1791 struct fscrypt_policy policy
;
1792 struct inode
*inode
= file_inode(filp
);
1795 err
= fscrypt_get_policy(inode
, &policy
);
1799 if (copy_to_user((struct fscrypt_policy __user
*)arg
, &policy
, sizeof(policy
)))
1804 static int f2fs_ioc_get_encryption_pwsalt(struct file
*filp
, unsigned long arg
)
1806 struct inode
*inode
= file_inode(filp
);
1807 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1810 if (!f2fs_sb_has_crypto(inode
->i_sb
))
1813 if (uuid_is_nonzero(sbi
->raw_super
->encrypt_pw_salt
))
1816 err
= mnt_want_write_file(filp
);
1820 /* update superblock with uuid */
1821 generate_random_uuid(sbi
->raw_super
->encrypt_pw_salt
);
1823 err
= f2fs_commit_super(sbi
, false);
1826 memset(sbi
->raw_super
->encrypt_pw_salt
, 0, 16);
1827 mnt_drop_write_file(filp
);
1830 mnt_drop_write_file(filp
);
1832 if (copy_to_user((__u8 __user
*)arg
, sbi
->raw_super
->encrypt_pw_salt
,
1838 static int f2fs_ioc_gc(struct file
*filp
, unsigned long arg
)
1840 struct inode
*inode
= file_inode(filp
);
1841 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1845 if (!capable(CAP_SYS_ADMIN
))
1848 if (get_user(sync
, (__u32 __user
*)arg
))
1851 if (f2fs_readonly(sbi
->sb
))
1854 ret
= mnt_want_write_file(filp
);
1859 if (!mutex_trylock(&sbi
->gc_mutex
)) {
1864 mutex_lock(&sbi
->gc_mutex
);
1867 ret
= f2fs_gc(sbi
, sync
);
1869 mnt_drop_write_file(filp
);
1873 static int f2fs_ioc_write_checkpoint(struct file
*filp
, unsigned long arg
)
1875 struct inode
*inode
= file_inode(filp
);
1876 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1879 if (!capable(CAP_SYS_ADMIN
))
1882 if (f2fs_readonly(sbi
->sb
))
1885 ret
= mnt_want_write_file(filp
);
1889 ret
= f2fs_sync_fs(sbi
->sb
, 1);
1891 mnt_drop_write_file(filp
);
1895 static int f2fs_defragment_range(struct f2fs_sb_info
*sbi
,
1897 struct f2fs_defragment
*range
)
1899 struct inode
*inode
= file_inode(filp
);
1900 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
};
1901 struct extent_info ei
;
1902 pgoff_t pg_start
, pg_end
;
1903 unsigned int blk_per_seg
= sbi
->blocks_per_seg
;
1904 unsigned int total
= 0, sec_num
;
1905 unsigned int pages_per_sec
= sbi
->segs_per_sec
* blk_per_seg
;
1906 block_t blk_end
= 0;
1907 bool fragmented
= false;
1910 /* if in-place-update policy is enabled, don't waste time here */
1911 if (need_inplace_update(inode
))
1914 pg_start
= range
->start
>> PAGE_SHIFT
;
1915 pg_end
= (range
->start
+ range
->len
) >> PAGE_SHIFT
;
1917 f2fs_balance_fs(sbi
, true);
1921 /* writeback all dirty pages in the range */
1922 err
= filemap_write_and_wait_range(inode
->i_mapping
, range
->start
,
1923 range
->start
+ range
->len
- 1);
1928 * lookup mapping info in extent cache, skip defragmenting if physical
1929 * block addresses are continuous.
1931 if (f2fs_lookup_extent_cache(inode
, pg_start
, &ei
)) {
1932 if (ei
.fofs
+ ei
.len
>= pg_end
)
1936 map
.m_lblk
= pg_start
;
1939 * lookup mapping info in dnode page cache, skip defragmenting if all
1940 * physical block addresses are continuous even if there are hole(s)
1941 * in logical blocks.
1943 while (map
.m_lblk
< pg_end
) {
1944 map
.m_len
= pg_end
- map
.m_lblk
;
1945 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_READ
);
1949 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
1954 if (blk_end
&& blk_end
!= map
.m_pblk
) {
1958 blk_end
= map
.m_pblk
+ map
.m_len
;
1960 map
.m_lblk
+= map
.m_len
;
1966 map
.m_lblk
= pg_start
;
1967 map
.m_len
= pg_end
- pg_start
;
1969 sec_num
= (map
.m_len
+ pages_per_sec
- 1) / pages_per_sec
;
1972 * make sure there are enough free section for LFS allocation, this can
1973 * avoid defragment running in SSR mode when free section are allocated
1976 if (has_not_enough_free_secs(sbi
, 0, sec_num
)) {
1981 while (map
.m_lblk
< pg_end
) {
1986 map
.m_len
= pg_end
- map
.m_lblk
;
1987 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_READ
);
1991 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
1996 set_inode_flag(inode
, FI_DO_DEFRAG
);
1999 while (idx
< map
.m_lblk
+ map
.m_len
&& cnt
< blk_per_seg
) {
2002 page
= get_lock_data_page(inode
, idx
, true);
2004 err
= PTR_ERR(page
);
2008 set_page_dirty(page
);
2009 f2fs_put_page(page
, 1);
2018 if (idx
< pg_end
&& cnt
< blk_per_seg
)
2021 clear_inode_flag(inode
, FI_DO_DEFRAG
);
2023 err
= filemap_fdatawrite(inode
->i_mapping
);
2028 clear_inode_flag(inode
, FI_DO_DEFRAG
);
2030 inode_unlock(inode
);
2032 range
->len
= (u64
)total
<< PAGE_SHIFT
;
2036 static int f2fs_ioc_defragment(struct file
*filp
, unsigned long arg
)
2038 struct inode
*inode
= file_inode(filp
);
2039 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2040 struct f2fs_defragment range
;
2043 if (!capable(CAP_SYS_ADMIN
))
2046 if (!S_ISREG(inode
->i_mode
))
2049 err
= mnt_want_write_file(filp
);
2053 if (f2fs_readonly(sbi
->sb
)) {
2058 if (copy_from_user(&range
, (struct f2fs_defragment __user
*)arg
,
2064 /* verify alignment of offset & size */
2065 if (range
.start
& (F2FS_BLKSIZE
- 1) ||
2066 range
.len
& (F2FS_BLKSIZE
- 1)) {
2071 err
= f2fs_defragment_range(sbi
, filp
, &range
);
2072 f2fs_update_time(sbi
, REQ_TIME
);
2076 if (copy_to_user((struct f2fs_defragment __user
*)arg
, &range
,
2080 mnt_drop_write_file(filp
);
2084 static int f2fs_move_file_range(struct file
*file_in
, loff_t pos_in
,
2085 struct file
*file_out
, loff_t pos_out
, size_t len
)
2087 struct inode
*src
= file_inode(file_in
);
2088 struct inode
*dst
= file_inode(file_out
);
2089 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src
);
2090 size_t olen
= len
, dst_max_i_size
= 0;
2094 if (file_in
->f_path
.mnt
!= file_out
->f_path
.mnt
||
2095 src
->i_sb
!= dst
->i_sb
)
2098 if (unlikely(f2fs_readonly(src
->i_sb
)))
2101 if (!S_ISREG(src
->i_mode
) || !S_ISREG(dst
->i_mode
))
2104 if (f2fs_encrypted_inode(src
) || f2fs_encrypted_inode(dst
))
2108 if (pos_in
== pos_out
)
2110 if (pos_out
> pos_in
&& pos_out
< pos_in
+ len
)
2116 if (!inode_trylock(dst
)) {
2123 if (pos_in
+ len
> src
->i_size
|| pos_in
+ len
< pos_in
)
2126 olen
= len
= src
->i_size
- pos_in
;
2127 if (pos_in
+ len
== src
->i_size
)
2128 len
= ALIGN(src
->i_size
, F2FS_BLKSIZE
) - pos_in
;
2134 dst_osize
= dst
->i_size
;
2135 if (pos_out
+ olen
> dst
->i_size
)
2136 dst_max_i_size
= pos_out
+ olen
;
2138 /* verify the end result is block aligned */
2139 if (!IS_ALIGNED(pos_in
, F2FS_BLKSIZE
) ||
2140 !IS_ALIGNED(pos_in
+ len
, F2FS_BLKSIZE
) ||
2141 !IS_ALIGNED(pos_out
, F2FS_BLKSIZE
))
2144 ret
= f2fs_convert_inline_inode(src
);
2148 ret
= f2fs_convert_inline_inode(dst
);
2152 /* write out all dirty pages from offset */
2153 ret
= filemap_write_and_wait_range(src
->i_mapping
,
2154 pos_in
, pos_in
+ len
);
2158 ret
= filemap_write_and_wait_range(dst
->i_mapping
,
2159 pos_out
, pos_out
+ len
);
2163 f2fs_balance_fs(sbi
, true);
2165 ret
= __exchange_data_block(src
, dst
, pos_in
>> F2FS_BLKSIZE_BITS
,
2166 pos_out
>> F2FS_BLKSIZE_BITS
,
2167 len
>> F2FS_BLKSIZE_BITS
, false);
2171 f2fs_i_size_write(dst
, dst_max_i_size
);
2172 else if (dst_osize
!= dst
->i_size
)
2173 f2fs_i_size_write(dst
, dst_osize
);
2175 f2fs_unlock_op(sbi
);
2184 static int f2fs_ioc_move_range(struct file
*filp
, unsigned long arg
)
2186 struct f2fs_move_range range
;
2190 if (!(filp
->f_mode
& FMODE_READ
) ||
2191 !(filp
->f_mode
& FMODE_WRITE
))
2194 if (copy_from_user(&range
, (struct f2fs_move_range __user
*)arg
,
2198 dst
= fdget(range
.dst_fd
);
2202 if (!(dst
.file
->f_mode
& FMODE_WRITE
)) {
2207 err
= mnt_want_write_file(filp
);
2211 err
= f2fs_move_file_range(filp
, range
.pos_in
, dst
.file
,
2212 range
.pos_out
, range
.len
);
2214 mnt_drop_write_file(filp
);
2216 if (copy_to_user((struct f2fs_move_range __user
*)arg
,
2217 &range
, sizeof(range
)))
2224 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2227 case F2FS_IOC_GETFLAGS
:
2228 return f2fs_ioc_getflags(filp
, arg
);
2229 case F2FS_IOC_SETFLAGS
:
2230 return f2fs_ioc_setflags(filp
, arg
);
2231 case F2FS_IOC_GETVERSION
:
2232 return f2fs_ioc_getversion(filp
, arg
);
2233 case F2FS_IOC_START_ATOMIC_WRITE
:
2234 return f2fs_ioc_start_atomic_write(filp
);
2235 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
2236 return f2fs_ioc_commit_atomic_write(filp
);
2237 case F2FS_IOC_START_VOLATILE_WRITE
:
2238 return f2fs_ioc_start_volatile_write(filp
);
2239 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
2240 return f2fs_ioc_release_volatile_write(filp
);
2241 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
2242 return f2fs_ioc_abort_volatile_write(filp
);
2243 case F2FS_IOC_SHUTDOWN
:
2244 return f2fs_ioc_shutdown(filp
, arg
);
2246 return f2fs_ioc_fitrim(filp
, arg
);
2247 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
2248 return f2fs_ioc_set_encryption_policy(filp
, arg
);
2249 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
2250 return f2fs_ioc_get_encryption_policy(filp
, arg
);
2251 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
2252 return f2fs_ioc_get_encryption_pwsalt(filp
, arg
);
2253 case F2FS_IOC_GARBAGE_COLLECT
:
2254 return f2fs_ioc_gc(filp
, arg
);
2255 case F2FS_IOC_WRITE_CHECKPOINT
:
2256 return f2fs_ioc_write_checkpoint(filp
, arg
);
2257 case F2FS_IOC_DEFRAGMENT
:
2258 return f2fs_ioc_defragment(filp
, arg
);
2259 case F2FS_IOC_MOVE_RANGE
:
2260 return f2fs_ioc_move_range(filp
, arg
);
2266 static ssize_t
f2fs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
2268 struct file
*file
= iocb
->ki_filp
;
2269 struct inode
*inode
= file_inode(file
);
2270 struct blk_plug plug
;
2273 if (f2fs_encrypted_inode(inode
) &&
2274 !fscrypt_has_encryption_key(inode
) &&
2275 fscrypt_get_encryption_info(inode
))
2279 ret
= generic_write_checks(iocb
, from
);
2281 ret
= f2fs_preallocate_blocks(iocb
, from
);
2283 blk_start_plug(&plug
);
2284 ret
= __generic_file_write_iter(iocb
, from
);
2285 blk_finish_plug(&plug
);
2288 inode_unlock(inode
);
2291 ret
= generic_write_sync(iocb
, ret
);
2295 #ifdef CONFIG_COMPAT
2296 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2299 case F2FS_IOC32_GETFLAGS
:
2300 cmd
= F2FS_IOC_GETFLAGS
;
2302 case F2FS_IOC32_SETFLAGS
:
2303 cmd
= F2FS_IOC_SETFLAGS
;
2305 case F2FS_IOC32_GETVERSION
:
2306 cmd
= F2FS_IOC_GETVERSION
;
2308 case F2FS_IOC_START_ATOMIC_WRITE
:
2309 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
2310 case F2FS_IOC_START_VOLATILE_WRITE
:
2311 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
2312 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
2313 case F2FS_IOC_SHUTDOWN
:
2314 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
2315 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
2316 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
2317 case F2FS_IOC_GARBAGE_COLLECT
:
2318 case F2FS_IOC_WRITE_CHECKPOINT
:
2319 case F2FS_IOC_DEFRAGMENT
:
2321 case F2FS_IOC_MOVE_RANGE
:
2324 return -ENOIOCTLCMD
;
2326 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
2330 const struct file_operations f2fs_file_operations
= {
2331 .llseek
= f2fs_llseek
,
2332 .read_iter
= generic_file_read_iter
,
2333 .write_iter
= f2fs_file_write_iter
,
2334 .open
= f2fs_file_open
,
2335 .release
= f2fs_release_file
,
2336 .mmap
= f2fs_file_mmap
,
2337 .fsync
= f2fs_sync_file
,
2338 .fallocate
= f2fs_fallocate
,
2339 .unlocked_ioctl
= f2fs_ioctl
,
2340 #ifdef CONFIG_COMPAT
2341 .compat_ioctl
= f2fs_compat_ioctl
,
2343 .splice_read
= generic_file_splice_read
,
2344 .splice_write
= iter_file_splice_write
,