1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/buffer_head.h>
12 #include <linux/writeback.h>
13 #include <linux/blkdev.h>
14 #include <linux/falloc.h>
15 #include <linux/types.h>
16 #include <linux/compat.h>
17 #include <linux/uaccess.h>
18 #include <linux/mount.h>
19 #include <linux/pagevec.h>
20 #include <linux/uio.h>
21 #include <linux/uuid.h>
22 #include <linux/file.h>
23 #include <linux/nls.h>
24 #include <linux/sched/signal.h>
25 #include <linux/fileattr.h>
26 #include <linux/fadvise.h>
27 #include <linux/iomap.h>
36 #include <trace/events/f2fs.h>
37 #include <uapi/linux/f2fs.h>
39 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
41 struct inode *inode = file_inode(vmf->vma->vm_file);
44 ret = filemap_fault(vmf);
46 f2fs_update_iostat(F2FS_I_SB(inode), inode,
47 APP_MAPPED_READ_IO, F2FS_BLKSIZE);
49 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
54 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
56 struct page *page = vmf->page;
57 struct inode *inode = file_inode(vmf->vma->vm_file);
58 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
59 struct dnode_of_data dn;
60 bool need_alloc = true;
63 if (unlikely(IS_IMMUTABLE(inode)))
64 return VM_FAULT_SIGBUS;
66 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
67 return VM_FAULT_SIGBUS;
69 if (unlikely(f2fs_cp_error(sbi))) {
74 if (!f2fs_is_checkpoint_ready(sbi)) {
79 err = f2fs_convert_inline_inode(inode);
83 #ifdef CONFIG_F2FS_FS_COMPRESSION
84 if (f2fs_compressed_file(inode)) {
85 int ret = f2fs_is_compressed_cluster(inode, page->index);
95 /* should do out of any locked page */
97 f2fs_balance_fs(sbi, true);
99 sb_start_pagefault(inode->i_sb);
101 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
103 file_update_time(vmf->vma->vm_file);
104 filemap_invalidate_lock_shared(inode->i_mapping);
106 if (unlikely(page->mapping != inode->i_mapping ||
107 page_offset(page) > i_size_read(inode) ||
108 !PageUptodate(page))) {
115 /* block allocation */
116 set_new_dnode(&dn, inode, NULL, NULL, 0);
117 err = f2fs_get_block_locked(&dn, page->index);
120 #ifdef CONFIG_F2FS_FS_COMPRESSION
122 set_new_dnode(&dn, inode, NULL, NULL, 0);
123 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
132 f2fs_wait_on_page_writeback(page, DATA, false, true);
134 /* wait for GCed page writeback via META_MAPPING */
135 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
138 * check to see if the page is mapped already (no holes)
140 if (PageMappedToDisk(page))
143 /* page is wholly or partially inside EOF */
144 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
145 i_size_read(inode)) {
148 offset = i_size_read(inode) & ~PAGE_MASK;
149 zero_user_segment(page, offset, PAGE_SIZE);
151 set_page_dirty(page);
153 f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
154 f2fs_update_time(sbi, REQ_TIME);
156 trace_f2fs_vm_page_mkwrite(page, DATA);
158 filemap_invalidate_unlock_shared(inode->i_mapping);
160 sb_end_pagefault(inode->i_sb);
162 return vmf_fs_error(err);
165 static const struct vm_operations_struct f2fs_file_vm_ops = {
166 .fault = f2fs_filemap_fault,
167 .map_pages = filemap_map_pages,
168 .page_mkwrite = f2fs_vm_page_mkwrite,
171 static int get_parent_ino(struct inode *inode, nid_t *pino)
173 struct dentry *dentry;
176 * Make sure to get the non-deleted alias. The alias associated with
177 * the open file descriptor being fsync()'ed may be deleted already.
179 dentry = d_find_alias(inode);
183 *pino = parent_ino(dentry);
188 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
190 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
191 enum cp_reason_type cp_reason = CP_NO_NEEDED;
193 if (!S_ISREG(inode->i_mode))
194 cp_reason = CP_NON_REGULAR;
195 else if (f2fs_compressed_file(inode))
196 cp_reason = CP_COMPRESSED;
197 else if (inode->i_nlink != 1)
198 cp_reason = CP_HARDLINK;
199 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
200 cp_reason = CP_SB_NEED_CP;
201 else if (file_wrong_pino(inode))
202 cp_reason = CP_WRONG_PINO;
203 else if (!f2fs_space_for_roll_forward(sbi))
204 cp_reason = CP_NO_SPC_ROLL;
205 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
206 cp_reason = CP_NODE_NEED_CP;
207 else if (test_opt(sbi, FASTBOOT))
208 cp_reason = CP_FASTBOOT_MODE;
209 else if (F2FS_OPTION(sbi).active_logs == 2)
210 cp_reason = CP_SPEC_LOG_NUM;
211 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
212 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
213 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
215 cp_reason = CP_RECOVER_DIR;
220 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
222 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
224 /* But we need to avoid that there are some inode updates */
225 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
231 static void try_to_fix_pino(struct inode *inode)
233 struct f2fs_inode_info *fi = F2FS_I(inode);
236 f2fs_down_write(&fi->i_sem);
237 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
238 get_parent_ino(inode, &pino)) {
239 f2fs_i_pino_write(inode, pino);
240 file_got_pino(inode);
242 f2fs_up_write(&fi->i_sem);
245 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
246 int datasync, bool atomic)
248 struct inode *inode = file->f_mapping->host;
249 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
250 nid_t ino = inode->i_ino;
252 enum cp_reason_type cp_reason = 0;
253 struct writeback_control wbc = {
254 .sync_mode = WB_SYNC_ALL,
255 .nr_to_write = LONG_MAX,
258 unsigned int seq_id = 0;
260 if (unlikely(f2fs_readonly(inode->i_sb)))
263 trace_f2fs_sync_file_enter(inode);
265 if (S_ISDIR(inode->i_mode))
268 /* if fdatasync is triggered, let's do in-place-update */
269 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
270 set_inode_flag(inode, FI_NEED_IPU);
271 ret = file_write_and_wait_range(file, start, end);
272 clear_inode_flag(inode, FI_NEED_IPU);
274 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
275 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
279 /* if the inode is dirty, let's recover all the time */
280 if (!f2fs_skip_inode_update(inode, datasync)) {
281 f2fs_write_inode(inode, NULL);
286 * if there is no written data, don't waste time to write recovery info.
288 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
289 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
291 /* it may call write_inode just prior to fsync */
292 if (need_inode_page_update(sbi, ino))
295 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
296 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
301 * for OPU case, during fsync(), node can be persisted before
302 * data when lower device doesn't support write barrier, result
303 * in data corruption after SPO.
304 * So for strict fsync mode, force to use atomic write semantics
305 * to keep write order in between data/node and last node to
306 * avoid potential data corruption.
308 if (F2FS_OPTION(sbi).fsync_mode ==
309 FSYNC_MODE_STRICT && !atomic)
314 * Both of fdatasync() and fsync() are able to be recovered from
317 f2fs_down_read(&F2FS_I(inode)->i_sem);
318 cp_reason = need_do_checkpoint(inode);
319 f2fs_up_read(&F2FS_I(inode)->i_sem);
322 /* all the dirty node pages should be flushed for POR */
323 ret = f2fs_sync_fs(inode->i_sb, 1);
326 * We've secured consistency through sync_fs. Following pino
327 * will be used only for fsynced inodes after checkpoint.
329 try_to_fix_pino(inode);
330 clear_inode_flag(inode, FI_APPEND_WRITE);
331 clear_inode_flag(inode, FI_UPDATE_WRITE);
335 atomic_inc(&sbi->wb_sync_req[NODE]);
336 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
337 atomic_dec(&sbi->wb_sync_req[NODE]);
341 /* if cp_error was enabled, we should avoid infinite loop */
342 if (unlikely(f2fs_cp_error(sbi))) {
347 if (f2fs_need_inode_block_update(sbi, ino)) {
348 f2fs_mark_inode_dirty_sync(inode, true);
349 f2fs_write_inode(inode, NULL);
354 * If it's atomic_write, it's just fine to keep write ordering. So
355 * here we don't need to wait for node write completion, since we use
356 * node chain which serializes node blocks. If one of node writes are
357 * reordered, we can see simply broken chain, resulting in stopping
358 * roll-forward recovery. It means we'll recover all or none node blocks
362 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
367 /* once recovery info is written, don't need to tack this */
368 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
369 clear_inode_flag(inode, FI_APPEND_WRITE);
371 if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) ||
372 (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi)))
373 ret = f2fs_issue_flush(sbi, inode->i_ino);
375 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
376 clear_inode_flag(inode, FI_UPDATE_WRITE);
377 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
379 f2fs_update_time(sbi, REQ_TIME);
381 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
385 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
387 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
389 return f2fs_do_sync_file(file, start, end, datasync, false);
392 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
393 pgoff_t index, int whence)
397 if (__is_valid_data_blkaddr(blkaddr))
399 if (blkaddr == NEW_ADDR &&
400 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
404 if (blkaddr == NULL_ADDR)
411 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
413 struct inode *inode = file->f_mapping->host;
414 loff_t maxbytes = inode->i_sb->s_maxbytes;
415 struct dnode_of_data dn;
416 pgoff_t pgofs, end_offset;
417 loff_t data_ofs = offset;
423 isize = i_size_read(inode);
427 /* handle inline data case */
428 if (f2fs_has_inline_data(inode)) {
429 if (whence == SEEK_HOLE) {
432 } else if (whence == SEEK_DATA) {
438 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
440 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
441 set_new_dnode(&dn, inode, NULL, NULL, 0);
442 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
443 if (err && err != -ENOENT) {
445 } else if (err == -ENOENT) {
446 /* direct node does not exists */
447 if (whence == SEEK_DATA) {
448 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
455 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
457 /* find data/hole in dnode block */
458 for (; dn.ofs_in_node < end_offset;
459 dn.ofs_in_node++, pgofs++,
460 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
463 blkaddr = f2fs_data_blkaddr(&dn);
465 if (__is_valid_data_blkaddr(blkaddr) &&
466 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
467 blkaddr, DATA_GENERIC_ENHANCE)) {
472 if (__found_offset(file->f_mapping, blkaddr,
481 if (whence == SEEK_DATA)
484 if (whence == SEEK_HOLE && data_ofs > isize)
487 return vfs_setpos(file, data_ofs, maxbytes);
493 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
495 struct inode *inode = file->f_mapping->host;
496 loff_t maxbytes = inode->i_sb->s_maxbytes;
498 if (f2fs_compressed_file(inode))
499 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
505 return generic_file_llseek_size(file, offset, whence,
506 maxbytes, i_size_read(inode));
511 return f2fs_seek_block(file, offset, whence);
517 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
519 struct inode *inode = file_inode(file);
521 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
524 if (!f2fs_is_compress_backend_ready(inode))
528 vma->vm_ops = &f2fs_file_vm_ops;
530 f2fs_down_read(&F2FS_I(inode)->i_sem);
531 set_inode_flag(inode, FI_MMAP_FILE);
532 f2fs_up_read(&F2FS_I(inode)->i_sem);
537 static int f2fs_file_open(struct inode *inode, struct file *filp)
539 int err = fscrypt_file_open(inode, filp);
544 if (!f2fs_is_compress_backend_ready(inode))
547 err = fsverity_file_open(inode, filp);
551 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
552 filp->f_mode |= FMODE_CAN_ODIRECT;
554 return dquot_file_open(inode, filp);
557 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
559 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
560 struct f2fs_node *raw_node;
561 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
564 bool compressed_cluster = false;
565 int cluster_index = 0, valid_blocks = 0;
566 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
567 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
569 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
570 base = get_extra_isize(dn->inode);
572 raw_node = F2FS_NODE(dn->node_page);
573 addr = blkaddr_in_node(raw_node) + base + ofs;
575 /* Assumption: truncation starts with cluster */
576 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
577 block_t blkaddr = le32_to_cpu(*addr);
579 if (f2fs_compressed_file(dn->inode) &&
580 !(cluster_index & (cluster_size - 1))) {
581 if (compressed_cluster)
582 f2fs_i_compr_blocks_update(dn->inode,
583 valid_blocks, false);
584 compressed_cluster = (blkaddr == COMPRESS_ADDR);
588 if (blkaddr == NULL_ADDR)
591 dn->data_blkaddr = NULL_ADDR;
592 f2fs_set_data_blkaddr(dn);
594 if (__is_valid_data_blkaddr(blkaddr)) {
595 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
596 DATA_GENERIC_ENHANCE))
598 if (compressed_cluster)
602 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
603 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
605 f2fs_invalidate_blocks(sbi, blkaddr);
607 if (!released || blkaddr != COMPRESS_ADDR)
611 if (compressed_cluster)
612 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
617 * once we invalidate valid blkaddr in range [ofs, ofs + count],
618 * we will invalidate all blkaddr in the whole range.
620 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
622 f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
623 f2fs_update_age_extent_cache_range(dn, fofs, len);
624 dec_valid_block_count(sbi, dn->inode, nr_free);
626 dn->ofs_in_node = ofs;
628 f2fs_update_time(sbi, REQ_TIME);
629 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
630 dn->ofs_in_node, nr_free);
633 static int truncate_partial_data_page(struct inode *inode, u64 from,
636 loff_t offset = from & (PAGE_SIZE - 1);
637 pgoff_t index = from >> PAGE_SHIFT;
638 struct address_space *mapping = inode->i_mapping;
641 if (!offset && !cache_only)
645 page = find_lock_page(mapping, index);
646 if (page && PageUptodate(page))
648 f2fs_put_page(page, 1);
652 page = f2fs_get_lock_data_page(inode, index, true);
654 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
656 f2fs_wait_on_page_writeback(page, DATA, true, true);
657 zero_user(page, offset, PAGE_SIZE - offset);
659 /* An encrypted inode should have a key and truncate the last page. */
660 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
662 set_page_dirty(page);
663 f2fs_put_page(page, 1);
667 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
669 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
670 struct dnode_of_data dn;
672 int count = 0, err = 0;
674 bool truncate_page = false;
676 trace_f2fs_truncate_blocks_enter(inode, from);
678 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
680 if (free_from >= max_file_blocks(inode))
686 ipage = f2fs_get_node_page(sbi, inode->i_ino);
688 err = PTR_ERR(ipage);
692 if (f2fs_has_inline_data(inode)) {
693 f2fs_truncate_inline_inode(inode, ipage, from);
694 f2fs_put_page(ipage, 1);
695 truncate_page = true;
699 set_new_dnode(&dn, inode, ipage, NULL, 0);
700 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
707 count = ADDRS_PER_PAGE(dn.node_page, inode);
709 count -= dn.ofs_in_node;
710 f2fs_bug_on(sbi, count < 0);
712 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
713 f2fs_truncate_data_blocks_range(&dn, count);
719 err = f2fs_truncate_inode_blocks(inode, free_from);
724 /* lastly zero out the first data page */
726 err = truncate_partial_data_page(inode, from, truncate_page);
728 trace_f2fs_truncate_blocks_exit(inode, err);
732 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
734 u64 free_from = from;
737 #ifdef CONFIG_F2FS_FS_COMPRESSION
739 * for compressed file, only support cluster size
740 * aligned truncation.
742 if (f2fs_compressed_file(inode))
743 free_from = round_up(from,
744 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
747 err = f2fs_do_truncate_blocks(inode, free_from, lock);
751 #ifdef CONFIG_F2FS_FS_COMPRESSION
753 * For compressed file, after release compress blocks, don't allow write
754 * direct, but we should allow write direct after truncate to zero.
756 if (f2fs_compressed_file(inode) && !free_from
757 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
758 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
760 if (from != free_from) {
761 err = f2fs_truncate_partial_cluster(inode, from, lock);
770 int f2fs_truncate(struct inode *inode)
774 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
777 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
778 S_ISLNK(inode->i_mode)))
781 trace_f2fs_truncate(inode);
783 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
786 err = f2fs_dquot_initialize(inode);
790 /* we should check inline_data size */
791 if (!f2fs_may_inline_data(inode)) {
792 err = f2fs_convert_inline_inode(inode);
797 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
801 inode->i_mtime = inode_set_ctime_current(inode);
802 f2fs_mark_inode_dirty_sync(inode, false);
806 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
808 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
810 if (!fscrypt_dio_supported(inode))
812 if (fsverity_active(inode))
814 if (f2fs_compressed_file(inode))
817 /* disallow direct IO if any of devices has unaligned blksize */
818 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
821 * for blkzoned device, fallback direct IO to buffered IO, so
822 * all IOs can be serialized by log-structured write.
824 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE))
826 if (f2fs_lfs_mode(sbi) && rw == WRITE && F2FS_IO_ALIGNED(sbi))
828 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
834 int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
835 struct kstat *stat, u32 request_mask, unsigned int query_flags)
837 struct inode *inode = d_inode(path->dentry);
838 struct f2fs_inode_info *fi = F2FS_I(inode);
839 struct f2fs_inode *ri = NULL;
842 if (f2fs_has_extra_attr(inode) &&
843 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
844 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
845 stat->result_mask |= STATX_BTIME;
846 stat->btime.tv_sec = fi->i_crtime.tv_sec;
847 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
851 * Return the DIO alignment restrictions if requested. We only return
852 * this information when requested, since on encrypted files it might
853 * take a fair bit of work to get if the file wasn't opened recently.
855 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
856 * cannot represent that, so in that case we report no DIO support.
858 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
859 unsigned int bsize = i_blocksize(inode);
861 stat->result_mask |= STATX_DIOALIGN;
862 if (!f2fs_force_buffered_io(inode, WRITE)) {
863 stat->dio_mem_align = bsize;
864 stat->dio_offset_align = bsize;
869 if (flags & F2FS_COMPR_FL)
870 stat->attributes |= STATX_ATTR_COMPRESSED;
871 if (flags & F2FS_APPEND_FL)
872 stat->attributes |= STATX_ATTR_APPEND;
873 if (IS_ENCRYPTED(inode))
874 stat->attributes |= STATX_ATTR_ENCRYPTED;
875 if (flags & F2FS_IMMUTABLE_FL)
876 stat->attributes |= STATX_ATTR_IMMUTABLE;
877 if (flags & F2FS_NODUMP_FL)
878 stat->attributes |= STATX_ATTR_NODUMP;
879 if (IS_VERITY(inode))
880 stat->attributes |= STATX_ATTR_VERITY;
882 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
884 STATX_ATTR_ENCRYPTED |
885 STATX_ATTR_IMMUTABLE |
889 generic_fillattr(idmap, request_mask, inode, stat);
891 /* we need to show initial sectors used for inline_data/dentries */
892 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
893 f2fs_has_inline_dentry(inode))
894 stat->blocks += (stat->size + 511) >> 9;
899 #ifdef CONFIG_F2FS_FS_POSIX_ACL
900 static void __setattr_copy(struct mnt_idmap *idmap,
901 struct inode *inode, const struct iattr *attr)
903 unsigned int ia_valid = attr->ia_valid;
905 i_uid_update(idmap, attr, inode);
906 i_gid_update(idmap, attr, inode);
907 if (ia_valid & ATTR_ATIME)
908 inode->i_atime = attr->ia_atime;
909 if (ia_valid & ATTR_MTIME)
910 inode->i_mtime = attr->ia_mtime;
911 if (ia_valid & ATTR_CTIME)
912 inode_set_ctime_to_ts(inode, attr->ia_ctime);
913 if (ia_valid & ATTR_MODE) {
914 umode_t mode = attr->ia_mode;
915 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
917 if (!vfsgid_in_group_p(vfsgid) &&
918 !capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
920 set_acl_inode(inode, mode);
924 #define __setattr_copy setattr_copy
927 int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
930 struct inode *inode = d_inode(dentry);
933 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
936 if (unlikely(IS_IMMUTABLE(inode)))
939 if (unlikely(IS_APPEND(inode) &&
940 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
941 ATTR_GID | ATTR_TIMES_SET))))
944 if ((attr->ia_valid & ATTR_SIZE) &&
945 !f2fs_is_compress_backend_ready(inode))
948 err = setattr_prepare(idmap, dentry, attr);
952 err = fscrypt_prepare_setattr(dentry, attr);
956 err = fsverity_prepare_setattr(dentry, attr);
960 if (is_quota_modification(idmap, inode, attr)) {
961 err = f2fs_dquot_initialize(inode);
965 if (i_uid_needs_update(idmap, attr, inode) ||
966 i_gid_needs_update(idmap, attr, inode)) {
967 f2fs_lock_op(F2FS_I_SB(inode));
968 err = dquot_transfer(idmap, inode, attr);
970 set_sbi_flag(F2FS_I_SB(inode),
971 SBI_QUOTA_NEED_REPAIR);
972 f2fs_unlock_op(F2FS_I_SB(inode));
976 * update uid/gid under lock_op(), so that dquot and inode can
977 * be updated atomically.
979 i_uid_update(idmap, attr, inode);
980 i_gid_update(idmap, attr, inode);
981 f2fs_mark_inode_dirty_sync(inode, true);
982 f2fs_unlock_op(F2FS_I_SB(inode));
985 if (attr->ia_valid & ATTR_SIZE) {
986 loff_t old_size = i_size_read(inode);
988 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
990 * should convert inline inode before i_size_write to
991 * keep smaller than inline_data size with inline flag.
993 err = f2fs_convert_inline_inode(inode);
998 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
999 filemap_invalidate_lock(inode->i_mapping);
1001 truncate_setsize(inode, attr->ia_size);
1003 if (attr->ia_size <= old_size)
1004 err = f2fs_truncate(inode);
1006 * do not trim all blocks after i_size if target size is
1007 * larger than i_size.
1009 filemap_invalidate_unlock(inode->i_mapping);
1010 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1014 spin_lock(&F2FS_I(inode)->i_size_lock);
1015 inode->i_mtime = inode_set_ctime_current(inode);
1016 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1017 spin_unlock(&F2FS_I(inode)->i_size_lock);
1020 __setattr_copy(idmap, inode, attr);
1022 if (attr->ia_valid & ATTR_MODE) {
1023 err = posix_acl_chmod(idmap, dentry, f2fs_get_inode_mode(inode));
1025 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1027 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1028 clear_inode_flag(inode, FI_ACL_MODE);
1032 /* file size may changed here */
1033 f2fs_mark_inode_dirty_sync(inode, true);
1035 /* inode change will produce dirty node pages flushed by checkpoint */
1036 f2fs_balance_fs(F2FS_I_SB(inode), true);
1041 const struct inode_operations f2fs_file_inode_operations = {
1042 .getattr = f2fs_getattr,
1043 .setattr = f2fs_setattr,
1044 .get_inode_acl = f2fs_get_acl,
1045 .set_acl = f2fs_set_acl,
1046 .listxattr = f2fs_listxattr,
1047 .fiemap = f2fs_fiemap,
1048 .fileattr_get = f2fs_fileattr_get,
1049 .fileattr_set = f2fs_fileattr_set,
1052 static int fill_zero(struct inode *inode, pgoff_t index,
1053 loff_t start, loff_t len)
1055 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1061 f2fs_balance_fs(sbi, true);
1064 page = f2fs_get_new_data_page(inode, NULL, index, false);
1065 f2fs_unlock_op(sbi);
1068 return PTR_ERR(page);
1070 f2fs_wait_on_page_writeback(page, DATA, true, true);
1071 zero_user(page, start, len);
1072 set_page_dirty(page);
1073 f2fs_put_page(page, 1);
1077 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1081 while (pg_start < pg_end) {
1082 struct dnode_of_data dn;
1083 pgoff_t end_offset, count;
1085 set_new_dnode(&dn, inode, NULL, NULL, 0);
1086 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1088 if (err == -ENOENT) {
1089 pg_start = f2fs_get_next_page_offset(&dn,
1096 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1097 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1099 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1101 f2fs_truncate_data_blocks_range(&dn, count);
1102 f2fs_put_dnode(&dn);
1109 static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1111 pgoff_t pg_start, pg_end;
1112 loff_t off_start, off_end;
1115 ret = f2fs_convert_inline_inode(inode);
1119 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1120 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1122 off_start = offset & (PAGE_SIZE - 1);
1123 off_end = (offset + len) & (PAGE_SIZE - 1);
1125 if (pg_start == pg_end) {
1126 ret = fill_zero(inode, pg_start, off_start,
1127 off_end - off_start);
1132 ret = fill_zero(inode, pg_start++, off_start,
1133 PAGE_SIZE - off_start);
1138 ret = fill_zero(inode, pg_end, 0, off_end);
1143 if (pg_start < pg_end) {
1144 loff_t blk_start, blk_end;
1145 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1147 f2fs_balance_fs(sbi, true);
1149 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1150 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1152 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1153 filemap_invalidate_lock(inode->i_mapping);
1155 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1158 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1159 f2fs_unlock_op(sbi);
1161 filemap_invalidate_unlock(inode->i_mapping);
1162 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1169 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1170 int *do_replace, pgoff_t off, pgoff_t len)
1172 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1173 struct dnode_of_data dn;
1177 set_new_dnode(&dn, inode, NULL, NULL, 0);
1178 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1179 if (ret && ret != -ENOENT) {
1181 } else if (ret == -ENOENT) {
1182 if (dn.max_level == 0)
1184 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1185 dn.ofs_in_node, len);
1191 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1192 dn.ofs_in_node, len);
1193 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1194 *blkaddr = f2fs_data_blkaddr(&dn);
1196 if (__is_valid_data_blkaddr(*blkaddr) &&
1197 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1198 DATA_GENERIC_ENHANCE)) {
1199 f2fs_put_dnode(&dn);
1200 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1201 return -EFSCORRUPTED;
1204 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1206 if (f2fs_lfs_mode(sbi)) {
1207 f2fs_put_dnode(&dn);
1211 /* do not invalidate this block address */
1212 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1216 f2fs_put_dnode(&dn);
1225 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1226 int *do_replace, pgoff_t off, int len)
1228 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1229 struct dnode_of_data dn;
1232 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1233 if (*do_replace == 0)
1236 set_new_dnode(&dn, inode, NULL, NULL, 0);
1237 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1239 dec_valid_block_count(sbi, inode, 1);
1240 f2fs_invalidate_blocks(sbi, *blkaddr);
1242 f2fs_update_data_blkaddr(&dn, *blkaddr);
1244 f2fs_put_dnode(&dn);
1249 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1250 block_t *blkaddr, int *do_replace,
1251 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1253 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1258 if (blkaddr[i] == NULL_ADDR && !full) {
1263 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1264 struct dnode_of_data dn;
1265 struct node_info ni;
1269 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1270 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1274 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1276 f2fs_put_dnode(&dn);
1280 ilen = min((pgoff_t)
1281 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1282 dn.ofs_in_node, len - i);
1284 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1285 f2fs_truncate_data_blocks_range(&dn, 1);
1287 if (do_replace[i]) {
1288 f2fs_i_blocks_write(src_inode,
1290 f2fs_i_blocks_write(dst_inode,
1292 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1293 blkaddr[i], ni.version, true, false);
1299 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1300 if (dst_inode->i_size < new_size)
1301 f2fs_i_size_write(dst_inode, new_size);
1302 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1304 f2fs_put_dnode(&dn);
1306 struct page *psrc, *pdst;
1308 psrc = f2fs_get_lock_data_page(src_inode,
1311 return PTR_ERR(psrc);
1312 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1315 f2fs_put_page(psrc, 1);
1316 return PTR_ERR(pdst);
1318 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1319 set_page_dirty(pdst);
1320 f2fs_put_page(pdst, 1);
1321 f2fs_put_page(psrc, 1);
1323 ret = f2fs_truncate_hole(src_inode,
1324 src + i, src + i + 1);
1333 static int __exchange_data_block(struct inode *src_inode,
1334 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1335 pgoff_t len, bool full)
1337 block_t *src_blkaddr;
1343 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1345 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1346 array_size(olen, sizeof(block_t)),
1351 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1352 array_size(olen, sizeof(int)),
1355 kvfree(src_blkaddr);
1359 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1360 do_replace, src, olen);
1364 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1365 do_replace, src, dst, olen, full);
1373 kvfree(src_blkaddr);
1379 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1380 kvfree(src_blkaddr);
1385 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1387 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1388 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1389 pgoff_t start = offset >> PAGE_SHIFT;
1390 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1393 f2fs_balance_fs(sbi, true);
1395 /* avoid gc operation during block exchange */
1396 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1397 filemap_invalidate_lock(inode->i_mapping);
1400 f2fs_drop_extent_tree(inode);
1401 truncate_pagecache(inode, offset);
1402 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1403 f2fs_unlock_op(sbi);
1405 filemap_invalidate_unlock(inode->i_mapping);
1406 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1410 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1415 if (offset + len >= i_size_read(inode))
1418 /* collapse range should be aligned to block size of f2fs. */
1419 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1422 ret = f2fs_convert_inline_inode(inode);
1426 /* write out all dirty pages from offset */
1427 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1431 ret = f2fs_do_collapse(inode, offset, len);
1435 /* write out all moved pages, if possible */
1436 filemap_invalidate_lock(inode->i_mapping);
1437 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1438 truncate_pagecache(inode, offset);
1440 new_size = i_size_read(inode) - len;
1441 ret = f2fs_truncate_blocks(inode, new_size, true);
1442 filemap_invalidate_unlock(inode->i_mapping);
1444 f2fs_i_size_write(inode, new_size);
1448 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1451 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1452 pgoff_t index = start;
1453 unsigned int ofs_in_node = dn->ofs_in_node;
1457 for (; index < end; index++, dn->ofs_in_node++) {
1458 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1462 dn->ofs_in_node = ofs_in_node;
1463 ret = f2fs_reserve_new_blocks(dn, count);
1467 dn->ofs_in_node = ofs_in_node;
1468 for (index = start; index < end; index++, dn->ofs_in_node++) {
1469 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1471 * f2fs_reserve_new_blocks will not guarantee entire block
1474 if (dn->data_blkaddr == NULL_ADDR) {
1479 if (dn->data_blkaddr == NEW_ADDR)
1482 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1483 DATA_GENERIC_ENHANCE)) {
1484 ret = -EFSCORRUPTED;
1485 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1489 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1490 dn->data_blkaddr = NEW_ADDR;
1491 f2fs_set_data_blkaddr(dn);
1494 f2fs_update_read_extent_cache_range(dn, start, 0, index - start);
1495 f2fs_update_age_extent_cache_range(dn, start, index - start);
1500 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1503 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1504 struct address_space *mapping = inode->i_mapping;
1505 pgoff_t index, pg_start, pg_end;
1506 loff_t new_size = i_size_read(inode);
1507 loff_t off_start, off_end;
1510 ret = inode_newsize_ok(inode, (len + offset));
1514 ret = f2fs_convert_inline_inode(inode);
1518 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1522 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1523 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1525 off_start = offset & (PAGE_SIZE - 1);
1526 off_end = (offset + len) & (PAGE_SIZE - 1);
1528 if (pg_start == pg_end) {
1529 ret = fill_zero(inode, pg_start, off_start,
1530 off_end - off_start);
1534 new_size = max_t(loff_t, new_size, offset + len);
1537 ret = fill_zero(inode, pg_start++, off_start,
1538 PAGE_SIZE - off_start);
1542 new_size = max_t(loff_t, new_size,
1543 (loff_t)pg_start << PAGE_SHIFT);
1546 for (index = pg_start; index < pg_end;) {
1547 struct dnode_of_data dn;
1548 unsigned int end_offset;
1551 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1552 filemap_invalidate_lock(mapping);
1554 truncate_pagecache_range(inode,
1555 (loff_t)index << PAGE_SHIFT,
1556 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1560 set_new_dnode(&dn, inode, NULL, NULL, 0);
1561 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1563 f2fs_unlock_op(sbi);
1564 filemap_invalidate_unlock(mapping);
1565 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1569 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1570 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1572 ret = f2fs_do_zero_range(&dn, index, end);
1573 f2fs_put_dnode(&dn);
1575 f2fs_unlock_op(sbi);
1576 filemap_invalidate_unlock(mapping);
1577 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1579 f2fs_balance_fs(sbi, dn.node_changed);
1585 new_size = max_t(loff_t, new_size,
1586 (loff_t)index << PAGE_SHIFT);
1590 ret = fill_zero(inode, pg_end, 0, off_end);
1594 new_size = max_t(loff_t, new_size, offset + len);
1599 if (new_size > i_size_read(inode)) {
1600 if (mode & FALLOC_FL_KEEP_SIZE)
1601 file_set_keep_isize(inode);
1603 f2fs_i_size_write(inode, new_size);
1608 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1610 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1611 struct address_space *mapping = inode->i_mapping;
1612 pgoff_t nr, pg_start, pg_end, delta, idx;
1616 new_size = i_size_read(inode) + len;
1617 ret = inode_newsize_ok(inode, new_size);
1621 if (offset >= i_size_read(inode))
1624 /* insert range should be aligned to block size of f2fs. */
1625 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1628 ret = f2fs_convert_inline_inode(inode);
1632 f2fs_balance_fs(sbi, true);
1634 filemap_invalidate_lock(mapping);
1635 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1636 filemap_invalidate_unlock(mapping);
1640 /* write out all dirty pages from offset */
1641 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1645 pg_start = offset >> PAGE_SHIFT;
1646 pg_end = (offset + len) >> PAGE_SHIFT;
1647 delta = pg_end - pg_start;
1648 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1650 /* avoid gc operation during block exchange */
1651 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1652 filemap_invalidate_lock(mapping);
1653 truncate_pagecache(inode, offset);
1655 while (!ret && idx > pg_start) {
1656 nr = idx - pg_start;
1662 f2fs_drop_extent_tree(inode);
1664 ret = __exchange_data_block(inode, inode, idx,
1665 idx + delta, nr, false);
1666 f2fs_unlock_op(sbi);
1668 filemap_invalidate_unlock(mapping);
1669 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1671 /* write out all moved pages, if possible */
1672 filemap_invalidate_lock(mapping);
1673 filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1674 truncate_pagecache(inode, offset);
1675 filemap_invalidate_unlock(mapping);
1678 f2fs_i_size_write(inode, new_size);
1682 static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
1683 loff_t len, int mode)
1685 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1686 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1687 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1688 .m_may_create = true };
1689 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1690 .init_gc_type = FG_GC,
1691 .should_migrate_blocks = false,
1692 .err_gc_skipped = true,
1693 .nr_free_secs = 0 };
1694 pgoff_t pg_start, pg_end;
1697 block_t expanded = 0;
1700 err = inode_newsize_ok(inode, (len + offset));
1704 err = f2fs_convert_inline_inode(inode);
1708 f2fs_balance_fs(sbi, true);
1710 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1711 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1712 off_end = (offset + len) & (PAGE_SIZE - 1);
1714 map.m_lblk = pg_start;
1715 map.m_len = pg_end - pg_start;
1722 if (f2fs_is_pinned_file(inode)) {
1723 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1724 block_t sec_len = roundup(map.m_len, sec_blks);
1726 map.m_len = sec_blks;
1728 if (has_not_enough_free_secs(sbi, 0,
1729 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1730 f2fs_down_write(&sbi->gc_lock);
1731 stat_inc_gc_call_count(sbi, FOREGROUND);
1732 err = f2fs_gc(sbi, &gc_control);
1733 if (err && err != -ENODATA)
1737 f2fs_down_write(&sbi->pin_sem);
1740 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1741 f2fs_unlock_op(sbi);
1743 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1744 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
1745 file_dont_truncate(inode);
1747 f2fs_up_write(&sbi->pin_sem);
1749 expanded += map.m_len;
1750 sec_len -= map.m_len;
1751 map.m_lblk += map.m_len;
1752 if (!err && sec_len)
1755 map.m_len = expanded;
1757 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
1758 expanded = map.m_len;
1767 last_off = pg_start + expanded - 1;
1769 /* update new size to the failed position */
1770 new_size = (last_off == pg_end) ? offset + len :
1771 (loff_t)(last_off + 1) << PAGE_SHIFT;
1773 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1776 if (new_size > i_size_read(inode)) {
1777 if (mode & FALLOC_FL_KEEP_SIZE)
1778 file_set_keep_isize(inode);
1780 f2fs_i_size_write(inode, new_size);
1786 static long f2fs_fallocate(struct file *file, int mode,
1787 loff_t offset, loff_t len)
1789 struct inode *inode = file_inode(file);
1792 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1794 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1796 if (!f2fs_is_compress_backend_ready(inode))
1799 /* f2fs only support ->fallocate for regular file */
1800 if (!S_ISREG(inode->i_mode))
1803 if (IS_ENCRYPTED(inode) &&
1804 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1808 * Pinned file should not support partial truncation since the block
1809 * can be used by applications.
1811 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1812 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1813 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1816 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1817 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1818 FALLOC_FL_INSERT_RANGE))
1823 ret = file_modified(file);
1827 if (mode & FALLOC_FL_PUNCH_HOLE) {
1828 if (offset >= inode->i_size)
1831 ret = f2fs_punch_hole(inode, offset, len);
1832 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1833 ret = f2fs_collapse_range(inode, offset, len);
1834 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1835 ret = f2fs_zero_range(inode, offset, len, mode);
1836 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1837 ret = f2fs_insert_range(inode, offset, len);
1839 ret = f2fs_expand_inode_data(inode, offset, len, mode);
1843 inode->i_mtime = inode_set_ctime_current(inode);
1844 f2fs_mark_inode_dirty_sync(inode, false);
1845 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1849 inode_unlock(inode);
1851 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1855 static int f2fs_release_file(struct inode *inode, struct file *filp)
1858 * f2fs_release_file is called at every close calls. So we should
1859 * not drop any inmemory pages by close called by other process.
1861 if (!(filp->f_mode & FMODE_WRITE) ||
1862 atomic_read(&inode->i_writecount) != 1)
1866 f2fs_abort_atomic_write(inode, true);
1867 inode_unlock(inode);
1872 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1874 struct inode *inode = file_inode(file);
1877 * If the process doing a transaction is crashed, we should do
1878 * roll-back. Otherwise, other reader/write can see corrupted database
1879 * until all the writers close its file. Since this should be done
1880 * before dropping file lock, it needs to do in ->flush.
1882 if (F2FS_I(inode)->atomic_write_task == current &&
1883 (current->flags & PF_EXITING)) {
1885 f2fs_abort_atomic_write(inode, true);
1886 inode_unlock(inode);
1892 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1894 struct f2fs_inode_info *fi = F2FS_I(inode);
1895 u32 masked_flags = fi->i_flags & mask;
1897 /* mask can be shrunk by flags_valid selector */
1900 /* Is it quota file? Do not allow user to mess with it */
1901 if (IS_NOQUOTA(inode))
1904 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1905 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1907 if (!f2fs_empty_dir(inode))
1911 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1912 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1914 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1918 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1919 if (masked_flags & F2FS_COMPR_FL) {
1920 if (!f2fs_disable_compressed_file(inode))
1923 /* try to convert inline_data to support compression */
1924 int err = f2fs_convert_inline_inode(inode);
1928 f2fs_down_write(&F2FS_I(inode)->i_sem);
1929 if (!f2fs_may_compress(inode) ||
1930 (S_ISREG(inode->i_mode) &&
1931 F2FS_HAS_BLOCKS(inode))) {
1932 f2fs_up_write(&F2FS_I(inode)->i_sem);
1935 err = set_compress_context(inode);
1936 f2fs_up_write(&F2FS_I(inode)->i_sem);
1943 fi->i_flags = iflags | (fi->i_flags & ~mask);
1944 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1945 (fi->i_flags & F2FS_NOCOMP_FL));
1947 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1948 set_inode_flag(inode, FI_PROJ_INHERIT);
1950 clear_inode_flag(inode, FI_PROJ_INHERIT);
1952 inode_set_ctime_current(inode);
1953 f2fs_set_inode_flags(inode);
1954 f2fs_mark_inode_dirty_sync(inode, true);
1958 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1961 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1962 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1963 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1964 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1966 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1967 * FS_IOC_FSSETXATTR is done by the VFS.
1970 static const struct {
1973 } f2fs_fsflags_map[] = {
1974 { F2FS_COMPR_FL, FS_COMPR_FL },
1975 { F2FS_SYNC_FL, FS_SYNC_FL },
1976 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1977 { F2FS_APPEND_FL, FS_APPEND_FL },
1978 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1979 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1980 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1981 { F2FS_INDEX_FL, FS_INDEX_FL },
1982 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1983 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1984 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1987 #define F2FS_GETTABLE_FS_FL ( \
1997 FS_PROJINHERIT_FL | \
1999 FS_INLINE_DATA_FL | \
2004 #define F2FS_SETTABLE_FS_FL ( \
2013 FS_PROJINHERIT_FL | \
2016 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2017 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2022 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2023 if (iflags & f2fs_fsflags_map[i].iflag)
2024 fsflags |= f2fs_fsflags_map[i].fsflag;
2029 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2030 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2035 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2036 if (fsflags & f2fs_fsflags_map[i].fsflag)
2037 iflags |= f2fs_fsflags_map[i].iflag;
2042 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2044 struct inode *inode = file_inode(filp);
2046 return put_user(inode->i_generation, (int __user *)arg);
2049 static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
2051 struct inode *inode = file_inode(filp);
2052 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2053 struct f2fs_inode_info *fi = F2FS_I(inode);
2054 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2055 struct inode *pinode;
2059 if (!inode_owner_or_capable(idmap, inode))
2062 if (!S_ISREG(inode->i_mode))
2065 if (filp->f_flags & O_DIRECT)
2068 ret = mnt_want_write_file(filp);
2074 if (!f2fs_disable_compressed_file(inode)) {
2079 if (f2fs_is_atomic_file(inode))
2082 ret = f2fs_convert_inline_inode(inode);
2086 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2089 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2090 * f2fs_is_atomic_file.
2092 if (get_dirty_pages(inode))
2093 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2094 inode->i_ino, get_dirty_pages(inode));
2095 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2097 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2101 /* Check if the inode already has a COW inode */
2102 if (fi->cow_inode == NULL) {
2103 /* Create a COW inode for atomic write */
2104 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2105 if (IS_ERR(pinode)) {
2106 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2107 ret = PTR_ERR(pinode);
2111 ret = f2fs_get_tmpfile(idmap, pinode, &fi->cow_inode);
2114 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2118 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2119 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2121 /* Reuse the already created COW inode */
2122 ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2124 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2129 f2fs_write_inode(inode, NULL);
2131 stat_inc_atomic_inode(inode);
2133 set_inode_flag(inode, FI_ATOMIC_FILE);
2135 isize = i_size_read(inode);
2136 fi->original_i_size = isize;
2138 set_inode_flag(inode, FI_ATOMIC_REPLACE);
2139 truncate_inode_pages_final(inode->i_mapping);
2140 f2fs_i_size_write(inode, 0);
2143 f2fs_i_size_write(fi->cow_inode, isize);
2145 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2147 f2fs_update_time(sbi, REQ_TIME);
2148 fi->atomic_write_task = current;
2149 stat_update_max_atomic_write(inode);
2150 fi->atomic_write_cnt = 0;
2152 inode_unlock(inode);
2153 mnt_drop_write_file(filp);
2157 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2159 struct inode *inode = file_inode(filp);
2160 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2163 if (!inode_owner_or_capable(idmap, inode))
2166 ret = mnt_want_write_file(filp);
2170 f2fs_balance_fs(F2FS_I_SB(inode), true);
2174 if (f2fs_is_atomic_file(inode)) {
2175 ret = f2fs_commit_atomic_write(inode);
2177 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2179 f2fs_abort_atomic_write(inode, ret);
2181 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2184 inode_unlock(inode);
2185 mnt_drop_write_file(filp);
2189 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2191 struct inode *inode = file_inode(filp);
2192 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2195 if (!inode_owner_or_capable(idmap, inode))
2198 ret = mnt_want_write_file(filp);
2204 f2fs_abort_atomic_write(inode, true);
2206 inode_unlock(inode);
2208 mnt_drop_write_file(filp);
2209 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2213 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2215 struct inode *inode = file_inode(filp);
2216 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2217 struct super_block *sb = sbi->sb;
2221 if (!capable(CAP_SYS_ADMIN))
2224 if (get_user(in, (__u32 __user *)arg))
2227 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2228 ret = mnt_want_write_file(filp);
2230 if (ret == -EROFS) {
2232 f2fs_stop_checkpoint(sbi, false,
2233 STOP_CP_REASON_SHUTDOWN);
2234 trace_f2fs_shutdown(sbi, in, ret);
2241 case F2FS_GOING_DOWN_FULLSYNC:
2242 ret = freeze_bdev(sb->s_bdev);
2245 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2246 thaw_bdev(sb->s_bdev);
2248 case F2FS_GOING_DOWN_METASYNC:
2249 /* do checkpoint only */
2250 ret = f2fs_sync_fs(sb, 1);
2253 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2255 case F2FS_GOING_DOWN_NOSYNC:
2256 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2258 case F2FS_GOING_DOWN_METAFLUSH:
2259 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2260 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2262 case F2FS_GOING_DOWN_NEED_FSCK:
2263 set_sbi_flag(sbi, SBI_NEED_FSCK);
2264 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2265 set_sbi_flag(sbi, SBI_IS_DIRTY);
2266 /* do checkpoint only */
2267 ret = f2fs_sync_fs(sb, 1);
2274 f2fs_stop_gc_thread(sbi);
2275 f2fs_stop_discard_thread(sbi);
2277 f2fs_drop_discard_cmd(sbi);
2278 clear_opt(sbi, DISCARD);
2280 f2fs_update_time(sbi, REQ_TIME);
2282 if (in != F2FS_GOING_DOWN_FULLSYNC)
2283 mnt_drop_write_file(filp);
2285 trace_f2fs_shutdown(sbi, in, ret);
2290 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2292 struct inode *inode = file_inode(filp);
2293 struct super_block *sb = inode->i_sb;
2294 struct fstrim_range range;
2297 if (!capable(CAP_SYS_ADMIN))
2300 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2303 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2307 ret = mnt_want_write_file(filp);
2311 range.minlen = max((unsigned int)range.minlen,
2312 bdev_discard_granularity(sb->s_bdev));
2313 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2314 mnt_drop_write_file(filp);
2318 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2321 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2325 static bool uuid_is_nonzero(__u8 u[16])
2329 for (i = 0; i < 16; i++)
2335 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2337 struct inode *inode = file_inode(filp);
2339 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2342 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2344 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2347 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2349 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2351 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2354 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2356 struct inode *inode = file_inode(filp);
2357 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2358 u8 encrypt_pw_salt[16];
2361 if (!f2fs_sb_has_encrypt(sbi))
2364 err = mnt_want_write_file(filp);
2368 f2fs_down_write(&sbi->sb_lock);
2370 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2373 /* update superblock with uuid */
2374 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2376 err = f2fs_commit_super(sbi, false);
2379 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2383 memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2385 f2fs_up_write(&sbi->sb_lock);
2386 mnt_drop_write_file(filp);
2388 if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2394 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2397 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2400 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2403 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2405 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2408 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2411 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2413 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2416 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2419 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2422 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2425 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2428 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2431 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2434 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2437 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2439 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2442 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2445 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2447 struct inode *inode = file_inode(filp);
2448 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2449 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2451 .should_migrate_blocks = false,
2452 .nr_free_secs = 0 };
2456 if (!capable(CAP_SYS_ADMIN))
2459 if (get_user(sync, (__u32 __user *)arg))
2462 if (f2fs_readonly(sbi->sb))
2465 ret = mnt_want_write_file(filp);
2470 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2475 f2fs_down_write(&sbi->gc_lock);
2478 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2479 gc_control.err_gc_skipped = sync;
2480 stat_inc_gc_call_count(sbi, FOREGROUND);
2481 ret = f2fs_gc(sbi, &gc_control);
2483 mnt_drop_write_file(filp);
2487 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2489 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2490 struct f2fs_gc_control gc_control = {
2491 .init_gc_type = range->sync ? FG_GC : BG_GC,
2493 .should_migrate_blocks = false,
2494 .err_gc_skipped = range->sync,
2495 .nr_free_secs = 0 };
2499 if (!capable(CAP_SYS_ADMIN))
2501 if (f2fs_readonly(sbi->sb))
2504 end = range->start + range->len;
2505 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2506 end >= MAX_BLKADDR(sbi))
2509 ret = mnt_want_write_file(filp);
2515 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2520 f2fs_down_write(&sbi->gc_lock);
2523 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2524 stat_inc_gc_call_count(sbi, FOREGROUND);
2525 ret = f2fs_gc(sbi, &gc_control);
2531 range->start += CAP_BLKS_PER_SEC(sbi);
2532 if (range->start <= end)
2535 mnt_drop_write_file(filp);
2539 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2541 struct f2fs_gc_range range;
2543 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2546 return __f2fs_ioc_gc_range(filp, &range);
2549 static int f2fs_ioc_write_checkpoint(struct file *filp)
2551 struct inode *inode = file_inode(filp);
2552 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2555 if (!capable(CAP_SYS_ADMIN))
2558 if (f2fs_readonly(sbi->sb))
2561 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2562 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2566 ret = mnt_want_write_file(filp);
2570 ret = f2fs_sync_fs(sbi->sb, 1);
2572 mnt_drop_write_file(filp);
2576 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2578 struct f2fs_defragment *range)
2580 struct inode *inode = file_inode(filp);
2581 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2582 .m_seg_type = NO_CHECK_TYPE,
2583 .m_may_create = false };
2584 struct extent_info ei = {};
2585 pgoff_t pg_start, pg_end, next_pgofs;
2586 unsigned int blk_per_seg = sbi->blocks_per_seg;
2587 unsigned int total = 0, sec_num;
2588 block_t blk_end = 0;
2589 bool fragmented = false;
2592 pg_start = range->start >> PAGE_SHIFT;
2593 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2595 f2fs_balance_fs(sbi, true);
2599 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
2604 /* if in-place-update policy is enabled, don't waste time here */
2605 set_inode_flag(inode, FI_OPU_WRITE);
2606 if (f2fs_should_update_inplace(inode, NULL)) {
2611 /* writeback all dirty pages in the range */
2612 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2613 range->start + range->len - 1);
2618 * lookup mapping info in extent cache, skip defragmenting if physical
2619 * block addresses are continuous.
2621 if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
2622 if (ei.fofs + ei.len >= pg_end)
2626 map.m_lblk = pg_start;
2627 map.m_next_pgofs = &next_pgofs;
2630 * lookup mapping info in dnode page cache, skip defragmenting if all
2631 * physical block addresses are continuous even if there are hole(s)
2632 * in logical blocks.
2634 while (map.m_lblk < pg_end) {
2635 map.m_len = pg_end - map.m_lblk;
2636 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2640 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2641 map.m_lblk = next_pgofs;
2645 if (blk_end && blk_end != map.m_pblk)
2648 /* record total count of block that we're going to move */
2651 blk_end = map.m_pblk + map.m_len;
2653 map.m_lblk += map.m_len;
2661 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2664 * make sure there are enough free section for LFS allocation, this can
2665 * avoid defragment running in SSR mode when free section are allocated
2668 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2673 map.m_lblk = pg_start;
2674 map.m_len = pg_end - pg_start;
2677 while (map.m_lblk < pg_end) {
2682 map.m_len = pg_end - map.m_lblk;
2683 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2687 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2688 map.m_lblk = next_pgofs;
2692 set_inode_flag(inode, FI_SKIP_WRITES);
2695 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2698 page = f2fs_get_lock_data_page(inode, idx, true);
2700 err = PTR_ERR(page);
2704 set_page_dirty(page);
2705 set_page_private_gcing(page);
2706 f2fs_put_page(page, 1);
2715 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2718 clear_inode_flag(inode, FI_SKIP_WRITES);
2720 err = filemap_fdatawrite(inode->i_mapping);
2725 clear_inode_flag(inode, FI_SKIP_WRITES);
2727 clear_inode_flag(inode, FI_OPU_WRITE);
2729 inode_unlock(inode);
2731 range->len = (u64)total << PAGE_SHIFT;
2735 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2737 struct inode *inode = file_inode(filp);
2738 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2739 struct f2fs_defragment range;
2742 if (!capable(CAP_SYS_ADMIN))
2745 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2748 if (f2fs_readonly(sbi->sb))
2751 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2755 /* verify alignment of offset & size */
2756 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2759 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2760 max_file_blocks(inode)))
2763 err = mnt_want_write_file(filp);
2767 err = f2fs_defragment_range(sbi, filp, &range);
2768 mnt_drop_write_file(filp);
2770 f2fs_update_time(sbi, REQ_TIME);
2774 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2781 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2782 struct file *file_out, loff_t pos_out, size_t len)
2784 struct inode *src = file_inode(file_in);
2785 struct inode *dst = file_inode(file_out);
2786 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2787 size_t olen = len, dst_max_i_size = 0;
2791 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2792 src->i_sb != dst->i_sb)
2795 if (unlikely(f2fs_readonly(src->i_sb)))
2798 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2801 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2804 if (pos_out < 0 || pos_in < 0)
2808 if (pos_in == pos_out)
2810 if (pos_out > pos_in && pos_out < pos_in + len)
2817 if (!inode_trylock(dst))
2821 if (f2fs_compressed_file(src) || f2fs_compressed_file(dst)) {
2827 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2830 olen = len = src->i_size - pos_in;
2831 if (pos_in + len == src->i_size)
2832 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2838 dst_osize = dst->i_size;
2839 if (pos_out + olen > dst->i_size)
2840 dst_max_i_size = pos_out + olen;
2842 /* verify the end result is block aligned */
2843 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2844 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2845 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2848 ret = f2fs_convert_inline_inode(src);
2852 ret = f2fs_convert_inline_inode(dst);
2856 /* write out all dirty pages from offset */
2857 ret = filemap_write_and_wait_range(src->i_mapping,
2858 pos_in, pos_in + len);
2862 ret = filemap_write_and_wait_range(dst->i_mapping,
2863 pos_out, pos_out + len);
2867 f2fs_balance_fs(sbi, true);
2869 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2872 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2877 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2878 pos_out >> F2FS_BLKSIZE_BITS,
2879 len >> F2FS_BLKSIZE_BITS, false);
2883 f2fs_i_size_write(dst, dst_max_i_size);
2884 else if (dst_osize != dst->i_size)
2885 f2fs_i_size_write(dst, dst_osize);
2887 f2fs_unlock_op(sbi);
2890 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2892 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2896 src->i_mtime = inode_set_ctime_current(src);
2897 f2fs_mark_inode_dirty_sync(src, false);
2899 dst->i_mtime = inode_set_ctime_current(dst);
2900 f2fs_mark_inode_dirty_sync(dst, false);
2902 f2fs_update_time(sbi, REQ_TIME);
2912 static int __f2fs_ioc_move_range(struct file *filp,
2913 struct f2fs_move_range *range)
2918 if (!(filp->f_mode & FMODE_READ) ||
2919 !(filp->f_mode & FMODE_WRITE))
2922 dst = fdget(range->dst_fd);
2926 if (!(dst.file->f_mode & FMODE_WRITE)) {
2931 err = mnt_want_write_file(filp);
2935 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2936 range->pos_out, range->len);
2938 mnt_drop_write_file(filp);
2944 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2946 struct f2fs_move_range range;
2948 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2951 return __f2fs_ioc_move_range(filp, &range);
2954 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2956 struct inode *inode = file_inode(filp);
2957 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2958 struct sit_info *sm = SIT_I(sbi);
2959 unsigned int start_segno = 0, end_segno = 0;
2960 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2961 struct f2fs_flush_device range;
2962 struct f2fs_gc_control gc_control = {
2963 .init_gc_type = FG_GC,
2964 .should_migrate_blocks = true,
2965 .err_gc_skipped = true,
2966 .nr_free_secs = 0 };
2969 if (!capable(CAP_SYS_ADMIN))
2972 if (f2fs_readonly(sbi->sb))
2975 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2978 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2982 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2983 __is_large_section(sbi)) {
2984 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2985 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2989 ret = mnt_want_write_file(filp);
2993 if (range.dev_num != 0)
2994 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2995 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2997 start_segno = sm->last_victim[FLUSH_DEVICE];
2998 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2999 start_segno = dev_start_segno;
3000 end_segno = min(start_segno + range.segments, dev_end_segno);
3002 while (start_segno < end_segno) {
3003 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
3007 sm->last_victim[GC_CB] = end_segno + 1;
3008 sm->last_victim[GC_GREEDY] = end_segno + 1;
3009 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
3011 gc_control.victim_segno = start_segno;
3012 stat_inc_gc_call_count(sbi, FOREGROUND);
3013 ret = f2fs_gc(sbi, &gc_control);
3021 mnt_drop_write_file(filp);
3025 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
3027 struct inode *inode = file_inode(filp);
3028 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3030 /* Must validate to set it with SQLite behavior in Android. */
3031 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3033 return put_user(sb_feature, (u32 __user *)arg);
3037 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3039 struct dquot *transfer_to[MAXQUOTAS] = {};
3040 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3041 struct super_block *sb = sbi->sb;
3044 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3045 if (IS_ERR(transfer_to[PRJQUOTA]))
3046 return PTR_ERR(transfer_to[PRJQUOTA]);
3048 err = __dquot_transfer(inode, transfer_to);
3050 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3051 dqput(transfer_to[PRJQUOTA]);
3055 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3057 struct f2fs_inode_info *fi = F2FS_I(inode);
3058 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3059 struct f2fs_inode *ri = NULL;
3063 if (!f2fs_sb_has_project_quota(sbi)) {
3064 if (projid != F2FS_DEF_PROJID)
3070 if (!f2fs_has_extra_attr(inode))
3073 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3075 if (projid_eq(kprojid, fi->i_projid))
3079 /* Is it quota file? Do not allow user to mess with it */
3080 if (IS_NOQUOTA(inode))
3083 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3086 err = f2fs_dquot_initialize(inode);
3091 err = f2fs_transfer_project_quota(inode, kprojid);
3095 fi->i_projid = kprojid;
3096 inode_set_ctime_current(inode);
3097 f2fs_mark_inode_dirty_sync(inode, true);
3099 f2fs_unlock_op(sbi);
3103 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3108 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3110 if (projid != F2FS_DEF_PROJID)
3116 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3118 struct inode *inode = d_inode(dentry);
3119 struct f2fs_inode_info *fi = F2FS_I(inode);
3120 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3122 if (IS_ENCRYPTED(inode))
3123 fsflags |= FS_ENCRYPT_FL;
3124 if (IS_VERITY(inode))
3125 fsflags |= FS_VERITY_FL;
3126 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3127 fsflags |= FS_INLINE_DATA_FL;
3128 if (is_inode_flag_set(inode, FI_PIN_FILE))
3129 fsflags |= FS_NOCOW_FL;
3131 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3133 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3134 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3139 int f2fs_fileattr_set(struct mnt_idmap *idmap,
3140 struct dentry *dentry, struct fileattr *fa)
3142 struct inode *inode = d_inode(dentry);
3143 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3147 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3149 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3151 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3153 fsflags &= F2FS_SETTABLE_FS_FL;
3154 if (!fa->flags_valid)
3155 mask &= FS_COMMON_FL;
3157 iflags = f2fs_fsflags_to_iflags(fsflags);
3158 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3161 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3163 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3168 int f2fs_pin_file_control(struct inode *inode, bool inc)
3170 struct f2fs_inode_info *fi = F2FS_I(inode);
3171 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3173 /* Use i_gc_failures for normal file as a risk signal. */
3175 f2fs_i_gc_failures_write(inode,
3176 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3178 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3179 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3180 __func__, inode->i_ino,
3181 fi->i_gc_failures[GC_FAILURE_PIN]);
3182 clear_inode_flag(inode, FI_PIN_FILE);
3188 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3190 struct inode *inode = file_inode(filp);
3194 if (get_user(pin, (__u32 __user *)arg))
3197 if (!S_ISREG(inode->i_mode))
3200 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3203 ret = mnt_want_write_file(filp);
3210 clear_inode_flag(inode, FI_PIN_FILE);
3211 f2fs_i_gc_failures_write(inode, 0);
3215 if (f2fs_should_update_outplace(inode, NULL)) {
3220 if (f2fs_pin_file_control(inode, false)) {
3225 ret = f2fs_convert_inline_inode(inode);
3229 if (!f2fs_disable_compressed_file(inode)) {
3234 set_inode_flag(inode, FI_PIN_FILE);
3235 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3237 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3239 inode_unlock(inode);
3240 mnt_drop_write_file(filp);
3244 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3246 struct inode *inode = file_inode(filp);
3249 if (is_inode_flag_set(inode, FI_PIN_FILE))
3250 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3251 return put_user(pin, (u32 __user *)arg);
3254 int f2fs_precache_extents(struct inode *inode)
3256 struct f2fs_inode_info *fi = F2FS_I(inode);
3257 struct f2fs_map_blocks map;
3258 pgoff_t m_next_extent;
3262 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3267 map.m_next_pgofs = NULL;
3268 map.m_next_extent = &m_next_extent;
3269 map.m_seg_type = NO_CHECK_TYPE;
3270 map.m_may_create = false;
3271 end = max_file_blocks(inode);
3273 while (map.m_lblk < end) {
3274 map.m_len = end - map.m_lblk;
3276 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3277 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
3278 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3282 map.m_lblk = m_next_extent;
3288 static int f2fs_ioc_precache_extents(struct file *filp)
3290 return f2fs_precache_extents(file_inode(filp));
3293 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3295 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3298 if (!capable(CAP_SYS_ADMIN))
3301 if (f2fs_readonly(sbi->sb))
3304 if (copy_from_user(&block_count, (void __user *)arg,
3305 sizeof(block_count)))
3308 return f2fs_resize_fs(filp, block_count);
3311 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3313 struct inode *inode = file_inode(filp);
3315 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3317 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3318 f2fs_warn(F2FS_I_SB(inode),
3319 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3324 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3327 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3329 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3332 return fsverity_ioctl_measure(filp, (void __user *)arg);
3335 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3337 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3340 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3343 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3345 struct inode *inode = file_inode(filp);
3346 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3351 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3355 f2fs_down_read(&sbi->sb_lock);
3356 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3357 ARRAY_SIZE(sbi->raw_super->volume_name),
3358 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3359 f2fs_up_read(&sbi->sb_lock);
3361 if (copy_to_user((char __user *)arg, vbuf,
3362 min(FSLABEL_MAX, count)))
3369 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3371 struct inode *inode = file_inode(filp);
3372 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3376 if (!capable(CAP_SYS_ADMIN))
3379 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3381 return PTR_ERR(vbuf);
3383 err = mnt_want_write_file(filp);
3387 f2fs_down_write(&sbi->sb_lock);
3389 memset(sbi->raw_super->volume_name, 0,
3390 sizeof(sbi->raw_super->volume_name));
3391 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3392 sbi->raw_super->volume_name,
3393 ARRAY_SIZE(sbi->raw_super->volume_name));
3395 err = f2fs_commit_super(sbi, false);
3397 f2fs_up_write(&sbi->sb_lock);
3399 mnt_drop_write_file(filp);
3405 static int f2fs_get_compress_blocks(struct inode *inode, __u64 *blocks)
3407 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3410 if (!f2fs_compressed_file(inode))
3413 *blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3418 static int f2fs_ioc_get_compress_blocks(struct file *filp, unsigned long arg)
3420 struct inode *inode = file_inode(filp);
3424 ret = f2fs_get_compress_blocks(inode, &blocks);
3428 return put_user(blocks, (u64 __user *)arg);
3431 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3433 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3434 unsigned int released_blocks = 0;
3435 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3439 for (i = 0; i < count; i++) {
3440 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3441 dn->ofs_in_node + i);
3443 if (!__is_valid_data_blkaddr(blkaddr))
3445 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3446 DATA_GENERIC_ENHANCE))) {
3447 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3448 return -EFSCORRUPTED;
3453 int compr_blocks = 0;
3455 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3456 blkaddr = f2fs_data_blkaddr(dn);
3459 if (blkaddr == COMPRESS_ADDR)
3461 dn->ofs_in_node += cluster_size;
3465 if (__is_valid_data_blkaddr(blkaddr))
3468 if (blkaddr != NEW_ADDR)
3471 dn->data_blkaddr = NULL_ADDR;
3472 f2fs_set_data_blkaddr(dn);
3475 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3476 dec_valid_block_count(sbi, dn->inode,
3477 cluster_size - compr_blocks);
3479 released_blocks += cluster_size - compr_blocks;
3481 count -= cluster_size;
3484 return released_blocks;
3487 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3489 struct inode *inode = file_inode(filp);
3490 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3491 pgoff_t page_idx = 0, last_idx;
3492 unsigned int released_blocks = 0;
3496 if (!f2fs_sb_has_compression(sbi))
3499 if (!f2fs_compressed_file(inode))
3502 if (f2fs_readonly(sbi->sb))
3505 ret = mnt_want_write_file(filp);
3509 f2fs_balance_fs(sbi, true);
3513 writecount = atomic_read(&inode->i_writecount);
3514 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3515 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3520 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3525 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3529 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3534 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3535 inode_set_ctime_current(inode);
3536 f2fs_mark_inode_dirty_sync(inode, true);
3538 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3539 filemap_invalidate_lock(inode->i_mapping);
3541 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3543 while (page_idx < last_idx) {
3544 struct dnode_of_data dn;
3545 pgoff_t end_offset, count;
3547 set_new_dnode(&dn, inode, NULL, NULL, 0);
3548 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3550 if (ret == -ENOENT) {
3551 page_idx = f2fs_get_next_page_offset(&dn,
3559 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3560 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3561 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3563 ret = release_compress_blocks(&dn, count);
3565 f2fs_put_dnode(&dn);
3571 released_blocks += ret;
3574 filemap_invalidate_unlock(inode->i_mapping);
3575 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3577 inode_unlock(inode);
3579 mnt_drop_write_file(filp);
3582 ret = put_user(released_blocks, (u64 __user *)arg);
3583 } else if (released_blocks &&
3584 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3585 set_sbi_flag(sbi, SBI_NEED_FSCK);
3586 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3587 "iblocks=%llu, released=%u, compr_blocks=%u, "
3589 __func__, inode->i_ino, inode->i_blocks,
3591 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3597 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3599 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3600 unsigned int reserved_blocks = 0;
3601 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3605 for (i = 0; i < count; i++) {
3606 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3607 dn->ofs_in_node + i);
3609 if (!__is_valid_data_blkaddr(blkaddr))
3611 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3612 DATA_GENERIC_ENHANCE))) {
3613 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3614 return -EFSCORRUPTED;
3619 int compr_blocks = 0;
3623 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3624 blkaddr = f2fs_data_blkaddr(dn);
3627 if (blkaddr == COMPRESS_ADDR)
3629 dn->ofs_in_node += cluster_size;
3633 if (__is_valid_data_blkaddr(blkaddr)) {
3638 dn->data_blkaddr = NEW_ADDR;
3639 f2fs_set_data_blkaddr(dn);
3642 reserved = cluster_size - compr_blocks;
3643 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3647 if (reserved != cluster_size - compr_blocks)
3650 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3652 reserved_blocks += reserved;
3654 count -= cluster_size;
3657 return reserved_blocks;
3660 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3662 struct inode *inode = file_inode(filp);
3663 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3664 pgoff_t page_idx = 0, last_idx;
3665 unsigned int reserved_blocks = 0;
3668 if (!f2fs_sb_has_compression(sbi))
3671 if (!f2fs_compressed_file(inode))
3674 if (f2fs_readonly(sbi->sb))
3677 ret = mnt_want_write_file(filp);
3681 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3684 f2fs_balance_fs(sbi, true);
3688 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3693 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3694 filemap_invalidate_lock(inode->i_mapping);
3696 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3698 while (page_idx < last_idx) {
3699 struct dnode_of_data dn;
3700 pgoff_t end_offset, count;
3702 set_new_dnode(&dn, inode, NULL, NULL, 0);
3703 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3705 if (ret == -ENOENT) {
3706 page_idx = f2fs_get_next_page_offset(&dn,
3714 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3715 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3716 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3718 ret = reserve_compress_blocks(&dn, count);
3720 f2fs_put_dnode(&dn);
3726 reserved_blocks += ret;
3729 filemap_invalidate_unlock(inode->i_mapping);
3730 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3733 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3734 inode_set_ctime_current(inode);
3735 f2fs_mark_inode_dirty_sync(inode, true);
3738 inode_unlock(inode);
3740 mnt_drop_write_file(filp);
3743 ret = put_user(reserved_blocks, (u64 __user *)arg);
3744 } else if (reserved_blocks &&
3745 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3746 set_sbi_flag(sbi, SBI_NEED_FSCK);
3747 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3748 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3750 __func__, inode->i_ino, inode->i_blocks,
3752 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3758 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3759 pgoff_t off, block_t block, block_t len, u32 flags)
3761 sector_t sector = SECTOR_FROM_BLOCK(block);
3762 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3765 if (flags & F2FS_TRIM_FILE_DISCARD) {
3766 if (bdev_max_secure_erase_sectors(bdev))
3767 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3770 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3774 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3775 if (IS_ENCRYPTED(inode))
3776 ret = fscrypt_zeroout_range(inode, off, block, len);
3778 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3785 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3787 struct inode *inode = file_inode(filp);
3788 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3789 struct address_space *mapping = inode->i_mapping;
3790 struct block_device *prev_bdev = NULL;
3791 struct f2fs_sectrim_range range;
3792 pgoff_t index, pg_end, prev_index = 0;
3793 block_t prev_block = 0, len = 0;
3795 bool to_end = false;
3798 if (!(filp->f_mode & FMODE_WRITE))
3801 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3805 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3806 !S_ISREG(inode->i_mode))
3809 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3810 !f2fs_hw_support_discard(sbi)) ||
3811 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3812 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3815 file_start_write(filp);
3818 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3819 range.start >= inode->i_size) {
3827 if (inode->i_size - range.start > range.len) {
3828 end_addr = range.start + range.len;
3830 end_addr = range.len == (u64)-1 ?
3831 sbi->sb->s_maxbytes : inode->i_size;
3835 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3836 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3841 index = F2FS_BYTES_TO_BLK(range.start);
3842 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3844 ret = f2fs_convert_inline_inode(inode);
3848 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3849 filemap_invalidate_lock(mapping);
3851 ret = filemap_write_and_wait_range(mapping, range.start,
3852 to_end ? LLONG_MAX : end_addr - 1);
3856 truncate_inode_pages_range(mapping, range.start,
3857 to_end ? -1 : end_addr - 1);
3859 while (index < pg_end) {
3860 struct dnode_of_data dn;
3861 pgoff_t end_offset, count;
3864 set_new_dnode(&dn, inode, NULL, NULL, 0);
3865 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3867 if (ret == -ENOENT) {
3868 index = f2fs_get_next_page_offset(&dn, index);
3874 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3875 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3876 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3877 struct block_device *cur_bdev;
3878 block_t blkaddr = f2fs_data_blkaddr(&dn);
3880 if (!__is_valid_data_blkaddr(blkaddr))
3883 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3884 DATA_GENERIC_ENHANCE)) {
3885 ret = -EFSCORRUPTED;
3886 f2fs_put_dnode(&dn);
3887 f2fs_handle_error(sbi,
3888 ERROR_INVALID_BLKADDR);
3892 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3893 if (f2fs_is_multi_device(sbi)) {
3894 int di = f2fs_target_device_index(sbi, blkaddr);
3896 blkaddr -= FDEV(di).start_blk;
3900 if (prev_bdev == cur_bdev &&
3901 index == prev_index + len &&
3902 blkaddr == prev_block + len) {
3905 ret = f2fs_secure_erase(prev_bdev,
3906 inode, prev_index, prev_block,
3909 f2fs_put_dnode(&dn);
3918 prev_bdev = cur_bdev;
3920 prev_block = blkaddr;
3925 f2fs_put_dnode(&dn);
3927 if (fatal_signal_pending(current)) {
3935 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3936 prev_block, len, range.flags);
3938 filemap_invalidate_unlock(mapping);
3939 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3941 inode_unlock(inode);
3942 file_end_write(filp);
3947 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3949 struct inode *inode = file_inode(filp);
3950 struct f2fs_comp_option option;
3952 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3955 inode_lock_shared(inode);
3957 if (!f2fs_compressed_file(inode)) {
3958 inode_unlock_shared(inode);
3962 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3963 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3965 inode_unlock_shared(inode);
3967 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3974 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3976 struct inode *inode = file_inode(filp);
3977 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3978 struct f2fs_comp_option option;
3981 if (!f2fs_sb_has_compression(sbi))
3984 if (!(filp->f_mode & FMODE_WRITE))
3987 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3991 if (!f2fs_compressed_file(inode) ||
3992 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3993 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3994 option.algorithm >= COMPRESS_MAX)
3997 file_start_write(filp);
4000 f2fs_down_write(&F2FS_I(inode)->i_sem);
4001 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4006 if (F2FS_HAS_BLOCKS(inode)) {
4011 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
4012 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
4013 F2FS_I(inode)->i_cluster_size = BIT(option.log_cluster_size);
4014 /* Set default level */
4015 if (F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD)
4016 F2FS_I(inode)->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
4018 F2FS_I(inode)->i_compress_level = 0;
4019 /* Adjust mount option level */
4020 if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
4021 F2FS_OPTION(sbi).compress_level)
4022 F2FS_I(inode)->i_compress_level = F2FS_OPTION(sbi).compress_level;
4023 f2fs_mark_inode_dirty_sync(inode, true);
4025 if (!f2fs_is_compress_backend_ready(inode))
4026 f2fs_warn(sbi, "compression algorithm is successfully set, "
4027 "but current kernel doesn't support this algorithm.");
4029 f2fs_up_write(&F2FS_I(inode)->i_sem);
4030 inode_unlock(inode);
4031 file_end_write(filp);
4036 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4038 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
4039 struct address_space *mapping = inode->i_mapping;
4041 pgoff_t redirty_idx = page_idx;
4042 int i, page_len = 0, ret = 0;
4044 page_cache_ra_unbounded(&ractl, len, 0);
4046 for (i = 0; i < len; i++, page_idx++) {
4047 page = read_cache_page(mapping, page_idx, NULL, NULL);
4049 ret = PTR_ERR(page);
4055 for (i = 0; i < page_len; i++, redirty_idx++) {
4056 page = find_lock_page(mapping, redirty_idx);
4058 /* It will never fail, when page has pinned above */
4059 f2fs_bug_on(F2FS_I_SB(inode), !page);
4061 set_page_dirty(page);
4062 f2fs_put_page(page, 1);
4063 f2fs_put_page(page, 0);
4069 static int f2fs_ioc_decompress_file(struct file *filp)
4071 struct inode *inode = file_inode(filp);
4072 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4073 struct f2fs_inode_info *fi = F2FS_I(inode);
4074 pgoff_t page_idx = 0, last_idx;
4075 unsigned int blk_per_seg = sbi->blocks_per_seg;
4076 int cluster_size = fi->i_cluster_size;
4079 if (!f2fs_sb_has_compression(sbi) ||
4080 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4083 if (!(filp->f_mode & FMODE_WRITE))
4086 if (!f2fs_compressed_file(inode))
4089 f2fs_balance_fs(sbi, true);
4091 file_start_write(filp);
4094 if (!f2fs_is_compress_backend_ready(inode)) {
4099 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4104 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4108 if (!atomic_read(&fi->i_compr_blocks))
4111 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4113 count = last_idx - page_idx;
4114 while (count && count >= cluster_size) {
4115 ret = redirty_blocks(inode, page_idx, cluster_size);
4119 if (get_dirty_pages(inode) >= blk_per_seg) {
4120 ret = filemap_fdatawrite(inode->i_mapping);
4125 count -= cluster_size;
4126 page_idx += cluster_size;
4129 if (fatal_signal_pending(current)) {
4136 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4140 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4143 inode_unlock(inode);
4144 file_end_write(filp);
4149 static int f2fs_ioc_compress_file(struct file *filp)
4151 struct inode *inode = file_inode(filp);
4152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4153 pgoff_t page_idx = 0, last_idx;
4154 unsigned int blk_per_seg = sbi->blocks_per_seg;
4155 int cluster_size = F2FS_I(inode)->i_cluster_size;
4158 if (!f2fs_sb_has_compression(sbi) ||
4159 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4162 if (!(filp->f_mode & FMODE_WRITE))
4165 if (!f2fs_compressed_file(inode))
4168 f2fs_balance_fs(sbi, true);
4170 file_start_write(filp);
4173 if (!f2fs_is_compress_backend_ready(inode)) {
4178 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4183 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4187 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4189 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4191 count = last_idx - page_idx;
4192 while (count && count >= cluster_size) {
4193 ret = redirty_blocks(inode, page_idx, cluster_size);
4197 if (get_dirty_pages(inode) >= blk_per_seg) {
4198 ret = filemap_fdatawrite(inode->i_mapping);
4203 count -= cluster_size;
4204 page_idx += cluster_size;
4207 if (fatal_signal_pending(current)) {
4214 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4217 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4220 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4223 inode_unlock(inode);
4224 file_end_write(filp);
4229 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4232 case FS_IOC_GETVERSION:
4233 return f2fs_ioc_getversion(filp, arg);
4234 case F2FS_IOC_START_ATOMIC_WRITE:
4235 return f2fs_ioc_start_atomic_write(filp, false);
4236 case F2FS_IOC_START_ATOMIC_REPLACE:
4237 return f2fs_ioc_start_atomic_write(filp, true);
4238 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4239 return f2fs_ioc_commit_atomic_write(filp);
4240 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4241 return f2fs_ioc_abort_atomic_write(filp);
4242 case F2FS_IOC_START_VOLATILE_WRITE:
4243 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4245 case F2FS_IOC_SHUTDOWN:
4246 return f2fs_ioc_shutdown(filp, arg);
4248 return f2fs_ioc_fitrim(filp, arg);
4249 case FS_IOC_SET_ENCRYPTION_POLICY:
4250 return f2fs_ioc_set_encryption_policy(filp, arg);
4251 case FS_IOC_GET_ENCRYPTION_POLICY:
4252 return f2fs_ioc_get_encryption_policy(filp, arg);
4253 case FS_IOC_GET_ENCRYPTION_PWSALT:
4254 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4255 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4256 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4257 case FS_IOC_ADD_ENCRYPTION_KEY:
4258 return f2fs_ioc_add_encryption_key(filp, arg);
4259 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4260 return f2fs_ioc_remove_encryption_key(filp, arg);
4261 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4262 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4263 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4264 return f2fs_ioc_get_encryption_key_status(filp, arg);
4265 case FS_IOC_GET_ENCRYPTION_NONCE:
4266 return f2fs_ioc_get_encryption_nonce(filp, arg);
4267 case F2FS_IOC_GARBAGE_COLLECT:
4268 return f2fs_ioc_gc(filp, arg);
4269 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4270 return f2fs_ioc_gc_range(filp, arg);
4271 case F2FS_IOC_WRITE_CHECKPOINT:
4272 return f2fs_ioc_write_checkpoint(filp);
4273 case F2FS_IOC_DEFRAGMENT:
4274 return f2fs_ioc_defragment(filp, arg);
4275 case F2FS_IOC_MOVE_RANGE:
4276 return f2fs_ioc_move_range(filp, arg);
4277 case F2FS_IOC_FLUSH_DEVICE:
4278 return f2fs_ioc_flush_device(filp, arg);
4279 case F2FS_IOC_GET_FEATURES:
4280 return f2fs_ioc_get_features(filp, arg);
4281 case F2FS_IOC_GET_PIN_FILE:
4282 return f2fs_ioc_get_pin_file(filp, arg);
4283 case F2FS_IOC_SET_PIN_FILE:
4284 return f2fs_ioc_set_pin_file(filp, arg);
4285 case F2FS_IOC_PRECACHE_EXTENTS:
4286 return f2fs_ioc_precache_extents(filp);
4287 case F2FS_IOC_RESIZE_FS:
4288 return f2fs_ioc_resize_fs(filp, arg);
4289 case FS_IOC_ENABLE_VERITY:
4290 return f2fs_ioc_enable_verity(filp, arg);
4291 case FS_IOC_MEASURE_VERITY:
4292 return f2fs_ioc_measure_verity(filp, arg);
4293 case FS_IOC_READ_VERITY_METADATA:
4294 return f2fs_ioc_read_verity_metadata(filp, arg);
4295 case FS_IOC_GETFSLABEL:
4296 return f2fs_ioc_getfslabel(filp, arg);
4297 case FS_IOC_SETFSLABEL:
4298 return f2fs_ioc_setfslabel(filp, arg);
4299 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4300 return f2fs_ioc_get_compress_blocks(filp, arg);
4301 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4302 return f2fs_release_compress_blocks(filp, arg);
4303 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4304 return f2fs_reserve_compress_blocks(filp, arg);
4305 case F2FS_IOC_SEC_TRIM_FILE:
4306 return f2fs_sec_trim_file(filp, arg);
4307 case F2FS_IOC_GET_COMPRESS_OPTION:
4308 return f2fs_ioc_get_compress_option(filp, arg);
4309 case F2FS_IOC_SET_COMPRESS_OPTION:
4310 return f2fs_ioc_set_compress_option(filp, arg);
4311 case F2FS_IOC_DECOMPRESS_FILE:
4312 return f2fs_ioc_decompress_file(filp);
4313 case F2FS_IOC_COMPRESS_FILE:
4314 return f2fs_ioc_compress_file(filp);
4320 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4322 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4324 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4327 return __f2fs_ioctl(filp, cmd, arg);
4331 * Return %true if the given read or write request should use direct I/O, or
4332 * %false if it should use buffered I/O.
4334 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4335 struct iov_iter *iter)
4339 if (!(iocb->ki_flags & IOCB_DIRECT))
4342 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4346 * Direct I/O not aligned to the disk's logical_block_size will be
4347 * attempted, but will fail with -EINVAL.
4349 * f2fs additionally requires that direct I/O be aligned to the
4350 * filesystem block size, which is often a stricter requirement.
4351 * However, f2fs traditionally falls back to buffered I/O on requests
4352 * that are logical_block_size-aligned but not fs-block aligned.
4354 * The below logic implements this behavior.
4356 align = iocb->ki_pos | iov_iter_alignment(iter);
4357 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4358 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4364 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4367 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4369 dec_page_count(sbi, F2FS_DIO_READ);
4372 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4376 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4377 .end_io = f2fs_dio_read_end_io,
4380 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4382 struct file *file = iocb->ki_filp;
4383 struct inode *inode = file_inode(file);
4384 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4385 struct f2fs_inode_info *fi = F2FS_I(inode);
4386 const loff_t pos = iocb->ki_pos;
4387 const size_t count = iov_iter_count(to);
4388 struct iomap_dio *dio;
4392 return 0; /* skip atime update */
4394 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4396 if (iocb->ki_flags & IOCB_NOWAIT) {
4397 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4402 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4406 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4407 * the higher-level function iomap_dio_rw() in order to ensure that the
4408 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4410 inc_page_count(sbi, F2FS_DIO_READ);
4411 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4412 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4413 if (IS_ERR_OR_NULL(dio)) {
4414 ret = PTR_ERR_OR_ZERO(dio);
4415 if (ret != -EIOCBQUEUED)
4416 dec_page_count(sbi, F2FS_DIO_READ);
4418 ret = iomap_dio_complete(dio);
4421 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4423 file_accessed(file);
4425 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4429 static void f2fs_trace_rw_file_path(struct file *file, loff_t pos, size_t count,
4432 struct inode *inode = file_inode(file);
4435 buf = f2fs_getname(F2FS_I_SB(inode));
4438 path = dentry_path_raw(file_dentry(file), buf, PATH_MAX);
4442 trace_f2fs_datawrite_start(inode, pos, count,
4443 current->pid, path, current->comm);
4445 trace_f2fs_dataread_start(inode, pos, count,
4446 current->pid, path, current->comm);
4451 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4453 struct inode *inode = file_inode(iocb->ki_filp);
4454 const loff_t pos = iocb->ki_pos;
4457 if (!f2fs_is_compress_backend_ready(inode))
4460 if (trace_f2fs_dataread_start_enabled())
4461 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4462 iov_iter_count(to), READ);
4464 if (f2fs_should_use_dio(inode, iocb, to)) {
4465 ret = f2fs_dio_read_iter(iocb, to);
4467 ret = filemap_read(iocb, to, 0);
4469 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4470 APP_BUFFERED_READ_IO, ret);
4472 if (trace_f2fs_dataread_end_enabled())
4473 trace_f2fs_dataread_end(inode, pos, ret);
4477 static ssize_t f2fs_file_splice_read(struct file *in, loff_t *ppos,
4478 struct pipe_inode_info *pipe,
4479 size_t len, unsigned int flags)
4481 struct inode *inode = file_inode(in);
4482 const loff_t pos = *ppos;
4485 if (!f2fs_is_compress_backend_ready(inode))
4488 if (trace_f2fs_dataread_start_enabled())
4489 f2fs_trace_rw_file_path(in, pos, len, READ);
4491 ret = filemap_splice_read(in, ppos, pipe, len, flags);
4493 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4494 APP_BUFFERED_READ_IO, ret);
4496 if (trace_f2fs_dataread_end_enabled())
4497 trace_f2fs_dataread_end(inode, pos, ret);
4501 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4503 struct file *file = iocb->ki_filp;
4504 struct inode *inode = file_inode(file);
4508 if (IS_IMMUTABLE(inode))
4511 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4514 count = generic_write_checks(iocb, from);
4518 err = file_modified(file);
4525 * Preallocate blocks for a write request, if it is possible and helpful to do
4526 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4527 * blocks were preallocated, or a negative errno value if something went
4528 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4529 * requested blocks (not just some of them) have been allocated.
4531 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4534 struct inode *inode = file_inode(iocb->ki_filp);
4535 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4536 const loff_t pos = iocb->ki_pos;
4537 const size_t count = iov_iter_count(iter);
4538 struct f2fs_map_blocks map = {};
4542 /* If it will be an out-of-place direct write, don't bother. */
4543 if (dio && f2fs_lfs_mode(sbi))
4546 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4547 * buffered IO, if DIO meets any holes.
4549 if (dio && i_size_read(inode) &&
4550 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4553 /* No-wait I/O can't allocate blocks. */
4554 if (iocb->ki_flags & IOCB_NOWAIT)
4557 /* If it will be a short write, don't bother. */
4558 if (fault_in_iov_iter_readable(iter, count))
4561 if (f2fs_has_inline_data(inode)) {
4562 /* If the data will fit inline, don't bother. */
4563 if (pos + count <= MAX_INLINE_DATA(inode))
4565 ret = f2fs_convert_inline_inode(inode);
4570 /* Do not preallocate blocks that will be written partially in 4KB. */
4571 map.m_lblk = F2FS_BLK_ALIGN(pos);
4572 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4573 if (map.m_len > map.m_lblk)
4574 map.m_len -= map.m_lblk;
4577 map.m_may_create = true;
4579 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4580 flag = F2FS_GET_BLOCK_PRE_DIO;
4582 map.m_seg_type = NO_CHECK_TYPE;
4583 flag = F2FS_GET_BLOCK_PRE_AIO;
4586 ret = f2fs_map_blocks(inode, &map, flag);
4587 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4588 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4591 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4595 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4596 struct iov_iter *from)
4598 struct file *file = iocb->ki_filp;
4599 struct inode *inode = file_inode(file);
4602 if (iocb->ki_flags & IOCB_NOWAIT)
4605 ret = generic_perform_write(iocb, from);
4608 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4609 APP_BUFFERED_IO, ret);
4614 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4617 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4619 dec_page_count(sbi, F2FS_DIO_WRITE);
4622 f2fs_update_time(sbi, REQ_TIME);
4623 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4627 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4628 .end_io = f2fs_dio_write_end_io,
4631 static void f2fs_flush_buffered_write(struct address_space *mapping,
4632 loff_t start_pos, loff_t end_pos)
4636 ret = filemap_write_and_wait_range(mapping, start_pos, end_pos);
4639 invalidate_mapping_pages(mapping,
4640 start_pos >> PAGE_SHIFT,
4641 end_pos >> PAGE_SHIFT);
4644 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4645 bool *may_need_sync)
4647 struct file *file = iocb->ki_filp;
4648 struct inode *inode = file_inode(file);
4649 struct f2fs_inode_info *fi = F2FS_I(inode);
4650 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4651 const bool do_opu = f2fs_lfs_mode(sbi);
4652 const loff_t pos = iocb->ki_pos;
4653 const ssize_t count = iov_iter_count(from);
4654 unsigned int dio_flags;
4655 struct iomap_dio *dio;
4658 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4660 if (iocb->ki_flags & IOCB_NOWAIT) {
4661 /* f2fs_convert_inline_inode() and block allocation can block */
4662 if (f2fs_has_inline_data(inode) ||
4663 !f2fs_overwrite_io(inode, pos, count)) {
4668 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4672 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4673 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4678 ret = f2fs_convert_inline_inode(inode);
4682 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4684 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4688 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4689 * the higher-level function iomap_dio_rw() in order to ensure that the
4690 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4692 inc_page_count(sbi, F2FS_DIO_WRITE);
4694 if (pos + count > inode->i_size)
4695 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4696 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4697 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4698 if (IS_ERR_OR_NULL(dio)) {
4699 ret = PTR_ERR_OR_ZERO(dio);
4700 if (ret == -ENOTBLK)
4702 if (ret != -EIOCBQUEUED)
4703 dec_page_count(sbi, F2FS_DIO_WRITE);
4705 ret = iomap_dio_complete(dio);
4709 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4710 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4714 if (pos + ret > inode->i_size)
4715 f2fs_i_size_write(inode, pos + ret);
4717 set_inode_flag(inode, FI_UPDATE_WRITE);
4719 if (iov_iter_count(from)) {
4721 loff_t bufio_start_pos = iocb->ki_pos;
4724 * The direct write was partial, so we need to fall back to a
4725 * buffered write for the remainder.
4728 ret2 = f2fs_buffered_write_iter(iocb, from);
4729 if (iov_iter_count(from))
4730 f2fs_write_failed(inode, iocb->ki_pos);
4735 * Ensure that the pagecache pages are written to disk and
4736 * invalidated to preserve the expected O_DIRECT semantics.
4739 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4743 f2fs_flush_buffered_write(file->f_mapping,
4748 /* iomap_dio_rw() already handled the generic_write_sync(). */
4749 *may_need_sync = false;
4752 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4756 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4758 struct inode *inode = file_inode(iocb->ki_filp);
4759 const loff_t orig_pos = iocb->ki_pos;
4760 const size_t orig_count = iov_iter_count(from);
4763 bool may_need_sync = true;
4767 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4772 if (!f2fs_is_compress_backend_ready(inode)) {
4777 if (iocb->ki_flags & IOCB_NOWAIT) {
4778 if (!inode_trylock(inode)) {
4786 ret = f2fs_write_checks(iocb, from);
4790 /* Determine whether we will do a direct write or a buffered write. */
4791 dio = f2fs_should_use_dio(inode, iocb, from);
4793 /* Possibly preallocate the blocks for the write. */
4794 target_size = iocb->ki_pos + iov_iter_count(from);
4795 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4796 if (preallocated < 0) {
4799 if (trace_f2fs_datawrite_start_enabled())
4800 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4803 /* Do the actual write. */
4805 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
4806 f2fs_buffered_write_iter(iocb, from);
4808 if (trace_f2fs_datawrite_end_enabled())
4809 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4812 /* Don't leave any preallocated blocks around past i_size. */
4813 if (preallocated && i_size_read(inode) < target_size) {
4814 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4815 filemap_invalidate_lock(inode->i_mapping);
4816 if (!f2fs_truncate(inode))
4817 file_dont_truncate(inode);
4818 filemap_invalidate_unlock(inode->i_mapping);
4819 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4821 file_dont_truncate(inode);
4824 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4826 inode_unlock(inode);
4828 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4830 if (ret > 0 && may_need_sync)
4831 ret = generic_write_sync(iocb, ret);
4833 /* If buffered IO was forced, flush and drop the data from
4834 * the page cache to preserve O_DIRECT semantics
4836 if (ret > 0 && !dio && (iocb->ki_flags & IOCB_DIRECT))
4837 f2fs_flush_buffered_write(iocb->ki_filp->f_mapping,
4839 orig_pos + ret - 1);
4844 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4847 struct address_space *mapping;
4848 struct backing_dev_info *bdi;
4849 struct inode *inode = file_inode(filp);
4852 if (advice == POSIX_FADV_SEQUENTIAL) {
4853 if (S_ISFIFO(inode->i_mode))
4856 mapping = filp->f_mapping;
4857 if (!mapping || len < 0)
4860 bdi = inode_to_bdi(mapping->host);
4861 filp->f_ra.ra_pages = bdi->ra_pages *
4862 F2FS_I_SB(inode)->seq_file_ra_mul;
4863 spin_lock(&filp->f_lock);
4864 filp->f_mode &= ~FMODE_RANDOM;
4865 spin_unlock(&filp->f_lock);
4869 err = generic_fadvise(filp, offset, len, advice);
4870 if (!err && advice == POSIX_FADV_DONTNEED &&
4871 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4872 f2fs_compressed_file(inode))
4873 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4878 #ifdef CONFIG_COMPAT
4879 struct compat_f2fs_gc_range {
4884 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4885 struct compat_f2fs_gc_range)
4887 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4889 struct compat_f2fs_gc_range __user *urange;
4890 struct f2fs_gc_range range;
4893 urange = compat_ptr(arg);
4894 err = get_user(range.sync, &urange->sync);
4895 err |= get_user(range.start, &urange->start);
4896 err |= get_user(range.len, &urange->len);
4900 return __f2fs_ioc_gc_range(file, &range);
4903 struct compat_f2fs_move_range {
4909 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4910 struct compat_f2fs_move_range)
4912 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4914 struct compat_f2fs_move_range __user *urange;
4915 struct f2fs_move_range range;
4918 urange = compat_ptr(arg);
4919 err = get_user(range.dst_fd, &urange->dst_fd);
4920 err |= get_user(range.pos_in, &urange->pos_in);
4921 err |= get_user(range.pos_out, &urange->pos_out);
4922 err |= get_user(range.len, &urange->len);
4926 return __f2fs_ioc_move_range(file, &range);
4929 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4931 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4933 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4937 case FS_IOC32_GETVERSION:
4938 cmd = FS_IOC_GETVERSION;
4940 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4941 return f2fs_compat_ioc_gc_range(file, arg);
4942 case F2FS_IOC32_MOVE_RANGE:
4943 return f2fs_compat_ioc_move_range(file, arg);
4944 case F2FS_IOC_START_ATOMIC_WRITE:
4945 case F2FS_IOC_START_ATOMIC_REPLACE:
4946 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4947 case F2FS_IOC_START_VOLATILE_WRITE:
4948 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4949 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4950 case F2FS_IOC_SHUTDOWN:
4952 case FS_IOC_SET_ENCRYPTION_POLICY:
4953 case FS_IOC_GET_ENCRYPTION_PWSALT:
4954 case FS_IOC_GET_ENCRYPTION_POLICY:
4955 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4956 case FS_IOC_ADD_ENCRYPTION_KEY:
4957 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4958 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4959 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4960 case FS_IOC_GET_ENCRYPTION_NONCE:
4961 case F2FS_IOC_GARBAGE_COLLECT:
4962 case F2FS_IOC_WRITE_CHECKPOINT:
4963 case F2FS_IOC_DEFRAGMENT:
4964 case F2FS_IOC_FLUSH_DEVICE:
4965 case F2FS_IOC_GET_FEATURES:
4966 case F2FS_IOC_GET_PIN_FILE:
4967 case F2FS_IOC_SET_PIN_FILE:
4968 case F2FS_IOC_PRECACHE_EXTENTS:
4969 case F2FS_IOC_RESIZE_FS:
4970 case FS_IOC_ENABLE_VERITY:
4971 case FS_IOC_MEASURE_VERITY:
4972 case FS_IOC_READ_VERITY_METADATA:
4973 case FS_IOC_GETFSLABEL:
4974 case FS_IOC_SETFSLABEL:
4975 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4976 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4977 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4978 case F2FS_IOC_SEC_TRIM_FILE:
4979 case F2FS_IOC_GET_COMPRESS_OPTION:
4980 case F2FS_IOC_SET_COMPRESS_OPTION:
4981 case F2FS_IOC_DECOMPRESS_FILE:
4982 case F2FS_IOC_COMPRESS_FILE:
4985 return -ENOIOCTLCMD;
4987 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4991 const struct file_operations f2fs_file_operations = {
4992 .llseek = f2fs_llseek,
4993 .read_iter = f2fs_file_read_iter,
4994 .write_iter = f2fs_file_write_iter,
4995 .iopoll = iocb_bio_iopoll,
4996 .open = f2fs_file_open,
4997 .release = f2fs_release_file,
4998 .mmap = f2fs_file_mmap,
4999 .flush = f2fs_file_flush,
5000 .fsync = f2fs_sync_file,
5001 .fallocate = f2fs_fallocate,
5002 .unlocked_ioctl = f2fs_ioctl,
5003 #ifdef CONFIG_COMPAT
5004 .compat_ioctl = f2fs_compat_ioctl,
5006 .splice_read = f2fs_file_splice_read,
5007 .splice_write = iter_file_splice_write,
5008 .fadvise = f2fs_file_fadvise,