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 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
117 set_new_dnode(&dn, inode, NULL, NULL, 0);
118 err = f2fs_get_block(&dn, page->index);
119 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
122 #ifdef CONFIG_F2FS_FS_COMPRESSION
124 set_new_dnode(&dn, inode, NULL, NULL, 0);
125 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
134 f2fs_wait_on_page_writeback(page, DATA, false, true);
136 /* wait for GCed page writeback via META_MAPPING */
137 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
140 * check to see if the page is mapped already (no holes)
142 if (PageMappedToDisk(page))
145 /* page is wholly or partially inside EOF */
146 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
147 i_size_read(inode)) {
150 offset = i_size_read(inode) & ~PAGE_MASK;
151 zero_user_segment(page, offset, PAGE_SIZE);
153 set_page_dirty(page);
154 if (!PageUptodate(page))
155 SetPageUptodate(page);
157 f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
158 f2fs_update_time(sbi, REQ_TIME);
160 trace_f2fs_vm_page_mkwrite(page, DATA);
162 filemap_invalidate_unlock_shared(inode->i_mapping);
164 sb_end_pagefault(inode->i_sb);
166 return block_page_mkwrite_return(err);
169 static const struct vm_operations_struct f2fs_file_vm_ops = {
170 .fault = f2fs_filemap_fault,
171 .map_pages = filemap_map_pages,
172 .page_mkwrite = f2fs_vm_page_mkwrite,
175 static int get_parent_ino(struct inode *inode, nid_t *pino)
177 struct dentry *dentry;
180 * Make sure to get the non-deleted alias. The alias associated with
181 * the open file descriptor being fsync()'ed may be deleted already.
183 dentry = d_find_alias(inode);
187 *pino = parent_ino(dentry);
192 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
194 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
195 enum cp_reason_type cp_reason = CP_NO_NEEDED;
197 if (!S_ISREG(inode->i_mode))
198 cp_reason = CP_NON_REGULAR;
199 else if (f2fs_compressed_file(inode))
200 cp_reason = CP_COMPRESSED;
201 else if (inode->i_nlink != 1)
202 cp_reason = CP_HARDLINK;
203 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
204 cp_reason = CP_SB_NEED_CP;
205 else if (file_wrong_pino(inode))
206 cp_reason = CP_WRONG_PINO;
207 else if (!f2fs_space_for_roll_forward(sbi))
208 cp_reason = CP_NO_SPC_ROLL;
209 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
210 cp_reason = CP_NODE_NEED_CP;
211 else if (test_opt(sbi, FASTBOOT))
212 cp_reason = CP_FASTBOOT_MODE;
213 else if (F2FS_OPTION(sbi).active_logs == 2)
214 cp_reason = CP_SPEC_LOG_NUM;
215 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
216 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
217 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
219 cp_reason = CP_RECOVER_DIR;
224 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
226 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
228 /* But we need to avoid that there are some inode updates */
229 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
235 static void try_to_fix_pino(struct inode *inode)
237 struct f2fs_inode_info *fi = F2FS_I(inode);
240 f2fs_down_write(&fi->i_sem);
241 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
242 get_parent_ino(inode, &pino)) {
243 f2fs_i_pino_write(inode, pino);
244 file_got_pino(inode);
246 f2fs_up_write(&fi->i_sem);
249 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
250 int datasync, bool atomic)
252 struct inode *inode = file->f_mapping->host;
253 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
254 nid_t ino = inode->i_ino;
256 enum cp_reason_type cp_reason = 0;
257 struct writeback_control wbc = {
258 .sync_mode = WB_SYNC_ALL,
259 .nr_to_write = LONG_MAX,
262 unsigned int seq_id = 0;
264 if (unlikely(f2fs_readonly(inode->i_sb)))
267 trace_f2fs_sync_file_enter(inode);
269 if (S_ISDIR(inode->i_mode))
272 /* if fdatasync is triggered, let's do in-place-update */
273 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
274 set_inode_flag(inode, FI_NEED_IPU);
275 ret = file_write_and_wait_range(file, start, end);
276 clear_inode_flag(inode, FI_NEED_IPU);
278 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
279 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
283 /* if the inode is dirty, let's recover all the time */
284 if (!f2fs_skip_inode_update(inode, datasync)) {
285 f2fs_write_inode(inode, NULL);
290 * if there is no written data, don't waste time to write recovery info.
292 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
293 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
295 /* it may call write_inode just prior to fsync */
296 if (need_inode_page_update(sbi, ino))
299 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
300 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
305 * for OPU case, during fsync(), node can be persisted before
306 * data when lower device doesn't support write barrier, result
307 * in data corruption after SPO.
308 * So for strict fsync mode, force to use atomic write sematics
309 * to keep write order in between data/node and last node to
310 * avoid potential data corruption.
312 if (F2FS_OPTION(sbi).fsync_mode ==
313 FSYNC_MODE_STRICT && !atomic)
318 * Both of fdatasync() and fsync() are able to be recovered from
321 f2fs_down_read(&F2FS_I(inode)->i_sem);
322 cp_reason = need_do_checkpoint(inode);
323 f2fs_up_read(&F2FS_I(inode)->i_sem);
326 /* all the dirty node pages should be flushed for POR */
327 ret = f2fs_sync_fs(inode->i_sb, 1);
330 * We've secured consistency through sync_fs. Following pino
331 * will be used only for fsynced inodes after checkpoint.
333 try_to_fix_pino(inode);
334 clear_inode_flag(inode, FI_APPEND_WRITE);
335 clear_inode_flag(inode, FI_UPDATE_WRITE);
339 atomic_inc(&sbi->wb_sync_req[NODE]);
340 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
341 atomic_dec(&sbi->wb_sync_req[NODE]);
345 /* if cp_error was enabled, we should avoid infinite loop */
346 if (unlikely(f2fs_cp_error(sbi))) {
351 if (f2fs_need_inode_block_update(sbi, ino)) {
352 f2fs_mark_inode_dirty_sync(inode, true);
353 f2fs_write_inode(inode, NULL);
358 * If it's atomic_write, it's just fine to keep write ordering. So
359 * here we don't need to wait for node write completion, since we use
360 * node chain which serializes node blocks. If one of node writes are
361 * reordered, we can see simply broken chain, resulting in stopping
362 * roll-forward recovery. It means we'll recover all or none node blocks
366 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
371 /* once recovery info is written, don't need to tack this */
372 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
373 clear_inode_flag(inode, FI_APPEND_WRITE);
375 if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) ||
376 (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi)))
377 ret = f2fs_issue_flush(sbi, inode->i_ino);
379 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
380 clear_inode_flag(inode, FI_UPDATE_WRITE);
381 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
383 f2fs_update_time(sbi, REQ_TIME);
385 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
389 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
391 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
393 return f2fs_do_sync_file(file, start, end, datasync, false);
396 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
397 pgoff_t index, int whence)
401 if (__is_valid_data_blkaddr(blkaddr))
403 if (blkaddr == NEW_ADDR &&
404 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
408 if (blkaddr == NULL_ADDR)
415 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
417 struct inode *inode = file->f_mapping->host;
418 loff_t maxbytes = inode->i_sb->s_maxbytes;
419 struct dnode_of_data dn;
420 pgoff_t pgofs, end_offset;
421 loff_t data_ofs = offset;
427 isize = i_size_read(inode);
431 /* handle inline data case */
432 if (f2fs_has_inline_data(inode)) {
433 if (whence == SEEK_HOLE) {
436 } else if (whence == SEEK_DATA) {
442 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
444 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
445 set_new_dnode(&dn, inode, NULL, NULL, 0);
446 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
447 if (err && err != -ENOENT) {
449 } else if (err == -ENOENT) {
450 /* direct node does not exists */
451 if (whence == SEEK_DATA) {
452 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
459 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
461 /* find data/hole in dnode block */
462 for (; dn.ofs_in_node < end_offset;
463 dn.ofs_in_node++, pgofs++,
464 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
467 blkaddr = f2fs_data_blkaddr(&dn);
469 if (__is_valid_data_blkaddr(blkaddr) &&
470 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
471 blkaddr, DATA_GENERIC_ENHANCE)) {
476 if (__found_offset(file->f_mapping, blkaddr,
485 if (whence == SEEK_DATA)
488 if (whence == SEEK_HOLE && data_ofs > isize)
491 return vfs_setpos(file, data_ofs, maxbytes);
497 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
499 struct inode *inode = file->f_mapping->host;
500 loff_t maxbytes = inode->i_sb->s_maxbytes;
502 if (f2fs_compressed_file(inode))
503 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
509 return generic_file_llseek_size(file, offset, whence,
510 maxbytes, i_size_read(inode));
515 return f2fs_seek_block(file, offset, whence);
521 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
523 struct inode *inode = file_inode(file);
525 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
528 if (!f2fs_is_compress_backend_ready(inode))
532 vma->vm_ops = &f2fs_file_vm_ops;
533 set_inode_flag(inode, FI_MMAP_FILE);
537 static int f2fs_file_open(struct inode *inode, struct file *filp)
539 int err = fscrypt_file_open(inode, filp);
544 if (!f2fs_is_compress_backend_ready(inode))
547 err = fsverity_file_open(inode, filp);
551 filp->f_mode |= FMODE_NOWAIT;
553 return dquot_file_open(inode, filp);
556 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
558 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
559 struct f2fs_node *raw_node;
560 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
563 bool compressed_cluster = false;
564 int cluster_index = 0, valid_blocks = 0;
565 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
566 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
568 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
569 base = get_extra_isize(dn->inode);
571 raw_node = F2FS_NODE(dn->node_page);
572 addr = blkaddr_in_node(raw_node) + base + ofs;
574 /* Assumption: truncateion starts with cluster */
575 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
576 block_t blkaddr = le32_to_cpu(*addr);
578 if (f2fs_compressed_file(dn->inode) &&
579 !(cluster_index & (cluster_size - 1))) {
580 if (compressed_cluster)
581 f2fs_i_compr_blocks_update(dn->inode,
582 valid_blocks, false);
583 compressed_cluster = (blkaddr == COMPRESS_ADDR);
587 if (blkaddr == NULL_ADDR)
590 dn->data_blkaddr = NULL_ADDR;
591 f2fs_set_data_blkaddr(dn);
593 if (__is_valid_data_blkaddr(blkaddr)) {
594 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
595 DATA_GENERIC_ENHANCE))
597 if (compressed_cluster)
601 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
602 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
604 f2fs_invalidate_blocks(sbi, blkaddr);
606 if (!released || blkaddr != COMPRESS_ADDR)
610 if (compressed_cluster)
611 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
616 * once we invalidate valid blkaddr in range [ofs, ofs + count],
617 * we will invalidate all blkaddr in the whole range.
619 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
621 f2fs_update_extent_cache_range(dn, fofs, 0, len);
622 dec_valid_block_count(sbi, dn->inode, nr_free);
624 dn->ofs_in_node = ofs;
626 f2fs_update_time(sbi, REQ_TIME);
627 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
628 dn->ofs_in_node, nr_free);
631 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
633 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
636 static int truncate_partial_data_page(struct inode *inode, u64 from,
639 loff_t offset = from & (PAGE_SIZE - 1);
640 pgoff_t index = from >> PAGE_SHIFT;
641 struct address_space *mapping = inode->i_mapping;
644 if (!offset && !cache_only)
648 page = find_lock_page(mapping, index);
649 if (page && PageUptodate(page))
651 f2fs_put_page(page, 1);
655 page = f2fs_get_lock_data_page(inode, index, true);
657 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
659 f2fs_wait_on_page_writeback(page, DATA, true, true);
660 zero_user(page, offset, PAGE_SIZE - offset);
662 /* An encrypted inode should have a key and truncate the last page. */
663 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
665 set_page_dirty(page);
666 f2fs_put_page(page, 1);
670 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
672 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
673 struct dnode_of_data dn;
675 int count = 0, err = 0;
677 bool truncate_page = false;
679 trace_f2fs_truncate_blocks_enter(inode, from);
681 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
683 if (free_from >= max_file_blocks(inode))
689 ipage = f2fs_get_node_page(sbi, inode->i_ino);
691 err = PTR_ERR(ipage);
695 if (f2fs_has_inline_data(inode)) {
696 f2fs_truncate_inline_inode(inode, ipage, from);
697 f2fs_put_page(ipage, 1);
698 truncate_page = true;
702 set_new_dnode(&dn, inode, ipage, NULL, 0);
703 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
710 count = ADDRS_PER_PAGE(dn.node_page, inode);
712 count -= dn.ofs_in_node;
713 f2fs_bug_on(sbi, count < 0);
715 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
716 f2fs_truncate_data_blocks_range(&dn, count);
722 err = f2fs_truncate_inode_blocks(inode, free_from);
727 /* lastly zero out the first data page */
729 err = truncate_partial_data_page(inode, from, truncate_page);
731 trace_f2fs_truncate_blocks_exit(inode, err);
735 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
737 u64 free_from = from;
740 #ifdef CONFIG_F2FS_FS_COMPRESSION
742 * for compressed file, only support cluster size
743 * aligned truncation.
745 if (f2fs_compressed_file(inode))
746 free_from = round_up(from,
747 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
750 err = f2fs_do_truncate_blocks(inode, free_from, lock);
754 #ifdef CONFIG_F2FS_FS_COMPRESSION
756 * For compressed file, after release compress blocks, don't allow write
757 * direct, but we should allow write direct after truncate to zero.
759 if (f2fs_compressed_file(inode) && !free_from
760 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
761 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
763 if (from != free_from) {
764 err = f2fs_truncate_partial_cluster(inode, from, lock);
773 int f2fs_truncate(struct inode *inode)
777 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
780 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
781 S_ISLNK(inode->i_mode)))
784 trace_f2fs_truncate(inode);
786 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
787 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
791 err = f2fs_dquot_initialize(inode);
795 /* we should check inline_data size */
796 if (!f2fs_may_inline_data(inode)) {
797 err = f2fs_convert_inline_inode(inode);
802 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
806 inode->i_mtime = inode->i_ctime = current_time(inode);
807 f2fs_mark_inode_dirty_sync(inode, false);
811 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
813 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
815 if (!fscrypt_dio_supported(inode))
817 if (fsverity_active(inode))
819 if (f2fs_compressed_file(inode))
822 /* disallow direct IO if any of devices has unaligned blksize */
823 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
826 * for blkzoned device, fallback direct IO to buffered IO, so
827 * all IOs can be serialized by log-structured write.
829 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE))
831 if (f2fs_lfs_mode(sbi) && rw == WRITE && F2FS_IO_ALIGNED(sbi))
833 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
839 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
840 struct kstat *stat, u32 request_mask, unsigned int query_flags)
842 struct inode *inode = d_inode(path->dentry);
843 struct f2fs_inode_info *fi = F2FS_I(inode);
844 struct f2fs_inode *ri = NULL;
847 if (f2fs_has_extra_attr(inode) &&
848 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
849 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
850 stat->result_mask |= STATX_BTIME;
851 stat->btime.tv_sec = fi->i_crtime.tv_sec;
852 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
856 * Return the DIO alignment restrictions if requested. We only return
857 * this information when requested, since on encrypted files it might
858 * take a fair bit of work to get if the file wasn't opened recently.
860 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
861 * cannot represent that, so in that case we report no DIO support.
863 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
864 unsigned int bsize = i_blocksize(inode);
866 stat->result_mask |= STATX_DIOALIGN;
867 if (!f2fs_force_buffered_io(inode, WRITE)) {
868 stat->dio_mem_align = bsize;
869 stat->dio_offset_align = bsize;
874 if (flags & F2FS_COMPR_FL)
875 stat->attributes |= STATX_ATTR_COMPRESSED;
876 if (flags & F2FS_APPEND_FL)
877 stat->attributes |= STATX_ATTR_APPEND;
878 if (IS_ENCRYPTED(inode))
879 stat->attributes |= STATX_ATTR_ENCRYPTED;
880 if (flags & F2FS_IMMUTABLE_FL)
881 stat->attributes |= STATX_ATTR_IMMUTABLE;
882 if (flags & F2FS_NODUMP_FL)
883 stat->attributes |= STATX_ATTR_NODUMP;
884 if (IS_VERITY(inode))
885 stat->attributes |= STATX_ATTR_VERITY;
887 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
889 STATX_ATTR_ENCRYPTED |
890 STATX_ATTR_IMMUTABLE |
894 generic_fillattr(mnt_userns, inode, stat);
896 /* we need to show initial sectors used for inline_data/dentries */
897 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
898 f2fs_has_inline_dentry(inode))
899 stat->blocks += (stat->size + 511) >> 9;
904 #ifdef CONFIG_F2FS_FS_POSIX_ACL
905 static void __setattr_copy(struct user_namespace *mnt_userns,
906 struct inode *inode, const struct iattr *attr)
908 unsigned int ia_valid = attr->ia_valid;
910 i_uid_update(mnt_userns, attr, inode);
911 i_gid_update(mnt_userns, attr, inode);
912 if (ia_valid & ATTR_ATIME)
913 inode->i_atime = attr->ia_atime;
914 if (ia_valid & ATTR_MTIME)
915 inode->i_mtime = attr->ia_mtime;
916 if (ia_valid & ATTR_CTIME)
917 inode->i_ctime = attr->ia_ctime;
918 if (ia_valid & ATTR_MODE) {
919 umode_t mode = attr->ia_mode;
920 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
922 if (!vfsgid_in_group_p(vfsgid) &&
923 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
925 set_acl_inode(inode, mode);
929 #define __setattr_copy setattr_copy
932 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
935 struct inode *inode = d_inode(dentry);
938 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
941 if (unlikely(IS_IMMUTABLE(inode)))
944 if (unlikely(IS_APPEND(inode) &&
945 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
946 ATTR_GID | ATTR_TIMES_SET))))
949 if ((attr->ia_valid & ATTR_SIZE) &&
950 !f2fs_is_compress_backend_ready(inode))
953 err = setattr_prepare(mnt_userns, dentry, attr);
957 err = fscrypt_prepare_setattr(dentry, attr);
961 err = fsverity_prepare_setattr(dentry, attr);
965 if (is_quota_modification(mnt_userns, inode, attr)) {
966 err = f2fs_dquot_initialize(inode);
970 if (i_uid_needs_update(mnt_userns, attr, inode) ||
971 i_gid_needs_update(mnt_userns, attr, inode)) {
972 f2fs_lock_op(F2FS_I_SB(inode));
973 err = dquot_transfer(mnt_userns, inode, attr);
975 set_sbi_flag(F2FS_I_SB(inode),
976 SBI_QUOTA_NEED_REPAIR);
977 f2fs_unlock_op(F2FS_I_SB(inode));
981 * update uid/gid under lock_op(), so that dquot and inode can
982 * be updated atomically.
984 i_uid_update(mnt_userns, attr, inode);
985 i_gid_update(mnt_userns, attr, inode);
986 f2fs_mark_inode_dirty_sync(inode, true);
987 f2fs_unlock_op(F2FS_I_SB(inode));
990 if (attr->ia_valid & ATTR_SIZE) {
991 loff_t old_size = i_size_read(inode);
993 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
995 * should convert inline inode before i_size_write to
996 * keep smaller than inline_data size with inline flag.
998 err = f2fs_convert_inline_inode(inode);
1003 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1004 filemap_invalidate_lock(inode->i_mapping);
1006 truncate_setsize(inode, attr->ia_size);
1008 if (attr->ia_size <= old_size)
1009 err = f2fs_truncate(inode);
1011 * do not trim all blocks after i_size if target size is
1012 * larger than i_size.
1014 filemap_invalidate_unlock(inode->i_mapping);
1015 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1019 spin_lock(&F2FS_I(inode)->i_size_lock);
1020 inode->i_mtime = inode->i_ctime = current_time(inode);
1021 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1022 spin_unlock(&F2FS_I(inode)->i_size_lock);
1025 __setattr_copy(mnt_userns, inode, attr);
1027 if (attr->ia_valid & ATTR_MODE) {
1028 err = posix_acl_chmod(mnt_userns, inode, f2fs_get_inode_mode(inode));
1030 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1032 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1033 clear_inode_flag(inode, FI_ACL_MODE);
1037 /* file size may changed here */
1038 f2fs_mark_inode_dirty_sync(inode, true);
1040 /* inode change will produce dirty node pages flushed by checkpoint */
1041 f2fs_balance_fs(F2FS_I_SB(inode), true);
1046 const struct inode_operations f2fs_file_inode_operations = {
1047 .getattr = f2fs_getattr,
1048 .setattr = f2fs_setattr,
1049 .get_acl = f2fs_get_acl,
1050 .set_acl = f2fs_set_acl,
1051 .listxattr = f2fs_listxattr,
1052 .fiemap = f2fs_fiemap,
1053 .fileattr_get = f2fs_fileattr_get,
1054 .fileattr_set = f2fs_fileattr_set,
1057 static int fill_zero(struct inode *inode, pgoff_t index,
1058 loff_t start, loff_t len)
1060 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1066 f2fs_balance_fs(sbi, true);
1069 page = f2fs_get_new_data_page(inode, NULL, index, false);
1070 f2fs_unlock_op(sbi);
1073 return PTR_ERR(page);
1075 f2fs_wait_on_page_writeback(page, DATA, true, true);
1076 zero_user(page, start, len);
1077 set_page_dirty(page);
1078 f2fs_put_page(page, 1);
1082 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1086 while (pg_start < pg_end) {
1087 struct dnode_of_data dn;
1088 pgoff_t end_offset, count;
1090 set_new_dnode(&dn, inode, NULL, NULL, 0);
1091 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1093 if (err == -ENOENT) {
1094 pg_start = f2fs_get_next_page_offset(&dn,
1101 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1102 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1104 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1106 f2fs_truncate_data_blocks_range(&dn, count);
1107 f2fs_put_dnode(&dn);
1114 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1116 pgoff_t pg_start, pg_end;
1117 loff_t off_start, off_end;
1120 ret = f2fs_convert_inline_inode(inode);
1124 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1125 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1127 off_start = offset & (PAGE_SIZE - 1);
1128 off_end = (offset + len) & (PAGE_SIZE - 1);
1130 if (pg_start == pg_end) {
1131 ret = fill_zero(inode, pg_start, off_start,
1132 off_end - off_start);
1137 ret = fill_zero(inode, pg_start++, off_start,
1138 PAGE_SIZE - off_start);
1143 ret = fill_zero(inode, pg_end, 0, off_end);
1148 if (pg_start < pg_end) {
1149 loff_t blk_start, blk_end;
1150 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1152 f2fs_balance_fs(sbi, true);
1154 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1155 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1157 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1158 filemap_invalidate_lock(inode->i_mapping);
1160 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1163 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1164 f2fs_unlock_op(sbi);
1166 filemap_invalidate_unlock(inode->i_mapping);
1167 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1174 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1175 int *do_replace, pgoff_t off, pgoff_t len)
1177 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1178 struct dnode_of_data dn;
1182 set_new_dnode(&dn, inode, NULL, NULL, 0);
1183 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1184 if (ret && ret != -ENOENT) {
1186 } else if (ret == -ENOENT) {
1187 if (dn.max_level == 0)
1189 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1190 dn.ofs_in_node, len);
1196 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1197 dn.ofs_in_node, len);
1198 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1199 *blkaddr = f2fs_data_blkaddr(&dn);
1201 if (__is_valid_data_blkaddr(*blkaddr) &&
1202 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1203 DATA_GENERIC_ENHANCE)) {
1204 f2fs_put_dnode(&dn);
1205 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1206 return -EFSCORRUPTED;
1209 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1211 if (f2fs_lfs_mode(sbi)) {
1212 f2fs_put_dnode(&dn);
1216 /* do not invalidate this block address */
1217 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1221 f2fs_put_dnode(&dn);
1230 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1231 int *do_replace, pgoff_t off, int len)
1233 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1234 struct dnode_of_data dn;
1237 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1238 if (*do_replace == 0)
1241 set_new_dnode(&dn, inode, NULL, NULL, 0);
1242 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1244 dec_valid_block_count(sbi, inode, 1);
1245 f2fs_invalidate_blocks(sbi, *blkaddr);
1247 f2fs_update_data_blkaddr(&dn, *blkaddr);
1249 f2fs_put_dnode(&dn);
1254 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1255 block_t *blkaddr, int *do_replace,
1256 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1258 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1263 if (blkaddr[i] == NULL_ADDR && !full) {
1268 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1269 struct dnode_of_data dn;
1270 struct node_info ni;
1274 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1275 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1279 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1281 f2fs_put_dnode(&dn);
1285 ilen = min((pgoff_t)
1286 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1287 dn.ofs_in_node, len - i);
1289 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1290 f2fs_truncate_data_blocks_range(&dn, 1);
1292 if (do_replace[i]) {
1293 f2fs_i_blocks_write(src_inode,
1295 f2fs_i_blocks_write(dst_inode,
1297 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1298 blkaddr[i], ni.version, true, false);
1304 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1305 if (dst_inode->i_size < new_size)
1306 f2fs_i_size_write(dst_inode, new_size);
1307 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1309 f2fs_put_dnode(&dn);
1311 struct page *psrc, *pdst;
1313 psrc = f2fs_get_lock_data_page(src_inode,
1316 return PTR_ERR(psrc);
1317 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1320 f2fs_put_page(psrc, 1);
1321 return PTR_ERR(pdst);
1323 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1324 set_page_dirty(pdst);
1325 f2fs_put_page(pdst, 1);
1326 f2fs_put_page(psrc, 1);
1328 ret = f2fs_truncate_hole(src_inode,
1329 src + i, src + i + 1);
1338 static int __exchange_data_block(struct inode *src_inode,
1339 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1340 pgoff_t len, bool full)
1342 block_t *src_blkaddr;
1348 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1350 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1351 array_size(olen, sizeof(block_t)),
1356 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1357 array_size(olen, sizeof(int)),
1360 kvfree(src_blkaddr);
1364 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1365 do_replace, src, olen);
1369 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1370 do_replace, src, dst, olen, full);
1378 kvfree(src_blkaddr);
1384 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1385 kvfree(src_blkaddr);
1390 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1392 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1393 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1394 pgoff_t start = offset >> PAGE_SHIFT;
1395 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1398 f2fs_balance_fs(sbi, true);
1400 /* avoid gc operation during block exchange */
1401 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1402 filemap_invalidate_lock(inode->i_mapping);
1405 f2fs_drop_extent_tree(inode);
1406 truncate_pagecache(inode, offset);
1407 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1408 f2fs_unlock_op(sbi);
1410 filemap_invalidate_unlock(inode->i_mapping);
1411 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1415 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1420 if (offset + len >= i_size_read(inode))
1423 /* collapse range should be aligned to block size of f2fs. */
1424 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1427 ret = f2fs_convert_inline_inode(inode);
1431 /* write out all dirty pages from offset */
1432 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1436 ret = f2fs_do_collapse(inode, offset, len);
1440 /* write out all moved pages, if possible */
1441 filemap_invalidate_lock(inode->i_mapping);
1442 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1443 truncate_pagecache(inode, offset);
1445 new_size = i_size_read(inode) - len;
1446 ret = f2fs_truncate_blocks(inode, new_size, true);
1447 filemap_invalidate_unlock(inode->i_mapping);
1449 f2fs_i_size_write(inode, new_size);
1453 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1456 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1457 pgoff_t index = start;
1458 unsigned int ofs_in_node = dn->ofs_in_node;
1462 for (; index < end; index++, dn->ofs_in_node++) {
1463 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1467 dn->ofs_in_node = ofs_in_node;
1468 ret = f2fs_reserve_new_blocks(dn, count);
1472 dn->ofs_in_node = ofs_in_node;
1473 for (index = start; index < end; index++, dn->ofs_in_node++) {
1474 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1476 * f2fs_reserve_new_blocks will not guarantee entire block
1479 if (dn->data_blkaddr == NULL_ADDR) {
1484 if (dn->data_blkaddr == NEW_ADDR)
1487 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1488 DATA_GENERIC_ENHANCE)) {
1489 ret = -EFSCORRUPTED;
1490 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1494 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1495 dn->data_blkaddr = NEW_ADDR;
1496 f2fs_set_data_blkaddr(dn);
1499 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1504 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1507 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1508 struct address_space *mapping = inode->i_mapping;
1509 pgoff_t index, pg_start, pg_end;
1510 loff_t new_size = i_size_read(inode);
1511 loff_t off_start, off_end;
1514 ret = inode_newsize_ok(inode, (len + offset));
1518 ret = f2fs_convert_inline_inode(inode);
1522 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1526 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1527 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1529 off_start = offset & (PAGE_SIZE - 1);
1530 off_end = (offset + len) & (PAGE_SIZE - 1);
1532 if (pg_start == pg_end) {
1533 ret = fill_zero(inode, pg_start, off_start,
1534 off_end - off_start);
1538 new_size = max_t(loff_t, new_size, offset + len);
1541 ret = fill_zero(inode, pg_start++, off_start,
1542 PAGE_SIZE - off_start);
1546 new_size = max_t(loff_t, new_size,
1547 (loff_t)pg_start << PAGE_SHIFT);
1550 for (index = pg_start; index < pg_end;) {
1551 struct dnode_of_data dn;
1552 unsigned int end_offset;
1555 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1556 filemap_invalidate_lock(mapping);
1558 truncate_pagecache_range(inode,
1559 (loff_t)index << PAGE_SHIFT,
1560 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1564 set_new_dnode(&dn, inode, NULL, NULL, 0);
1565 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1567 f2fs_unlock_op(sbi);
1568 filemap_invalidate_unlock(mapping);
1569 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1573 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1574 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1576 ret = f2fs_do_zero_range(&dn, index, end);
1577 f2fs_put_dnode(&dn);
1579 f2fs_unlock_op(sbi);
1580 filemap_invalidate_unlock(mapping);
1581 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1583 f2fs_balance_fs(sbi, dn.node_changed);
1589 new_size = max_t(loff_t, new_size,
1590 (loff_t)index << PAGE_SHIFT);
1594 ret = fill_zero(inode, pg_end, 0, off_end);
1598 new_size = max_t(loff_t, new_size, offset + len);
1603 if (new_size > i_size_read(inode)) {
1604 if (mode & FALLOC_FL_KEEP_SIZE)
1605 file_set_keep_isize(inode);
1607 f2fs_i_size_write(inode, new_size);
1612 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1614 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1615 struct address_space *mapping = inode->i_mapping;
1616 pgoff_t nr, pg_start, pg_end, delta, idx;
1620 new_size = i_size_read(inode) + len;
1621 ret = inode_newsize_ok(inode, new_size);
1625 if (offset >= i_size_read(inode))
1628 /* insert range should be aligned to block size of f2fs. */
1629 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1632 ret = f2fs_convert_inline_inode(inode);
1636 f2fs_balance_fs(sbi, true);
1638 filemap_invalidate_lock(mapping);
1639 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1640 filemap_invalidate_unlock(mapping);
1644 /* write out all dirty pages from offset */
1645 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1649 pg_start = offset >> PAGE_SHIFT;
1650 pg_end = (offset + len) >> PAGE_SHIFT;
1651 delta = pg_end - pg_start;
1652 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1654 /* avoid gc operation during block exchange */
1655 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1656 filemap_invalidate_lock(mapping);
1657 truncate_pagecache(inode, offset);
1659 while (!ret && idx > pg_start) {
1660 nr = idx - pg_start;
1666 f2fs_drop_extent_tree(inode);
1668 ret = __exchange_data_block(inode, inode, idx,
1669 idx + delta, nr, false);
1670 f2fs_unlock_op(sbi);
1672 filemap_invalidate_unlock(mapping);
1673 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1675 /* write out all moved pages, if possible */
1676 filemap_invalidate_lock(mapping);
1677 filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1678 truncate_pagecache(inode, offset);
1679 filemap_invalidate_unlock(mapping);
1682 f2fs_i_size_write(inode, new_size);
1686 static int expand_inode_data(struct inode *inode, loff_t offset,
1687 loff_t len, int mode)
1689 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1690 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1691 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1692 .m_may_create = true };
1693 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1694 .init_gc_type = FG_GC,
1695 .should_migrate_blocks = false,
1696 .err_gc_skipped = true,
1697 .nr_free_secs = 0 };
1698 pgoff_t pg_start, pg_end;
1699 loff_t new_size = i_size_read(inode);
1701 block_t expanded = 0;
1704 err = inode_newsize_ok(inode, (len + offset));
1708 err = f2fs_convert_inline_inode(inode);
1712 f2fs_balance_fs(sbi, true);
1714 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1715 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1716 off_end = (offset + len) & (PAGE_SIZE - 1);
1718 map.m_lblk = pg_start;
1719 map.m_len = pg_end - pg_start;
1726 if (f2fs_is_pinned_file(inode)) {
1727 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1728 block_t sec_len = roundup(map.m_len, sec_blks);
1730 map.m_len = sec_blks;
1732 if (has_not_enough_free_secs(sbi, 0,
1733 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1734 f2fs_down_write(&sbi->gc_lock);
1735 err = f2fs_gc(sbi, &gc_control);
1736 if (err && err != -ENODATA)
1740 f2fs_down_write(&sbi->pin_sem);
1743 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1744 f2fs_unlock_op(sbi);
1746 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1747 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1748 file_dont_truncate(inode);
1750 f2fs_up_write(&sbi->pin_sem);
1752 expanded += map.m_len;
1753 sec_len -= map.m_len;
1754 map.m_lblk += map.m_len;
1755 if (!err && sec_len)
1758 map.m_len = expanded;
1760 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1761 expanded = map.m_len;
1770 last_off = pg_start + expanded - 1;
1772 /* update new size to the failed position */
1773 new_size = (last_off == pg_end) ? offset + len :
1774 (loff_t)(last_off + 1) << PAGE_SHIFT;
1776 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1779 if (new_size > i_size_read(inode)) {
1780 if (mode & FALLOC_FL_KEEP_SIZE)
1781 file_set_keep_isize(inode);
1783 f2fs_i_size_write(inode, new_size);
1789 static long f2fs_fallocate(struct file *file, int mode,
1790 loff_t offset, loff_t len)
1792 struct inode *inode = file_inode(file);
1795 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1797 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1799 if (!f2fs_is_compress_backend_ready(inode))
1802 /* f2fs only support ->fallocate for regular file */
1803 if (!S_ISREG(inode->i_mode))
1806 if (IS_ENCRYPTED(inode) &&
1807 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1811 * Pinned file should not support partial trucation since the block
1812 * can be used by applications.
1814 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1815 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1816 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1819 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1820 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1821 FALLOC_FL_INSERT_RANGE))
1826 ret = file_modified(file);
1830 if (mode & FALLOC_FL_PUNCH_HOLE) {
1831 if (offset >= inode->i_size)
1834 ret = punch_hole(inode, offset, len);
1835 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1836 ret = f2fs_collapse_range(inode, offset, len);
1837 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1838 ret = f2fs_zero_range(inode, offset, len, mode);
1839 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1840 ret = f2fs_insert_range(inode, offset, len);
1842 ret = expand_inode_data(inode, offset, len, mode);
1846 inode->i_mtime = inode->i_ctime = current_time(inode);
1847 f2fs_mark_inode_dirty_sync(inode, false);
1848 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1852 inode_unlock(inode);
1854 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1858 static int f2fs_release_file(struct inode *inode, struct file *filp)
1861 * f2fs_relase_file is called at every close calls. So we should
1862 * not drop any inmemory pages by close called by other process.
1864 if (!(filp->f_mode & FMODE_WRITE) ||
1865 atomic_read(&inode->i_writecount) != 1)
1869 f2fs_abort_atomic_write(inode, true);
1870 inode_unlock(inode);
1875 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1877 struct inode *inode = file_inode(file);
1880 * If the process doing a transaction is crashed, we should do
1881 * roll-back. Otherwise, other reader/write can see corrupted database
1882 * until all the writers close its file. Since this should be done
1883 * before dropping file lock, it needs to do in ->flush.
1885 if (F2FS_I(inode)->atomic_write_task == current &&
1886 (current->flags & PF_EXITING)) {
1888 f2fs_abort_atomic_write(inode, true);
1889 inode_unlock(inode);
1895 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1897 struct f2fs_inode_info *fi = F2FS_I(inode);
1898 u32 masked_flags = fi->i_flags & mask;
1900 /* mask can be shrunk by flags_valid selector */
1903 /* Is it quota file? Do not allow user to mess with it */
1904 if (IS_NOQUOTA(inode))
1907 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1908 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1910 if (!f2fs_empty_dir(inode))
1914 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1915 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1917 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1921 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1922 if (masked_flags & F2FS_COMPR_FL) {
1923 if (!f2fs_disable_compressed_file(inode))
1926 /* try to convert inline_data to support compression */
1927 int err = f2fs_convert_inline_inode(inode);
1930 if (!f2fs_may_compress(inode))
1932 if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
1934 if (set_compress_context(inode))
1939 fi->i_flags = iflags | (fi->i_flags & ~mask);
1940 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1941 (fi->i_flags & F2FS_NOCOMP_FL));
1943 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1944 set_inode_flag(inode, FI_PROJ_INHERIT);
1946 clear_inode_flag(inode, FI_PROJ_INHERIT);
1948 inode->i_ctime = current_time(inode);
1949 f2fs_set_inode_flags(inode);
1950 f2fs_mark_inode_dirty_sync(inode, true);
1954 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1957 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1958 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1959 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1960 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1962 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1963 * FS_IOC_FSSETXATTR is done by the VFS.
1966 static const struct {
1969 } f2fs_fsflags_map[] = {
1970 { F2FS_COMPR_FL, FS_COMPR_FL },
1971 { F2FS_SYNC_FL, FS_SYNC_FL },
1972 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1973 { F2FS_APPEND_FL, FS_APPEND_FL },
1974 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1975 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1976 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1977 { F2FS_INDEX_FL, FS_INDEX_FL },
1978 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1979 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1980 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1983 #define F2FS_GETTABLE_FS_FL ( \
1993 FS_PROJINHERIT_FL | \
1995 FS_INLINE_DATA_FL | \
2000 #define F2FS_SETTABLE_FS_FL ( \
2009 FS_PROJINHERIT_FL | \
2012 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2013 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2018 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2019 if (iflags & f2fs_fsflags_map[i].iflag)
2020 fsflags |= f2fs_fsflags_map[i].fsflag;
2025 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2026 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2031 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2032 if (fsflags & f2fs_fsflags_map[i].fsflag)
2033 iflags |= f2fs_fsflags_map[i].iflag;
2038 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2040 struct inode *inode = file_inode(filp);
2042 return put_user(inode->i_generation, (int __user *)arg);
2045 static int f2fs_ioc_start_atomic_write(struct file *filp)
2047 struct inode *inode = file_inode(filp);
2048 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2049 struct f2fs_inode_info *fi = F2FS_I(inode);
2050 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2051 struct inode *pinode;
2055 if (!inode_owner_or_capable(mnt_userns, inode))
2058 if (!S_ISREG(inode->i_mode))
2061 if (filp->f_flags & O_DIRECT)
2064 ret = mnt_want_write_file(filp);
2070 if (!f2fs_disable_compressed_file(inode)) {
2075 if (f2fs_is_atomic_file(inode))
2078 ret = f2fs_convert_inline_inode(inode);
2082 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2085 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2086 * f2fs_is_atomic_file.
2088 if (get_dirty_pages(inode))
2089 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2090 inode->i_ino, get_dirty_pages(inode));
2091 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2093 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2097 /* Check if the inode already has a COW inode */
2098 if (fi->cow_inode == NULL) {
2099 /* Create a COW inode for atomic write */
2100 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2101 if (IS_ERR(pinode)) {
2102 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2103 ret = PTR_ERR(pinode);
2107 ret = f2fs_get_tmpfile(mnt_userns, pinode, &fi->cow_inode);
2110 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2114 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2115 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2117 /* Reuse the already created COW inode */
2118 f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2121 f2fs_write_inode(inode, NULL);
2123 isize = i_size_read(inode);
2124 fi->original_i_size = isize;
2125 f2fs_i_size_write(fi->cow_inode, isize);
2127 stat_inc_atomic_inode(inode);
2129 set_inode_flag(inode, FI_ATOMIC_FILE);
2130 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2132 f2fs_update_time(sbi, REQ_TIME);
2133 fi->atomic_write_task = current;
2134 stat_update_max_atomic_write(inode);
2135 fi->atomic_write_cnt = 0;
2137 inode_unlock(inode);
2138 mnt_drop_write_file(filp);
2142 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2144 struct inode *inode = file_inode(filp);
2145 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2148 if (!inode_owner_or_capable(mnt_userns, inode))
2151 ret = mnt_want_write_file(filp);
2155 f2fs_balance_fs(F2FS_I_SB(inode), true);
2159 if (f2fs_is_atomic_file(inode)) {
2160 ret = f2fs_commit_atomic_write(inode);
2162 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2164 f2fs_abort_atomic_write(inode, ret);
2166 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2169 inode_unlock(inode);
2170 mnt_drop_write_file(filp);
2174 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2176 struct inode *inode = file_inode(filp);
2177 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2180 if (!inode_owner_or_capable(mnt_userns, inode))
2183 ret = mnt_want_write_file(filp);
2189 f2fs_abort_atomic_write(inode, true);
2191 inode_unlock(inode);
2193 mnt_drop_write_file(filp);
2194 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2198 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2200 struct inode *inode = file_inode(filp);
2201 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2202 struct super_block *sb = sbi->sb;
2206 if (!capable(CAP_SYS_ADMIN))
2209 if (get_user(in, (__u32 __user *)arg))
2212 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2213 ret = mnt_want_write_file(filp);
2215 if (ret == -EROFS) {
2217 f2fs_stop_checkpoint(sbi, false,
2218 STOP_CP_REASON_SHUTDOWN);
2219 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2220 trace_f2fs_shutdown(sbi, in, ret);
2227 case F2FS_GOING_DOWN_FULLSYNC:
2228 ret = freeze_bdev(sb->s_bdev);
2231 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2232 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2233 thaw_bdev(sb->s_bdev);
2235 case F2FS_GOING_DOWN_METASYNC:
2236 /* do checkpoint only */
2237 ret = f2fs_sync_fs(sb, 1);
2240 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2241 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2243 case F2FS_GOING_DOWN_NOSYNC:
2244 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2245 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2247 case F2FS_GOING_DOWN_METAFLUSH:
2248 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2249 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2250 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2252 case F2FS_GOING_DOWN_NEED_FSCK:
2253 set_sbi_flag(sbi, SBI_NEED_FSCK);
2254 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2255 set_sbi_flag(sbi, SBI_IS_DIRTY);
2256 /* do checkpoint only */
2257 ret = f2fs_sync_fs(sb, 1);
2264 f2fs_stop_gc_thread(sbi);
2265 f2fs_stop_discard_thread(sbi);
2267 f2fs_drop_discard_cmd(sbi);
2268 clear_opt(sbi, DISCARD);
2270 f2fs_update_time(sbi, REQ_TIME);
2272 if (in != F2FS_GOING_DOWN_FULLSYNC)
2273 mnt_drop_write_file(filp);
2275 trace_f2fs_shutdown(sbi, in, ret);
2280 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2282 struct inode *inode = file_inode(filp);
2283 struct super_block *sb = inode->i_sb;
2284 struct fstrim_range range;
2287 if (!capable(CAP_SYS_ADMIN))
2290 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2293 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2297 ret = mnt_want_write_file(filp);
2301 range.minlen = max((unsigned int)range.minlen,
2302 bdev_discard_granularity(sb->s_bdev));
2303 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2304 mnt_drop_write_file(filp);
2308 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2311 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2315 static bool uuid_is_nonzero(__u8 u[16])
2319 for (i = 0; i < 16; i++)
2325 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2327 struct inode *inode = file_inode(filp);
2329 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2332 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2334 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2337 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2339 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2341 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2344 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2346 struct inode *inode = file_inode(filp);
2347 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2348 u8 encrypt_pw_salt[16];
2351 if (!f2fs_sb_has_encrypt(sbi))
2354 err = mnt_want_write_file(filp);
2358 f2fs_down_write(&sbi->sb_lock);
2360 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2363 /* update superblock with uuid */
2364 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2366 err = f2fs_commit_super(sbi, false);
2369 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2373 memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2375 f2fs_up_write(&sbi->sb_lock);
2376 mnt_drop_write_file(filp);
2378 if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2384 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2387 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2390 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2393 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2395 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2398 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2401 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2403 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2406 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2409 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2412 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2415 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2418 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2421 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2424 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2427 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2429 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2432 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2435 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2437 struct inode *inode = file_inode(filp);
2438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2439 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2441 .should_migrate_blocks = false,
2442 .nr_free_secs = 0 };
2446 if (!capable(CAP_SYS_ADMIN))
2449 if (get_user(sync, (__u32 __user *)arg))
2452 if (f2fs_readonly(sbi->sb))
2455 ret = mnt_want_write_file(filp);
2460 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2465 f2fs_down_write(&sbi->gc_lock);
2468 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2469 gc_control.err_gc_skipped = sync;
2470 ret = f2fs_gc(sbi, &gc_control);
2472 mnt_drop_write_file(filp);
2476 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2478 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2479 struct f2fs_gc_control gc_control = {
2480 .init_gc_type = range->sync ? FG_GC : BG_GC,
2482 .should_migrate_blocks = false,
2483 .err_gc_skipped = range->sync,
2484 .nr_free_secs = 0 };
2488 if (!capable(CAP_SYS_ADMIN))
2490 if (f2fs_readonly(sbi->sb))
2493 end = range->start + range->len;
2494 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2495 end >= MAX_BLKADDR(sbi))
2498 ret = mnt_want_write_file(filp);
2504 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2509 f2fs_down_write(&sbi->gc_lock);
2512 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2513 ret = f2fs_gc(sbi, &gc_control);
2519 range->start += CAP_BLKS_PER_SEC(sbi);
2520 if (range->start <= end)
2523 mnt_drop_write_file(filp);
2527 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2529 struct f2fs_gc_range range;
2531 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2534 return __f2fs_ioc_gc_range(filp, &range);
2537 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2539 struct inode *inode = file_inode(filp);
2540 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2543 if (!capable(CAP_SYS_ADMIN))
2546 if (f2fs_readonly(sbi->sb))
2549 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2550 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2554 ret = mnt_want_write_file(filp);
2558 ret = f2fs_sync_fs(sbi->sb, 1);
2560 mnt_drop_write_file(filp);
2564 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2566 struct f2fs_defragment *range)
2568 struct inode *inode = file_inode(filp);
2569 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2570 .m_seg_type = NO_CHECK_TYPE,
2571 .m_may_create = false };
2572 struct extent_info ei = {0, 0, 0};
2573 pgoff_t pg_start, pg_end, next_pgofs;
2574 unsigned int blk_per_seg = sbi->blocks_per_seg;
2575 unsigned int total = 0, sec_num;
2576 block_t blk_end = 0;
2577 bool fragmented = false;
2580 pg_start = range->start >> PAGE_SHIFT;
2581 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2583 f2fs_balance_fs(sbi, true);
2587 /* if in-place-update policy is enabled, don't waste time here */
2588 set_inode_flag(inode, FI_OPU_WRITE);
2589 if (f2fs_should_update_inplace(inode, NULL)) {
2594 /* writeback all dirty pages in the range */
2595 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2596 range->start + range->len - 1);
2601 * lookup mapping info in extent cache, skip defragmenting if physical
2602 * block addresses are continuous.
2604 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2605 if (ei.fofs + ei.len >= pg_end)
2609 map.m_lblk = pg_start;
2610 map.m_next_pgofs = &next_pgofs;
2613 * lookup mapping info in dnode page cache, skip defragmenting if all
2614 * physical block addresses are continuous even if there are hole(s)
2615 * in logical blocks.
2617 while (map.m_lblk < pg_end) {
2618 map.m_len = pg_end - map.m_lblk;
2619 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2623 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2624 map.m_lblk = next_pgofs;
2628 if (blk_end && blk_end != map.m_pblk)
2631 /* record total count of block that we're going to move */
2634 blk_end = map.m_pblk + map.m_len;
2636 map.m_lblk += map.m_len;
2644 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2647 * make sure there are enough free section for LFS allocation, this can
2648 * avoid defragment running in SSR mode when free section are allocated
2651 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2656 map.m_lblk = pg_start;
2657 map.m_len = pg_end - pg_start;
2660 while (map.m_lblk < pg_end) {
2665 map.m_len = pg_end - map.m_lblk;
2666 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2670 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2671 map.m_lblk = next_pgofs;
2675 set_inode_flag(inode, FI_SKIP_WRITES);
2678 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2681 page = f2fs_get_lock_data_page(inode, idx, true);
2683 err = PTR_ERR(page);
2687 set_page_dirty(page);
2688 set_page_private_gcing(page);
2689 f2fs_put_page(page, 1);
2698 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2701 clear_inode_flag(inode, FI_SKIP_WRITES);
2703 err = filemap_fdatawrite(inode->i_mapping);
2708 clear_inode_flag(inode, FI_SKIP_WRITES);
2710 clear_inode_flag(inode, FI_OPU_WRITE);
2711 inode_unlock(inode);
2713 range->len = (u64)total << PAGE_SHIFT;
2717 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2719 struct inode *inode = file_inode(filp);
2720 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2721 struct f2fs_defragment range;
2724 if (!capable(CAP_SYS_ADMIN))
2727 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2730 if (f2fs_readonly(sbi->sb))
2733 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2737 /* verify alignment of offset & size */
2738 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2741 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2742 max_file_blocks(inode)))
2745 err = mnt_want_write_file(filp);
2749 err = f2fs_defragment_range(sbi, filp, &range);
2750 mnt_drop_write_file(filp);
2752 f2fs_update_time(sbi, REQ_TIME);
2756 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2763 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2764 struct file *file_out, loff_t pos_out, size_t len)
2766 struct inode *src = file_inode(file_in);
2767 struct inode *dst = file_inode(file_out);
2768 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2769 size_t olen = len, dst_max_i_size = 0;
2773 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2774 src->i_sb != dst->i_sb)
2777 if (unlikely(f2fs_readonly(src->i_sb)))
2780 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2783 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2786 if (pos_out < 0 || pos_in < 0)
2790 if (pos_in == pos_out)
2792 if (pos_out > pos_in && pos_out < pos_in + len)
2799 if (!inode_trylock(dst))
2804 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2807 olen = len = src->i_size - pos_in;
2808 if (pos_in + len == src->i_size)
2809 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2815 dst_osize = dst->i_size;
2816 if (pos_out + olen > dst->i_size)
2817 dst_max_i_size = pos_out + olen;
2819 /* verify the end result is block aligned */
2820 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2821 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2822 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2825 ret = f2fs_convert_inline_inode(src);
2829 ret = f2fs_convert_inline_inode(dst);
2833 /* write out all dirty pages from offset */
2834 ret = filemap_write_and_wait_range(src->i_mapping,
2835 pos_in, pos_in + len);
2839 ret = filemap_write_and_wait_range(dst->i_mapping,
2840 pos_out, pos_out + len);
2844 f2fs_balance_fs(sbi, true);
2846 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2849 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2854 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2855 pos_out >> F2FS_BLKSIZE_BITS,
2856 len >> F2FS_BLKSIZE_BITS, false);
2860 f2fs_i_size_write(dst, dst_max_i_size);
2861 else if (dst_osize != dst->i_size)
2862 f2fs_i_size_write(dst, dst_osize);
2864 f2fs_unlock_op(sbi);
2867 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2869 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2878 static int __f2fs_ioc_move_range(struct file *filp,
2879 struct f2fs_move_range *range)
2884 if (!(filp->f_mode & FMODE_READ) ||
2885 !(filp->f_mode & FMODE_WRITE))
2888 dst = fdget(range->dst_fd);
2892 if (!(dst.file->f_mode & FMODE_WRITE)) {
2897 err = mnt_want_write_file(filp);
2901 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2902 range->pos_out, range->len);
2904 mnt_drop_write_file(filp);
2910 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2912 struct f2fs_move_range range;
2914 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2917 return __f2fs_ioc_move_range(filp, &range);
2920 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2922 struct inode *inode = file_inode(filp);
2923 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2924 struct sit_info *sm = SIT_I(sbi);
2925 unsigned int start_segno = 0, end_segno = 0;
2926 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2927 struct f2fs_flush_device range;
2928 struct f2fs_gc_control gc_control = {
2929 .init_gc_type = FG_GC,
2930 .should_migrate_blocks = true,
2931 .err_gc_skipped = true,
2932 .nr_free_secs = 0 };
2935 if (!capable(CAP_SYS_ADMIN))
2938 if (f2fs_readonly(sbi->sb))
2941 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2944 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2948 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2949 __is_large_section(sbi)) {
2950 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2951 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2955 ret = mnt_want_write_file(filp);
2959 if (range.dev_num != 0)
2960 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2961 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2963 start_segno = sm->last_victim[FLUSH_DEVICE];
2964 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2965 start_segno = dev_start_segno;
2966 end_segno = min(start_segno + range.segments, dev_end_segno);
2968 while (start_segno < end_segno) {
2969 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2973 sm->last_victim[GC_CB] = end_segno + 1;
2974 sm->last_victim[GC_GREEDY] = end_segno + 1;
2975 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2977 gc_control.victim_segno = start_segno;
2978 ret = f2fs_gc(sbi, &gc_control);
2986 mnt_drop_write_file(filp);
2990 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2992 struct inode *inode = file_inode(filp);
2993 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2995 /* Must validate to set it with SQLite behavior in Android. */
2996 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2998 return put_user(sb_feature, (u32 __user *)arg);
3002 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3004 struct dquot *transfer_to[MAXQUOTAS] = {};
3005 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3006 struct super_block *sb = sbi->sb;
3009 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3010 if (IS_ERR(transfer_to[PRJQUOTA]))
3011 return PTR_ERR(transfer_to[PRJQUOTA]);
3013 err = __dquot_transfer(inode, transfer_to);
3015 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3016 dqput(transfer_to[PRJQUOTA]);
3020 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3022 struct f2fs_inode_info *fi = F2FS_I(inode);
3023 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3024 struct f2fs_inode *ri = NULL;
3028 if (!f2fs_sb_has_project_quota(sbi)) {
3029 if (projid != F2FS_DEF_PROJID)
3035 if (!f2fs_has_extra_attr(inode))
3038 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3040 if (projid_eq(kprojid, fi->i_projid))
3044 /* Is it quota file? Do not allow user to mess with it */
3045 if (IS_NOQUOTA(inode))
3048 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3051 err = f2fs_dquot_initialize(inode);
3056 err = f2fs_transfer_project_quota(inode, kprojid);
3060 fi->i_projid = kprojid;
3061 inode->i_ctime = current_time(inode);
3062 f2fs_mark_inode_dirty_sync(inode, true);
3064 f2fs_unlock_op(sbi);
3068 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3073 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3075 if (projid != F2FS_DEF_PROJID)
3081 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3083 struct inode *inode = d_inode(dentry);
3084 struct f2fs_inode_info *fi = F2FS_I(inode);
3085 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3087 if (IS_ENCRYPTED(inode))
3088 fsflags |= FS_ENCRYPT_FL;
3089 if (IS_VERITY(inode))
3090 fsflags |= FS_VERITY_FL;
3091 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3092 fsflags |= FS_INLINE_DATA_FL;
3093 if (is_inode_flag_set(inode, FI_PIN_FILE))
3094 fsflags |= FS_NOCOW_FL;
3096 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3098 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3099 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3104 int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3105 struct dentry *dentry, struct fileattr *fa)
3107 struct inode *inode = d_inode(dentry);
3108 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3112 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3114 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3116 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3118 fsflags &= F2FS_SETTABLE_FS_FL;
3119 if (!fa->flags_valid)
3120 mask &= FS_COMMON_FL;
3122 iflags = f2fs_fsflags_to_iflags(fsflags);
3123 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3126 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3128 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3133 int f2fs_pin_file_control(struct inode *inode, bool inc)
3135 struct f2fs_inode_info *fi = F2FS_I(inode);
3136 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3138 /* Use i_gc_failures for normal file as a risk signal. */
3140 f2fs_i_gc_failures_write(inode,
3141 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3143 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3144 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3145 __func__, inode->i_ino,
3146 fi->i_gc_failures[GC_FAILURE_PIN]);
3147 clear_inode_flag(inode, FI_PIN_FILE);
3153 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3155 struct inode *inode = file_inode(filp);
3159 if (get_user(pin, (__u32 __user *)arg))
3162 if (!S_ISREG(inode->i_mode))
3165 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3168 ret = mnt_want_write_file(filp);
3175 clear_inode_flag(inode, FI_PIN_FILE);
3176 f2fs_i_gc_failures_write(inode, 0);
3180 if (f2fs_should_update_outplace(inode, NULL)) {
3185 if (f2fs_pin_file_control(inode, false)) {
3190 ret = f2fs_convert_inline_inode(inode);
3194 if (!f2fs_disable_compressed_file(inode)) {
3199 set_inode_flag(inode, FI_PIN_FILE);
3200 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3202 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3204 inode_unlock(inode);
3205 mnt_drop_write_file(filp);
3209 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3211 struct inode *inode = file_inode(filp);
3214 if (is_inode_flag_set(inode, FI_PIN_FILE))
3215 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3216 return put_user(pin, (u32 __user *)arg);
3219 int f2fs_precache_extents(struct inode *inode)
3221 struct f2fs_inode_info *fi = F2FS_I(inode);
3222 struct f2fs_map_blocks map;
3223 pgoff_t m_next_extent;
3227 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3231 map.m_next_pgofs = NULL;
3232 map.m_next_extent = &m_next_extent;
3233 map.m_seg_type = NO_CHECK_TYPE;
3234 map.m_may_create = false;
3235 end = max_file_blocks(inode);
3237 while (map.m_lblk < end) {
3238 map.m_len = end - map.m_lblk;
3240 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3241 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3242 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3246 map.m_lblk = m_next_extent;
3252 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3254 return f2fs_precache_extents(file_inode(filp));
3257 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3259 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3262 if (!capable(CAP_SYS_ADMIN))
3265 if (f2fs_readonly(sbi->sb))
3268 if (copy_from_user(&block_count, (void __user *)arg,
3269 sizeof(block_count)))
3272 return f2fs_resize_fs(sbi, block_count);
3275 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3277 struct inode *inode = file_inode(filp);
3279 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3281 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3282 f2fs_warn(F2FS_I_SB(inode),
3283 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3288 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3291 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3293 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3296 return fsverity_ioctl_measure(filp, (void __user *)arg);
3299 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3301 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3304 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3307 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3309 struct inode *inode = file_inode(filp);
3310 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3315 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3319 f2fs_down_read(&sbi->sb_lock);
3320 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3321 ARRAY_SIZE(sbi->raw_super->volume_name),
3322 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3323 f2fs_up_read(&sbi->sb_lock);
3325 if (copy_to_user((char __user *)arg, vbuf,
3326 min(FSLABEL_MAX, count)))
3333 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3335 struct inode *inode = file_inode(filp);
3336 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3340 if (!capable(CAP_SYS_ADMIN))
3343 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3345 return PTR_ERR(vbuf);
3347 err = mnt_want_write_file(filp);
3351 f2fs_down_write(&sbi->sb_lock);
3353 memset(sbi->raw_super->volume_name, 0,
3354 sizeof(sbi->raw_super->volume_name));
3355 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3356 sbi->raw_super->volume_name,
3357 ARRAY_SIZE(sbi->raw_super->volume_name));
3359 err = f2fs_commit_super(sbi, false);
3361 f2fs_up_write(&sbi->sb_lock);
3363 mnt_drop_write_file(filp);
3369 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3371 struct inode *inode = file_inode(filp);
3374 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3377 if (!f2fs_compressed_file(inode))
3380 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3381 return put_user(blocks, (u64 __user *)arg);
3384 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3386 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3387 unsigned int released_blocks = 0;
3388 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3392 for (i = 0; i < count; i++) {
3393 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3394 dn->ofs_in_node + i);
3396 if (!__is_valid_data_blkaddr(blkaddr))
3398 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3399 DATA_GENERIC_ENHANCE))) {
3400 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3401 return -EFSCORRUPTED;
3406 int compr_blocks = 0;
3408 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3409 blkaddr = f2fs_data_blkaddr(dn);
3412 if (blkaddr == COMPRESS_ADDR)
3414 dn->ofs_in_node += cluster_size;
3418 if (__is_valid_data_blkaddr(blkaddr))
3421 if (blkaddr != NEW_ADDR)
3424 dn->data_blkaddr = NULL_ADDR;
3425 f2fs_set_data_blkaddr(dn);
3428 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3429 dec_valid_block_count(sbi, dn->inode,
3430 cluster_size - compr_blocks);
3432 released_blocks += cluster_size - compr_blocks;
3434 count -= cluster_size;
3437 return released_blocks;
3440 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3442 struct inode *inode = file_inode(filp);
3443 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3444 pgoff_t page_idx = 0, last_idx;
3445 unsigned int released_blocks = 0;
3449 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3452 if (!f2fs_compressed_file(inode))
3455 if (f2fs_readonly(sbi->sb))
3458 ret = mnt_want_write_file(filp);
3462 f2fs_balance_fs(F2FS_I_SB(inode), true);
3466 writecount = atomic_read(&inode->i_writecount);
3467 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3468 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3473 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3478 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3482 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3483 inode->i_ctime = current_time(inode);
3484 f2fs_mark_inode_dirty_sync(inode, true);
3486 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3489 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3490 filemap_invalidate_lock(inode->i_mapping);
3492 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3494 while (page_idx < last_idx) {
3495 struct dnode_of_data dn;
3496 pgoff_t end_offset, count;
3498 set_new_dnode(&dn, inode, NULL, NULL, 0);
3499 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3501 if (ret == -ENOENT) {
3502 page_idx = f2fs_get_next_page_offset(&dn,
3510 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3511 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3512 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3514 ret = release_compress_blocks(&dn, count);
3516 f2fs_put_dnode(&dn);
3522 released_blocks += ret;
3525 filemap_invalidate_unlock(inode->i_mapping);
3526 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3528 inode_unlock(inode);
3530 mnt_drop_write_file(filp);
3533 ret = put_user(released_blocks, (u64 __user *)arg);
3534 } else if (released_blocks &&
3535 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3536 set_sbi_flag(sbi, SBI_NEED_FSCK);
3537 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3538 "iblocks=%llu, released=%u, compr_blocks=%u, "
3540 __func__, inode->i_ino, inode->i_blocks,
3542 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3548 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3550 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3551 unsigned int reserved_blocks = 0;
3552 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3556 for (i = 0; i < count; i++) {
3557 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3558 dn->ofs_in_node + i);
3560 if (!__is_valid_data_blkaddr(blkaddr))
3562 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3563 DATA_GENERIC_ENHANCE))) {
3564 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3565 return -EFSCORRUPTED;
3570 int compr_blocks = 0;
3574 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3575 blkaddr = f2fs_data_blkaddr(dn);
3578 if (blkaddr == COMPRESS_ADDR)
3580 dn->ofs_in_node += cluster_size;
3584 if (__is_valid_data_blkaddr(blkaddr)) {
3589 dn->data_blkaddr = NEW_ADDR;
3590 f2fs_set_data_blkaddr(dn);
3593 reserved = cluster_size - compr_blocks;
3594 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3598 if (reserved != cluster_size - compr_blocks)
3601 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3603 reserved_blocks += reserved;
3605 count -= cluster_size;
3608 return reserved_blocks;
3611 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3613 struct inode *inode = file_inode(filp);
3614 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3615 pgoff_t page_idx = 0, last_idx;
3616 unsigned int reserved_blocks = 0;
3619 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3622 if (!f2fs_compressed_file(inode))
3625 if (f2fs_readonly(sbi->sb))
3628 ret = mnt_want_write_file(filp);
3632 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3635 f2fs_balance_fs(F2FS_I_SB(inode), true);
3639 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3644 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3645 filemap_invalidate_lock(inode->i_mapping);
3647 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3649 while (page_idx < last_idx) {
3650 struct dnode_of_data dn;
3651 pgoff_t end_offset, count;
3653 set_new_dnode(&dn, inode, NULL, NULL, 0);
3654 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3656 if (ret == -ENOENT) {
3657 page_idx = f2fs_get_next_page_offset(&dn,
3665 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3666 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3667 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3669 ret = reserve_compress_blocks(&dn, count);
3671 f2fs_put_dnode(&dn);
3677 reserved_blocks += ret;
3680 filemap_invalidate_unlock(inode->i_mapping);
3681 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3684 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3685 inode->i_ctime = current_time(inode);
3686 f2fs_mark_inode_dirty_sync(inode, true);
3689 inode_unlock(inode);
3691 mnt_drop_write_file(filp);
3694 ret = put_user(reserved_blocks, (u64 __user *)arg);
3695 } else if (reserved_blocks &&
3696 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3697 set_sbi_flag(sbi, SBI_NEED_FSCK);
3698 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3699 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3701 __func__, inode->i_ino, inode->i_blocks,
3703 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3709 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3710 pgoff_t off, block_t block, block_t len, u32 flags)
3712 sector_t sector = SECTOR_FROM_BLOCK(block);
3713 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3716 if (flags & F2FS_TRIM_FILE_DISCARD) {
3717 if (bdev_max_secure_erase_sectors(bdev))
3718 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3721 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3725 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3726 if (IS_ENCRYPTED(inode))
3727 ret = fscrypt_zeroout_range(inode, off, block, len);
3729 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3736 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3738 struct inode *inode = file_inode(filp);
3739 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3740 struct address_space *mapping = inode->i_mapping;
3741 struct block_device *prev_bdev = NULL;
3742 struct f2fs_sectrim_range range;
3743 pgoff_t index, pg_end, prev_index = 0;
3744 block_t prev_block = 0, len = 0;
3746 bool to_end = false;
3749 if (!(filp->f_mode & FMODE_WRITE))
3752 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3756 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3757 !S_ISREG(inode->i_mode))
3760 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3761 !f2fs_hw_support_discard(sbi)) ||
3762 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3763 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3766 file_start_write(filp);
3769 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3770 range.start >= inode->i_size) {
3778 if (inode->i_size - range.start > range.len) {
3779 end_addr = range.start + range.len;
3781 end_addr = range.len == (u64)-1 ?
3782 sbi->sb->s_maxbytes : inode->i_size;
3786 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3787 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3792 index = F2FS_BYTES_TO_BLK(range.start);
3793 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3795 ret = f2fs_convert_inline_inode(inode);
3799 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3800 filemap_invalidate_lock(mapping);
3802 ret = filemap_write_and_wait_range(mapping, range.start,
3803 to_end ? LLONG_MAX : end_addr - 1);
3807 truncate_inode_pages_range(mapping, range.start,
3808 to_end ? -1 : end_addr - 1);
3810 while (index < pg_end) {
3811 struct dnode_of_data dn;
3812 pgoff_t end_offset, count;
3815 set_new_dnode(&dn, inode, NULL, NULL, 0);
3816 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3818 if (ret == -ENOENT) {
3819 index = f2fs_get_next_page_offset(&dn, index);
3825 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3826 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3827 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3828 struct block_device *cur_bdev;
3829 block_t blkaddr = f2fs_data_blkaddr(&dn);
3831 if (!__is_valid_data_blkaddr(blkaddr))
3834 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3835 DATA_GENERIC_ENHANCE)) {
3836 ret = -EFSCORRUPTED;
3837 f2fs_put_dnode(&dn);
3838 f2fs_handle_error(sbi,
3839 ERROR_INVALID_BLKADDR);
3843 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3844 if (f2fs_is_multi_device(sbi)) {
3845 int di = f2fs_target_device_index(sbi, blkaddr);
3847 blkaddr -= FDEV(di).start_blk;
3851 if (prev_bdev == cur_bdev &&
3852 index == prev_index + len &&
3853 blkaddr == prev_block + len) {
3856 ret = f2fs_secure_erase(prev_bdev,
3857 inode, prev_index, prev_block,
3860 f2fs_put_dnode(&dn);
3869 prev_bdev = cur_bdev;
3871 prev_block = blkaddr;
3876 f2fs_put_dnode(&dn);
3878 if (fatal_signal_pending(current)) {
3886 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3887 prev_block, len, range.flags);
3889 filemap_invalidate_unlock(mapping);
3890 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3892 inode_unlock(inode);
3893 file_end_write(filp);
3898 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3900 struct inode *inode = file_inode(filp);
3901 struct f2fs_comp_option option;
3903 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3906 inode_lock_shared(inode);
3908 if (!f2fs_compressed_file(inode)) {
3909 inode_unlock_shared(inode);
3913 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3914 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3916 inode_unlock_shared(inode);
3918 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3925 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3927 struct inode *inode = file_inode(filp);
3928 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3929 struct f2fs_comp_option option;
3932 if (!f2fs_sb_has_compression(sbi))
3935 if (!(filp->f_mode & FMODE_WRITE))
3938 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3942 if (!f2fs_compressed_file(inode) ||
3943 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3944 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3945 option.algorithm >= COMPRESS_MAX)
3948 file_start_write(filp);
3951 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3956 if (F2FS_HAS_BLOCKS(inode)) {
3961 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3962 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3963 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
3964 f2fs_mark_inode_dirty_sync(inode, true);
3966 if (!f2fs_is_compress_backend_ready(inode))
3967 f2fs_warn(sbi, "compression algorithm is successfully set, "
3968 "but current kernel doesn't support this algorithm.");
3970 inode_unlock(inode);
3971 file_end_write(filp);
3976 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
3978 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
3979 struct address_space *mapping = inode->i_mapping;
3981 pgoff_t redirty_idx = page_idx;
3982 int i, page_len = 0, ret = 0;
3984 page_cache_ra_unbounded(&ractl, len, 0);
3986 for (i = 0; i < len; i++, page_idx++) {
3987 page = read_cache_page(mapping, page_idx, NULL, NULL);
3989 ret = PTR_ERR(page);
3995 for (i = 0; i < page_len; i++, redirty_idx++) {
3996 page = find_lock_page(mapping, redirty_idx);
3998 /* It will never fail, when page has pinned above */
3999 f2fs_bug_on(F2FS_I_SB(inode), !page);
4001 set_page_dirty(page);
4002 f2fs_put_page(page, 1);
4003 f2fs_put_page(page, 0);
4009 static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
4011 struct inode *inode = file_inode(filp);
4012 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4013 struct f2fs_inode_info *fi = F2FS_I(inode);
4014 pgoff_t page_idx = 0, last_idx;
4015 unsigned int blk_per_seg = sbi->blocks_per_seg;
4016 int cluster_size = fi->i_cluster_size;
4019 if (!f2fs_sb_has_compression(sbi) ||
4020 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4023 if (!(filp->f_mode & FMODE_WRITE))
4026 if (!f2fs_compressed_file(inode))
4029 f2fs_balance_fs(F2FS_I_SB(inode), true);
4031 file_start_write(filp);
4034 if (!f2fs_is_compress_backend_ready(inode)) {
4039 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4044 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4048 if (!atomic_read(&fi->i_compr_blocks))
4051 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4053 count = last_idx - page_idx;
4055 int len = min(cluster_size, count);
4057 ret = redirty_blocks(inode, page_idx, len);
4061 if (get_dirty_pages(inode) >= blk_per_seg)
4062 filemap_fdatawrite(inode->i_mapping);
4069 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4073 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4076 inode_unlock(inode);
4077 file_end_write(filp);
4082 static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4084 struct inode *inode = file_inode(filp);
4085 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4086 pgoff_t page_idx = 0, last_idx;
4087 unsigned int blk_per_seg = sbi->blocks_per_seg;
4088 int cluster_size = F2FS_I(inode)->i_cluster_size;
4091 if (!f2fs_sb_has_compression(sbi) ||
4092 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4095 if (!(filp->f_mode & FMODE_WRITE))
4098 if (!f2fs_compressed_file(inode))
4101 f2fs_balance_fs(F2FS_I_SB(inode), true);
4103 file_start_write(filp);
4106 if (!f2fs_is_compress_backend_ready(inode)) {
4111 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4116 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4120 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4122 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4124 count = last_idx - page_idx;
4126 int len = min(cluster_size, count);
4128 ret = redirty_blocks(inode, page_idx, len);
4132 if (get_dirty_pages(inode) >= blk_per_seg)
4133 filemap_fdatawrite(inode->i_mapping);
4140 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4143 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4146 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4149 inode_unlock(inode);
4150 file_end_write(filp);
4155 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4158 case FS_IOC_GETVERSION:
4159 return f2fs_ioc_getversion(filp, arg);
4160 case F2FS_IOC_START_ATOMIC_WRITE:
4161 return f2fs_ioc_start_atomic_write(filp);
4162 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4163 return f2fs_ioc_commit_atomic_write(filp);
4164 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4165 return f2fs_ioc_abort_atomic_write(filp);
4166 case F2FS_IOC_START_VOLATILE_WRITE:
4167 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4169 case F2FS_IOC_SHUTDOWN:
4170 return f2fs_ioc_shutdown(filp, arg);
4172 return f2fs_ioc_fitrim(filp, arg);
4173 case FS_IOC_SET_ENCRYPTION_POLICY:
4174 return f2fs_ioc_set_encryption_policy(filp, arg);
4175 case FS_IOC_GET_ENCRYPTION_POLICY:
4176 return f2fs_ioc_get_encryption_policy(filp, arg);
4177 case FS_IOC_GET_ENCRYPTION_PWSALT:
4178 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4179 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4180 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4181 case FS_IOC_ADD_ENCRYPTION_KEY:
4182 return f2fs_ioc_add_encryption_key(filp, arg);
4183 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4184 return f2fs_ioc_remove_encryption_key(filp, arg);
4185 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4186 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4187 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4188 return f2fs_ioc_get_encryption_key_status(filp, arg);
4189 case FS_IOC_GET_ENCRYPTION_NONCE:
4190 return f2fs_ioc_get_encryption_nonce(filp, arg);
4191 case F2FS_IOC_GARBAGE_COLLECT:
4192 return f2fs_ioc_gc(filp, arg);
4193 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4194 return f2fs_ioc_gc_range(filp, arg);
4195 case F2FS_IOC_WRITE_CHECKPOINT:
4196 return f2fs_ioc_write_checkpoint(filp, arg);
4197 case F2FS_IOC_DEFRAGMENT:
4198 return f2fs_ioc_defragment(filp, arg);
4199 case F2FS_IOC_MOVE_RANGE:
4200 return f2fs_ioc_move_range(filp, arg);
4201 case F2FS_IOC_FLUSH_DEVICE:
4202 return f2fs_ioc_flush_device(filp, arg);
4203 case F2FS_IOC_GET_FEATURES:
4204 return f2fs_ioc_get_features(filp, arg);
4205 case F2FS_IOC_GET_PIN_FILE:
4206 return f2fs_ioc_get_pin_file(filp, arg);
4207 case F2FS_IOC_SET_PIN_FILE:
4208 return f2fs_ioc_set_pin_file(filp, arg);
4209 case F2FS_IOC_PRECACHE_EXTENTS:
4210 return f2fs_ioc_precache_extents(filp, arg);
4211 case F2FS_IOC_RESIZE_FS:
4212 return f2fs_ioc_resize_fs(filp, arg);
4213 case FS_IOC_ENABLE_VERITY:
4214 return f2fs_ioc_enable_verity(filp, arg);
4215 case FS_IOC_MEASURE_VERITY:
4216 return f2fs_ioc_measure_verity(filp, arg);
4217 case FS_IOC_READ_VERITY_METADATA:
4218 return f2fs_ioc_read_verity_metadata(filp, arg);
4219 case FS_IOC_GETFSLABEL:
4220 return f2fs_ioc_getfslabel(filp, arg);
4221 case FS_IOC_SETFSLABEL:
4222 return f2fs_ioc_setfslabel(filp, arg);
4223 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4224 return f2fs_get_compress_blocks(filp, arg);
4225 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4226 return f2fs_release_compress_blocks(filp, arg);
4227 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4228 return f2fs_reserve_compress_blocks(filp, arg);
4229 case F2FS_IOC_SEC_TRIM_FILE:
4230 return f2fs_sec_trim_file(filp, arg);
4231 case F2FS_IOC_GET_COMPRESS_OPTION:
4232 return f2fs_ioc_get_compress_option(filp, arg);
4233 case F2FS_IOC_SET_COMPRESS_OPTION:
4234 return f2fs_ioc_set_compress_option(filp, arg);
4235 case F2FS_IOC_DECOMPRESS_FILE:
4236 return f2fs_ioc_decompress_file(filp, arg);
4237 case F2FS_IOC_COMPRESS_FILE:
4238 return f2fs_ioc_compress_file(filp, arg);
4244 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4246 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4248 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4251 return __f2fs_ioctl(filp, cmd, arg);
4255 * Return %true if the given read or write request should use direct I/O, or
4256 * %false if it should use buffered I/O.
4258 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4259 struct iov_iter *iter)
4263 if (!(iocb->ki_flags & IOCB_DIRECT))
4266 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4270 * Direct I/O not aligned to the disk's logical_block_size will be
4271 * attempted, but will fail with -EINVAL.
4273 * f2fs additionally requires that direct I/O be aligned to the
4274 * filesystem block size, which is often a stricter requirement.
4275 * However, f2fs traditionally falls back to buffered I/O on requests
4276 * that are logical_block_size-aligned but not fs-block aligned.
4278 * The below logic implements this behavior.
4280 align = iocb->ki_pos | iov_iter_alignment(iter);
4281 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4282 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4288 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4291 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4293 dec_page_count(sbi, F2FS_DIO_READ);
4296 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4300 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4301 .end_io = f2fs_dio_read_end_io,
4304 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4306 struct file *file = iocb->ki_filp;
4307 struct inode *inode = file_inode(file);
4308 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4309 struct f2fs_inode_info *fi = F2FS_I(inode);
4310 const loff_t pos = iocb->ki_pos;
4311 const size_t count = iov_iter_count(to);
4312 struct iomap_dio *dio;
4316 return 0; /* skip atime update */
4318 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4320 if (iocb->ki_flags & IOCB_NOWAIT) {
4321 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4326 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4330 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4331 * the higher-level function iomap_dio_rw() in order to ensure that the
4332 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4334 inc_page_count(sbi, F2FS_DIO_READ);
4335 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4336 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4337 if (IS_ERR_OR_NULL(dio)) {
4338 ret = PTR_ERR_OR_ZERO(dio);
4339 if (ret != -EIOCBQUEUED)
4340 dec_page_count(sbi, F2FS_DIO_READ);
4342 ret = iomap_dio_complete(dio);
4345 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4347 file_accessed(file);
4349 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4353 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4355 struct inode *inode = file_inode(iocb->ki_filp);
4356 const loff_t pos = iocb->ki_pos;
4359 if (!f2fs_is_compress_backend_ready(inode))
4362 if (trace_f2fs_dataread_start_enabled()) {
4363 char *p = f2fs_kmalloc(F2FS_I_SB(inode), PATH_MAX, GFP_KERNEL);
4367 goto skip_read_trace;
4369 path = dentry_path_raw(file_dentry(iocb->ki_filp), p, PATH_MAX);
4372 goto skip_read_trace;
4375 trace_f2fs_dataread_start(inode, pos, iov_iter_count(to),
4376 current->pid, path, current->comm);
4380 if (f2fs_should_use_dio(inode, iocb, to)) {
4381 ret = f2fs_dio_read_iter(iocb, to);
4383 ret = filemap_read(iocb, to, 0);
4385 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4386 APP_BUFFERED_READ_IO, ret);
4388 if (trace_f2fs_dataread_end_enabled())
4389 trace_f2fs_dataread_end(inode, pos, ret);
4393 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4395 struct file *file = iocb->ki_filp;
4396 struct inode *inode = file_inode(file);
4400 if (IS_IMMUTABLE(inode))
4403 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4406 count = generic_write_checks(iocb, from);
4410 err = file_modified(file);
4417 * Preallocate blocks for a write request, if it is possible and helpful to do
4418 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4419 * blocks were preallocated, or a negative errno value if something went
4420 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4421 * requested blocks (not just some of them) have been allocated.
4423 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4426 struct inode *inode = file_inode(iocb->ki_filp);
4427 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4428 const loff_t pos = iocb->ki_pos;
4429 const size_t count = iov_iter_count(iter);
4430 struct f2fs_map_blocks map = {};
4434 /* If it will be an out-of-place direct write, don't bother. */
4435 if (dio && f2fs_lfs_mode(sbi))
4438 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4439 * buffered IO, if DIO meets any holes.
4441 if (dio && i_size_read(inode) &&
4442 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4445 /* No-wait I/O can't allocate blocks. */
4446 if (iocb->ki_flags & IOCB_NOWAIT)
4449 /* If it will be a short write, don't bother. */
4450 if (fault_in_iov_iter_readable(iter, count))
4453 if (f2fs_has_inline_data(inode)) {
4454 /* If the data will fit inline, don't bother. */
4455 if (pos + count <= MAX_INLINE_DATA(inode))
4457 ret = f2fs_convert_inline_inode(inode);
4462 /* Do not preallocate blocks that will be written partially in 4KB. */
4463 map.m_lblk = F2FS_BLK_ALIGN(pos);
4464 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4465 if (map.m_len > map.m_lblk)
4466 map.m_len -= map.m_lblk;
4469 map.m_may_create = true;
4471 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4472 flag = F2FS_GET_BLOCK_PRE_DIO;
4474 map.m_seg_type = NO_CHECK_TYPE;
4475 flag = F2FS_GET_BLOCK_PRE_AIO;
4478 ret = f2fs_map_blocks(inode, &map, 1, flag);
4479 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4480 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4483 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4487 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4488 struct iov_iter *from)
4490 struct file *file = iocb->ki_filp;
4491 struct inode *inode = file_inode(file);
4494 if (iocb->ki_flags & IOCB_NOWAIT)
4497 current->backing_dev_info = inode_to_bdi(inode);
4498 ret = generic_perform_write(iocb, from);
4499 current->backing_dev_info = NULL;
4502 iocb->ki_pos += ret;
4503 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4504 APP_BUFFERED_IO, ret);
4509 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4512 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4514 dec_page_count(sbi, F2FS_DIO_WRITE);
4517 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4521 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4522 .end_io = f2fs_dio_write_end_io,
4525 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4526 bool *may_need_sync)
4528 struct file *file = iocb->ki_filp;
4529 struct inode *inode = file_inode(file);
4530 struct f2fs_inode_info *fi = F2FS_I(inode);
4531 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4532 const bool do_opu = f2fs_lfs_mode(sbi);
4533 const loff_t pos = iocb->ki_pos;
4534 const ssize_t count = iov_iter_count(from);
4535 unsigned int dio_flags;
4536 struct iomap_dio *dio;
4539 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4541 if (iocb->ki_flags & IOCB_NOWAIT) {
4542 /* f2fs_convert_inline_inode() and block allocation can block */
4543 if (f2fs_has_inline_data(inode) ||
4544 !f2fs_overwrite_io(inode, pos, count)) {
4549 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4553 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4554 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4559 ret = f2fs_convert_inline_inode(inode);
4563 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4565 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4569 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4570 * the higher-level function iomap_dio_rw() in order to ensure that the
4571 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4573 inc_page_count(sbi, F2FS_DIO_WRITE);
4575 if (pos + count > inode->i_size)
4576 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4577 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4578 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4579 if (IS_ERR_OR_NULL(dio)) {
4580 ret = PTR_ERR_OR_ZERO(dio);
4581 if (ret == -ENOTBLK)
4583 if (ret != -EIOCBQUEUED)
4584 dec_page_count(sbi, F2FS_DIO_WRITE);
4586 ret = iomap_dio_complete(dio);
4590 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4591 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4595 if (pos + ret > inode->i_size)
4596 f2fs_i_size_write(inode, pos + ret);
4598 set_inode_flag(inode, FI_UPDATE_WRITE);
4600 if (iov_iter_count(from)) {
4602 loff_t bufio_start_pos = iocb->ki_pos;
4605 * The direct write was partial, so we need to fall back to a
4606 * buffered write for the remainder.
4609 ret2 = f2fs_buffered_write_iter(iocb, from);
4610 if (iov_iter_count(from))
4611 f2fs_write_failed(inode, iocb->ki_pos);
4616 * Ensure that the pagecache pages are written to disk and
4617 * invalidated to preserve the expected O_DIRECT semantics.
4620 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4624 ret2 = filemap_write_and_wait_range(file->f_mapping,
4629 invalidate_mapping_pages(file->f_mapping,
4630 bufio_start_pos >> PAGE_SHIFT,
4631 bufio_end_pos >> PAGE_SHIFT);
4634 /* iomap_dio_rw() already handled the generic_write_sync(). */
4635 *may_need_sync = false;
4638 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4642 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4644 struct inode *inode = file_inode(iocb->ki_filp);
4645 const loff_t orig_pos = iocb->ki_pos;
4646 const size_t orig_count = iov_iter_count(from);
4649 bool may_need_sync = true;
4653 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4658 if (!f2fs_is_compress_backend_ready(inode)) {
4663 if (iocb->ki_flags & IOCB_NOWAIT) {
4664 if (!inode_trylock(inode)) {
4672 ret = f2fs_write_checks(iocb, from);
4676 /* Determine whether we will do a direct write or a buffered write. */
4677 dio = f2fs_should_use_dio(inode, iocb, from);
4679 /* Possibly preallocate the blocks for the write. */
4680 target_size = iocb->ki_pos + iov_iter_count(from);
4681 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4682 if (preallocated < 0) {
4685 if (trace_f2fs_datawrite_start_enabled()) {
4686 char *p = f2fs_kmalloc(F2FS_I_SB(inode),
4687 PATH_MAX, GFP_KERNEL);
4691 goto skip_write_trace;
4692 path = dentry_path_raw(file_dentry(iocb->ki_filp),
4696 goto skip_write_trace;
4698 trace_f2fs_datawrite_start(inode, orig_pos, orig_count,
4699 current->pid, path, current->comm);
4703 /* Do the actual write. */
4705 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
4706 f2fs_buffered_write_iter(iocb, from);
4708 if (trace_f2fs_datawrite_end_enabled())
4709 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4712 /* Don't leave any preallocated blocks around past i_size. */
4713 if (preallocated && i_size_read(inode) < target_size) {
4714 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4715 filemap_invalidate_lock(inode->i_mapping);
4716 if (!f2fs_truncate(inode))
4717 file_dont_truncate(inode);
4718 filemap_invalidate_unlock(inode->i_mapping);
4719 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4721 file_dont_truncate(inode);
4724 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4726 inode_unlock(inode);
4728 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4729 if (ret > 0 && may_need_sync)
4730 ret = generic_write_sync(iocb, ret);
4734 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4737 struct address_space *mapping;
4738 struct backing_dev_info *bdi;
4739 struct inode *inode = file_inode(filp);
4742 if (advice == POSIX_FADV_SEQUENTIAL) {
4743 if (S_ISFIFO(inode->i_mode))
4746 mapping = filp->f_mapping;
4747 if (!mapping || len < 0)
4750 bdi = inode_to_bdi(mapping->host);
4751 filp->f_ra.ra_pages = bdi->ra_pages *
4752 F2FS_I_SB(inode)->seq_file_ra_mul;
4753 spin_lock(&filp->f_lock);
4754 filp->f_mode &= ~FMODE_RANDOM;
4755 spin_unlock(&filp->f_lock);
4759 err = generic_fadvise(filp, offset, len, advice);
4760 if (!err && advice == POSIX_FADV_DONTNEED &&
4761 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4762 f2fs_compressed_file(inode))
4763 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4768 #ifdef CONFIG_COMPAT
4769 struct compat_f2fs_gc_range {
4774 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4775 struct compat_f2fs_gc_range)
4777 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4779 struct compat_f2fs_gc_range __user *urange;
4780 struct f2fs_gc_range range;
4783 urange = compat_ptr(arg);
4784 err = get_user(range.sync, &urange->sync);
4785 err |= get_user(range.start, &urange->start);
4786 err |= get_user(range.len, &urange->len);
4790 return __f2fs_ioc_gc_range(file, &range);
4793 struct compat_f2fs_move_range {
4799 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4800 struct compat_f2fs_move_range)
4802 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4804 struct compat_f2fs_move_range __user *urange;
4805 struct f2fs_move_range range;
4808 urange = compat_ptr(arg);
4809 err = get_user(range.dst_fd, &urange->dst_fd);
4810 err |= get_user(range.pos_in, &urange->pos_in);
4811 err |= get_user(range.pos_out, &urange->pos_out);
4812 err |= get_user(range.len, &urange->len);
4816 return __f2fs_ioc_move_range(file, &range);
4819 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4821 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4823 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4827 case FS_IOC32_GETVERSION:
4828 cmd = FS_IOC_GETVERSION;
4830 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4831 return f2fs_compat_ioc_gc_range(file, arg);
4832 case F2FS_IOC32_MOVE_RANGE:
4833 return f2fs_compat_ioc_move_range(file, arg);
4834 case F2FS_IOC_START_ATOMIC_WRITE:
4835 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4836 case F2FS_IOC_START_VOLATILE_WRITE:
4837 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4838 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4839 case F2FS_IOC_SHUTDOWN:
4841 case FS_IOC_SET_ENCRYPTION_POLICY:
4842 case FS_IOC_GET_ENCRYPTION_PWSALT:
4843 case FS_IOC_GET_ENCRYPTION_POLICY:
4844 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4845 case FS_IOC_ADD_ENCRYPTION_KEY:
4846 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4847 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4848 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4849 case FS_IOC_GET_ENCRYPTION_NONCE:
4850 case F2FS_IOC_GARBAGE_COLLECT:
4851 case F2FS_IOC_WRITE_CHECKPOINT:
4852 case F2FS_IOC_DEFRAGMENT:
4853 case F2FS_IOC_FLUSH_DEVICE:
4854 case F2FS_IOC_GET_FEATURES:
4855 case F2FS_IOC_GET_PIN_FILE:
4856 case F2FS_IOC_SET_PIN_FILE:
4857 case F2FS_IOC_PRECACHE_EXTENTS:
4858 case F2FS_IOC_RESIZE_FS:
4859 case FS_IOC_ENABLE_VERITY:
4860 case FS_IOC_MEASURE_VERITY:
4861 case FS_IOC_READ_VERITY_METADATA:
4862 case FS_IOC_GETFSLABEL:
4863 case FS_IOC_SETFSLABEL:
4864 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4865 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4866 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4867 case F2FS_IOC_SEC_TRIM_FILE:
4868 case F2FS_IOC_GET_COMPRESS_OPTION:
4869 case F2FS_IOC_SET_COMPRESS_OPTION:
4870 case F2FS_IOC_DECOMPRESS_FILE:
4871 case F2FS_IOC_COMPRESS_FILE:
4874 return -ENOIOCTLCMD;
4876 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4880 const struct file_operations f2fs_file_operations = {
4881 .llseek = f2fs_llseek,
4882 .read_iter = f2fs_file_read_iter,
4883 .write_iter = f2fs_file_write_iter,
4884 .open = f2fs_file_open,
4885 .release = f2fs_release_file,
4886 .mmap = f2fs_file_mmap,
4887 .flush = f2fs_file_flush,
4888 .fsync = f2fs_sync_file,
4889 .fallocate = f2fs_fallocate,
4890 .unlocked_ioctl = f2fs_ioctl,
4891 #ifdef CONFIG_COMPAT
4892 .compat_ioctl = f2fs_compat_ioctl,
4894 .splice_read = generic_file_splice_read,
4895 .splice_write = iter_file_splice_write,
4896 .fadvise = f2fs_file_fadvise,