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/random.h>
32 #include <trace/events/f2fs.h>
34 static int f2fs_vm_page_mkwrite(struct vm_area_struct
*vma
,
37 struct page
*page
= vmf
->page
;
38 struct inode
*inode
= file_inode(vma
->vm_file
);
39 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
40 struct dnode_of_data dn
;
43 sb_start_pagefault(inode
->i_sb
);
45 f2fs_bug_on(sbi
, f2fs_has_inline_data(inode
));
47 /* block allocation */
49 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
50 err
= f2fs_reserve_block(&dn
, page
->index
);
58 f2fs_balance_fs(sbi
, dn
.node_changed
);
60 file_update_time(vma
->vm_file
);
62 if (unlikely(page
->mapping
!= inode
->i_mapping
||
63 page_offset(page
) > i_size_read(inode
) ||
64 !PageUptodate(page
))) {
71 * check to see if the page is mapped already (no holes)
73 if (PageMappedToDisk(page
))
76 /* page is wholly or partially inside EOF */
77 if (((loff_t
)(page
->index
+ 1) << PAGE_SHIFT
) >
80 offset
= i_size_read(inode
) & ~PAGE_MASK
;
81 zero_user_segment(page
, offset
, PAGE_SIZE
);
84 SetPageUptodate(page
);
86 trace_f2fs_vm_page_mkwrite(page
, DATA
);
89 f2fs_wait_on_page_writeback(page
, DATA
, false);
91 /* wait for GCed encrypted page writeback */
92 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
93 f2fs_wait_on_encrypted_page_writeback(sbi
, dn
.data_blkaddr
);
95 /* if gced page is attached, don't write to cold segment */
96 clear_cold_data(page
);
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 (file_enc_name(inode
) && need_dentry_mark(sbi
, inode
->i_ino
))
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
)) {
175 file_got_pino(inode
);
176 up_write(&fi
->i_sem
);
178 mark_inode_dirty_sync(inode
);
179 f2fs_write_inode(inode
, NULL
);
181 up_write(&fi
->i_sem
);
185 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
187 struct inode
*inode
= file
->f_mapping
->host
;
188 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
189 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
190 nid_t ino
= inode
->i_ino
;
192 bool need_cp
= false;
193 struct writeback_control wbc
= {
194 .sync_mode
= WB_SYNC_ALL
,
195 .nr_to_write
= LONG_MAX
,
199 if (unlikely(f2fs_readonly(inode
->i_sb
)))
202 trace_f2fs_sync_file_enter(inode
);
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(fi
, FI_NEED_IPU
);
207 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
208 clear_inode_flag(fi
, 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 */
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(fi
, 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(fi
, 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(&fi
->i_sem
);
242 need_cp
= need_do_checkpoint(inode
);
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(fi
, FI_APPEND_WRITE
);
255 clear_inode_flag(fi
, FI_UPDATE_WRITE
);
259 sync_node_pages(sbi
, ino
, &wbc
);
261 /* if cp_error was enabled, we should avoid infinite loop */
262 if (unlikely(f2fs_cp_error(sbi
))) {
267 if (need_inode_block_update(sbi
, ino
)) {
268 mark_inode_dirty_sync(inode
);
269 f2fs_write_inode(inode
, NULL
);
273 ret
= wait_on_node_pages_writeback(sbi
, ino
);
277 /* once recovery info is written, don't need to tack this */
278 remove_ino_entry(sbi
, ino
, APPEND_INO
);
279 clear_inode_flag(fi
, FI_APPEND_WRITE
);
281 remove_ino_entry(sbi
, ino
, UPDATE_INO
);
282 clear_inode_flag(fi
, FI_UPDATE_WRITE
);
283 ret
= f2fs_issue_flush(sbi
);
284 f2fs_update_time(sbi
, REQ_TIME
);
286 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
287 f2fs_trace_ios(NULL
, 1);
291 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
292 pgoff_t pgofs
, int whence
)
297 if (whence
!= SEEK_DATA
)
300 /* find first dirty page index */
301 pagevec_init(&pvec
, 0);
302 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &pgofs
,
303 PAGECACHE_TAG_DIRTY
, 1);
304 pgofs
= nr_pages
? pvec
.pages
[0]->index
: ULONG_MAX
;
305 pagevec_release(&pvec
);
309 static bool __found_offset(block_t blkaddr
, pgoff_t dirty
, pgoff_t pgofs
,
314 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
315 (blkaddr
!= NEW_ADDR
&& blkaddr
!= NULL_ADDR
))
319 if (blkaddr
== NULL_ADDR
)
326 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
328 struct inode
*inode
= file
->f_mapping
->host
;
329 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
330 struct dnode_of_data dn
;
331 pgoff_t pgofs
, end_offset
, dirty
;
332 loff_t data_ofs
= offset
;
338 isize
= i_size_read(inode
);
342 /* handle inline data case */
343 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
344 if (whence
== SEEK_HOLE
)
349 pgofs
= (pgoff_t
)(offset
>> PAGE_SHIFT
);
351 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
353 for (; data_ofs
< isize
; data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
354 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
355 err
= get_dnode_of_data(&dn
, pgofs
, LOOKUP_NODE_RA
);
356 if (err
&& err
!= -ENOENT
) {
358 } else if (err
== -ENOENT
) {
359 /* direct node does not exists */
360 if (whence
== SEEK_DATA
) {
361 pgofs
= get_next_page_offset(&dn
, pgofs
);
368 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
370 /* find data/hole in dnode block */
371 for (; dn
.ofs_in_node
< end_offset
;
372 dn
.ofs_in_node
++, pgofs
++,
373 data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
375 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
377 if (__found_offset(blkaddr
, dirty
, pgofs
, whence
)) {
385 if (whence
== SEEK_DATA
)
388 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
391 return vfs_setpos(file
, data_ofs
, maxbytes
);
397 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
399 struct inode
*inode
= file
->f_mapping
->host
;
400 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
406 return generic_file_llseek_size(file
, offset
, whence
,
407 maxbytes
, i_size_read(inode
));
412 return f2fs_seek_block(file
, offset
, whence
);
418 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
420 struct inode
*inode
= file_inode(file
);
423 if (f2fs_encrypted_inode(inode
)) {
424 err
= fscrypt_get_encryption_info(inode
);
427 if (!f2fs_encrypted_inode(inode
))
431 /* we don't need to use inline_data strictly */
432 err
= f2fs_convert_inline_inode(inode
);
437 vma
->vm_ops
= &f2fs_file_vm_ops
;
441 static int f2fs_file_open(struct inode
*inode
, struct file
*filp
)
443 int ret
= generic_file_open(inode
, filp
);
446 if (!ret
&& f2fs_encrypted_inode(inode
)) {
447 ret
= fscrypt_get_encryption_info(inode
);
450 if (!fscrypt_has_encryption_key(inode
))
453 dir
= dget_parent(file_dentry(filp
));
454 if (f2fs_encrypted_inode(d_inode(dir
)) &&
455 !fscrypt_has_permitted_context(d_inode(dir
), inode
)) {
463 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
465 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
466 struct f2fs_node
*raw_node
;
467 int nr_free
= 0, ofs
= dn
->ofs_in_node
, len
= count
;
470 raw_node
= F2FS_NODE(dn
->node_page
);
471 addr
= blkaddr_in_node(raw_node
) + ofs
;
473 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
474 block_t blkaddr
= le32_to_cpu(*addr
);
475 if (blkaddr
== NULL_ADDR
)
478 dn
->data_blkaddr
= NULL_ADDR
;
479 set_data_blkaddr(dn
);
480 invalidate_blocks(sbi
, blkaddr
);
481 if (dn
->ofs_in_node
== 0 && IS_INODE(dn
->node_page
))
482 clear_inode_flag(F2FS_I(dn
->inode
),
483 FI_FIRST_BLOCK_WRITTEN
);
490 * once we invalidate valid blkaddr in range [ofs, ofs + count],
491 * we will invalidate all blkaddr in the whole range.
493 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
),
495 f2fs_update_extent_cache_range(dn
, fofs
, 0, len
);
496 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
= f2fs_grab_cache_page(mapping
, index
, false);
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);
561 ipage
= get_node_page(sbi
, inode
->i_ino
);
563 err
= PTR_ERR(ipage
);
567 if (f2fs_has_inline_data(inode
)) {
568 if (truncate_inline_inode(ipage
, from
))
569 set_page_dirty(ipage
);
570 f2fs_put_page(ipage
, 1);
571 truncate_page
= true;
575 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
576 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE
);
583 count
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
585 count
-= dn
.ofs_in_node
;
586 f2fs_bug_on(sbi
, count
< 0);
588 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
589 truncate_data_blocks_range(&dn
, count
);
595 err
= truncate_inode_blocks(inode
, free_from
);
600 /* lastly zero out the first data page */
602 err
= truncate_partial_data_page(inode
, from
, truncate_page
);
604 trace_f2fs_truncate_blocks_exit(inode
, err
);
608 int f2fs_truncate(struct inode
*inode
, bool lock
)
612 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
613 S_ISLNK(inode
->i_mode
)))
616 trace_f2fs_truncate(inode
);
618 /* we should check inline_data size */
619 if (!f2fs_may_inline_data(inode
)) {
620 err
= f2fs_convert_inline_inode(inode
);
625 err
= truncate_blocks(inode
, i_size_read(inode
), lock
);
629 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
630 mark_inode_dirty(inode
);
634 int f2fs_getattr(struct vfsmount
*mnt
,
635 struct dentry
*dentry
, struct kstat
*stat
)
637 struct inode
*inode
= d_inode(dentry
);
638 generic_fillattr(inode
, stat
);
643 #ifdef CONFIG_F2FS_FS_POSIX_ACL
644 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
646 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
647 unsigned int ia_valid
= attr
->ia_valid
;
649 if (ia_valid
& ATTR_UID
)
650 inode
->i_uid
= attr
->ia_uid
;
651 if (ia_valid
& ATTR_GID
)
652 inode
->i_gid
= attr
->ia_gid
;
653 if (ia_valid
& ATTR_ATIME
)
654 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
655 inode
->i_sb
->s_time_gran
);
656 if (ia_valid
& ATTR_MTIME
)
657 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
658 inode
->i_sb
->s_time_gran
);
659 if (ia_valid
& ATTR_CTIME
)
660 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
661 inode
->i_sb
->s_time_gran
);
662 if (ia_valid
& ATTR_MODE
) {
663 umode_t mode
= attr
->ia_mode
;
665 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
667 set_acl_inode(fi
, mode
);
671 #define __setattr_copy setattr_copy
674 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
676 struct inode
*inode
= d_inode(dentry
);
677 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
680 err
= inode_change_ok(inode
, attr
);
684 if (attr
->ia_valid
& ATTR_SIZE
) {
685 if (f2fs_encrypted_inode(inode
) &&
686 fscrypt_get_encryption_info(inode
))
689 if (attr
->ia_size
<= i_size_read(inode
)) {
690 truncate_setsize(inode
, attr
->ia_size
);
691 err
= f2fs_truncate(inode
, true);
694 f2fs_balance_fs(F2FS_I_SB(inode
), true);
697 * do not trim all blocks after i_size if target size is
698 * larger than i_size.
700 truncate_setsize(inode
, attr
->ia_size
);
702 /* should convert inline inode here */
703 if (!f2fs_may_inline_data(inode
)) {
704 err
= f2fs_convert_inline_inode(inode
);
708 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
712 __setattr_copy(inode
, attr
);
714 if (attr
->ia_valid
& ATTR_MODE
) {
715 err
= posix_acl_chmod(inode
, get_inode_mode(inode
));
716 if (err
|| is_inode_flag_set(fi
, FI_ACL_MODE
)) {
717 inode
->i_mode
= fi
->i_acl_mode
;
718 clear_inode_flag(fi
, FI_ACL_MODE
);
722 mark_inode_dirty(inode
);
726 const struct inode_operations f2fs_file_inode_operations
= {
727 .getattr
= f2fs_getattr
,
728 .setattr
= f2fs_setattr
,
729 .get_acl
= f2fs_get_acl
,
730 .set_acl
= f2fs_set_acl
,
731 #ifdef CONFIG_F2FS_FS_XATTR
732 .setxattr
= generic_setxattr
,
733 .getxattr
= generic_getxattr
,
734 .listxattr
= f2fs_listxattr
,
735 .removexattr
= generic_removexattr
,
737 .fiemap
= f2fs_fiemap
,
740 static int fill_zero(struct inode
*inode
, pgoff_t index
,
741 loff_t start
, loff_t len
)
743 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
749 f2fs_balance_fs(sbi
, true);
752 page
= get_new_data_page(inode
, NULL
, index
, false);
756 return PTR_ERR(page
);
758 f2fs_wait_on_page_writeback(page
, DATA
, true);
759 zero_user(page
, start
, len
);
760 set_page_dirty(page
);
761 f2fs_put_page(page
, 1);
765 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
769 while (pg_start
< pg_end
) {
770 struct dnode_of_data dn
;
771 pgoff_t end_offset
, count
;
773 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
774 err
= get_dnode_of_data(&dn
, pg_start
, LOOKUP_NODE
);
776 if (err
== -ENOENT
) {
783 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
784 count
= min(end_offset
- dn
.ofs_in_node
, pg_end
- pg_start
);
786 f2fs_bug_on(F2FS_I_SB(inode
), count
== 0 || count
> end_offset
);
788 truncate_data_blocks_range(&dn
, count
);
796 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
798 pgoff_t pg_start
, pg_end
;
799 loff_t off_start
, off_end
;
802 ret
= f2fs_convert_inline_inode(inode
);
806 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
807 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
809 off_start
= offset
& (PAGE_SIZE
- 1);
810 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
812 if (pg_start
== pg_end
) {
813 ret
= fill_zero(inode
, pg_start
, off_start
,
814 off_end
- off_start
);
819 ret
= fill_zero(inode
, pg_start
++, off_start
,
820 PAGE_SIZE
- off_start
);
825 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
830 if (pg_start
< pg_end
) {
831 struct address_space
*mapping
= inode
->i_mapping
;
832 loff_t blk_start
, blk_end
;
833 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
835 f2fs_balance_fs(sbi
, true);
837 blk_start
= (loff_t
)pg_start
<< PAGE_SHIFT
;
838 blk_end
= (loff_t
)pg_end
<< PAGE_SHIFT
;
839 truncate_inode_pages_range(mapping
, blk_start
,
843 ret
= truncate_hole(inode
, pg_start
, pg_end
);
851 static int __exchange_data_block(struct inode
*inode
, pgoff_t src
,
852 pgoff_t dst
, bool full
)
854 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
855 struct dnode_of_data dn
;
857 bool do_replace
= false;
860 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
861 ret
= get_dnode_of_data(&dn
, src
, LOOKUP_NODE_RA
);
862 if (ret
&& ret
!= -ENOENT
) {
864 } else if (ret
== -ENOENT
) {
865 new_addr
= NULL_ADDR
;
867 new_addr
= dn
.data_blkaddr
;
868 if (!is_checkpointed_data(sbi
, new_addr
)) {
869 /* do not invalidate this block address */
870 f2fs_update_data_blkaddr(&dn
, NULL_ADDR
);
876 if (new_addr
== NULL_ADDR
)
877 return full
? truncate_hole(inode
, dst
, dst
+ 1) : 0;
880 struct page
*ipage
= get_node_page(sbi
, inode
->i_ino
);
884 ret
= PTR_ERR(ipage
);
888 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
889 ret
= f2fs_reserve_block(&dn
, dst
);
893 truncate_data_blocks_range(&dn
, 1);
895 get_node_info(sbi
, dn
.nid
, &ni
);
896 f2fs_replace_block(sbi
, &dn
, dn
.data_blkaddr
, new_addr
,
897 ni
.version
, true, false);
900 struct page
*psrc
, *pdst
;
902 psrc
= get_lock_data_page(inode
, src
, true);
904 return PTR_ERR(psrc
);
905 pdst
= get_new_data_page(inode
, NULL
, dst
, true);
907 f2fs_put_page(psrc
, 1);
908 return PTR_ERR(pdst
);
910 f2fs_copy_page(psrc
, pdst
);
911 set_page_dirty(pdst
);
912 f2fs_put_page(pdst
, 1);
913 f2fs_put_page(psrc
, 1);
915 return truncate_hole(inode
, src
, src
+ 1);
920 if (!get_dnode_of_data(&dn
, src
, LOOKUP_NODE
)) {
921 f2fs_update_data_blkaddr(&dn
, new_addr
);
927 static int f2fs_do_collapse(struct inode
*inode
, pgoff_t start
, pgoff_t end
)
929 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
930 pgoff_t nrpages
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
933 for (; end
< nrpages
; start
++, end
++) {
934 f2fs_balance_fs(sbi
, true);
936 ret
= __exchange_data_block(inode
, end
, start
, true);
944 static int f2fs_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
946 pgoff_t pg_start
, pg_end
;
950 if (offset
+ len
>= i_size_read(inode
))
953 /* collapse range should be aligned to block size of f2fs. */
954 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
957 ret
= f2fs_convert_inline_inode(inode
);
961 pg_start
= offset
>> PAGE_SHIFT
;
962 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
964 /* write out all dirty pages from offset */
965 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
969 truncate_pagecache(inode
, offset
);
971 ret
= f2fs_do_collapse(inode
, pg_start
, pg_end
);
975 /* write out all moved pages, if possible */
976 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
977 truncate_pagecache(inode
, offset
);
979 new_size
= i_size_read(inode
) - len
;
980 truncate_pagecache(inode
, new_size
);
982 ret
= truncate_blocks(inode
, new_size
, true);
984 i_size_write(inode
, new_size
);
989 static int f2fs_zero_range(struct inode
*inode
, loff_t offset
, loff_t len
,
992 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
993 struct address_space
*mapping
= inode
->i_mapping
;
994 pgoff_t index
, pg_start
, pg_end
;
995 loff_t new_size
= i_size_read(inode
);
996 loff_t off_start
, off_end
;
999 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1003 ret
= f2fs_convert_inline_inode(inode
);
1007 ret
= filemap_write_and_wait_range(mapping
, offset
, offset
+ len
- 1);
1011 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
1013 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
1014 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
1016 off_start
= offset
& (PAGE_SIZE
- 1);
1017 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1019 if (pg_start
== pg_end
) {
1020 ret
= fill_zero(inode
, pg_start
, off_start
,
1021 off_end
- off_start
);
1025 if (offset
+ len
> new_size
)
1026 new_size
= offset
+ len
;
1027 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1030 ret
= fill_zero(inode
, pg_start
++, off_start
,
1031 PAGE_SIZE
- off_start
);
1035 new_size
= max_t(loff_t
, new_size
,
1036 (loff_t
)pg_start
<< PAGE_SHIFT
);
1039 for (index
= pg_start
; index
< pg_end
; index
++) {
1040 struct dnode_of_data dn
;
1045 ipage
= get_node_page(sbi
, inode
->i_ino
);
1046 if (IS_ERR(ipage
)) {
1047 ret
= PTR_ERR(ipage
);
1048 f2fs_unlock_op(sbi
);
1052 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
1053 ret
= f2fs_reserve_block(&dn
, index
);
1055 f2fs_unlock_op(sbi
);
1059 if (dn
.data_blkaddr
!= NEW_ADDR
) {
1060 invalidate_blocks(sbi
, dn
.data_blkaddr
);
1061 f2fs_update_data_blkaddr(&dn
, NEW_ADDR
);
1063 f2fs_put_dnode(&dn
);
1064 f2fs_unlock_op(sbi
);
1066 new_size
= max_t(loff_t
, new_size
,
1067 (loff_t
)(index
+ 1) << PAGE_SHIFT
);
1071 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
1075 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1080 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && i_size_read(inode
) < new_size
) {
1081 i_size_write(inode
, new_size
);
1082 mark_inode_dirty(inode
);
1083 update_inode_page(inode
);
1089 static int f2fs_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1091 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1092 pgoff_t pg_start
, pg_end
, delta
, nrpages
, idx
;
1096 new_size
= i_size_read(inode
) + len
;
1097 if (new_size
> inode
->i_sb
->s_maxbytes
)
1100 if (offset
>= i_size_read(inode
))
1103 /* insert range should be aligned to block size of f2fs. */
1104 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1107 ret
= f2fs_convert_inline_inode(inode
);
1111 f2fs_balance_fs(sbi
, true);
1113 ret
= truncate_blocks(inode
, i_size_read(inode
), true);
1117 /* write out all dirty pages from offset */
1118 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1122 truncate_pagecache(inode
, offset
);
1124 pg_start
= offset
>> PAGE_SHIFT
;
1125 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1126 delta
= pg_end
- pg_start
;
1127 nrpages
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1129 for (idx
= nrpages
- 1; idx
>= pg_start
&& idx
!= -1; idx
--) {
1131 ret
= __exchange_data_block(inode
, idx
, idx
+ delta
, false);
1132 f2fs_unlock_op(sbi
);
1137 /* write out all moved pages, if possible */
1138 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1139 truncate_pagecache(inode
, offset
);
1142 i_size_write(inode
, new_size
);
1146 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
1147 loff_t len
, int mode
)
1149 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1150 pgoff_t index
, pg_start
, pg_end
;
1151 loff_t new_size
= i_size_read(inode
);
1152 loff_t off_start
, off_end
;
1155 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1159 ret
= f2fs_convert_inline_inode(inode
);
1163 f2fs_balance_fs(sbi
, true);
1165 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
1166 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
1168 off_start
= offset
& (PAGE_SIZE
- 1);
1169 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1173 for (index
= pg_start
; index
<= pg_end
; index
++) {
1174 struct dnode_of_data dn
;
1176 if (index
== pg_end
&& !off_end
)
1179 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1180 ret
= f2fs_reserve_block(&dn
, index
);
1184 if (pg_start
== pg_end
)
1185 new_size
= offset
+ len
;
1186 else if (index
== pg_start
&& off_start
)
1187 new_size
= (loff_t
)(index
+ 1) << PAGE_SHIFT
;
1188 else if (index
== pg_end
)
1189 new_size
= ((loff_t
)index
<< PAGE_SHIFT
) +
1192 new_size
+= PAGE_SIZE
;
1195 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
1196 i_size_read(inode
) < new_size
) {
1197 i_size_write(inode
, new_size
);
1198 mark_inode_dirty(inode
);
1199 update_inode_page(inode
);
1201 f2fs_unlock_op(sbi
);
1206 static long f2fs_fallocate(struct file
*file
, int mode
,
1207 loff_t offset
, loff_t len
)
1209 struct inode
*inode
= file_inode(file
);
1212 /* f2fs only support ->fallocate for regular file */
1213 if (!S_ISREG(inode
->i_mode
))
1216 if (f2fs_encrypted_inode(inode
) &&
1217 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
)))
1220 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
1221 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
1222 FALLOC_FL_INSERT_RANGE
))
1227 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
1228 if (offset
>= inode
->i_size
)
1231 ret
= punch_hole(inode
, offset
, len
);
1232 } else if (mode
& FALLOC_FL_COLLAPSE_RANGE
) {
1233 ret
= f2fs_collapse_range(inode
, offset
, len
);
1234 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
1235 ret
= f2fs_zero_range(inode
, offset
, len
, mode
);
1236 } else if (mode
& FALLOC_FL_INSERT_RANGE
) {
1237 ret
= f2fs_insert_range(inode
, offset
, len
);
1239 ret
= expand_inode_data(inode
, offset
, len
, mode
);
1243 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1244 mark_inode_dirty(inode
);
1245 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1249 inode_unlock(inode
);
1251 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
1255 static int f2fs_release_file(struct inode
*inode
, struct file
*filp
)
1257 /* some remained atomic pages should discarded */
1258 if (f2fs_is_atomic_file(inode
))
1259 drop_inmem_pages(inode
);
1260 if (f2fs_is_volatile_file(inode
)) {
1261 set_inode_flag(F2FS_I(inode
), FI_DROP_CACHE
);
1262 filemap_fdatawrite(inode
->i_mapping
);
1263 clear_inode_flag(F2FS_I(inode
), FI_DROP_CACHE
);
1268 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1269 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1271 static inline __u32
f2fs_mask_flags(umode_t mode
, __u32 flags
)
1275 else if (S_ISREG(mode
))
1276 return flags
& F2FS_REG_FLMASK
;
1278 return flags
& F2FS_OTHER_FLMASK
;
1281 static int f2fs_ioc_getflags(struct file
*filp
, unsigned long arg
)
1283 struct inode
*inode
= file_inode(filp
);
1284 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1285 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
1286 return put_user(flags
, (int __user
*)arg
);
1289 static int f2fs_ioc_setflags(struct file
*filp
, unsigned long arg
)
1291 struct inode
*inode
= file_inode(filp
);
1292 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1293 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
1294 unsigned int oldflags
;
1297 ret
= mnt_want_write_file(filp
);
1301 if (!inode_owner_or_capable(inode
)) {
1306 if (get_user(flags
, (int __user
*)arg
)) {
1311 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
1315 oldflags
= fi
->i_flags
;
1317 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
1318 if (!capable(CAP_LINUX_IMMUTABLE
)) {
1319 inode_unlock(inode
);
1325 flags
= flags
& FS_FL_USER_MODIFIABLE
;
1326 flags
|= oldflags
& ~FS_FL_USER_MODIFIABLE
;
1327 fi
->i_flags
= flags
;
1328 inode_unlock(inode
);
1330 f2fs_set_inode_flags(inode
);
1331 inode
->i_ctime
= CURRENT_TIME
;
1332 mark_inode_dirty(inode
);
1334 mnt_drop_write_file(filp
);
1338 static int f2fs_ioc_getversion(struct file
*filp
, unsigned long arg
)
1340 struct inode
*inode
= file_inode(filp
);
1342 return put_user(inode
->i_generation
, (int __user
*)arg
);
1345 static int f2fs_ioc_start_atomic_write(struct file
*filp
)
1347 struct inode
*inode
= file_inode(filp
);
1350 if (!inode_owner_or_capable(inode
))
1353 if (f2fs_is_atomic_file(inode
))
1356 ret
= f2fs_convert_inline_inode(inode
);
1360 set_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
1361 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1366 static int f2fs_ioc_commit_atomic_write(struct file
*filp
)
1368 struct inode
*inode
= file_inode(filp
);
1371 if (!inode_owner_or_capable(inode
))
1374 if (f2fs_is_volatile_file(inode
))
1377 ret
= mnt_want_write_file(filp
);
1381 if (f2fs_is_atomic_file(inode
)) {
1382 clear_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
1383 ret
= commit_inmem_pages(inode
);
1385 set_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
1390 ret
= f2fs_sync_file(filp
, 0, LLONG_MAX
, 0);
1392 mnt_drop_write_file(filp
);
1396 static int f2fs_ioc_start_volatile_write(struct file
*filp
)
1398 struct inode
*inode
= file_inode(filp
);
1401 if (!inode_owner_or_capable(inode
))
1404 if (f2fs_is_volatile_file(inode
))
1407 ret
= f2fs_convert_inline_inode(inode
);
1411 set_inode_flag(F2FS_I(inode
), FI_VOLATILE_FILE
);
1412 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1416 static int f2fs_ioc_release_volatile_write(struct file
*filp
)
1418 struct inode
*inode
= file_inode(filp
);
1420 if (!inode_owner_or_capable(inode
))
1423 if (!f2fs_is_volatile_file(inode
))
1426 if (!f2fs_is_first_block_written(inode
))
1427 return truncate_partial_data_page(inode
, 0, true);
1429 return punch_hole(inode
, 0, F2FS_BLKSIZE
);
1432 static int f2fs_ioc_abort_volatile_write(struct file
*filp
)
1434 struct inode
*inode
= file_inode(filp
);
1437 if (!inode_owner_or_capable(inode
))
1440 ret
= mnt_want_write_file(filp
);
1444 if (f2fs_is_atomic_file(inode
)) {
1445 clear_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
1446 drop_inmem_pages(inode
);
1448 if (f2fs_is_volatile_file(inode
)) {
1449 clear_inode_flag(F2FS_I(inode
), FI_VOLATILE_FILE
);
1450 ret
= f2fs_sync_file(filp
, 0, LLONG_MAX
, 0);
1453 mnt_drop_write_file(filp
);
1454 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1458 static int f2fs_ioc_shutdown(struct file
*filp
, unsigned long arg
)
1460 struct inode
*inode
= file_inode(filp
);
1461 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1462 struct super_block
*sb
= sbi
->sb
;
1465 if (!capable(CAP_SYS_ADMIN
))
1468 if (get_user(in
, (__u32 __user
*)arg
))
1472 case F2FS_GOING_DOWN_FULLSYNC
:
1473 sb
= freeze_bdev(sb
->s_bdev
);
1474 if (sb
&& !IS_ERR(sb
)) {
1475 f2fs_stop_checkpoint(sbi
);
1476 thaw_bdev(sb
->s_bdev
, sb
);
1479 case F2FS_GOING_DOWN_METASYNC
:
1480 /* do checkpoint only */
1481 f2fs_sync_fs(sb
, 1);
1482 f2fs_stop_checkpoint(sbi
);
1484 case F2FS_GOING_DOWN_NOSYNC
:
1485 f2fs_stop_checkpoint(sbi
);
1487 case F2FS_GOING_DOWN_METAFLUSH
:
1488 sync_meta_pages(sbi
, META
, LONG_MAX
);
1489 f2fs_stop_checkpoint(sbi
);
1494 f2fs_update_time(sbi
, REQ_TIME
);
1498 static int f2fs_ioc_fitrim(struct file
*filp
, unsigned long arg
)
1500 struct inode
*inode
= file_inode(filp
);
1501 struct super_block
*sb
= inode
->i_sb
;
1502 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
1503 struct fstrim_range range
;
1506 if (!capable(CAP_SYS_ADMIN
))
1509 if (!blk_queue_discard(q
))
1512 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
1516 range
.minlen
= max((unsigned int)range
.minlen
,
1517 q
->limits
.discard_granularity
);
1518 ret
= f2fs_trim_fs(F2FS_SB(sb
), &range
);
1522 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
1525 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1529 static bool uuid_is_nonzero(__u8 u
[16])
1533 for (i
= 0; i
< 16; i
++)
1539 static int f2fs_ioc_set_encryption_policy(struct file
*filp
, unsigned long arg
)
1541 struct fscrypt_policy policy
;
1542 struct inode
*inode
= file_inode(filp
);
1544 if (copy_from_user(&policy
, (struct fscrypt_policy __user
*)arg
,
1548 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1549 return fscrypt_process_policy(inode
, &policy
);
1552 static int f2fs_ioc_get_encryption_policy(struct file
*filp
, unsigned long arg
)
1554 struct fscrypt_policy policy
;
1555 struct inode
*inode
= file_inode(filp
);
1558 err
= fscrypt_get_policy(inode
, &policy
);
1562 if (copy_to_user((struct fscrypt_policy __user
*)arg
, &policy
, sizeof(policy
)))
1567 static int f2fs_ioc_get_encryption_pwsalt(struct file
*filp
, unsigned long arg
)
1569 struct inode
*inode
= file_inode(filp
);
1570 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1573 if (!f2fs_sb_has_crypto(inode
->i_sb
))
1576 if (uuid_is_nonzero(sbi
->raw_super
->encrypt_pw_salt
))
1579 err
= mnt_want_write_file(filp
);
1583 /* update superblock with uuid */
1584 generate_random_uuid(sbi
->raw_super
->encrypt_pw_salt
);
1586 err
= f2fs_commit_super(sbi
, false);
1589 memset(sbi
->raw_super
->encrypt_pw_salt
, 0, 16);
1590 mnt_drop_write_file(filp
);
1593 mnt_drop_write_file(filp
);
1595 if (copy_to_user((__u8 __user
*)arg
, sbi
->raw_super
->encrypt_pw_salt
,
1601 static int f2fs_ioc_gc(struct file
*filp
, unsigned long arg
)
1603 struct inode
*inode
= file_inode(filp
);
1604 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1607 if (!capable(CAP_SYS_ADMIN
))
1610 if (get_user(sync
, (__u32 __user
*)arg
))
1613 if (f2fs_readonly(sbi
->sb
))
1617 if (!mutex_trylock(&sbi
->gc_mutex
))
1620 mutex_lock(&sbi
->gc_mutex
);
1623 return f2fs_gc(sbi
, sync
);
1626 static int f2fs_ioc_write_checkpoint(struct file
*filp
, unsigned long arg
)
1628 struct inode
*inode
= file_inode(filp
);
1629 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1631 if (!capable(CAP_SYS_ADMIN
))
1634 if (f2fs_readonly(sbi
->sb
))
1637 return f2fs_sync_fs(sbi
->sb
, 1);
1640 static int f2fs_defragment_range(struct f2fs_sb_info
*sbi
,
1642 struct f2fs_defragment
*range
)
1644 struct inode
*inode
= file_inode(filp
);
1645 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
};
1646 struct extent_info ei
;
1647 pgoff_t pg_start
, pg_end
;
1648 unsigned int blk_per_seg
= sbi
->blocks_per_seg
;
1649 unsigned int total
= 0, sec_num
;
1650 unsigned int pages_per_sec
= sbi
->segs_per_sec
* blk_per_seg
;
1651 block_t blk_end
= 0;
1652 bool fragmented
= false;
1655 /* if in-place-update policy is enabled, don't waste time here */
1656 if (need_inplace_update(inode
))
1659 pg_start
= range
->start
>> PAGE_SHIFT
;
1660 pg_end
= (range
->start
+ range
->len
) >> PAGE_SHIFT
;
1662 f2fs_balance_fs(sbi
, true);
1666 /* writeback all dirty pages in the range */
1667 err
= filemap_write_and_wait_range(inode
->i_mapping
, range
->start
,
1668 range
->start
+ range
->len
- 1);
1673 * lookup mapping info in extent cache, skip defragmenting if physical
1674 * block addresses are continuous.
1676 if (f2fs_lookup_extent_cache(inode
, pg_start
, &ei
)) {
1677 if (ei
.fofs
+ ei
.len
>= pg_end
)
1681 map
.m_lblk
= pg_start
;
1684 * lookup mapping info in dnode page cache, skip defragmenting if all
1685 * physical block addresses are continuous even if there are hole(s)
1686 * in logical blocks.
1688 while (map
.m_lblk
< pg_end
) {
1689 map
.m_len
= pg_end
- map
.m_lblk
;
1690 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_READ
);
1694 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
1699 if (blk_end
&& blk_end
!= map
.m_pblk
) {
1703 blk_end
= map
.m_pblk
+ map
.m_len
;
1705 map
.m_lblk
+= map
.m_len
;
1711 map
.m_lblk
= pg_start
;
1712 map
.m_len
= pg_end
- pg_start
;
1714 sec_num
= (map
.m_len
+ pages_per_sec
- 1) / pages_per_sec
;
1717 * make sure there are enough free section for LFS allocation, this can
1718 * avoid defragment running in SSR mode when free section are allocated
1721 if (has_not_enough_free_secs(sbi
, sec_num
)) {
1726 while (map
.m_lblk
< pg_end
) {
1731 map
.m_len
= pg_end
- map
.m_lblk
;
1732 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_READ
);
1736 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
1741 set_inode_flag(F2FS_I(inode
), FI_DO_DEFRAG
);
1744 while (idx
< map
.m_lblk
+ map
.m_len
&& cnt
< blk_per_seg
) {
1747 page
= get_lock_data_page(inode
, idx
, true);
1749 err
= PTR_ERR(page
);
1753 set_page_dirty(page
);
1754 f2fs_put_page(page
, 1);
1763 if (idx
< pg_end
&& cnt
< blk_per_seg
)
1766 clear_inode_flag(F2FS_I(inode
), FI_DO_DEFRAG
);
1768 err
= filemap_fdatawrite(inode
->i_mapping
);
1773 clear_inode_flag(F2FS_I(inode
), FI_DO_DEFRAG
);
1775 inode_unlock(inode
);
1777 range
->len
= (u64
)total
<< PAGE_SHIFT
;
1781 static int f2fs_ioc_defragment(struct file
*filp
, unsigned long arg
)
1783 struct inode
*inode
= file_inode(filp
);
1784 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1785 struct f2fs_defragment range
;
1788 if (!capable(CAP_SYS_ADMIN
))
1791 if (!S_ISREG(inode
->i_mode
))
1794 err
= mnt_want_write_file(filp
);
1798 if (f2fs_readonly(sbi
->sb
)) {
1803 if (copy_from_user(&range
, (struct f2fs_defragment __user
*)arg
,
1809 /* verify alignment of offset & size */
1810 if (range
.start
& (F2FS_BLKSIZE
- 1) ||
1811 range
.len
& (F2FS_BLKSIZE
- 1)) {
1816 err
= f2fs_defragment_range(sbi
, filp
, &range
);
1817 f2fs_update_time(sbi
, REQ_TIME
);
1821 if (copy_to_user((struct f2fs_defragment __user
*)arg
, &range
,
1825 mnt_drop_write_file(filp
);
1829 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1832 case F2FS_IOC_GETFLAGS
:
1833 return f2fs_ioc_getflags(filp
, arg
);
1834 case F2FS_IOC_SETFLAGS
:
1835 return f2fs_ioc_setflags(filp
, arg
);
1836 case F2FS_IOC_GETVERSION
:
1837 return f2fs_ioc_getversion(filp
, arg
);
1838 case F2FS_IOC_START_ATOMIC_WRITE
:
1839 return f2fs_ioc_start_atomic_write(filp
);
1840 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
1841 return f2fs_ioc_commit_atomic_write(filp
);
1842 case F2FS_IOC_START_VOLATILE_WRITE
:
1843 return f2fs_ioc_start_volatile_write(filp
);
1844 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
1845 return f2fs_ioc_release_volatile_write(filp
);
1846 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
1847 return f2fs_ioc_abort_volatile_write(filp
);
1848 case F2FS_IOC_SHUTDOWN
:
1849 return f2fs_ioc_shutdown(filp
, arg
);
1851 return f2fs_ioc_fitrim(filp
, arg
);
1852 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
1853 return f2fs_ioc_set_encryption_policy(filp
, arg
);
1854 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
1855 return f2fs_ioc_get_encryption_policy(filp
, arg
);
1856 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
1857 return f2fs_ioc_get_encryption_pwsalt(filp
, arg
);
1858 case F2FS_IOC_GARBAGE_COLLECT
:
1859 return f2fs_ioc_gc(filp
, arg
);
1860 case F2FS_IOC_WRITE_CHECKPOINT
:
1861 return f2fs_ioc_write_checkpoint(filp
, arg
);
1862 case F2FS_IOC_DEFRAGMENT
:
1863 return f2fs_ioc_defragment(filp
, arg
);
1869 static ssize_t
f2fs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1871 struct file
*file
= iocb
->ki_filp
;
1872 struct inode
*inode
= file_inode(file
);
1875 if (f2fs_encrypted_inode(inode
) &&
1876 !fscrypt_has_encryption_key(inode
) &&
1877 fscrypt_get_encryption_info(inode
))
1881 ret
= generic_write_checks(iocb
, from
);
1883 ret
= f2fs_preallocate_blocks(iocb
, from
);
1885 ret
= __generic_file_write_iter(iocb
, from
);
1887 inode_unlock(inode
);
1892 err
= generic_write_sync(file
, iocb
->ki_pos
- ret
, ret
);
1899 #ifdef CONFIG_COMPAT
1900 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1903 case F2FS_IOC32_GETFLAGS
:
1904 cmd
= F2FS_IOC_GETFLAGS
;
1906 case F2FS_IOC32_SETFLAGS
:
1907 cmd
= F2FS_IOC_SETFLAGS
;
1909 case F2FS_IOC32_GETVERSION
:
1910 cmd
= F2FS_IOC_GETVERSION
;
1912 case F2FS_IOC_START_ATOMIC_WRITE
:
1913 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
1914 case F2FS_IOC_START_VOLATILE_WRITE
:
1915 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
1916 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
1917 case F2FS_IOC_SHUTDOWN
:
1918 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
1919 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
1920 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
1921 case F2FS_IOC_GARBAGE_COLLECT
:
1922 case F2FS_IOC_WRITE_CHECKPOINT
:
1923 case F2FS_IOC_DEFRAGMENT
:
1926 return -ENOIOCTLCMD
;
1928 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
1932 const struct file_operations f2fs_file_operations
= {
1933 .llseek
= f2fs_llseek
,
1934 .read_iter
= generic_file_read_iter
,
1935 .write_iter
= f2fs_file_write_iter
,
1936 .open
= f2fs_file_open
,
1937 .release
= f2fs_release_file
,
1938 .mmap
= f2fs_file_mmap
,
1939 .fsync
= f2fs_sync_file
,
1940 .fallocate
= f2fs_fallocate
,
1941 .unlocked_ioctl
= f2fs_ioctl
,
1942 #ifdef CONFIG_COMPAT
1943 .compat_ioctl
= f2fs_compat_ioctl
,
1945 .splice_read
= generic_file_splice_read
,
1946 .splice_write
= iter_file_splice_write
,