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>
32 #include <trace/events/f2fs.h>
34 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
36 struct inode *inode = file_inode(vmf->vma->vm_file);
39 down_read(&F2FS_I(inode)->i_mmap_sem);
40 ret = filemap_fault(vmf);
41 up_read(&F2FS_I(inode)->i_mmap_sem);
43 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
48 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
50 struct page *page = vmf->page;
51 struct inode *inode = file_inode(vmf->vma->vm_file);
52 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
53 struct dnode_of_data dn = { .node_changed = false };
56 if (unlikely(f2fs_cp_error(sbi))) {
61 if (!f2fs_is_checkpoint_ready(sbi)) {
66 sb_start_pagefault(inode->i_sb);
68 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
70 file_update_time(vmf->vma->vm_file);
71 down_read(&F2FS_I(inode)->i_mmap_sem);
73 if (unlikely(page->mapping != inode->i_mapping ||
74 page_offset(page) > i_size_read(inode) ||
75 !PageUptodate(page))) {
81 /* block allocation */
82 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
83 set_new_dnode(&dn, inode, NULL, NULL, 0);
84 err = f2fs_get_block(&dn, page->index);
86 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
93 f2fs_wait_on_page_writeback(page, DATA, false, true);
95 /* wait for GCed page writeback via META_MAPPING */
96 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
99 * check to see if the page is mapped already (no holes)
101 if (PageMappedToDisk(page))
104 /* page is wholly or partially inside EOF */
105 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
106 i_size_read(inode)) {
109 offset = i_size_read(inode) & ~PAGE_MASK;
110 zero_user_segment(page, offset, PAGE_SIZE);
112 set_page_dirty(page);
113 if (!PageUptodate(page))
114 SetPageUptodate(page);
116 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
117 f2fs_update_time(sbi, REQ_TIME);
119 trace_f2fs_vm_page_mkwrite(page, DATA);
121 up_read(&F2FS_I(inode)->i_mmap_sem);
123 f2fs_balance_fs(sbi, dn.node_changed);
125 sb_end_pagefault(inode->i_sb);
127 return block_page_mkwrite_return(err);
130 static const struct vm_operations_struct f2fs_file_vm_ops = {
131 .fault = f2fs_filemap_fault,
132 .map_pages = filemap_map_pages,
133 .page_mkwrite = f2fs_vm_page_mkwrite,
136 static int get_parent_ino(struct inode *inode, nid_t *pino)
138 struct dentry *dentry;
140 inode = igrab(inode);
141 dentry = d_find_any_alias(inode);
146 *pino = parent_ino(dentry);
151 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
153 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
154 enum cp_reason_type cp_reason = CP_NO_NEEDED;
156 if (!S_ISREG(inode->i_mode))
157 cp_reason = CP_NON_REGULAR;
158 else if (inode->i_nlink != 1)
159 cp_reason = CP_HARDLINK;
160 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
161 cp_reason = CP_SB_NEED_CP;
162 else if (file_wrong_pino(inode))
163 cp_reason = CP_WRONG_PINO;
164 else if (!f2fs_space_for_roll_forward(sbi))
165 cp_reason = CP_NO_SPC_ROLL;
166 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
167 cp_reason = CP_NODE_NEED_CP;
168 else if (test_opt(sbi, FASTBOOT))
169 cp_reason = CP_FASTBOOT_MODE;
170 else if (F2FS_OPTION(sbi).active_logs == 2)
171 cp_reason = CP_SPEC_LOG_NUM;
172 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
173 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
174 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
176 cp_reason = CP_RECOVER_DIR;
181 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
183 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
185 /* But we need to avoid that there are some inode updates */
186 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
192 static void try_to_fix_pino(struct inode *inode)
194 struct f2fs_inode_info *fi = F2FS_I(inode);
197 down_write(&fi->i_sem);
198 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
199 get_parent_ino(inode, &pino)) {
200 f2fs_i_pino_write(inode, pino);
201 file_got_pino(inode);
203 up_write(&fi->i_sem);
206 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
207 int datasync, bool atomic)
209 struct inode *inode = file->f_mapping->host;
210 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
211 nid_t ino = inode->i_ino;
213 enum cp_reason_type cp_reason = 0;
214 struct writeback_control wbc = {
215 .sync_mode = WB_SYNC_ALL,
216 .nr_to_write = LONG_MAX,
219 unsigned int seq_id = 0;
221 if (unlikely(f2fs_readonly(inode->i_sb) ||
222 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
225 trace_f2fs_sync_file_enter(inode);
227 if (S_ISDIR(inode->i_mode))
230 /* if fdatasync is triggered, let's do in-place-update */
231 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
232 set_inode_flag(inode, FI_NEED_IPU);
233 ret = file_write_and_wait_range(file, start, end);
234 clear_inode_flag(inode, FI_NEED_IPU);
237 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
241 /* if the inode is dirty, let's recover all the time */
242 if (!f2fs_skip_inode_update(inode, datasync)) {
243 f2fs_write_inode(inode, NULL);
248 * if there is no written data, don't waste time to write recovery info.
250 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
251 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
253 /* it may call write_inode just prior to fsync */
254 if (need_inode_page_update(sbi, ino))
257 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
258 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
264 * Both of fdatasync() and fsync() are able to be recovered from
267 down_read(&F2FS_I(inode)->i_sem);
268 cp_reason = need_do_checkpoint(inode);
269 up_read(&F2FS_I(inode)->i_sem);
272 /* all the dirty node pages should be flushed for POR */
273 ret = f2fs_sync_fs(inode->i_sb, 1);
276 * We've secured consistency through sync_fs. Following pino
277 * will be used only for fsynced inodes after checkpoint.
279 try_to_fix_pino(inode);
280 clear_inode_flag(inode, FI_APPEND_WRITE);
281 clear_inode_flag(inode, FI_UPDATE_WRITE);
285 atomic_inc(&sbi->wb_sync_req[NODE]);
286 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
287 atomic_dec(&sbi->wb_sync_req[NODE]);
291 /* if cp_error was enabled, we should avoid infinite loop */
292 if (unlikely(f2fs_cp_error(sbi))) {
297 if (f2fs_need_inode_block_update(sbi, ino)) {
298 f2fs_mark_inode_dirty_sync(inode, true);
299 f2fs_write_inode(inode, NULL);
304 * If it's atomic_write, it's just fine to keep write ordering. So
305 * here we don't need to wait for node write completion, since we use
306 * node chain which serializes node blocks. If one of node writes are
307 * reordered, we can see simply broken chain, resulting in stopping
308 * roll-forward recovery. It means we'll recover all or none node blocks
312 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
317 /* once recovery info is written, don't need to tack this */
318 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
319 clear_inode_flag(inode, FI_APPEND_WRITE);
321 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
322 ret = f2fs_issue_flush(sbi, inode->i_ino);
324 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
325 clear_inode_flag(inode, FI_UPDATE_WRITE);
326 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
328 f2fs_update_time(sbi, REQ_TIME);
330 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
331 f2fs_trace_ios(NULL, 1);
335 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
337 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
339 return f2fs_do_sync_file(file, start, end, datasync, false);
342 static pgoff_t __get_first_dirty_index(struct address_space *mapping,
343 pgoff_t pgofs, int whence)
348 if (whence != SEEK_DATA)
351 /* find first dirty page index */
352 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
361 static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
362 pgoff_t dirty, pgoff_t pgofs, int whence)
366 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
367 __is_valid_data_blkaddr(blkaddr))
371 if (blkaddr == NULL_ADDR)
378 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
380 struct inode *inode = file->f_mapping->host;
381 loff_t maxbytes = inode->i_sb->s_maxbytes;
382 struct dnode_of_data dn;
383 pgoff_t pgofs, end_offset, dirty;
384 loff_t data_ofs = offset;
390 isize = i_size_read(inode);
394 /* handle inline data case */
395 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
396 if (whence == SEEK_HOLE)
401 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
403 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
405 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
406 set_new_dnode(&dn, inode, NULL, NULL, 0);
407 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
408 if (err && err != -ENOENT) {
410 } else if (err == -ENOENT) {
411 /* direct node does not exists */
412 if (whence == SEEK_DATA) {
413 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
420 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
422 /* find data/hole in dnode block */
423 for (; dn.ofs_in_node < end_offset;
424 dn.ofs_in_node++, pgofs++,
425 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
428 blkaddr = datablock_addr(dn.inode,
429 dn.node_page, dn.ofs_in_node);
431 if (__is_valid_data_blkaddr(blkaddr) &&
432 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
433 blkaddr, DATA_GENERIC_ENHANCE)) {
438 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
447 if (whence == SEEK_DATA)
450 if (whence == SEEK_HOLE && data_ofs > isize)
453 return vfs_setpos(file, data_ofs, maxbytes);
459 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
461 struct inode *inode = file->f_mapping->host;
462 loff_t maxbytes = inode->i_sb->s_maxbytes;
468 return generic_file_llseek_size(file, offset, whence,
469 maxbytes, i_size_read(inode));
474 return f2fs_seek_block(file, offset, whence);
480 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
482 struct inode *inode = file_inode(file);
485 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
488 /* we don't need to use inline_data strictly */
489 err = f2fs_convert_inline_inode(inode);
494 vma->vm_ops = &f2fs_file_vm_ops;
498 static int f2fs_file_open(struct inode *inode, struct file *filp)
500 int err = fscrypt_file_open(inode, filp);
505 err = fsverity_file_open(inode, filp);
509 filp->f_mode |= FMODE_NOWAIT;
511 return dquot_file_open(inode, filp);
514 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
516 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
517 struct f2fs_node *raw_node;
518 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
522 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
523 base = get_extra_isize(dn->inode);
525 raw_node = F2FS_NODE(dn->node_page);
526 addr = blkaddr_in_node(raw_node) + base + ofs;
528 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
529 block_t blkaddr = le32_to_cpu(*addr);
531 if (blkaddr == NULL_ADDR)
534 dn->data_blkaddr = NULL_ADDR;
535 f2fs_set_data_blkaddr(dn);
537 if (__is_valid_data_blkaddr(blkaddr) &&
538 !f2fs_is_valid_blkaddr(sbi, blkaddr,
539 DATA_GENERIC_ENHANCE))
542 f2fs_invalidate_blocks(sbi, blkaddr);
543 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
544 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
551 * once we invalidate valid blkaddr in range [ofs, ofs + count],
552 * we will invalidate all blkaddr in the whole range.
554 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
556 f2fs_update_extent_cache_range(dn, fofs, 0, len);
557 dec_valid_block_count(sbi, dn->inode, nr_free);
559 dn->ofs_in_node = ofs;
561 f2fs_update_time(sbi, REQ_TIME);
562 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
563 dn->ofs_in_node, nr_free);
566 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
568 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
571 static int truncate_partial_data_page(struct inode *inode, u64 from,
574 loff_t offset = from & (PAGE_SIZE - 1);
575 pgoff_t index = from >> PAGE_SHIFT;
576 struct address_space *mapping = inode->i_mapping;
579 if (!offset && !cache_only)
583 page = find_lock_page(mapping, index);
584 if (page && PageUptodate(page))
586 f2fs_put_page(page, 1);
590 page = f2fs_get_lock_data_page(inode, index, true);
592 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
594 f2fs_wait_on_page_writeback(page, DATA, true, true);
595 zero_user(page, offset, PAGE_SIZE - offset);
597 /* An encrypted inode should have a key and truncate the last page. */
598 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
600 set_page_dirty(page);
601 f2fs_put_page(page, 1);
605 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
607 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
608 struct dnode_of_data dn;
610 int count = 0, err = 0;
612 bool truncate_page = false;
614 trace_f2fs_truncate_blocks_enter(inode, from);
616 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
618 if (free_from >= sbi->max_file_blocks)
624 ipage = f2fs_get_node_page(sbi, inode->i_ino);
626 err = PTR_ERR(ipage);
630 if (f2fs_has_inline_data(inode)) {
631 f2fs_truncate_inline_inode(inode, ipage, from);
632 f2fs_put_page(ipage, 1);
633 truncate_page = true;
637 set_new_dnode(&dn, inode, ipage, NULL, 0);
638 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
645 count = ADDRS_PER_PAGE(dn.node_page, inode);
647 count -= dn.ofs_in_node;
648 f2fs_bug_on(sbi, count < 0);
650 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
651 f2fs_truncate_data_blocks_range(&dn, count);
657 err = f2fs_truncate_inode_blocks(inode, free_from);
662 /* lastly zero out the first data page */
664 err = truncate_partial_data_page(inode, from, truncate_page);
666 trace_f2fs_truncate_blocks_exit(inode, err);
670 int f2fs_truncate(struct inode *inode)
674 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
677 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
678 S_ISLNK(inode->i_mode)))
681 trace_f2fs_truncate(inode);
683 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
684 f2fs_show_injection_info(FAULT_TRUNCATE);
688 /* we should check inline_data size */
689 if (!f2fs_may_inline_data(inode)) {
690 err = f2fs_convert_inline_inode(inode);
695 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
699 inode->i_mtime = inode->i_ctime = current_time(inode);
700 f2fs_mark_inode_dirty_sync(inode, false);
704 int f2fs_getattr(const struct path *path, struct kstat *stat,
705 u32 request_mask, unsigned int query_flags)
707 struct inode *inode = d_inode(path->dentry);
708 struct f2fs_inode_info *fi = F2FS_I(inode);
709 struct f2fs_inode *ri;
712 if (f2fs_has_extra_attr(inode) &&
713 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
714 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
715 stat->result_mask |= STATX_BTIME;
716 stat->btime.tv_sec = fi->i_crtime.tv_sec;
717 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
721 if (flags & F2FS_APPEND_FL)
722 stat->attributes |= STATX_ATTR_APPEND;
723 if (IS_ENCRYPTED(inode))
724 stat->attributes |= STATX_ATTR_ENCRYPTED;
725 if (flags & F2FS_IMMUTABLE_FL)
726 stat->attributes |= STATX_ATTR_IMMUTABLE;
727 if (flags & F2FS_NODUMP_FL)
728 stat->attributes |= STATX_ATTR_NODUMP;
730 stat->attributes_mask |= (STATX_ATTR_APPEND |
731 STATX_ATTR_ENCRYPTED |
732 STATX_ATTR_IMMUTABLE |
735 generic_fillattr(inode, stat);
737 /* we need to show initial sectors used for inline_data/dentries */
738 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
739 f2fs_has_inline_dentry(inode))
740 stat->blocks += (stat->size + 511) >> 9;
745 #ifdef CONFIG_F2FS_FS_POSIX_ACL
746 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
748 unsigned int ia_valid = attr->ia_valid;
750 if (ia_valid & ATTR_UID)
751 inode->i_uid = attr->ia_uid;
752 if (ia_valid & ATTR_GID)
753 inode->i_gid = attr->ia_gid;
754 if (ia_valid & ATTR_ATIME) {
755 inode->i_atime = timestamp_truncate(attr->ia_atime,
758 if (ia_valid & ATTR_MTIME) {
759 inode->i_mtime = timestamp_truncate(attr->ia_mtime,
762 if (ia_valid & ATTR_CTIME) {
763 inode->i_ctime = timestamp_truncate(attr->ia_ctime,
766 if (ia_valid & ATTR_MODE) {
767 umode_t mode = attr->ia_mode;
769 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
771 set_acl_inode(inode, mode);
775 #define __setattr_copy setattr_copy
778 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
780 struct inode *inode = d_inode(dentry);
783 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
786 err = setattr_prepare(dentry, attr);
790 err = fscrypt_prepare_setattr(dentry, attr);
794 err = fsverity_prepare_setattr(dentry, attr);
798 if (is_quota_modification(inode, attr)) {
799 err = dquot_initialize(inode);
803 if ((attr->ia_valid & ATTR_UID &&
804 !uid_eq(attr->ia_uid, inode->i_uid)) ||
805 (attr->ia_valid & ATTR_GID &&
806 !gid_eq(attr->ia_gid, inode->i_gid))) {
807 f2fs_lock_op(F2FS_I_SB(inode));
808 err = dquot_transfer(inode, attr);
810 set_sbi_flag(F2FS_I_SB(inode),
811 SBI_QUOTA_NEED_REPAIR);
812 f2fs_unlock_op(F2FS_I_SB(inode));
816 * update uid/gid under lock_op(), so that dquot and inode can
817 * be updated atomically.
819 if (attr->ia_valid & ATTR_UID)
820 inode->i_uid = attr->ia_uid;
821 if (attr->ia_valid & ATTR_GID)
822 inode->i_gid = attr->ia_gid;
823 f2fs_mark_inode_dirty_sync(inode, true);
824 f2fs_unlock_op(F2FS_I_SB(inode));
827 if (attr->ia_valid & ATTR_SIZE) {
828 loff_t old_size = i_size_read(inode);
830 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
832 * should convert inline inode before i_size_write to
833 * keep smaller than inline_data size with inline flag.
835 err = f2fs_convert_inline_inode(inode);
840 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
841 down_write(&F2FS_I(inode)->i_mmap_sem);
843 truncate_setsize(inode, attr->ia_size);
845 if (attr->ia_size <= old_size)
846 err = f2fs_truncate(inode);
848 * do not trim all blocks after i_size if target size is
849 * larger than i_size.
851 up_write(&F2FS_I(inode)->i_mmap_sem);
852 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
856 down_write(&F2FS_I(inode)->i_sem);
857 inode->i_mtime = inode->i_ctime = current_time(inode);
858 F2FS_I(inode)->last_disk_size = i_size_read(inode);
859 up_write(&F2FS_I(inode)->i_sem);
862 __setattr_copy(inode, attr);
864 if (attr->ia_valid & ATTR_MODE) {
865 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
866 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
867 inode->i_mode = F2FS_I(inode)->i_acl_mode;
868 clear_inode_flag(inode, FI_ACL_MODE);
872 /* file size may changed here */
873 f2fs_mark_inode_dirty_sync(inode, true);
875 /* inode change will produce dirty node pages flushed by checkpoint */
876 f2fs_balance_fs(F2FS_I_SB(inode), true);
881 const struct inode_operations f2fs_file_inode_operations = {
882 .getattr = f2fs_getattr,
883 .setattr = f2fs_setattr,
884 .get_acl = f2fs_get_acl,
885 .set_acl = f2fs_set_acl,
886 #ifdef CONFIG_F2FS_FS_XATTR
887 .listxattr = f2fs_listxattr,
889 .fiemap = f2fs_fiemap,
892 static int fill_zero(struct inode *inode, pgoff_t index,
893 loff_t start, loff_t len)
895 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
901 f2fs_balance_fs(sbi, true);
904 page = f2fs_get_new_data_page(inode, NULL, index, false);
908 return PTR_ERR(page);
910 f2fs_wait_on_page_writeback(page, DATA, true, true);
911 zero_user(page, start, len);
912 set_page_dirty(page);
913 f2fs_put_page(page, 1);
917 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
921 while (pg_start < pg_end) {
922 struct dnode_of_data dn;
923 pgoff_t end_offset, count;
925 set_new_dnode(&dn, inode, NULL, NULL, 0);
926 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
928 if (err == -ENOENT) {
929 pg_start = f2fs_get_next_page_offset(&dn,
936 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
937 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
939 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
941 f2fs_truncate_data_blocks_range(&dn, count);
949 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
951 pgoff_t pg_start, pg_end;
952 loff_t off_start, off_end;
955 ret = f2fs_convert_inline_inode(inode);
959 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
960 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
962 off_start = offset & (PAGE_SIZE - 1);
963 off_end = (offset + len) & (PAGE_SIZE - 1);
965 if (pg_start == pg_end) {
966 ret = fill_zero(inode, pg_start, off_start,
967 off_end - off_start);
972 ret = fill_zero(inode, pg_start++, off_start,
973 PAGE_SIZE - off_start);
978 ret = fill_zero(inode, pg_end, 0, off_end);
983 if (pg_start < pg_end) {
984 struct address_space *mapping = inode->i_mapping;
985 loff_t blk_start, blk_end;
986 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
988 f2fs_balance_fs(sbi, true);
990 blk_start = (loff_t)pg_start << PAGE_SHIFT;
991 blk_end = (loff_t)pg_end << PAGE_SHIFT;
993 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
994 down_write(&F2FS_I(inode)->i_mmap_sem);
996 truncate_inode_pages_range(mapping, blk_start,
1000 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1001 f2fs_unlock_op(sbi);
1003 up_write(&F2FS_I(inode)->i_mmap_sem);
1004 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1011 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1012 int *do_replace, pgoff_t off, pgoff_t len)
1014 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1015 struct dnode_of_data dn;
1019 set_new_dnode(&dn, inode, NULL, NULL, 0);
1020 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1021 if (ret && ret != -ENOENT) {
1023 } else if (ret == -ENOENT) {
1024 if (dn.max_level == 0)
1026 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) - dn.ofs_in_node,
1033 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1034 dn.ofs_in_node, len);
1035 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1036 *blkaddr = datablock_addr(dn.inode,
1037 dn.node_page, dn.ofs_in_node);
1039 if (__is_valid_data_blkaddr(*blkaddr) &&
1040 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1041 DATA_GENERIC_ENHANCE)) {
1042 f2fs_put_dnode(&dn);
1043 return -EFSCORRUPTED;
1046 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1048 if (test_opt(sbi, LFS)) {
1049 f2fs_put_dnode(&dn);
1053 /* do not invalidate this block address */
1054 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1058 f2fs_put_dnode(&dn);
1067 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1068 int *do_replace, pgoff_t off, int len)
1070 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1071 struct dnode_of_data dn;
1074 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1075 if (*do_replace == 0)
1078 set_new_dnode(&dn, inode, NULL, NULL, 0);
1079 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1081 dec_valid_block_count(sbi, inode, 1);
1082 f2fs_invalidate_blocks(sbi, *blkaddr);
1084 f2fs_update_data_blkaddr(&dn, *blkaddr);
1086 f2fs_put_dnode(&dn);
1091 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1092 block_t *blkaddr, int *do_replace,
1093 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1095 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1100 if (blkaddr[i] == NULL_ADDR && !full) {
1105 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1106 struct dnode_of_data dn;
1107 struct node_info ni;
1111 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1112 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1116 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1118 f2fs_put_dnode(&dn);
1122 ilen = min((pgoff_t)
1123 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1124 dn.ofs_in_node, len - i);
1126 dn.data_blkaddr = datablock_addr(dn.inode,
1127 dn.node_page, dn.ofs_in_node);
1128 f2fs_truncate_data_blocks_range(&dn, 1);
1130 if (do_replace[i]) {
1131 f2fs_i_blocks_write(src_inode,
1133 f2fs_i_blocks_write(dst_inode,
1135 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1136 blkaddr[i], ni.version, true, false);
1142 new_size = (dst + i) << PAGE_SHIFT;
1143 if (dst_inode->i_size < new_size)
1144 f2fs_i_size_write(dst_inode, new_size);
1145 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1147 f2fs_put_dnode(&dn);
1149 struct page *psrc, *pdst;
1151 psrc = f2fs_get_lock_data_page(src_inode,
1154 return PTR_ERR(psrc);
1155 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1158 f2fs_put_page(psrc, 1);
1159 return PTR_ERR(pdst);
1161 f2fs_copy_page(psrc, pdst);
1162 set_page_dirty(pdst);
1163 f2fs_put_page(pdst, 1);
1164 f2fs_put_page(psrc, 1);
1166 ret = f2fs_truncate_hole(src_inode,
1167 src + i, src + i + 1);
1176 static int __exchange_data_block(struct inode *src_inode,
1177 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1178 pgoff_t len, bool full)
1180 block_t *src_blkaddr;
1186 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1188 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1189 array_size(olen, sizeof(block_t)),
1194 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1195 array_size(olen, sizeof(int)),
1198 kvfree(src_blkaddr);
1202 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1203 do_replace, src, olen);
1207 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1208 do_replace, src, dst, olen, full);
1216 kvfree(src_blkaddr);
1222 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1223 kvfree(src_blkaddr);
1228 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1230 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1231 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1232 pgoff_t start = offset >> PAGE_SHIFT;
1233 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1236 f2fs_balance_fs(sbi, true);
1238 /* avoid gc operation during block exchange */
1239 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1240 down_write(&F2FS_I(inode)->i_mmap_sem);
1243 f2fs_drop_extent_tree(inode);
1244 truncate_pagecache(inode, offset);
1245 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1246 f2fs_unlock_op(sbi);
1248 up_write(&F2FS_I(inode)->i_mmap_sem);
1249 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1253 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1258 if (offset + len >= i_size_read(inode))
1261 /* collapse range should be aligned to block size of f2fs. */
1262 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1265 ret = f2fs_convert_inline_inode(inode);
1269 /* write out all dirty pages from offset */
1270 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1274 ret = f2fs_do_collapse(inode, offset, len);
1278 /* write out all moved pages, if possible */
1279 down_write(&F2FS_I(inode)->i_mmap_sem);
1280 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1281 truncate_pagecache(inode, offset);
1283 new_size = i_size_read(inode) - len;
1284 truncate_pagecache(inode, new_size);
1286 ret = f2fs_truncate_blocks(inode, new_size, true);
1287 up_write(&F2FS_I(inode)->i_mmap_sem);
1289 f2fs_i_size_write(inode, new_size);
1293 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1296 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1297 pgoff_t index = start;
1298 unsigned int ofs_in_node = dn->ofs_in_node;
1302 for (; index < end; index++, dn->ofs_in_node++) {
1303 if (datablock_addr(dn->inode, dn->node_page,
1304 dn->ofs_in_node) == NULL_ADDR)
1308 dn->ofs_in_node = ofs_in_node;
1309 ret = f2fs_reserve_new_blocks(dn, count);
1313 dn->ofs_in_node = ofs_in_node;
1314 for (index = start; index < end; index++, dn->ofs_in_node++) {
1315 dn->data_blkaddr = datablock_addr(dn->inode,
1316 dn->node_page, dn->ofs_in_node);
1318 * f2fs_reserve_new_blocks will not guarantee entire block
1321 if (dn->data_blkaddr == NULL_ADDR) {
1325 if (dn->data_blkaddr != NEW_ADDR) {
1326 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1327 dn->data_blkaddr = NEW_ADDR;
1328 f2fs_set_data_blkaddr(dn);
1332 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1337 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1340 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1341 struct address_space *mapping = inode->i_mapping;
1342 pgoff_t index, pg_start, pg_end;
1343 loff_t new_size = i_size_read(inode);
1344 loff_t off_start, off_end;
1347 ret = inode_newsize_ok(inode, (len + offset));
1351 ret = f2fs_convert_inline_inode(inode);
1355 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1359 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1360 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1362 off_start = offset & (PAGE_SIZE - 1);
1363 off_end = (offset + len) & (PAGE_SIZE - 1);
1365 if (pg_start == pg_end) {
1366 ret = fill_zero(inode, pg_start, off_start,
1367 off_end - off_start);
1371 new_size = max_t(loff_t, new_size, offset + len);
1374 ret = fill_zero(inode, pg_start++, off_start,
1375 PAGE_SIZE - off_start);
1379 new_size = max_t(loff_t, new_size,
1380 (loff_t)pg_start << PAGE_SHIFT);
1383 for (index = pg_start; index < pg_end;) {
1384 struct dnode_of_data dn;
1385 unsigned int end_offset;
1388 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1389 down_write(&F2FS_I(inode)->i_mmap_sem);
1391 truncate_pagecache_range(inode,
1392 (loff_t)index << PAGE_SHIFT,
1393 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1397 set_new_dnode(&dn, inode, NULL, NULL, 0);
1398 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1400 f2fs_unlock_op(sbi);
1401 up_write(&F2FS_I(inode)->i_mmap_sem);
1402 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1406 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1407 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1409 ret = f2fs_do_zero_range(&dn, index, end);
1410 f2fs_put_dnode(&dn);
1412 f2fs_unlock_op(sbi);
1413 up_write(&F2FS_I(inode)->i_mmap_sem);
1414 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1416 f2fs_balance_fs(sbi, dn.node_changed);
1422 new_size = max_t(loff_t, new_size,
1423 (loff_t)index << PAGE_SHIFT);
1427 ret = fill_zero(inode, pg_end, 0, off_end);
1431 new_size = max_t(loff_t, new_size, offset + len);
1436 if (new_size > i_size_read(inode)) {
1437 if (mode & FALLOC_FL_KEEP_SIZE)
1438 file_set_keep_isize(inode);
1440 f2fs_i_size_write(inode, new_size);
1445 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1447 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1448 pgoff_t nr, pg_start, pg_end, delta, idx;
1452 new_size = i_size_read(inode) + len;
1453 ret = inode_newsize_ok(inode, new_size);
1457 if (offset >= i_size_read(inode))
1460 /* insert range should be aligned to block size of f2fs. */
1461 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1464 ret = f2fs_convert_inline_inode(inode);
1468 f2fs_balance_fs(sbi, true);
1470 down_write(&F2FS_I(inode)->i_mmap_sem);
1471 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1472 up_write(&F2FS_I(inode)->i_mmap_sem);
1476 /* write out all dirty pages from offset */
1477 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1481 pg_start = offset >> PAGE_SHIFT;
1482 pg_end = (offset + len) >> PAGE_SHIFT;
1483 delta = pg_end - pg_start;
1484 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1486 /* avoid gc operation during block exchange */
1487 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1488 down_write(&F2FS_I(inode)->i_mmap_sem);
1489 truncate_pagecache(inode, offset);
1491 while (!ret && idx > pg_start) {
1492 nr = idx - pg_start;
1498 f2fs_drop_extent_tree(inode);
1500 ret = __exchange_data_block(inode, inode, idx,
1501 idx + delta, nr, false);
1502 f2fs_unlock_op(sbi);
1504 up_write(&F2FS_I(inode)->i_mmap_sem);
1505 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1507 /* write out all moved pages, if possible */
1508 down_write(&F2FS_I(inode)->i_mmap_sem);
1509 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1510 truncate_pagecache(inode, offset);
1511 up_write(&F2FS_I(inode)->i_mmap_sem);
1514 f2fs_i_size_write(inode, new_size);
1518 static int expand_inode_data(struct inode *inode, loff_t offset,
1519 loff_t len, int mode)
1521 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1522 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1523 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1524 .m_may_create = true };
1526 loff_t new_size = i_size_read(inode);
1530 err = inode_newsize_ok(inode, (len + offset));
1534 err = f2fs_convert_inline_inode(inode);
1538 f2fs_balance_fs(sbi, true);
1540 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1541 off_end = (offset + len) & (PAGE_SIZE - 1);
1543 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1544 map.m_len = pg_end - map.m_lblk;
1548 if (f2fs_is_pinned_file(inode))
1549 map.m_seg_type = CURSEG_COLD_DATA;
1551 err = f2fs_map_blocks(inode, &map, 1, (f2fs_is_pinned_file(inode) ?
1552 F2FS_GET_BLOCK_PRE_DIO :
1553 F2FS_GET_BLOCK_PRE_AIO));
1560 last_off = map.m_lblk + map.m_len - 1;
1562 /* update new size to the failed position */
1563 new_size = (last_off == pg_end) ? offset + len :
1564 (loff_t)(last_off + 1) << PAGE_SHIFT;
1566 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1569 if (new_size > i_size_read(inode)) {
1570 if (mode & FALLOC_FL_KEEP_SIZE)
1571 file_set_keep_isize(inode);
1573 f2fs_i_size_write(inode, new_size);
1579 static long f2fs_fallocate(struct file *file, int mode,
1580 loff_t offset, loff_t len)
1582 struct inode *inode = file_inode(file);
1585 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1587 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1590 /* f2fs only support ->fallocate for regular file */
1591 if (!S_ISREG(inode->i_mode))
1594 if (IS_ENCRYPTED(inode) &&
1595 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1598 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1599 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1600 FALLOC_FL_INSERT_RANGE))
1605 if (mode & FALLOC_FL_PUNCH_HOLE) {
1606 if (offset >= inode->i_size)
1609 ret = punch_hole(inode, offset, len);
1610 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1611 ret = f2fs_collapse_range(inode, offset, len);
1612 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1613 ret = f2fs_zero_range(inode, offset, len, mode);
1614 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1615 ret = f2fs_insert_range(inode, offset, len);
1617 ret = expand_inode_data(inode, offset, len, mode);
1621 inode->i_mtime = inode->i_ctime = current_time(inode);
1622 f2fs_mark_inode_dirty_sync(inode, false);
1623 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1627 inode_unlock(inode);
1629 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1633 static int f2fs_release_file(struct inode *inode, struct file *filp)
1636 * f2fs_relase_file is called at every close calls. So we should
1637 * not drop any inmemory pages by close called by other process.
1639 if (!(filp->f_mode & FMODE_WRITE) ||
1640 atomic_read(&inode->i_writecount) != 1)
1643 /* some remained atomic pages should discarded */
1644 if (f2fs_is_atomic_file(inode))
1645 f2fs_drop_inmem_pages(inode);
1646 if (f2fs_is_volatile_file(inode)) {
1647 set_inode_flag(inode, FI_DROP_CACHE);
1648 filemap_fdatawrite(inode->i_mapping);
1649 clear_inode_flag(inode, FI_DROP_CACHE);
1650 clear_inode_flag(inode, FI_VOLATILE_FILE);
1651 stat_dec_volatile_write(inode);
1656 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1658 struct inode *inode = file_inode(file);
1661 * If the process doing a transaction is crashed, we should do
1662 * roll-back. Otherwise, other reader/write can see corrupted database
1663 * until all the writers close its file. Since this should be done
1664 * before dropping file lock, it needs to do in ->flush.
1666 if (f2fs_is_atomic_file(inode) &&
1667 F2FS_I(inode)->inmem_task == current)
1668 f2fs_drop_inmem_pages(inode);
1672 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1674 struct f2fs_inode_info *fi = F2FS_I(inode);
1676 /* Is it quota file? Do not allow user to mess with it */
1677 if (IS_NOQUOTA(inode))
1680 if ((iflags ^ fi->i_flags) & F2FS_CASEFOLD_FL) {
1681 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1683 if (!f2fs_empty_dir(inode))
1687 fi->i_flags = iflags | (fi->i_flags & ~mask);
1689 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1690 set_inode_flag(inode, FI_PROJ_INHERIT);
1692 clear_inode_flag(inode, FI_PROJ_INHERIT);
1694 inode->i_ctime = current_time(inode);
1695 f2fs_set_inode_flags(inode);
1696 f2fs_mark_inode_dirty_sync(inode, true);
1700 /* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1703 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1704 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1705 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1706 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1709 static const struct {
1712 } f2fs_fsflags_map[] = {
1713 { F2FS_SYNC_FL, FS_SYNC_FL },
1714 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1715 { F2FS_APPEND_FL, FS_APPEND_FL },
1716 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1717 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1718 { F2FS_INDEX_FL, FS_INDEX_FL },
1719 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1720 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1721 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1724 #define F2FS_GETTABLE_FS_FL ( \
1732 FS_PROJINHERIT_FL | \
1734 FS_INLINE_DATA_FL | \
1739 #define F2FS_SETTABLE_FS_FL ( \
1746 FS_PROJINHERIT_FL | \
1749 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1750 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1755 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1756 if (iflags & f2fs_fsflags_map[i].iflag)
1757 fsflags |= f2fs_fsflags_map[i].fsflag;
1762 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1763 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1768 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1769 if (fsflags & f2fs_fsflags_map[i].fsflag)
1770 iflags |= f2fs_fsflags_map[i].iflag;
1775 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1777 struct inode *inode = file_inode(filp);
1778 struct f2fs_inode_info *fi = F2FS_I(inode);
1779 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1781 if (IS_ENCRYPTED(inode))
1782 fsflags |= FS_ENCRYPT_FL;
1783 if (IS_VERITY(inode))
1784 fsflags |= FS_VERITY_FL;
1785 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1786 fsflags |= FS_INLINE_DATA_FL;
1787 if (is_inode_flag_set(inode, FI_PIN_FILE))
1788 fsflags |= FS_NOCOW_FL;
1790 fsflags &= F2FS_GETTABLE_FS_FL;
1792 return put_user(fsflags, (int __user *)arg);
1795 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1797 struct inode *inode = file_inode(filp);
1798 struct f2fs_inode_info *fi = F2FS_I(inode);
1799 u32 fsflags, old_fsflags;
1803 if (!inode_owner_or_capable(inode))
1806 if (get_user(fsflags, (int __user *)arg))
1809 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1811 fsflags &= F2FS_SETTABLE_FS_FL;
1813 iflags = f2fs_fsflags_to_iflags(fsflags);
1814 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1817 ret = mnt_want_write_file(filp);
1823 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1824 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1828 ret = f2fs_setflags_common(inode, iflags,
1829 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
1831 inode_unlock(inode);
1832 mnt_drop_write_file(filp);
1836 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1838 struct inode *inode = file_inode(filp);
1840 return put_user(inode->i_generation, (int __user *)arg);
1843 static int f2fs_ioc_start_atomic_write(struct file *filp)
1845 struct inode *inode = file_inode(filp);
1846 struct f2fs_inode_info *fi = F2FS_I(inode);
1847 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1850 if (!inode_owner_or_capable(inode))
1853 if (!S_ISREG(inode->i_mode))
1856 if (filp->f_flags & O_DIRECT)
1859 ret = mnt_want_write_file(filp);
1865 if (f2fs_is_atomic_file(inode)) {
1866 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1871 ret = f2fs_convert_inline_inode(inode);
1875 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1878 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
1879 * f2fs_is_atomic_file.
1881 if (get_dirty_pages(inode))
1882 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1883 inode->i_ino, get_dirty_pages(inode));
1884 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1886 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1890 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
1891 if (list_empty(&fi->inmem_ilist))
1892 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
1893 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
1895 /* add inode in inmem_list first and set atomic_file */
1896 set_inode_flag(inode, FI_ATOMIC_FILE);
1897 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1898 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1900 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1901 F2FS_I(inode)->inmem_task = current;
1902 stat_inc_atomic_write(inode);
1903 stat_update_max_atomic_write(inode);
1905 inode_unlock(inode);
1906 mnt_drop_write_file(filp);
1910 static int f2fs_ioc_commit_atomic_write(struct file *filp)
1912 struct inode *inode = file_inode(filp);
1915 if (!inode_owner_or_capable(inode))
1918 ret = mnt_want_write_file(filp);
1922 f2fs_balance_fs(F2FS_I_SB(inode), true);
1926 if (f2fs_is_volatile_file(inode)) {
1931 if (f2fs_is_atomic_file(inode)) {
1932 ret = f2fs_commit_inmem_pages(inode);
1936 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1938 f2fs_drop_inmem_pages(inode);
1940 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
1943 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1944 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1947 inode_unlock(inode);
1948 mnt_drop_write_file(filp);
1952 static int f2fs_ioc_start_volatile_write(struct file *filp)
1954 struct inode *inode = file_inode(filp);
1957 if (!inode_owner_or_capable(inode))
1960 if (!S_ISREG(inode->i_mode))
1963 ret = mnt_want_write_file(filp);
1969 if (f2fs_is_volatile_file(inode))
1972 ret = f2fs_convert_inline_inode(inode);
1976 stat_inc_volatile_write(inode);
1977 stat_update_max_volatile_write(inode);
1979 set_inode_flag(inode, FI_VOLATILE_FILE);
1980 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1982 inode_unlock(inode);
1983 mnt_drop_write_file(filp);
1987 static int f2fs_ioc_release_volatile_write(struct file *filp)
1989 struct inode *inode = file_inode(filp);
1992 if (!inode_owner_or_capable(inode))
1995 ret = mnt_want_write_file(filp);
2001 if (!f2fs_is_volatile_file(inode))
2004 if (!f2fs_is_first_block_written(inode)) {
2005 ret = truncate_partial_data_page(inode, 0, true);
2009 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2011 inode_unlock(inode);
2012 mnt_drop_write_file(filp);
2016 static int f2fs_ioc_abort_volatile_write(struct file *filp)
2018 struct inode *inode = file_inode(filp);
2021 if (!inode_owner_or_capable(inode))
2024 ret = mnt_want_write_file(filp);
2030 if (f2fs_is_atomic_file(inode))
2031 f2fs_drop_inmem_pages(inode);
2032 if (f2fs_is_volatile_file(inode)) {
2033 clear_inode_flag(inode, FI_VOLATILE_FILE);
2034 stat_dec_volatile_write(inode);
2035 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2038 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2040 inode_unlock(inode);
2042 mnt_drop_write_file(filp);
2043 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2047 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2049 struct inode *inode = file_inode(filp);
2050 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2051 struct super_block *sb = sbi->sb;
2055 if (!capable(CAP_SYS_ADMIN))
2058 if (get_user(in, (__u32 __user *)arg))
2061 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2062 ret = mnt_want_write_file(filp);
2068 case F2FS_GOING_DOWN_FULLSYNC:
2069 sb = freeze_bdev(sb->s_bdev);
2075 f2fs_stop_checkpoint(sbi, false);
2076 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2077 thaw_bdev(sb->s_bdev, sb);
2080 case F2FS_GOING_DOWN_METASYNC:
2081 /* do checkpoint only */
2082 ret = f2fs_sync_fs(sb, 1);
2085 f2fs_stop_checkpoint(sbi, false);
2086 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2088 case F2FS_GOING_DOWN_NOSYNC:
2089 f2fs_stop_checkpoint(sbi, false);
2090 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2092 case F2FS_GOING_DOWN_METAFLUSH:
2093 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2094 f2fs_stop_checkpoint(sbi, false);
2095 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2097 case F2FS_GOING_DOWN_NEED_FSCK:
2098 set_sbi_flag(sbi, SBI_NEED_FSCK);
2099 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2100 set_sbi_flag(sbi, SBI_IS_DIRTY);
2101 /* do checkpoint only */
2102 ret = f2fs_sync_fs(sb, 1);
2109 f2fs_stop_gc_thread(sbi);
2110 f2fs_stop_discard_thread(sbi);
2112 f2fs_drop_discard_cmd(sbi);
2113 clear_opt(sbi, DISCARD);
2115 f2fs_update_time(sbi, REQ_TIME);
2117 if (in != F2FS_GOING_DOWN_FULLSYNC)
2118 mnt_drop_write_file(filp);
2120 trace_f2fs_shutdown(sbi, in, ret);
2125 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2127 struct inode *inode = file_inode(filp);
2128 struct super_block *sb = inode->i_sb;
2129 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2130 struct fstrim_range range;
2133 if (!capable(CAP_SYS_ADMIN))
2136 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2139 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2143 ret = mnt_want_write_file(filp);
2147 range.minlen = max((unsigned int)range.minlen,
2148 q->limits.discard_granularity);
2149 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2150 mnt_drop_write_file(filp);
2154 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2157 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2161 static bool uuid_is_nonzero(__u8 u[16])
2165 for (i = 0; i < 16; i++)
2171 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2173 struct inode *inode = file_inode(filp);
2175 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2178 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2180 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2183 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2185 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2187 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2190 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2192 struct inode *inode = file_inode(filp);
2193 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2196 if (!f2fs_sb_has_encrypt(sbi))
2199 err = mnt_want_write_file(filp);
2203 down_write(&sbi->sb_lock);
2205 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2208 /* update superblock with uuid */
2209 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2211 err = f2fs_commit_super(sbi, false);
2214 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2218 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2222 up_write(&sbi->sb_lock);
2223 mnt_drop_write_file(filp);
2227 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2230 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2233 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2236 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2238 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2241 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2244 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2246 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2249 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2252 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2255 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2258 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2261 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2264 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2267 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2270 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2272 struct inode *inode = file_inode(filp);
2273 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2277 if (!capable(CAP_SYS_ADMIN))
2280 if (get_user(sync, (__u32 __user *)arg))
2283 if (f2fs_readonly(sbi->sb))
2286 ret = mnt_want_write_file(filp);
2291 if (!mutex_trylock(&sbi->gc_mutex)) {
2296 mutex_lock(&sbi->gc_mutex);
2299 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
2301 mnt_drop_write_file(filp);
2305 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2307 struct inode *inode = file_inode(filp);
2308 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2309 struct f2fs_gc_range range;
2313 if (!capable(CAP_SYS_ADMIN))
2316 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2320 if (f2fs_readonly(sbi->sb))
2323 end = range.start + range.len;
2324 if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
2325 end >= MAX_BLKADDR(sbi))
2328 ret = mnt_want_write_file(filp);
2334 if (!mutex_trylock(&sbi->gc_mutex)) {
2339 mutex_lock(&sbi->gc_mutex);
2342 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2343 range.start += BLKS_PER_SEC(sbi);
2344 if (range.start <= end)
2347 mnt_drop_write_file(filp);
2351 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2353 struct inode *inode = file_inode(filp);
2354 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2357 if (!capable(CAP_SYS_ADMIN))
2360 if (f2fs_readonly(sbi->sb))
2363 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2364 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2368 ret = mnt_want_write_file(filp);
2372 ret = f2fs_sync_fs(sbi->sb, 1);
2374 mnt_drop_write_file(filp);
2378 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2380 struct f2fs_defragment *range)
2382 struct inode *inode = file_inode(filp);
2383 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2384 .m_seg_type = NO_CHECK_TYPE ,
2385 .m_may_create = false };
2386 struct extent_info ei = {0, 0, 0};
2387 pgoff_t pg_start, pg_end, next_pgofs;
2388 unsigned int blk_per_seg = sbi->blocks_per_seg;
2389 unsigned int total = 0, sec_num;
2390 block_t blk_end = 0;
2391 bool fragmented = false;
2394 /* if in-place-update policy is enabled, don't waste time here */
2395 if (f2fs_should_update_inplace(inode, NULL))
2398 pg_start = range->start >> PAGE_SHIFT;
2399 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2401 f2fs_balance_fs(sbi, true);
2405 /* writeback all dirty pages in the range */
2406 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2407 range->start + range->len - 1);
2412 * lookup mapping info in extent cache, skip defragmenting if physical
2413 * block addresses are continuous.
2415 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2416 if (ei.fofs + ei.len >= pg_end)
2420 map.m_lblk = pg_start;
2421 map.m_next_pgofs = &next_pgofs;
2424 * lookup mapping info in dnode page cache, skip defragmenting if all
2425 * physical block addresses are continuous even if there are hole(s)
2426 * in logical blocks.
2428 while (map.m_lblk < pg_end) {
2429 map.m_len = pg_end - map.m_lblk;
2430 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2434 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2435 map.m_lblk = next_pgofs;
2439 if (blk_end && blk_end != map.m_pblk)
2442 /* record total count of block that we're going to move */
2445 blk_end = map.m_pblk + map.m_len;
2447 map.m_lblk += map.m_len;
2455 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
2458 * make sure there are enough free section for LFS allocation, this can
2459 * avoid defragment running in SSR mode when free section are allocated
2462 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2467 map.m_lblk = pg_start;
2468 map.m_len = pg_end - pg_start;
2471 while (map.m_lblk < pg_end) {
2476 map.m_len = pg_end - map.m_lblk;
2477 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2481 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2482 map.m_lblk = next_pgofs;
2486 set_inode_flag(inode, FI_DO_DEFRAG);
2489 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2492 page = f2fs_get_lock_data_page(inode, idx, true);
2494 err = PTR_ERR(page);
2498 set_page_dirty(page);
2499 f2fs_put_page(page, 1);
2508 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2511 clear_inode_flag(inode, FI_DO_DEFRAG);
2513 err = filemap_fdatawrite(inode->i_mapping);
2518 clear_inode_flag(inode, FI_DO_DEFRAG);
2520 inode_unlock(inode);
2522 range->len = (u64)total << PAGE_SHIFT;
2526 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2528 struct inode *inode = file_inode(filp);
2529 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2530 struct f2fs_defragment range;
2533 if (!capable(CAP_SYS_ADMIN))
2536 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2539 if (f2fs_readonly(sbi->sb))
2542 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2546 /* verify alignment of offset & size */
2547 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2550 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2551 sbi->max_file_blocks))
2554 err = mnt_want_write_file(filp);
2558 err = f2fs_defragment_range(sbi, filp, &range);
2559 mnt_drop_write_file(filp);
2561 f2fs_update_time(sbi, REQ_TIME);
2565 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2572 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2573 struct file *file_out, loff_t pos_out, size_t len)
2575 struct inode *src = file_inode(file_in);
2576 struct inode *dst = file_inode(file_out);
2577 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2578 size_t olen = len, dst_max_i_size = 0;
2582 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2583 src->i_sb != dst->i_sb)
2586 if (unlikely(f2fs_readonly(src->i_sb)))
2589 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2592 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2596 if (pos_in == pos_out)
2598 if (pos_out > pos_in && pos_out < pos_in + len)
2605 if (!inode_trylock(dst))
2610 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2613 olen = len = src->i_size - pos_in;
2614 if (pos_in + len == src->i_size)
2615 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2621 dst_osize = dst->i_size;
2622 if (pos_out + olen > dst->i_size)
2623 dst_max_i_size = pos_out + olen;
2625 /* verify the end result is block aligned */
2626 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2627 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2628 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2631 ret = f2fs_convert_inline_inode(src);
2635 ret = f2fs_convert_inline_inode(dst);
2639 /* write out all dirty pages from offset */
2640 ret = filemap_write_and_wait_range(src->i_mapping,
2641 pos_in, pos_in + len);
2645 ret = filemap_write_and_wait_range(dst->i_mapping,
2646 pos_out, pos_out + len);
2650 f2fs_balance_fs(sbi, true);
2652 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2655 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2660 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2661 pos_out >> F2FS_BLKSIZE_BITS,
2662 len >> F2FS_BLKSIZE_BITS, false);
2666 f2fs_i_size_write(dst, dst_max_i_size);
2667 else if (dst_osize != dst->i_size)
2668 f2fs_i_size_write(dst, dst_osize);
2670 f2fs_unlock_op(sbi);
2673 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2675 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2684 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2686 struct f2fs_move_range range;
2690 if (!(filp->f_mode & FMODE_READ) ||
2691 !(filp->f_mode & FMODE_WRITE))
2694 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2698 dst = fdget(range.dst_fd);
2702 if (!(dst.file->f_mode & FMODE_WRITE)) {
2707 err = mnt_want_write_file(filp);
2711 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2712 range.pos_out, range.len);
2714 mnt_drop_write_file(filp);
2718 if (copy_to_user((struct f2fs_move_range __user *)arg,
2719 &range, sizeof(range)))
2726 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2728 struct inode *inode = file_inode(filp);
2729 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2730 struct sit_info *sm = SIT_I(sbi);
2731 unsigned int start_segno = 0, end_segno = 0;
2732 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2733 struct f2fs_flush_device range;
2736 if (!capable(CAP_SYS_ADMIN))
2739 if (f2fs_readonly(sbi->sb))
2742 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2745 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2749 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2750 __is_large_section(sbi)) {
2751 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2752 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2756 ret = mnt_want_write_file(filp);
2760 if (range.dev_num != 0)
2761 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2762 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2764 start_segno = sm->last_victim[FLUSH_DEVICE];
2765 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2766 start_segno = dev_start_segno;
2767 end_segno = min(start_segno + range.segments, dev_end_segno);
2769 while (start_segno < end_segno) {
2770 if (!mutex_trylock(&sbi->gc_mutex)) {
2774 sm->last_victim[GC_CB] = end_segno + 1;
2775 sm->last_victim[GC_GREEDY] = end_segno + 1;
2776 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2777 ret = f2fs_gc(sbi, true, true, start_segno);
2785 mnt_drop_write_file(filp);
2789 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2791 struct inode *inode = file_inode(filp);
2792 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2794 /* Must validate to set it with SQLite behavior in Android. */
2795 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2797 return put_user(sb_feature, (u32 __user *)arg);
2801 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2803 struct dquot *transfer_to[MAXQUOTAS] = {};
2804 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2805 struct super_block *sb = sbi->sb;
2808 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2809 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2810 err = __dquot_transfer(inode, transfer_to);
2812 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2813 dqput(transfer_to[PRJQUOTA]);
2818 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2820 struct inode *inode = file_inode(filp);
2821 struct f2fs_inode_info *fi = F2FS_I(inode);
2822 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2827 if (!f2fs_sb_has_project_quota(sbi)) {
2828 if (projid != F2FS_DEF_PROJID)
2834 if (!f2fs_has_extra_attr(inode))
2837 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2839 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2843 /* Is it quota file? Do not allow user to mess with it */
2844 if (IS_NOQUOTA(inode))
2847 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2849 return PTR_ERR(ipage);
2851 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2854 f2fs_put_page(ipage, 1);
2857 f2fs_put_page(ipage, 1);
2859 err = dquot_initialize(inode);
2864 err = f2fs_transfer_project_quota(inode, kprojid);
2868 F2FS_I(inode)->i_projid = kprojid;
2869 inode->i_ctime = current_time(inode);
2870 f2fs_mark_inode_dirty_sync(inode, true);
2872 f2fs_unlock_op(sbi);
2876 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2881 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2883 if (projid != F2FS_DEF_PROJID)
2889 /* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
2892 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
2893 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
2894 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
2897 static const struct {
2900 } f2fs_xflags_map[] = {
2901 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
2902 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
2903 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
2904 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
2905 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
2906 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
2909 #define F2FS_SUPPORTED_XFLAGS ( \
2911 FS_XFLAG_IMMUTABLE | \
2914 FS_XFLAG_NOATIME | \
2915 FS_XFLAG_PROJINHERIT)
2917 /* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
2918 static inline u32 f2fs_iflags_to_xflags(u32 iflags)
2923 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
2924 if (iflags & f2fs_xflags_map[i].iflag)
2925 xflags |= f2fs_xflags_map[i].xflag;
2930 /* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
2931 static inline u32 f2fs_xflags_to_iflags(u32 xflags)
2936 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
2937 if (xflags & f2fs_xflags_map[i].xflag)
2938 iflags |= f2fs_xflags_map[i].iflag;
2943 static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
2945 struct f2fs_inode_info *fi = F2FS_I(inode);
2947 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
2949 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
2950 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
2953 static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2955 struct inode *inode = file_inode(filp);
2958 f2fs_fill_fsxattr(inode, &fa);
2960 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2965 static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2967 struct inode *inode = file_inode(filp);
2968 struct fsxattr fa, old_fa;
2972 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2975 /* Make sure caller has proper permission */
2976 if (!inode_owner_or_capable(inode))
2979 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
2982 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2983 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
2986 err = mnt_want_write_file(filp);
2992 f2fs_fill_fsxattr(inode, &old_fa);
2993 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
2997 err = f2fs_setflags_common(inode, iflags,
2998 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
3002 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
3004 inode_unlock(inode);
3005 mnt_drop_write_file(filp);
3009 int f2fs_pin_file_control(struct inode *inode, bool inc)
3011 struct f2fs_inode_info *fi = F2FS_I(inode);
3012 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3014 /* Use i_gc_failures for normal file as a risk signal. */
3016 f2fs_i_gc_failures_write(inode,
3017 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3019 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3020 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3021 __func__, inode->i_ino,
3022 fi->i_gc_failures[GC_FAILURE_PIN]);
3023 clear_inode_flag(inode, FI_PIN_FILE);
3029 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3031 struct inode *inode = file_inode(filp);
3035 if (get_user(pin, (__u32 __user *)arg))
3038 if (!S_ISREG(inode->i_mode))
3041 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3044 ret = mnt_want_write_file(filp);
3050 if (f2fs_should_update_outplace(inode, NULL)) {
3056 clear_inode_flag(inode, FI_PIN_FILE);
3057 f2fs_i_gc_failures_write(inode, 0);
3061 if (f2fs_pin_file_control(inode, false)) {
3065 ret = f2fs_convert_inline_inode(inode);
3069 set_inode_flag(inode, FI_PIN_FILE);
3070 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3072 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3074 inode_unlock(inode);
3075 mnt_drop_write_file(filp);
3079 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3081 struct inode *inode = file_inode(filp);
3084 if (is_inode_flag_set(inode, FI_PIN_FILE))
3085 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3086 return put_user(pin, (u32 __user *)arg);
3089 int f2fs_precache_extents(struct inode *inode)
3091 struct f2fs_inode_info *fi = F2FS_I(inode);
3092 struct f2fs_map_blocks map;
3093 pgoff_t m_next_extent;
3097 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3101 map.m_next_pgofs = NULL;
3102 map.m_next_extent = &m_next_extent;
3103 map.m_seg_type = NO_CHECK_TYPE;
3104 map.m_may_create = false;
3105 end = F2FS_I_SB(inode)->max_file_blocks;
3107 while (map.m_lblk < end) {
3108 map.m_len = end - map.m_lblk;
3110 down_write(&fi->i_gc_rwsem[WRITE]);
3111 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3112 up_write(&fi->i_gc_rwsem[WRITE]);
3116 map.m_lblk = m_next_extent;
3122 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3124 return f2fs_precache_extents(file_inode(filp));
3127 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3129 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3133 if (!capable(CAP_SYS_ADMIN))
3136 if (f2fs_readonly(sbi->sb))
3139 if (copy_from_user(&block_count, (void __user *)arg,
3140 sizeof(block_count)))
3143 ret = f2fs_resize_fs(sbi, block_count);
3148 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3150 struct inode *inode = file_inode(filp);
3152 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3154 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3155 f2fs_warn(F2FS_I_SB(inode),
3156 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3161 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3164 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3166 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3169 return fsverity_ioctl_measure(filp, (void __user *)arg);
3172 static int f2fs_get_volume_name(struct file *filp, unsigned long arg)
3174 struct inode *inode = file_inode(filp);
3175 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3180 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3184 down_read(&sbi->sb_lock);
3185 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3186 ARRAY_SIZE(sbi->raw_super->volume_name),
3187 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3188 up_read(&sbi->sb_lock);
3190 if (copy_to_user((char __user *)arg, vbuf,
3191 min(FSLABEL_MAX, count)))
3198 static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
3200 struct inode *inode = file_inode(filp);
3201 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3205 if (!capable(CAP_SYS_ADMIN))
3208 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3210 return PTR_ERR(vbuf);
3212 err = mnt_want_write_file(filp);
3216 down_write(&sbi->sb_lock);
3218 memset(sbi->raw_super->volume_name, 0,
3219 sizeof(sbi->raw_super->volume_name));
3220 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3221 sbi->raw_super->volume_name,
3222 ARRAY_SIZE(sbi->raw_super->volume_name));
3224 err = f2fs_commit_super(sbi, false);
3226 up_write(&sbi->sb_lock);
3228 mnt_drop_write_file(filp);
3234 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3236 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3238 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
3242 case F2FS_IOC_GETFLAGS:
3243 return f2fs_ioc_getflags(filp, arg);
3244 case F2FS_IOC_SETFLAGS:
3245 return f2fs_ioc_setflags(filp, arg);
3246 case F2FS_IOC_GETVERSION:
3247 return f2fs_ioc_getversion(filp, arg);
3248 case F2FS_IOC_START_ATOMIC_WRITE:
3249 return f2fs_ioc_start_atomic_write(filp);
3250 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3251 return f2fs_ioc_commit_atomic_write(filp);
3252 case F2FS_IOC_START_VOLATILE_WRITE:
3253 return f2fs_ioc_start_volatile_write(filp);
3254 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3255 return f2fs_ioc_release_volatile_write(filp);
3256 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3257 return f2fs_ioc_abort_volatile_write(filp);
3258 case F2FS_IOC_SHUTDOWN:
3259 return f2fs_ioc_shutdown(filp, arg);
3261 return f2fs_ioc_fitrim(filp, arg);
3262 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3263 return f2fs_ioc_set_encryption_policy(filp, arg);
3264 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3265 return f2fs_ioc_get_encryption_policy(filp, arg);
3266 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3267 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
3268 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3269 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3270 case FS_IOC_ADD_ENCRYPTION_KEY:
3271 return f2fs_ioc_add_encryption_key(filp, arg);
3272 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3273 return f2fs_ioc_remove_encryption_key(filp, arg);
3274 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3275 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
3276 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3277 return f2fs_ioc_get_encryption_key_status(filp, arg);
3278 case F2FS_IOC_GARBAGE_COLLECT:
3279 return f2fs_ioc_gc(filp, arg);
3280 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3281 return f2fs_ioc_gc_range(filp, arg);
3282 case F2FS_IOC_WRITE_CHECKPOINT:
3283 return f2fs_ioc_write_checkpoint(filp, arg);
3284 case F2FS_IOC_DEFRAGMENT:
3285 return f2fs_ioc_defragment(filp, arg);
3286 case F2FS_IOC_MOVE_RANGE:
3287 return f2fs_ioc_move_range(filp, arg);
3288 case F2FS_IOC_FLUSH_DEVICE:
3289 return f2fs_ioc_flush_device(filp, arg);
3290 case F2FS_IOC_GET_FEATURES:
3291 return f2fs_ioc_get_features(filp, arg);
3292 case F2FS_IOC_FSGETXATTR:
3293 return f2fs_ioc_fsgetxattr(filp, arg);
3294 case F2FS_IOC_FSSETXATTR:
3295 return f2fs_ioc_fssetxattr(filp, arg);
3296 case F2FS_IOC_GET_PIN_FILE:
3297 return f2fs_ioc_get_pin_file(filp, arg);
3298 case F2FS_IOC_SET_PIN_FILE:
3299 return f2fs_ioc_set_pin_file(filp, arg);
3300 case F2FS_IOC_PRECACHE_EXTENTS:
3301 return f2fs_ioc_precache_extents(filp, arg);
3302 case F2FS_IOC_RESIZE_FS:
3303 return f2fs_ioc_resize_fs(filp, arg);
3304 case FS_IOC_ENABLE_VERITY:
3305 return f2fs_ioc_enable_verity(filp, arg);
3306 case FS_IOC_MEASURE_VERITY:
3307 return f2fs_ioc_measure_verity(filp, arg);
3308 case F2FS_IOC_GET_VOLUME_NAME:
3309 return f2fs_get_volume_name(filp, arg);
3310 case F2FS_IOC_SET_VOLUME_NAME:
3311 return f2fs_set_volume_name(filp, arg);
3317 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3319 struct file *file = iocb->ki_filp;
3320 struct inode *inode = file_inode(file);
3323 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
3328 if (iocb->ki_flags & IOCB_NOWAIT) {
3329 if (!inode_trylock(inode)) {
3337 ret = generic_write_checks(iocb, from);
3339 bool preallocated = false;
3340 size_t target_size = 0;
3343 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3344 set_inode_flag(inode, FI_NO_PREALLOC);
3346 if ((iocb->ki_flags & IOCB_NOWAIT)) {
3347 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
3348 iov_iter_count(from)) ||
3349 f2fs_has_inline_data(inode) ||
3350 f2fs_force_buffered_io(inode, iocb, from)) {
3351 clear_inode_flag(inode, FI_NO_PREALLOC);
3352 inode_unlock(inode);
3357 preallocated = true;
3358 target_size = iocb->ki_pos + iov_iter_count(from);
3360 err = f2fs_preallocate_blocks(iocb, from);
3362 clear_inode_flag(inode, FI_NO_PREALLOC);
3363 inode_unlock(inode);
3368 ret = __generic_file_write_iter(iocb, from);
3369 clear_inode_flag(inode, FI_NO_PREALLOC);
3371 /* if we couldn't write data, we should deallocate blocks. */
3372 if (preallocated && i_size_read(inode) < target_size)
3373 f2fs_truncate(inode);
3376 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
3378 inode_unlock(inode);
3380 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
3381 iov_iter_count(from), ret);
3383 ret = generic_write_sync(iocb, ret);
3387 #ifdef CONFIG_COMPAT
3388 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3391 case F2FS_IOC32_GETFLAGS:
3392 cmd = F2FS_IOC_GETFLAGS;
3394 case F2FS_IOC32_SETFLAGS:
3395 cmd = F2FS_IOC_SETFLAGS;
3397 case F2FS_IOC32_GETVERSION:
3398 cmd = F2FS_IOC_GETVERSION;
3400 case F2FS_IOC_START_ATOMIC_WRITE:
3401 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3402 case F2FS_IOC_START_VOLATILE_WRITE:
3403 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3404 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3405 case F2FS_IOC_SHUTDOWN:
3406 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3407 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3408 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3409 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3410 case FS_IOC_ADD_ENCRYPTION_KEY:
3411 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3412 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3413 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3414 case F2FS_IOC_GARBAGE_COLLECT:
3415 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3416 case F2FS_IOC_WRITE_CHECKPOINT:
3417 case F2FS_IOC_DEFRAGMENT:
3418 case F2FS_IOC_MOVE_RANGE:
3419 case F2FS_IOC_FLUSH_DEVICE:
3420 case F2FS_IOC_GET_FEATURES:
3421 case F2FS_IOC_FSGETXATTR:
3422 case F2FS_IOC_FSSETXATTR:
3423 case F2FS_IOC_GET_PIN_FILE:
3424 case F2FS_IOC_SET_PIN_FILE:
3425 case F2FS_IOC_PRECACHE_EXTENTS:
3426 case F2FS_IOC_RESIZE_FS:
3427 case FS_IOC_ENABLE_VERITY:
3428 case FS_IOC_MEASURE_VERITY:
3429 case F2FS_IOC_GET_VOLUME_NAME:
3430 case F2FS_IOC_SET_VOLUME_NAME:
3433 return -ENOIOCTLCMD;
3435 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3439 const struct file_operations f2fs_file_operations = {
3440 .llseek = f2fs_llseek,
3441 .read_iter = generic_file_read_iter,
3442 .write_iter = f2fs_file_write_iter,
3443 .open = f2fs_file_open,
3444 .release = f2fs_release_file,
3445 .mmap = f2fs_file_mmap,
3446 .flush = f2fs_file_flush,
3447 .fsync = f2fs_sync_file,
3448 .fallocate = f2fs_fallocate,
3449 .unlocked_ioctl = f2fs_ioctl,
3450 #ifdef CONFIG_COMPAT
3451 .compat_ioctl = f2fs_compat_ioctl,
3453 .splice_read = generic_file_splice_read,
3454 .splice_write = iter_file_splice_write,