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 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
813 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
815 if (!fscrypt_dio_supported(inode))
817 if (fsverity_active(inode))
819 if (f2fs_compressed_file(inode))
822 /* disallow direct IO if any of devices has unaligned blksize */
823 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
826 if (f2fs_lfs_mode(sbi) && rw == WRITE && F2FS_IO_ALIGNED(sbi))
828 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
834 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
835 struct kstat *stat, u32 request_mask, unsigned int query_flags)
837 struct inode *inode = d_inode(path->dentry);
838 struct f2fs_inode_info *fi = F2FS_I(inode);
839 struct f2fs_inode *ri = NULL;
842 if (f2fs_has_extra_attr(inode) &&
843 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
844 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
845 stat->result_mask |= STATX_BTIME;
846 stat->btime.tv_sec = fi->i_crtime.tv_sec;
847 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
851 * Return the DIO alignment restrictions if requested. We only return
852 * this information when requested, since on encrypted files it might
853 * take a fair bit of work to get if the file wasn't opened recently.
855 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
856 * cannot represent that, so in that case we report no DIO support.
858 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
859 unsigned int bsize = i_blocksize(inode);
861 stat->result_mask |= STATX_DIOALIGN;
862 if (!f2fs_force_buffered_io(inode, WRITE)) {
863 stat->dio_mem_align = bsize;
864 stat->dio_offset_align = bsize;
869 if (flags & F2FS_COMPR_FL)
870 stat->attributes |= STATX_ATTR_COMPRESSED;
871 if (flags & F2FS_APPEND_FL)
872 stat->attributes |= STATX_ATTR_APPEND;
873 if (IS_ENCRYPTED(inode))
874 stat->attributes |= STATX_ATTR_ENCRYPTED;
875 if (flags & F2FS_IMMUTABLE_FL)
876 stat->attributes |= STATX_ATTR_IMMUTABLE;
877 if (flags & F2FS_NODUMP_FL)
878 stat->attributes |= STATX_ATTR_NODUMP;
879 if (IS_VERITY(inode))
880 stat->attributes |= STATX_ATTR_VERITY;
882 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
884 STATX_ATTR_ENCRYPTED |
885 STATX_ATTR_IMMUTABLE |
889 generic_fillattr(mnt_userns, inode, stat);
891 /* we need to show initial sectors used for inline_data/dentries */
892 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
893 f2fs_has_inline_dentry(inode))
894 stat->blocks += (stat->size + 511) >> 9;
899 #ifdef CONFIG_F2FS_FS_POSIX_ACL
900 static void __setattr_copy(struct user_namespace *mnt_userns,
901 struct inode *inode, const struct iattr *attr)
903 unsigned int ia_valid = attr->ia_valid;
905 i_uid_update(mnt_userns, attr, inode);
906 i_gid_update(mnt_userns, attr, inode);
907 if (ia_valid & ATTR_ATIME)
908 inode->i_atime = attr->ia_atime;
909 if (ia_valid & ATTR_MTIME)
910 inode->i_mtime = attr->ia_mtime;
911 if (ia_valid & ATTR_CTIME)
912 inode->i_ctime = attr->ia_ctime;
913 if (ia_valid & ATTR_MODE) {
914 umode_t mode = attr->ia_mode;
915 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
917 if (!in_group_p(kgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
919 set_acl_inode(inode, mode);
923 #define __setattr_copy setattr_copy
926 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
929 struct inode *inode = d_inode(dentry);
932 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
935 if (unlikely(IS_IMMUTABLE(inode)))
938 if (unlikely(IS_APPEND(inode) &&
939 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
940 ATTR_GID | ATTR_TIMES_SET))))
943 if ((attr->ia_valid & ATTR_SIZE) &&
944 !f2fs_is_compress_backend_ready(inode))
947 err = setattr_prepare(mnt_userns, dentry, attr);
951 err = fscrypt_prepare_setattr(dentry, attr);
955 err = fsverity_prepare_setattr(dentry, attr);
959 if (is_quota_modification(mnt_userns, inode, attr)) {
960 err = f2fs_dquot_initialize(inode);
964 if (i_uid_needs_update(mnt_userns, attr, inode) ||
965 i_gid_needs_update(mnt_userns, attr, inode)) {
966 f2fs_lock_op(F2FS_I_SB(inode));
967 err = dquot_transfer(mnt_userns, inode, attr);
969 set_sbi_flag(F2FS_I_SB(inode),
970 SBI_QUOTA_NEED_REPAIR);
971 f2fs_unlock_op(F2FS_I_SB(inode));
975 * update uid/gid under lock_op(), so that dquot and inode can
976 * be updated atomically.
978 i_uid_update(mnt_userns, attr, inode);
979 i_gid_update(mnt_userns, attr, inode);
980 f2fs_mark_inode_dirty_sync(inode, true);
981 f2fs_unlock_op(F2FS_I_SB(inode));
984 if (attr->ia_valid & ATTR_SIZE) {
985 loff_t old_size = i_size_read(inode);
987 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
989 * should convert inline inode before i_size_write to
990 * keep smaller than inline_data size with inline flag.
992 err = f2fs_convert_inline_inode(inode);
997 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
998 filemap_invalidate_lock(inode->i_mapping);
1000 truncate_setsize(inode, attr->ia_size);
1002 if (attr->ia_size <= old_size)
1003 err = f2fs_truncate(inode);
1005 * do not trim all blocks after i_size if target size is
1006 * larger than i_size.
1008 filemap_invalidate_unlock(inode->i_mapping);
1009 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1013 spin_lock(&F2FS_I(inode)->i_size_lock);
1014 inode->i_mtime = inode->i_ctime = current_time(inode);
1015 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1016 spin_unlock(&F2FS_I(inode)->i_size_lock);
1019 __setattr_copy(mnt_userns, inode, attr);
1021 if (attr->ia_valid & ATTR_MODE) {
1022 err = posix_acl_chmod(mnt_userns, inode, f2fs_get_inode_mode(inode));
1024 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1026 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1027 clear_inode_flag(inode, FI_ACL_MODE);
1031 /* file size may changed here */
1032 f2fs_mark_inode_dirty_sync(inode, true);
1034 /* inode change will produce dirty node pages flushed by checkpoint */
1035 f2fs_balance_fs(F2FS_I_SB(inode), true);
1040 const struct inode_operations f2fs_file_inode_operations = {
1041 .getattr = f2fs_getattr,
1042 .setattr = f2fs_setattr,
1043 .get_acl = f2fs_get_acl,
1044 .set_acl = f2fs_set_acl,
1045 .listxattr = f2fs_listxattr,
1046 .fiemap = f2fs_fiemap,
1047 .fileattr_get = f2fs_fileattr_get,
1048 .fileattr_set = f2fs_fileattr_set,
1051 static int fill_zero(struct inode *inode, pgoff_t index,
1052 loff_t start, loff_t len)
1054 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1060 f2fs_balance_fs(sbi, true);
1063 page = f2fs_get_new_data_page(inode, NULL, index, false);
1064 f2fs_unlock_op(sbi);
1067 return PTR_ERR(page);
1069 f2fs_wait_on_page_writeback(page, DATA, true, true);
1070 zero_user(page, start, len);
1071 set_page_dirty(page);
1072 f2fs_put_page(page, 1);
1076 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1080 while (pg_start < pg_end) {
1081 struct dnode_of_data dn;
1082 pgoff_t end_offset, count;
1084 set_new_dnode(&dn, inode, NULL, NULL, 0);
1085 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1087 if (err == -ENOENT) {
1088 pg_start = f2fs_get_next_page_offset(&dn,
1095 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1096 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1098 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1100 f2fs_truncate_data_blocks_range(&dn, count);
1101 f2fs_put_dnode(&dn);
1108 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1110 pgoff_t pg_start, pg_end;
1111 loff_t off_start, off_end;
1114 ret = f2fs_convert_inline_inode(inode);
1118 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1119 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1121 off_start = offset & (PAGE_SIZE - 1);
1122 off_end = (offset + len) & (PAGE_SIZE - 1);
1124 if (pg_start == pg_end) {
1125 ret = fill_zero(inode, pg_start, off_start,
1126 off_end - off_start);
1131 ret = fill_zero(inode, pg_start++, off_start,
1132 PAGE_SIZE - off_start);
1137 ret = fill_zero(inode, pg_end, 0, off_end);
1142 if (pg_start < pg_end) {
1143 loff_t blk_start, blk_end;
1144 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1146 f2fs_balance_fs(sbi, true);
1148 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1149 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1151 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1152 filemap_invalidate_lock(inode->i_mapping);
1154 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1157 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1158 f2fs_unlock_op(sbi);
1160 filemap_invalidate_unlock(inode->i_mapping);
1161 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1168 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1169 int *do_replace, pgoff_t off, pgoff_t len)
1171 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1172 struct dnode_of_data dn;
1176 set_new_dnode(&dn, inode, NULL, NULL, 0);
1177 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1178 if (ret && ret != -ENOENT) {
1180 } else if (ret == -ENOENT) {
1181 if (dn.max_level == 0)
1183 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1184 dn.ofs_in_node, len);
1190 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1191 dn.ofs_in_node, len);
1192 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1193 *blkaddr = f2fs_data_blkaddr(&dn);
1195 if (__is_valid_data_blkaddr(*blkaddr) &&
1196 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1197 DATA_GENERIC_ENHANCE)) {
1198 f2fs_put_dnode(&dn);
1199 return -EFSCORRUPTED;
1202 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1204 if (f2fs_lfs_mode(sbi)) {
1205 f2fs_put_dnode(&dn);
1209 /* do not invalidate this block address */
1210 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1214 f2fs_put_dnode(&dn);
1223 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1224 int *do_replace, pgoff_t off, int len)
1226 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1227 struct dnode_of_data dn;
1230 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1231 if (*do_replace == 0)
1234 set_new_dnode(&dn, inode, NULL, NULL, 0);
1235 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1237 dec_valid_block_count(sbi, inode, 1);
1238 f2fs_invalidate_blocks(sbi, *blkaddr);
1240 f2fs_update_data_blkaddr(&dn, *blkaddr);
1242 f2fs_put_dnode(&dn);
1247 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1248 block_t *blkaddr, int *do_replace,
1249 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1251 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1256 if (blkaddr[i] == NULL_ADDR && !full) {
1261 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1262 struct dnode_of_data dn;
1263 struct node_info ni;
1267 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1268 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1272 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1274 f2fs_put_dnode(&dn);
1278 ilen = min((pgoff_t)
1279 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1280 dn.ofs_in_node, len - i);
1282 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1283 f2fs_truncate_data_blocks_range(&dn, 1);
1285 if (do_replace[i]) {
1286 f2fs_i_blocks_write(src_inode,
1288 f2fs_i_blocks_write(dst_inode,
1290 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1291 blkaddr[i], ni.version, true, false);
1297 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1298 if (dst_inode->i_size < new_size)
1299 f2fs_i_size_write(dst_inode, new_size);
1300 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1302 f2fs_put_dnode(&dn);
1304 struct page *psrc, *pdst;
1306 psrc = f2fs_get_lock_data_page(src_inode,
1309 return PTR_ERR(psrc);
1310 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1313 f2fs_put_page(psrc, 1);
1314 return PTR_ERR(pdst);
1316 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1317 set_page_dirty(pdst);
1318 f2fs_put_page(pdst, 1);
1319 f2fs_put_page(psrc, 1);
1321 ret = f2fs_truncate_hole(src_inode,
1322 src + i, src + i + 1);
1331 static int __exchange_data_block(struct inode *src_inode,
1332 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1333 pgoff_t len, bool full)
1335 block_t *src_blkaddr;
1341 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1343 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1344 array_size(olen, sizeof(block_t)),
1349 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1350 array_size(olen, sizeof(int)),
1353 kvfree(src_blkaddr);
1357 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1358 do_replace, src, olen);
1362 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1363 do_replace, src, dst, olen, full);
1371 kvfree(src_blkaddr);
1377 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1378 kvfree(src_blkaddr);
1383 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1385 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1386 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1387 pgoff_t start = offset >> PAGE_SHIFT;
1388 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1391 f2fs_balance_fs(sbi, true);
1393 /* avoid gc operation during block exchange */
1394 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1395 filemap_invalidate_lock(inode->i_mapping);
1398 f2fs_drop_extent_tree(inode);
1399 truncate_pagecache(inode, offset);
1400 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1401 f2fs_unlock_op(sbi);
1403 filemap_invalidate_unlock(inode->i_mapping);
1404 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1408 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1413 if (offset + len >= i_size_read(inode))
1416 /* collapse range should be aligned to block size of f2fs. */
1417 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1420 ret = f2fs_convert_inline_inode(inode);
1424 /* write out all dirty pages from offset */
1425 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1429 ret = f2fs_do_collapse(inode, offset, len);
1433 /* write out all moved pages, if possible */
1434 filemap_invalidate_lock(inode->i_mapping);
1435 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1436 truncate_pagecache(inode, offset);
1438 new_size = i_size_read(inode) - len;
1439 ret = f2fs_truncate_blocks(inode, new_size, true);
1440 filemap_invalidate_unlock(inode->i_mapping);
1442 f2fs_i_size_write(inode, new_size);
1446 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1449 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1450 pgoff_t index = start;
1451 unsigned int ofs_in_node = dn->ofs_in_node;
1455 for (; index < end; index++, dn->ofs_in_node++) {
1456 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1460 dn->ofs_in_node = ofs_in_node;
1461 ret = f2fs_reserve_new_blocks(dn, count);
1465 dn->ofs_in_node = ofs_in_node;
1466 for (index = start; index < end; index++, dn->ofs_in_node++) {
1467 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1469 * f2fs_reserve_new_blocks will not guarantee entire block
1472 if (dn->data_blkaddr == NULL_ADDR) {
1477 if (dn->data_blkaddr == NEW_ADDR)
1480 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1481 DATA_GENERIC_ENHANCE)) {
1482 ret = -EFSCORRUPTED;
1486 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1487 dn->data_blkaddr = NEW_ADDR;
1488 f2fs_set_data_blkaddr(dn);
1491 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1496 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1499 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1500 struct address_space *mapping = inode->i_mapping;
1501 pgoff_t index, pg_start, pg_end;
1502 loff_t new_size = i_size_read(inode);
1503 loff_t off_start, off_end;
1506 ret = inode_newsize_ok(inode, (len + offset));
1510 ret = f2fs_convert_inline_inode(inode);
1514 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1518 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1519 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1521 off_start = offset & (PAGE_SIZE - 1);
1522 off_end = (offset + len) & (PAGE_SIZE - 1);
1524 if (pg_start == pg_end) {
1525 ret = fill_zero(inode, pg_start, off_start,
1526 off_end - off_start);
1530 new_size = max_t(loff_t, new_size, offset + len);
1533 ret = fill_zero(inode, pg_start++, off_start,
1534 PAGE_SIZE - off_start);
1538 new_size = max_t(loff_t, new_size,
1539 (loff_t)pg_start << PAGE_SHIFT);
1542 for (index = pg_start; index < pg_end;) {
1543 struct dnode_of_data dn;
1544 unsigned int end_offset;
1547 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1548 filemap_invalidate_lock(mapping);
1550 truncate_pagecache_range(inode,
1551 (loff_t)index << PAGE_SHIFT,
1552 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1556 set_new_dnode(&dn, inode, NULL, NULL, 0);
1557 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1559 f2fs_unlock_op(sbi);
1560 filemap_invalidate_unlock(mapping);
1561 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1565 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1566 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1568 ret = f2fs_do_zero_range(&dn, index, end);
1569 f2fs_put_dnode(&dn);
1571 f2fs_unlock_op(sbi);
1572 filemap_invalidate_unlock(mapping);
1573 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1575 f2fs_balance_fs(sbi, dn.node_changed);
1581 new_size = max_t(loff_t, new_size,
1582 (loff_t)index << PAGE_SHIFT);
1586 ret = fill_zero(inode, pg_end, 0, off_end);
1590 new_size = max_t(loff_t, new_size, offset + len);
1595 if (new_size > i_size_read(inode)) {
1596 if (mode & FALLOC_FL_KEEP_SIZE)
1597 file_set_keep_isize(inode);
1599 f2fs_i_size_write(inode, new_size);
1604 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1606 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1607 struct address_space *mapping = inode->i_mapping;
1608 pgoff_t nr, pg_start, pg_end, delta, idx;
1612 new_size = i_size_read(inode) + len;
1613 ret = inode_newsize_ok(inode, new_size);
1617 if (offset >= i_size_read(inode))
1620 /* insert range should be aligned to block size of f2fs. */
1621 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1624 ret = f2fs_convert_inline_inode(inode);
1628 f2fs_balance_fs(sbi, true);
1630 filemap_invalidate_lock(mapping);
1631 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1632 filemap_invalidate_unlock(mapping);
1636 /* write out all dirty pages from offset */
1637 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1641 pg_start = offset >> PAGE_SHIFT;
1642 pg_end = (offset + len) >> PAGE_SHIFT;
1643 delta = pg_end - pg_start;
1644 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1646 /* avoid gc operation during block exchange */
1647 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1648 filemap_invalidate_lock(mapping);
1649 truncate_pagecache(inode, offset);
1651 while (!ret && idx > pg_start) {
1652 nr = idx - pg_start;
1658 f2fs_drop_extent_tree(inode);
1660 ret = __exchange_data_block(inode, inode, idx,
1661 idx + delta, nr, false);
1662 f2fs_unlock_op(sbi);
1664 filemap_invalidate_unlock(mapping);
1665 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1667 /* write out all moved pages, if possible */
1668 filemap_invalidate_lock(mapping);
1669 filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1670 truncate_pagecache(inode, offset);
1671 filemap_invalidate_unlock(mapping);
1674 f2fs_i_size_write(inode, new_size);
1678 static int expand_inode_data(struct inode *inode, loff_t offset,
1679 loff_t len, int mode)
1681 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1682 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1683 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1684 .m_may_create = true };
1685 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1686 .init_gc_type = FG_GC,
1687 .should_migrate_blocks = false,
1688 .err_gc_skipped = true,
1689 .nr_free_secs = 0 };
1690 pgoff_t pg_start, pg_end;
1691 loff_t new_size = i_size_read(inode);
1693 block_t expanded = 0;
1696 err = inode_newsize_ok(inode, (len + offset));
1700 err = f2fs_convert_inline_inode(inode);
1704 f2fs_balance_fs(sbi, true);
1706 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1707 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1708 off_end = (offset + len) & (PAGE_SIZE - 1);
1710 map.m_lblk = pg_start;
1711 map.m_len = pg_end - pg_start;
1718 if (f2fs_is_pinned_file(inode)) {
1719 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1720 block_t sec_len = roundup(map.m_len, sec_blks);
1722 map.m_len = sec_blks;
1724 if (has_not_enough_free_secs(sbi, 0,
1725 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1726 f2fs_down_write(&sbi->gc_lock);
1727 err = f2fs_gc(sbi, &gc_control);
1728 if (err && err != -ENODATA)
1732 f2fs_down_write(&sbi->pin_sem);
1735 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1736 f2fs_unlock_op(sbi);
1738 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1739 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1740 file_dont_truncate(inode);
1742 f2fs_up_write(&sbi->pin_sem);
1744 expanded += map.m_len;
1745 sec_len -= map.m_len;
1746 map.m_lblk += map.m_len;
1747 if (!err && sec_len)
1750 map.m_len = expanded;
1752 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1753 expanded = map.m_len;
1762 last_off = pg_start + expanded - 1;
1764 /* update new size to the failed position */
1765 new_size = (last_off == pg_end) ? offset + len :
1766 (loff_t)(last_off + 1) << PAGE_SHIFT;
1768 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1771 if (new_size > i_size_read(inode)) {
1772 if (mode & FALLOC_FL_KEEP_SIZE)
1773 file_set_keep_isize(inode);
1775 f2fs_i_size_write(inode, new_size);
1781 static long f2fs_fallocate(struct file *file, int mode,
1782 loff_t offset, loff_t len)
1784 struct inode *inode = file_inode(file);
1787 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1789 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1791 if (!f2fs_is_compress_backend_ready(inode))
1794 /* f2fs only support ->fallocate for regular file */
1795 if (!S_ISREG(inode->i_mode))
1798 if (IS_ENCRYPTED(inode) &&
1799 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1803 * Pinned file should not support partial trucation since the block
1804 * can be used by applications.
1806 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1807 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1808 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1811 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1812 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1813 FALLOC_FL_INSERT_RANGE))
1818 ret = file_modified(file);
1822 if (mode & FALLOC_FL_PUNCH_HOLE) {
1823 if (offset >= inode->i_size)
1826 ret = punch_hole(inode, offset, len);
1827 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1828 ret = f2fs_collapse_range(inode, offset, len);
1829 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1830 ret = f2fs_zero_range(inode, offset, len, mode);
1831 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1832 ret = f2fs_insert_range(inode, offset, len);
1834 ret = expand_inode_data(inode, offset, len, mode);
1838 inode->i_mtime = inode->i_ctime = current_time(inode);
1839 f2fs_mark_inode_dirty_sync(inode, false);
1840 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1844 inode_unlock(inode);
1846 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1850 static int f2fs_release_file(struct inode *inode, struct file *filp)
1853 * f2fs_relase_file is called at every close calls. So we should
1854 * not drop any inmemory pages by close called by other process.
1856 if (!(filp->f_mode & FMODE_WRITE) ||
1857 atomic_read(&inode->i_writecount) != 1)
1860 f2fs_abort_atomic_write(inode, true);
1864 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1866 struct inode *inode = file_inode(file);
1869 * If the process doing a transaction is crashed, we should do
1870 * roll-back. Otherwise, other reader/write can see corrupted database
1871 * until all the writers close its file. Since this should be done
1872 * before dropping file lock, it needs to do in ->flush.
1874 if (F2FS_I(inode)->atomic_write_task == current)
1875 f2fs_abort_atomic_write(inode, true);
1879 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1881 struct f2fs_inode_info *fi = F2FS_I(inode);
1882 u32 masked_flags = fi->i_flags & mask;
1884 /* mask can be shrunk by flags_valid selector */
1887 /* Is it quota file? Do not allow user to mess with it */
1888 if (IS_NOQUOTA(inode))
1891 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1892 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1894 if (!f2fs_empty_dir(inode))
1898 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1899 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1901 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1905 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1906 if (masked_flags & F2FS_COMPR_FL) {
1907 if (!f2fs_disable_compressed_file(inode))
1910 if (!f2fs_may_compress(inode))
1912 if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
1914 if (set_compress_context(inode))
1919 fi->i_flags = iflags | (fi->i_flags & ~mask);
1920 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1921 (fi->i_flags & F2FS_NOCOMP_FL));
1923 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1924 set_inode_flag(inode, FI_PROJ_INHERIT);
1926 clear_inode_flag(inode, FI_PROJ_INHERIT);
1928 inode->i_ctime = current_time(inode);
1929 f2fs_set_inode_flags(inode);
1930 f2fs_mark_inode_dirty_sync(inode, true);
1934 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1937 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1938 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1939 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1940 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1942 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1943 * FS_IOC_FSSETXATTR is done by the VFS.
1946 static const struct {
1949 } f2fs_fsflags_map[] = {
1950 { F2FS_COMPR_FL, FS_COMPR_FL },
1951 { F2FS_SYNC_FL, FS_SYNC_FL },
1952 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1953 { F2FS_APPEND_FL, FS_APPEND_FL },
1954 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1955 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1956 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1957 { F2FS_INDEX_FL, FS_INDEX_FL },
1958 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1959 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1960 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1963 #define F2FS_GETTABLE_FS_FL ( \
1973 FS_PROJINHERIT_FL | \
1975 FS_INLINE_DATA_FL | \
1980 #define F2FS_SETTABLE_FS_FL ( \
1989 FS_PROJINHERIT_FL | \
1992 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1993 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1998 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1999 if (iflags & f2fs_fsflags_map[i].iflag)
2000 fsflags |= f2fs_fsflags_map[i].fsflag;
2005 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2006 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2011 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2012 if (fsflags & f2fs_fsflags_map[i].fsflag)
2013 iflags |= f2fs_fsflags_map[i].iflag;
2018 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2020 struct inode *inode = file_inode(filp);
2022 return put_user(inode->i_generation, (int __user *)arg);
2025 static int f2fs_ioc_start_atomic_write(struct file *filp)
2027 struct inode *inode = file_inode(filp);
2028 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2029 struct f2fs_inode_info *fi = F2FS_I(inode);
2030 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2031 struct inode *pinode;
2034 if (!inode_owner_or_capable(mnt_userns, inode))
2037 if (!S_ISREG(inode->i_mode))
2040 if (filp->f_flags & O_DIRECT)
2043 ret = mnt_want_write_file(filp);
2049 if (!f2fs_disable_compressed_file(inode)) {
2054 if (f2fs_is_atomic_file(inode))
2057 ret = f2fs_convert_inline_inode(inode);
2061 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2064 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2065 * f2fs_is_atomic_file.
2067 if (get_dirty_pages(inode))
2068 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2069 inode->i_ino, get_dirty_pages(inode));
2070 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2072 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2076 /* Create a COW inode for atomic write */
2077 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2078 if (IS_ERR(pinode)) {
2079 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2080 ret = PTR_ERR(pinode);
2084 ret = f2fs_get_tmpfile(mnt_userns, pinode, &fi->cow_inode);
2087 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2090 f2fs_i_size_write(fi->cow_inode, i_size_read(inode));
2092 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2093 sbi->atomic_files++;
2094 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2096 set_inode_flag(inode, FI_ATOMIC_FILE);
2097 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2098 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2099 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2101 f2fs_update_time(sbi, REQ_TIME);
2102 fi->atomic_write_task = current;
2103 stat_update_max_atomic_write(inode);
2104 fi->atomic_write_cnt = 0;
2106 inode_unlock(inode);
2107 mnt_drop_write_file(filp);
2111 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2113 struct inode *inode = file_inode(filp);
2114 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2117 if (!inode_owner_or_capable(mnt_userns, inode))
2120 ret = mnt_want_write_file(filp);
2124 f2fs_balance_fs(F2FS_I_SB(inode), true);
2128 if (f2fs_is_atomic_file(inode)) {
2129 ret = f2fs_commit_atomic_write(inode);
2133 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2135 f2fs_abort_atomic_write(inode, false);
2137 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2140 inode_unlock(inode);
2141 mnt_drop_write_file(filp);
2145 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2147 struct inode *inode = file_inode(filp);
2148 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2151 if (!inode_owner_or_capable(mnt_userns, inode))
2154 ret = mnt_want_write_file(filp);
2160 f2fs_abort_atomic_write(inode, true);
2162 inode_unlock(inode);
2164 mnt_drop_write_file(filp);
2165 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2169 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2171 struct inode *inode = file_inode(filp);
2172 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2173 struct super_block *sb = sbi->sb;
2177 if (!capable(CAP_SYS_ADMIN))
2180 if (get_user(in, (__u32 __user *)arg))
2183 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2184 ret = mnt_want_write_file(filp);
2186 if (ret == -EROFS) {
2188 f2fs_stop_checkpoint(sbi, false);
2189 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2190 trace_f2fs_shutdown(sbi, in, ret);
2197 case F2FS_GOING_DOWN_FULLSYNC:
2198 ret = freeze_bdev(sb->s_bdev);
2201 f2fs_stop_checkpoint(sbi, false);
2202 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2203 thaw_bdev(sb->s_bdev);
2205 case F2FS_GOING_DOWN_METASYNC:
2206 /* do checkpoint only */
2207 ret = f2fs_sync_fs(sb, 1);
2210 f2fs_stop_checkpoint(sbi, false);
2211 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2213 case F2FS_GOING_DOWN_NOSYNC:
2214 f2fs_stop_checkpoint(sbi, false);
2215 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2217 case F2FS_GOING_DOWN_METAFLUSH:
2218 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2219 f2fs_stop_checkpoint(sbi, false);
2220 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2222 case F2FS_GOING_DOWN_NEED_FSCK:
2223 set_sbi_flag(sbi, SBI_NEED_FSCK);
2224 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2225 set_sbi_flag(sbi, SBI_IS_DIRTY);
2226 /* do checkpoint only */
2227 ret = f2fs_sync_fs(sb, 1);
2234 f2fs_stop_gc_thread(sbi);
2235 f2fs_stop_discard_thread(sbi);
2237 f2fs_drop_discard_cmd(sbi);
2238 clear_opt(sbi, DISCARD);
2240 f2fs_update_time(sbi, REQ_TIME);
2242 if (in != F2FS_GOING_DOWN_FULLSYNC)
2243 mnt_drop_write_file(filp);
2245 trace_f2fs_shutdown(sbi, in, ret);
2250 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2252 struct inode *inode = file_inode(filp);
2253 struct super_block *sb = inode->i_sb;
2254 struct fstrim_range range;
2257 if (!capable(CAP_SYS_ADMIN))
2260 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2263 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2267 ret = mnt_want_write_file(filp);
2271 range.minlen = max((unsigned int)range.minlen,
2272 bdev_discard_granularity(sb->s_bdev));
2273 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2274 mnt_drop_write_file(filp);
2278 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2281 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2285 static bool uuid_is_nonzero(__u8 u[16])
2289 for (i = 0; i < 16; i++)
2295 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2297 struct inode *inode = file_inode(filp);
2299 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2302 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2304 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2307 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2309 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2311 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2314 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2316 struct inode *inode = file_inode(filp);
2317 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2320 if (!f2fs_sb_has_encrypt(sbi))
2323 err = mnt_want_write_file(filp);
2327 f2fs_down_write(&sbi->sb_lock);
2329 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2332 /* update superblock with uuid */
2333 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2335 err = f2fs_commit_super(sbi, false);
2338 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2342 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2346 f2fs_up_write(&sbi->sb_lock);
2347 mnt_drop_write_file(filp);
2351 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2354 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2357 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2360 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2362 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2365 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2368 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2370 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2373 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2376 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2379 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2382 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2385 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2388 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2391 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2394 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2396 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2399 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2402 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2404 struct inode *inode = file_inode(filp);
2405 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2406 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2408 .should_migrate_blocks = false,
2409 .nr_free_secs = 0 };
2413 if (!capable(CAP_SYS_ADMIN))
2416 if (get_user(sync, (__u32 __user *)arg))
2419 if (f2fs_readonly(sbi->sb))
2422 ret = mnt_want_write_file(filp);
2427 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2432 f2fs_down_write(&sbi->gc_lock);
2435 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2436 gc_control.err_gc_skipped = sync;
2437 ret = f2fs_gc(sbi, &gc_control);
2439 mnt_drop_write_file(filp);
2443 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2445 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2446 struct f2fs_gc_control gc_control = {
2447 .init_gc_type = range->sync ? FG_GC : BG_GC,
2449 .should_migrate_blocks = false,
2450 .err_gc_skipped = range->sync,
2451 .nr_free_secs = 0 };
2455 if (!capable(CAP_SYS_ADMIN))
2457 if (f2fs_readonly(sbi->sb))
2460 end = range->start + range->len;
2461 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2462 end >= MAX_BLKADDR(sbi))
2465 ret = mnt_want_write_file(filp);
2471 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2476 f2fs_down_write(&sbi->gc_lock);
2479 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2480 ret = f2fs_gc(sbi, &gc_control);
2486 range->start += CAP_BLKS_PER_SEC(sbi);
2487 if (range->start <= end)
2490 mnt_drop_write_file(filp);
2494 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2496 struct f2fs_gc_range range;
2498 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2501 return __f2fs_ioc_gc_range(filp, &range);
2504 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2506 struct inode *inode = file_inode(filp);
2507 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2510 if (!capable(CAP_SYS_ADMIN))
2513 if (f2fs_readonly(sbi->sb))
2516 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2517 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2521 ret = mnt_want_write_file(filp);
2525 ret = f2fs_sync_fs(sbi->sb, 1);
2527 mnt_drop_write_file(filp);
2531 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2533 struct f2fs_defragment *range)
2535 struct inode *inode = file_inode(filp);
2536 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2537 .m_seg_type = NO_CHECK_TYPE,
2538 .m_may_create = false };
2539 struct extent_info ei = {0, 0, 0};
2540 pgoff_t pg_start, pg_end, next_pgofs;
2541 unsigned int blk_per_seg = sbi->blocks_per_seg;
2542 unsigned int total = 0, sec_num;
2543 block_t blk_end = 0;
2544 bool fragmented = false;
2547 pg_start = range->start >> PAGE_SHIFT;
2548 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2550 f2fs_balance_fs(sbi, true);
2554 /* if in-place-update policy is enabled, don't waste time here */
2555 set_inode_flag(inode, FI_OPU_WRITE);
2556 if (f2fs_should_update_inplace(inode, NULL)) {
2561 /* writeback all dirty pages in the range */
2562 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2563 range->start + range->len - 1);
2568 * lookup mapping info in extent cache, skip defragmenting if physical
2569 * block addresses are continuous.
2571 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2572 if (ei.fofs + ei.len >= pg_end)
2576 map.m_lblk = pg_start;
2577 map.m_next_pgofs = &next_pgofs;
2580 * lookup mapping info in dnode page cache, skip defragmenting if all
2581 * physical block addresses are continuous even if there are hole(s)
2582 * in logical blocks.
2584 while (map.m_lblk < pg_end) {
2585 map.m_len = pg_end - map.m_lblk;
2586 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2590 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2591 map.m_lblk = next_pgofs;
2595 if (blk_end && blk_end != map.m_pblk)
2598 /* record total count of block that we're going to move */
2601 blk_end = map.m_pblk + map.m_len;
2603 map.m_lblk += map.m_len;
2611 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2614 * make sure there are enough free section for LFS allocation, this can
2615 * avoid defragment running in SSR mode when free section are allocated
2618 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2623 map.m_lblk = pg_start;
2624 map.m_len = pg_end - pg_start;
2627 while (map.m_lblk < pg_end) {
2632 map.m_len = pg_end - map.m_lblk;
2633 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2637 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2638 map.m_lblk = next_pgofs;
2642 set_inode_flag(inode, FI_SKIP_WRITES);
2645 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2648 page = f2fs_get_lock_data_page(inode, idx, true);
2650 err = PTR_ERR(page);
2654 set_page_dirty(page);
2655 set_page_private_gcing(page);
2656 f2fs_put_page(page, 1);
2665 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2668 clear_inode_flag(inode, FI_SKIP_WRITES);
2670 err = filemap_fdatawrite(inode->i_mapping);
2675 clear_inode_flag(inode, FI_SKIP_WRITES);
2677 clear_inode_flag(inode, FI_OPU_WRITE);
2678 inode_unlock(inode);
2680 range->len = (u64)total << PAGE_SHIFT;
2684 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2686 struct inode *inode = file_inode(filp);
2687 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2688 struct f2fs_defragment range;
2691 if (!capable(CAP_SYS_ADMIN))
2694 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2697 if (f2fs_readonly(sbi->sb))
2700 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2704 /* verify alignment of offset & size */
2705 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2708 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2709 max_file_blocks(inode)))
2712 err = mnt_want_write_file(filp);
2716 err = f2fs_defragment_range(sbi, filp, &range);
2717 mnt_drop_write_file(filp);
2719 f2fs_update_time(sbi, REQ_TIME);
2723 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2730 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2731 struct file *file_out, loff_t pos_out, size_t len)
2733 struct inode *src = file_inode(file_in);
2734 struct inode *dst = file_inode(file_out);
2735 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2736 size_t olen = len, dst_max_i_size = 0;
2740 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2741 src->i_sb != dst->i_sb)
2744 if (unlikely(f2fs_readonly(src->i_sb)))
2747 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2750 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2753 if (pos_out < 0 || pos_in < 0)
2757 if (pos_in == pos_out)
2759 if (pos_out > pos_in && pos_out < pos_in + len)
2766 if (!inode_trylock(dst))
2771 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2774 olen = len = src->i_size - pos_in;
2775 if (pos_in + len == src->i_size)
2776 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2782 dst_osize = dst->i_size;
2783 if (pos_out + olen > dst->i_size)
2784 dst_max_i_size = pos_out + olen;
2786 /* verify the end result is block aligned */
2787 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2788 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2789 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2792 ret = f2fs_convert_inline_inode(src);
2796 ret = f2fs_convert_inline_inode(dst);
2800 /* write out all dirty pages from offset */
2801 ret = filemap_write_and_wait_range(src->i_mapping,
2802 pos_in, pos_in + len);
2806 ret = filemap_write_and_wait_range(dst->i_mapping,
2807 pos_out, pos_out + len);
2811 f2fs_balance_fs(sbi, true);
2813 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2816 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2821 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2822 pos_out >> F2FS_BLKSIZE_BITS,
2823 len >> F2FS_BLKSIZE_BITS, false);
2827 f2fs_i_size_write(dst, dst_max_i_size);
2828 else if (dst_osize != dst->i_size)
2829 f2fs_i_size_write(dst, dst_osize);
2831 f2fs_unlock_op(sbi);
2834 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2836 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2845 static int __f2fs_ioc_move_range(struct file *filp,
2846 struct f2fs_move_range *range)
2851 if (!(filp->f_mode & FMODE_READ) ||
2852 !(filp->f_mode & FMODE_WRITE))
2855 dst = fdget(range->dst_fd);
2859 if (!(dst.file->f_mode & FMODE_WRITE)) {
2864 err = mnt_want_write_file(filp);
2868 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2869 range->pos_out, range->len);
2871 mnt_drop_write_file(filp);
2877 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2879 struct f2fs_move_range range;
2881 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2884 return __f2fs_ioc_move_range(filp, &range);
2887 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2889 struct inode *inode = file_inode(filp);
2890 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2891 struct sit_info *sm = SIT_I(sbi);
2892 unsigned int start_segno = 0, end_segno = 0;
2893 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2894 struct f2fs_flush_device range;
2895 struct f2fs_gc_control gc_control = {
2896 .init_gc_type = FG_GC,
2897 .should_migrate_blocks = true,
2898 .err_gc_skipped = true,
2899 .nr_free_secs = 0 };
2902 if (!capable(CAP_SYS_ADMIN))
2905 if (f2fs_readonly(sbi->sb))
2908 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2911 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2915 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2916 __is_large_section(sbi)) {
2917 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2918 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2922 ret = mnt_want_write_file(filp);
2926 if (range.dev_num != 0)
2927 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2928 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2930 start_segno = sm->last_victim[FLUSH_DEVICE];
2931 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2932 start_segno = dev_start_segno;
2933 end_segno = min(start_segno + range.segments, dev_end_segno);
2935 while (start_segno < end_segno) {
2936 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2940 sm->last_victim[GC_CB] = end_segno + 1;
2941 sm->last_victim[GC_GREEDY] = end_segno + 1;
2942 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2944 gc_control.victim_segno = start_segno;
2945 ret = f2fs_gc(sbi, &gc_control);
2953 mnt_drop_write_file(filp);
2957 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2959 struct inode *inode = file_inode(filp);
2960 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2962 /* Must validate to set it with SQLite behavior in Android. */
2963 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2965 return put_user(sb_feature, (u32 __user *)arg);
2969 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2971 struct dquot *transfer_to[MAXQUOTAS] = {};
2972 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2973 struct super_block *sb = sbi->sb;
2976 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2977 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2978 err = __dquot_transfer(inode, transfer_to);
2980 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2981 dqput(transfer_to[PRJQUOTA]);
2986 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
2988 struct f2fs_inode_info *fi = F2FS_I(inode);
2989 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2990 struct f2fs_inode *ri = NULL;
2994 if (!f2fs_sb_has_project_quota(sbi)) {
2995 if (projid != F2FS_DEF_PROJID)
3001 if (!f2fs_has_extra_attr(inode))
3004 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3006 if (projid_eq(kprojid, fi->i_projid))
3010 /* Is it quota file? Do not allow user to mess with it */
3011 if (IS_NOQUOTA(inode))
3014 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3017 err = f2fs_dquot_initialize(inode);
3022 err = f2fs_transfer_project_quota(inode, kprojid);
3026 fi->i_projid = kprojid;
3027 inode->i_ctime = current_time(inode);
3028 f2fs_mark_inode_dirty_sync(inode, true);
3030 f2fs_unlock_op(sbi);
3034 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3039 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3041 if (projid != F2FS_DEF_PROJID)
3047 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3049 struct inode *inode = d_inode(dentry);
3050 struct f2fs_inode_info *fi = F2FS_I(inode);
3051 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3053 if (IS_ENCRYPTED(inode))
3054 fsflags |= FS_ENCRYPT_FL;
3055 if (IS_VERITY(inode))
3056 fsflags |= FS_VERITY_FL;
3057 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3058 fsflags |= FS_INLINE_DATA_FL;
3059 if (is_inode_flag_set(inode, FI_PIN_FILE))
3060 fsflags |= FS_NOCOW_FL;
3062 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3064 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3065 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3070 int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3071 struct dentry *dentry, struct fileattr *fa)
3073 struct inode *inode = d_inode(dentry);
3074 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3078 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3080 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3082 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3084 fsflags &= F2FS_SETTABLE_FS_FL;
3085 if (!fa->flags_valid)
3086 mask &= FS_COMMON_FL;
3088 iflags = f2fs_fsflags_to_iflags(fsflags);
3089 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3092 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3094 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3099 int f2fs_pin_file_control(struct inode *inode, bool inc)
3101 struct f2fs_inode_info *fi = F2FS_I(inode);
3102 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3104 /* Use i_gc_failures for normal file as a risk signal. */
3106 f2fs_i_gc_failures_write(inode,
3107 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3109 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3110 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3111 __func__, inode->i_ino,
3112 fi->i_gc_failures[GC_FAILURE_PIN]);
3113 clear_inode_flag(inode, FI_PIN_FILE);
3119 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3121 struct inode *inode = file_inode(filp);
3125 if (get_user(pin, (__u32 __user *)arg))
3128 if (!S_ISREG(inode->i_mode))
3131 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3134 ret = mnt_want_write_file(filp);
3141 clear_inode_flag(inode, FI_PIN_FILE);
3142 f2fs_i_gc_failures_write(inode, 0);
3146 if (f2fs_should_update_outplace(inode, NULL)) {
3151 if (f2fs_pin_file_control(inode, false)) {
3156 ret = f2fs_convert_inline_inode(inode);
3160 if (!f2fs_disable_compressed_file(inode)) {
3165 set_inode_flag(inode, FI_PIN_FILE);
3166 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3168 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3170 inode_unlock(inode);
3171 mnt_drop_write_file(filp);
3175 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3177 struct inode *inode = file_inode(filp);
3180 if (is_inode_flag_set(inode, FI_PIN_FILE))
3181 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3182 return put_user(pin, (u32 __user *)arg);
3185 int f2fs_precache_extents(struct inode *inode)
3187 struct f2fs_inode_info *fi = F2FS_I(inode);
3188 struct f2fs_map_blocks map;
3189 pgoff_t m_next_extent;
3193 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3197 map.m_next_pgofs = NULL;
3198 map.m_next_extent = &m_next_extent;
3199 map.m_seg_type = NO_CHECK_TYPE;
3200 map.m_may_create = false;
3201 end = max_file_blocks(inode);
3203 while (map.m_lblk < end) {
3204 map.m_len = end - map.m_lblk;
3206 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3207 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3208 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3212 map.m_lblk = m_next_extent;
3218 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3220 return f2fs_precache_extents(file_inode(filp));
3223 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3225 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3228 if (!capable(CAP_SYS_ADMIN))
3231 if (f2fs_readonly(sbi->sb))
3234 if (copy_from_user(&block_count, (void __user *)arg,
3235 sizeof(block_count)))
3238 return f2fs_resize_fs(sbi, block_count);
3241 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3243 struct inode *inode = file_inode(filp);
3245 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3247 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3248 f2fs_warn(F2FS_I_SB(inode),
3249 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3254 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3257 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3259 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3262 return fsverity_ioctl_measure(filp, (void __user *)arg);
3265 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3267 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3270 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3273 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3275 struct inode *inode = file_inode(filp);
3276 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3281 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3285 f2fs_down_read(&sbi->sb_lock);
3286 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3287 ARRAY_SIZE(sbi->raw_super->volume_name),
3288 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3289 f2fs_up_read(&sbi->sb_lock);
3291 if (copy_to_user((char __user *)arg, vbuf,
3292 min(FSLABEL_MAX, count)))
3299 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3301 struct inode *inode = file_inode(filp);
3302 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3306 if (!capable(CAP_SYS_ADMIN))
3309 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3311 return PTR_ERR(vbuf);
3313 err = mnt_want_write_file(filp);
3317 f2fs_down_write(&sbi->sb_lock);
3319 memset(sbi->raw_super->volume_name, 0,
3320 sizeof(sbi->raw_super->volume_name));
3321 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3322 sbi->raw_super->volume_name,
3323 ARRAY_SIZE(sbi->raw_super->volume_name));
3325 err = f2fs_commit_super(sbi, false);
3327 f2fs_up_write(&sbi->sb_lock);
3329 mnt_drop_write_file(filp);
3335 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3337 struct inode *inode = file_inode(filp);
3340 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3343 if (!f2fs_compressed_file(inode))
3346 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3347 return put_user(blocks, (u64 __user *)arg);
3350 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3352 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3353 unsigned int released_blocks = 0;
3354 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3358 for (i = 0; i < count; i++) {
3359 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3360 dn->ofs_in_node + i);
3362 if (!__is_valid_data_blkaddr(blkaddr))
3364 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3365 DATA_GENERIC_ENHANCE)))
3366 return -EFSCORRUPTED;
3370 int compr_blocks = 0;
3372 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3373 blkaddr = f2fs_data_blkaddr(dn);
3376 if (blkaddr == COMPRESS_ADDR)
3378 dn->ofs_in_node += cluster_size;
3382 if (__is_valid_data_blkaddr(blkaddr))
3385 if (blkaddr != NEW_ADDR)
3388 dn->data_blkaddr = NULL_ADDR;
3389 f2fs_set_data_blkaddr(dn);
3392 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3393 dec_valid_block_count(sbi, dn->inode,
3394 cluster_size - compr_blocks);
3396 released_blocks += cluster_size - compr_blocks;
3398 count -= cluster_size;
3401 return released_blocks;
3404 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3406 struct inode *inode = file_inode(filp);
3407 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3408 pgoff_t page_idx = 0, last_idx;
3409 unsigned int released_blocks = 0;
3413 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3416 if (!f2fs_compressed_file(inode))
3419 if (f2fs_readonly(sbi->sb))
3422 ret = mnt_want_write_file(filp);
3426 f2fs_balance_fs(F2FS_I_SB(inode), true);
3430 writecount = atomic_read(&inode->i_writecount);
3431 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3432 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3437 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3442 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3446 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3447 inode->i_ctime = current_time(inode);
3448 f2fs_mark_inode_dirty_sync(inode, true);
3450 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3453 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3454 filemap_invalidate_lock(inode->i_mapping);
3456 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3458 while (page_idx < last_idx) {
3459 struct dnode_of_data dn;
3460 pgoff_t end_offset, count;
3462 set_new_dnode(&dn, inode, NULL, NULL, 0);
3463 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3465 if (ret == -ENOENT) {
3466 page_idx = f2fs_get_next_page_offset(&dn,
3474 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3475 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3476 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3478 ret = release_compress_blocks(&dn, count);
3480 f2fs_put_dnode(&dn);
3486 released_blocks += ret;
3489 filemap_invalidate_unlock(inode->i_mapping);
3490 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3492 inode_unlock(inode);
3494 mnt_drop_write_file(filp);
3497 ret = put_user(released_blocks, (u64 __user *)arg);
3498 } else if (released_blocks &&
3499 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3500 set_sbi_flag(sbi, SBI_NEED_FSCK);
3501 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3502 "iblocks=%llu, released=%u, compr_blocks=%u, "
3504 __func__, inode->i_ino, inode->i_blocks,
3506 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3512 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3514 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3515 unsigned int reserved_blocks = 0;
3516 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3520 for (i = 0; i < count; i++) {
3521 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3522 dn->ofs_in_node + i);
3524 if (!__is_valid_data_blkaddr(blkaddr))
3526 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3527 DATA_GENERIC_ENHANCE)))
3528 return -EFSCORRUPTED;
3532 int compr_blocks = 0;
3536 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3537 blkaddr = f2fs_data_blkaddr(dn);
3540 if (blkaddr == COMPRESS_ADDR)
3542 dn->ofs_in_node += cluster_size;
3546 if (__is_valid_data_blkaddr(blkaddr)) {
3551 dn->data_blkaddr = NEW_ADDR;
3552 f2fs_set_data_blkaddr(dn);
3555 reserved = cluster_size - compr_blocks;
3556 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3560 if (reserved != cluster_size - compr_blocks)
3563 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3565 reserved_blocks += reserved;
3567 count -= cluster_size;
3570 return reserved_blocks;
3573 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3575 struct inode *inode = file_inode(filp);
3576 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3577 pgoff_t page_idx = 0, last_idx;
3578 unsigned int reserved_blocks = 0;
3581 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3584 if (!f2fs_compressed_file(inode))
3587 if (f2fs_readonly(sbi->sb))
3590 ret = mnt_want_write_file(filp);
3594 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3597 f2fs_balance_fs(F2FS_I_SB(inode), true);
3601 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3606 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3607 filemap_invalidate_lock(inode->i_mapping);
3609 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3611 while (page_idx < last_idx) {
3612 struct dnode_of_data dn;
3613 pgoff_t end_offset, count;
3615 set_new_dnode(&dn, inode, NULL, NULL, 0);
3616 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3618 if (ret == -ENOENT) {
3619 page_idx = f2fs_get_next_page_offset(&dn,
3627 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3628 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3629 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3631 ret = reserve_compress_blocks(&dn, count);
3633 f2fs_put_dnode(&dn);
3639 reserved_blocks += ret;
3642 filemap_invalidate_unlock(inode->i_mapping);
3643 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3646 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3647 inode->i_ctime = current_time(inode);
3648 f2fs_mark_inode_dirty_sync(inode, true);
3651 inode_unlock(inode);
3653 mnt_drop_write_file(filp);
3656 ret = put_user(reserved_blocks, (u64 __user *)arg);
3657 } else if (reserved_blocks &&
3658 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3659 set_sbi_flag(sbi, SBI_NEED_FSCK);
3660 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3661 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3663 __func__, inode->i_ino, inode->i_blocks,
3665 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3671 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3672 pgoff_t off, block_t block, block_t len, u32 flags)
3674 sector_t sector = SECTOR_FROM_BLOCK(block);
3675 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3678 if (flags & F2FS_TRIM_FILE_DISCARD) {
3679 if (bdev_max_secure_erase_sectors(bdev))
3680 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3683 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3687 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3688 if (IS_ENCRYPTED(inode))
3689 ret = fscrypt_zeroout_range(inode, off, block, len);
3691 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3698 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3700 struct inode *inode = file_inode(filp);
3701 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3702 struct address_space *mapping = inode->i_mapping;
3703 struct block_device *prev_bdev = NULL;
3704 struct f2fs_sectrim_range range;
3705 pgoff_t index, pg_end, prev_index = 0;
3706 block_t prev_block = 0, len = 0;
3708 bool to_end = false;
3711 if (!(filp->f_mode & FMODE_WRITE))
3714 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3718 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3719 !S_ISREG(inode->i_mode))
3722 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3723 !f2fs_hw_support_discard(sbi)) ||
3724 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3725 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3728 file_start_write(filp);
3731 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3732 range.start >= inode->i_size) {
3740 if (inode->i_size - range.start > range.len) {
3741 end_addr = range.start + range.len;
3743 end_addr = range.len == (u64)-1 ?
3744 sbi->sb->s_maxbytes : inode->i_size;
3748 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3749 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3754 index = F2FS_BYTES_TO_BLK(range.start);
3755 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3757 ret = f2fs_convert_inline_inode(inode);
3761 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3762 filemap_invalidate_lock(mapping);
3764 ret = filemap_write_and_wait_range(mapping, range.start,
3765 to_end ? LLONG_MAX : end_addr - 1);
3769 truncate_inode_pages_range(mapping, range.start,
3770 to_end ? -1 : end_addr - 1);
3772 while (index < pg_end) {
3773 struct dnode_of_data dn;
3774 pgoff_t end_offset, count;
3777 set_new_dnode(&dn, inode, NULL, NULL, 0);
3778 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3780 if (ret == -ENOENT) {
3781 index = f2fs_get_next_page_offset(&dn, index);
3787 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3788 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3789 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3790 struct block_device *cur_bdev;
3791 block_t blkaddr = f2fs_data_blkaddr(&dn);
3793 if (!__is_valid_data_blkaddr(blkaddr))
3796 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3797 DATA_GENERIC_ENHANCE)) {
3798 ret = -EFSCORRUPTED;
3799 f2fs_put_dnode(&dn);
3803 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3804 if (f2fs_is_multi_device(sbi)) {
3805 int di = f2fs_target_device_index(sbi, blkaddr);
3807 blkaddr -= FDEV(di).start_blk;
3811 if (prev_bdev == cur_bdev &&
3812 index == prev_index + len &&
3813 blkaddr == prev_block + len) {
3816 ret = f2fs_secure_erase(prev_bdev,
3817 inode, prev_index, prev_block,
3820 f2fs_put_dnode(&dn);
3829 prev_bdev = cur_bdev;
3831 prev_block = blkaddr;
3836 f2fs_put_dnode(&dn);
3838 if (fatal_signal_pending(current)) {
3846 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3847 prev_block, len, range.flags);
3849 filemap_invalidate_unlock(mapping);
3850 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3852 inode_unlock(inode);
3853 file_end_write(filp);
3858 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3860 struct inode *inode = file_inode(filp);
3861 struct f2fs_comp_option option;
3863 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3866 inode_lock_shared(inode);
3868 if (!f2fs_compressed_file(inode)) {
3869 inode_unlock_shared(inode);
3873 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3874 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3876 inode_unlock_shared(inode);
3878 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3885 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3887 struct inode *inode = file_inode(filp);
3888 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3889 struct f2fs_comp_option option;
3892 if (!f2fs_sb_has_compression(sbi))
3895 if (!(filp->f_mode & FMODE_WRITE))
3898 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3902 if (!f2fs_compressed_file(inode) ||
3903 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3904 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3905 option.algorithm >= COMPRESS_MAX)
3908 file_start_write(filp);
3911 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3916 if (inode->i_size != 0) {
3921 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3922 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3923 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
3924 f2fs_mark_inode_dirty_sync(inode, true);
3926 if (!f2fs_is_compress_backend_ready(inode))
3927 f2fs_warn(sbi, "compression algorithm is successfully set, "
3928 "but current kernel doesn't support this algorithm.");
3930 inode_unlock(inode);
3931 file_end_write(filp);
3936 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
3938 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
3939 struct address_space *mapping = inode->i_mapping;
3941 pgoff_t redirty_idx = page_idx;
3942 int i, page_len = 0, ret = 0;
3944 page_cache_ra_unbounded(&ractl, len, 0);
3946 for (i = 0; i < len; i++, page_idx++) {
3947 page = read_cache_page(mapping, page_idx, NULL, NULL);
3949 ret = PTR_ERR(page);
3955 for (i = 0; i < page_len; i++, redirty_idx++) {
3956 page = find_lock_page(mapping, redirty_idx);
3958 /* It will never fail, when page has pinned above */
3959 f2fs_bug_on(F2FS_I_SB(inode), !page);
3961 set_page_dirty(page);
3962 f2fs_put_page(page, 1);
3963 f2fs_put_page(page, 0);
3969 static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
3971 struct inode *inode = file_inode(filp);
3972 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3973 struct f2fs_inode_info *fi = F2FS_I(inode);
3974 pgoff_t page_idx = 0, last_idx;
3975 unsigned int blk_per_seg = sbi->blocks_per_seg;
3976 int cluster_size = fi->i_cluster_size;
3979 if (!f2fs_sb_has_compression(sbi) ||
3980 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
3983 if (!(filp->f_mode & FMODE_WRITE))
3986 if (!f2fs_compressed_file(inode))
3989 f2fs_balance_fs(F2FS_I_SB(inode), true);
3991 file_start_write(filp);
3994 if (!f2fs_is_compress_backend_ready(inode)) {
3999 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4004 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4008 if (!atomic_read(&fi->i_compr_blocks))
4011 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4013 count = last_idx - page_idx;
4015 int len = min(cluster_size, count);
4017 ret = redirty_blocks(inode, page_idx, len);
4021 if (get_dirty_pages(inode) >= blk_per_seg)
4022 filemap_fdatawrite(inode->i_mapping);
4029 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4033 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4036 inode_unlock(inode);
4037 file_end_write(filp);
4042 static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4044 struct inode *inode = file_inode(filp);
4045 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4046 pgoff_t page_idx = 0, last_idx;
4047 unsigned int blk_per_seg = sbi->blocks_per_seg;
4048 int cluster_size = F2FS_I(inode)->i_cluster_size;
4051 if (!f2fs_sb_has_compression(sbi) ||
4052 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4055 if (!(filp->f_mode & FMODE_WRITE))
4058 if (!f2fs_compressed_file(inode))
4061 f2fs_balance_fs(F2FS_I_SB(inode), true);
4063 file_start_write(filp);
4066 if (!f2fs_is_compress_backend_ready(inode)) {
4071 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4076 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4080 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4082 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4084 count = last_idx - page_idx;
4086 int len = min(cluster_size, count);
4088 ret = redirty_blocks(inode, page_idx, len);
4092 if (get_dirty_pages(inode) >= blk_per_seg)
4093 filemap_fdatawrite(inode->i_mapping);
4100 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4103 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4106 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4109 inode_unlock(inode);
4110 file_end_write(filp);
4115 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4118 case FS_IOC_GETVERSION:
4119 return f2fs_ioc_getversion(filp, arg);
4120 case F2FS_IOC_START_ATOMIC_WRITE:
4121 return f2fs_ioc_start_atomic_write(filp);
4122 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4123 return f2fs_ioc_commit_atomic_write(filp);
4124 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4125 return f2fs_ioc_abort_atomic_write(filp);
4126 case F2FS_IOC_START_VOLATILE_WRITE:
4127 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4129 case F2FS_IOC_SHUTDOWN:
4130 return f2fs_ioc_shutdown(filp, arg);
4132 return f2fs_ioc_fitrim(filp, arg);
4133 case FS_IOC_SET_ENCRYPTION_POLICY:
4134 return f2fs_ioc_set_encryption_policy(filp, arg);
4135 case FS_IOC_GET_ENCRYPTION_POLICY:
4136 return f2fs_ioc_get_encryption_policy(filp, arg);
4137 case FS_IOC_GET_ENCRYPTION_PWSALT:
4138 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4139 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4140 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4141 case FS_IOC_ADD_ENCRYPTION_KEY:
4142 return f2fs_ioc_add_encryption_key(filp, arg);
4143 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4144 return f2fs_ioc_remove_encryption_key(filp, arg);
4145 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4146 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4147 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4148 return f2fs_ioc_get_encryption_key_status(filp, arg);
4149 case FS_IOC_GET_ENCRYPTION_NONCE:
4150 return f2fs_ioc_get_encryption_nonce(filp, arg);
4151 case F2FS_IOC_GARBAGE_COLLECT:
4152 return f2fs_ioc_gc(filp, arg);
4153 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4154 return f2fs_ioc_gc_range(filp, arg);
4155 case F2FS_IOC_WRITE_CHECKPOINT:
4156 return f2fs_ioc_write_checkpoint(filp, arg);
4157 case F2FS_IOC_DEFRAGMENT:
4158 return f2fs_ioc_defragment(filp, arg);
4159 case F2FS_IOC_MOVE_RANGE:
4160 return f2fs_ioc_move_range(filp, arg);
4161 case F2FS_IOC_FLUSH_DEVICE:
4162 return f2fs_ioc_flush_device(filp, arg);
4163 case F2FS_IOC_GET_FEATURES:
4164 return f2fs_ioc_get_features(filp, arg);
4165 case F2FS_IOC_GET_PIN_FILE:
4166 return f2fs_ioc_get_pin_file(filp, arg);
4167 case F2FS_IOC_SET_PIN_FILE:
4168 return f2fs_ioc_set_pin_file(filp, arg);
4169 case F2FS_IOC_PRECACHE_EXTENTS:
4170 return f2fs_ioc_precache_extents(filp, arg);
4171 case F2FS_IOC_RESIZE_FS:
4172 return f2fs_ioc_resize_fs(filp, arg);
4173 case FS_IOC_ENABLE_VERITY:
4174 return f2fs_ioc_enable_verity(filp, arg);
4175 case FS_IOC_MEASURE_VERITY:
4176 return f2fs_ioc_measure_verity(filp, arg);
4177 case FS_IOC_READ_VERITY_METADATA:
4178 return f2fs_ioc_read_verity_metadata(filp, arg);
4179 case FS_IOC_GETFSLABEL:
4180 return f2fs_ioc_getfslabel(filp, arg);
4181 case FS_IOC_SETFSLABEL:
4182 return f2fs_ioc_setfslabel(filp, arg);
4183 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4184 return f2fs_get_compress_blocks(filp, arg);
4185 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4186 return f2fs_release_compress_blocks(filp, arg);
4187 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4188 return f2fs_reserve_compress_blocks(filp, arg);
4189 case F2FS_IOC_SEC_TRIM_FILE:
4190 return f2fs_sec_trim_file(filp, arg);
4191 case F2FS_IOC_GET_COMPRESS_OPTION:
4192 return f2fs_ioc_get_compress_option(filp, arg);
4193 case F2FS_IOC_SET_COMPRESS_OPTION:
4194 return f2fs_ioc_set_compress_option(filp, arg);
4195 case F2FS_IOC_DECOMPRESS_FILE:
4196 return f2fs_ioc_decompress_file(filp, arg);
4197 case F2FS_IOC_COMPRESS_FILE:
4198 return f2fs_ioc_compress_file(filp, arg);
4204 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4206 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4208 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4211 return __f2fs_ioctl(filp, cmd, arg);
4215 * Return %true if the given read or write request should use direct I/O, or
4216 * %false if it should use buffered I/O.
4218 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4219 struct iov_iter *iter)
4223 if (!(iocb->ki_flags & IOCB_DIRECT))
4226 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4230 * Direct I/O not aligned to the disk's logical_block_size will be
4231 * attempted, but will fail with -EINVAL.
4233 * f2fs additionally requires that direct I/O be aligned to the
4234 * filesystem block size, which is often a stricter requirement.
4235 * However, f2fs traditionally falls back to buffered I/O on requests
4236 * that are logical_block_size-aligned but not fs-block aligned.
4238 * The below logic implements this behavior.
4240 align = iocb->ki_pos | iov_iter_alignment(iter);
4241 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4242 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4248 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4251 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4253 dec_page_count(sbi, F2FS_DIO_READ);
4256 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, size);
4260 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4261 .end_io = f2fs_dio_read_end_io,
4264 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4266 struct file *file = iocb->ki_filp;
4267 struct inode *inode = file_inode(file);
4268 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4269 struct f2fs_inode_info *fi = F2FS_I(inode);
4270 const loff_t pos = iocb->ki_pos;
4271 const size_t count = iov_iter_count(to);
4272 struct iomap_dio *dio;
4276 return 0; /* skip atime update */
4278 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4280 if (iocb->ki_flags & IOCB_NOWAIT) {
4281 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4286 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4290 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4291 * the higher-level function iomap_dio_rw() in order to ensure that the
4292 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4294 inc_page_count(sbi, F2FS_DIO_READ);
4295 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4296 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4297 if (IS_ERR_OR_NULL(dio)) {
4298 ret = PTR_ERR_OR_ZERO(dio);
4299 if (ret != -EIOCBQUEUED)
4300 dec_page_count(sbi, F2FS_DIO_READ);
4302 ret = iomap_dio_complete(dio);
4305 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4307 file_accessed(file);
4309 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4313 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4315 struct inode *inode = file_inode(iocb->ki_filp);
4316 const loff_t pos = iocb->ki_pos;
4319 if (!f2fs_is_compress_backend_ready(inode))
4322 if (trace_f2fs_dataread_start_enabled()) {
4323 char *p = f2fs_kmalloc(F2FS_I_SB(inode), PATH_MAX, GFP_KERNEL);
4327 goto skip_read_trace;
4329 path = dentry_path_raw(file_dentry(iocb->ki_filp), p, PATH_MAX);
4332 goto skip_read_trace;
4335 trace_f2fs_dataread_start(inode, pos, iov_iter_count(to),
4336 current->pid, path, current->comm);
4340 if (f2fs_should_use_dio(inode, iocb, to)) {
4341 ret = f2fs_dio_read_iter(iocb, to);
4343 ret = filemap_read(iocb, to, 0);
4345 f2fs_update_iostat(F2FS_I_SB(inode), APP_BUFFERED_READ_IO, ret);
4347 if (trace_f2fs_dataread_end_enabled())
4348 trace_f2fs_dataread_end(inode, pos, ret);
4352 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4354 struct file *file = iocb->ki_filp;
4355 struct inode *inode = file_inode(file);
4359 if (IS_IMMUTABLE(inode))
4362 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4365 count = generic_write_checks(iocb, from);
4369 err = file_modified(file);
4376 * Preallocate blocks for a write request, if it is possible and helpful to do
4377 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4378 * blocks were preallocated, or a negative errno value if something went
4379 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4380 * requested blocks (not just some of them) have been allocated.
4382 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4385 struct inode *inode = file_inode(iocb->ki_filp);
4386 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4387 const loff_t pos = iocb->ki_pos;
4388 const size_t count = iov_iter_count(iter);
4389 struct f2fs_map_blocks map = {};
4393 /* If it will be an out-of-place direct write, don't bother. */
4394 if (dio && f2fs_lfs_mode(sbi))
4397 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4398 * buffered IO, if DIO meets any holes.
4400 if (dio && i_size_read(inode) &&
4401 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4404 /* No-wait I/O can't allocate blocks. */
4405 if (iocb->ki_flags & IOCB_NOWAIT)
4408 /* If it will be a short write, don't bother. */
4409 if (fault_in_iov_iter_readable(iter, count))
4412 if (f2fs_has_inline_data(inode)) {
4413 /* If the data will fit inline, don't bother. */
4414 if (pos + count <= MAX_INLINE_DATA(inode))
4416 ret = f2fs_convert_inline_inode(inode);
4421 /* Do not preallocate blocks that will be written partially in 4KB. */
4422 map.m_lblk = F2FS_BLK_ALIGN(pos);
4423 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4424 if (map.m_len > map.m_lblk)
4425 map.m_len -= map.m_lblk;
4428 map.m_may_create = true;
4430 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4431 flag = F2FS_GET_BLOCK_PRE_DIO;
4433 map.m_seg_type = NO_CHECK_TYPE;
4434 flag = F2FS_GET_BLOCK_PRE_AIO;
4437 ret = f2fs_map_blocks(inode, &map, 1, flag);
4438 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4439 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4442 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4446 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4447 struct iov_iter *from)
4449 struct file *file = iocb->ki_filp;
4450 struct inode *inode = file_inode(file);
4453 if (iocb->ki_flags & IOCB_NOWAIT)
4456 current->backing_dev_info = inode_to_bdi(inode);
4457 ret = generic_perform_write(iocb, from);
4458 current->backing_dev_info = NULL;
4461 iocb->ki_pos += ret;
4462 f2fs_update_iostat(F2FS_I_SB(inode), APP_BUFFERED_IO, ret);
4467 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4470 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4472 dec_page_count(sbi, F2FS_DIO_WRITE);
4475 f2fs_update_iostat(sbi, APP_DIRECT_IO, size);
4479 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4480 .end_io = f2fs_dio_write_end_io,
4483 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4484 bool *may_need_sync)
4486 struct file *file = iocb->ki_filp;
4487 struct inode *inode = file_inode(file);
4488 struct f2fs_inode_info *fi = F2FS_I(inode);
4489 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4490 const bool do_opu = f2fs_lfs_mode(sbi);
4491 const loff_t pos = iocb->ki_pos;
4492 const ssize_t count = iov_iter_count(from);
4493 unsigned int dio_flags;
4494 struct iomap_dio *dio;
4497 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4499 if (iocb->ki_flags & IOCB_NOWAIT) {
4500 /* f2fs_convert_inline_inode() and block allocation can block */
4501 if (f2fs_has_inline_data(inode) ||
4502 !f2fs_overwrite_io(inode, pos, count)) {
4507 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4511 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4512 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4517 ret = f2fs_convert_inline_inode(inode);
4521 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4523 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4527 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4528 * the higher-level function iomap_dio_rw() in order to ensure that the
4529 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4531 inc_page_count(sbi, F2FS_DIO_WRITE);
4533 if (pos + count > inode->i_size)
4534 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4535 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4536 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4537 if (IS_ERR_OR_NULL(dio)) {
4538 ret = PTR_ERR_OR_ZERO(dio);
4539 if (ret == -ENOTBLK)
4541 if (ret != -EIOCBQUEUED)
4542 dec_page_count(sbi, F2FS_DIO_WRITE);
4544 ret = iomap_dio_complete(dio);
4548 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4549 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4553 if (pos + ret > inode->i_size)
4554 f2fs_i_size_write(inode, pos + ret);
4556 set_inode_flag(inode, FI_UPDATE_WRITE);
4558 if (iov_iter_count(from)) {
4560 loff_t bufio_start_pos = iocb->ki_pos;
4563 * The direct write was partial, so we need to fall back to a
4564 * buffered write for the remainder.
4567 ret2 = f2fs_buffered_write_iter(iocb, from);
4568 if (iov_iter_count(from))
4569 f2fs_write_failed(inode, iocb->ki_pos);
4574 * Ensure that the pagecache pages are written to disk and
4575 * invalidated to preserve the expected O_DIRECT semantics.
4578 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4582 ret2 = filemap_write_and_wait_range(file->f_mapping,
4587 invalidate_mapping_pages(file->f_mapping,
4588 bufio_start_pos >> PAGE_SHIFT,
4589 bufio_end_pos >> PAGE_SHIFT);
4592 /* iomap_dio_rw() already handled the generic_write_sync(). */
4593 *may_need_sync = false;
4596 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4600 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4602 struct inode *inode = file_inode(iocb->ki_filp);
4603 const loff_t orig_pos = iocb->ki_pos;
4604 const size_t orig_count = iov_iter_count(from);
4607 bool may_need_sync = true;
4611 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4616 if (!f2fs_is_compress_backend_ready(inode)) {
4621 if (iocb->ki_flags & IOCB_NOWAIT) {
4622 if (!inode_trylock(inode)) {
4630 ret = f2fs_write_checks(iocb, from);
4634 /* Determine whether we will do a direct write or a buffered write. */
4635 dio = f2fs_should_use_dio(inode, iocb, from);
4637 /* Possibly preallocate the blocks for the write. */
4638 target_size = iocb->ki_pos + iov_iter_count(from);
4639 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4640 if (preallocated < 0) {
4643 if (trace_f2fs_datawrite_start_enabled()) {
4644 char *p = f2fs_kmalloc(F2FS_I_SB(inode),
4645 PATH_MAX, GFP_KERNEL);
4649 goto skip_write_trace;
4650 path = dentry_path_raw(file_dentry(iocb->ki_filp),
4654 goto skip_write_trace;
4656 trace_f2fs_datawrite_start(inode, orig_pos, orig_count,
4657 current->pid, path, current->comm);
4661 /* Do the actual write. */
4663 f2fs_dio_write_iter(iocb, from, &may_need_sync):
4664 f2fs_buffered_write_iter(iocb, from);
4666 if (trace_f2fs_datawrite_end_enabled())
4667 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4670 /* Don't leave any preallocated blocks around past i_size. */
4671 if (preallocated && i_size_read(inode) < target_size) {
4672 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4673 filemap_invalidate_lock(inode->i_mapping);
4674 if (!f2fs_truncate(inode))
4675 file_dont_truncate(inode);
4676 filemap_invalidate_unlock(inode->i_mapping);
4677 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4679 file_dont_truncate(inode);
4682 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4684 inode_unlock(inode);
4686 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4687 if (ret > 0 && may_need_sync)
4688 ret = generic_write_sync(iocb, ret);
4692 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4695 struct address_space *mapping;
4696 struct backing_dev_info *bdi;
4697 struct inode *inode = file_inode(filp);
4700 if (advice == POSIX_FADV_SEQUENTIAL) {
4701 if (S_ISFIFO(inode->i_mode))
4704 mapping = filp->f_mapping;
4705 if (!mapping || len < 0)
4708 bdi = inode_to_bdi(mapping->host);
4709 filp->f_ra.ra_pages = bdi->ra_pages *
4710 F2FS_I_SB(inode)->seq_file_ra_mul;
4711 spin_lock(&filp->f_lock);
4712 filp->f_mode &= ~FMODE_RANDOM;
4713 spin_unlock(&filp->f_lock);
4717 err = generic_fadvise(filp, offset, len, advice);
4718 if (!err && advice == POSIX_FADV_DONTNEED &&
4719 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4720 f2fs_compressed_file(inode))
4721 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4726 #ifdef CONFIG_COMPAT
4727 struct compat_f2fs_gc_range {
4732 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4733 struct compat_f2fs_gc_range)
4735 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4737 struct compat_f2fs_gc_range __user *urange;
4738 struct f2fs_gc_range range;
4741 urange = compat_ptr(arg);
4742 err = get_user(range.sync, &urange->sync);
4743 err |= get_user(range.start, &urange->start);
4744 err |= get_user(range.len, &urange->len);
4748 return __f2fs_ioc_gc_range(file, &range);
4751 struct compat_f2fs_move_range {
4757 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4758 struct compat_f2fs_move_range)
4760 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4762 struct compat_f2fs_move_range __user *urange;
4763 struct f2fs_move_range range;
4766 urange = compat_ptr(arg);
4767 err = get_user(range.dst_fd, &urange->dst_fd);
4768 err |= get_user(range.pos_in, &urange->pos_in);
4769 err |= get_user(range.pos_out, &urange->pos_out);
4770 err |= get_user(range.len, &urange->len);
4774 return __f2fs_ioc_move_range(file, &range);
4777 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4779 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4781 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4785 case FS_IOC32_GETVERSION:
4786 cmd = FS_IOC_GETVERSION;
4788 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4789 return f2fs_compat_ioc_gc_range(file, arg);
4790 case F2FS_IOC32_MOVE_RANGE:
4791 return f2fs_compat_ioc_move_range(file, arg);
4792 case F2FS_IOC_START_ATOMIC_WRITE:
4793 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4794 case F2FS_IOC_START_VOLATILE_WRITE:
4795 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4796 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4797 case F2FS_IOC_SHUTDOWN:
4799 case FS_IOC_SET_ENCRYPTION_POLICY:
4800 case FS_IOC_GET_ENCRYPTION_PWSALT:
4801 case FS_IOC_GET_ENCRYPTION_POLICY:
4802 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4803 case FS_IOC_ADD_ENCRYPTION_KEY:
4804 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4805 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4806 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4807 case FS_IOC_GET_ENCRYPTION_NONCE:
4808 case F2FS_IOC_GARBAGE_COLLECT:
4809 case F2FS_IOC_WRITE_CHECKPOINT:
4810 case F2FS_IOC_DEFRAGMENT:
4811 case F2FS_IOC_FLUSH_DEVICE:
4812 case F2FS_IOC_GET_FEATURES:
4813 case F2FS_IOC_GET_PIN_FILE:
4814 case F2FS_IOC_SET_PIN_FILE:
4815 case F2FS_IOC_PRECACHE_EXTENTS:
4816 case F2FS_IOC_RESIZE_FS:
4817 case FS_IOC_ENABLE_VERITY:
4818 case FS_IOC_MEASURE_VERITY:
4819 case FS_IOC_READ_VERITY_METADATA:
4820 case FS_IOC_GETFSLABEL:
4821 case FS_IOC_SETFSLABEL:
4822 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4823 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4824 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4825 case F2FS_IOC_SEC_TRIM_FILE:
4826 case F2FS_IOC_GET_COMPRESS_OPTION:
4827 case F2FS_IOC_SET_COMPRESS_OPTION:
4828 case F2FS_IOC_DECOMPRESS_FILE:
4829 case F2FS_IOC_COMPRESS_FILE:
4832 return -ENOIOCTLCMD;
4834 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4838 const struct file_operations f2fs_file_operations = {
4839 .llseek = f2fs_llseek,
4840 .read_iter = f2fs_file_read_iter,
4841 .write_iter = f2fs_file_write_iter,
4842 .open = f2fs_file_open,
4843 .release = f2fs_release_file,
4844 .mmap = f2fs_file_mmap,
4845 .flush = f2fs_file_flush,
4846 .fsync = f2fs_sync_file,
4847 .fallocate = f2fs_fallocate,
4848 .unlocked_ioctl = f2fs_ioctl,
4849 #ifdef CONFIG_COMPAT
4850 .compat_ioctl = f2fs_compat_ioctl,
4852 .splice_read = generic_file_splice_read,
4853 .splice_write = iter_file_splice_write,
4854 .fadvise = f2fs_file_fadvise,