1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/buffer_head.h>
12 #include <linux/writeback.h>
13 #include <linux/blkdev.h>
14 #include <linux/falloc.h>
15 #include <linux/types.h>
16 #include <linux/compat.h>
17 #include <linux/uaccess.h>
18 #include <linux/mount.h>
19 #include <linux/pagevec.h>
20 #include <linux/uio.h>
21 #include <linux/uuid.h>
22 #include <linux/file.h>
23 #include <linux/nls.h>
24 #include <linux/sched/signal.h>
25 #include <linux/fileattr.h>
26 #include <linux/fadvise.h>
27 #include <linux/iomap.h>
36 #include <trace/events/f2fs.h>
37 #include <uapi/linux/f2fs.h>
39 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
41 struct inode *inode = file_inode(vmf->vma->vm_file);
44 ret = filemap_fault(vmf);
46 f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
49 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
54 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
56 struct page *page = vmf->page;
57 struct inode *inode = file_inode(vmf->vma->vm_file);
58 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
59 struct dnode_of_data dn;
60 bool need_alloc = true;
63 if (unlikely(IS_IMMUTABLE(inode)))
64 return VM_FAULT_SIGBUS;
66 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
67 return VM_FAULT_SIGBUS;
69 if (unlikely(f2fs_cp_error(sbi))) {
74 if (!f2fs_is_checkpoint_ready(sbi)) {
79 err = f2fs_convert_inline_inode(inode);
83 #ifdef CONFIG_F2FS_FS_COMPRESSION
84 if (f2fs_compressed_file(inode)) {
85 int ret = f2fs_is_compressed_cluster(inode, page->index);
95 /* should do out of any locked page */
97 f2fs_balance_fs(sbi, true);
99 sb_start_pagefault(inode->i_sb);
101 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
103 file_update_time(vmf->vma->vm_file);
104 filemap_invalidate_lock_shared(inode->i_mapping);
106 if (unlikely(page->mapping != inode->i_mapping ||
107 page_offset(page) > i_size_read(inode) ||
108 !PageUptodate(page))) {
115 /* block allocation */
116 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
117 set_new_dnode(&dn, inode, NULL, NULL, 0);
118 err = f2fs_get_block(&dn, page->index);
119 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
122 #ifdef CONFIG_F2FS_FS_COMPRESSION
124 set_new_dnode(&dn, inode, NULL, NULL, 0);
125 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
134 f2fs_wait_on_page_writeback(page, DATA, false, true);
136 /* wait for GCed page writeback via META_MAPPING */
137 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
140 * check to see if the page is mapped already (no holes)
142 if (PageMappedToDisk(page))
145 /* page is wholly or partially inside EOF */
146 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
147 i_size_read(inode)) {
150 offset = i_size_read(inode) & ~PAGE_MASK;
151 zero_user_segment(page, offset, PAGE_SIZE);
153 set_page_dirty(page);
154 if (!PageUptodate(page))
155 SetPageUptodate(page);
157 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
158 f2fs_update_time(sbi, REQ_TIME);
160 trace_f2fs_vm_page_mkwrite(page, DATA);
162 filemap_invalidate_unlock_shared(inode->i_mapping);
164 sb_end_pagefault(inode->i_sb);
166 return block_page_mkwrite_return(err);
169 static const struct vm_operations_struct f2fs_file_vm_ops = {
170 .fault = f2fs_filemap_fault,
171 .map_pages = filemap_map_pages,
172 .page_mkwrite = f2fs_vm_page_mkwrite,
175 static int get_parent_ino(struct inode *inode, nid_t *pino)
177 struct dentry *dentry;
180 * Make sure to get the non-deleted alias. The alias associated with
181 * the open file descriptor being fsync()'ed may be deleted already.
183 dentry = d_find_alias(inode);
187 *pino = parent_ino(dentry);
192 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
194 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
195 enum cp_reason_type cp_reason = CP_NO_NEEDED;
197 if (!S_ISREG(inode->i_mode))
198 cp_reason = CP_NON_REGULAR;
199 else if (f2fs_compressed_file(inode))
200 cp_reason = CP_COMPRESSED;
201 else if (inode->i_nlink != 1)
202 cp_reason = CP_HARDLINK;
203 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
204 cp_reason = CP_SB_NEED_CP;
205 else if (file_wrong_pino(inode))
206 cp_reason = CP_WRONG_PINO;
207 else if (!f2fs_space_for_roll_forward(sbi))
208 cp_reason = CP_NO_SPC_ROLL;
209 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
210 cp_reason = CP_NODE_NEED_CP;
211 else if (test_opt(sbi, FASTBOOT))
212 cp_reason = CP_FASTBOOT_MODE;
213 else if (F2FS_OPTION(sbi).active_logs == 2)
214 cp_reason = CP_SPEC_LOG_NUM;
215 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
216 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
217 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
219 cp_reason = CP_RECOVER_DIR;
224 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
226 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
228 /* But we need to avoid that there are some inode updates */
229 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
235 static void try_to_fix_pino(struct inode *inode)
237 struct f2fs_inode_info *fi = F2FS_I(inode);
240 f2fs_down_write(&fi->i_sem);
241 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
242 get_parent_ino(inode, &pino)) {
243 f2fs_i_pino_write(inode, pino);
244 file_got_pino(inode);
246 f2fs_up_write(&fi->i_sem);
249 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
250 int datasync, bool atomic)
252 struct inode *inode = file->f_mapping->host;
253 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
254 nid_t ino = inode->i_ino;
256 enum cp_reason_type cp_reason = 0;
257 struct writeback_control wbc = {
258 .sync_mode = WB_SYNC_ALL,
259 .nr_to_write = LONG_MAX,
262 unsigned int seq_id = 0;
264 if (unlikely(f2fs_readonly(inode->i_sb)))
267 trace_f2fs_sync_file_enter(inode);
269 if (S_ISDIR(inode->i_mode))
272 /* if fdatasync is triggered, let's do in-place-update */
273 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
274 set_inode_flag(inode, FI_NEED_IPU);
275 ret = file_write_and_wait_range(file, start, end);
276 clear_inode_flag(inode, FI_NEED_IPU);
278 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
279 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
283 /* if the inode is dirty, let's recover all the time */
284 if (!f2fs_skip_inode_update(inode, datasync)) {
285 f2fs_write_inode(inode, NULL);
290 * if there is no written data, don't waste time to write recovery info.
292 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
293 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
295 /* it may call write_inode just prior to fsync */
296 if (need_inode_page_update(sbi, ino))
299 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
300 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
305 * for OPU case, during fsync(), node can be persisted before
306 * data when lower device doesn't support write barrier, result
307 * in data corruption after SPO.
308 * So for strict fsync mode, force to use atomic write sematics
309 * to keep write order in between data/node and last node to
310 * avoid potential data corruption.
312 if (F2FS_OPTION(sbi).fsync_mode ==
313 FSYNC_MODE_STRICT && !atomic)
318 * Both of fdatasync() and fsync() are able to be recovered from
321 f2fs_down_read(&F2FS_I(inode)->i_sem);
322 cp_reason = need_do_checkpoint(inode);
323 f2fs_up_read(&F2FS_I(inode)->i_sem);
326 /* all the dirty node pages should be flushed for POR */
327 ret = f2fs_sync_fs(inode->i_sb, 1);
330 * We've secured consistency through sync_fs. Following pino
331 * will be used only for fsynced inodes after checkpoint.
333 try_to_fix_pino(inode);
334 clear_inode_flag(inode, FI_APPEND_WRITE);
335 clear_inode_flag(inode, FI_UPDATE_WRITE);
339 atomic_inc(&sbi->wb_sync_req[NODE]);
340 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
341 atomic_dec(&sbi->wb_sync_req[NODE]);
345 /* if cp_error was enabled, we should avoid infinite loop */
346 if (unlikely(f2fs_cp_error(sbi))) {
351 if (f2fs_need_inode_block_update(sbi, ino)) {
352 f2fs_mark_inode_dirty_sync(inode, true);
353 f2fs_write_inode(inode, NULL);
358 * If it's atomic_write, it's just fine to keep write ordering. So
359 * here we don't need to wait for node write completion, since we use
360 * node chain which serializes node blocks. If one of node writes are
361 * reordered, we can see simply broken chain, resulting in stopping
362 * roll-forward recovery. It means we'll recover all or none node blocks
366 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
371 /* once recovery info is written, don't need to tack this */
372 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
373 clear_inode_flag(inode, FI_APPEND_WRITE);
375 if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) ||
376 (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi)))
377 ret = f2fs_issue_flush(sbi, inode->i_ino);
379 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
380 clear_inode_flag(inode, FI_UPDATE_WRITE);
381 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
383 f2fs_update_time(sbi, REQ_TIME);
385 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
389 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
391 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
393 return f2fs_do_sync_file(file, start, end, datasync, false);
396 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
397 pgoff_t index, int whence)
401 if (__is_valid_data_blkaddr(blkaddr))
403 if (blkaddr == NEW_ADDR &&
404 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
408 if (blkaddr == NULL_ADDR)
415 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
417 struct inode *inode = file->f_mapping->host;
418 loff_t maxbytes = inode->i_sb->s_maxbytes;
419 struct dnode_of_data dn;
420 pgoff_t pgofs, end_offset;
421 loff_t data_ofs = offset;
427 isize = i_size_read(inode);
431 /* handle inline data case */
432 if (f2fs_has_inline_data(inode)) {
433 if (whence == SEEK_HOLE) {
436 } else if (whence == SEEK_DATA) {
442 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
444 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
445 set_new_dnode(&dn, inode, NULL, NULL, 0);
446 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
447 if (err && err != -ENOENT) {
449 } else if (err == -ENOENT) {
450 /* direct node does not exists */
451 if (whence == SEEK_DATA) {
452 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
459 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
461 /* find data/hole in dnode block */
462 for (; dn.ofs_in_node < end_offset;
463 dn.ofs_in_node++, pgofs++,
464 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
467 blkaddr = f2fs_data_blkaddr(&dn);
469 if (__is_valid_data_blkaddr(blkaddr) &&
470 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
471 blkaddr, DATA_GENERIC_ENHANCE)) {
476 if (__found_offset(file->f_mapping, blkaddr,
485 if (whence == SEEK_DATA)
488 if (whence == SEEK_HOLE && data_ofs > isize)
491 return vfs_setpos(file, data_ofs, maxbytes);
497 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
499 struct inode *inode = file->f_mapping->host;
500 loff_t maxbytes = inode->i_sb->s_maxbytes;
502 if (f2fs_compressed_file(inode))
503 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
509 return generic_file_llseek_size(file, offset, whence,
510 maxbytes, i_size_read(inode));
515 return f2fs_seek_block(file, offset, whence);
521 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
523 struct inode *inode = file_inode(file);
525 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
528 if (!f2fs_is_compress_backend_ready(inode))
532 vma->vm_ops = &f2fs_file_vm_ops;
533 set_inode_flag(inode, FI_MMAP_FILE);
537 static int f2fs_file_open(struct inode *inode, struct file *filp)
539 int err = fscrypt_file_open(inode, filp);
544 if (!f2fs_is_compress_backend_ready(inode))
547 err = fsverity_file_open(inode, filp);
551 filp->f_mode |= FMODE_NOWAIT;
553 return dquot_file_open(inode, filp);
556 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
558 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
559 struct f2fs_node *raw_node;
560 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
563 bool compressed_cluster = false;
564 int cluster_index = 0, valid_blocks = 0;
565 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
566 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
568 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
569 base = get_extra_isize(dn->inode);
571 raw_node = F2FS_NODE(dn->node_page);
572 addr = blkaddr_in_node(raw_node) + base + ofs;
574 /* Assumption: truncateion starts with cluster */
575 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
576 block_t blkaddr = le32_to_cpu(*addr);
578 if (f2fs_compressed_file(dn->inode) &&
579 !(cluster_index & (cluster_size - 1))) {
580 if (compressed_cluster)
581 f2fs_i_compr_blocks_update(dn->inode,
582 valid_blocks, false);
583 compressed_cluster = (blkaddr == COMPRESS_ADDR);
587 if (blkaddr == NULL_ADDR)
590 dn->data_blkaddr = NULL_ADDR;
591 f2fs_set_data_blkaddr(dn);
593 if (__is_valid_data_blkaddr(blkaddr)) {
594 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
595 DATA_GENERIC_ENHANCE))
597 if (compressed_cluster)
601 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
602 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
604 f2fs_invalidate_blocks(sbi, blkaddr);
606 if (!released || blkaddr != COMPRESS_ADDR)
610 if (compressed_cluster)
611 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
616 * once we invalidate valid blkaddr in range [ofs, ofs + count],
617 * we will invalidate all blkaddr in the whole range.
619 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
621 f2fs_update_extent_cache_range(dn, fofs, 0, len);
622 dec_valid_block_count(sbi, dn->inode, nr_free);
624 dn->ofs_in_node = ofs;
626 f2fs_update_time(sbi, REQ_TIME);
627 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
628 dn->ofs_in_node, nr_free);
631 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
633 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
636 static int truncate_partial_data_page(struct inode *inode, u64 from,
639 loff_t offset = from & (PAGE_SIZE - 1);
640 pgoff_t index = from >> PAGE_SHIFT;
641 struct address_space *mapping = inode->i_mapping;
644 if (!offset && !cache_only)
648 page = find_lock_page(mapping, index);
649 if (page && PageUptodate(page))
651 f2fs_put_page(page, 1);
655 page = f2fs_get_lock_data_page(inode, index, true);
657 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
659 f2fs_wait_on_page_writeback(page, DATA, true, true);
660 zero_user(page, offset, PAGE_SIZE - offset);
662 /* An encrypted inode should have a key and truncate the last page. */
663 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
665 set_page_dirty(page);
666 f2fs_put_page(page, 1);
670 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
672 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
673 struct dnode_of_data dn;
675 int count = 0, err = 0;
677 bool truncate_page = false;
679 trace_f2fs_truncate_blocks_enter(inode, from);
681 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
683 if (free_from >= max_file_blocks(inode))
689 ipage = f2fs_get_node_page(sbi, inode->i_ino);
691 err = PTR_ERR(ipage);
695 if (f2fs_has_inline_data(inode)) {
696 f2fs_truncate_inline_inode(inode, ipage, from);
697 f2fs_put_page(ipage, 1);
698 truncate_page = true;
702 set_new_dnode(&dn, inode, ipage, NULL, 0);
703 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
710 count = ADDRS_PER_PAGE(dn.node_page, inode);
712 count -= dn.ofs_in_node;
713 f2fs_bug_on(sbi, count < 0);
715 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
716 f2fs_truncate_data_blocks_range(&dn, count);
722 err = f2fs_truncate_inode_blocks(inode, free_from);
727 /* lastly zero out the first data page */
729 err = truncate_partial_data_page(inode, from, truncate_page);
731 trace_f2fs_truncate_blocks_exit(inode, err);
735 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
737 u64 free_from = from;
740 #ifdef CONFIG_F2FS_FS_COMPRESSION
742 * for compressed file, only support cluster size
743 * aligned truncation.
745 if (f2fs_compressed_file(inode))
746 free_from = round_up(from,
747 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
750 err = f2fs_do_truncate_blocks(inode, free_from, lock);
754 #ifdef CONFIG_F2FS_FS_COMPRESSION
756 * For compressed file, after release compress blocks, don't allow write
757 * direct, but we should allow write direct after truncate to zero.
759 if (f2fs_compressed_file(inode) && !free_from
760 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
761 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
763 if (from != free_from) {
764 err = f2fs_truncate_partial_cluster(inode, from, lock);
773 int f2fs_truncate(struct inode *inode)
777 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
780 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
781 S_ISLNK(inode->i_mode)))
784 trace_f2fs_truncate(inode);
786 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
787 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
791 err = f2fs_dquot_initialize(inode);
795 /* we should check inline_data size */
796 if (!f2fs_may_inline_data(inode)) {
797 err = f2fs_convert_inline_inode(inode);
802 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
806 inode->i_mtime = inode->i_ctime = current_time(inode);
807 f2fs_mark_inode_dirty_sync(inode, false);
811 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
812 struct kstat *stat, u32 request_mask, unsigned int query_flags)
814 struct inode *inode = d_inode(path->dentry);
815 struct f2fs_inode_info *fi = F2FS_I(inode);
816 struct f2fs_inode *ri = NULL;
819 if (f2fs_has_extra_attr(inode) &&
820 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
821 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
822 stat->result_mask |= STATX_BTIME;
823 stat->btime.tv_sec = fi->i_crtime.tv_sec;
824 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
828 if (flags & F2FS_COMPR_FL)
829 stat->attributes |= STATX_ATTR_COMPRESSED;
830 if (flags & F2FS_APPEND_FL)
831 stat->attributes |= STATX_ATTR_APPEND;
832 if (IS_ENCRYPTED(inode))
833 stat->attributes |= STATX_ATTR_ENCRYPTED;
834 if (flags & F2FS_IMMUTABLE_FL)
835 stat->attributes |= STATX_ATTR_IMMUTABLE;
836 if (flags & F2FS_NODUMP_FL)
837 stat->attributes |= STATX_ATTR_NODUMP;
838 if (IS_VERITY(inode))
839 stat->attributes |= STATX_ATTR_VERITY;
841 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
843 STATX_ATTR_ENCRYPTED |
844 STATX_ATTR_IMMUTABLE |
848 generic_fillattr(mnt_userns, inode, stat);
850 /* we need to show initial sectors used for inline_data/dentries */
851 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
852 f2fs_has_inline_dentry(inode))
853 stat->blocks += (stat->size + 511) >> 9;
858 #ifdef CONFIG_F2FS_FS_POSIX_ACL
859 static void __setattr_copy(struct user_namespace *mnt_userns,
860 struct inode *inode, const struct iattr *attr)
862 unsigned int ia_valid = attr->ia_valid;
864 i_uid_update(mnt_userns, attr, inode);
865 i_gid_update(mnt_userns, attr, inode);
866 if (ia_valid & ATTR_ATIME)
867 inode->i_atime = attr->ia_atime;
868 if (ia_valid & ATTR_MTIME)
869 inode->i_mtime = attr->ia_mtime;
870 if (ia_valid & ATTR_CTIME)
871 inode->i_ctime = attr->ia_ctime;
872 if (ia_valid & ATTR_MODE) {
873 umode_t mode = attr->ia_mode;
874 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
876 if (!in_group_p(kgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
878 set_acl_inode(inode, mode);
882 #define __setattr_copy setattr_copy
885 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
888 struct inode *inode = d_inode(dentry);
891 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
894 if (unlikely(IS_IMMUTABLE(inode)))
897 if (unlikely(IS_APPEND(inode) &&
898 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
899 ATTR_GID | ATTR_TIMES_SET))))
902 if ((attr->ia_valid & ATTR_SIZE) &&
903 !f2fs_is_compress_backend_ready(inode))
906 err = setattr_prepare(mnt_userns, dentry, attr);
910 err = fscrypt_prepare_setattr(dentry, attr);
914 err = fsverity_prepare_setattr(dentry, attr);
918 if (is_quota_modification(mnt_userns, inode, attr)) {
919 err = f2fs_dquot_initialize(inode);
923 if (i_uid_needs_update(mnt_userns, attr, inode) ||
924 i_gid_needs_update(mnt_userns, attr, inode)) {
925 f2fs_lock_op(F2FS_I_SB(inode));
926 err = dquot_transfer(mnt_userns, inode, attr);
928 set_sbi_flag(F2FS_I_SB(inode),
929 SBI_QUOTA_NEED_REPAIR);
930 f2fs_unlock_op(F2FS_I_SB(inode));
934 * update uid/gid under lock_op(), so that dquot and inode can
935 * be updated atomically.
937 i_uid_update(mnt_userns, attr, inode);
938 i_gid_update(mnt_userns, attr, inode);
939 f2fs_mark_inode_dirty_sync(inode, true);
940 f2fs_unlock_op(F2FS_I_SB(inode));
943 if (attr->ia_valid & ATTR_SIZE) {
944 loff_t old_size = i_size_read(inode);
946 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
948 * should convert inline inode before i_size_write to
949 * keep smaller than inline_data size with inline flag.
951 err = f2fs_convert_inline_inode(inode);
956 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
957 filemap_invalidate_lock(inode->i_mapping);
959 truncate_setsize(inode, attr->ia_size);
961 if (attr->ia_size <= old_size)
962 err = f2fs_truncate(inode);
964 * do not trim all blocks after i_size if target size is
965 * larger than i_size.
967 filemap_invalidate_unlock(inode->i_mapping);
968 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
972 spin_lock(&F2FS_I(inode)->i_size_lock);
973 inode->i_mtime = inode->i_ctime = current_time(inode);
974 F2FS_I(inode)->last_disk_size = i_size_read(inode);
975 spin_unlock(&F2FS_I(inode)->i_size_lock);
978 __setattr_copy(mnt_userns, inode, attr);
980 if (attr->ia_valid & ATTR_MODE) {
981 err = posix_acl_chmod(mnt_userns, inode, f2fs_get_inode_mode(inode));
983 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
985 inode->i_mode = F2FS_I(inode)->i_acl_mode;
986 clear_inode_flag(inode, FI_ACL_MODE);
990 /* file size may changed here */
991 f2fs_mark_inode_dirty_sync(inode, true);
993 /* inode change will produce dirty node pages flushed by checkpoint */
994 f2fs_balance_fs(F2FS_I_SB(inode), true);
999 const struct inode_operations f2fs_file_inode_operations = {
1000 .getattr = f2fs_getattr,
1001 .setattr = f2fs_setattr,
1002 .get_acl = f2fs_get_acl,
1003 .set_acl = f2fs_set_acl,
1004 .listxattr = f2fs_listxattr,
1005 .fiemap = f2fs_fiemap,
1006 .fileattr_get = f2fs_fileattr_get,
1007 .fileattr_set = f2fs_fileattr_set,
1010 static int fill_zero(struct inode *inode, pgoff_t index,
1011 loff_t start, loff_t len)
1013 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1019 f2fs_balance_fs(sbi, true);
1022 page = f2fs_get_new_data_page(inode, NULL, index, false);
1023 f2fs_unlock_op(sbi);
1026 return PTR_ERR(page);
1028 f2fs_wait_on_page_writeback(page, DATA, true, true);
1029 zero_user(page, start, len);
1030 set_page_dirty(page);
1031 f2fs_put_page(page, 1);
1035 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1039 while (pg_start < pg_end) {
1040 struct dnode_of_data dn;
1041 pgoff_t end_offset, count;
1043 set_new_dnode(&dn, inode, NULL, NULL, 0);
1044 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1046 if (err == -ENOENT) {
1047 pg_start = f2fs_get_next_page_offset(&dn,
1054 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1055 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1057 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1059 f2fs_truncate_data_blocks_range(&dn, count);
1060 f2fs_put_dnode(&dn);
1067 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1069 pgoff_t pg_start, pg_end;
1070 loff_t off_start, off_end;
1073 ret = f2fs_convert_inline_inode(inode);
1077 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1078 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1080 off_start = offset & (PAGE_SIZE - 1);
1081 off_end = (offset + len) & (PAGE_SIZE - 1);
1083 if (pg_start == pg_end) {
1084 ret = fill_zero(inode, pg_start, off_start,
1085 off_end - off_start);
1090 ret = fill_zero(inode, pg_start++, off_start,
1091 PAGE_SIZE - off_start);
1096 ret = fill_zero(inode, pg_end, 0, off_end);
1101 if (pg_start < pg_end) {
1102 loff_t blk_start, blk_end;
1103 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1105 f2fs_balance_fs(sbi, true);
1107 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1108 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1110 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1111 filemap_invalidate_lock(inode->i_mapping);
1113 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1116 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1117 f2fs_unlock_op(sbi);
1119 filemap_invalidate_unlock(inode->i_mapping);
1120 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1127 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1128 int *do_replace, pgoff_t off, pgoff_t len)
1130 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1131 struct dnode_of_data dn;
1135 set_new_dnode(&dn, inode, NULL, NULL, 0);
1136 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1137 if (ret && ret != -ENOENT) {
1139 } else if (ret == -ENOENT) {
1140 if (dn.max_level == 0)
1142 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1143 dn.ofs_in_node, len);
1149 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1150 dn.ofs_in_node, len);
1151 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1152 *blkaddr = f2fs_data_blkaddr(&dn);
1154 if (__is_valid_data_blkaddr(*blkaddr) &&
1155 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1156 DATA_GENERIC_ENHANCE)) {
1157 f2fs_put_dnode(&dn);
1158 return -EFSCORRUPTED;
1161 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1163 if (f2fs_lfs_mode(sbi)) {
1164 f2fs_put_dnode(&dn);
1168 /* do not invalidate this block address */
1169 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1173 f2fs_put_dnode(&dn);
1182 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1183 int *do_replace, pgoff_t off, int len)
1185 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1186 struct dnode_of_data dn;
1189 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1190 if (*do_replace == 0)
1193 set_new_dnode(&dn, inode, NULL, NULL, 0);
1194 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1196 dec_valid_block_count(sbi, inode, 1);
1197 f2fs_invalidate_blocks(sbi, *blkaddr);
1199 f2fs_update_data_blkaddr(&dn, *blkaddr);
1201 f2fs_put_dnode(&dn);
1206 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1207 block_t *blkaddr, int *do_replace,
1208 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1210 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1215 if (blkaddr[i] == NULL_ADDR && !full) {
1220 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1221 struct dnode_of_data dn;
1222 struct node_info ni;
1226 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1227 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1231 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1233 f2fs_put_dnode(&dn);
1237 ilen = min((pgoff_t)
1238 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1239 dn.ofs_in_node, len - i);
1241 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1242 f2fs_truncate_data_blocks_range(&dn, 1);
1244 if (do_replace[i]) {
1245 f2fs_i_blocks_write(src_inode,
1247 f2fs_i_blocks_write(dst_inode,
1249 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1250 blkaddr[i], ni.version, true, false);
1256 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1257 if (dst_inode->i_size < new_size)
1258 f2fs_i_size_write(dst_inode, new_size);
1259 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1261 f2fs_put_dnode(&dn);
1263 struct page *psrc, *pdst;
1265 psrc = f2fs_get_lock_data_page(src_inode,
1268 return PTR_ERR(psrc);
1269 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1272 f2fs_put_page(psrc, 1);
1273 return PTR_ERR(pdst);
1275 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1276 set_page_dirty(pdst);
1277 f2fs_put_page(pdst, 1);
1278 f2fs_put_page(psrc, 1);
1280 ret = f2fs_truncate_hole(src_inode,
1281 src + i, src + i + 1);
1290 static int __exchange_data_block(struct inode *src_inode,
1291 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1292 pgoff_t len, bool full)
1294 block_t *src_blkaddr;
1300 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1302 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1303 array_size(olen, sizeof(block_t)),
1308 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1309 array_size(olen, sizeof(int)),
1312 kvfree(src_blkaddr);
1316 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1317 do_replace, src, olen);
1321 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1322 do_replace, src, dst, olen, full);
1330 kvfree(src_blkaddr);
1336 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1337 kvfree(src_blkaddr);
1342 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1344 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1345 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1346 pgoff_t start = offset >> PAGE_SHIFT;
1347 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1350 f2fs_balance_fs(sbi, true);
1352 /* avoid gc operation during block exchange */
1353 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1354 filemap_invalidate_lock(inode->i_mapping);
1357 f2fs_drop_extent_tree(inode);
1358 truncate_pagecache(inode, offset);
1359 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1360 f2fs_unlock_op(sbi);
1362 filemap_invalidate_unlock(inode->i_mapping);
1363 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1367 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1372 if (offset + len >= i_size_read(inode))
1375 /* collapse range should be aligned to block size of f2fs. */
1376 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1379 ret = f2fs_convert_inline_inode(inode);
1383 /* write out all dirty pages from offset */
1384 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1388 ret = f2fs_do_collapse(inode, offset, len);
1392 /* write out all moved pages, if possible */
1393 filemap_invalidate_lock(inode->i_mapping);
1394 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1395 truncate_pagecache(inode, offset);
1397 new_size = i_size_read(inode) - len;
1398 ret = f2fs_truncate_blocks(inode, new_size, true);
1399 filemap_invalidate_unlock(inode->i_mapping);
1401 f2fs_i_size_write(inode, new_size);
1405 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1408 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1409 pgoff_t index = start;
1410 unsigned int ofs_in_node = dn->ofs_in_node;
1414 for (; index < end; index++, dn->ofs_in_node++) {
1415 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1419 dn->ofs_in_node = ofs_in_node;
1420 ret = f2fs_reserve_new_blocks(dn, count);
1424 dn->ofs_in_node = ofs_in_node;
1425 for (index = start; index < end; index++, dn->ofs_in_node++) {
1426 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1428 * f2fs_reserve_new_blocks will not guarantee entire block
1431 if (dn->data_blkaddr == NULL_ADDR) {
1436 if (dn->data_blkaddr == NEW_ADDR)
1439 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1440 DATA_GENERIC_ENHANCE)) {
1441 ret = -EFSCORRUPTED;
1445 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1446 dn->data_blkaddr = NEW_ADDR;
1447 f2fs_set_data_blkaddr(dn);
1450 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1455 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1458 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1459 struct address_space *mapping = inode->i_mapping;
1460 pgoff_t index, pg_start, pg_end;
1461 loff_t new_size = i_size_read(inode);
1462 loff_t off_start, off_end;
1465 ret = inode_newsize_ok(inode, (len + offset));
1469 ret = f2fs_convert_inline_inode(inode);
1473 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1477 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1478 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1480 off_start = offset & (PAGE_SIZE - 1);
1481 off_end = (offset + len) & (PAGE_SIZE - 1);
1483 if (pg_start == pg_end) {
1484 ret = fill_zero(inode, pg_start, off_start,
1485 off_end - off_start);
1489 new_size = max_t(loff_t, new_size, offset + len);
1492 ret = fill_zero(inode, pg_start++, off_start,
1493 PAGE_SIZE - off_start);
1497 new_size = max_t(loff_t, new_size,
1498 (loff_t)pg_start << PAGE_SHIFT);
1501 for (index = pg_start; index < pg_end;) {
1502 struct dnode_of_data dn;
1503 unsigned int end_offset;
1506 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1507 filemap_invalidate_lock(mapping);
1509 truncate_pagecache_range(inode,
1510 (loff_t)index << PAGE_SHIFT,
1511 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1515 set_new_dnode(&dn, inode, NULL, NULL, 0);
1516 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1518 f2fs_unlock_op(sbi);
1519 filemap_invalidate_unlock(mapping);
1520 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1524 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1525 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1527 ret = f2fs_do_zero_range(&dn, index, end);
1528 f2fs_put_dnode(&dn);
1530 f2fs_unlock_op(sbi);
1531 filemap_invalidate_unlock(mapping);
1532 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1534 f2fs_balance_fs(sbi, dn.node_changed);
1540 new_size = max_t(loff_t, new_size,
1541 (loff_t)index << PAGE_SHIFT);
1545 ret = fill_zero(inode, pg_end, 0, off_end);
1549 new_size = max_t(loff_t, new_size, offset + len);
1554 if (new_size > i_size_read(inode)) {
1555 if (mode & FALLOC_FL_KEEP_SIZE)
1556 file_set_keep_isize(inode);
1558 f2fs_i_size_write(inode, new_size);
1563 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1565 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1566 struct address_space *mapping = inode->i_mapping;
1567 pgoff_t nr, pg_start, pg_end, delta, idx;
1571 new_size = i_size_read(inode) + len;
1572 ret = inode_newsize_ok(inode, new_size);
1576 if (offset >= i_size_read(inode))
1579 /* insert range should be aligned to block size of f2fs. */
1580 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1583 ret = f2fs_convert_inline_inode(inode);
1587 f2fs_balance_fs(sbi, true);
1589 filemap_invalidate_lock(mapping);
1590 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1591 filemap_invalidate_unlock(mapping);
1595 /* write out all dirty pages from offset */
1596 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1600 pg_start = offset >> PAGE_SHIFT;
1601 pg_end = (offset + len) >> PAGE_SHIFT;
1602 delta = pg_end - pg_start;
1603 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1605 /* avoid gc operation during block exchange */
1606 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1607 filemap_invalidate_lock(mapping);
1608 truncate_pagecache(inode, offset);
1610 while (!ret && idx > pg_start) {
1611 nr = idx - pg_start;
1617 f2fs_drop_extent_tree(inode);
1619 ret = __exchange_data_block(inode, inode, idx,
1620 idx + delta, nr, false);
1621 f2fs_unlock_op(sbi);
1623 filemap_invalidate_unlock(mapping);
1624 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1626 /* write out all moved pages, if possible */
1627 filemap_invalidate_lock(mapping);
1628 filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1629 truncate_pagecache(inode, offset);
1630 filemap_invalidate_unlock(mapping);
1633 f2fs_i_size_write(inode, new_size);
1637 static int expand_inode_data(struct inode *inode, loff_t offset,
1638 loff_t len, int mode)
1640 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1641 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1642 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1643 .m_may_create = true };
1644 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1645 .init_gc_type = FG_GC,
1646 .should_migrate_blocks = false,
1647 .err_gc_skipped = true,
1648 .nr_free_secs = 0 };
1649 pgoff_t pg_start, pg_end;
1650 loff_t new_size = i_size_read(inode);
1652 block_t expanded = 0;
1655 err = inode_newsize_ok(inode, (len + offset));
1659 err = f2fs_convert_inline_inode(inode);
1663 f2fs_balance_fs(sbi, true);
1665 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1666 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1667 off_end = (offset + len) & (PAGE_SIZE - 1);
1669 map.m_lblk = pg_start;
1670 map.m_len = pg_end - pg_start;
1677 if (f2fs_is_pinned_file(inode)) {
1678 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1679 block_t sec_len = roundup(map.m_len, sec_blks);
1681 map.m_len = sec_blks;
1683 if (has_not_enough_free_secs(sbi, 0,
1684 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1685 f2fs_down_write(&sbi->gc_lock);
1686 err = f2fs_gc(sbi, &gc_control);
1687 if (err && err != -ENODATA)
1691 f2fs_down_write(&sbi->pin_sem);
1694 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1695 f2fs_unlock_op(sbi);
1697 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1698 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1699 file_dont_truncate(inode);
1701 f2fs_up_write(&sbi->pin_sem);
1703 expanded += map.m_len;
1704 sec_len -= map.m_len;
1705 map.m_lblk += map.m_len;
1706 if (!err && sec_len)
1709 map.m_len = expanded;
1711 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1712 expanded = map.m_len;
1721 last_off = pg_start + expanded - 1;
1723 /* update new size to the failed position */
1724 new_size = (last_off == pg_end) ? offset + len :
1725 (loff_t)(last_off + 1) << PAGE_SHIFT;
1727 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1730 if (new_size > i_size_read(inode)) {
1731 if (mode & FALLOC_FL_KEEP_SIZE)
1732 file_set_keep_isize(inode);
1734 f2fs_i_size_write(inode, new_size);
1740 static long f2fs_fallocate(struct file *file, int mode,
1741 loff_t offset, loff_t len)
1743 struct inode *inode = file_inode(file);
1746 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1748 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1750 if (!f2fs_is_compress_backend_ready(inode))
1753 /* f2fs only support ->fallocate for regular file */
1754 if (!S_ISREG(inode->i_mode))
1757 if (IS_ENCRYPTED(inode) &&
1758 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1762 * Pinned file should not support partial trucation since the block
1763 * can be used by applications.
1765 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1766 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1767 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1770 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1771 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1772 FALLOC_FL_INSERT_RANGE))
1777 ret = file_modified(file);
1781 if (mode & FALLOC_FL_PUNCH_HOLE) {
1782 if (offset >= inode->i_size)
1785 ret = punch_hole(inode, offset, len);
1786 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1787 ret = f2fs_collapse_range(inode, offset, len);
1788 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1789 ret = f2fs_zero_range(inode, offset, len, mode);
1790 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1791 ret = f2fs_insert_range(inode, offset, len);
1793 ret = expand_inode_data(inode, offset, len, mode);
1797 inode->i_mtime = inode->i_ctime = current_time(inode);
1798 f2fs_mark_inode_dirty_sync(inode, false);
1799 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1803 inode_unlock(inode);
1805 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1809 static int f2fs_release_file(struct inode *inode, struct file *filp)
1812 * f2fs_relase_file is called at every close calls. So we should
1813 * not drop any inmemory pages by close called by other process.
1815 if (!(filp->f_mode & FMODE_WRITE) ||
1816 atomic_read(&inode->i_writecount) != 1)
1819 f2fs_abort_atomic_write(inode, true);
1823 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1825 struct inode *inode = file_inode(file);
1828 * If the process doing a transaction is crashed, we should do
1829 * roll-back. Otherwise, other reader/write can see corrupted database
1830 * until all the writers close its file. Since this should be done
1831 * before dropping file lock, it needs to do in ->flush.
1833 if (F2FS_I(inode)->atomic_write_task == current)
1834 f2fs_abort_atomic_write(inode, true);
1838 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1840 struct f2fs_inode_info *fi = F2FS_I(inode);
1841 u32 masked_flags = fi->i_flags & mask;
1843 /* mask can be shrunk by flags_valid selector */
1846 /* Is it quota file? Do not allow user to mess with it */
1847 if (IS_NOQUOTA(inode))
1850 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1851 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1853 if (!f2fs_empty_dir(inode))
1857 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1858 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1860 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1864 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1865 if (masked_flags & F2FS_COMPR_FL) {
1866 if (!f2fs_disable_compressed_file(inode))
1869 if (!f2fs_may_compress(inode))
1871 if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
1873 if (set_compress_context(inode))
1878 fi->i_flags = iflags | (fi->i_flags & ~mask);
1879 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1880 (fi->i_flags & F2FS_NOCOMP_FL));
1882 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1883 set_inode_flag(inode, FI_PROJ_INHERIT);
1885 clear_inode_flag(inode, FI_PROJ_INHERIT);
1887 inode->i_ctime = current_time(inode);
1888 f2fs_set_inode_flags(inode);
1889 f2fs_mark_inode_dirty_sync(inode, true);
1893 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1896 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1897 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1898 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1899 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1901 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1902 * FS_IOC_FSSETXATTR is done by the VFS.
1905 static const struct {
1908 } f2fs_fsflags_map[] = {
1909 { F2FS_COMPR_FL, FS_COMPR_FL },
1910 { F2FS_SYNC_FL, FS_SYNC_FL },
1911 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1912 { F2FS_APPEND_FL, FS_APPEND_FL },
1913 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1914 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1915 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1916 { F2FS_INDEX_FL, FS_INDEX_FL },
1917 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1918 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1919 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1922 #define F2FS_GETTABLE_FS_FL ( \
1932 FS_PROJINHERIT_FL | \
1934 FS_INLINE_DATA_FL | \
1939 #define F2FS_SETTABLE_FS_FL ( \
1948 FS_PROJINHERIT_FL | \
1951 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1952 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1957 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1958 if (iflags & f2fs_fsflags_map[i].iflag)
1959 fsflags |= f2fs_fsflags_map[i].fsflag;
1964 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1965 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1970 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1971 if (fsflags & f2fs_fsflags_map[i].fsflag)
1972 iflags |= f2fs_fsflags_map[i].iflag;
1977 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1979 struct inode *inode = file_inode(filp);
1981 return put_user(inode->i_generation, (int __user *)arg);
1984 static int f2fs_ioc_start_atomic_write(struct file *filp)
1986 struct inode *inode = file_inode(filp);
1987 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
1988 struct f2fs_inode_info *fi = F2FS_I(inode);
1989 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1990 struct inode *pinode;
1993 if (!inode_owner_or_capable(mnt_userns, inode))
1996 if (!S_ISREG(inode->i_mode))
1999 if (filp->f_flags & O_DIRECT)
2002 ret = mnt_want_write_file(filp);
2008 if (!f2fs_disable_compressed_file(inode)) {
2013 if (f2fs_is_atomic_file(inode))
2016 ret = f2fs_convert_inline_inode(inode);
2020 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2023 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2024 * f2fs_is_atomic_file.
2026 if (get_dirty_pages(inode))
2027 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2028 inode->i_ino, get_dirty_pages(inode));
2029 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2031 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2035 /* Create a COW inode for atomic write */
2036 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2037 if (IS_ERR(pinode)) {
2038 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2039 ret = PTR_ERR(pinode);
2043 ret = f2fs_get_tmpfile(mnt_userns, pinode, &fi->cow_inode);
2046 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2049 f2fs_i_size_write(fi->cow_inode, i_size_read(inode));
2051 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2052 sbi->atomic_files++;
2053 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2055 set_inode_flag(inode, FI_ATOMIC_FILE);
2056 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2057 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2058 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2060 f2fs_update_time(sbi, REQ_TIME);
2061 fi->atomic_write_task = current;
2062 stat_update_max_atomic_write(inode);
2063 fi->atomic_write_cnt = 0;
2065 inode_unlock(inode);
2066 mnt_drop_write_file(filp);
2070 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2072 struct inode *inode = file_inode(filp);
2073 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2076 if (!inode_owner_or_capable(mnt_userns, inode))
2079 ret = mnt_want_write_file(filp);
2083 f2fs_balance_fs(F2FS_I_SB(inode), true);
2087 if (f2fs_is_atomic_file(inode)) {
2088 ret = f2fs_commit_atomic_write(inode);
2092 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2094 f2fs_abort_atomic_write(inode, false);
2096 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2099 inode_unlock(inode);
2100 mnt_drop_write_file(filp);
2104 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2106 struct inode *inode = file_inode(filp);
2107 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2110 if (!inode_owner_or_capable(mnt_userns, inode))
2113 ret = mnt_want_write_file(filp);
2119 f2fs_abort_atomic_write(inode, true);
2121 inode_unlock(inode);
2123 mnt_drop_write_file(filp);
2124 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2128 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2130 struct inode *inode = file_inode(filp);
2131 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2132 struct super_block *sb = sbi->sb;
2136 if (!capable(CAP_SYS_ADMIN))
2139 if (get_user(in, (__u32 __user *)arg))
2142 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2143 ret = mnt_want_write_file(filp);
2145 if (ret == -EROFS) {
2147 f2fs_stop_checkpoint(sbi, false);
2148 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2149 trace_f2fs_shutdown(sbi, in, ret);
2156 case F2FS_GOING_DOWN_FULLSYNC:
2157 ret = freeze_bdev(sb->s_bdev);
2160 f2fs_stop_checkpoint(sbi, false);
2161 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2162 thaw_bdev(sb->s_bdev);
2164 case F2FS_GOING_DOWN_METASYNC:
2165 /* do checkpoint only */
2166 ret = f2fs_sync_fs(sb, 1);
2169 f2fs_stop_checkpoint(sbi, false);
2170 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2172 case F2FS_GOING_DOWN_NOSYNC:
2173 f2fs_stop_checkpoint(sbi, false);
2174 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2176 case F2FS_GOING_DOWN_METAFLUSH:
2177 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2178 f2fs_stop_checkpoint(sbi, false);
2179 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2181 case F2FS_GOING_DOWN_NEED_FSCK:
2182 set_sbi_flag(sbi, SBI_NEED_FSCK);
2183 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2184 set_sbi_flag(sbi, SBI_IS_DIRTY);
2185 /* do checkpoint only */
2186 ret = f2fs_sync_fs(sb, 1);
2193 f2fs_stop_gc_thread(sbi);
2194 f2fs_stop_discard_thread(sbi);
2196 f2fs_drop_discard_cmd(sbi);
2197 clear_opt(sbi, DISCARD);
2199 f2fs_update_time(sbi, REQ_TIME);
2201 if (in != F2FS_GOING_DOWN_FULLSYNC)
2202 mnt_drop_write_file(filp);
2204 trace_f2fs_shutdown(sbi, in, ret);
2209 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2211 struct inode *inode = file_inode(filp);
2212 struct super_block *sb = inode->i_sb;
2213 struct fstrim_range range;
2216 if (!capable(CAP_SYS_ADMIN))
2219 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2222 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2226 ret = mnt_want_write_file(filp);
2230 range.minlen = max((unsigned int)range.minlen,
2231 bdev_discard_granularity(sb->s_bdev));
2232 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2233 mnt_drop_write_file(filp);
2237 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2240 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2244 static bool uuid_is_nonzero(__u8 u[16])
2248 for (i = 0; i < 16; i++)
2254 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2256 struct inode *inode = file_inode(filp);
2258 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2261 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2263 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2266 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2268 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2270 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2273 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2275 struct inode *inode = file_inode(filp);
2276 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2279 if (!f2fs_sb_has_encrypt(sbi))
2282 err = mnt_want_write_file(filp);
2286 f2fs_down_write(&sbi->sb_lock);
2288 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2291 /* update superblock with uuid */
2292 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2294 err = f2fs_commit_super(sbi, false);
2297 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2301 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2305 f2fs_up_write(&sbi->sb_lock);
2306 mnt_drop_write_file(filp);
2310 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2313 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2316 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2319 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2321 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2324 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2327 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2329 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2332 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2335 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2338 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2341 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2344 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2347 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2350 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2353 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2355 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2358 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2361 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2363 struct inode *inode = file_inode(filp);
2364 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2365 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2367 .should_migrate_blocks = false,
2368 .nr_free_secs = 0 };
2372 if (!capable(CAP_SYS_ADMIN))
2375 if (get_user(sync, (__u32 __user *)arg))
2378 if (f2fs_readonly(sbi->sb))
2381 ret = mnt_want_write_file(filp);
2386 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2391 f2fs_down_write(&sbi->gc_lock);
2394 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2395 gc_control.err_gc_skipped = sync;
2396 ret = f2fs_gc(sbi, &gc_control);
2398 mnt_drop_write_file(filp);
2402 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2404 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2405 struct f2fs_gc_control gc_control = {
2406 .init_gc_type = range->sync ? FG_GC : BG_GC,
2408 .should_migrate_blocks = false,
2409 .err_gc_skipped = range->sync,
2410 .nr_free_secs = 0 };
2414 if (!capable(CAP_SYS_ADMIN))
2416 if (f2fs_readonly(sbi->sb))
2419 end = range->start + range->len;
2420 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2421 end >= MAX_BLKADDR(sbi))
2424 ret = mnt_want_write_file(filp);
2430 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2435 f2fs_down_write(&sbi->gc_lock);
2438 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2439 ret = f2fs_gc(sbi, &gc_control);
2445 range->start += CAP_BLKS_PER_SEC(sbi);
2446 if (range->start <= end)
2449 mnt_drop_write_file(filp);
2453 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2455 struct f2fs_gc_range range;
2457 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2460 return __f2fs_ioc_gc_range(filp, &range);
2463 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2465 struct inode *inode = file_inode(filp);
2466 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2469 if (!capable(CAP_SYS_ADMIN))
2472 if (f2fs_readonly(sbi->sb))
2475 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2476 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2480 ret = mnt_want_write_file(filp);
2484 ret = f2fs_sync_fs(sbi->sb, 1);
2486 mnt_drop_write_file(filp);
2490 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2492 struct f2fs_defragment *range)
2494 struct inode *inode = file_inode(filp);
2495 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2496 .m_seg_type = NO_CHECK_TYPE,
2497 .m_may_create = false };
2498 struct extent_info ei = {0, 0, 0};
2499 pgoff_t pg_start, pg_end, next_pgofs;
2500 unsigned int blk_per_seg = sbi->blocks_per_seg;
2501 unsigned int total = 0, sec_num;
2502 block_t blk_end = 0;
2503 bool fragmented = false;
2506 pg_start = range->start >> PAGE_SHIFT;
2507 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2509 f2fs_balance_fs(sbi, true);
2513 /* if in-place-update policy is enabled, don't waste time here */
2514 set_inode_flag(inode, FI_OPU_WRITE);
2515 if (f2fs_should_update_inplace(inode, NULL)) {
2520 /* writeback all dirty pages in the range */
2521 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2522 range->start + range->len - 1);
2527 * lookup mapping info in extent cache, skip defragmenting if physical
2528 * block addresses are continuous.
2530 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2531 if (ei.fofs + ei.len >= pg_end)
2535 map.m_lblk = pg_start;
2536 map.m_next_pgofs = &next_pgofs;
2539 * lookup mapping info in dnode page cache, skip defragmenting if all
2540 * physical block addresses are continuous even if there are hole(s)
2541 * in logical blocks.
2543 while (map.m_lblk < pg_end) {
2544 map.m_len = pg_end - map.m_lblk;
2545 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2549 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2550 map.m_lblk = next_pgofs;
2554 if (blk_end && blk_end != map.m_pblk)
2557 /* record total count of block that we're going to move */
2560 blk_end = map.m_pblk + map.m_len;
2562 map.m_lblk += map.m_len;
2570 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2573 * make sure there are enough free section for LFS allocation, this can
2574 * avoid defragment running in SSR mode when free section are allocated
2577 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2582 map.m_lblk = pg_start;
2583 map.m_len = pg_end - pg_start;
2586 while (map.m_lblk < pg_end) {
2591 map.m_len = pg_end - map.m_lblk;
2592 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2596 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2597 map.m_lblk = next_pgofs;
2601 set_inode_flag(inode, FI_SKIP_WRITES);
2604 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2607 page = f2fs_get_lock_data_page(inode, idx, true);
2609 err = PTR_ERR(page);
2613 set_page_dirty(page);
2614 set_page_private_gcing(page);
2615 f2fs_put_page(page, 1);
2624 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2627 clear_inode_flag(inode, FI_SKIP_WRITES);
2629 err = filemap_fdatawrite(inode->i_mapping);
2634 clear_inode_flag(inode, FI_SKIP_WRITES);
2636 clear_inode_flag(inode, FI_OPU_WRITE);
2637 inode_unlock(inode);
2639 range->len = (u64)total << PAGE_SHIFT;
2643 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2645 struct inode *inode = file_inode(filp);
2646 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2647 struct f2fs_defragment range;
2650 if (!capable(CAP_SYS_ADMIN))
2653 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2656 if (f2fs_readonly(sbi->sb))
2659 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2663 /* verify alignment of offset & size */
2664 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2667 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2668 max_file_blocks(inode)))
2671 err = mnt_want_write_file(filp);
2675 err = f2fs_defragment_range(sbi, filp, &range);
2676 mnt_drop_write_file(filp);
2678 f2fs_update_time(sbi, REQ_TIME);
2682 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2689 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2690 struct file *file_out, loff_t pos_out, size_t len)
2692 struct inode *src = file_inode(file_in);
2693 struct inode *dst = file_inode(file_out);
2694 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2695 size_t olen = len, dst_max_i_size = 0;
2699 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2700 src->i_sb != dst->i_sb)
2703 if (unlikely(f2fs_readonly(src->i_sb)))
2706 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2709 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2712 if (pos_out < 0 || pos_in < 0)
2716 if (pos_in == pos_out)
2718 if (pos_out > pos_in && pos_out < pos_in + len)
2725 if (!inode_trylock(dst))
2730 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2733 olen = len = src->i_size - pos_in;
2734 if (pos_in + len == src->i_size)
2735 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2741 dst_osize = dst->i_size;
2742 if (pos_out + olen > dst->i_size)
2743 dst_max_i_size = pos_out + olen;
2745 /* verify the end result is block aligned */
2746 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2747 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2748 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2751 ret = f2fs_convert_inline_inode(src);
2755 ret = f2fs_convert_inline_inode(dst);
2759 /* write out all dirty pages from offset */
2760 ret = filemap_write_and_wait_range(src->i_mapping,
2761 pos_in, pos_in + len);
2765 ret = filemap_write_and_wait_range(dst->i_mapping,
2766 pos_out, pos_out + len);
2770 f2fs_balance_fs(sbi, true);
2772 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2775 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2780 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2781 pos_out >> F2FS_BLKSIZE_BITS,
2782 len >> F2FS_BLKSIZE_BITS, false);
2786 f2fs_i_size_write(dst, dst_max_i_size);
2787 else if (dst_osize != dst->i_size)
2788 f2fs_i_size_write(dst, dst_osize);
2790 f2fs_unlock_op(sbi);
2793 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2795 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2804 static int __f2fs_ioc_move_range(struct file *filp,
2805 struct f2fs_move_range *range)
2810 if (!(filp->f_mode & FMODE_READ) ||
2811 !(filp->f_mode & FMODE_WRITE))
2814 dst = fdget(range->dst_fd);
2818 if (!(dst.file->f_mode & FMODE_WRITE)) {
2823 err = mnt_want_write_file(filp);
2827 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2828 range->pos_out, range->len);
2830 mnt_drop_write_file(filp);
2836 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2838 struct f2fs_move_range range;
2840 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2843 return __f2fs_ioc_move_range(filp, &range);
2846 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2848 struct inode *inode = file_inode(filp);
2849 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2850 struct sit_info *sm = SIT_I(sbi);
2851 unsigned int start_segno = 0, end_segno = 0;
2852 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2853 struct f2fs_flush_device range;
2854 struct f2fs_gc_control gc_control = {
2855 .init_gc_type = FG_GC,
2856 .should_migrate_blocks = true,
2857 .err_gc_skipped = true,
2858 .nr_free_secs = 0 };
2861 if (!capable(CAP_SYS_ADMIN))
2864 if (f2fs_readonly(sbi->sb))
2867 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2870 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2874 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2875 __is_large_section(sbi)) {
2876 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2877 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2881 ret = mnt_want_write_file(filp);
2885 if (range.dev_num != 0)
2886 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2887 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2889 start_segno = sm->last_victim[FLUSH_DEVICE];
2890 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2891 start_segno = dev_start_segno;
2892 end_segno = min(start_segno + range.segments, dev_end_segno);
2894 while (start_segno < end_segno) {
2895 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2899 sm->last_victim[GC_CB] = end_segno + 1;
2900 sm->last_victim[GC_GREEDY] = end_segno + 1;
2901 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2903 gc_control.victim_segno = start_segno;
2904 ret = f2fs_gc(sbi, &gc_control);
2912 mnt_drop_write_file(filp);
2916 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2918 struct inode *inode = file_inode(filp);
2919 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2921 /* Must validate to set it with SQLite behavior in Android. */
2922 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2924 return put_user(sb_feature, (u32 __user *)arg);
2928 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2930 struct dquot *transfer_to[MAXQUOTAS] = {};
2931 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2932 struct super_block *sb = sbi->sb;
2935 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2936 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2937 err = __dquot_transfer(inode, transfer_to);
2939 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2940 dqput(transfer_to[PRJQUOTA]);
2945 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
2947 struct f2fs_inode_info *fi = F2FS_I(inode);
2948 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2949 struct f2fs_inode *ri = NULL;
2953 if (!f2fs_sb_has_project_quota(sbi)) {
2954 if (projid != F2FS_DEF_PROJID)
2960 if (!f2fs_has_extra_attr(inode))
2963 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2965 if (projid_eq(kprojid, fi->i_projid))
2969 /* Is it quota file? Do not allow user to mess with it */
2970 if (IS_NOQUOTA(inode))
2973 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
2976 err = f2fs_dquot_initialize(inode);
2981 err = f2fs_transfer_project_quota(inode, kprojid);
2985 fi->i_projid = kprojid;
2986 inode->i_ctime = current_time(inode);
2987 f2fs_mark_inode_dirty_sync(inode, true);
2989 f2fs_unlock_op(sbi);
2993 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2998 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3000 if (projid != F2FS_DEF_PROJID)
3006 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3008 struct inode *inode = d_inode(dentry);
3009 struct f2fs_inode_info *fi = F2FS_I(inode);
3010 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3012 if (IS_ENCRYPTED(inode))
3013 fsflags |= FS_ENCRYPT_FL;
3014 if (IS_VERITY(inode))
3015 fsflags |= FS_VERITY_FL;
3016 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3017 fsflags |= FS_INLINE_DATA_FL;
3018 if (is_inode_flag_set(inode, FI_PIN_FILE))
3019 fsflags |= FS_NOCOW_FL;
3021 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3023 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3024 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3029 int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3030 struct dentry *dentry, struct fileattr *fa)
3032 struct inode *inode = d_inode(dentry);
3033 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3037 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3039 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3041 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3043 fsflags &= F2FS_SETTABLE_FS_FL;
3044 if (!fa->flags_valid)
3045 mask &= FS_COMMON_FL;
3047 iflags = f2fs_fsflags_to_iflags(fsflags);
3048 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3051 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3053 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3058 int f2fs_pin_file_control(struct inode *inode, bool inc)
3060 struct f2fs_inode_info *fi = F2FS_I(inode);
3061 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3063 /* Use i_gc_failures for normal file as a risk signal. */
3065 f2fs_i_gc_failures_write(inode,
3066 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3068 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3069 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3070 __func__, inode->i_ino,
3071 fi->i_gc_failures[GC_FAILURE_PIN]);
3072 clear_inode_flag(inode, FI_PIN_FILE);
3078 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3080 struct inode *inode = file_inode(filp);
3084 if (get_user(pin, (__u32 __user *)arg))
3087 if (!S_ISREG(inode->i_mode))
3090 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3093 ret = mnt_want_write_file(filp);
3100 clear_inode_flag(inode, FI_PIN_FILE);
3101 f2fs_i_gc_failures_write(inode, 0);
3105 if (f2fs_should_update_outplace(inode, NULL)) {
3110 if (f2fs_pin_file_control(inode, false)) {
3115 ret = f2fs_convert_inline_inode(inode);
3119 if (!f2fs_disable_compressed_file(inode)) {
3124 set_inode_flag(inode, FI_PIN_FILE);
3125 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3127 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3129 inode_unlock(inode);
3130 mnt_drop_write_file(filp);
3134 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3136 struct inode *inode = file_inode(filp);
3139 if (is_inode_flag_set(inode, FI_PIN_FILE))
3140 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3141 return put_user(pin, (u32 __user *)arg);
3144 int f2fs_precache_extents(struct inode *inode)
3146 struct f2fs_inode_info *fi = F2FS_I(inode);
3147 struct f2fs_map_blocks map;
3148 pgoff_t m_next_extent;
3152 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3156 map.m_next_pgofs = NULL;
3157 map.m_next_extent = &m_next_extent;
3158 map.m_seg_type = NO_CHECK_TYPE;
3159 map.m_may_create = false;
3160 end = max_file_blocks(inode);
3162 while (map.m_lblk < end) {
3163 map.m_len = end - map.m_lblk;
3165 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3166 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3167 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3171 map.m_lblk = m_next_extent;
3177 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3179 return f2fs_precache_extents(file_inode(filp));
3182 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3184 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3187 if (!capable(CAP_SYS_ADMIN))
3190 if (f2fs_readonly(sbi->sb))
3193 if (copy_from_user(&block_count, (void __user *)arg,
3194 sizeof(block_count)))
3197 return f2fs_resize_fs(sbi, block_count);
3200 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3202 struct inode *inode = file_inode(filp);
3204 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3206 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3207 f2fs_warn(F2FS_I_SB(inode),
3208 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3213 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3216 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3218 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3221 return fsverity_ioctl_measure(filp, (void __user *)arg);
3224 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3226 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3229 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3232 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3234 struct inode *inode = file_inode(filp);
3235 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3240 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3244 f2fs_down_read(&sbi->sb_lock);
3245 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3246 ARRAY_SIZE(sbi->raw_super->volume_name),
3247 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3248 f2fs_up_read(&sbi->sb_lock);
3250 if (copy_to_user((char __user *)arg, vbuf,
3251 min(FSLABEL_MAX, count)))
3258 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3260 struct inode *inode = file_inode(filp);
3261 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3265 if (!capable(CAP_SYS_ADMIN))
3268 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3270 return PTR_ERR(vbuf);
3272 err = mnt_want_write_file(filp);
3276 f2fs_down_write(&sbi->sb_lock);
3278 memset(sbi->raw_super->volume_name, 0,
3279 sizeof(sbi->raw_super->volume_name));
3280 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3281 sbi->raw_super->volume_name,
3282 ARRAY_SIZE(sbi->raw_super->volume_name));
3284 err = f2fs_commit_super(sbi, false);
3286 f2fs_up_write(&sbi->sb_lock);
3288 mnt_drop_write_file(filp);
3294 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3296 struct inode *inode = file_inode(filp);
3299 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3302 if (!f2fs_compressed_file(inode))
3305 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3306 return put_user(blocks, (u64 __user *)arg);
3309 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3311 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3312 unsigned int released_blocks = 0;
3313 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3317 for (i = 0; i < count; i++) {
3318 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3319 dn->ofs_in_node + i);
3321 if (!__is_valid_data_blkaddr(blkaddr))
3323 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3324 DATA_GENERIC_ENHANCE)))
3325 return -EFSCORRUPTED;
3329 int compr_blocks = 0;
3331 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3332 blkaddr = f2fs_data_blkaddr(dn);
3335 if (blkaddr == COMPRESS_ADDR)
3337 dn->ofs_in_node += cluster_size;
3341 if (__is_valid_data_blkaddr(blkaddr))
3344 if (blkaddr != NEW_ADDR)
3347 dn->data_blkaddr = NULL_ADDR;
3348 f2fs_set_data_blkaddr(dn);
3351 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3352 dec_valid_block_count(sbi, dn->inode,
3353 cluster_size - compr_blocks);
3355 released_blocks += cluster_size - compr_blocks;
3357 count -= cluster_size;
3360 return released_blocks;
3363 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3365 struct inode *inode = file_inode(filp);
3366 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3367 pgoff_t page_idx = 0, last_idx;
3368 unsigned int released_blocks = 0;
3372 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3375 if (!f2fs_compressed_file(inode))
3378 if (f2fs_readonly(sbi->sb))
3381 ret = mnt_want_write_file(filp);
3385 f2fs_balance_fs(F2FS_I_SB(inode), true);
3389 writecount = atomic_read(&inode->i_writecount);
3390 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3391 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3396 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3401 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3405 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3406 inode->i_ctime = current_time(inode);
3407 f2fs_mark_inode_dirty_sync(inode, true);
3409 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3412 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3413 filemap_invalidate_lock(inode->i_mapping);
3415 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3417 while (page_idx < last_idx) {
3418 struct dnode_of_data dn;
3419 pgoff_t end_offset, count;
3421 set_new_dnode(&dn, inode, NULL, NULL, 0);
3422 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3424 if (ret == -ENOENT) {
3425 page_idx = f2fs_get_next_page_offset(&dn,
3433 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3434 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3435 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3437 ret = release_compress_blocks(&dn, count);
3439 f2fs_put_dnode(&dn);
3445 released_blocks += ret;
3448 filemap_invalidate_unlock(inode->i_mapping);
3449 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3451 inode_unlock(inode);
3453 mnt_drop_write_file(filp);
3456 ret = put_user(released_blocks, (u64 __user *)arg);
3457 } else if (released_blocks &&
3458 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3459 set_sbi_flag(sbi, SBI_NEED_FSCK);
3460 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3461 "iblocks=%llu, released=%u, compr_blocks=%u, "
3463 __func__, inode->i_ino, inode->i_blocks,
3465 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3471 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3473 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3474 unsigned int reserved_blocks = 0;
3475 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3479 for (i = 0; i < count; i++) {
3480 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3481 dn->ofs_in_node + i);
3483 if (!__is_valid_data_blkaddr(blkaddr))
3485 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3486 DATA_GENERIC_ENHANCE)))
3487 return -EFSCORRUPTED;
3491 int compr_blocks = 0;
3495 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3496 blkaddr = f2fs_data_blkaddr(dn);
3499 if (blkaddr == COMPRESS_ADDR)
3501 dn->ofs_in_node += cluster_size;
3505 if (__is_valid_data_blkaddr(blkaddr)) {
3510 dn->data_blkaddr = NEW_ADDR;
3511 f2fs_set_data_blkaddr(dn);
3514 reserved = cluster_size - compr_blocks;
3515 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3519 if (reserved != cluster_size - compr_blocks)
3522 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3524 reserved_blocks += reserved;
3526 count -= cluster_size;
3529 return reserved_blocks;
3532 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3534 struct inode *inode = file_inode(filp);
3535 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3536 pgoff_t page_idx = 0, last_idx;
3537 unsigned int reserved_blocks = 0;
3540 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3543 if (!f2fs_compressed_file(inode))
3546 if (f2fs_readonly(sbi->sb))
3549 ret = mnt_want_write_file(filp);
3553 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3556 f2fs_balance_fs(F2FS_I_SB(inode), true);
3560 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3565 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3566 filemap_invalidate_lock(inode->i_mapping);
3568 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3570 while (page_idx < last_idx) {
3571 struct dnode_of_data dn;
3572 pgoff_t end_offset, count;
3574 set_new_dnode(&dn, inode, NULL, NULL, 0);
3575 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3577 if (ret == -ENOENT) {
3578 page_idx = f2fs_get_next_page_offset(&dn,
3586 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3587 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3588 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3590 ret = reserve_compress_blocks(&dn, count);
3592 f2fs_put_dnode(&dn);
3598 reserved_blocks += ret;
3601 filemap_invalidate_unlock(inode->i_mapping);
3602 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3605 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3606 inode->i_ctime = current_time(inode);
3607 f2fs_mark_inode_dirty_sync(inode, true);
3610 inode_unlock(inode);
3612 mnt_drop_write_file(filp);
3615 ret = put_user(reserved_blocks, (u64 __user *)arg);
3616 } else if (reserved_blocks &&
3617 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3618 set_sbi_flag(sbi, SBI_NEED_FSCK);
3619 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3620 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3622 __func__, inode->i_ino, inode->i_blocks,
3624 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3630 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3631 pgoff_t off, block_t block, block_t len, u32 flags)
3633 sector_t sector = SECTOR_FROM_BLOCK(block);
3634 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3637 if (flags & F2FS_TRIM_FILE_DISCARD) {
3638 if (bdev_max_secure_erase_sectors(bdev))
3639 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3642 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3646 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3647 if (IS_ENCRYPTED(inode))
3648 ret = fscrypt_zeroout_range(inode, off, block, len);
3650 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3657 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3659 struct inode *inode = file_inode(filp);
3660 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3661 struct address_space *mapping = inode->i_mapping;
3662 struct block_device *prev_bdev = NULL;
3663 struct f2fs_sectrim_range range;
3664 pgoff_t index, pg_end, prev_index = 0;
3665 block_t prev_block = 0, len = 0;
3667 bool to_end = false;
3670 if (!(filp->f_mode & FMODE_WRITE))
3673 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3677 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3678 !S_ISREG(inode->i_mode))
3681 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3682 !f2fs_hw_support_discard(sbi)) ||
3683 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3684 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3687 file_start_write(filp);
3690 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3691 range.start >= inode->i_size) {
3699 if (inode->i_size - range.start > range.len) {
3700 end_addr = range.start + range.len;
3702 end_addr = range.len == (u64)-1 ?
3703 sbi->sb->s_maxbytes : inode->i_size;
3707 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3708 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3713 index = F2FS_BYTES_TO_BLK(range.start);
3714 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3716 ret = f2fs_convert_inline_inode(inode);
3720 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3721 filemap_invalidate_lock(mapping);
3723 ret = filemap_write_and_wait_range(mapping, range.start,
3724 to_end ? LLONG_MAX : end_addr - 1);
3728 truncate_inode_pages_range(mapping, range.start,
3729 to_end ? -1 : end_addr - 1);
3731 while (index < pg_end) {
3732 struct dnode_of_data dn;
3733 pgoff_t end_offset, count;
3736 set_new_dnode(&dn, inode, NULL, NULL, 0);
3737 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3739 if (ret == -ENOENT) {
3740 index = f2fs_get_next_page_offset(&dn, index);
3746 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3747 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3748 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3749 struct block_device *cur_bdev;
3750 block_t blkaddr = f2fs_data_blkaddr(&dn);
3752 if (!__is_valid_data_blkaddr(blkaddr))
3755 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3756 DATA_GENERIC_ENHANCE)) {
3757 ret = -EFSCORRUPTED;
3758 f2fs_put_dnode(&dn);
3762 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3763 if (f2fs_is_multi_device(sbi)) {
3764 int di = f2fs_target_device_index(sbi, blkaddr);
3766 blkaddr -= FDEV(di).start_blk;
3770 if (prev_bdev == cur_bdev &&
3771 index == prev_index + len &&
3772 blkaddr == prev_block + len) {
3775 ret = f2fs_secure_erase(prev_bdev,
3776 inode, prev_index, prev_block,
3779 f2fs_put_dnode(&dn);
3788 prev_bdev = cur_bdev;
3790 prev_block = blkaddr;
3795 f2fs_put_dnode(&dn);
3797 if (fatal_signal_pending(current)) {
3805 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3806 prev_block, len, range.flags);
3808 filemap_invalidate_unlock(mapping);
3809 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3811 inode_unlock(inode);
3812 file_end_write(filp);
3817 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3819 struct inode *inode = file_inode(filp);
3820 struct f2fs_comp_option option;
3822 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3825 inode_lock_shared(inode);
3827 if (!f2fs_compressed_file(inode)) {
3828 inode_unlock_shared(inode);
3832 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3833 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3835 inode_unlock_shared(inode);
3837 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3844 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3846 struct inode *inode = file_inode(filp);
3847 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3848 struct f2fs_comp_option option;
3851 if (!f2fs_sb_has_compression(sbi))
3854 if (!(filp->f_mode & FMODE_WRITE))
3857 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3861 if (!f2fs_compressed_file(inode) ||
3862 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3863 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3864 option.algorithm >= COMPRESS_MAX)
3867 file_start_write(filp);
3870 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3875 if (inode->i_size != 0) {
3880 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3881 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3882 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
3883 f2fs_mark_inode_dirty_sync(inode, true);
3885 if (!f2fs_is_compress_backend_ready(inode))
3886 f2fs_warn(sbi, "compression algorithm is successfully set, "
3887 "but current kernel doesn't support this algorithm.");
3889 inode_unlock(inode);
3890 file_end_write(filp);
3895 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
3897 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
3898 struct address_space *mapping = inode->i_mapping;
3900 pgoff_t redirty_idx = page_idx;
3901 int i, page_len = 0, ret = 0;
3903 page_cache_ra_unbounded(&ractl, len, 0);
3905 for (i = 0; i < len; i++, page_idx++) {
3906 page = read_cache_page(mapping, page_idx, NULL, NULL);
3908 ret = PTR_ERR(page);
3914 for (i = 0; i < page_len; i++, redirty_idx++) {
3915 page = find_lock_page(mapping, redirty_idx);
3917 /* It will never fail, when page has pinned above */
3918 f2fs_bug_on(F2FS_I_SB(inode), !page);
3920 set_page_dirty(page);
3921 f2fs_put_page(page, 1);
3922 f2fs_put_page(page, 0);
3928 static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
3930 struct inode *inode = file_inode(filp);
3931 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3932 struct f2fs_inode_info *fi = F2FS_I(inode);
3933 pgoff_t page_idx = 0, last_idx;
3934 unsigned int blk_per_seg = sbi->blocks_per_seg;
3935 int cluster_size = fi->i_cluster_size;
3938 if (!f2fs_sb_has_compression(sbi) ||
3939 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
3942 if (!(filp->f_mode & FMODE_WRITE))
3945 if (!f2fs_compressed_file(inode))
3948 f2fs_balance_fs(F2FS_I_SB(inode), true);
3950 file_start_write(filp);
3953 if (!f2fs_is_compress_backend_ready(inode)) {
3958 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3963 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3967 if (!atomic_read(&fi->i_compr_blocks))
3970 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3972 count = last_idx - page_idx;
3974 int len = min(cluster_size, count);
3976 ret = redirty_blocks(inode, page_idx, len);
3980 if (get_dirty_pages(inode) >= blk_per_seg)
3981 filemap_fdatawrite(inode->i_mapping);
3988 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
3992 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
3995 inode_unlock(inode);
3996 file_end_write(filp);
4001 static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4003 struct inode *inode = file_inode(filp);
4004 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4005 pgoff_t page_idx = 0, last_idx;
4006 unsigned int blk_per_seg = sbi->blocks_per_seg;
4007 int cluster_size = F2FS_I(inode)->i_cluster_size;
4010 if (!f2fs_sb_has_compression(sbi) ||
4011 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4014 if (!(filp->f_mode & FMODE_WRITE))
4017 if (!f2fs_compressed_file(inode))
4020 f2fs_balance_fs(F2FS_I_SB(inode), true);
4022 file_start_write(filp);
4025 if (!f2fs_is_compress_backend_ready(inode)) {
4030 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4035 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4039 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4041 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4043 count = last_idx - page_idx;
4045 int len = min(cluster_size, count);
4047 ret = redirty_blocks(inode, page_idx, len);
4051 if (get_dirty_pages(inode) >= blk_per_seg)
4052 filemap_fdatawrite(inode->i_mapping);
4059 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4062 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4065 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4068 inode_unlock(inode);
4069 file_end_write(filp);
4074 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4077 case FS_IOC_GETVERSION:
4078 return f2fs_ioc_getversion(filp, arg);
4079 case F2FS_IOC_START_ATOMIC_WRITE:
4080 return f2fs_ioc_start_atomic_write(filp);
4081 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4082 return f2fs_ioc_commit_atomic_write(filp);
4083 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4084 return f2fs_ioc_abort_atomic_write(filp);
4085 case F2FS_IOC_START_VOLATILE_WRITE:
4086 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4088 case F2FS_IOC_SHUTDOWN:
4089 return f2fs_ioc_shutdown(filp, arg);
4091 return f2fs_ioc_fitrim(filp, arg);
4092 case FS_IOC_SET_ENCRYPTION_POLICY:
4093 return f2fs_ioc_set_encryption_policy(filp, arg);
4094 case FS_IOC_GET_ENCRYPTION_POLICY:
4095 return f2fs_ioc_get_encryption_policy(filp, arg);
4096 case FS_IOC_GET_ENCRYPTION_PWSALT:
4097 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4098 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4099 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4100 case FS_IOC_ADD_ENCRYPTION_KEY:
4101 return f2fs_ioc_add_encryption_key(filp, arg);
4102 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4103 return f2fs_ioc_remove_encryption_key(filp, arg);
4104 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4105 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4106 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4107 return f2fs_ioc_get_encryption_key_status(filp, arg);
4108 case FS_IOC_GET_ENCRYPTION_NONCE:
4109 return f2fs_ioc_get_encryption_nonce(filp, arg);
4110 case F2FS_IOC_GARBAGE_COLLECT:
4111 return f2fs_ioc_gc(filp, arg);
4112 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4113 return f2fs_ioc_gc_range(filp, arg);
4114 case F2FS_IOC_WRITE_CHECKPOINT:
4115 return f2fs_ioc_write_checkpoint(filp, arg);
4116 case F2FS_IOC_DEFRAGMENT:
4117 return f2fs_ioc_defragment(filp, arg);
4118 case F2FS_IOC_MOVE_RANGE:
4119 return f2fs_ioc_move_range(filp, arg);
4120 case F2FS_IOC_FLUSH_DEVICE:
4121 return f2fs_ioc_flush_device(filp, arg);
4122 case F2FS_IOC_GET_FEATURES:
4123 return f2fs_ioc_get_features(filp, arg);
4124 case F2FS_IOC_GET_PIN_FILE:
4125 return f2fs_ioc_get_pin_file(filp, arg);
4126 case F2FS_IOC_SET_PIN_FILE:
4127 return f2fs_ioc_set_pin_file(filp, arg);
4128 case F2FS_IOC_PRECACHE_EXTENTS:
4129 return f2fs_ioc_precache_extents(filp, arg);
4130 case F2FS_IOC_RESIZE_FS:
4131 return f2fs_ioc_resize_fs(filp, arg);
4132 case FS_IOC_ENABLE_VERITY:
4133 return f2fs_ioc_enable_verity(filp, arg);
4134 case FS_IOC_MEASURE_VERITY:
4135 return f2fs_ioc_measure_verity(filp, arg);
4136 case FS_IOC_READ_VERITY_METADATA:
4137 return f2fs_ioc_read_verity_metadata(filp, arg);
4138 case FS_IOC_GETFSLABEL:
4139 return f2fs_ioc_getfslabel(filp, arg);
4140 case FS_IOC_SETFSLABEL:
4141 return f2fs_ioc_setfslabel(filp, arg);
4142 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4143 return f2fs_get_compress_blocks(filp, arg);
4144 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4145 return f2fs_release_compress_blocks(filp, arg);
4146 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4147 return f2fs_reserve_compress_blocks(filp, arg);
4148 case F2FS_IOC_SEC_TRIM_FILE:
4149 return f2fs_sec_trim_file(filp, arg);
4150 case F2FS_IOC_GET_COMPRESS_OPTION:
4151 return f2fs_ioc_get_compress_option(filp, arg);
4152 case F2FS_IOC_SET_COMPRESS_OPTION:
4153 return f2fs_ioc_set_compress_option(filp, arg);
4154 case F2FS_IOC_DECOMPRESS_FILE:
4155 return f2fs_ioc_decompress_file(filp, arg);
4156 case F2FS_IOC_COMPRESS_FILE:
4157 return f2fs_ioc_compress_file(filp, arg);
4163 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4165 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4167 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4170 return __f2fs_ioctl(filp, cmd, arg);
4174 * Return %true if the given read or write request should use direct I/O, or
4175 * %false if it should use buffered I/O.
4177 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4178 struct iov_iter *iter)
4182 if (!(iocb->ki_flags & IOCB_DIRECT))
4185 if (f2fs_force_buffered_io(inode, iocb, iter))
4189 * Direct I/O not aligned to the disk's logical_block_size will be
4190 * attempted, but will fail with -EINVAL.
4192 * f2fs additionally requires that direct I/O be aligned to the
4193 * filesystem block size, which is often a stricter requirement.
4194 * However, f2fs traditionally falls back to buffered I/O on requests
4195 * that are logical_block_size-aligned but not fs-block aligned.
4197 * The below logic implements this behavior.
4199 align = iocb->ki_pos | iov_iter_alignment(iter);
4200 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4201 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4207 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4210 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4212 dec_page_count(sbi, F2FS_DIO_READ);
4215 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, size);
4219 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4220 .end_io = f2fs_dio_read_end_io,
4223 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4225 struct file *file = iocb->ki_filp;
4226 struct inode *inode = file_inode(file);
4227 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4228 struct f2fs_inode_info *fi = F2FS_I(inode);
4229 const loff_t pos = iocb->ki_pos;
4230 const size_t count = iov_iter_count(to);
4231 struct iomap_dio *dio;
4235 return 0; /* skip atime update */
4237 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4239 if (iocb->ki_flags & IOCB_NOWAIT) {
4240 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4245 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4249 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4250 * the higher-level function iomap_dio_rw() in order to ensure that the
4251 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4253 inc_page_count(sbi, F2FS_DIO_READ);
4254 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4255 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4256 if (IS_ERR_OR_NULL(dio)) {
4257 ret = PTR_ERR_OR_ZERO(dio);
4258 if (ret != -EIOCBQUEUED)
4259 dec_page_count(sbi, F2FS_DIO_READ);
4261 ret = iomap_dio_complete(dio);
4264 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4266 file_accessed(file);
4268 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4272 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4274 struct inode *inode = file_inode(iocb->ki_filp);
4275 const loff_t pos = iocb->ki_pos;
4278 if (!f2fs_is_compress_backend_ready(inode))
4281 if (trace_f2fs_dataread_start_enabled()) {
4282 char *p = f2fs_kmalloc(F2FS_I_SB(inode), PATH_MAX, GFP_KERNEL);
4286 goto skip_read_trace;
4288 path = dentry_path_raw(file_dentry(iocb->ki_filp), p, PATH_MAX);
4291 goto skip_read_trace;
4294 trace_f2fs_dataread_start(inode, pos, iov_iter_count(to),
4295 current->pid, path, current->comm);
4299 if (f2fs_should_use_dio(inode, iocb, to)) {
4300 ret = f2fs_dio_read_iter(iocb, to);
4302 ret = filemap_read(iocb, to, 0);
4304 f2fs_update_iostat(F2FS_I_SB(inode), APP_BUFFERED_READ_IO, ret);
4306 if (trace_f2fs_dataread_end_enabled())
4307 trace_f2fs_dataread_end(inode, pos, ret);
4311 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4313 struct file *file = iocb->ki_filp;
4314 struct inode *inode = file_inode(file);
4318 if (IS_IMMUTABLE(inode))
4321 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4324 count = generic_write_checks(iocb, from);
4328 err = file_modified(file);
4335 * Preallocate blocks for a write request, if it is possible and helpful to do
4336 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4337 * blocks were preallocated, or a negative errno value if something went
4338 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4339 * requested blocks (not just some of them) have been allocated.
4341 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4344 struct inode *inode = file_inode(iocb->ki_filp);
4345 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4346 const loff_t pos = iocb->ki_pos;
4347 const size_t count = iov_iter_count(iter);
4348 struct f2fs_map_blocks map = {};
4352 /* If it will be an out-of-place direct write, don't bother. */
4353 if (dio && f2fs_lfs_mode(sbi))
4356 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4357 * buffered IO, if DIO meets any holes.
4359 if (dio && i_size_read(inode) &&
4360 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4363 /* No-wait I/O can't allocate blocks. */
4364 if (iocb->ki_flags & IOCB_NOWAIT)
4367 /* If it will be a short write, don't bother. */
4368 if (fault_in_iov_iter_readable(iter, count))
4371 if (f2fs_has_inline_data(inode)) {
4372 /* If the data will fit inline, don't bother. */
4373 if (pos + count <= MAX_INLINE_DATA(inode))
4375 ret = f2fs_convert_inline_inode(inode);
4380 /* Do not preallocate blocks that will be written partially in 4KB. */
4381 map.m_lblk = F2FS_BLK_ALIGN(pos);
4382 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4383 if (map.m_len > map.m_lblk)
4384 map.m_len -= map.m_lblk;
4387 map.m_may_create = true;
4389 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4390 flag = F2FS_GET_BLOCK_PRE_DIO;
4392 map.m_seg_type = NO_CHECK_TYPE;
4393 flag = F2FS_GET_BLOCK_PRE_AIO;
4396 ret = f2fs_map_blocks(inode, &map, 1, flag);
4397 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4398 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4401 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4405 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4406 struct iov_iter *from)
4408 struct file *file = iocb->ki_filp;
4409 struct inode *inode = file_inode(file);
4412 if (iocb->ki_flags & IOCB_NOWAIT)
4415 current->backing_dev_info = inode_to_bdi(inode);
4416 ret = generic_perform_write(iocb, from);
4417 current->backing_dev_info = NULL;
4420 iocb->ki_pos += ret;
4421 f2fs_update_iostat(F2FS_I_SB(inode), APP_BUFFERED_IO, ret);
4426 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4429 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4431 dec_page_count(sbi, F2FS_DIO_WRITE);
4434 f2fs_update_iostat(sbi, APP_DIRECT_IO, size);
4438 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4439 .end_io = f2fs_dio_write_end_io,
4442 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4443 bool *may_need_sync)
4445 struct file *file = iocb->ki_filp;
4446 struct inode *inode = file_inode(file);
4447 struct f2fs_inode_info *fi = F2FS_I(inode);
4448 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4449 const bool do_opu = f2fs_lfs_mode(sbi);
4450 const loff_t pos = iocb->ki_pos;
4451 const ssize_t count = iov_iter_count(from);
4452 unsigned int dio_flags;
4453 struct iomap_dio *dio;
4456 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4458 if (iocb->ki_flags & IOCB_NOWAIT) {
4459 /* f2fs_convert_inline_inode() and block allocation can block */
4460 if (f2fs_has_inline_data(inode) ||
4461 !f2fs_overwrite_io(inode, pos, count)) {
4466 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4470 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4471 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4476 ret = f2fs_convert_inline_inode(inode);
4480 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4482 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4486 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4487 * the higher-level function iomap_dio_rw() in order to ensure that the
4488 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4490 inc_page_count(sbi, F2FS_DIO_WRITE);
4492 if (pos + count > inode->i_size)
4493 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4494 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4495 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4496 if (IS_ERR_OR_NULL(dio)) {
4497 ret = PTR_ERR_OR_ZERO(dio);
4498 if (ret == -ENOTBLK)
4500 if (ret != -EIOCBQUEUED)
4501 dec_page_count(sbi, F2FS_DIO_WRITE);
4503 ret = iomap_dio_complete(dio);
4507 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4508 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4512 if (pos + ret > inode->i_size)
4513 f2fs_i_size_write(inode, pos + ret);
4515 set_inode_flag(inode, FI_UPDATE_WRITE);
4517 if (iov_iter_count(from)) {
4519 loff_t bufio_start_pos = iocb->ki_pos;
4522 * The direct write was partial, so we need to fall back to a
4523 * buffered write for the remainder.
4526 ret2 = f2fs_buffered_write_iter(iocb, from);
4527 if (iov_iter_count(from))
4528 f2fs_write_failed(inode, iocb->ki_pos);
4533 * Ensure that the pagecache pages are written to disk and
4534 * invalidated to preserve the expected O_DIRECT semantics.
4537 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4541 ret2 = filemap_write_and_wait_range(file->f_mapping,
4546 invalidate_mapping_pages(file->f_mapping,
4547 bufio_start_pos >> PAGE_SHIFT,
4548 bufio_end_pos >> PAGE_SHIFT);
4551 /* iomap_dio_rw() already handled the generic_write_sync(). */
4552 *may_need_sync = false;
4555 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4559 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4561 struct inode *inode = file_inode(iocb->ki_filp);
4562 const loff_t orig_pos = iocb->ki_pos;
4563 const size_t orig_count = iov_iter_count(from);
4566 bool may_need_sync = true;
4570 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4575 if (!f2fs_is_compress_backend_ready(inode)) {
4580 if (iocb->ki_flags & IOCB_NOWAIT) {
4581 if (!inode_trylock(inode)) {
4589 ret = f2fs_write_checks(iocb, from);
4593 /* Determine whether we will do a direct write or a buffered write. */
4594 dio = f2fs_should_use_dio(inode, iocb, from);
4596 /* Possibly preallocate the blocks for the write. */
4597 target_size = iocb->ki_pos + iov_iter_count(from);
4598 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4599 if (preallocated < 0) {
4602 if (trace_f2fs_datawrite_start_enabled()) {
4603 char *p = f2fs_kmalloc(F2FS_I_SB(inode),
4604 PATH_MAX, GFP_KERNEL);
4608 goto skip_write_trace;
4609 path = dentry_path_raw(file_dentry(iocb->ki_filp),
4613 goto skip_write_trace;
4615 trace_f2fs_datawrite_start(inode, orig_pos, orig_count,
4616 current->pid, path, current->comm);
4620 /* Do the actual write. */
4622 f2fs_dio_write_iter(iocb, from, &may_need_sync):
4623 f2fs_buffered_write_iter(iocb, from);
4625 if (trace_f2fs_datawrite_end_enabled())
4626 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4629 /* Don't leave any preallocated blocks around past i_size. */
4630 if (preallocated && i_size_read(inode) < target_size) {
4631 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4632 filemap_invalidate_lock(inode->i_mapping);
4633 if (!f2fs_truncate(inode))
4634 file_dont_truncate(inode);
4635 filemap_invalidate_unlock(inode->i_mapping);
4636 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4638 file_dont_truncate(inode);
4641 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4643 inode_unlock(inode);
4645 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4646 if (ret > 0 && may_need_sync)
4647 ret = generic_write_sync(iocb, ret);
4651 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4654 struct address_space *mapping;
4655 struct backing_dev_info *bdi;
4656 struct inode *inode = file_inode(filp);
4659 if (advice == POSIX_FADV_SEQUENTIAL) {
4660 if (S_ISFIFO(inode->i_mode))
4663 mapping = filp->f_mapping;
4664 if (!mapping || len < 0)
4667 bdi = inode_to_bdi(mapping->host);
4668 filp->f_ra.ra_pages = bdi->ra_pages *
4669 F2FS_I_SB(inode)->seq_file_ra_mul;
4670 spin_lock(&filp->f_lock);
4671 filp->f_mode &= ~FMODE_RANDOM;
4672 spin_unlock(&filp->f_lock);
4676 err = generic_fadvise(filp, offset, len, advice);
4677 if (!err && advice == POSIX_FADV_DONTNEED &&
4678 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4679 f2fs_compressed_file(inode))
4680 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4685 #ifdef CONFIG_COMPAT
4686 struct compat_f2fs_gc_range {
4691 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4692 struct compat_f2fs_gc_range)
4694 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4696 struct compat_f2fs_gc_range __user *urange;
4697 struct f2fs_gc_range range;
4700 urange = compat_ptr(arg);
4701 err = get_user(range.sync, &urange->sync);
4702 err |= get_user(range.start, &urange->start);
4703 err |= get_user(range.len, &urange->len);
4707 return __f2fs_ioc_gc_range(file, &range);
4710 struct compat_f2fs_move_range {
4716 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4717 struct compat_f2fs_move_range)
4719 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4721 struct compat_f2fs_move_range __user *urange;
4722 struct f2fs_move_range range;
4725 urange = compat_ptr(arg);
4726 err = get_user(range.dst_fd, &urange->dst_fd);
4727 err |= get_user(range.pos_in, &urange->pos_in);
4728 err |= get_user(range.pos_out, &urange->pos_out);
4729 err |= get_user(range.len, &urange->len);
4733 return __f2fs_ioc_move_range(file, &range);
4736 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4738 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4740 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4744 case FS_IOC32_GETVERSION:
4745 cmd = FS_IOC_GETVERSION;
4747 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4748 return f2fs_compat_ioc_gc_range(file, arg);
4749 case F2FS_IOC32_MOVE_RANGE:
4750 return f2fs_compat_ioc_move_range(file, arg);
4751 case F2FS_IOC_START_ATOMIC_WRITE:
4752 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4753 case F2FS_IOC_START_VOLATILE_WRITE:
4754 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4755 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4756 case F2FS_IOC_SHUTDOWN:
4758 case FS_IOC_SET_ENCRYPTION_POLICY:
4759 case FS_IOC_GET_ENCRYPTION_PWSALT:
4760 case FS_IOC_GET_ENCRYPTION_POLICY:
4761 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4762 case FS_IOC_ADD_ENCRYPTION_KEY:
4763 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4764 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4765 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4766 case FS_IOC_GET_ENCRYPTION_NONCE:
4767 case F2FS_IOC_GARBAGE_COLLECT:
4768 case F2FS_IOC_WRITE_CHECKPOINT:
4769 case F2FS_IOC_DEFRAGMENT:
4770 case F2FS_IOC_FLUSH_DEVICE:
4771 case F2FS_IOC_GET_FEATURES:
4772 case F2FS_IOC_GET_PIN_FILE:
4773 case F2FS_IOC_SET_PIN_FILE:
4774 case F2FS_IOC_PRECACHE_EXTENTS:
4775 case F2FS_IOC_RESIZE_FS:
4776 case FS_IOC_ENABLE_VERITY:
4777 case FS_IOC_MEASURE_VERITY:
4778 case FS_IOC_READ_VERITY_METADATA:
4779 case FS_IOC_GETFSLABEL:
4780 case FS_IOC_SETFSLABEL:
4781 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4782 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4783 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4784 case F2FS_IOC_SEC_TRIM_FILE:
4785 case F2FS_IOC_GET_COMPRESS_OPTION:
4786 case F2FS_IOC_SET_COMPRESS_OPTION:
4787 case F2FS_IOC_DECOMPRESS_FILE:
4788 case F2FS_IOC_COMPRESS_FILE:
4791 return -ENOIOCTLCMD;
4793 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4797 const struct file_operations f2fs_file_operations = {
4798 .llseek = f2fs_llseek,
4799 .read_iter = f2fs_file_read_iter,
4800 .write_iter = f2fs_file_write_iter,
4801 .open = f2fs_file_open,
4802 .release = f2fs_release_file,
4803 .mmap = f2fs_file_mmap,
4804 .flush = f2fs_file_flush,
4805 .fsync = f2fs_sync_file,
4806 .fallocate = f2fs_fallocate,
4807 .unlocked_ioctl = f2fs_ioctl,
4808 #ifdef CONFIG_COMPAT
4809 .compat_ioctl = f2fs_compat_ioctl,
4811 .splice_read = generic_file_splice_read,
4812 .splice_write = iter_file_splice_write,
4813 .fadvise = f2fs_file_fadvise,