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);
42 vm_flags_t flags = vmf->vma->vm_flags;
45 ret = filemap_fault(vmf);
46 if (ret & VM_FAULT_LOCKED)
47 f2fs_update_iostat(F2FS_I_SB(inode), inode,
48 APP_MAPPED_READ_IO, F2FS_BLKSIZE);
50 trace_f2fs_filemap_fault(inode, vmf->pgoff, flags, ret);
55 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
57 struct page *page = vmf->page;
58 struct inode *inode = file_inode(vmf->vma->vm_file);
59 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
60 struct dnode_of_data dn;
61 bool need_alloc = true;
65 if (unlikely(IS_IMMUTABLE(inode)))
66 return VM_FAULT_SIGBUS;
68 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
73 if (unlikely(f2fs_cp_error(sbi))) {
78 if (!f2fs_is_checkpoint_ready(sbi)) {
83 err = f2fs_convert_inline_inode(inode);
87 #ifdef CONFIG_F2FS_FS_COMPRESSION
88 if (f2fs_compressed_file(inode)) {
89 int ret = f2fs_is_compressed_cluster(inode, page->index);
99 /* should do out of any locked page */
101 f2fs_balance_fs(sbi, true);
103 sb_start_pagefault(inode->i_sb);
105 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
107 file_update_time(vmf->vma->vm_file);
108 filemap_invalidate_lock_shared(inode->i_mapping);
110 if (unlikely(page->mapping != inode->i_mapping ||
111 page_offset(page) > i_size_read(inode) ||
112 !PageUptodate(page))) {
119 /* block allocation */
120 set_new_dnode(&dn, inode, NULL, NULL, 0);
121 err = f2fs_get_block_locked(&dn, page->index);
124 #ifdef CONFIG_F2FS_FS_COMPRESSION
126 set_new_dnode(&dn, inode, NULL, NULL, 0);
127 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
136 f2fs_wait_on_page_writeback(page, DATA, false, true);
138 /* wait for GCed page writeback via META_MAPPING */
139 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
142 * check to see if the page is mapped already (no holes)
144 if (PageMappedToDisk(page))
147 /* page is wholly or partially inside EOF */
148 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
149 i_size_read(inode)) {
152 offset = i_size_read(inode) & ~PAGE_MASK;
153 zero_user_segment(page, offset, PAGE_SIZE);
155 set_page_dirty(page);
157 f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
158 f2fs_update_time(sbi, REQ_TIME);
161 filemap_invalidate_unlock_shared(inode->i_mapping);
163 sb_end_pagefault(inode->i_sb);
165 ret = vmf_fs_error(err);
167 trace_f2fs_vm_page_mkwrite(inode, page->index, vmf->vma->vm_flags, ret);
171 static const struct vm_operations_struct f2fs_file_vm_ops = {
172 .fault = f2fs_filemap_fault,
173 .map_pages = filemap_map_pages,
174 .page_mkwrite = f2fs_vm_page_mkwrite,
177 static int get_parent_ino(struct inode *inode, nid_t *pino)
179 struct dentry *dentry;
182 * Make sure to get the non-deleted alias. The alias associated with
183 * the open file descriptor being fsync()'ed may be deleted already.
185 dentry = d_find_alias(inode);
189 *pino = parent_ino(dentry);
194 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
196 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
197 enum cp_reason_type cp_reason = CP_NO_NEEDED;
199 if (!S_ISREG(inode->i_mode))
200 cp_reason = CP_NON_REGULAR;
201 else if (f2fs_compressed_file(inode))
202 cp_reason = CP_COMPRESSED;
203 else if (inode->i_nlink != 1)
204 cp_reason = CP_HARDLINK;
205 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
206 cp_reason = CP_SB_NEED_CP;
207 else if (file_wrong_pino(inode))
208 cp_reason = CP_WRONG_PINO;
209 else if (!f2fs_space_for_roll_forward(sbi))
210 cp_reason = CP_NO_SPC_ROLL;
211 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
212 cp_reason = CP_NODE_NEED_CP;
213 else if (test_opt(sbi, FASTBOOT))
214 cp_reason = CP_FASTBOOT_MODE;
215 else if (F2FS_OPTION(sbi).active_logs == 2)
216 cp_reason = CP_SPEC_LOG_NUM;
217 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
218 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
219 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
221 cp_reason = CP_RECOVER_DIR;
226 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
228 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
230 /* But we need to avoid that there are some inode updates */
231 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
237 static void try_to_fix_pino(struct inode *inode)
239 struct f2fs_inode_info *fi = F2FS_I(inode);
242 f2fs_down_write(&fi->i_sem);
243 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
244 get_parent_ino(inode, &pino)) {
245 f2fs_i_pino_write(inode, pino);
246 file_got_pino(inode);
248 f2fs_up_write(&fi->i_sem);
251 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
252 int datasync, bool atomic)
254 struct inode *inode = file->f_mapping->host;
255 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
256 nid_t ino = inode->i_ino;
258 enum cp_reason_type cp_reason = 0;
259 struct writeback_control wbc = {
260 .sync_mode = WB_SYNC_ALL,
261 .nr_to_write = LONG_MAX,
264 unsigned int seq_id = 0;
266 if (unlikely(f2fs_readonly(inode->i_sb)))
269 trace_f2fs_sync_file_enter(inode);
271 if (S_ISDIR(inode->i_mode))
274 /* if fdatasync is triggered, let's do in-place-update */
275 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
276 set_inode_flag(inode, FI_NEED_IPU);
277 ret = file_write_and_wait_range(file, start, end);
278 clear_inode_flag(inode, FI_NEED_IPU);
280 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
281 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
285 /* if the inode is dirty, let's recover all the time */
286 if (!f2fs_skip_inode_update(inode, datasync)) {
287 f2fs_write_inode(inode, NULL);
292 * if there is no written data, don't waste time to write recovery info.
294 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
295 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
297 /* it may call write_inode just prior to fsync */
298 if (need_inode_page_update(sbi, ino))
301 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
302 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
307 * for OPU case, during fsync(), node can be persisted before
308 * data when lower device doesn't support write barrier, result
309 * in data corruption after SPO.
310 * So for strict fsync mode, force to use atomic write semantics
311 * to keep write order in between data/node and last node to
312 * avoid potential data corruption.
314 if (F2FS_OPTION(sbi).fsync_mode ==
315 FSYNC_MODE_STRICT && !atomic)
320 * Both of fdatasync() and fsync() are able to be recovered from
323 f2fs_down_read(&F2FS_I(inode)->i_sem);
324 cp_reason = need_do_checkpoint(inode);
325 f2fs_up_read(&F2FS_I(inode)->i_sem);
328 /* all the dirty node pages should be flushed for POR */
329 ret = f2fs_sync_fs(inode->i_sb, 1);
332 * We've secured consistency through sync_fs. Following pino
333 * will be used only for fsynced inodes after checkpoint.
335 try_to_fix_pino(inode);
336 clear_inode_flag(inode, FI_APPEND_WRITE);
337 clear_inode_flag(inode, FI_UPDATE_WRITE);
341 atomic_inc(&sbi->wb_sync_req[NODE]);
342 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
343 atomic_dec(&sbi->wb_sync_req[NODE]);
347 /* if cp_error was enabled, we should avoid infinite loop */
348 if (unlikely(f2fs_cp_error(sbi))) {
353 if (f2fs_need_inode_block_update(sbi, ino)) {
354 f2fs_mark_inode_dirty_sync(inode, true);
355 f2fs_write_inode(inode, NULL);
360 * If it's atomic_write, it's just fine to keep write ordering. So
361 * here we don't need to wait for node write completion, since we use
362 * node chain which serializes node blocks. If one of node writes are
363 * reordered, we can see simply broken chain, resulting in stopping
364 * roll-forward recovery. It means we'll recover all or none node blocks
368 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
373 /* once recovery info is written, don't need to tack this */
374 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
375 clear_inode_flag(inode, FI_APPEND_WRITE);
377 if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) ||
378 (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi)))
379 ret = f2fs_issue_flush(sbi, inode->i_ino);
381 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
382 clear_inode_flag(inode, FI_UPDATE_WRITE);
383 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
385 f2fs_update_time(sbi, REQ_TIME);
387 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
391 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
393 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
395 return f2fs_do_sync_file(file, start, end, datasync, false);
398 static bool __found_offset(struct address_space *mapping,
399 struct dnode_of_data *dn, pgoff_t index, int whence)
401 block_t blkaddr = f2fs_data_blkaddr(dn);
402 struct inode *inode = mapping->host;
403 bool compressed_cluster = false;
405 if (f2fs_compressed_file(inode)) {
406 block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
407 ALIGN_DOWN(dn->ofs_in_node, F2FS_I(inode)->i_cluster_size));
409 compressed_cluster = first_blkaddr == COMPRESS_ADDR;
414 if (__is_valid_data_blkaddr(blkaddr))
416 if (blkaddr == NEW_ADDR &&
417 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
419 if (compressed_cluster)
423 if (compressed_cluster)
425 if (blkaddr == NULL_ADDR)
432 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
434 struct inode *inode = file->f_mapping->host;
435 loff_t maxbytes = inode->i_sb->s_maxbytes;
436 struct dnode_of_data dn;
437 pgoff_t pgofs, end_offset;
438 loff_t data_ofs = offset;
442 inode_lock_shared(inode);
444 isize = i_size_read(inode);
448 /* handle inline data case */
449 if (f2fs_has_inline_data(inode)) {
450 if (whence == SEEK_HOLE) {
453 } else if (whence == SEEK_DATA) {
459 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
461 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
462 set_new_dnode(&dn, inode, NULL, NULL, 0);
463 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
464 if (err && err != -ENOENT) {
466 } else if (err == -ENOENT) {
467 /* direct node does not exists */
468 if (whence == SEEK_DATA) {
469 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
476 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
478 /* find data/hole in dnode block */
479 for (; dn.ofs_in_node < end_offset;
480 dn.ofs_in_node++, pgofs++,
481 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
484 blkaddr = f2fs_data_blkaddr(&dn);
486 if (__is_valid_data_blkaddr(blkaddr) &&
487 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
488 blkaddr, DATA_GENERIC_ENHANCE)) {
493 if (__found_offset(file->f_mapping, &dn,
502 if (whence == SEEK_DATA)
505 if (whence == SEEK_HOLE && data_ofs > isize)
507 inode_unlock_shared(inode);
508 return vfs_setpos(file, data_ofs, maxbytes);
510 inode_unlock_shared(inode);
514 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
516 struct inode *inode = file->f_mapping->host;
517 loff_t maxbytes = inode->i_sb->s_maxbytes;
519 if (f2fs_compressed_file(inode))
520 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
526 return generic_file_llseek_size(file, offset, whence,
527 maxbytes, i_size_read(inode));
532 return f2fs_seek_block(file, offset, whence);
538 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
540 struct inode *inode = file_inode(file);
542 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
545 if (!f2fs_is_compress_backend_ready(inode))
549 vma->vm_ops = &f2fs_file_vm_ops;
551 f2fs_down_read(&F2FS_I(inode)->i_sem);
552 set_inode_flag(inode, FI_MMAP_FILE);
553 f2fs_up_read(&F2FS_I(inode)->i_sem);
558 static int f2fs_file_open(struct inode *inode, struct file *filp)
560 int err = fscrypt_file_open(inode, filp);
565 if (!f2fs_is_compress_backend_ready(inode))
568 err = fsverity_file_open(inode, filp);
572 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
573 filp->f_mode |= FMODE_CAN_ODIRECT;
575 return dquot_file_open(inode, filp);
578 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
580 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
581 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
583 bool compressed_cluster = false;
584 int cluster_index = 0, valid_blocks = 0;
585 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
586 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
588 addr = get_dnode_addr(dn->inode, dn->node_page) + ofs;
590 /* Assumption: truncation starts with cluster */
591 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
592 block_t blkaddr = le32_to_cpu(*addr);
594 if (f2fs_compressed_file(dn->inode) &&
595 !(cluster_index & (cluster_size - 1))) {
596 if (compressed_cluster)
597 f2fs_i_compr_blocks_update(dn->inode,
598 valid_blocks, false);
599 compressed_cluster = (blkaddr == COMPRESS_ADDR);
603 if (blkaddr == NULL_ADDR)
606 f2fs_set_data_blkaddr(dn, NULL_ADDR);
608 if (__is_valid_data_blkaddr(blkaddr)) {
609 if (time_to_inject(sbi, FAULT_BLKADDR_CONSISTENCE))
611 if (!f2fs_is_valid_blkaddr_raw(sbi, blkaddr,
612 DATA_GENERIC_ENHANCE))
614 if (compressed_cluster)
618 f2fs_invalidate_blocks(sbi, blkaddr);
620 if (!released || blkaddr != COMPRESS_ADDR)
624 if (compressed_cluster)
625 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
630 * once we invalidate valid blkaddr in range [ofs, ofs + count],
631 * we will invalidate all blkaddr in the whole range.
633 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
635 f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
636 f2fs_update_age_extent_cache_range(dn, fofs, len);
637 dec_valid_block_count(sbi, dn->inode, nr_free);
639 dn->ofs_in_node = ofs;
641 f2fs_update_time(sbi, REQ_TIME);
642 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
643 dn->ofs_in_node, nr_free);
646 static int truncate_partial_data_page(struct inode *inode, u64 from,
649 loff_t offset = from & (PAGE_SIZE - 1);
650 pgoff_t index = from >> PAGE_SHIFT;
651 struct address_space *mapping = inode->i_mapping;
654 if (!offset && !cache_only)
658 page = find_lock_page(mapping, index);
659 if (page && PageUptodate(page))
661 f2fs_put_page(page, 1);
665 page = f2fs_get_lock_data_page(inode, index, true);
667 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
669 f2fs_wait_on_page_writeback(page, DATA, true, true);
670 zero_user(page, offset, PAGE_SIZE - offset);
672 /* An encrypted inode should have a key and truncate the last page. */
673 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
675 set_page_dirty(page);
676 f2fs_put_page(page, 1);
680 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
682 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
683 struct dnode_of_data dn;
685 int count = 0, err = 0;
687 bool truncate_page = false;
689 trace_f2fs_truncate_blocks_enter(inode, from);
691 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
693 if (free_from >= max_file_blocks(inode))
699 ipage = f2fs_get_node_page(sbi, inode->i_ino);
701 err = PTR_ERR(ipage);
705 if (f2fs_has_inline_data(inode)) {
706 f2fs_truncate_inline_inode(inode, ipage, from);
707 f2fs_put_page(ipage, 1);
708 truncate_page = true;
712 set_new_dnode(&dn, inode, ipage, NULL, 0);
713 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
720 count = ADDRS_PER_PAGE(dn.node_page, inode);
722 count -= dn.ofs_in_node;
723 f2fs_bug_on(sbi, count < 0);
725 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
726 f2fs_truncate_data_blocks_range(&dn, count);
732 err = f2fs_truncate_inode_blocks(inode, free_from);
737 /* lastly zero out the first data page */
739 err = truncate_partial_data_page(inode, from, truncate_page);
741 trace_f2fs_truncate_blocks_exit(inode, err);
745 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
747 u64 free_from = from;
750 #ifdef CONFIG_F2FS_FS_COMPRESSION
752 * for compressed file, only support cluster size
753 * aligned truncation.
755 if (f2fs_compressed_file(inode))
756 free_from = round_up(from,
757 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
760 err = f2fs_do_truncate_blocks(inode, free_from, lock);
764 #ifdef CONFIG_F2FS_FS_COMPRESSION
766 * For compressed file, after release compress blocks, don't allow write
767 * direct, but we should allow write direct after truncate to zero.
769 if (f2fs_compressed_file(inode) && !free_from
770 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
771 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
773 if (from != free_from) {
774 err = f2fs_truncate_partial_cluster(inode, from, lock);
783 int f2fs_truncate(struct inode *inode)
787 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
790 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
791 S_ISLNK(inode->i_mode)))
794 trace_f2fs_truncate(inode);
796 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
799 err = f2fs_dquot_initialize(inode);
803 /* we should check inline_data size */
804 if (!f2fs_may_inline_data(inode)) {
805 err = f2fs_convert_inline_inode(inode);
810 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
814 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
815 f2fs_mark_inode_dirty_sync(inode, false);
819 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
821 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
823 if (!fscrypt_dio_supported(inode))
825 if (fsverity_active(inode))
827 if (f2fs_compressed_file(inode))
830 /* disallow direct IO if any of devices has unaligned blksize */
831 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
834 * for blkzoned device, fallback direct IO to buffered IO, so
835 * all IOs can be serialized by log-structured write.
837 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE))
839 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
845 int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
846 struct kstat *stat, u32 request_mask, unsigned int query_flags)
848 struct inode *inode = d_inode(path->dentry);
849 struct f2fs_inode_info *fi = F2FS_I(inode);
850 struct f2fs_inode *ri = NULL;
853 if (f2fs_has_extra_attr(inode) &&
854 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
855 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
856 stat->result_mask |= STATX_BTIME;
857 stat->btime.tv_sec = fi->i_crtime.tv_sec;
858 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
862 * Return the DIO alignment restrictions if requested. We only return
863 * this information when requested, since on encrypted files it might
864 * take a fair bit of work to get if the file wasn't opened recently.
866 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
867 * cannot represent that, so in that case we report no DIO support.
869 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
870 unsigned int bsize = i_blocksize(inode);
872 stat->result_mask |= STATX_DIOALIGN;
873 if (!f2fs_force_buffered_io(inode, WRITE)) {
874 stat->dio_mem_align = bsize;
875 stat->dio_offset_align = bsize;
880 if (flags & F2FS_COMPR_FL)
881 stat->attributes |= STATX_ATTR_COMPRESSED;
882 if (flags & F2FS_APPEND_FL)
883 stat->attributes |= STATX_ATTR_APPEND;
884 if (IS_ENCRYPTED(inode))
885 stat->attributes |= STATX_ATTR_ENCRYPTED;
886 if (flags & F2FS_IMMUTABLE_FL)
887 stat->attributes |= STATX_ATTR_IMMUTABLE;
888 if (flags & F2FS_NODUMP_FL)
889 stat->attributes |= STATX_ATTR_NODUMP;
890 if (IS_VERITY(inode))
891 stat->attributes |= STATX_ATTR_VERITY;
893 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
895 STATX_ATTR_ENCRYPTED |
896 STATX_ATTR_IMMUTABLE |
900 generic_fillattr(idmap, request_mask, inode, stat);
902 /* we need to show initial sectors used for inline_data/dentries */
903 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
904 f2fs_has_inline_dentry(inode))
905 stat->blocks += (stat->size + 511) >> 9;
910 #ifdef CONFIG_F2FS_FS_POSIX_ACL
911 static void __setattr_copy(struct mnt_idmap *idmap,
912 struct inode *inode, const struct iattr *attr)
914 unsigned int ia_valid = attr->ia_valid;
916 i_uid_update(idmap, attr, inode);
917 i_gid_update(idmap, attr, inode);
918 if (ia_valid & ATTR_ATIME)
919 inode_set_atime_to_ts(inode, attr->ia_atime);
920 if (ia_valid & ATTR_MTIME)
921 inode_set_mtime_to_ts(inode, attr->ia_mtime);
922 if (ia_valid & ATTR_CTIME)
923 inode_set_ctime_to_ts(inode, attr->ia_ctime);
924 if (ia_valid & ATTR_MODE) {
925 umode_t mode = attr->ia_mode;
926 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
928 if (!vfsgid_in_group_p(vfsgid) &&
929 !capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
931 set_acl_inode(inode, mode);
935 #define __setattr_copy setattr_copy
938 int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
941 struct inode *inode = d_inode(dentry);
944 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
947 if (unlikely(IS_IMMUTABLE(inode)))
950 if (unlikely(IS_APPEND(inode) &&
951 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
952 ATTR_GID | ATTR_TIMES_SET))))
955 if ((attr->ia_valid & ATTR_SIZE) &&
956 !f2fs_is_compress_backend_ready(inode))
959 err = setattr_prepare(idmap, dentry, attr);
963 err = fscrypt_prepare_setattr(dentry, attr);
967 err = fsverity_prepare_setattr(dentry, attr);
971 if (is_quota_modification(idmap, inode, attr)) {
972 err = f2fs_dquot_initialize(inode);
976 if (i_uid_needs_update(idmap, attr, inode) ||
977 i_gid_needs_update(idmap, attr, inode)) {
978 f2fs_lock_op(F2FS_I_SB(inode));
979 err = dquot_transfer(idmap, inode, attr);
981 set_sbi_flag(F2FS_I_SB(inode),
982 SBI_QUOTA_NEED_REPAIR);
983 f2fs_unlock_op(F2FS_I_SB(inode));
987 * update uid/gid under lock_op(), so that dquot and inode can
988 * be updated atomically.
990 i_uid_update(idmap, attr, inode);
991 i_gid_update(idmap, attr, inode);
992 f2fs_mark_inode_dirty_sync(inode, true);
993 f2fs_unlock_op(F2FS_I_SB(inode));
996 if (attr->ia_valid & ATTR_SIZE) {
997 loff_t old_size = i_size_read(inode);
999 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
1001 * should convert inline inode before i_size_write to
1002 * keep smaller than inline_data size with inline flag.
1004 err = f2fs_convert_inline_inode(inode);
1009 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1010 filemap_invalidate_lock(inode->i_mapping);
1012 truncate_setsize(inode, attr->ia_size);
1014 if (attr->ia_size <= old_size)
1015 err = f2fs_truncate(inode);
1017 * do not trim all blocks after i_size if target size is
1018 * larger than i_size.
1020 filemap_invalidate_unlock(inode->i_mapping);
1021 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1025 spin_lock(&F2FS_I(inode)->i_size_lock);
1026 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1027 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1028 spin_unlock(&F2FS_I(inode)->i_size_lock);
1031 __setattr_copy(idmap, inode, attr);
1033 if (attr->ia_valid & ATTR_MODE) {
1034 err = posix_acl_chmod(idmap, dentry, f2fs_get_inode_mode(inode));
1036 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1038 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1039 clear_inode_flag(inode, FI_ACL_MODE);
1043 /* file size may changed here */
1044 f2fs_mark_inode_dirty_sync(inode, true);
1046 /* inode change will produce dirty node pages flushed by checkpoint */
1047 f2fs_balance_fs(F2FS_I_SB(inode), true);
1052 const struct inode_operations f2fs_file_inode_operations = {
1053 .getattr = f2fs_getattr,
1054 .setattr = f2fs_setattr,
1055 .get_inode_acl = f2fs_get_acl,
1056 .set_acl = f2fs_set_acl,
1057 .listxattr = f2fs_listxattr,
1058 .fiemap = f2fs_fiemap,
1059 .fileattr_get = f2fs_fileattr_get,
1060 .fileattr_set = f2fs_fileattr_set,
1063 static int fill_zero(struct inode *inode, pgoff_t index,
1064 loff_t start, loff_t len)
1066 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1072 f2fs_balance_fs(sbi, true);
1075 page = f2fs_get_new_data_page(inode, NULL, index, false);
1076 f2fs_unlock_op(sbi);
1079 return PTR_ERR(page);
1081 f2fs_wait_on_page_writeback(page, DATA, true, true);
1082 zero_user(page, start, len);
1083 set_page_dirty(page);
1084 f2fs_put_page(page, 1);
1088 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1092 while (pg_start < pg_end) {
1093 struct dnode_of_data dn;
1094 pgoff_t end_offset, count;
1096 set_new_dnode(&dn, inode, NULL, NULL, 0);
1097 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1099 if (err == -ENOENT) {
1100 pg_start = f2fs_get_next_page_offset(&dn,
1107 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1108 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1110 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1112 f2fs_truncate_data_blocks_range(&dn, count);
1113 f2fs_put_dnode(&dn);
1120 static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1122 pgoff_t pg_start, pg_end;
1123 loff_t off_start, off_end;
1126 ret = f2fs_convert_inline_inode(inode);
1130 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1131 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1133 off_start = offset & (PAGE_SIZE - 1);
1134 off_end = (offset + len) & (PAGE_SIZE - 1);
1136 if (pg_start == pg_end) {
1137 ret = fill_zero(inode, pg_start, off_start,
1138 off_end - off_start);
1143 ret = fill_zero(inode, pg_start++, off_start,
1144 PAGE_SIZE - off_start);
1149 ret = fill_zero(inode, pg_end, 0, off_end);
1154 if (pg_start < pg_end) {
1155 loff_t blk_start, blk_end;
1156 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1158 f2fs_balance_fs(sbi, true);
1160 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1161 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1163 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1164 filemap_invalidate_lock(inode->i_mapping);
1166 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1169 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1170 f2fs_unlock_op(sbi);
1172 filemap_invalidate_unlock(inode->i_mapping);
1173 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1180 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1181 int *do_replace, pgoff_t off, pgoff_t len)
1183 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1184 struct dnode_of_data dn;
1188 set_new_dnode(&dn, inode, NULL, NULL, 0);
1189 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1190 if (ret && ret != -ENOENT) {
1192 } else if (ret == -ENOENT) {
1193 if (dn.max_level == 0)
1195 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1196 dn.ofs_in_node, len);
1202 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1203 dn.ofs_in_node, len);
1204 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1205 *blkaddr = f2fs_data_blkaddr(&dn);
1207 if (__is_valid_data_blkaddr(*blkaddr) &&
1208 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1209 DATA_GENERIC_ENHANCE)) {
1210 f2fs_put_dnode(&dn);
1211 return -EFSCORRUPTED;
1214 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1216 if (f2fs_lfs_mode(sbi)) {
1217 f2fs_put_dnode(&dn);
1221 /* do not invalidate this block address */
1222 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1226 f2fs_put_dnode(&dn);
1235 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1236 int *do_replace, pgoff_t off, int len)
1238 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1239 struct dnode_of_data dn;
1242 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1243 if (*do_replace == 0)
1246 set_new_dnode(&dn, inode, NULL, NULL, 0);
1247 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1249 dec_valid_block_count(sbi, inode, 1);
1250 f2fs_invalidate_blocks(sbi, *blkaddr);
1252 f2fs_update_data_blkaddr(&dn, *blkaddr);
1254 f2fs_put_dnode(&dn);
1259 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1260 block_t *blkaddr, int *do_replace,
1261 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1263 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1268 if (blkaddr[i] == NULL_ADDR && !full) {
1273 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1274 struct dnode_of_data dn;
1275 struct node_info ni;
1279 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1280 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1284 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1286 f2fs_put_dnode(&dn);
1290 ilen = min((pgoff_t)
1291 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1292 dn.ofs_in_node, len - i);
1294 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1295 f2fs_truncate_data_blocks_range(&dn, 1);
1297 if (do_replace[i]) {
1298 f2fs_i_blocks_write(src_inode,
1300 f2fs_i_blocks_write(dst_inode,
1302 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1303 blkaddr[i], ni.version, true, false);
1309 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1310 if (dst_inode->i_size < new_size)
1311 f2fs_i_size_write(dst_inode, new_size);
1312 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1314 f2fs_put_dnode(&dn);
1316 struct page *psrc, *pdst;
1318 psrc = f2fs_get_lock_data_page(src_inode,
1321 return PTR_ERR(psrc);
1322 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1325 f2fs_put_page(psrc, 1);
1326 return PTR_ERR(pdst);
1328 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1329 set_page_dirty(pdst);
1330 set_page_private_gcing(pdst);
1331 f2fs_put_page(pdst, 1);
1332 f2fs_put_page(psrc, 1);
1334 ret = f2fs_truncate_hole(src_inode,
1335 src + i, src + i + 1);
1344 static int __exchange_data_block(struct inode *src_inode,
1345 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1346 pgoff_t len, bool full)
1348 block_t *src_blkaddr;
1354 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1356 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1357 array_size(olen, sizeof(block_t)),
1362 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1363 array_size(olen, sizeof(int)),
1366 kvfree(src_blkaddr);
1370 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1371 do_replace, src, olen);
1375 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1376 do_replace, src, dst, olen, full);
1384 kvfree(src_blkaddr);
1390 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1391 kvfree(src_blkaddr);
1396 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1398 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1399 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1400 pgoff_t start = offset >> PAGE_SHIFT;
1401 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1404 f2fs_balance_fs(sbi, true);
1406 /* avoid gc operation during block exchange */
1407 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1408 filemap_invalidate_lock(inode->i_mapping);
1411 f2fs_drop_extent_tree(inode);
1412 truncate_pagecache(inode, offset);
1413 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1414 f2fs_unlock_op(sbi);
1416 filemap_invalidate_unlock(inode->i_mapping);
1417 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1421 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1426 if (offset + len >= i_size_read(inode))
1429 /* collapse range should be aligned to block size of f2fs. */
1430 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1433 ret = f2fs_convert_inline_inode(inode);
1437 /* write out all dirty pages from offset */
1438 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1442 ret = f2fs_do_collapse(inode, offset, len);
1446 /* write out all moved pages, if possible */
1447 filemap_invalidate_lock(inode->i_mapping);
1448 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1449 truncate_pagecache(inode, offset);
1451 new_size = i_size_read(inode) - len;
1452 ret = f2fs_truncate_blocks(inode, new_size, true);
1453 filemap_invalidate_unlock(inode->i_mapping);
1455 f2fs_i_size_write(inode, new_size);
1459 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1462 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1463 pgoff_t index = start;
1464 unsigned int ofs_in_node = dn->ofs_in_node;
1468 for (; index < end; index++, dn->ofs_in_node++) {
1469 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1473 dn->ofs_in_node = ofs_in_node;
1474 ret = f2fs_reserve_new_blocks(dn, count);
1478 dn->ofs_in_node = ofs_in_node;
1479 for (index = start; index < end; index++, dn->ofs_in_node++) {
1480 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1482 * f2fs_reserve_new_blocks will not guarantee entire block
1485 if (dn->data_blkaddr == NULL_ADDR) {
1490 if (dn->data_blkaddr == NEW_ADDR)
1493 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1494 DATA_GENERIC_ENHANCE)) {
1495 ret = -EFSCORRUPTED;
1499 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1500 f2fs_set_data_blkaddr(dn, NEW_ADDR);
1503 f2fs_update_read_extent_cache_range(dn, start, 0, index - start);
1504 f2fs_update_age_extent_cache_range(dn, start, index - start);
1509 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1512 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1513 struct address_space *mapping = inode->i_mapping;
1514 pgoff_t index, pg_start, pg_end;
1515 loff_t new_size = i_size_read(inode);
1516 loff_t off_start, off_end;
1519 ret = inode_newsize_ok(inode, (len + offset));
1523 ret = f2fs_convert_inline_inode(inode);
1527 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1531 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1532 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1534 off_start = offset & (PAGE_SIZE - 1);
1535 off_end = (offset + len) & (PAGE_SIZE - 1);
1537 if (pg_start == pg_end) {
1538 ret = fill_zero(inode, pg_start, off_start,
1539 off_end - off_start);
1543 new_size = max_t(loff_t, new_size, offset + len);
1546 ret = fill_zero(inode, pg_start++, off_start,
1547 PAGE_SIZE - off_start);
1551 new_size = max_t(loff_t, new_size,
1552 (loff_t)pg_start << PAGE_SHIFT);
1555 for (index = pg_start; index < pg_end;) {
1556 struct dnode_of_data dn;
1557 unsigned int end_offset;
1560 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1561 filemap_invalidate_lock(mapping);
1563 truncate_pagecache_range(inode,
1564 (loff_t)index << PAGE_SHIFT,
1565 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1569 set_new_dnode(&dn, inode, NULL, NULL, 0);
1570 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1572 f2fs_unlock_op(sbi);
1573 filemap_invalidate_unlock(mapping);
1574 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1578 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1579 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1581 ret = f2fs_do_zero_range(&dn, index, end);
1582 f2fs_put_dnode(&dn);
1584 f2fs_unlock_op(sbi);
1585 filemap_invalidate_unlock(mapping);
1586 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1588 f2fs_balance_fs(sbi, dn.node_changed);
1594 new_size = max_t(loff_t, new_size,
1595 (loff_t)index << PAGE_SHIFT);
1599 ret = fill_zero(inode, pg_end, 0, off_end);
1603 new_size = max_t(loff_t, new_size, offset + len);
1608 if (new_size > i_size_read(inode)) {
1609 if (mode & FALLOC_FL_KEEP_SIZE)
1610 file_set_keep_isize(inode);
1612 f2fs_i_size_write(inode, new_size);
1617 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1619 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1620 struct address_space *mapping = inode->i_mapping;
1621 pgoff_t nr, pg_start, pg_end, delta, idx;
1625 new_size = i_size_read(inode) + len;
1626 ret = inode_newsize_ok(inode, new_size);
1630 if (offset >= i_size_read(inode))
1633 /* insert range should be aligned to block size of f2fs. */
1634 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1637 ret = f2fs_convert_inline_inode(inode);
1641 f2fs_balance_fs(sbi, true);
1643 filemap_invalidate_lock(mapping);
1644 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1645 filemap_invalidate_unlock(mapping);
1649 /* write out all dirty pages from offset */
1650 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1654 pg_start = offset >> PAGE_SHIFT;
1655 pg_end = (offset + len) >> PAGE_SHIFT;
1656 delta = pg_end - pg_start;
1657 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1659 /* avoid gc operation during block exchange */
1660 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1661 filemap_invalidate_lock(mapping);
1662 truncate_pagecache(inode, offset);
1664 while (!ret && idx > pg_start) {
1665 nr = idx - pg_start;
1671 f2fs_drop_extent_tree(inode);
1673 ret = __exchange_data_block(inode, inode, idx,
1674 idx + delta, nr, false);
1675 f2fs_unlock_op(sbi);
1677 filemap_invalidate_unlock(mapping);
1678 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1682 /* write out all moved pages, if possible */
1683 filemap_invalidate_lock(mapping);
1684 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1685 truncate_pagecache(inode, offset);
1686 filemap_invalidate_unlock(mapping);
1689 f2fs_i_size_write(inode, new_size);
1693 static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
1694 loff_t len, int mode)
1696 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1697 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1698 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1699 .m_may_create = true };
1700 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1701 .init_gc_type = FG_GC,
1702 .should_migrate_blocks = false,
1703 .err_gc_skipped = true,
1704 .nr_free_secs = 0 };
1705 pgoff_t pg_start, pg_end;
1708 block_t expanded = 0;
1711 err = inode_newsize_ok(inode, (len + offset));
1715 err = f2fs_convert_inline_inode(inode);
1719 f2fs_balance_fs(sbi, true);
1721 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1722 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1723 off_end = (offset + len) & (PAGE_SIZE - 1);
1725 map.m_lblk = pg_start;
1726 map.m_len = pg_end - pg_start;
1733 if (f2fs_is_pinned_file(inode)) {
1734 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1735 block_t sec_len = roundup(map.m_len, sec_blks);
1737 map.m_len = sec_blks;
1739 if (has_not_enough_free_secs(sbi, 0,
1740 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1741 f2fs_down_write(&sbi->gc_lock);
1742 stat_inc_gc_call_count(sbi, FOREGROUND);
1743 err = f2fs_gc(sbi, &gc_control);
1744 if (err && err != -ENODATA)
1748 f2fs_down_write(&sbi->pin_sem);
1750 err = f2fs_allocate_pinning_section(sbi);
1752 f2fs_up_write(&sbi->pin_sem);
1756 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1757 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
1758 file_dont_truncate(inode);
1760 f2fs_up_write(&sbi->pin_sem);
1762 expanded += map.m_len;
1763 sec_len -= map.m_len;
1764 map.m_lblk += map.m_len;
1765 if (!err && sec_len)
1768 map.m_len = expanded;
1770 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
1771 expanded = map.m_len;
1780 last_off = pg_start + expanded - 1;
1782 /* update new size to the failed position */
1783 new_size = (last_off == pg_end) ? offset + len :
1784 (loff_t)(last_off + 1) << PAGE_SHIFT;
1786 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1789 if (new_size > i_size_read(inode)) {
1790 if (mode & FALLOC_FL_KEEP_SIZE)
1791 file_set_keep_isize(inode);
1793 f2fs_i_size_write(inode, new_size);
1799 static long f2fs_fallocate(struct file *file, int mode,
1800 loff_t offset, loff_t len)
1802 struct inode *inode = file_inode(file);
1805 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1807 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1809 if (!f2fs_is_compress_backend_ready(inode))
1812 /* f2fs only support ->fallocate for regular file */
1813 if (!S_ISREG(inode->i_mode))
1816 if (IS_ENCRYPTED(inode) &&
1817 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1821 * Pinned file should not support partial truncation since the block
1822 * can be used by applications.
1824 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1825 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1826 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1829 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1830 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1831 FALLOC_FL_INSERT_RANGE))
1836 ret = file_modified(file);
1840 if (mode & FALLOC_FL_PUNCH_HOLE) {
1841 if (offset >= inode->i_size)
1844 ret = f2fs_punch_hole(inode, offset, len);
1845 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1846 ret = f2fs_collapse_range(inode, offset, len);
1847 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1848 ret = f2fs_zero_range(inode, offset, len, mode);
1849 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1850 ret = f2fs_insert_range(inode, offset, len);
1852 ret = f2fs_expand_inode_data(inode, offset, len, mode);
1856 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1857 f2fs_mark_inode_dirty_sync(inode, false);
1858 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1862 inode_unlock(inode);
1864 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1868 static int f2fs_release_file(struct inode *inode, struct file *filp)
1871 * f2fs_release_file is called at every close calls. So we should
1872 * not drop any inmemory pages by close called by other process.
1874 if (!(filp->f_mode & FMODE_WRITE) ||
1875 atomic_read(&inode->i_writecount) != 1)
1879 f2fs_abort_atomic_write(inode, true);
1880 inode_unlock(inode);
1885 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1887 struct inode *inode = file_inode(file);
1890 * If the process doing a transaction is crashed, we should do
1891 * roll-back. Otherwise, other reader/write can see corrupted database
1892 * until all the writers close its file. Since this should be done
1893 * before dropping file lock, it needs to do in ->flush.
1895 if (F2FS_I(inode)->atomic_write_task == current &&
1896 (current->flags & PF_EXITING)) {
1898 f2fs_abort_atomic_write(inode, true);
1899 inode_unlock(inode);
1905 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1907 struct f2fs_inode_info *fi = F2FS_I(inode);
1908 u32 masked_flags = fi->i_flags & mask;
1910 /* mask can be shrunk by flags_valid selector */
1913 /* Is it quota file? Do not allow user to mess with it */
1914 if (IS_NOQUOTA(inode))
1917 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1918 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1920 if (!f2fs_empty_dir(inode))
1924 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1925 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1927 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1931 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1932 if (masked_flags & F2FS_COMPR_FL) {
1933 if (!f2fs_disable_compressed_file(inode))
1936 /* try to convert inline_data to support compression */
1937 int err = f2fs_convert_inline_inode(inode);
1941 f2fs_down_write(&F2FS_I(inode)->i_sem);
1942 if (!f2fs_may_compress(inode) ||
1943 (S_ISREG(inode->i_mode) &&
1944 F2FS_HAS_BLOCKS(inode))) {
1945 f2fs_up_write(&F2FS_I(inode)->i_sem);
1948 err = set_compress_context(inode);
1949 f2fs_up_write(&F2FS_I(inode)->i_sem);
1956 fi->i_flags = iflags | (fi->i_flags & ~mask);
1957 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1958 (fi->i_flags & F2FS_NOCOMP_FL));
1960 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1961 set_inode_flag(inode, FI_PROJ_INHERIT);
1963 clear_inode_flag(inode, FI_PROJ_INHERIT);
1965 inode_set_ctime_current(inode);
1966 f2fs_set_inode_flags(inode);
1967 f2fs_mark_inode_dirty_sync(inode, true);
1971 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1974 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1975 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1976 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1977 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1979 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1980 * FS_IOC_FSSETXATTR is done by the VFS.
1983 static const struct {
1986 } f2fs_fsflags_map[] = {
1987 { F2FS_COMPR_FL, FS_COMPR_FL },
1988 { F2FS_SYNC_FL, FS_SYNC_FL },
1989 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1990 { F2FS_APPEND_FL, FS_APPEND_FL },
1991 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1992 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1993 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1994 { F2FS_INDEX_FL, FS_INDEX_FL },
1995 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1996 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1997 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
2000 #define F2FS_GETTABLE_FS_FL ( \
2010 FS_PROJINHERIT_FL | \
2012 FS_INLINE_DATA_FL | \
2017 #define F2FS_SETTABLE_FS_FL ( \
2026 FS_PROJINHERIT_FL | \
2029 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2030 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2035 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2036 if (iflags & f2fs_fsflags_map[i].iflag)
2037 fsflags |= f2fs_fsflags_map[i].fsflag;
2042 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2043 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2048 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2049 if (fsflags & f2fs_fsflags_map[i].fsflag)
2050 iflags |= f2fs_fsflags_map[i].iflag;
2055 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2057 struct inode *inode = file_inode(filp);
2059 return put_user(inode->i_generation, (int __user *)arg);
2062 static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
2064 struct inode *inode = file_inode(filp);
2065 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2066 struct f2fs_inode_info *fi = F2FS_I(inode);
2067 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2068 struct inode *pinode;
2072 if (!inode_owner_or_capable(idmap, inode))
2075 if (!S_ISREG(inode->i_mode))
2078 if (filp->f_flags & O_DIRECT)
2081 ret = mnt_want_write_file(filp);
2087 if (!f2fs_disable_compressed_file(inode) ||
2088 f2fs_is_pinned_file(inode)) {
2093 if (f2fs_is_atomic_file(inode))
2096 ret = f2fs_convert_inline_inode(inode);
2100 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2103 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2104 * f2fs_is_atomic_file.
2106 if (get_dirty_pages(inode))
2107 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2108 inode->i_ino, get_dirty_pages(inode));
2109 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2111 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2115 /* Check if the inode already has a COW inode */
2116 if (fi->cow_inode == NULL) {
2117 /* Create a COW inode for atomic write */
2118 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2119 if (IS_ERR(pinode)) {
2120 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2121 ret = PTR_ERR(pinode);
2125 ret = f2fs_get_tmpfile(idmap, pinode, &fi->cow_inode);
2128 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2132 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2133 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2135 /* Reuse the already created COW inode */
2136 ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2138 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2143 f2fs_write_inode(inode, NULL);
2145 stat_inc_atomic_inode(inode);
2147 set_inode_flag(inode, FI_ATOMIC_FILE);
2149 isize = i_size_read(inode);
2150 fi->original_i_size = isize;
2152 set_inode_flag(inode, FI_ATOMIC_REPLACE);
2153 truncate_inode_pages_final(inode->i_mapping);
2154 f2fs_i_size_write(inode, 0);
2157 f2fs_i_size_write(fi->cow_inode, isize);
2159 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2161 f2fs_update_time(sbi, REQ_TIME);
2162 fi->atomic_write_task = current;
2163 stat_update_max_atomic_write(inode);
2164 fi->atomic_write_cnt = 0;
2166 inode_unlock(inode);
2167 mnt_drop_write_file(filp);
2171 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2173 struct inode *inode = file_inode(filp);
2174 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2177 if (!inode_owner_or_capable(idmap, inode))
2180 ret = mnt_want_write_file(filp);
2184 f2fs_balance_fs(F2FS_I_SB(inode), true);
2188 if (f2fs_is_atomic_file(inode)) {
2189 ret = f2fs_commit_atomic_write(inode);
2191 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2193 f2fs_abort_atomic_write(inode, ret);
2195 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2198 inode_unlock(inode);
2199 mnt_drop_write_file(filp);
2203 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2205 struct inode *inode = file_inode(filp);
2206 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2209 if (!inode_owner_or_capable(idmap, inode))
2212 ret = mnt_want_write_file(filp);
2218 f2fs_abort_atomic_write(inode, true);
2220 inode_unlock(inode);
2222 mnt_drop_write_file(filp);
2223 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2227 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2229 struct inode *inode = file_inode(filp);
2230 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2231 struct super_block *sb = sbi->sb;
2235 if (!capable(CAP_SYS_ADMIN))
2238 if (get_user(in, (__u32 __user *)arg))
2241 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2242 ret = mnt_want_write_file(filp);
2244 if (ret == -EROFS) {
2246 f2fs_stop_checkpoint(sbi, false,
2247 STOP_CP_REASON_SHUTDOWN);
2248 trace_f2fs_shutdown(sbi, in, ret);
2255 case F2FS_GOING_DOWN_FULLSYNC:
2256 ret = bdev_freeze(sb->s_bdev);
2259 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2260 bdev_thaw(sb->s_bdev);
2262 case F2FS_GOING_DOWN_METASYNC:
2263 /* do checkpoint only */
2264 ret = f2fs_sync_fs(sb, 1);
2270 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2272 case F2FS_GOING_DOWN_NOSYNC:
2273 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2275 case F2FS_GOING_DOWN_METAFLUSH:
2276 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2277 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2279 case F2FS_GOING_DOWN_NEED_FSCK:
2280 set_sbi_flag(sbi, SBI_NEED_FSCK);
2281 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2282 set_sbi_flag(sbi, SBI_IS_DIRTY);
2283 /* do checkpoint only */
2284 ret = f2fs_sync_fs(sb, 1);
2293 f2fs_stop_gc_thread(sbi);
2294 f2fs_stop_discard_thread(sbi);
2296 f2fs_drop_discard_cmd(sbi);
2297 clear_opt(sbi, DISCARD);
2299 f2fs_update_time(sbi, REQ_TIME);
2301 if (in != F2FS_GOING_DOWN_FULLSYNC)
2302 mnt_drop_write_file(filp);
2304 trace_f2fs_shutdown(sbi, in, ret);
2309 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2311 struct inode *inode = file_inode(filp);
2312 struct super_block *sb = inode->i_sb;
2313 struct fstrim_range range;
2316 if (!capable(CAP_SYS_ADMIN))
2319 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2322 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2326 ret = mnt_want_write_file(filp);
2330 range.minlen = max((unsigned int)range.minlen,
2331 bdev_discard_granularity(sb->s_bdev));
2332 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2333 mnt_drop_write_file(filp);
2337 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2340 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2344 static bool uuid_is_nonzero(__u8 u[16])
2348 for (i = 0; i < 16; i++)
2354 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2356 struct inode *inode = file_inode(filp);
2358 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2361 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2363 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2366 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2368 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2370 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2373 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2375 struct inode *inode = file_inode(filp);
2376 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2377 u8 encrypt_pw_salt[16];
2380 if (!f2fs_sb_has_encrypt(sbi))
2383 err = mnt_want_write_file(filp);
2387 f2fs_down_write(&sbi->sb_lock);
2389 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2392 /* update superblock with uuid */
2393 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2395 err = f2fs_commit_super(sbi, false);
2398 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2402 memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2404 f2fs_up_write(&sbi->sb_lock);
2405 mnt_drop_write_file(filp);
2407 if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2413 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2416 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2419 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2422 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2424 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2427 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2430 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2432 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2435 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2438 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2441 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2444 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2447 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2450 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2453 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2456 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2458 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2461 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2464 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2466 struct inode *inode = file_inode(filp);
2467 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2468 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2470 .should_migrate_blocks = false,
2471 .nr_free_secs = 0 };
2475 if (!capable(CAP_SYS_ADMIN))
2478 if (get_user(sync, (__u32 __user *)arg))
2481 if (f2fs_readonly(sbi->sb))
2484 ret = mnt_want_write_file(filp);
2489 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2494 f2fs_down_write(&sbi->gc_lock);
2497 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2498 gc_control.err_gc_skipped = sync;
2499 stat_inc_gc_call_count(sbi, FOREGROUND);
2500 ret = f2fs_gc(sbi, &gc_control);
2502 mnt_drop_write_file(filp);
2506 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2508 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2509 struct f2fs_gc_control gc_control = {
2510 .init_gc_type = range->sync ? FG_GC : BG_GC,
2512 .should_migrate_blocks = false,
2513 .err_gc_skipped = range->sync,
2514 .nr_free_secs = 0 };
2518 if (!capable(CAP_SYS_ADMIN))
2520 if (f2fs_readonly(sbi->sb))
2523 end = range->start + range->len;
2524 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2525 end >= MAX_BLKADDR(sbi))
2528 ret = mnt_want_write_file(filp);
2534 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2539 f2fs_down_write(&sbi->gc_lock);
2542 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2543 stat_inc_gc_call_count(sbi, FOREGROUND);
2544 ret = f2fs_gc(sbi, &gc_control);
2550 range->start += CAP_BLKS_PER_SEC(sbi);
2551 if (range->start <= end)
2554 mnt_drop_write_file(filp);
2558 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2560 struct f2fs_gc_range range;
2562 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2565 return __f2fs_ioc_gc_range(filp, &range);
2568 static int f2fs_ioc_write_checkpoint(struct file *filp)
2570 struct inode *inode = file_inode(filp);
2571 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2574 if (!capable(CAP_SYS_ADMIN))
2577 if (f2fs_readonly(sbi->sb))
2580 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2581 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2585 ret = mnt_want_write_file(filp);
2589 ret = f2fs_sync_fs(sbi->sb, 1);
2591 mnt_drop_write_file(filp);
2595 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2597 struct f2fs_defragment *range)
2599 struct inode *inode = file_inode(filp);
2600 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2601 .m_seg_type = NO_CHECK_TYPE,
2602 .m_may_create = false };
2603 struct extent_info ei = {};
2604 pgoff_t pg_start, pg_end, next_pgofs;
2605 unsigned int total = 0, sec_num;
2606 block_t blk_end = 0;
2607 bool fragmented = false;
2610 pg_start = range->start >> PAGE_SHIFT;
2611 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2613 f2fs_balance_fs(sbi, true);
2617 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
2622 /* if in-place-update policy is enabled, don't waste time here */
2623 set_inode_flag(inode, FI_OPU_WRITE);
2624 if (f2fs_should_update_inplace(inode, NULL)) {
2629 /* writeback all dirty pages in the range */
2630 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2631 range->start + range->len - 1);
2636 * lookup mapping info in extent cache, skip defragmenting if physical
2637 * block addresses are continuous.
2639 if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
2640 if (ei.fofs + ei.len >= pg_end)
2644 map.m_lblk = pg_start;
2645 map.m_next_pgofs = &next_pgofs;
2648 * lookup mapping info in dnode page cache, skip defragmenting if all
2649 * physical block addresses are continuous even if there are hole(s)
2650 * in logical blocks.
2652 while (map.m_lblk < pg_end) {
2653 map.m_len = pg_end - map.m_lblk;
2654 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2658 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2659 map.m_lblk = next_pgofs;
2663 if (blk_end && blk_end != map.m_pblk)
2666 /* record total count of block that we're going to move */
2669 blk_end = map.m_pblk + map.m_len;
2671 map.m_lblk += map.m_len;
2679 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2682 * make sure there are enough free section for LFS allocation, this can
2683 * avoid defragment running in SSR mode when free section are allocated
2686 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2691 map.m_lblk = pg_start;
2692 map.m_len = pg_end - pg_start;
2695 while (map.m_lblk < pg_end) {
2700 map.m_len = pg_end - map.m_lblk;
2701 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2705 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2706 map.m_lblk = next_pgofs;
2710 set_inode_flag(inode, FI_SKIP_WRITES);
2713 while (idx < map.m_lblk + map.m_len &&
2714 cnt < BLKS_PER_SEG(sbi)) {
2717 page = f2fs_get_lock_data_page(inode, idx, true);
2719 err = PTR_ERR(page);
2723 set_page_dirty(page);
2724 set_page_private_gcing(page);
2725 f2fs_put_page(page, 1);
2734 if (map.m_lblk < pg_end && cnt < BLKS_PER_SEG(sbi))
2737 clear_inode_flag(inode, FI_SKIP_WRITES);
2739 err = filemap_fdatawrite(inode->i_mapping);
2744 clear_inode_flag(inode, FI_SKIP_WRITES);
2746 clear_inode_flag(inode, FI_OPU_WRITE);
2748 inode_unlock(inode);
2750 range->len = (u64)total << PAGE_SHIFT;
2754 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2756 struct inode *inode = file_inode(filp);
2757 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2758 struct f2fs_defragment range;
2761 if (!capable(CAP_SYS_ADMIN))
2764 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2767 if (f2fs_readonly(sbi->sb))
2770 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2774 /* verify alignment of offset & size */
2775 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2778 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2779 max_file_blocks(inode)))
2782 err = mnt_want_write_file(filp);
2786 err = f2fs_defragment_range(sbi, filp, &range);
2787 mnt_drop_write_file(filp);
2789 f2fs_update_time(sbi, REQ_TIME);
2793 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2800 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2801 struct file *file_out, loff_t pos_out, size_t len)
2803 struct inode *src = file_inode(file_in);
2804 struct inode *dst = file_inode(file_out);
2805 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2806 size_t olen = len, dst_max_i_size = 0;
2810 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2811 src->i_sb != dst->i_sb)
2814 if (unlikely(f2fs_readonly(src->i_sb)))
2817 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2820 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2823 if (pos_out < 0 || pos_in < 0)
2827 if (pos_in == pos_out)
2829 if (pos_out > pos_in && pos_out < pos_in + len)
2836 if (!inode_trylock(dst))
2840 if (f2fs_compressed_file(src) || f2fs_compressed_file(dst)) {
2846 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2849 olen = len = src->i_size - pos_in;
2850 if (pos_in + len == src->i_size)
2851 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2857 dst_osize = dst->i_size;
2858 if (pos_out + olen > dst->i_size)
2859 dst_max_i_size = pos_out + olen;
2861 /* verify the end result is block aligned */
2862 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2863 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2864 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2867 ret = f2fs_convert_inline_inode(src);
2871 ret = f2fs_convert_inline_inode(dst);
2875 /* write out all dirty pages from offset */
2876 ret = filemap_write_and_wait_range(src->i_mapping,
2877 pos_in, pos_in + len);
2881 ret = filemap_write_and_wait_range(dst->i_mapping,
2882 pos_out, pos_out + len);
2886 f2fs_balance_fs(sbi, true);
2888 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2891 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2896 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2897 pos_out >> F2FS_BLKSIZE_BITS,
2898 len >> F2FS_BLKSIZE_BITS, false);
2902 f2fs_i_size_write(dst, dst_max_i_size);
2903 else if (dst_osize != dst->i_size)
2904 f2fs_i_size_write(dst, dst_osize);
2906 f2fs_unlock_op(sbi);
2909 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2911 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2915 inode_set_mtime_to_ts(src, inode_set_ctime_current(src));
2916 f2fs_mark_inode_dirty_sync(src, false);
2918 inode_set_mtime_to_ts(dst, inode_set_ctime_current(dst));
2919 f2fs_mark_inode_dirty_sync(dst, false);
2921 f2fs_update_time(sbi, REQ_TIME);
2931 static int __f2fs_ioc_move_range(struct file *filp,
2932 struct f2fs_move_range *range)
2937 if (!(filp->f_mode & FMODE_READ) ||
2938 !(filp->f_mode & FMODE_WRITE))
2941 dst = fdget(range->dst_fd);
2945 if (!(dst.file->f_mode & FMODE_WRITE)) {
2950 err = mnt_want_write_file(filp);
2954 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2955 range->pos_out, range->len);
2957 mnt_drop_write_file(filp);
2963 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2965 struct f2fs_move_range range;
2967 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2970 return __f2fs_ioc_move_range(filp, &range);
2973 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2975 struct inode *inode = file_inode(filp);
2976 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2977 struct sit_info *sm = SIT_I(sbi);
2978 unsigned int start_segno = 0, end_segno = 0;
2979 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2980 struct f2fs_flush_device range;
2981 struct f2fs_gc_control gc_control = {
2982 .init_gc_type = FG_GC,
2983 .should_migrate_blocks = true,
2984 .err_gc_skipped = true,
2985 .nr_free_secs = 0 };
2988 if (!capable(CAP_SYS_ADMIN))
2991 if (f2fs_readonly(sbi->sb))
2994 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2997 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
3001 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
3002 __is_large_section(sbi)) {
3003 f2fs_warn(sbi, "Can't flush %u in %d for SEGS_PER_SEC %u != 1",
3004 range.dev_num, sbi->s_ndevs, SEGS_PER_SEC(sbi));
3008 ret = mnt_want_write_file(filp);
3012 if (range.dev_num != 0)
3013 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
3014 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
3016 start_segno = sm->last_victim[FLUSH_DEVICE];
3017 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
3018 start_segno = dev_start_segno;
3019 end_segno = min(start_segno + range.segments, dev_end_segno);
3021 while (start_segno < end_segno) {
3022 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
3026 sm->last_victim[GC_CB] = end_segno + 1;
3027 sm->last_victim[GC_GREEDY] = end_segno + 1;
3028 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
3030 gc_control.victim_segno = start_segno;
3031 stat_inc_gc_call_count(sbi, FOREGROUND);
3032 ret = f2fs_gc(sbi, &gc_control);
3040 mnt_drop_write_file(filp);
3044 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
3046 struct inode *inode = file_inode(filp);
3047 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3049 /* Must validate to set it with SQLite behavior in Android. */
3050 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3052 return put_user(sb_feature, (u32 __user *)arg);
3056 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3058 struct dquot *transfer_to[MAXQUOTAS] = {};
3059 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3060 struct super_block *sb = sbi->sb;
3063 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3064 if (IS_ERR(transfer_to[PRJQUOTA]))
3065 return PTR_ERR(transfer_to[PRJQUOTA]);
3067 err = __dquot_transfer(inode, transfer_to);
3069 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3070 dqput(transfer_to[PRJQUOTA]);
3074 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3076 struct f2fs_inode_info *fi = F2FS_I(inode);
3077 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3078 struct f2fs_inode *ri = NULL;
3082 if (!f2fs_sb_has_project_quota(sbi)) {
3083 if (projid != F2FS_DEF_PROJID)
3089 if (!f2fs_has_extra_attr(inode))
3092 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3094 if (projid_eq(kprojid, fi->i_projid))
3098 /* Is it quota file? Do not allow user to mess with it */
3099 if (IS_NOQUOTA(inode))
3102 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3105 err = f2fs_dquot_initialize(inode);
3110 err = f2fs_transfer_project_quota(inode, kprojid);
3114 fi->i_projid = kprojid;
3115 inode_set_ctime_current(inode);
3116 f2fs_mark_inode_dirty_sync(inode, true);
3118 f2fs_unlock_op(sbi);
3122 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3127 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3129 if (projid != F2FS_DEF_PROJID)
3135 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3137 struct inode *inode = d_inode(dentry);
3138 struct f2fs_inode_info *fi = F2FS_I(inode);
3139 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3141 if (IS_ENCRYPTED(inode))
3142 fsflags |= FS_ENCRYPT_FL;
3143 if (IS_VERITY(inode))
3144 fsflags |= FS_VERITY_FL;
3145 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3146 fsflags |= FS_INLINE_DATA_FL;
3147 if (is_inode_flag_set(inode, FI_PIN_FILE))
3148 fsflags |= FS_NOCOW_FL;
3150 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3152 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3153 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3158 int f2fs_fileattr_set(struct mnt_idmap *idmap,
3159 struct dentry *dentry, struct fileattr *fa)
3161 struct inode *inode = d_inode(dentry);
3162 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3166 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3168 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3170 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3172 fsflags &= F2FS_SETTABLE_FS_FL;
3173 if (!fa->flags_valid)
3174 mask &= FS_COMMON_FL;
3176 iflags = f2fs_fsflags_to_iflags(fsflags);
3177 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3180 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3182 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3187 int f2fs_pin_file_control(struct inode *inode, bool inc)
3189 struct f2fs_inode_info *fi = F2FS_I(inode);
3190 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3192 /* Use i_gc_failures for normal file as a risk signal. */
3194 f2fs_i_gc_failures_write(inode,
3195 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3197 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3198 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3199 __func__, inode->i_ino,
3200 fi->i_gc_failures[GC_FAILURE_PIN]);
3201 clear_inode_flag(inode, FI_PIN_FILE);
3207 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3209 struct inode *inode = file_inode(filp);
3210 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3214 if (get_user(pin, (__u32 __user *)arg))
3217 if (!S_ISREG(inode->i_mode))
3220 if (f2fs_readonly(sbi->sb))
3223 ret = mnt_want_write_file(filp);
3230 clear_inode_flag(inode, FI_PIN_FILE);
3231 f2fs_i_gc_failures_write(inode, 0);
3233 } else if (f2fs_is_pinned_file(inode)) {
3237 if (f2fs_sb_has_blkzoned(sbi) && F2FS_HAS_BLOCKS(inode)) {
3242 /* Let's allow file pinning on zoned device. */
3243 if (!f2fs_sb_has_blkzoned(sbi) &&
3244 f2fs_should_update_outplace(inode, NULL)) {
3249 if (f2fs_pin_file_control(inode, false)) {
3254 ret = f2fs_convert_inline_inode(inode);
3258 if (!f2fs_disable_compressed_file(inode)) {
3263 set_inode_flag(inode, FI_PIN_FILE);
3264 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3266 f2fs_update_time(sbi, REQ_TIME);
3268 inode_unlock(inode);
3269 mnt_drop_write_file(filp);
3273 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3275 struct inode *inode = file_inode(filp);
3278 if (is_inode_flag_set(inode, FI_PIN_FILE))
3279 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3280 return put_user(pin, (u32 __user *)arg);
3283 int f2fs_precache_extents(struct inode *inode)
3285 struct f2fs_inode_info *fi = F2FS_I(inode);
3286 struct f2fs_map_blocks map;
3287 pgoff_t m_next_extent;
3291 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3296 map.m_next_pgofs = NULL;
3297 map.m_next_extent = &m_next_extent;
3298 map.m_seg_type = NO_CHECK_TYPE;
3299 map.m_may_create = false;
3300 end = F2FS_BLK_ALIGN(i_size_read(inode));
3302 while (map.m_lblk < end) {
3303 map.m_len = end - map.m_lblk;
3305 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3306 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
3307 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3308 if (err || !map.m_len)
3311 map.m_lblk = m_next_extent;
3317 static int f2fs_ioc_precache_extents(struct file *filp)
3319 return f2fs_precache_extents(file_inode(filp));
3322 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3324 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3327 if (!capable(CAP_SYS_ADMIN))
3330 if (f2fs_readonly(sbi->sb))
3333 if (copy_from_user(&block_count, (void __user *)arg,
3334 sizeof(block_count)))
3337 return f2fs_resize_fs(filp, block_count);
3340 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3342 struct inode *inode = file_inode(filp);
3344 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3346 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3347 f2fs_warn(F2FS_I_SB(inode),
3348 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3353 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3356 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3358 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3361 return fsverity_ioctl_measure(filp, (void __user *)arg);
3364 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3366 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3369 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3372 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3374 struct inode *inode = file_inode(filp);
3375 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3380 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3384 f2fs_down_read(&sbi->sb_lock);
3385 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3386 ARRAY_SIZE(sbi->raw_super->volume_name),
3387 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3388 f2fs_up_read(&sbi->sb_lock);
3390 if (copy_to_user((char __user *)arg, vbuf,
3391 min(FSLABEL_MAX, count)))
3398 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3400 struct inode *inode = file_inode(filp);
3401 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3405 if (!capable(CAP_SYS_ADMIN))
3408 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3410 return PTR_ERR(vbuf);
3412 err = mnt_want_write_file(filp);
3416 f2fs_down_write(&sbi->sb_lock);
3418 memset(sbi->raw_super->volume_name, 0,
3419 sizeof(sbi->raw_super->volume_name));
3420 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3421 sbi->raw_super->volume_name,
3422 ARRAY_SIZE(sbi->raw_super->volume_name));
3424 err = f2fs_commit_super(sbi, false);
3426 f2fs_up_write(&sbi->sb_lock);
3428 mnt_drop_write_file(filp);
3434 static int f2fs_get_compress_blocks(struct inode *inode, __u64 *blocks)
3436 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3439 if (!f2fs_compressed_file(inode))
3442 *blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3447 static int f2fs_ioc_get_compress_blocks(struct file *filp, unsigned long arg)
3449 struct inode *inode = file_inode(filp);
3453 ret = f2fs_get_compress_blocks(inode, &blocks);
3457 return put_user(blocks, (u64 __user *)arg);
3460 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3462 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3463 unsigned int released_blocks = 0;
3464 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3468 for (i = 0; i < count; i++) {
3469 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3470 dn->ofs_in_node + i);
3472 if (!__is_valid_data_blkaddr(blkaddr))
3474 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3475 DATA_GENERIC_ENHANCE)))
3476 return -EFSCORRUPTED;
3480 int compr_blocks = 0;
3482 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3483 blkaddr = f2fs_data_blkaddr(dn);
3486 if (blkaddr == COMPRESS_ADDR)
3488 dn->ofs_in_node += cluster_size;
3492 if (__is_valid_data_blkaddr(blkaddr))
3495 if (blkaddr != NEW_ADDR)
3498 f2fs_set_data_blkaddr(dn, NULL_ADDR);
3501 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3502 dec_valid_block_count(sbi, dn->inode,
3503 cluster_size - compr_blocks);
3505 released_blocks += cluster_size - compr_blocks;
3507 count -= cluster_size;
3510 return released_blocks;
3513 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3515 struct inode *inode = file_inode(filp);
3516 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3517 pgoff_t page_idx = 0, last_idx;
3518 unsigned int released_blocks = 0;
3522 if (!f2fs_sb_has_compression(sbi))
3525 if (!f2fs_compressed_file(inode))
3528 if (f2fs_readonly(sbi->sb))
3531 ret = mnt_want_write_file(filp);
3535 f2fs_balance_fs(sbi, true);
3539 writecount = atomic_read(&inode->i_writecount);
3540 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3541 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3546 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3551 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3555 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3560 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3561 inode_set_ctime_current(inode);
3562 f2fs_mark_inode_dirty_sync(inode, true);
3564 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3565 filemap_invalidate_lock(inode->i_mapping);
3567 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3569 while (page_idx < last_idx) {
3570 struct dnode_of_data dn;
3571 pgoff_t end_offset, count;
3573 set_new_dnode(&dn, inode, NULL, NULL, 0);
3574 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3576 if (ret == -ENOENT) {
3577 page_idx = f2fs_get_next_page_offset(&dn,
3585 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3586 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3587 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3589 ret = release_compress_blocks(&dn, count);
3591 f2fs_put_dnode(&dn);
3597 released_blocks += ret;
3600 filemap_invalidate_unlock(inode->i_mapping);
3601 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3603 inode_unlock(inode);
3605 mnt_drop_write_file(filp);
3608 ret = put_user(released_blocks, (u64 __user *)arg);
3609 } else if (released_blocks &&
3610 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3611 set_sbi_flag(sbi, SBI_NEED_FSCK);
3612 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3613 "iblocks=%llu, released=%u, compr_blocks=%u, "
3615 __func__, inode->i_ino, inode->i_blocks,
3617 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3623 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,
3624 unsigned int *reserved_blocks)
3626 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3627 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3631 for (i = 0; i < count; i++) {
3632 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3633 dn->ofs_in_node + i);
3635 if (!__is_valid_data_blkaddr(blkaddr))
3637 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3638 DATA_GENERIC_ENHANCE)))
3639 return -EFSCORRUPTED;
3643 int compr_blocks = 0;
3647 for (i = 0; i < cluster_size; i++) {
3648 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3649 dn->ofs_in_node + i);
3652 if (blkaddr != COMPRESS_ADDR) {
3653 dn->ofs_in_node += cluster_size;
3660 * compressed cluster was not released due to it
3661 * fails in release_compress_blocks(), so NEW_ADDR
3662 * is a possible case.
3664 if (blkaddr == NEW_ADDR ||
3665 __is_valid_data_blkaddr(blkaddr)) {
3671 reserved = cluster_size - compr_blocks;
3673 /* for the case all blocks in cluster were reserved */
3677 ret = inc_valid_block_count(sbi, dn->inode, &reserved, false);
3681 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3682 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
3683 f2fs_set_data_blkaddr(dn, NEW_ADDR);
3686 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3688 *reserved_blocks += reserved;
3690 count -= cluster_size;
3696 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3698 struct inode *inode = file_inode(filp);
3699 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3700 pgoff_t page_idx = 0, last_idx;
3701 unsigned int reserved_blocks = 0;
3704 if (!f2fs_sb_has_compression(sbi))
3707 if (!f2fs_compressed_file(inode))
3710 if (f2fs_readonly(sbi->sb))
3713 ret = mnt_want_write_file(filp);
3717 f2fs_balance_fs(sbi, true);
3721 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3726 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3729 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3730 filemap_invalidate_lock(inode->i_mapping);
3732 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3734 while (page_idx < last_idx) {
3735 struct dnode_of_data dn;
3736 pgoff_t end_offset, count;
3738 set_new_dnode(&dn, inode, NULL, NULL, 0);
3739 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3741 if (ret == -ENOENT) {
3742 page_idx = f2fs_get_next_page_offset(&dn,
3750 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3751 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3752 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3754 ret = reserve_compress_blocks(&dn, count, &reserved_blocks);
3756 f2fs_put_dnode(&dn);
3764 filemap_invalidate_unlock(inode->i_mapping);
3765 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3768 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3769 inode_set_ctime_current(inode);
3770 f2fs_mark_inode_dirty_sync(inode, true);
3773 inode_unlock(inode);
3774 mnt_drop_write_file(filp);
3777 ret = put_user(reserved_blocks, (u64 __user *)arg);
3778 } else if (reserved_blocks &&
3779 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3780 set_sbi_flag(sbi, SBI_NEED_FSCK);
3781 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3782 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3784 __func__, inode->i_ino, inode->i_blocks,
3786 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3792 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3793 pgoff_t off, block_t block, block_t len, u32 flags)
3795 sector_t sector = SECTOR_FROM_BLOCK(block);
3796 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3799 if (flags & F2FS_TRIM_FILE_DISCARD) {
3800 if (bdev_max_secure_erase_sectors(bdev))
3801 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3804 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3808 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3809 if (IS_ENCRYPTED(inode))
3810 ret = fscrypt_zeroout_range(inode, off, block, len);
3812 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3819 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3821 struct inode *inode = file_inode(filp);
3822 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3823 struct address_space *mapping = inode->i_mapping;
3824 struct block_device *prev_bdev = NULL;
3825 struct f2fs_sectrim_range range;
3826 pgoff_t index, pg_end, prev_index = 0;
3827 block_t prev_block = 0, len = 0;
3829 bool to_end = false;
3832 if (!(filp->f_mode & FMODE_WRITE))
3835 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3839 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3840 !S_ISREG(inode->i_mode))
3843 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3844 !f2fs_hw_support_discard(sbi)) ||
3845 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3846 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3849 file_start_write(filp);
3852 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3853 range.start >= inode->i_size) {
3861 if (inode->i_size - range.start > range.len) {
3862 end_addr = range.start + range.len;
3864 end_addr = range.len == (u64)-1 ?
3865 sbi->sb->s_maxbytes : inode->i_size;
3869 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3870 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3875 index = F2FS_BYTES_TO_BLK(range.start);
3876 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3878 ret = f2fs_convert_inline_inode(inode);
3882 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3883 filemap_invalidate_lock(mapping);
3885 ret = filemap_write_and_wait_range(mapping, range.start,
3886 to_end ? LLONG_MAX : end_addr - 1);
3890 truncate_inode_pages_range(mapping, range.start,
3891 to_end ? -1 : end_addr - 1);
3893 while (index < pg_end) {
3894 struct dnode_of_data dn;
3895 pgoff_t end_offset, count;
3898 set_new_dnode(&dn, inode, NULL, NULL, 0);
3899 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3901 if (ret == -ENOENT) {
3902 index = f2fs_get_next_page_offset(&dn, index);
3908 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3909 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3910 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3911 struct block_device *cur_bdev;
3912 block_t blkaddr = f2fs_data_blkaddr(&dn);
3914 if (!__is_valid_data_blkaddr(blkaddr))
3917 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3918 DATA_GENERIC_ENHANCE)) {
3919 ret = -EFSCORRUPTED;
3920 f2fs_put_dnode(&dn);
3924 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3925 if (f2fs_is_multi_device(sbi)) {
3926 int di = f2fs_target_device_index(sbi, blkaddr);
3928 blkaddr -= FDEV(di).start_blk;
3932 if (prev_bdev == cur_bdev &&
3933 index == prev_index + len &&
3934 blkaddr == prev_block + len) {
3937 ret = f2fs_secure_erase(prev_bdev,
3938 inode, prev_index, prev_block,
3941 f2fs_put_dnode(&dn);
3950 prev_bdev = cur_bdev;
3952 prev_block = blkaddr;
3957 f2fs_put_dnode(&dn);
3959 if (fatal_signal_pending(current)) {
3967 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3968 prev_block, len, range.flags);
3970 filemap_invalidate_unlock(mapping);
3971 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3973 inode_unlock(inode);
3974 file_end_write(filp);
3979 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3981 struct inode *inode = file_inode(filp);
3982 struct f2fs_comp_option option;
3984 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3987 inode_lock_shared(inode);
3989 if (!f2fs_compressed_file(inode)) {
3990 inode_unlock_shared(inode);
3994 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3995 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3997 inode_unlock_shared(inode);
3999 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
4006 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
4008 struct inode *inode = file_inode(filp);
4009 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4010 struct f2fs_comp_option option;
4013 if (!f2fs_sb_has_compression(sbi))
4016 if (!(filp->f_mode & FMODE_WRITE))
4019 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
4023 if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
4024 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
4025 option.algorithm >= COMPRESS_MAX)
4028 file_start_write(filp);
4031 f2fs_down_write(&F2FS_I(inode)->i_sem);
4032 if (!f2fs_compressed_file(inode)) {
4037 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4042 if (F2FS_HAS_BLOCKS(inode)) {
4047 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
4048 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
4049 F2FS_I(inode)->i_cluster_size = BIT(option.log_cluster_size);
4050 /* Set default level */
4051 if (F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD)
4052 F2FS_I(inode)->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
4054 F2FS_I(inode)->i_compress_level = 0;
4055 /* Adjust mount option level */
4056 if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
4057 F2FS_OPTION(sbi).compress_level)
4058 F2FS_I(inode)->i_compress_level = F2FS_OPTION(sbi).compress_level;
4059 f2fs_mark_inode_dirty_sync(inode, true);
4061 if (!f2fs_is_compress_backend_ready(inode))
4062 f2fs_warn(sbi, "compression algorithm is successfully set, "
4063 "but current kernel doesn't support this algorithm.");
4065 f2fs_up_write(&F2FS_I(inode)->i_sem);
4066 inode_unlock(inode);
4067 file_end_write(filp);
4072 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4074 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
4075 struct address_space *mapping = inode->i_mapping;
4077 pgoff_t redirty_idx = page_idx;
4078 int i, page_len = 0, ret = 0;
4080 page_cache_ra_unbounded(&ractl, len, 0);
4082 for (i = 0; i < len; i++, page_idx++) {
4083 page = read_cache_page(mapping, page_idx, NULL, NULL);
4085 ret = PTR_ERR(page);
4091 for (i = 0; i < page_len; i++, redirty_idx++) {
4092 page = find_lock_page(mapping, redirty_idx);
4094 /* It will never fail, when page has pinned above */
4095 f2fs_bug_on(F2FS_I_SB(inode), !page);
4097 set_page_dirty(page);
4098 set_page_private_gcing(page);
4099 f2fs_put_page(page, 1);
4100 f2fs_put_page(page, 0);
4106 static int f2fs_ioc_decompress_file(struct file *filp)
4108 struct inode *inode = file_inode(filp);
4109 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4110 struct f2fs_inode_info *fi = F2FS_I(inode);
4111 pgoff_t page_idx = 0, last_idx;
4112 int cluster_size = fi->i_cluster_size;
4115 if (!f2fs_sb_has_compression(sbi) ||
4116 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4119 if (!(filp->f_mode & FMODE_WRITE))
4122 if (!f2fs_compressed_file(inode))
4125 f2fs_balance_fs(sbi, true);
4127 file_start_write(filp);
4130 if (!f2fs_is_compress_backend_ready(inode)) {
4135 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4140 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4144 if (!atomic_read(&fi->i_compr_blocks))
4147 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4149 count = last_idx - page_idx;
4150 while (count && count >= cluster_size) {
4151 ret = redirty_blocks(inode, page_idx, cluster_size);
4155 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4156 ret = filemap_fdatawrite(inode->i_mapping);
4161 count -= cluster_size;
4162 page_idx += cluster_size;
4165 if (fatal_signal_pending(current)) {
4172 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4176 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4179 inode_unlock(inode);
4180 file_end_write(filp);
4185 static int f2fs_ioc_compress_file(struct file *filp)
4187 struct inode *inode = file_inode(filp);
4188 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4189 pgoff_t page_idx = 0, last_idx;
4190 int cluster_size = F2FS_I(inode)->i_cluster_size;
4193 if (!f2fs_sb_has_compression(sbi) ||
4194 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4197 if (!(filp->f_mode & FMODE_WRITE))
4200 if (!f2fs_compressed_file(inode))
4203 f2fs_balance_fs(sbi, true);
4205 file_start_write(filp);
4208 if (!f2fs_is_compress_backend_ready(inode)) {
4213 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4218 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4222 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4224 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4226 count = last_idx - page_idx;
4227 while (count && count >= cluster_size) {
4228 ret = redirty_blocks(inode, page_idx, cluster_size);
4232 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4233 ret = filemap_fdatawrite(inode->i_mapping);
4238 count -= cluster_size;
4239 page_idx += cluster_size;
4242 if (fatal_signal_pending(current)) {
4249 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4252 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4255 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4258 inode_unlock(inode);
4259 file_end_write(filp);
4264 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4267 case FS_IOC_GETVERSION:
4268 return f2fs_ioc_getversion(filp, arg);
4269 case F2FS_IOC_START_ATOMIC_WRITE:
4270 return f2fs_ioc_start_atomic_write(filp, false);
4271 case F2FS_IOC_START_ATOMIC_REPLACE:
4272 return f2fs_ioc_start_atomic_write(filp, true);
4273 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4274 return f2fs_ioc_commit_atomic_write(filp);
4275 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4276 return f2fs_ioc_abort_atomic_write(filp);
4277 case F2FS_IOC_START_VOLATILE_WRITE:
4278 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4280 case F2FS_IOC_SHUTDOWN:
4281 return f2fs_ioc_shutdown(filp, arg);
4283 return f2fs_ioc_fitrim(filp, arg);
4284 case FS_IOC_SET_ENCRYPTION_POLICY:
4285 return f2fs_ioc_set_encryption_policy(filp, arg);
4286 case FS_IOC_GET_ENCRYPTION_POLICY:
4287 return f2fs_ioc_get_encryption_policy(filp, arg);
4288 case FS_IOC_GET_ENCRYPTION_PWSALT:
4289 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4290 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4291 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4292 case FS_IOC_ADD_ENCRYPTION_KEY:
4293 return f2fs_ioc_add_encryption_key(filp, arg);
4294 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4295 return f2fs_ioc_remove_encryption_key(filp, arg);
4296 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4297 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4298 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4299 return f2fs_ioc_get_encryption_key_status(filp, arg);
4300 case FS_IOC_GET_ENCRYPTION_NONCE:
4301 return f2fs_ioc_get_encryption_nonce(filp, arg);
4302 case F2FS_IOC_GARBAGE_COLLECT:
4303 return f2fs_ioc_gc(filp, arg);
4304 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4305 return f2fs_ioc_gc_range(filp, arg);
4306 case F2FS_IOC_WRITE_CHECKPOINT:
4307 return f2fs_ioc_write_checkpoint(filp);
4308 case F2FS_IOC_DEFRAGMENT:
4309 return f2fs_ioc_defragment(filp, arg);
4310 case F2FS_IOC_MOVE_RANGE:
4311 return f2fs_ioc_move_range(filp, arg);
4312 case F2FS_IOC_FLUSH_DEVICE:
4313 return f2fs_ioc_flush_device(filp, arg);
4314 case F2FS_IOC_GET_FEATURES:
4315 return f2fs_ioc_get_features(filp, arg);
4316 case F2FS_IOC_GET_PIN_FILE:
4317 return f2fs_ioc_get_pin_file(filp, arg);
4318 case F2FS_IOC_SET_PIN_FILE:
4319 return f2fs_ioc_set_pin_file(filp, arg);
4320 case F2FS_IOC_PRECACHE_EXTENTS:
4321 return f2fs_ioc_precache_extents(filp);
4322 case F2FS_IOC_RESIZE_FS:
4323 return f2fs_ioc_resize_fs(filp, arg);
4324 case FS_IOC_ENABLE_VERITY:
4325 return f2fs_ioc_enable_verity(filp, arg);
4326 case FS_IOC_MEASURE_VERITY:
4327 return f2fs_ioc_measure_verity(filp, arg);
4328 case FS_IOC_READ_VERITY_METADATA:
4329 return f2fs_ioc_read_verity_metadata(filp, arg);
4330 case FS_IOC_GETFSLABEL:
4331 return f2fs_ioc_getfslabel(filp, arg);
4332 case FS_IOC_SETFSLABEL:
4333 return f2fs_ioc_setfslabel(filp, arg);
4334 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4335 return f2fs_ioc_get_compress_blocks(filp, arg);
4336 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4337 return f2fs_release_compress_blocks(filp, arg);
4338 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4339 return f2fs_reserve_compress_blocks(filp, arg);
4340 case F2FS_IOC_SEC_TRIM_FILE:
4341 return f2fs_sec_trim_file(filp, arg);
4342 case F2FS_IOC_GET_COMPRESS_OPTION:
4343 return f2fs_ioc_get_compress_option(filp, arg);
4344 case F2FS_IOC_SET_COMPRESS_OPTION:
4345 return f2fs_ioc_set_compress_option(filp, arg);
4346 case F2FS_IOC_DECOMPRESS_FILE:
4347 return f2fs_ioc_decompress_file(filp);
4348 case F2FS_IOC_COMPRESS_FILE:
4349 return f2fs_ioc_compress_file(filp);
4355 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4357 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4359 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4362 return __f2fs_ioctl(filp, cmd, arg);
4366 * Return %true if the given read or write request should use direct I/O, or
4367 * %false if it should use buffered I/O.
4369 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4370 struct iov_iter *iter)
4374 if (!(iocb->ki_flags & IOCB_DIRECT))
4377 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4381 * Direct I/O not aligned to the disk's logical_block_size will be
4382 * attempted, but will fail with -EINVAL.
4384 * f2fs additionally requires that direct I/O be aligned to the
4385 * filesystem block size, which is often a stricter requirement.
4386 * However, f2fs traditionally falls back to buffered I/O on requests
4387 * that are logical_block_size-aligned but not fs-block aligned.
4389 * The below logic implements this behavior.
4391 align = iocb->ki_pos | iov_iter_alignment(iter);
4392 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4393 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4399 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4402 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4404 dec_page_count(sbi, F2FS_DIO_READ);
4407 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4411 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4412 .end_io = f2fs_dio_read_end_io,
4415 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4417 struct file *file = iocb->ki_filp;
4418 struct inode *inode = file_inode(file);
4419 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4420 struct f2fs_inode_info *fi = F2FS_I(inode);
4421 const loff_t pos = iocb->ki_pos;
4422 const size_t count = iov_iter_count(to);
4423 struct iomap_dio *dio;
4427 return 0; /* skip atime update */
4429 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4431 if (iocb->ki_flags & IOCB_NOWAIT) {
4432 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4437 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4441 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4442 * the higher-level function iomap_dio_rw() in order to ensure that the
4443 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4445 inc_page_count(sbi, F2FS_DIO_READ);
4446 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4447 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4448 if (IS_ERR_OR_NULL(dio)) {
4449 ret = PTR_ERR_OR_ZERO(dio);
4450 if (ret != -EIOCBQUEUED)
4451 dec_page_count(sbi, F2FS_DIO_READ);
4453 ret = iomap_dio_complete(dio);
4456 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4458 file_accessed(file);
4460 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4464 static void f2fs_trace_rw_file_path(struct file *file, loff_t pos, size_t count,
4467 struct inode *inode = file_inode(file);
4470 buf = f2fs_getname(F2FS_I_SB(inode));
4473 path = dentry_path_raw(file_dentry(file), buf, PATH_MAX);
4477 trace_f2fs_datawrite_start(inode, pos, count,
4478 current->pid, path, current->comm);
4480 trace_f2fs_dataread_start(inode, pos, count,
4481 current->pid, path, current->comm);
4486 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4488 struct inode *inode = file_inode(iocb->ki_filp);
4489 const loff_t pos = iocb->ki_pos;
4492 if (!f2fs_is_compress_backend_ready(inode))
4495 if (trace_f2fs_dataread_start_enabled())
4496 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4497 iov_iter_count(to), READ);
4499 if (f2fs_should_use_dio(inode, iocb, to)) {
4500 ret = f2fs_dio_read_iter(iocb, to);
4502 ret = filemap_read(iocb, to, 0);
4504 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4505 APP_BUFFERED_READ_IO, ret);
4507 if (trace_f2fs_dataread_end_enabled())
4508 trace_f2fs_dataread_end(inode, pos, ret);
4512 static ssize_t f2fs_file_splice_read(struct file *in, loff_t *ppos,
4513 struct pipe_inode_info *pipe,
4514 size_t len, unsigned int flags)
4516 struct inode *inode = file_inode(in);
4517 const loff_t pos = *ppos;
4520 if (!f2fs_is_compress_backend_ready(inode))
4523 if (trace_f2fs_dataread_start_enabled())
4524 f2fs_trace_rw_file_path(in, pos, len, READ);
4526 ret = filemap_splice_read(in, ppos, pipe, len, flags);
4528 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4529 APP_BUFFERED_READ_IO, ret);
4531 if (trace_f2fs_dataread_end_enabled())
4532 trace_f2fs_dataread_end(inode, pos, ret);
4536 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4538 struct file *file = iocb->ki_filp;
4539 struct inode *inode = file_inode(file);
4543 if (IS_IMMUTABLE(inode))
4546 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4549 count = generic_write_checks(iocb, from);
4553 err = file_modified(file);
4560 * Preallocate blocks for a write request, if it is possible and helpful to do
4561 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4562 * blocks were preallocated, or a negative errno value if something went
4563 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4564 * requested blocks (not just some of them) have been allocated.
4566 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4569 struct inode *inode = file_inode(iocb->ki_filp);
4570 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4571 const loff_t pos = iocb->ki_pos;
4572 const size_t count = iov_iter_count(iter);
4573 struct f2fs_map_blocks map = {};
4577 /* If it will be an out-of-place direct write, don't bother. */
4578 if (dio && f2fs_lfs_mode(sbi))
4581 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4582 * buffered IO, if DIO meets any holes.
4584 if (dio && i_size_read(inode) &&
4585 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4588 /* No-wait I/O can't allocate blocks. */
4589 if (iocb->ki_flags & IOCB_NOWAIT)
4592 /* If it will be a short write, don't bother. */
4593 if (fault_in_iov_iter_readable(iter, count))
4596 if (f2fs_has_inline_data(inode)) {
4597 /* If the data will fit inline, don't bother. */
4598 if (pos + count <= MAX_INLINE_DATA(inode))
4600 ret = f2fs_convert_inline_inode(inode);
4605 /* Do not preallocate blocks that will be written partially in 4KB. */
4606 map.m_lblk = F2FS_BLK_ALIGN(pos);
4607 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4608 if (map.m_len > map.m_lblk)
4609 map.m_len -= map.m_lblk;
4613 map.m_may_create = true;
4615 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4616 flag = F2FS_GET_BLOCK_PRE_DIO;
4618 map.m_seg_type = NO_CHECK_TYPE;
4619 flag = F2FS_GET_BLOCK_PRE_AIO;
4622 ret = f2fs_map_blocks(inode, &map, flag);
4623 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4624 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4627 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4631 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4632 struct iov_iter *from)
4634 struct file *file = iocb->ki_filp;
4635 struct inode *inode = file_inode(file);
4638 if (iocb->ki_flags & IOCB_NOWAIT)
4641 ret = generic_perform_write(iocb, from);
4644 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4645 APP_BUFFERED_IO, ret);
4650 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4653 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4655 dec_page_count(sbi, F2FS_DIO_WRITE);
4658 f2fs_update_time(sbi, REQ_TIME);
4659 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4663 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4664 .end_io = f2fs_dio_write_end_io,
4667 static void f2fs_flush_buffered_write(struct address_space *mapping,
4668 loff_t start_pos, loff_t end_pos)
4672 ret = filemap_write_and_wait_range(mapping, start_pos, end_pos);
4675 invalidate_mapping_pages(mapping,
4676 start_pos >> PAGE_SHIFT,
4677 end_pos >> PAGE_SHIFT);
4680 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4681 bool *may_need_sync)
4683 struct file *file = iocb->ki_filp;
4684 struct inode *inode = file_inode(file);
4685 struct f2fs_inode_info *fi = F2FS_I(inode);
4686 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4687 const bool do_opu = f2fs_lfs_mode(sbi);
4688 const loff_t pos = iocb->ki_pos;
4689 const ssize_t count = iov_iter_count(from);
4690 unsigned int dio_flags;
4691 struct iomap_dio *dio;
4694 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4696 if (iocb->ki_flags & IOCB_NOWAIT) {
4697 /* f2fs_convert_inline_inode() and block allocation can block */
4698 if (f2fs_has_inline_data(inode) ||
4699 !f2fs_overwrite_io(inode, pos, count)) {
4704 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4708 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4709 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4714 ret = f2fs_convert_inline_inode(inode);
4718 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4720 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4724 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4725 * the higher-level function iomap_dio_rw() in order to ensure that the
4726 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4728 inc_page_count(sbi, F2FS_DIO_WRITE);
4730 if (pos + count > inode->i_size)
4731 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4732 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4733 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4734 if (IS_ERR_OR_NULL(dio)) {
4735 ret = PTR_ERR_OR_ZERO(dio);
4736 if (ret == -ENOTBLK)
4738 if (ret != -EIOCBQUEUED)
4739 dec_page_count(sbi, F2FS_DIO_WRITE);
4741 ret = iomap_dio_complete(dio);
4745 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4746 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4750 if (pos + ret > inode->i_size)
4751 f2fs_i_size_write(inode, pos + ret);
4753 set_inode_flag(inode, FI_UPDATE_WRITE);
4755 if (iov_iter_count(from)) {
4757 loff_t bufio_start_pos = iocb->ki_pos;
4760 * The direct write was partial, so we need to fall back to a
4761 * buffered write for the remainder.
4764 ret2 = f2fs_buffered_write_iter(iocb, from);
4765 if (iov_iter_count(from))
4766 f2fs_write_failed(inode, iocb->ki_pos);
4771 * Ensure that the pagecache pages are written to disk and
4772 * invalidated to preserve the expected O_DIRECT semantics.
4775 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4779 f2fs_flush_buffered_write(file->f_mapping,
4784 /* iomap_dio_rw() already handled the generic_write_sync(). */
4785 *may_need_sync = false;
4788 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4792 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4794 struct inode *inode = file_inode(iocb->ki_filp);
4795 const loff_t orig_pos = iocb->ki_pos;
4796 const size_t orig_count = iov_iter_count(from);
4799 bool may_need_sync = true;
4803 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4808 if (!f2fs_is_compress_backend_ready(inode)) {
4813 if (iocb->ki_flags & IOCB_NOWAIT) {
4814 if (!inode_trylock(inode)) {
4822 ret = f2fs_write_checks(iocb, from);
4826 /* Determine whether we will do a direct write or a buffered write. */
4827 dio = f2fs_should_use_dio(inode, iocb, from);
4829 /* Possibly preallocate the blocks for the write. */
4830 target_size = iocb->ki_pos + iov_iter_count(from);
4831 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4832 if (preallocated < 0) {
4835 if (trace_f2fs_datawrite_start_enabled())
4836 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4839 /* Do the actual write. */
4841 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
4842 f2fs_buffered_write_iter(iocb, from);
4844 if (trace_f2fs_datawrite_end_enabled())
4845 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4848 /* Don't leave any preallocated blocks around past i_size. */
4849 if (preallocated && i_size_read(inode) < target_size) {
4850 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4851 filemap_invalidate_lock(inode->i_mapping);
4852 if (!f2fs_truncate(inode))
4853 file_dont_truncate(inode);
4854 filemap_invalidate_unlock(inode->i_mapping);
4855 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4857 file_dont_truncate(inode);
4860 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4862 inode_unlock(inode);
4864 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4866 if (ret > 0 && may_need_sync)
4867 ret = generic_write_sync(iocb, ret);
4869 /* If buffered IO was forced, flush and drop the data from
4870 * the page cache to preserve O_DIRECT semantics
4872 if (ret > 0 && !dio && (iocb->ki_flags & IOCB_DIRECT))
4873 f2fs_flush_buffered_write(iocb->ki_filp->f_mapping,
4875 orig_pos + ret - 1);
4880 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4883 struct address_space *mapping;
4884 struct backing_dev_info *bdi;
4885 struct inode *inode = file_inode(filp);
4888 if (advice == POSIX_FADV_SEQUENTIAL) {
4889 if (S_ISFIFO(inode->i_mode))
4892 mapping = filp->f_mapping;
4893 if (!mapping || len < 0)
4896 bdi = inode_to_bdi(mapping->host);
4897 filp->f_ra.ra_pages = bdi->ra_pages *
4898 F2FS_I_SB(inode)->seq_file_ra_mul;
4899 spin_lock(&filp->f_lock);
4900 filp->f_mode &= ~FMODE_RANDOM;
4901 spin_unlock(&filp->f_lock);
4903 } else if (advice == POSIX_FADV_WILLNEED && offset == 0) {
4904 /* Load extent cache at the first readahead. */
4905 f2fs_precache_extents(inode);
4908 err = generic_fadvise(filp, offset, len, advice);
4909 if (!err && advice == POSIX_FADV_DONTNEED &&
4910 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4911 f2fs_compressed_file(inode))
4912 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4917 #ifdef CONFIG_COMPAT
4918 struct compat_f2fs_gc_range {
4923 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4924 struct compat_f2fs_gc_range)
4926 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4928 struct compat_f2fs_gc_range __user *urange;
4929 struct f2fs_gc_range range;
4932 urange = compat_ptr(arg);
4933 err = get_user(range.sync, &urange->sync);
4934 err |= get_user(range.start, &urange->start);
4935 err |= get_user(range.len, &urange->len);
4939 return __f2fs_ioc_gc_range(file, &range);
4942 struct compat_f2fs_move_range {
4948 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4949 struct compat_f2fs_move_range)
4951 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4953 struct compat_f2fs_move_range __user *urange;
4954 struct f2fs_move_range range;
4957 urange = compat_ptr(arg);
4958 err = get_user(range.dst_fd, &urange->dst_fd);
4959 err |= get_user(range.pos_in, &urange->pos_in);
4960 err |= get_user(range.pos_out, &urange->pos_out);
4961 err |= get_user(range.len, &urange->len);
4965 return __f2fs_ioc_move_range(file, &range);
4968 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4970 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4972 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4976 case FS_IOC32_GETVERSION:
4977 cmd = FS_IOC_GETVERSION;
4979 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4980 return f2fs_compat_ioc_gc_range(file, arg);
4981 case F2FS_IOC32_MOVE_RANGE:
4982 return f2fs_compat_ioc_move_range(file, arg);
4983 case F2FS_IOC_START_ATOMIC_WRITE:
4984 case F2FS_IOC_START_ATOMIC_REPLACE:
4985 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4986 case F2FS_IOC_START_VOLATILE_WRITE:
4987 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4988 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4989 case F2FS_IOC_SHUTDOWN:
4991 case FS_IOC_SET_ENCRYPTION_POLICY:
4992 case FS_IOC_GET_ENCRYPTION_PWSALT:
4993 case FS_IOC_GET_ENCRYPTION_POLICY:
4994 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4995 case FS_IOC_ADD_ENCRYPTION_KEY:
4996 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4997 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4998 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4999 case FS_IOC_GET_ENCRYPTION_NONCE:
5000 case F2FS_IOC_GARBAGE_COLLECT:
5001 case F2FS_IOC_WRITE_CHECKPOINT:
5002 case F2FS_IOC_DEFRAGMENT:
5003 case F2FS_IOC_FLUSH_DEVICE:
5004 case F2FS_IOC_GET_FEATURES:
5005 case F2FS_IOC_GET_PIN_FILE:
5006 case F2FS_IOC_SET_PIN_FILE:
5007 case F2FS_IOC_PRECACHE_EXTENTS:
5008 case F2FS_IOC_RESIZE_FS:
5009 case FS_IOC_ENABLE_VERITY:
5010 case FS_IOC_MEASURE_VERITY:
5011 case FS_IOC_READ_VERITY_METADATA:
5012 case FS_IOC_GETFSLABEL:
5013 case FS_IOC_SETFSLABEL:
5014 case F2FS_IOC_GET_COMPRESS_BLOCKS:
5015 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
5016 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
5017 case F2FS_IOC_SEC_TRIM_FILE:
5018 case F2FS_IOC_GET_COMPRESS_OPTION:
5019 case F2FS_IOC_SET_COMPRESS_OPTION:
5020 case F2FS_IOC_DECOMPRESS_FILE:
5021 case F2FS_IOC_COMPRESS_FILE:
5024 return -ENOIOCTLCMD;
5026 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5030 const struct file_operations f2fs_file_operations = {
5031 .llseek = f2fs_llseek,
5032 .read_iter = f2fs_file_read_iter,
5033 .write_iter = f2fs_file_write_iter,
5034 .iopoll = iocb_bio_iopoll,
5035 .open = f2fs_file_open,
5036 .release = f2fs_release_file,
5037 .mmap = f2fs_file_mmap,
5038 .flush = f2fs_file_flush,
5039 .fsync = f2fs_sync_file,
5040 .fallocate = f2fs_fallocate,
5041 .unlocked_ioctl = f2fs_ioctl,
5042 #ifdef CONFIG_COMPAT
5043 .compat_ioctl = f2fs_compat_ioctl,
5045 .splice_read = f2fs_file_splice_read,
5046 .splice_write = iter_file_splice_write,
5047 .fadvise = f2fs_file_fadvise,