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
);
98 sb_end_pagefault(inode
->i_sb
);
99 f2fs_update_time(sbi
, REQ_TIME
);
100 return block_page_mkwrite_return(err
);
103 static const struct vm_operations_struct f2fs_file_vm_ops
= {
104 .fault
= filemap_fault
,
105 .map_pages
= filemap_map_pages
,
106 .page_mkwrite
= f2fs_vm_page_mkwrite
,
109 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
111 struct dentry
*dentry
;
113 inode
= igrab(inode
);
114 dentry
= d_find_any_alias(inode
);
119 if (update_dent_inode(inode
, inode
, &dentry
->d_name
)) {
124 *pino
= parent_ino(dentry
);
129 static inline bool need_do_checkpoint(struct inode
*inode
)
131 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
132 bool need_cp
= false;
134 if (!S_ISREG(inode
->i_mode
) || inode
->i_nlink
!= 1)
136 else if (is_sbi_flag_set(sbi
, SBI_NEED_CP
))
138 else if (file_wrong_pino(inode
))
140 else if (!space_for_roll_forward(sbi
))
142 else if (!is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
144 else if (F2FS_I(inode
)->xattr_ver
== cur_cp_version(F2FS_CKPT(sbi
)))
146 else if (test_opt(sbi
, FASTBOOT
))
148 else if (sbi
->active_logs
== 2)
154 static bool need_inode_page_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
156 struct page
*i
= find_get_page(NODE_MAPPING(sbi
), ino
);
158 /* But we need to avoid that there are some inode updates */
159 if ((i
&& PageDirty(i
)) || need_inode_block_update(sbi
, ino
))
165 static void try_to_fix_pino(struct inode
*inode
)
167 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
170 down_write(&fi
->i_sem
);
172 if (file_wrong_pino(inode
) && inode
->i_nlink
== 1 &&
173 get_parent_ino(inode
, &pino
)) {
174 f2fs_i_pino_write(inode
, pino
);
175 file_got_pino(inode
);
177 up_write(&fi
->i_sem
);
180 static int f2fs_do_sync_file(struct file
*file
, loff_t start
, loff_t end
,
181 int datasync
, bool atomic
)
183 struct inode
*inode
= file
->f_mapping
->host
;
184 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
185 nid_t ino
= inode
->i_ino
;
187 bool need_cp
= false;
188 struct writeback_control wbc
= {
189 .sync_mode
= WB_SYNC_ALL
,
190 .nr_to_write
= LONG_MAX
,
194 if (unlikely(f2fs_readonly(inode
->i_sb
)))
197 trace_f2fs_sync_file_enter(inode
);
199 /* if fdatasync is triggered, let's do in-place-update */
200 if (datasync
|| get_dirty_pages(inode
) <= SM_I(sbi
)->min_fsync_blocks
)
201 set_inode_flag(inode
, FI_NEED_IPU
);
202 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
203 clear_inode_flag(inode
, FI_NEED_IPU
);
206 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
210 /* if the inode is dirty, let's recover all the time */
211 if (!f2fs_skip_inode_update(inode
, datasync
)) {
212 f2fs_write_inode(inode
, NULL
);
217 * if there is no written data, don't waste time to write recovery info.
219 if (!is_inode_flag_set(inode
, FI_APPEND_WRITE
) &&
220 !exist_written_data(sbi
, ino
, APPEND_INO
)) {
222 /* it may call write_inode just prior to fsync */
223 if (need_inode_page_update(sbi
, ino
))
226 if (is_inode_flag_set(inode
, FI_UPDATE_WRITE
) ||
227 exist_written_data(sbi
, ino
, UPDATE_INO
))
233 * Both of fdatasync() and fsync() are able to be recovered from
236 down_read(&F2FS_I(inode
)->i_sem
);
237 need_cp
= need_do_checkpoint(inode
);
238 up_read(&F2FS_I(inode
)->i_sem
);
241 /* all the dirty node pages should be flushed for POR */
242 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
245 * We've secured consistency through sync_fs. Following pino
246 * will be used only for fsynced inodes after checkpoint.
248 try_to_fix_pino(inode
);
249 clear_inode_flag(inode
, FI_APPEND_WRITE
);
250 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
254 ret
= fsync_node_pages(sbi
, inode
, &wbc
, atomic
);
258 /* if cp_error was enabled, we should avoid infinite loop */
259 if (unlikely(f2fs_cp_error(sbi
))) {
264 if (need_inode_block_update(sbi
, ino
)) {
265 f2fs_mark_inode_dirty_sync(inode
, true);
266 f2fs_write_inode(inode
, NULL
);
270 ret
= wait_on_node_pages_writeback(sbi
, ino
);
274 /* once recovery info is written, don't need to tack this */
275 remove_ino_entry(sbi
, ino
, APPEND_INO
);
276 clear_inode_flag(inode
, FI_APPEND_WRITE
);
278 remove_ino_entry(sbi
, ino
, UPDATE_INO
);
279 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
280 ret
= f2fs_issue_flush(sbi
);
281 f2fs_update_time(sbi
, REQ_TIME
);
283 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
284 f2fs_trace_ios(NULL
, 1);
288 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
290 return f2fs_do_sync_file(file
, start
, end
, datasync
, false);
293 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
294 pgoff_t pgofs
, int whence
)
299 if (whence
!= SEEK_DATA
)
302 /* find first dirty page index */
303 pagevec_init(&pvec
, 0);
304 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &pgofs
,
305 PAGECACHE_TAG_DIRTY
, 1);
306 pgofs
= nr_pages
? pvec
.pages
[0]->index
: ULONG_MAX
;
307 pagevec_release(&pvec
);
311 static bool __found_offset(block_t blkaddr
, pgoff_t dirty
, pgoff_t pgofs
,
316 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
317 (blkaddr
!= NEW_ADDR
&& blkaddr
!= NULL_ADDR
))
321 if (blkaddr
== NULL_ADDR
)
328 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
330 struct inode
*inode
= file
->f_mapping
->host
;
331 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
332 struct dnode_of_data dn
;
333 pgoff_t pgofs
, end_offset
, dirty
;
334 loff_t data_ofs
= offset
;
340 isize
= i_size_read(inode
);
344 /* handle inline data case */
345 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
346 if (whence
== SEEK_HOLE
)
351 pgofs
= (pgoff_t
)(offset
>> PAGE_SHIFT
);
353 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
355 for (; data_ofs
< isize
; data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
356 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
357 err
= get_dnode_of_data(&dn
, pgofs
, LOOKUP_NODE
);
358 if (err
&& err
!= -ENOENT
) {
360 } else if (err
== -ENOENT
) {
361 /* direct node does not exists */
362 if (whence
== SEEK_DATA
) {
363 pgofs
= get_next_page_offset(&dn
, pgofs
);
370 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
372 /* find data/hole in dnode block */
373 for (; dn
.ofs_in_node
< end_offset
;
374 dn
.ofs_in_node
++, pgofs
++,
375 data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
377 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
379 if (__found_offset(blkaddr
, dirty
, pgofs
, whence
)) {
387 if (whence
== SEEK_DATA
)
390 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
393 return vfs_setpos(file
, data_ofs
, maxbytes
);
399 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
401 struct inode
*inode
= file
->f_mapping
->host
;
402 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
408 return generic_file_llseek_size(file
, offset
, whence
,
409 maxbytes
, i_size_read(inode
));
414 return f2fs_seek_block(file
, offset
, whence
);
420 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
422 struct inode
*inode
= file_inode(file
);
425 if (f2fs_encrypted_inode(inode
)) {
426 err
= fscrypt_get_encryption_info(inode
);
429 if (!f2fs_encrypted_inode(inode
))
433 /* we don't need to use inline_data strictly */
434 err
= f2fs_convert_inline_inode(inode
);
439 vma
->vm_ops
= &f2fs_file_vm_ops
;
443 static int f2fs_file_open(struct inode
*inode
, struct file
*filp
)
445 int ret
= generic_file_open(inode
, filp
);
448 if (!ret
&& f2fs_encrypted_inode(inode
)) {
449 ret
= fscrypt_get_encryption_info(inode
);
452 if (!fscrypt_has_encryption_key(inode
))
455 dir
= dget_parent(file_dentry(filp
));
456 if (f2fs_encrypted_inode(d_inode(dir
)) &&
457 !fscrypt_has_permitted_context(d_inode(dir
), inode
)) {
465 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
467 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
468 struct f2fs_node
*raw_node
;
469 int nr_free
= 0, ofs
= dn
->ofs_in_node
, len
= count
;
472 raw_node
= F2FS_NODE(dn
->node_page
);
473 addr
= blkaddr_in_node(raw_node
) + ofs
;
475 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
476 block_t blkaddr
= le32_to_cpu(*addr
);
477 if (blkaddr
== NULL_ADDR
)
480 dn
->data_blkaddr
= NULL_ADDR
;
481 set_data_blkaddr(dn
);
482 invalidate_blocks(sbi
, blkaddr
);
483 if (dn
->ofs_in_node
== 0 && IS_INODE(dn
->node_page
))
484 clear_inode_flag(dn
->inode
, FI_FIRST_BLOCK_WRITTEN
);
491 * once we invalidate valid blkaddr in range [ofs, ofs + count],
492 * we will invalidate all blkaddr in the whole range.
494 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
),
496 f2fs_update_extent_cache_range(dn
, fofs
, 0, len
);
497 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
499 dn
->ofs_in_node
= ofs
;
501 f2fs_update_time(sbi
, REQ_TIME
);
502 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
503 dn
->ofs_in_node
, nr_free
);
507 void truncate_data_blocks(struct dnode_of_data
*dn
)
509 truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK
);
512 static int truncate_partial_data_page(struct inode
*inode
, u64 from
,
515 unsigned offset
= from
& (PAGE_SIZE
- 1);
516 pgoff_t index
= from
>> PAGE_SHIFT
;
517 struct address_space
*mapping
= inode
->i_mapping
;
520 if (!offset
&& !cache_only
)
524 page
= find_lock_page(mapping
, index
);
525 if (page
&& PageUptodate(page
))
527 f2fs_put_page(page
, 1);
531 page
= get_lock_data_page(inode
, index
, true);
535 f2fs_wait_on_page_writeback(page
, DATA
, true);
536 zero_user(page
, offset
, PAGE_SIZE
- offset
);
537 if (!cache_only
|| !f2fs_encrypted_inode(inode
) ||
538 !S_ISREG(inode
->i_mode
))
539 set_page_dirty(page
);
540 f2fs_put_page(page
, 1);
544 int truncate_blocks(struct inode
*inode
, u64 from
, bool lock
)
546 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
547 unsigned int blocksize
= inode
->i_sb
->s_blocksize
;
548 struct dnode_of_data dn
;
550 int count
= 0, err
= 0;
552 bool truncate_page
= false;
554 trace_f2fs_truncate_blocks_enter(inode
, from
);
556 free_from
= (pgoff_t
)F2FS_BYTES_TO_BLK(from
+ blocksize
- 1);
558 if (free_from
>= sbi
->max_file_blocks
)
564 ipage
= get_node_page(sbi
, inode
->i_ino
);
566 err
= PTR_ERR(ipage
);
570 if (f2fs_has_inline_data(inode
)) {
571 if (truncate_inline_inode(ipage
, from
))
572 set_page_dirty(ipage
);
573 f2fs_put_page(ipage
, 1);
574 truncate_page
= true;
578 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
579 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE_RA
);
586 count
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
588 count
-= dn
.ofs_in_node
;
589 f2fs_bug_on(sbi
, count
< 0);
591 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
592 truncate_data_blocks_range(&dn
, count
);
598 err
= truncate_inode_blocks(inode
, free_from
);
603 /* lastly zero out the first data page */
605 err
= truncate_partial_data_page(inode
, from
, truncate_page
);
607 trace_f2fs_truncate_blocks_exit(inode
, err
);
611 int f2fs_truncate(struct inode
*inode
)
615 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
616 S_ISLNK(inode
->i_mode
)))
619 trace_f2fs_truncate(inode
);
621 /* we should check inline_data size */
622 if (!f2fs_may_inline_data(inode
)) {
623 err
= f2fs_convert_inline_inode(inode
);
628 err
= truncate_blocks(inode
, i_size_read(inode
), true);
632 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
633 f2fs_mark_inode_dirty_sync(inode
, false);
637 int f2fs_getattr(struct vfsmount
*mnt
,
638 struct dentry
*dentry
, struct kstat
*stat
)
640 struct inode
*inode
= d_inode(dentry
);
641 generic_fillattr(inode
, stat
);
646 #ifdef CONFIG_F2FS_FS_POSIX_ACL
647 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
649 unsigned int ia_valid
= attr
->ia_valid
;
651 if (ia_valid
& ATTR_UID
)
652 inode
->i_uid
= attr
->ia_uid
;
653 if (ia_valid
& ATTR_GID
)
654 inode
->i_gid
= attr
->ia_gid
;
655 if (ia_valid
& ATTR_ATIME
)
656 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
657 inode
->i_sb
->s_time_gran
);
658 if (ia_valid
& ATTR_MTIME
)
659 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
660 inode
->i_sb
->s_time_gran
);
661 if (ia_valid
& ATTR_CTIME
)
662 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
663 inode
->i_sb
->s_time_gran
);
664 if (ia_valid
& ATTR_MODE
) {
665 umode_t mode
= attr
->ia_mode
;
667 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
669 set_acl_inode(inode
, mode
);
673 #define __setattr_copy setattr_copy
676 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
678 struct inode
*inode
= d_inode(dentry
);
680 bool size_changed
= false;
682 err
= setattr_prepare(dentry
, attr
);
686 if (attr
->ia_valid
& ATTR_SIZE
) {
687 if (f2fs_encrypted_inode(inode
) &&
688 fscrypt_get_encryption_info(inode
))
691 if (attr
->ia_size
<= i_size_read(inode
)) {
692 truncate_setsize(inode
, attr
->ia_size
);
693 err
= f2fs_truncate(inode
);
698 * do not trim all blocks after i_size if target size is
699 * larger than i_size.
701 truncate_setsize(inode
, attr
->ia_size
);
703 /* should convert inline inode here */
704 if (!f2fs_may_inline_data(inode
)) {
705 err
= f2fs_convert_inline_inode(inode
);
709 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
715 __setattr_copy(inode
, attr
);
717 if (attr
->ia_valid
& ATTR_MODE
) {
718 err
= posix_acl_chmod(inode
, get_inode_mode(inode
));
719 if (err
|| is_inode_flag_set(inode
, FI_ACL_MODE
)) {
720 inode
->i_mode
= F2FS_I(inode
)->i_acl_mode
;
721 clear_inode_flag(inode
, FI_ACL_MODE
);
725 /* file size may changed here */
726 f2fs_mark_inode_dirty_sync(inode
, size_changed
);
728 /* inode change will produce dirty node pages flushed by checkpoint */
729 f2fs_balance_fs(F2FS_I_SB(inode
), true);
734 const struct inode_operations f2fs_file_inode_operations
= {
735 .getattr
= f2fs_getattr
,
736 .setattr
= f2fs_setattr
,
737 .get_acl
= f2fs_get_acl
,
738 .set_acl
= f2fs_set_acl
,
739 #ifdef CONFIG_F2FS_FS_XATTR
740 .listxattr
= f2fs_listxattr
,
742 .fiemap
= f2fs_fiemap
,
745 static int fill_zero(struct inode
*inode
, pgoff_t index
,
746 loff_t start
, loff_t len
)
748 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
754 f2fs_balance_fs(sbi
, true);
757 page
= get_new_data_page(inode
, NULL
, index
, false);
761 return PTR_ERR(page
);
763 f2fs_wait_on_page_writeback(page
, DATA
, true);
764 zero_user(page
, start
, len
);
765 set_page_dirty(page
);
766 f2fs_put_page(page
, 1);
770 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
774 while (pg_start
< pg_end
) {
775 struct dnode_of_data dn
;
776 pgoff_t end_offset
, count
;
778 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
779 err
= get_dnode_of_data(&dn
, pg_start
, LOOKUP_NODE
);
781 if (err
== -ENOENT
) {
788 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
789 count
= min(end_offset
- dn
.ofs_in_node
, pg_end
- pg_start
);
791 f2fs_bug_on(F2FS_I_SB(inode
), count
== 0 || count
> end_offset
);
793 truncate_data_blocks_range(&dn
, count
);
801 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
803 pgoff_t pg_start
, pg_end
;
804 loff_t off_start
, off_end
;
807 ret
= f2fs_convert_inline_inode(inode
);
811 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
812 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
814 off_start
= offset
& (PAGE_SIZE
- 1);
815 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
817 if (pg_start
== pg_end
) {
818 ret
= fill_zero(inode
, pg_start
, off_start
,
819 off_end
- off_start
);
824 ret
= fill_zero(inode
, pg_start
++, off_start
,
825 PAGE_SIZE
- off_start
);
830 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
835 if (pg_start
< pg_end
) {
836 struct address_space
*mapping
= inode
->i_mapping
;
837 loff_t blk_start
, blk_end
;
838 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
840 f2fs_balance_fs(sbi
, true);
842 blk_start
= (loff_t
)pg_start
<< PAGE_SHIFT
;
843 blk_end
= (loff_t
)pg_end
<< PAGE_SHIFT
;
844 truncate_inode_pages_range(mapping
, blk_start
,
848 ret
= truncate_hole(inode
, pg_start
, pg_end
);
856 static int __read_out_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
857 int *do_replace
, pgoff_t off
, pgoff_t len
)
859 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
860 struct dnode_of_data dn
;
864 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
865 ret
= get_dnode_of_data(&dn
, off
, LOOKUP_NODE_RA
);
866 if (ret
&& ret
!= -ENOENT
) {
868 } else if (ret
== -ENOENT
) {
869 if (dn
.max_level
== 0)
871 done
= min((pgoff_t
)ADDRS_PER_BLOCK
- dn
.ofs_in_node
, len
);
877 done
= min((pgoff_t
)ADDRS_PER_PAGE(dn
.node_page
, inode
) -
878 dn
.ofs_in_node
, len
);
879 for (i
= 0; i
< done
; i
++, blkaddr
++, do_replace
++, dn
.ofs_in_node
++) {
880 *blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
881 if (!is_checkpointed_data(sbi
, *blkaddr
)) {
883 if (test_opt(sbi
, LFS
)) {
888 /* do not invalidate this block address */
889 f2fs_update_data_blkaddr(&dn
, NULL_ADDR
);
902 static int __roll_back_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
903 int *do_replace
, pgoff_t off
, int len
)
905 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
906 struct dnode_of_data dn
;
909 for (i
= 0; i
< len
; i
++, do_replace
++, blkaddr
++) {
910 if (*do_replace
== 0)
913 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
914 ret
= get_dnode_of_data(&dn
, off
+ i
, LOOKUP_NODE_RA
);
916 dec_valid_block_count(sbi
, inode
, 1);
917 invalidate_blocks(sbi
, *blkaddr
);
919 f2fs_update_data_blkaddr(&dn
, *blkaddr
);
926 static int __clone_blkaddrs(struct inode
*src_inode
, struct inode
*dst_inode
,
927 block_t
*blkaddr
, int *do_replace
,
928 pgoff_t src
, pgoff_t dst
, pgoff_t len
, bool full
)
930 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src_inode
);
935 if (blkaddr
[i
] == NULL_ADDR
&& !full
) {
940 if (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
) {
941 struct dnode_of_data dn
;
946 set_new_dnode(&dn
, dst_inode
, NULL
, NULL
, 0);
947 ret
= get_dnode_of_data(&dn
, dst
+ i
, ALLOC_NODE
);
951 get_node_info(sbi
, dn
.nid
, &ni
);
953 ADDRS_PER_PAGE(dn
.node_page
, dst_inode
) -
954 dn
.ofs_in_node
, len
- i
);
956 dn
.data_blkaddr
= datablock_addr(dn
.node_page
,
958 truncate_data_blocks_range(&dn
, 1);
961 f2fs_i_blocks_write(src_inode
,
963 f2fs_i_blocks_write(dst_inode
,
965 f2fs_replace_block(sbi
, &dn
, dn
.data_blkaddr
,
966 blkaddr
[i
], ni
.version
, true, false);
972 new_size
= (dst
+ i
) << PAGE_SHIFT
;
973 if (dst_inode
->i_size
< new_size
)
974 f2fs_i_size_write(dst_inode
, new_size
);
975 } while (--ilen
&& (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
));
979 struct page
*psrc
, *pdst
;
981 psrc
= get_lock_data_page(src_inode
, src
+ i
, true);
983 return PTR_ERR(psrc
);
984 pdst
= get_new_data_page(dst_inode
, NULL
, dst
+ i
,
987 f2fs_put_page(psrc
, 1);
988 return PTR_ERR(pdst
);
990 f2fs_copy_page(psrc
, pdst
);
991 set_page_dirty(pdst
);
992 f2fs_put_page(pdst
, 1);
993 f2fs_put_page(psrc
, 1);
995 ret
= truncate_hole(src_inode
, src
+ i
, src
+ i
+ 1);
1004 static int __exchange_data_block(struct inode
*src_inode
,
1005 struct inode
*dst_inode
, pgoff_t src
, pgoff_t dst
,
1006 pgoff_t len
, bool full
)
1008 block_t
*src_blkaddr
;
1014 olen
= min((pgoff_t
)4 * ADDRS_PER_BLOCK
, len
);
1016 src_blkaddr
= f2fs_kvzalloc(sizeof(block_t
) * olen
, GFP_KERNEL
);
1020 do_replace
= f2fs_kvzalloc(sizeof(int) * olen
, GFP_KERNEL
);
1022 kvfree(src_blkaddr
);
1026 ret
= __read_out_blkaddrs(src_inode
, src_blkaddr
,
1027 do_replace
, src
, olen
);
1031 ret
= __clone_blkaddrs(src_inode
, dst_inode
, src_blkaddr
,
1032 do_replace
, src
, dst
, olen
, full
);
1040 kvfree(src_blkaddr
);
1046 __roll_back_blkaddrs(src_inode
, src_blkaddr
, do_replace
, src
, len
);
1047 kvfree(src_blkaddr
);
1052 static int f2fs_do_collapse(struct inode
*inode
, pgoff_t start
, pgoff_t end
)
1054 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1055 pgoff_t nrpages
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1058 f2fs_balance_fs(sbi
, true);
1061 f2fs_drop_extent_tree(inode
);
1063 ret
= __exchange_data_block(inode
, inode
, end
, start
, nrpages
- end
, true);
1064 f2fs_unlock_op(sbi
);
1068 static int f2fs_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1070 pgoff_t pg_start
, pg_end
;
1074 if (offset
+ len
>= i_size_read(inode
))
1077 /* collapse range should be aligned to block size of f2fs. */
1078 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1081 ret
= f2fs_convert_inline_inode(inode
);
1085 pg_start
= offset
>> PAGE_SHIFT
;
1086 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1088 /* write out all dirty pages from offset */
1089 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1093 truncate_pagecache(inode
, offset
);
1095 ret
= f2fs_do_collapse(inode
, pg_start
, pg_end
);
1099 /* write out all moved pages, if possible */
1100 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1101 truncate_pagecache(inode
, offset
);
1103 new_size
= i_size_read(inode
) - len
;
1104 truncate_pagecache(inode
, new_size
);
1106 ret
= truncate_blocks(inode
, new_size
, true);
1108 f2fs_i_size_write(inode
, new_size
);
1113 static int f2fs_do_zero_range(struct dnode_of_data
*dn
, pgoff_t start
,
1116 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1117 pgoff_t index
= start
;
1118 unsigned int ofs_in_node
= dn
->ofs_in_node
;
1122 for (; index
< end
; index
++, dn
->ofs_in_node
++) {
1123 if (datablock_addr(dn
->node_page
, dn
->ofs_in_node
) == NULL_ADDR
)
1127 dn
->ofs_in_node
= ofs_in_node
;
1128 ret
= reserve_new_blocks(dn
, count
);
1132 dn
->ofs_in_node
= ofs_in_node
;
1133 for (index
= start
; index
< end
; index
++, dn
->ofs_in_node
++) {
1135 datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
1137 * reserve_new_blocks will not guarantee entire block
1140 if (dn
->data_blkaddr
== NULL_ADDR
) {
1144 if (dn
->data_blkaddr
!= NEW_ADDR
) {
1145 invalidate_blocks(sbi
, dn
->data_blkaddr
);
1146 dn
->data_blkaddr
= NEW_ADDR
;
1147 set_data_blkaddr(dn
);
1151 f2fs_update_extent_cache_range(dn
, start
, 0, index
- start
);
1156 static int f2fs_zero_range(struct inode
*inode
, loff_t offset
, loff_t len
,
1159 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1160 struct address_space
*mapping
= inode
->i_mapping
;
1161 pgoff_t index
, pg_start
, pg_end
;
1162 loff_t new_size
= i_size_read(inode
);
1163 loff_t off_start
, off_end
;
1166 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1170 ret
= f2fs_convert_inline_inode(inode
);
1174 ret
= filemap_write_and_wait_range(mapping
, offset
, offset
+ len
- 1);
1178 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
1180 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
1181 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
1183 off_start
= offset
& (PAGE_SIZE
- 1);
1184 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1186 if (pg_start
== pg_end
) {
1187 ret
= fill_zero(inode
, pg_start
, off_start
,
1188 off_end
- off_start
);
1192 if (offset
+ len
> new_size
)
1193 new_size
= offset
+ len
;
1194 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1197 ret
= fill_zero(inode
, pg_start
++, off_start
,
1198 PAGE_SIZE
- off_start
);
1202 new_size
= max_t(loff_t
, new_size
,
1203 (loff_t
)pg_start
<< PAGE_SHIFT
);
1206 for (index
= pg_start
; index
< pg_end
;) {
1207 struct dnode_of_data dn
;
1208 unsigned int end_offset
;
1213 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1214 ret
= get_dnode_of_data(&dn
, index
, ALLOC_NODE
);
1216 f2fs_unlock_op(sbi
);
1220 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1221 end
= min(pg_end
, end_offset
- dn
.ofs_in_node
+ index
);
1223 ret
= f2fs_do_zero_range(&dn
, index
, end
);
1224 f2fs_put_dnode(&dn
);
1225 f2fs_unlock_op(sbi
);
1227 f2fs_balance_fs(sbi
, dn
.node_changed
);
1233 new_size
= max_t(loff_t
, new_size
,
1234 (loff_t
)index
<< PAGE_SHIFT
);
1238 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
1242 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1247 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && i_size_read(inode
) < new_size
)
1248 f2fs_i_size_write(inode
, new_size
);
1253 static int f2fs_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1255 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1256 pgoff_t nr
, pg_start
, pg_end
, delta
, idx
;
1260 new_size
= i_size_read(inode
) + len
;
1261 if (new_size
> inode
->i_sb
->s_maxbytes
)
1264 if (offset
>= i_size_read(inode
))
1267 /* insert range should be aligned to block size of f2fs. */
1268 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1271 ret
= f2fs_convert_inline_inode(inode
);
1275 f2fs_balance_fs(sbi
, true);
1277 ret
= truncate_blocks(inode
, i_size_read(inode
), true);
1281 /* write out all dirty pages from offset */
1282 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1286 truncate_pagecache(inode
, offset
);
1288 pg_start
= offset
>> PAGE_SHIFT
;
1289 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1290 delta
= pg_end
- pg_start
;
1291 idx
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1293 while (!ret
&& idx
> pg_start
) {
1294 nr
= idx
- pg_start
;
1300 f2fs_drop_extent_tree(inode
);
1302 ret
= __exchange_data_block(inode
, inode
, idx
,
1303 idx
+ delta
, nr
, false);
1304 f2fs_unlock_op(sbi
);
1307 /* write out all moved pages, if possible */
1308 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1309 truncate_pagecache(inode
, offset
);
1312 f2fs_i_size_write(inode
, new_size
);
1316 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
1317 loff_t len
, int mode
)
1319 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1320 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
};
1322 loff_t new_size
= i_size_read(inode
);
1326 err
= inode_newsize_ok(inode
, (len
+ offset
));
1330 err
= f2fs_convert_inline_inode(inode
);
1334 f2fs_balance_fs(sbi
, true);
1336 pg_end
= ((unsigned long long)offset
+ len
) >> PAGE_SHIFT
;
1337 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1339 map
.m_lblk
= ((unsigned long long)offset
) >> PAGE_SHIFT
;
1340 map
.m_len
= pg_end
- map
.m_lblk
;
1344 err
= f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_AIO
);
1351 last_off
= map
.m_lblk
+ map
.m_len
- 1;
1353 /* update new size to the failed position */
1354 new_size
= (last_off
== pg_end
) ? offset
+ len
:
1355 (loff_t
)(last_off
+ 1) << PAGE_SHIFT
;
1357 new_size
= ((loff_t
)pg_end
<< PAGE_SHIFT
) + off_end
;
1360 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && i_size_read(inode
) < new_size
)
1361 f2fs_i_size_write(inode
, new_size
);
1366 static long f2fs_fallocate(struct file
*file
, int mode
,
1367 loff_t offset
, loff_t len
)
1369 struct inode
*inode
= file_inode(file
);
1372 /* f2fs only support ->fallocate for regular file */
1373 if (!S_ISREG(inode
->i_mode
))
1376 if (f2fs_encrypted_inode(inode
) &&
1377 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
)))
1380 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
1381 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
1382 FALLOC_FL_INSERT_RANGE
))
1387 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
1388 if (offset
>= inode
->i_size
)
1391 ret
= punch_hole(inode
, offset
, len
);
1392 } else if (mode
& FALLOC_FL_COLLAPSE_RANGE
) {
1393 ret
= f2fs_collapse_range(inode
, offset
, len
);
1394 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
1395 ret
= f2fs_zero_range(inode
, offset
, len
, mode
);
1396 } else if (mode
& FALLOC_FL_INSERT_RANGE
) {
1397 ret
= f2fs_insert_range(inode
, offset
, len
);
1399 ret
= expand_inode_data(inode
, offset
, len
, mode
);
1403 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
1404 f2fs_mark_inode_dirty_sync(inode
, false);
1405 if (mode
& FALLOC_FL_KEEP_SIZE
)
1406 file_set_keep_isize(inode
);
1407 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1411 inode_unlock(inode
);
1413 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
1417 static int f2fs_release_file(struct inode
*inode
, struct file
*filp
)
1420 * f2fs_relase_file is called at every close calls. So we should
1421 * not drop any inmemory pages by close called by other process.
1423 if (!(filp
->f_mode
& FMODE_WRITE
) ||
1424 atomic_read(&inode
->i_writecount
) != 1)
1427 /* some remained atomic pages should discarded */
1428 if (f2fs_is_atomic_file(inode
))
1429 drop_inmem_pages(inode
);
1430 if (f2fs_is_volatile_file(inode
)) {
1431 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1432 set_inode_flag(inode
, FI_DROP_CACHE
);
1433 filemap_fdatawrite(inode
->i_mapping
);
1434 clear_inode_flag(inode
, FI_DROP_CACHE
);
1439 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1440 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1442 static inline __u32
f2fs_mask_flags(umode_t mode
, __u32 flags
)
1446 else if (S_ISREG(mode
))
1447 return flags
& F2FS_REG_FLMASK
;
1449 return flags
& F2FS_OTHER_FLMASK
;
1452 static int f2fs_ioc_getflags(struct file
*filp
, unsigned long arg
)
1454 struct inode
*inode
= file_inode(filp
);
1455 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1456 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
1457 return put_user(flags
, (int __user
*)arg
);
1460 static int f2fs_ioc_setflags(struct file
*filp
, unsigned long arg
)
1462 struct inode
*inode
= file_inode(filp
);
1463 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1465 unsigned int oldflags
;
1468 if (!inode_owner_or_capable(inode
))
1471 if (get_user(flags
, (int __user
*)arg
))
1474 ret
= mnt_want_write_file(filp
);
1478 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
1482 oldflags
= fi
->i_flags
;
1484 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
1485 if (!capable(CAP_LINUX_IMMUTABLE
)) {
1486 inode_unlock(inode
);
1492 flags
= flags
& FS_FL_USER_MODIFIABLE
;
1493 flags
|= oldflags
& ~FS_FL_USER_MODIFIABLE
;
1494 fi
->i_flags
= flags
;
1495 inode_unlock(inode
);
1497 inode
->i_ctime
= current_time(inode
);
1498 f2fs_set_inode_flags(inode
);
1500 mnt_drop_write_file(filp
);
1504 static int f2fs_ioc_getversion(struct file
*filp
, unsigned long arg
)
1506 struct inode
*inode
= file_inode(filp
);
1508 return put_user(inode
->i_generation
, (int __user
*)arg
);
1511 static int f2fs_ioc_start_atomic_write(struct file
*filp
)
1513 struct inode
*inode
= file_inode(filp
);
1516 if (!inode_owner_or_capable(inode
))
1519 ret
= mnt_want_write_file(filp
);
1525 if (f2fs_is_atomic_file(inode
))
1528 ret
= f2fs_convert_inline_inode(inode
);
1532 set_inode_flag(inode
, FI_ATOMIC_FILE
);
1533 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1535 if (!get_dirty_pages(inode
))
1538 f2fs_msg(F2FS_I_SB(inode
)->sb
, KERN_WARNING
,
1539 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1540 inode
->i_ino
, get_dirty_pages(inode
));
1541 ret
= filemap_write_and_wait_range(inode
->i_mapping
, 0, LLONG_MAX
);
1543 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
1545 inode_unlock(inode
);
1546 mnt_drop_write_file(filp
);
1550 static int f2fs_ioc_commit_atomic_write(struct file
*filp
)
1552 struct inode
*inode
= file_inode(filp
);
1555 if (!inode_owner_or_capable(inode
))
1558 ret
= mnt_want_write_file(filp
);
1564 if (f2fs_is_volatile_file(inode
))
1567 if (f2fs_is_atomic_file(inode
)) {
1568 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
1569 ret
= commit_inmem_pages(inode
);
1571 set_inode_flag(inode
, FI_ATOMIC_FILE
);
1576 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1578 inode_unlock(inode
);
1579 mnt_drop_write_file(filp
);
1583 static int f2fs_ioc_start_volatile_write(struct file
*filp
)
1585 struct inode
*inode
= file_inode(filp
);
1588 if (!inode_owner_or_capable(inode
))
1591 ret
= mnt_want_write_file(filp
);
1597 if (f2fs_is_volatile_file(inode
))
1600 ret
= f2fs_convert_inline_inode(inode
);
1604 set_inode_flag(inode
, FI_VOLATILE_FILE
);
1605 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1607 inode_unlock(inode
);
1608 mnt_drop_write_file(filp
);
1612 static int f2fs_ioc_release_volatile_write(struct file
*filp
)
1614 struct inode
*inode
= file_inode(filp
);
1617 if (!inode_owner_or_capable(inode
))
1620 ret
= mnt_want_write_file(filp
);
1626 if (!f2fs_is_volatile_file(inode
))
1629 if (!f2fs_is_first_block_written(inode
)) {
1630 ret
= truncate_partial_data_page(inode
, 0, true);
1634 ret
= punch_hole(inode
, 0, F2FS_BLKSIZE
);
1636 inode_unlock(inode
);
1637 mnt_drop_write_file(filp
);
1641 static int f2fs_ioc_abort_volatile_write(struct file
*filp
)
1643 struct inode
*inode
= file_inode(filp
);
1646 if (!inode_owner_or_capable(inode
))
1649 ret
= mnt_want_write_file(filp
);
1655 if (f2fs_is_atomic_file(inode
))
1656 drop_inmem_pages(inode
);
1657 if (f2fs_is_volatile_file(inode
)) {
1658 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1659 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1662 inode_unlock(inode
);
1664 mnt_drop_write_file(filp
);
1665 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1669 static int f2fs_ioc_shutdown(struct file
*filp
, unsigned long arg
)
1671 struct inode
*inode
= file_inode(filp
);
1672 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1673 struct super_block
*sb
= sbi
->sb
;
1677 if (!capable(CAP_SYS_ADMIN
))
1680 if (get_user(in
, (__u32 __user
*)arg
))
1683 ret
= mnt_want_write_file(filp
);
1688 case F2FS_GOING_DOWN_FULLSYNC
:
1689 sb
= freeze_bdev(sb
->s_bdev
);
1690 if (sb
&& !IS_ERR(sb
)) {
1691 f2fs_stop_checkpoint(sbi
, false);
1692 thaw_bdev(sb
->s_bdev
, sb
);
1695 case F2FS_GOING_DOWN_METASYNC
:
1696 /* do checkpoint only */
1697 f2fs_sync_fs(sb
, 1);
1698 f2fs_stop_checkpoint(sbi
, false);
1700 case F2FS_GOING_DOWN_NOSYNC
:
1701 f2fs_stop_checkpoint(sbi
, false);
1703 case F2FS_GOING_DOWN_METAFLUSH
:
1704 sync_meta_pages(sbi
, META
, LONG_MAX
);
1705 f2fs_stop_checkpoint(sbi
, false);
1711 f2fs_update_time(sbi
, REQ_TIME
);
1713 mnt_drop_write_file(filp
);
1717 static int f2fs_ioc_fitrim(struct file
*filp
, unsigned long arg
)
1719 struct inode
*inode
= file_inode(filp
);
1720 struct super_block
*sb
= inode
->i_sb
;
1721 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
1722 struct fstrim_range range
;
1725 if (!capable(CAP_SYS_ADMIN
))
1728 if (!blk_queue_discard(q
))
1731 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
1735 ret
= mnt_want_write_file(filp
);
1739 range
.minlen
= max((unsigned int)range
.minlen
,
1740 q
->limits
.discard_granularity
);
1741 ret
= f2fs_trim_fs(F2FS_SB(sb
), &range
);
1742 mnt_drop_write_file(filp
);
1746 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
1749 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1753 static bool uuid_is_nonzero(__u8 u
[16])
1757 for (i
= 0; i
< 16; i
++)
1763 static int f2fs_ioc_set_encryption_policy(struct file
*filp
, unsigned long arg
)
1765 struct inode
*inode
= file_inode(filp
);
1767 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1769 return fscrypt_ioctl_set_policy(filp
, (const void __user
*)arg
);
1772 static int f2fs_ioc_get_encryption_policy(struct file
*filp
, unsigned long arg
)
1774 return fscrypt_ioctl_get_policy(filp
, (void __user
*)arg
);
1777 static int f2fs_ioc_get_encryption_pwsalt(struct file
*filp
, unsigned long arg
)
1779 struct inode
*inode
= file_inode(filp
);
1780 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1783 if (!f2fs_sb_has_crypto(inode
->i_sb
))
1786 if (uuid_is_nonzero(sbi
->raw_super
->encrypt_pw_salt
))
1789 err
= mnt_want_write_file(filp
);
1793 /* update superblock with uuid */
1794 generate_random_uuid(sbi
->raw_super
->encrypt_pw_salt
);
1796 err
= f2fs_commit_super(sbi
, false);
1799 memset(sbi
->raw_super
->encrypt_pw_salt
, 0, 16);
1800 mnt_drop_write_file(filp
);
1803 mnt_drop_write_file(filp
);
1805 if (copy_to_user((__u8 __user
*)arg
, sbi
->raw_super
->encrypt_pw_salt
,
1811 static int f2fs_ioc_gc(struct file
*filp
, unsigned long arg
)
1813 struct inode
*inode
= file_inode(filp
);
1814 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1818 if (!capable(CAP_SYS_ADMIN
))
1821 if (get_user(sync
, (__u32 __user
*)arg
))
1824 if (f2fs_readonly(sbi
->sb
))
1827 ret
= mnt_want_write_file(filp
);
1832 if (!mutex_trylock(&sbi
->gc_mutex
)) {
1837 mutex_lock(&sbi
->gc_mutex
);
1840 ret
= f2fs_gc(sbi
, sync
, true);
1842 mnt_drop_write_file(filp
);
1846 static int f2fs_ioc_write_checkpoint(struct file
*filp
, unsigned long arg
)
1848 struct inode
*inode
= file_inode(filp
);
1849 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1852 if (!capable(CAP_SYS_ADMIN
))
1855 if (f2fs_readonly(sbi
->sb
))
1858 ret
= mnt_want_write_file(filp
);
1862 ret
= f2fs_sync_fs(sbi
->sb
, 1);
1864 mnt_drop_write_file(filp
);
1868 static int f2fs_defragment_range(struct f2fs_sb_info
*sbi
,
1870 struct f2fs_defragment
*range
)
1872 struct inode
*inode
= file_inode(filp
);
1873 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
};
1874 struct extent_info ei
;
1875 pgoff_t pg_start
, pg_end
;
1876 unsigned int blk_per_seg
= sbi
->blocks_per_seg
;
1877 unsigned int total
= 0, sec_num
;
1878 unsigned int pages_per_sec
= sbi
->segs_per_sec
* blk_per_seg
;
1879 block_t blk_end
= 0;
1880 bool fragmented
= false;
1883 /* if in-place-update policy is enabled, don't waste time here */
1884 if (need_inplace_update(inode
))
1887 pg_start
= range
->start
>> PAGE_SHIFT
;
1888 pg_end
= (range
->start
+ range
->len
) >> PAGE_SHIFT
;
1890 f2fs_balance_fs(sbi
, true);
1894 /* writeback all dirty pages in the range */
1895 err
= filemap_write_and_wait_range(inode
->i_mapping
, range
->start
,
1896 range
->start
+ range
->len
- 1);
1901 * lookup mapping info in extent cache, skip defragmenting if physical
1902 * block addresses are continuous.
1904 if (f2fs_lookup_extent_cache(inode
, pg_start
, &ei
)) {
1905 if (ei
.fofs
+ ei
.len
>= pg_end
)
1909 map
.m_lblk
= pg_start
;
1912 * lookup mapping info in dnode page cache, skip defragmenting if all
1913 * physical block addresses are continuous even if there are hole(s)
1914 * in logical blocks.
1916 while (map
.m_lblk
< pg_end
) {
1917 map
.m_len
= pg_end
- map
.m_lblk
;
1918 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_READ
);
1922 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
1927 if (blk_end
&& blk_end
!= map
.m_pblk
) {
1931 blk_end
= map
.m_pblk
+ map
.m_len
;
1933 map
.m_lblk
+= map
.m_len
;
1939 map
.m_lblk
= pg_start
;
1940 map
.m_len
= pg_end
- pg_start
;
1942 sec_num
= (map
.m_len
+ pages_per_sec
- 1) / pages_per_sec
;
1945 * make sure there are enough free section for LFS allocation, this can
1946 * avoid defragment running in SSR mode when free section are allocated
1949 if (has_not_enough_free_secs(sbi
, 0, sec_num
)) {
1954 while (map
.m_lblk
< pg_end
) {
1959 map
.m_len
= pg_end
- map
.m_lblk
;
1960 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_READ
);
1964 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
1969 set_inode_flag(inode
, FI_DO_DEFRAG
);
1972 while (idx
< map
.m_lblk
+ map
.m_len
&& cnt
< blk_per_seg
) {
1975 page
= get_lock_data_page(inode
, idx
, true);
1977 err
= PTR_ERR(page
);
1981 set_page_dirty(page
);
1982 f2fs_put_page(page
, 1);
1991 if (idx
< pg_end
&& cnt
< blk_per_seg
)
1994 clear_inode_flag(inode
, FI_DO_DEFRAG
);
1996 err
= filemap_fdatawrite(inode
->i_mapping
);
2001 clear_inode_flag(inode
, FI_DO_DEFRAG
);
2003 inode_unlock(inode
);
2005 range
->len
= (u64
)total
<< PAGE_SHIFT
;
2009 static int f2fs_ioc_defragment(struct file
*filp
, unsigned long arg
)
2011 struct inode
*inode
= file_inode(filp
);
2012 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2013 struct f2fs_defragment range
;
2016 if (!capable(CAP_SYS_ADMIN
))
2019 if (!S_ISREG(inode
->i_mode
))
2022 err
= mnt_want_write_file(filp
);
2026 if (f2fs_readonly(sbi
->sb
)) {
2031 if (copy_from_user(&range
, (struct f2fs_defragment __user
*)arg
,
2037 /* verify alignment of offset & size */
2038 if (range
.start
& (F2FS_BLKSIZE
- 1) ||
2039 range
.len
& (F2FS_BLKSIZE
- 1)) {
2044 err
= f2fs_defragment_range(sbi
, filp
, &range
);
2045 f2fs_update_time(sbi
, REQ_TIME
);
2049 if (copy_to_user((struct f2fs_defragment __user
*)arg
, &range
,
2053 mnt_drop_write_file(filp
);
2057 static int f2fs_move_file_range(struct file
*file_in
, loff_t pos_in
,
2058 struct file
*file_out
, loff_t pos_out
, size_t len
)
2060 struct inode
*src
= file_inode(file_in
);
2061 struct inode
*dst
= file_inode(file_out
);
2062 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src
);
2063 size_t olen
= len
, dst_max_i_size
= 0;
2067 if (file_in
->f_path
.mnt
!= file_out
->f_path
.mnt
||
2068 src
->i_sb
!= dst
->i_sb
)
2071 if (unlikely(f2fs_readonly(src
->i_sb
)))
2074 if (!S_ISREG(src
->i_mode
) || !S_ISREG(dst
->i_mode
))
2077 if (f2fs_encrypted_inode(src
) || f2fs_encrypted_inode(dst
))
2081 if (pos_in
== pos_out
)
2083 if (pos_out
> pos_in
&& pos_out
< pos_in
+ len
)
2089 if (!inode_trylock(dst
)) {
2096 if (pos_in
+ len
> src
->i_size
|| pos_in
+ len
< pos_in
)
2099 olen
= len
= src
->i_size
- pos_in
;
2100 if (pos_in
+ len
== src
->i_size
)
2101 len
= ALIGN(src
->i_size
, F2FS_BLKSIZE
) - pos_in
;
2107 dst_osize
= dst
->i_size
;
2108 if (pos_out
+ olen
> dst
->i_size
)
2109 dst_max_i_size
= pos_out
+ olen
;
2111 /* verify the end result is block aligned */
2112 if (!IS_ALIGNED(pos_in
, F2FS_BLKSIZE
) ||
2113 !IS_ALIGNED(pos_in
+ len
, F2FS_BLKSIZE
) ||
2114 !IS_ALIGNED(pos_out
, F2FS_BLKSIZE
))
2117 ret
= f2fs_convert_inline_inode(src
);
2121 ret
= f2fs_convert_inline_inode(dst
);
2125 /* write out all dirty pages from offset */
2126 ret
= filemap_write_and_wait_range(src
->i_mapping
,
2127 pos_in
, pos_in
+ len
);
2131 ret
= filemap_write_and_wait_range(dst
->i_mapping
,
2132 pos_out
, pos_out
+ len
);
2136 f2fs_balance_fs(sbi
, true);
2138 ret
= __exchange_data_block(src
, dst
, pos_in
>> F2FS_BLKSIZE_BITS
,
2139 pos_out
>> F2FS_BLKSIZE_BITS
,
2140 len
>> F2FS_BLKSIZE_BITS
, false);
2144 f2fs_i_size_write(dst
, dst_max_i_size
);
2145 else if (dst_osize
!= dst
->i_size
)
2146 f2fs_i_size_write(dst
, dst_osize
);
2148 f2fs_unlock_op(sbi
);
2157 static int f2fs_ioc_move_range(struct file
*filp
, unsigned long arg
)
2159 struct f2fs_move_range range
;
2163 if (!(filp
->f_mode
& FMODE_READ
) ||
2164 !(filp
->f_mode
& FMODE_WRITE
))
2167 if (copy_from_user(&range
, (struct f2fs_move_range __user
*)arg
,
2171 dst
= fdget(range
.dst_fd
);
2175 if (!(dst
.file
->f_mode
& FMODE_WRITE
)) {
2180 err
= mnt_want_write_file(filp
);
2184 err
= f2fs_move_file_range(filp
, range
.pos_in
, dst
.file
,
2185 range
.pos_out
, range
.len
);
2187 mnt_drop_write_file(filp
);
2189 if (copy_to_user((struct f2fs_move_range __user
*)arg
,
2190 &range
, sizeof(range
)))
2197 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2200 case F2FS_IOC_GETFLAGS
:
2201 return f2fs_ioc_getflags(filp
, arg
);
2202 case F2FS_IOC_SETFLAGS
:
2203 return f2fs_ioc_setflags(filp
, arg
);
2204 case F2FS_IOC_GETVERSION
:
2205 return f2fs_ioc_getversion(filp
, arg
);
2206 case F2FS_IOC_START_ATOMIC_WRITE
:
2207 return f2fs_ioc_start_atomic_write(filp
);
2208 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
2209 return f2fs_ioc_commit_atomic_write(filp
);
2210 case F2FS_IOC_START_VOLATILE_WRITE
:
2211 return f2fs_ioc_start_volatile_write(filp
);
2212 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
2213 return f2fs_ioc_release_volatile_write(filp
);
2214 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
2215 return f2fs_ioc_abort_volatile_write(filp
);
2216 case F2FS_IOC_SHUTDOWN
:
2217 return f2fs_ioc_shutdown(filp
, arg
);
2219 return f2fs_ioc_fitrim(filp
, arg
);
2220 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
2221 return f2fs_ioc_set_encryption_policy(filp
, arg
);
2222 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
2223 return f2fs_ioc_get_encryption_policy(filp
, arg
);
2224 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
2225 return f2fs_ioc_get_encryption_pwsalt(filp
, arg
);
2226 case F2FS_IOC_GARBAGE_COLLECT
:
2227 return f2fs_ioc_gc(filp
, arg
);
2228 case F2FS_IOC_WRITE_CHECKPOINT
:
2229 return f2fs_ioc_write_checkpoint(filp
, arg
);
2230 case F2FS_IOC_DEFRAGMENT
:
2231 return f2fs_ioc_defragment(filp
, arg
);
2232 case F2FS_IOC_MOVE_RANGE
:
2233 return f2fs_ioc_move_range(filp
, arg
);
2239 static ssize_t
f2fs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
2241 struct file
*file
= iocb
->ki_filp
;
2242 struct inode
*inode
= file_inode(file
);
2243 struct blk_plug plug
;
2246 if (f2fs_encrypted_inode(inode
) &&
2247 !fscrypt_has_encryption_key(inode
) &&
2248 fscrypt_get_encryption_info(inode
))
2252 ret
= generic_write_checks(iocb
, from
);
2254 int err
= f2fs_preallocate_blocks(iocb
, from
);
2257 inode_unlock(inode
);
2260 blk_start_plug(&plug
);
2261 ret
= __generic_file_write_iter(iocb
, from
);
2262 blk_finish_plug(&plug
);
2264 inode_unlock(inode
);
2267 ret
= generic_write_sync(iocb
, ret
);
2271 #ifdef CONFIG_COMPAT
2272 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2275 case F2FS_IOC32_GETFLAGS
:
2276 cmd
= F2FS_IOC_GETFLAGS
;
2278 case F2FS_IOC32_SETFLAGS
:
2279 cmd
= F2FS_IOC_SETFLAGS
;
2281 case F2FS_IOC32_GETVERSION
:
2282 cmd
= F2FS_IOC_GETVERSION
;
2284 case F2FS_IOC_START_ATOMIC_WRITE
:
2285 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
2286 case F2FS_IOC_START_VOLATILE_WRITE
:
2287 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
2288 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
2289 case F2FS_IOC_SHUTDOWN
:
2290 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
2291 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
2292 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
2293 case F2FS_IOC_GARBAGE_COLLECT
:
2294 case F2FS_IOC_WRITE_CHECKPOINT
:
2295 case F2FS_IOC_DEFRAGMENT
:
2297 case F2FS_IOC_MOVE_RANGE
:
2300 return -ENOIOCTLCMD
;
2302 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
2306 const struct file_operations f2fs_file_operations
= {
2307 .llseek
= f2fs_llseek
,
2308 .read_iter
= generic_file_read_iter
,
2309 .write_iter
= f2fs_file_write_iter
,
2310 .open
= f2fs_file_open
,
2311 .release
= f2fs_release_file
,
2312 .mmap
= f2fs_file_mmap
,
2313 .fsync
= f2fs_sync_file
,
2314 .fallocate
= f2fs_fallocate
,
2315 .unlocked_ioctl
= f2fs_ioctl
,
2316 #ifdef CONFIG_COMPAT
2317 .compat_ioctl
= f2fs_compat_ioctl
,
2319 .splice_read
= generic_file_splice_read
,
2320 .splice_write
= iter_file_splice_write
,