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);
45 if (ret & VM_FAULT_LOCKED)
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;
534 f2fs_down_read(&F2FS_I(inode)->i_sem);
535 set_inode_flag(inode, FI_MMAP_FILE);
536 f2fs_up_read(&F2FS_I(inode)->i_sem);
541 static int f2fs_file_open(struct inode *inode, struct file *filp)
543 int err = fscrypt_file_open(inode, filp);
548 if (!f2fs_is_compress_backend_ready(inode))
551 err = fsverity_file_open(inode, filp);
555 filp->f_mode |= FMODE_NOWAIT;
557 return dquot_file_open(inode, filp);
560 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
562 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
563 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
565 bool compressed_cluster = false;
566 int cluster_index = 0, valid_blocks = 0;
567 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
568 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
570 addr = get_dnode_addr(dn->inode, dn->node_page) + ofs;
572 /* Assumption: truncateion starts with cluster */
573 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
574 block_t blkaddr = le32_to_cpu(*addr);
576 if (f2fs_compressed_file(dn->inode) &&
577 !(cluster_index & (cluster_size - 1))) {
578 if (compressed_cluster)
579 f2fs_i_compr_blocks_update(dn->inode,
580 valid_blocks, false);
581 compressed_cluster = (blkaddr == COMPRESS_ADDR);
585 if (blkaddr == NULL_ADDR)
588 f2fs_set_data_blkaddr(dn, NULL_ADDR);
590 if (__is_valid_data_blkaddr(blkaddr)) {
591 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
592 DATA_GENERIC_ENHANCE))
594 if (compressed_cluster)
598 f2fs_invalidate_blocks(sbi, blkaddr);
600 if (!released || blkaddr != COMPRESS_ADDR)
604 if (compressed_cluster)
605 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
610 * once we invalidate valid blkaddr in range [ofs, ofs + count],
611 * we will invalidate all blkaddr in the whole range.
613 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
615 f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
616 dec_valid_block_count(sbi, dn->inode, nr_free);
618 dn->ofs_in_node = ofs;
620 f2fs_update_time(sbi, REQ_TIME);
621 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
622 dn->ofs_in_node, nr_free);
625 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
627 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
630 static int truncate_partial_data_page(struct inode *inode, u64 from,
633 loff_t offset = from & (PAGE_SIZE - 1);
634 pgoff_t index = from >> PAGE_SHIFT;
635 struct address_space *mapping = inode->i_mapping;
638 if (!offset && !cache_only)
642 page = find_lock_page(mapping, index);
643 if (page && PageUptodate(page))
645 f2fs_put_page(page, 1);
649 page = f2fs_get_lock_data_page(inode, index, true);
651 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
653 f2fs_wait_on_page_writeback(page, DATA, true, true);
654 zero_user(page, offset, PAGE_SIZE - offset);
656 /* An encrypted inode should have a key and truncate the last page. */
657 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
659 set_page_dirty(page);
660 f2fs_put_page(page, 1);
664 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
666 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
667 struct dnode_of_data dn;
669 int count = 0, err = 0;
671 bool truncate_page = false;
673 trace_f2fs_truncate_blocks_enter(inode, from);
675 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
677 if (free_from >= max_file_blocks(inode))
683 ipage = f2fs_get_node_page(sbi, inode->i_ino);
685 err = PTR_ERR(ipage);
689 if (f2fs_has_inline_data(inode)) {
690 f2fs_truncate_inline_inode(inode, ipage, from);
691 f2fs_put_page(ipage, 1);
692 truncate_page = true;
696 set_new_dnode(&dn, inode, ipage, NULL, 0);
697 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
704 count = ADDRS_PER_PAGE(dn.node_page, inode);
706 count -= dn.ofs_in_node;
707 f2fs_bug_on(sbi, count < 0);
709 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
710 f2fs_truncate_data_blocks_range(&dn, count);
716 err = f2fs_truncate_inode_blocks(inode, free_from);
721 /* lastly zero out the first data page */
723 err = truncate_partial_data_page(inode, from, truncate_page);
725 trace_f2fs_truncate_blocks_exit(inode, err);
729 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
731 u64 free_from = from;
734 #ifdef CONFIG_F2FS_FS_COMPRESSION
736 * for compressed file, only support cluster size
737 * aligned truncation.
739 if (f2fs_compressed_file(inode))
740 free_from = round_up(from,
741 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
744 err = f2fs_do_truncate_blocks(inode, free_from, lock);
748 #ifdef CONFIG_F2FS_FS_COMPRESSION
750 * For compressed file, after release compress blocks, don't allow write
751 * direct, but we should allow write direct after truncate to zero.
753 if (f2fs_compressed_file(inode) && !free_from
754 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
755 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
757 if (from != free_from) {
758 err = f2fs_truncate_partial_cluster(inode, from, lock);
767 int f2fs_truncate(struct inode *inode)
771 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
774 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
775 S_ISLNK(inode->i_mode)))
778 trace_f2fs_truncate(inode);
780 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
781 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
785 err = f2fs_dquot_initialize(inode);
789 /* we should check inline_data size */
790 if (!f2fs_may_inline_data(inode)) {
791 err = f2fs_convert_inline_inode(inode);
796 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
800 inode->i_mtime = inode->i_ctime = current_time(inode);
801 f2fs_mark_inode_dirty_sync(inode, false);
805 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
807 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
809 if (!fscrypt_dio_supported(inode))
811 if (fsverity_active(inode))
813 if (f2fs_compressed_file(inode))
816 /* disallow direct IO if any of devices has unaligned blksize */
817 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
820 * for blkzoned device, fallback direct IO to buffered IO, so
821 * all IOs can be serialized by log-structured write.
823 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE))
825 if (f2fs_lfs_mode(sbi) && rw == WRITE && F2FS_IO_ALIGNED(sbi))
827 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
833 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
834 struct kstat *stat, u32 request_mask, unsigned int query_flags)
836 struct inode *inode = d_inode(path->dentry);
837 struct f2fs_inode_info *fi = F2FS_I(inode);
838 struct f2fs_inode *ri = NULL;
841 if (f2fs_has_extra_attr(inode) &&
842 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
843 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
844 stat->result_mask |= STATX_BTIME;
845 stat->btime.tv_sec = fi->i_crtime.tv_sec;
846 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
850 * Return the DIO alignment restrictions if requested. We only return
851 * this information when requested, since on encrypted files it might
852 * take a fair bit of work to get if the file wasn't opened recently.
854 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
855 * cannot represent that, so in that case we report no DIO support.
857 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
858 unsigned int bsize = i_blocksize(inode);
860 stat->result_mask |= STATX_DIOALIGN;
861 if (!f2fs_force_buffered_io(inode, WRITE)) {
862 stat->dio_mem_align = bsize;
863 stat->dio_offset_align = bsize;
868 if (flags & F2FS_COMPR_FL)
869 stat->attributes |= STATX_ATTR_COMPRESSED;
870 if (flags & F2FS_APPEND_FL)
871 stat->attributes |= STATX_ATTR_APPEND;
872 if (IS_ENCRYPTED(inode))
873 stat->attributes |= STATX_ATTR_ENCRYPTED;
874 if (flags & F2FS_IMMUTABLE_FL)
875 stat->attributes |= STATX_ATTR_IMMUTABLE;
876 if (flags & F2FS_NODUMP_FL)
877 stat->attributes |= STATX_ATTR_NODUMP;
878 if (IS_VERITY(inode))
879 stat->attributes |= STATX_ATTR_VERITY;
881 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
883 STATX_ATTR_ENCRYPTED |
884 STATX_ATTR_IMMUTABLE |
888 generic_fillattr(mnt_userns, inode, stat);
890 /* we need to show initial sectors used for inline_data/dentries */
891 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
892 f2fs_has_inline_dentry(inode))
893 stat->blocks += (stat->size + 511) >> 9;
898 #ifdef CONFIG_F2FS_FS_POSIX_ACL
899 static void __setattr_copy(struct user_namespace *mnt_userns,
900 struct inode *inode, const struct iattr *attr)
902 unsigned int ia_valid = attr->ia_valid;
904 i_uid_update(mnt_userns, attr, inode);
905 i_gid_update(mnt_userns, attr, inode);
906 if (ia_valid & ATTR_ATIME)
907 inode->i_atime = attr->ia_atime;
908 if (ia_valid & ATTR_MTIME)
909 inode->i_mtime = attr->ia_mtime;
910 if (ia_valid & ATTR_CTIME)
911 inode->i_ctime = attr->ia_ctime;
912 if (ia_valid & ATTR_MODE) {
913 umode_t mode = attr->ia_mode;
914 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
916 if (!vfsgid_in_group_p(vfsgid) &&
917 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
919 set_acl_inode(inode, mode);
923 #define __setattr_copy setattr_copy
926 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
929 struct inode *inode = d_inode(dentry);
932 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
935 if (unlikely(IS_IMMUTABLE(inode)))
938 if (unlikely(IS_APPEND(inode) &&
939 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
940 ATTR_GID | ATTR_TIMES_SET))))
943 if ((attr->ia_valid & ATTR_SIZE) &&
944 !f2fs_is_compress_backend_ready(inode))
947 err = setattr_prepare(mnt_userns, dentry, attr);
951 err = fscrypt_prepare_setattr(dentry, attr);
955 err = fsverity_prepare_setattr(dentry, attr);
959 if (is_quota_modification(mnt_userns, inode, attr)) {
960 err = f2fs_dquot_initialize(inode);
964 if (i_uid_needs_update(mnt_userns, attr, inode) ||
965 i_gid_needs_update(mnt_userns, attr, inode)) {
966 f2fs_lock_op(F2FS_I_SB(inode));
967 err = dquot_transfer(mnt_userns, inode, attr);
969 set_sbi_flag(F2FS_I_SB(inode),
970 SBI_QUOTA_NEED_REPAIR);
971 f2fs_unlock_op(F2FS_I_SB(inode));
975 * update uid/gid under lock_op(), so that dquot and inode can
976 * be updated atomically.
978 i_uid_update(mnt_userns, attr, inode);
979 i_gid_update(mnt_userns, attr, inode);
980 f2fs_mark_inode_dirty_sync(inode, true);
981 f2fs_unlock_op(F2FS_I_SB(inode));
984 if (attr->ia_valid & ATTR_SIZE) {
985 loff_t old_size = i_size_read(inode);
987 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
989 * should convert inline inode before i_size_write to
990 * keep smaller than inline_data size with inline flag.
992 err = f2fs_convert_inline_inode(inode);
997 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
998 filemap_invalidate_lock(inode->i_mapping);
1000 truncate_setsize(inode, attr->ia_size);
1002 if (attr->ia_size <= old_size)
1003 err = f2fs_truncate(inode);
1005 * do not trim all blocks after i_size if target size is
1006 * larger than i_size.
1008 filemap_invalidate_unlock(inode->i_mapping);
1009 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1013 spin_lock(&F2FS_I(inode)->i_size_lock);
1014 inode->i_mtime = inode->i_ctime = current_time(inode);
1015 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1016 spin_unlock(&F2FS_I(inode)->i_size_lock);
1019 __setattr_copy(mnt_userns, inode, attr);
1021 if (attr->ia_valid & ATTR_MODE) {
1022 err = posix_acl_chmod(mnt_userns, inode, f2fs_get_inode_mode(inode));
1024 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1026 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1027 clear_inode_flag(inode, FI_ACL_MODE);
1031 /* file size may changed here */
1032 f2fs_mark_inode_dirty_sync(inode, true);
1034 /* inode change will produce dirty node pages flushed by checkpoint */
1035 f2fs_balance_fs(F2FS_I_SB(inode), true);
1040 const struct inode_operations f2fs_file_inode_operations = {
1041 .getattr = f2fs_getattr,
1042 .setattr = f2fs_setattr,
1043 .get_acl = f2fs_get_acl,
1044 .set_acl = f2fs_set_acl,
1045 .listxattr = f2fs_listxattr,
1046 .fiemap = f2fs_fiemap,
1047 .fileattr_get = f2fs_fileattr_get,
1048 .fileattr_set = f2fs_fileattr_set,
1051 static int fill_zero(struct inode *inode, pgoff_t index,
1052 loff_t start, loff_t len)
1054 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1060 f2fs_balance_fs(sbi, true);
1063 page = f2fs_get_new_data_page(inode, NULL, index, false);
1064 f2fs_unlock_op(sbi);
1067 return PTR_ERR(page);
1069 f2fs_wait_on_page_writeback(page, DATA, true, true);
1070 zero_user(page, start, len);
1071 set_page_dirty(page);
1072 f2fs_put_page(page, 1);
1076 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1080 while (pg_start < pg_end) {
1081 struct dnode_of_data dn;
1082 pgoff_t end_offset, count;
1084 set_new_dnode(&dn, inode, NULL, NULL, 0);
1085 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1087 if (err == -ENOENT) {
1088 pg_start = f2fs_get_next_page_offset(&dn,
1095 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1096 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1098 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1100 f2fs_truncate_data_blocks_range(&dn, count);
1101 f2fs_put_dnode(&dn);
1108 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1110 pgoff_t pg_start, pg_end;
1111 loff_t off_start, off_end;
1114 ret = f2fs_convert_inline_inode(inode);
1118 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1119 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1121 off_start = offset & (PAGE_SIZE - 1);
1122 off_end = (offset + len) & (PAGE_SIZE - 1);
1124 if (pg_start == pg_end) {
1125 ret = fill_zero(inode, pg_start, off_start,
1126 off_end - off_start);
1131 ret = fill_zero(inode, pg_start++, off_start,
1132 PAGE_SIZE - off_start);
1137 ret = fill_zero(inode, pg_end, 0, off_end);
1142 if (pg_start < pg_end) {
1143 loff_t blk_start, blk_end;
1144 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1146 f2fs_balance_fs(sbi, true);
1148 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1149 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1151 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1152 filemap_invalidate_lock(inode->i_mapping);
1154 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1157 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1158 f2fs_unlock_op(sbi);
1160 filemap_invalidate_unlock(inode->i_mapping);
1161 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1168 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1169 int *do_replace, pgoff_t off, pgoff_t len)
1171 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1172 struct dnode_of_data dn;
1176 set_new_dnode(&dn, inode, NULL, NULL, 0);
1177 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1178 if (ret && ret != -ENOENT) {
1180 } else if (ret == -ENOENT) {
1181 if (dn.max_level == 0)
1183 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1184 dn.ofs_in_node, len);
1190 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1191 dn.ofs_in_node, len);
1192 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1193 *blkaddr = f2fs_data_blkaddr(&dn);
1195 if (__is_valid_data_blkaddr(*blkaddr) &&
1196 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1197 DATA_GENERIC_ENHANCE)) {
1198 f2fs_put_dnode(&dn);
1199 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1200 return -EFSCORRUPTED;
1203 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1205 if (f2fs_lfs_mode(sbi)) {
1206 f2fs_put_dnode(&dn);
1210 /* do not invalidate this block address */
1211 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1215 f2fs_put_dnode(&dn);
1224 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1225 int *do_replace, pgoff_t off, int len)
1227 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1228 struct dnode_of_data dn;
1231 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1232 if (*do_replace == 0)
1235 set_new_dnode(&dn, inode, NULL, NULL, 0);
1236 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1238 dec_valid_block_count(sbi, inode, 1);
1239 f2fs_invalidate_blocks(sbi, *blkaddr);
1241 f2fs_update_data_blkaddr(&dn, *blkaddr);
1243 f2fs_put_dnode(&dn);
1248 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1249 block_t *blkaddr, int *do_replace,
1250 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1252 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1257 if (blkaddr[i] == NULL_ADDR && !full) {
1262 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1263 struct dnode_of_data dn;
1264 struct node_info ni;
1268 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1269 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1273 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1275 f2fs_put_dnode(&dn);
1279 ilen = min((pgoff_t)
1280 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1281 dn.ofs_in_node, len - i);
1283 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1284 f2fs_truncate_data_blocks_range(&dn, 1);
1286 if (do_replace[i]) {
1287 f2fs_i_blocks_write(src_inode,
1289 f2fs_i_blocks_write(dst_inode,
1291 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1292 blkaddr[i], ni.version, true, false);
1298 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1299 if (dst_inode->i_size < new_size)
1300 f2fs_i_size_write(dst_inode, new_size);
1301 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1303 f2fs_put_dnode(&dn);
1305 struct page *psrc, *pdst;
1307 psrc = f2fs_get_lock_data_page(src_inode,
1310 return PTR_ERR(psrc);
1311 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1314 f2fs_put_page(psrc, 1);
1315 return PTR_ERR(pdst);
1317 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1318 set_page_dirty(pdst);
1319 set_page_private_gcing(pdst);
1320 f2fs_put_page(pdst, 1);
1321 f2fs_put_page(psrc, 1);
1323 ret = f2fs_truncate_hole(src_inode,
1324 src + i, src + i + 1);
1333 static int __exchange_data_block(struct inode *src_inode,
1334 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1335 pgoff_t len, bool full)
1337 block_t *src_blkaddr;
1343 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1345 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1346 array_size(olen, sizeof(block_t)),
1351 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1352 array_size(olen, sizeof(int)),
1355 kvfree(src_blkaddr);
1359 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1360 do_replace, src, olen);
1364 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1365 do_replace, src, dst, olen, full);
1373 kvfree(src_blkaddr);
1379 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1380 kvfree(src_blkaddr);
1385 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1387 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1388 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1389 pgoff_t start = offset >> PAGE_SHIFT;
1390 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1393 f2fs_balance_fs(sbi, true);
1395 /* avoid gc operation during block exchange */
1396 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1397 filemap_invalidate_lock(inode->i_mapping);
1400 f2fs_drop_extent_tree(inode);
1401 truncate_pagecache(inode, offset);
1402 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1403 f2fs_unlock_op(sbi);
1405 filemap_invalidate_unlock(inode->i_mapping);
1406 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1410 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1415 if (offset + len >= i_size_read(inode))
1418 /* collapse range should be aligned to block size of f2fs. */
1419 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1422 ret = f2fs_convert_inline_inode(inode);
1426 /* write out all dirty pages from offset */
1427 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1431 ret = f2fs_do_collapse(inode, offset, len);
1435 /* write out all moved pages, if possible */
1436 filemap_invalidate_lock(inode->i_mapping);
1437 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1438 truncate_pagecache(inode, offset);
1440 new_size = i_size_read(inode) - len;
1441 ret = f2fs_truncate_blocks(inode, new_size, true);
1442 filemap_invalidate_unlock(inode->i_mapping);
1444 f2fs_i_size_write(inode, new_size);
1448 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1451 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1452 pgoff_t index = start;
1453 unsigned int ofs_in_node = dn->ofs_in_node;
1457 for (; index < end; index++, dn->ofs_in_node++) {
1458 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1462 dn->ofs_in_node = ofs_in_node;
1463 ret = f2fs_reserve_new_blocks(dn, count);
1467 dn->ofs_in_node = ofs_in_node;
1468 for (index = start; index < end; index++, dn->ofs_in_node++) {
1469 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1471 * f2fs_reserve_new_blocks will not guarantee entire block
1474 if (dn->data_blkaddr == NULL_ADDR) {
1479 if (dn->data_blkaddr == NEW_ADDR)
1482 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1483 DATA_GENERIC_ENHANCE)) {
1484 ret = -EFSCORRUPTED;
1485 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1489 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1490 f2fs_set_data_blkaddr(dn, NEW_ADDR);
1493 f2fs_update_read_extent_cache_range(dn, start, 0, index - start);
1498 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1501 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1502 struct address_space *mapping = inode->i_mapping;
1503 pgoff_t index, pg_start, pg_end;
1504 loff_t new_size = i_size_read(inode);
1505 loff_t off_start, off_end;
1508 ret = inode_newsize_ok(inode, (len + offset));
1512 ret = f2fs_convert_inline_inode(inode);
1516 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1520 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1521 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1523 off_start = offset & (PAGE_SIZE - 1);
1524 off_end = (offset + len) & (PAGE_SIZE - 1);
1526 if (pg_start == pg_end) {
1527 ret = fill_zero(inode, pg_start, off_start,
1528 off_end - off_start);
1532 new_size = max_t(loff_t, new_size, offset + len);
1535 ret = fill_zero(inode, pg_start++, off_start,
1536 PAGE_SIZE - off_start);
1540 new_size = max_t(loff_t, new_size,
1541 (loff_t)pg_start << PAGE_SHIFT);
1544 for (index = pg_start; index < pg_end;) {
1545 struct dnode_of_data dn;
1546 unsigned int end_offset;
1549 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1550 filemap_invalidate_lock(mapping);
1552 truncate_pagecache_range(inode,
1553 (loff_t)index << PAGE_SHIFT,
1554 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1558 set_new_dnode(&dn, inode, NULL, NULL, 0);
1559 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1561 f2fs_unlock_op(sbi);
1562 filemap_invalidate_unlock(mapping);
1563 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1567 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1568 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1570 ret = f2fs_do_zero_range(&dn, index, end);
1571 f2fs_put_dnode(&dn);
1573 f2fs_unlock_op(sbi);
1574 filemap_invalidate_unlock(mapping);
1575 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1577 f2fs_balance_fs(sbi, dn.node_changed);
1583 new_size = max_t(loff_t, new_size,
1584 (loff_t)index << PAGE_SHIFT);
1588 ret = fill_zero(inode, pg_end, 0, off_end);
1592 new_size = max_t(loff_t, new_size, offset + len);
1597 if (new_size > i_size_read(inode)) {
1598 if (mode & FALLOC_FL_KEEP_SIZE)
1599 file_set_keep_isize(inode);
1601 f2fs_i_size_write(inode, new_size);
1606 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1608 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1609 struct address_space *mapping = inode->i_mapping;
1610 pgoff_t nr, pg_start, pg_end, delta, idx;
1614 new_size = i_size_read(inode) + len;
1615 ret = inode_newsize_ok(inode, new_size);
1619 if (offset >= i_size_read(inode))
1622 /* insert range should be aligned to block size of f2fs. */
1623 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1626 ret = f2fs_convert_inline_inode(inode);
1630 f2fs_balance_fs(sbi, true);
1632 filemap_invalidate_lock(mapping);
1633 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1634 filemap_invalidate_unlock(mapping);
1638 /* write out all dirty pages from offset */
1639 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1643 pg_start = offset >> PAGE_SHIFT;
1644 pg_end = (offset + len) >> PAGE_SHIFT;
1645 delta = pg_end - pg_start;
1646 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1648 /* avoid gc operation during block exchange */
1649 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1650 filemap_invalidate_lock(mapping);
1651 truncate_pagecache(inode, offset);
1653 while (!ret && idx > pg_start) {
1654 nr = idx - pg_start;
1660 f2fs_drop_extent_tree(inode);
1662 ret = __exchange_data_block(inode, inode, idx,
1663 idx + delta, nr, false);
1664 f2fs_unlock_op(sbi);
1666 filemap_invalidate_unlock(mapping);
1667 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1669 /* write out all moved pages, if possible */
1670 filemap_invalidate_lock(mapping);
1671 filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1672 truncate_pagecache(inode, offset);
1673 filemap_invalidate_unlock(mapping);
1676 f2fs_i_size_write(inode, new_size);
1680 static int expand_inode_data(struct inode *inode, loff_t offset,
1681 loff_t len, int mode)
1683 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1684 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1685 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1686 .m_may_create = true };
1687 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1688 .init_gc_type = FG_GC,
1689 .should_migrate_blocks = false,
1690 .err_gc_skipped = true,
1691 .nr_free_secs = 0 };
1692 pgoff_t pg_start, pg_end;
1693 loff_t new_size = i_size_read(inode);
1695 block_t expanded = 0;
1698 err = inode_newsize_ok(inode, (len + offset));
1702 err = f2fs_convert_inline_inode(inode);
1706 f2fs_balance_fs(sbi, true);
1708 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1709 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1710 off_end = (offset + len) & (PAGE_SIZE - 1);
1712 map.m_lblk = pg_start;
1713 map.m_len = pg_end - pg_start;
1720 if (f2fs_is_pinned_file(inode)) {
1721 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1722 block_t sec_len = roundup(map.m_len, sec_blks);
1724 map.m_len = sec_blks;
1726 if (has_not_enough_free_secs(sbi, 0,
1727 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1728 f2fs_down_write(&sbi->gc_lock);
1729 err = f2fs_gc(sbi, &gc_control);
1730 if (err && err != -ENODATA)
1734 f2fs_down_write(&sbi->pin_sem);
1737 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1738 f2fs_unlock_op(sbi);
1740 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1741 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1742 file_dont_truncate(inode);
1744 f2fs_up_write(&sbi->pin_sem);
1746 expanded += map.m_len;
1747 sec_len -= map.m_len;
1748 map.m_lblk += map.m_len;
1749 if (!err && sec_len)
1752 map.m_len = expanded;
1754 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1755 expanded = map.m_len;
1764 last_off = pg_start + expanded - 1;
1766 /* update new size to the failed position */
1767 new_size = (last_off == pg_end) ? offset + len :
1768 (loff_t)(last_off + 1) << PAGE_SHIFT;
1770 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1773 if (new_size > i_size_read(inode)) {
1774 if (mode & FALLOC_FL_KEEP_SIZE)
1775 file_set_keep_isize(inode);
1777 f2fs_i_size_write(inode, new_size);
1783 static long f2fs_fallocate(struct file *file, int mode,
1784 loff_t offset, loff_t len)
1786 struct inode *inode = file_inode(file);
1789 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1791 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1793 if (!f2fs_is_compress_backend_ready(inode))
1796 /* f2fs only support ->fallocate for regular file */
1797 if (!S_ISREG(inode->i_mode))
1800 if (IS_ENCRYPTED(inode) &&
1801 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1805 * Pinned file should not support partial trucation since the block
1806 * can be used by applications.
1808 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1809 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1810 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1813 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1814 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1815 FALLOC_FL_INSERT_RANGE))
1820 ret = file_modified(file);
1824 if (mode & FALLOC_FL_PUNCH_HOLE) {
1825 if (offset >= inode->i_size)
1828 ret = punch_hole(inode, offset, len);
1829 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1830 ret = f2fs_collapse_range(inode, offset, len);
1831 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1832 ret = f2fs_zero_range(inode, offset, len, mode);
1833 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1834 ret = f2fs_insert_range(inode, offset, len);
1836 ret = expand_inode_data(inode, offset, len, mode);
1840 inode->i_mtime = inode->i_ctime = current_time(inode);
1841 f2fs_mark_inode_dirty_sync(inode, false);
1842 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1846 inode_unlock(inode);
1848 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1852 static int f2fs_release_file(struct inode *inode, struct file *filp)
1855 * f2fs_relase_file is called at every close calls. So we should
1856 * not drop any inmemory pages by close called by other process.
1858 if (!(filp->f_mode & FMODE_WRITE) ||
1859 atomic_read(&inode->i_writecount) != 1)
1863 f2fs_abort_atomic_write(inode, true);
1864 inode_unlock(inode);
1869 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1871 struct inode *inode = file_inode(file);
1874 * If the process doing a transaction is crashed, we should do
1875 * roll-back. Otherwise, other reader/write can see corrupted database
1876 * until all the writers close its file. Since this should be done
1877 * before dropping file lock, it needs to do in ->flush.
1879 if (F2FS_I(inode)->atomic_write_task == current &&
1880 (current->flags & PF_EXITING)) {
1882 f2fs_abort_atomic_write(inode, true);
1883 inode_unlock(inode);
1889 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1891 struct f2fs_inode_info *fi = F2FS_I(inode);
1892 u32 masked_flags = fi->i_flags & mask;
1894 /* mask can be shrunk by flags_valid selector */
1897 /* Is it quota file? Do not allow user to mess with it */
1898 if (IS_NOQUOTA(inode))
1901 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1902 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1904 if (!f2fs_empty_dir(inode))
1908 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1909 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1911 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1915 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1916 if (masked_flags & F2FS_COMPR_FL) {
1917 if (!f2fs_disable_compressed_file(inode))
1920 /* try to convert inline_data to support compression */
1921 int err = f2fs_convert_inline_inode(inode);
1925 f2fs_down_write(&F2FS_I(inode)->i_sem);
1926 if (!f2fs_may_compress(inode) ||
1927 (S_ISREG(inode->i_mode) &&
1928 F2FS_HAS_BLOCKS(inode))) {
1929 f2fs_up_write(&F2FS_I(inode)->i_sem);
1932 err = set_compress_context(inode);
1933 f2fs_up_write(&F2FS_I(inode)->i_sem);
1940 fi->i_flags = iflags | (fi->i_flags & ~mask);
1941 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1942 (fi->i_flags & F2FS_NOCOMP_FL));
1944 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1945 set_inode_flag(inode, FI_PROJ_INHERIT);
1947 clear_inode_flag(inode, FI_PROJ_INHERIT);
1949 inode->i_ctime = current_time(inode);
1950 f2fs_set_inode_flags(inode);
1951 f2fs_mark_inode_dirty_sync(inode, true);
1955 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1958 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1959 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1960 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1961 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1963 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1964 * FS_IOC_FSSETXATTR is done by the VFS.
1967 static const struct {
1970 } f2fs_fsflags_map[] = {
1971 { F2FS_COMPR_FL, FS_COMPR_FL },
1972 { F2FS_SYNC_FL, FS_SYNC_FL },
1973 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1974 { F2FS_APPEND_FL, FS_APPEND_FL },
1975 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1976 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1977 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1978 { F2FS_INDEX_FL, FS_INDEX_FL },
1979 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1980 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1981 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1984 #define F2FS_GETTABLE_FS_FL ( \
1994 FS_PROJINHERIT_FL | \
1996 FS_INLINE_DATA_FL | \
2001 #define F2FS_SETTABLE_FS_FL ( \
2010 FS_PROJINHERIT_FL | \
2013 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2014 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2019 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2020 if (iflags & f2fs_fsflags_map[i].iflag)
2021 fsflags |= f2fs_fsflags_map[i].fsflag;
2026 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2027 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2032 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2033 if (fsflags & f2fs_fsflags_map[i].fsflag)
2034 iflags |= f2fs_fsflags_map[i].iflag;
2039 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2041 struct inode *inode = file_inode(filp);
2043 return put_user(inode->i_generation, (int __user *)arg);
2046 static int f2fs_ioc_start_atomic_write(struct file *filp)
2048 struct inode *inode = file_inode(filp);
2049 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2050 struct f2fs_inode_info *fi = F2FS_I(inode);
2051 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2052 struct inode *pinode;
2056 if (!inode_owner_or_capable(mnt_userns, inode))
2059 if (!S_ISREG(inode->i_mode))
2062 if (filp->f_flags & O_DIRECT)
2065 ret = mnt_want_write_file(filp);
2071 if (!f2fs_disable_compressed_file(inode)) {
2076 if (f2fs_is_atomic_file(inode))
2079 ret = f2fs_convert_inline_inode(inode);
2083 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2086 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2087 * f2fs_is_atomic_file.
2089 if (get_dirty_pages(inode))
2090 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2091 inode->i_ino, get_dirty_pages(inode));
2092 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2094 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2098 /* Check if the inode already has a COW inode */
2099 if (fi->cow_inode == NULL) {
2100 /* Create a COW inode for atomic write */
2101 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2102 if (IS_ERR(pinode)) {
2103 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2104 ret = PTR_ERR(pinode);
2108 ret = f2fs_get_tmpfile(mnt_userns, pinode, &fi->cow_inode);
2111 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2115 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2116 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2118 /* Reuse the already created COW inode */
2119 ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2121 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2126 f2fs_write_inode(inode, NULL);
2128 isize = i_size_read(inode);
2129 fi->original_i_size = isize;
2130 f2fs_i_size_write(fi->cow_inode, isize);
2132 stat_inc_atomic_inode(inode);
2134 set_inode_flag(inode, FI_ATOMIC_FILE);
2135 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2137 f2fs_update_time(sbi, REQ_TIME);
2138 fi->atomic_write_task = current;
2139 stat_update_max_atomic_write(inode);
2140 fi->atomic_write_cnt = 0;
2142 inode_unlock(inode);
2143 mnt_drop_write_file(filp);
2147 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2149 struct inode *inode = file_inode(filp);
2150 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2153 if (!inode_owner_or_capable(mnt_userns, inode))
2156 ret = mnt_want_write_file(filp);
2160 f2fs_balance_fs(F2FS_I_SB(inode), true);
2164 if (f2fs_is_atomic_file(inode)) {
2165 ret = f2fs_commit_atomic_write(inode);
2167 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2169 f2fs_abort_atomic_write(inode, ret);
2171 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2174 inode_unlock(inode);
2175 mnt_drop_write_file(filp);
2179 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2181 struct inode *inode = file_inode(filp);
2182 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2185 if (!inode_owner_or_capable(mnt_userns, inode))
2188 ret = mnt_want_write_file(filp);
2194 f2fs_abort_atomic_write(inode, true);
2196 inode_unlock(inode);
2198 mnt_drop_write_file(filp);
2199 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2203 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2205 struct inode *inode = file_inode(filp);
2206 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2207 struct super_block *sb = sbi->sb;
2211 if (!capable(CAP_SYS_ADMIN))
2214 if (get_user(in, (__u32 __user *)arg))
2217 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2218 ret = mnt_want_write_file(filp);
2220 if (ret == -EROFS) {
2222 f2fs_stop_checkpoint(sbi, false,
2223 STOP_CP_REASON_SHUTDOWN);
2224 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2225 trace_f2fs_shutdown(sbi, in, ret);
2232 case F2FS_GOING_DOWN_FULLSYNC:
2233 ret = freeze_bdev(sb->s_bdev);
2236 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2237 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2238 thaw_bdev(sb->s_bdev);
2240 case F2FS_GOING_DOWN_METASYNC:
2241 /* do checkpoint only */
2242 ret = f2fs_sync_fs(sb, 1);
2245 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2246 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2248 case F2FS_GOING_DOWN_NOSYNC:
2249 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2250 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2252 case F2FS_GOING_DOWN_METAFLUSH:
2253 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2254 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2255 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2257 case F2FS_GOING_DOWN_NEED_FSCK:
2258 set_sbi_flag(sbi, SBI_NEED_FSCK);
2259 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2260 set_sbi_flag(sbi, SBI_IS_DIRTY);
2261 /* do checkpoint only */
2262 ret = f2fs_sync_fs(sb, 1);
2269 f2fs_stop_gc_thread(sbi);
2270 f2fs_stop_discard_thread(sbi);
2272 f2fs_drop_discard_cmd(sbi);
2273 clear_opt(sbi, DISCARD);
2275 f2fs_update_time(sbi, REQ_TIME);
2277 if (in != F2FS_GOING_DOWN_FULLSYNC)
2278 mnt_drop_write_file(filp);
2280 trace_f2fs_shutdown(sbi, in, ret);
2285 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2287 struct inode *inode = file_inode(filp);
2288 struct super_block *sb = inode->i_sb;
2289 struct fstrim_range range;
2292 if (!capable(CAP_SYS_ADMIN))
2295 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2298 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2302 ret = mnt_want_write_file(filp);
2306 range.minlen = max((unsigned int)range.minlen,
2307 bdev_discard_granularity(sb->s_bdev));
2308 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2309 mnt_drop_write_file(filp);
2313 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2316 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2320 static bool uuid_is_nonzero(__u8 u[16])
2324 for (i = 0; i < 16; i++)
2330 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2332 struct inode *inode = file_inode(filp);
2334 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2337 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2339 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2342 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2344 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2346 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2349 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2351 struct inode *inode = file_inode(filp);
2352 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2353 u8 encrypt_pw_salt[16];
2356 if (!f2fs_sb_has_encrypt(sbi))
2359 err = mnt_want_write_file(filp);
2363 f2fs_down_write(&sbi->sb_lock);
2365 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2368 /* update superblock with uuid */
2369 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2371 err = f2fs_commit_super(sbi, false);
2374 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2378 memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2380 f2fs_up_write(&sbi->sb_lock);
2381 mnt_drop_write_file(filp);
2383 if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2389 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2392 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2395 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2398 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2400 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2403 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2406 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2408 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2411 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2414 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2417 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2420 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2423 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2426 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2429 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2432 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2434 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2437 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2440 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2442 struct inode *inode = file_inode(filp);
2443 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2444 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2446 .should_migrate_blocks = false,
2447 .nr_free_secs = 0 };
2451 if (!capable(CAP_SYS_ADMIN))
2454 if (get_user(sync, (__u32 __user *)arg))
2457 if (f2fs_readonly(sbi->sb))
2460 ret = mnt_want_write_file(filp);
2465 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2470 f2fs_down_write(&sbi->gc_lock);
2473 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2474 gc_control.err_gc_skipped = sync;
2475 ret = f2fs_gc(sbi, &gc_control);
2477 mnt_drop_write_file(filp);
2481 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2483 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2484 struct f2fs_gc_control gc_control = {
2485 .init_gc_type = range->sync ? FG_GC : BG_GC,
2487 .should_migrate_blocks = false,
2488 .err_gc_skipped = range->sync,
2489 .nr_free_secs = 0 };
2493 if (!capable(CAP_SYS_ADMIN))
2495 if (f2fs_readonly(sbi->sb))
2498 end = range->start + range->len;
2499 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2500 end >= MAX_BLKADDR(sbi))
2503 ret = mnt_want_write_file(filp);
2509 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2514 f2fs_down_write(&sbi->gc_lock);
2517 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2518 ret = f2fs_gc(sbi, &gc_control);
2524 range->start += CAP_BLKS_PER_SEC(sbi);
2525 if (range->start <= end)
2528 mnt_drop_write_file(filp);
2532 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2534 struct f2fs_gc_range range;
2536 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2539 return __f2fs_ioc_gc_range(filp, &range);
2542 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2544 struct inode *inode = file_inode(filp);
2545 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2548 if (!capable(CAP_SYS_ADMIN))
2551 if (f2fs_readonly(sbi->sb))
2554 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2555 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2559 ret = mnt_want_write_file(filp);
2563 ret = f2fs_sync_fs(sbi->sb, 1);
2565 mnt_drop_write_file(filp);
2569 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2571 struct f2fs_defragment *range)
2573 struct inode *inode = file_inode(filp);
2574 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2575 .m_seg_type = NO_CHECK_TYPE,
2576 .m_may_create = false };
2577 struct extent_info ei = {0, };
2578 pgoff_t pg_start, pg_end, next_pgofs;
2579 unsigned int blk_per_seg = sbi->blocks_per_seg;
2580 unsigned int total = 0, sec_num;
2581 block_t blk_end = 0;
2582 bool fragmented = false;
2585 pg_start = range->start >> PAGE_SHIFT;
2586 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2588 f2fs_balance_fs(sbi, true);
2592 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
2597 /* if in-place-update policy is enabled, don't waste time here */
2598 set_inode_flag(inode, FI_OPU_WRITE);
2599 if (f2fs_should_update_inplace(inode, NULL)) {
2604 /* writeback all dirty pages in the range */
2605 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2606 range->start + range->len - 1);
2611 * lookup mapping info in extent cache, skip defragmenting if physical
2612 * block addresses are continuous.
2614 if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
2615 if (ei.fofs + ei.len >= pg_end)
2619 map.m_lblk = pg_start;
2620 map.m_next_pgofs = &next_pgofs;
2623 * lookup mapping info in dnode page cache, skip defragmenting if all
2624 * physical block addresses are continuous even if there are hole(s)
2625 * in logical blocks.
2627 while (map.m_lblk < pg_end) {
2628 map.m_len = pg_end - map.m_lblk;
2629 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2633 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2634 map.m_lblk = next_pgofs;
2638 if (blk_end && blk_end != map.m_pblk)
2641 /* record total count of block that we're going to move */
2644 blk_end = map.m_pblk + map.m_len;
2646 map.m_lblk += map.m_len;
2654 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2657 * make sure there are enough free section for LFS allocation, this can
2658 * avoid defragment running in SSR mode when free section are allocated
2661 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2666 map.m_lblk = pg_start;
2667 map.m_len = pg_end - pg_start;
2670 while (map.m_lblk < pg_end) {
2675 map.m_len = pg_end - map.m_lblk;
2676 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2680 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2681 map.m_lblk = next_pgofs;
2685 set_inode_flag(inode, FI_SKIP_WRITES);
2688 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2691 page = f2fs_get_lock_data_page(inode, idx, true);
2693 err = PTR_ERR(page);
2697 set_page_dirty(page);
2698 set_page_private_gcing(page);
2699 f2fs_put_page(page, 1);
2708 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2711 clear_inode_flag(inode, FI_SKIP_WRITES);
2713 err = filemap_fdatawrite(inode->i_mapping);
2718 clear_inode_flag(inode, FI_SKIP_WRITES);
2720 clear_inode_flag(inode, FI_OPU_WRITE);
2722 inode_unlock(inode);
2724 range->len = (u64)total << PAGE_SHIFT;
2728 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2730 struct inode *inode = file_inode(filp);
2731 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2732 struct f2fs_defragment range;
2735 if (!capable(CAP_SYS_ADMIN))
2738 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2741 if (f2fs_readonly(sbi->sb))
2744 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2748 /* verify alignment of offset & size */
2749 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2752 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2753 max_file_blocks(inode)))
2756 err = mnt_want_write_file(filp);
2760 err = f2fs_defragment_range(sbi, filp, &range);
2761 mnt_drop_write_file(filp);
2763 f2fs_update_time(sbi, REQ_TIME);
2767 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2774 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2775 struct file *file_out, loff_t pos_out, size_t len)
2777 struct inode *src = file_inode(file_in);
2778 struct inode *dst = file_inode(file_out);
2779 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2780 size_t olen = len, dst_max_i_size = 0;
2784 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2785 src->i_sb != dst->i_sb)
2788 if (unlikely(f2fs_readonly(src->i_sb)))
2791 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2794 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2797 if (pos_out < 0 || pos_in < 0)
2801 if (pos_in == pos_out)
2803 if (pos_out > pos_in && pos_out < pos_in + len)
2810 if (!inode_trylock(dst))
2814 if (f2fs_compressed_file(src) || f2fs_compressed_file(dst)) {
2820 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2823 olen = len = src->i_size - pos_in;
2824 if (pos_in + len == src->i_size)
2825 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2831 dst_osize = dst->i_size;
2832 if (pos_out + olen > dst->i_size)
2833 dst_max_i_size = pos_out + olen;
2835 /* verify the end result is block aligned */
2836 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2837 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2838 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2841 ret = f2fs_convert_inline_inode(src);
2845 ret = f2fs_convert_inline_inode(dst);
2849 /* write out all dirty pages from offset */
2850 ret = filemap_write_and_wait_range(src->i_mapping,
2851 pos_in, pos_in + len);
2855 ret = filemap_write_and_wait_range(dst->i_mapping,
2856 pos_out, pos_out + len);
2860 f2fs_balance_fs(sbi, true);
2862 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2865 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2870 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2871 pos_out >> F2FS_BLKSIZE_BITS,
2872 len >> F2FS_BLKSIZE_BITS, false);
2876 f2fs_i_size_write(dst, dst_max_i_size);
2877 else if (dst_osize != dst->i_size)
2878 f2fs_i_size_write(dst, dst_osize);
2880 f2fs_unlock_op(sbi);
2883 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2885 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2894 static int __f2fs_ioc_move_range(struct file *filp,
2895 struct f2fs_move_range *range)
2900 if (!(filp->f_mode & FMODE_READ) ||
2901 !(filp->f_mode & FMODE_WRITE))
2904 dst = fdget(range->dst_fd);
2908 if (!(dst.file->f_mode & FMODE_WRITE)) {
2913 err = mnt_want_write_file(filp);
2917 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2918 range->pos_out, range->len);
2920 mnt_drop_write_file(filp);
2926 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2928 struct f2fs_move_range range;
2930 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2933 return __f2fs_ioc_move_range(filp, &range);
2936 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2938 struct inode *inode = file_inode(filp);
2939 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2940 struct sit_info *sm = SIT_I(sbi);
2941 unsigned int start_segno = 0, end_segno = 0;
2942 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2943 struct f2fs_flush_device range;
2944 struct f2fs_gc_control gc_control = {
2945 .init_gc_type = FG_GC,
2946 .should_migrate_blocks = true,
2947 .err_gc_skipped = true,
2948 .nr_free_secs = 0 };
2951 if (!capable(CAP_SYS_ADMIN))
2954 if (f2fs_readonly(sbi->sb))
2957 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2960 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2964 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2965 __is_large_section(sbi)) {
2966 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2967 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2971 ret = mnt_want_write_file(filp);
2975 if (range.dev_num != 0)
2976 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2977 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2979 start_segno = sm->last_victim[FLUSH_DEVICE];
2980 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2981 start_segno = dev_start_segno;
2982 end_segno = min(start_segno + range.segments, dev_end_segno);
2984 while (start_segno < end_segno) {
2985 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2989 sm->last_victim[GC_CB] = end_segno + 1;
2990 sm->last_victim[GC_GREEDY] = end_segno + 1;
2991 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2993 gc_control.victim_segno = start_segno;
2994 ret = f2fs_gc(sbi, &gc_control);
3002 mnt_drop_write_file(filp);
3006 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
3008 struct inode *inode = file_inode(filp);
3009 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3011 /* Must validate to set it with SQLite behavior in Android. */
3012 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3014 return put_user(sb_feature, (u32 __user *)arg);
3018 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3020 struct dquot *transfer_to[MAXQUOTAS] = {};
3021 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3022 struct super_block *sb = sbi->sb;
3025 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3026 if (IS_ERR(transfer_to[PRJQUOTA]))
3027 return PTR_ERR(transfer_to[PRJQUOTA]);
3029 err = __dquot_transfer(inode, transfer_to);
3031 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3032 dqput(transfer_to[PRJQUOTA]);
3036 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3038 struct f2fs_inode_info *fi = F2FS_I(inode);
3039 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3040 struct f2fs_inode *ri = NULL;
3044 if (!f2fs_sb_has_project_quota(sbi)) {
3045 if (projid != F2FS_DEF_PROJID)
3051 if (!f2fs_has_extra_attr(inode))
3054 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3056 if (projid_eq(kprojid, fi->i_projid))
3060 /* Is it quota file? Do not allow user to mess with it */
3061 if (IS_NOQUOTA(inode))
3064 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3067 err = f2fs_dquot_initialize(inode);
3072 err = f2fs_transfer_project_quota(inode, kprojid);
3076 fi->i_projid = kprojid;
3077 inode->i_ctime = current_time(inode);
3078 f2fs_mark_inode_dirty_sync(inode, true);
3080 f2fs_unlock_op(sbi);
3084 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3089 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3091 if (projid != F2FS_DEF_PROJID)
3097 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3099 struct inode *inode = d_inode(dentry);
3100 struct f2fs_inode_info *fi = F2FS_I(inode);
3101 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3103 if (IS_ENCRYPTED(inode))
3104 fsflags |= FS_ENCRYPT_FL;
3105 if (IS_VERITY(inode))
3106 fsflags |= FS_VERITY_FL;
3107 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3108 fsflags |= FS_INLINE_DATA_FL;
3109 if (is_inode_flag_set(inode, FI_PIN_FILE))
3110 fsflags |= FS_NOCOW_FL;
3112 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3114 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3115 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3120 int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3121 struct dentry *dentry, struct fileattr *fa)
3123 struct inode *inode = d_inode(dentry);
3124 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3128 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3130 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3132 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3134 fsflags &= F2FS_SETTABLE_FS_FL;
3135 if (!fa->flags_valid)
3136 mask &= FS_COMMON_FL;
3138 iflags = f2fs_fsflags_to_iflags(fsflags);
3139 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3142 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3144 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3149 int f2fs_pin_file_control(struct inode *inode, bool inc)
3151 struct f2fs_inode_info *fi = F2FS_I(inode);
3152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3154 /* Use i_gc_failures for normal file as a risk signal. */
3156 f2fs_i_gc_failures_write(inode,
3157 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3159 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3160 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3161 __func__, inode->i_ino,
3162 fi->i_gc_failures[GC_FAILURE_PIN]);
3163 clear_inode_flag(inode, FI_PIN_FILE);
3169 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3171 struct inode *inode = file_inode(filp);
3175 if (get_user(pin, (__u32 __user *)arg))
3178 if (!S_ISREG(inode->i_mode))
3181 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3184 ret = mnt_want_write_file(filp);
3191 clear_inode_flag(inode, FI_PIN_FILE);
3192 f2fs_i_gc_failures_write(inode, 0);
3196 if (f2fs_should_update_outplace(inode, NULL)) {
3201 if (f2fs_pin_file_control(inode, false)) {
3206 ret = f2fs_convert_inline_inode(inode);
3210 if (!f2fs_disable_compressed_file(inode)) {
3215 set_inode_flag(inode, FI_PIN_FILE);
3216 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3218 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3220 inode_unlock(inode);
3221 mnt_drop_write_file(filp);
3225 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3227 struct inode *inode = file_inode(filp);
3230 if (is_inode_flag_set(inode, FI_PIN_FILE))
3231 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3232 return put_user(pin, (u32 __user *)arg);
3235 int f2fs_precache_extents(struct inode *inode)
3237 struct f2fs_inode_info *fi = F2FS_I(inode);
3238 struct f2fs_map_blocks map;
3239 pgoff_t m_next_extent;
3243 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3248 map.m_next_pgofs = NULL;
3249 map.m_next_extent = &m_next_extent;
3250 map.m_seg_type = NO_CHECK_TYPE;
3251 map.m_may_create = false;
3252 end = max_file_blocks(inode);
3254 while (map.m_lblk < end) {
3255 map.m_len = end - map.m_lblk;
3257 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3258 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3259 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3263 map.m_lblk = m_next_extent;
3269 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3271 return f2fs_precache_extents(file_inode(filp));
3274 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3276 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3279 if (!capable(CAP_SYS_ADMIN))
3282 if (f2fs_readonly(sbi->sb))
3285 if (copy_from_user(&block_count, (void __user *)arg,
3286 sizeof(block_count)))
3289 return f2fs_resize_fs(filp, block_count);
3292 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3294 struct inode *inode = file_inode(filp);
3296 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3298 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3299 f2fs_warn(F2FS_I_SB(inode),
3300 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3305 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3308 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3310 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3313 return fsverity_ioctl_measure(filp, (void __user *)arg);
3316 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3318 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3321 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3324 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3326 struct inode *inode = file_inode(filp);
3327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3332 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3336 f2fs_down_read(&sbi->sb_lock);
3337 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3338 ARRAY_SIZE(sbi->raw_super->volume_name),
3339 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3340 f2fs_up_read(&sbi->sb_lock);
3342 if (copy_to_user((char __user *)arg, vbuf,
3343 min(FSLABEL_MAX, count)))
3350 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3352 struct inode *inode = file_inode(filp);
3353 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3357 if (!capable(CAP_SYS_ADMIN))
3360 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3362 return PTR_ERR(vbuf);
3364 err = mnt_want_write_file(filp);
3368 f2fs_down_write(&sbi->sb_lock);
3370 memset(sbi->raw_super->volume_name, 0,
3371 sizeof(sbi->raw_super->volume_name));
3372 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3373 sbi->raw_super->volume_name,
3374 ARRAY_SIZE(sbi->raw_super->volume_name));
3376 err = f2fs_commit_super(sbi, false);
3378 f2fs_up_write(&sbi->sb_lock);
3380 mnt_drop_write_file(filp);
3386 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3388 struct inode *inode = file_inode(filp);
3391 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3394 if (!f2fs_compressed_file(inode))
3397 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3398 return put_user(blocks, (u64 __user *)arg);
3401 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3403 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3404 unsigned int released_blocks = 0;
3405 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3409 for (i = 0; i < count; i++) {
3410 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3411 dn->ofs_in_node + i);
3413 if (!__is_valid_data_blkaddr(blkaddr))
3415 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3416 DATA_GENERIC_ENHANCE))) {
3417 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3418 return -EFSCORRUPTED;
3423 int compr_blocks = 0;
3425 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3426 blkaddr = f2fs_data_blkaddr(dn);
3429 if (blkaddr == COMPRESS_ADDR)
3431 dn->ofs_in_node += cluster_size;
3435 if (__is_valid_data_blkaddr(blkaddr))
3438 if (blkaddr != NEW_ADDR)
3441 f2fs_set_data_blkaddr(dn, NULL_ADDR);
3444 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3445 dec_valid_block_count(sbi, dn->inode,
3446 cluster_size - compr_blocks);
3448 released_blocks += cluster_size - compr_blocks;
3450 count -= cluster_size;
3453 return released_blocks;
3456 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3458 struct inode *inode = file_inode(filp);
3459 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3460 pgoff_t page_idx = 0, last_idx;
3461 unsigned int released_blocks = 0;
3465 if (!f2fs_sb_has_compression(sbi))
3468 if (!f2fs_compressed_file(inode))
3471 if (f2fs_readonly(sbi->sb))
3474 ret = mnt_want_write_file(filp);
3478 f2fs_balance_fs(sbi, true);
3482 writecount = atomic_read(&inode->i_writecount);
3483 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3484 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3489 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3494 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3498 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3499 inode->i_ctime = current_time(inode);
3500 f2fs_mark_inode_dirty_sync(inode, true);
3502 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3505 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3506 filemap_invalidate_lock(inode->i_mapping);
3508 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3510 while (page_idx < last_idx) {
3511 struct dnode_of_data dn;
3512 pgoff_t end_offset, count;
3514 set_new_dnode(&dn, inode, NULL, NULL, 0);
3515 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3517 if (ret == -ENOENT) {
3518 page_idx = f2fs_get_next_page_offset(&dn,
3526 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3527 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3528 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3530 ret = release_compress_blocks(&dn, count);
3532 f2fs_put_dnode(&dn);
3538 released_blocks += ret;
3541 filemap_invalidate_unlock(inode->i_mapping);
3542 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3544 inode_unlock(inode);
3546 mnt_drop_write_file(filp);
3549 ret = put_user(released_blocks, (u64 __user *)arg);
3550 } else if (released_blocks &&
3551 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3552 set_sbi_flag(sbi, SBI_NEED_FSCK);
3553 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3554 "iblocks=%llu, released=%u, compr_blocks=%u, "
3556 __func__, inode->i_ino, inode->i_blocks,
3558 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3564 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,
3565 unsigned int *reserved_blocks)
3567 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3568 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3572 for (i = 0; i < count; i++) {
3573 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3574 dn->ofs_in_node + i);
3576 if (!__is_valid_data_blkaddr(blkaddr))
3578 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3579 DATA_GENERIC_ENHANCE))) {
3580 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3581 return -EFSCORRUPTED;
3586 int compr_blocks = 0;
3590 for (i = 0; i < cluster_size; i++) {
3591 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3592 dn->ofs_in_node + i);
3595 if (blkaddr != COMPRESS_ADDR) {
3596 dn->ofs_in_node += cluster_size;
3603 * compressed cluster was not released due to it
3604 * fails in release_compress_blocks(), so NEW_ADDR
3605 * is a possible case.
3607 if (blkaddr == NEW_ADDR ||
3608 __is_valid_data_blkaddr(blkaddr)) {
3614 reserved = cluster_size - compr_blocks;
3616 /* for the case all blocks in cluster were reserved */
3620 ret = inc_valid_block_count(sbi, dn->inode, &reserved, false);
3624 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3625 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
3626 f2fs_set_data_blkaddr(dn, NEW_ADDR);
3629 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3631 *reserved_blocks += reserved;
3633 count -= cluster_size;
3639 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3641 struct inode *inode = file_inode(filp);
3642 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3643 pgoff_t page_idx = 0, last_idx;
3644 unsigned int reserved_blocks = 0;
3647 if (!f2fs_sb_has_compression(sbi))
3650 if (!f2fs_compressed_file(inode))
3653 if (f2fs_readonly(sbi->sb))
3656 ret = mnt_want_write_file(filp);
3660 f2fs_balance_fs(sbi, true);
3664 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3669 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3672 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3673 filemap_invalidate_lock(inode->i_mapping);
3675 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3677 while (page_idx < last_idx) {
3678 struct dnode_of_data dn;
3679 pgoff_t end_offset, count;
3681 set_new_dnode(&dn, inode, NULL, NULL, 0);
3682 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3684 if (ret == -ENOENT) {
3685 page_idx = f2fs_get_next_page_offset(&dn,
3693 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3694 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3695 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3697 ret = reserve_compress_blocks(&dn, count, &reserved_blocks);
3699 f2fs_put_dnode(&dn);
3707 filemap_invalidate_unlock(inode->i_mapping);
3708 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3711 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3712 inode->i_ctime = current_time(inode);
3713 f2fs_mark_inode_dirty_sync(inode, true);
3716 inode_unlock(inode);
3717 mnt_drop_write_file(filp);
3720 ret = put_user(reserved_blocks, (u64 __user *)arg);
3721 } else if (reserved_blocks &&
3722 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3723 set_sbi_flag(sbi, SBI_NEED_FSCK);
3724 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3725 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3727 __func__, inode->i_ino, inode->i_blocks,
3729 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3735 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3736 pgoff_t off, block_t block, block_t len, u32 flags)
3738 sector_t sector = SECTOR_FROM_BLOCK(block);
3739 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3742 if (flags & F2FS_TRIM_FILE_DISCARD) {
3743 if (bdev_max_secure_erase_sectors(bdev))
3744 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3747 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3751 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3752 if (IS_ENCRYPTED(inode))
3753 ret = fscrypt_zeroout_range(inode, off, block, len);
3755 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3762 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3764 struct inode *inode = file_inode(filp);
3765 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3766 struct address_space *mapping = inode->i_mapping;
3767 struct block_device *prev_bdev = NULL;
3768 struct f2fs_sectrim_range range;
3769 pgoff_t index, pg_end, prev_index = 0;
3770 block_t prev_block = 0, len = 0;
3772 bool to_end = false;
3775 if (!(filp->f_mode & FMODE_WRITE))
3778 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3782 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3783 !S_ISREG(inode->i_mode))
3786 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3787 !f2fs_hw_support_discard(sbi)) ||
3788 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3789 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3792 file_start_write(filp);
3795 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3796 range.start >= inode->i_size) {
3804 if (inode->i_size - range.start > range.len) {
3805 end_addr = range.start + range.len;
3807 end_addr = range.len == (u64)-1 ?
3808 sbi->sb->s_maxbytes : inode->i_size;
3812 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3813 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3818 index = F2FS_BYTES_TO_BLK(range.start);
3819 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3821 ret = f2fs_convert_inline_inode(inode);
3825 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3826 filemap_invalidate_lock(mapping);
3828 ret = filemap_write_and_wait_range(mapping, range.start,
3829 to_end ? LLONG_MAX : end_addr - 1);
3833 truncate_inode_pages_range(mapping, range.start,
3834 to_end ? -1 : end_addr - 1);
3836 while (index < pg_end) {
3837 struct dnode_of_data dn;
3838 pgoff_t end_offset, count;
3841 set_new_dnode(&dn, inode, NULL, NULL, 0);
3842 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3844 if (ret == -ENOENT) {
3845 index = f2fs_get_next_page_offset(&dn, index);
3851 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3852 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3853 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3854 struct block_device *cur_bdev;
3855 block_t blkaddr = f2fs_data_blkaddr(&dn);
3857 if (!__is_valid_data_blkaddr(blkaddr))
3860 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3861 DATA_GENERIC_ENHANCE)) {
3862 ret = -EFSCORRUPTED;
3863 f2fs_put_dnode(&dn);
3864 f2fs_handle_error(sbi,
3865 ERROR_INVALID_BLKADDR);
3869 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3870 if (f2fs_is_multi_device(sbi)) {
3871 int di = f2fs_target_device_index(sbi, blkaddr);
3873 blkaddr -= FDEV(di).start_blk;
3877 if (prev_bdev == cur_bdev &&
3878 index == prev_index + len &&
3879 blkaddr == prev_block + len) {
3882 ret = f2fs_secure_erase(prev_bdev,
3883 inode, prev_index, prev_block,
3886 f2fs_put_dnode(&dn);
3895 prev_bdev = cur_bdev;
3897 prev_block = blkaddr;
3902 f2fs_put_dnode(&dn);
3904 if (fatal_signal_pending(current)) {
3912 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3913 prev_block, len, range.flags);
3915 filemap_invalidate_unlock(mapping);
3916 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3918 inode_unlock(inode);
3919 file_end_write(filp);
3924 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3926 struct inode *inode = file_inode(filp);
3927 struct f2fs_comp_option option;
3929 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3932 inode_lock_shared(inode);
3934 if (!f2fs_compressed_file(inode)) {
3935 inode_unlock_shared(inode);
3939 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3940 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3942 inode_unlock_shared(inode);
3944 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3951 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3953 struct inode *inode = file_inode(filp);
3954 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3955 struct f2fs_comp_option option;
3958 if (!f2fs_sb_has_compression(sbi))
3961 if (!(filp->f_mode & FMODE_WRITE))
3964 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3968 if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3969 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3970 option.algorithm >= COMPRESS_MAX)
3973 file_start_write(filp);
3976 f2fs_down_write(&F2FS_I(inode)->i_sem);
3977 if (!f2fs_compressed_file(inode)) {
3982 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3987 if (F2FS_HAS_BLOCKS(inode)) {
3992 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3993 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3994 F2FS_I(inode)->i_cluster_size = BIT(option.log_cluster_size);
3995 /* Set default level */
3996 if (F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD)
3997 F2FS_I(inode)->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
3999 F2FS_I(inode)->i_compress_level = 0;
4000 /* Adjust mount option level */
4001 if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
4002 F2FS_OPTION(sbi).compress_level)
4003 F2FS_I(inode)->i_compress_level = F2FS_OPTION(sbi).compress_level;
4004 f2fs_mark_inode_dirty_sync(inode, true);
4006 if (!f2fs_is_compress_backend_ready(inode))
4007 f2fs_warn(sbi, "compression algorithm is successfully set, "
4008 "but current kernel doesn't support this algorithm.");
4010 f2fs_up_write(&F2FS_I(inode)->i_sem);
4011 inode_unlock(inode);
4012 file_end_write(filp);
4017 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4019 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
4020 struct address_space *mapping = inode->i_mapping;
4022 pgoff_t redirty_idx = page_idx;
4023 int i, page_len = 0, ret = 0;
4025 page_cache_ra_unbounded(&ractl, len, 0);
4027 for (i = 0; i < len; i++, page_idx++) {
4028 page = read_cache_page(mapping, page_idx, NULL, NULL);
4030 ret = PTR_ERR(page);
4036 for (i = 0; i < page_len; i++, redirty_idx++) {
4037 page = find_lock_page(mapping, redirty_idx);
4039 /* It will never fail, when page has pinned above */
4040 f2fs_bug_on(F2FS_I_SB(inode), !page);
4042 set_page_dirty(page);
4043 set_page_private_gcing(page);
4044 f2fs_put_page(page, 1);
4045 f2fs_put_page(page, 0);
4051 static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
4053 struct inode *inode = file_inode(filp);
4054 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4055 struct f2fs_inode_info *fi = F2FS_I(inode);
4056 pgoff_t page_idx = 0, last_idx;
4057 unsigned int blk_per_seg = sbi->blocks_per_seg;
4058 int cluster_size = fi->i_cluster_size;
4061 if (!f2fs_sb_has_compression(sbi) ||
4062 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4065 if (!(filp->f_mode & FMODE_WRITE))
4068 if (!f2fs_compressed_file(inode))
4071 f2fs_balance_fs(sbi, true);
4073 file_start_write(filp);
4076 if (!f2fs_is_compress_backend_ready(inode)) {
4081 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4086 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4090 if (!atomic_read(&fi->i_compr_blocks))
4093 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4095 count = last_idx - page_idx;
4097 int len = min(cluster_size, count);
4099 ret = redirty_blocks(inode, page_idx, len);
4103 if (get_dirty_pages(inode) >= blk_per_seg)
4104 filemap_fdatawrite(inode->i_mapping);
4111 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4115 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4118 inode_unlock(inode);
4119 file_end_write(filp);
4124 static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4126 struct inode *inode = file_inode(filp);
4127 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4128 pgoff_t page_idx = 0, last_idx;
4129 unsigned int blk_per_seg = sbi->blocks_per_seg;
4130 int cluster_size = F2FS_I(inode)->i_cluster_size;
4133 if (!f2fs_sb_has_compression(sbi) ||
4134 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4137 if (!(filp->f_mode & FMODE_WRITE))
4140 if (!f2fs_compressed_file(inode))
4143 f2fs_balance_fs(sbi, true);
4145 file_start_write(filp);
4148 if (!f2fs_is_compress_backend_ready(inode)) {
4153 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4158 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4162 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4164 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4166 count = last_idx - page_idx;
4168 int len = min(cluster_size, count);
4170 ret = redirty_blocks(inode, page_idx, len);
4174 if (get_dirty_pages(inode) >= blk_per_seg)
4175 filemap_fdatawrite(inode->i_mapping);
4182 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4185 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4188 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4191 inode_unlock(inode);
4192 file_end_write(filp);
4197 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4200 case FS_IOC_GETVERSION:
4201 return f2fs_ioc_getversion(filp, arg);
4202 case F2FS_IOC_START_ATOMIC_WRITE:
4203 return f2fs_ioc_start_atomic_write(filp);
4204 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4205 return f2fs_ioc_commit_atomic_write(filp);
4206 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4207 return f2fs_ioc_abort_atomic_write(filp);
4208 case F2FS_IOC_START_VOLATILE_WRITE:
4209 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4211 case F2FS_IOC_SHUTDOWN:
4212 return f2fs_ioc_shutdown(filp, arg);
4214 return f2fs_ioc_fitrim(filp, arg);
4215 case FS_IOC_SET_ENCRYPTION_POLICY:
4216 return f2fs_ioc_set_encryption_policy(filp, arg);
4217 case FS_IOC_GET_ENCRYPTION_POLICY:
4218 return f2fs_ioc_get_encryption_policy(filp, arg);
4219 case FS_IOC_GET_ENCRYPTION_PWSALT:
4220 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4221 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4222 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4223 case FS_IOC_ADD_ENCRYPTION_KEY:
4224 return f2fs_ioc_add_encryption_key(filp, arg);
4225 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4226 return f2fs_ioc_remove_encryption_key(filp, arg);
4227 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4228 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4229 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4230 return f2fs_ioc_get_encryption_key_status(filp, arg);
4231 case FS_IOC_GET_ENCRYPTION_NONCE:
4232 return f2fs_ioc_get_encryption_nonce(filp, arg);
4233 case F2FS_IOC_GARBAGE_COLLECT:
4234 return f2fs_ioc_gc(filp, arg);
4235 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4236 return f2fs_ioc_gc_range(filp, arg);
4237 case F2FS_IOC_WRITE_CHECKPOINT:
4238 return f2fs_ioc_write_checkpoint(filp, arg);
4239 case F2FS_IOC_DEFRAGMENT:
4240 return f2fs_ioc_defragment(filp, arg);
4241 case F2FS_IOC_MOVE_RANGE:
4242 return f2fs_ioc_move_range(filp, arg);
4243 case F2FS_IOC_FLUSH_DEVICE:
4244 return f2fs_ioc_flush_device(filp, arg);
4245 case F2FS_IOC_GET_FEATURES:
4246 return f2fs_ioc_get_features(filp, arg);
4247 case F2FS_IOC_GET_PIN_FILE:
4248 return f2fs_ioc_get_pin_file(filp, arg);
4249 case F2FS_IOC_SET_PIN_FILE:
4250 return f2fs_ioc_set_pin_file(filp, arg);
4251 case F2FS_IOC_PRECACHE_EXTENTS:
4252 return f2fs_ioc_precache_extents(filp, arg);
4253 case F2FS_IOC_RESIZE_FS:
4254 return f2fs_ioc_resize_fs(filp, arg);
4255 case FS_IOC_ENABLE_VERITY:
4256 return f2fs_ioc_enable_verity(filp, arg);
4257 case FS_IOC_MEASURE_VERITY:
4258 return f2fs_ioc_measure_verity(filp, arg);
4259 case FS_IOC_READ_VERITY_METADATA:
4260 return f2fs_ioc_read_verity_metadata(filp, arg);
4261 case FS_IOC_GETFSLABEL:
4262 return f2fs_ioc_getfslabel(filp, arg);
4263 case FS_IOC_SETFSLABEL:
4264 return f2fs_ioc_setfslabel(filp, arg);
4265 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4266 return f2fs_get_compress_blocks(filp, arg);
4267 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4268 return f2fs_release_compress_blocks(filp, arg);
4269 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4270 return f2fs_reserve_compress_blocks(filp, arg);
4271 case F2FS_IOC_SEC_TRIM_FILE:
4272 return f2fs_sec_trim_file(filp, arg);
4273 case F2FS_IOC_GET_COMPRESS_OPTION:
4274 return f2fs_ioc_get_compress_option(filp, arg);
4275 case F2FS_IOC_SET_COMPRESS_OPTION:
4276 return f2fs_ioc_set_compress_option(filp, arg);
4277 case F2FS_IOC_DECOMPRESS_FILE:
4278 return f2fs_ioc_decompress_file(filp, arg);
4279 case F2FS_IOC_COMPRESS_FILE:
4280 return f2fs_ioc_compress_file(filp, arg);
4286 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4288 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4290 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4293 return __f2fs_ioctl(filp, cmd, arg);
4297 * Return %true if the given read or write request should use direct I/O, or
4298 * %false if it should use buffered I/O.
4300 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4301 struct iov_iter *iter)
4305 if (!(iocb->ki_flags & IOCB_DIRECT))
4308 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4312 * Direct I/O not aligned to the disk's logical_block_size will be
4313 * attempted, but will fail with -EINVAL.
4315 * f2fs additionally requires that direct I/O be aligned to the
4316 * filesystem block size, which is often a stricter requirement.
4317 * However, f2fs traditionally falls back to buffered I/O on requests
4318 * that are logical_block_size-aligned but not fs-block aligned.
4320 * The below logic implements this behavior.
4322 align = iocb->ki_pos | iov_iter_alignment(iter);
4323 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4324 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4330 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4333 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4335 dec_page_count(sbi, F2FS_DIO_READ);
4338 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4342 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4343 .end_io = f2fs_dio_read_end_io,
4346 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4348 struct file *file = iocb->ki_filp;
4349 struct inode *inode = file_inode(file);
4350 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4351 struct f2fs_inode_info *fi = F2FS_I(inode);
4352 const loff_t pos = iocb->ki_pos;
4353 const size_t count = iov_iter_count(to);
4354 struct iomap_dio *dio;
4358 return 0; /* skip atime update */
4360 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4362 if (iocb->ki_flags & IOCB_NOWAIT) {
4363 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4368 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4372 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4373 * the higher-level function iomap_dio_rw() in order to ensure that the
4374 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4376 inc_page_count(sbi, F2FS_DIO_READ);
4377 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4378 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4379 if (IS_ERR_OR_NULL(dio)) {
4380 ret = PTR_ERR_OR_ZERO(dio);
4381 if (ret != -EIOCBQUEUED)
4382 dec_page_count(sbi, F2FS_DIO_READ);
4384 ret = iomap_dio_complete(dio);
4387 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4389 file_accessed(file);
4391 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4395 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4397 struct inode *inode = file_inode(iocb->ki_filp);
4398 const loff_t pos = iocb->ki_pos;
4401 if (!f2fs_is_compress_backend_ready(inode))
4404 if (trace_f2fs_dataread_start_enabled()) {
4405 char *p = f2fs_kmalloc(F2FS_I_SB(inode), PATH_MAX, GFP_KERNEL);
4409 goto skip_read_trace;
4411 path = dentry_path_raw(file_dentry(iocb->ki_filp), p, PATH_MAX);
4414 goto skip_read_trace;
4417 trace_f2fs_dataread_start(inode, pos, iov_iter_count(to),
4418 current->pid, path, current->comm);
4422 if (f2fs_should_use_dio(inode, iocb, to)) {
4423 ret = f2fs_dio_read_iter(iocb, to);
4425 ret = filemap_read(iocb, to, 0);
4427 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4428 APP_BUFFERED_READ_IO, ret);
4430 if (trace_f2fs_dataread_end_enabled())
4431 trace_f2fs_dataread_end(inode, pos, ret);
4435 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4437 struct file *file = iocb->ki_filp;
4438 struct inode *inode = file_inode(file);
4442 if (IS_IMMUTABLE(inode))
4445 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4448 count = generic_write_checks(iocb, from);
4452 err = file_modified(file);
4459 * Preallocate blocks for a write request, if it is possible and helpful to do
4460 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4461 * blocks were preallocated, or a negative errno value if something went
4462 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4463 * requested blocks (not just some of them) have been allocated.
4465 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4468 struct inode *inode = file_inode(iocb->ki_filp);
4469 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4470 const loff_t pos = iocb->ki_pos;
4471 const size_t count = iov_iter_count(iter);
4472 struct f2fs_map_blocks map = {};
4476 /* If it will be an out-of-place direct write, don't bother. */
4477 if (dio && f2fs_lfs_mode(sbi))
4480 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4481 * buffered IO, if DIO meets any holes.
4483 if (dio && i_size_read(inode) &&
4484 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4487 /* No-wait I/O can't allocate blocks. */
4488 if (iocb->ki_flags & IOCB_NOWAIT)
4491 /* If it will be a short write, don't bother. */
4492 if (fault_in_iov_iter_readable(iter, count))
4495 if (f2fs_has_inline_data(inode)) {
4496 /* If the data will fit inline, don't bother. */
4497 if (pos + count <= MAX_INLINE_DATA(inode))
4499 ret = f2fs_convert_inline_inode(inode);
4504 /* Do not preallocate blocks that will be written partially in 4KB. */
4505 map.m_lblk = F2FS_BLK_ALIGN(pos);
4506 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4507 if (map.m_len > map.m_lblk)
4508 map.m_len -= map.m_lblk;
4511 map.m_may_create = true;
4513 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4514 flag = F2FS_GET_BLOCK_PRE_DIO;
4516 map.m_seg_type = NO_CHECK_TYPE;
4517 flag = F2FS_GET_BLOCK_PRE_AIO;
4520 ret = f2fs_map_blocks(inode, &map, 1, flag);
4521 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4522 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4525 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4529 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4530 struct iov_iter *from)
4532 struct file *file = iocb->ki_filp;
4533 struct inode *inode = file_inode(file);
4536 if (iocb->ki_flags & IOCB_NOWAIT)
4539 current->backing_dev_info = inode_to_bdi(inode);
4540 ret = generic_perform_write(iocb, from);
4541 current->backing_dev_info = NULL;
4544 iocb->ki_pos += ret;
4545 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4546 APP_BUFFERED_IO, ret);
4551 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4554 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4556 dec_page_count(sbi, F2FS_DIO_WRITE);
4559 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4563 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4564 .end_io = f2fs_dio_write_end_io,
4567 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4568 bool *may_need_sync)
4570 struct file *file = iocb->ki_filp;
4571 struct inode *inode = file_inode(file);
4572 struct f2fs_inode_info *fi = F2FS_I(inode);
4573 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4574 const bool do_opu = f2fs_lfs_mode(sbi);
4575 const loff_t pos = iocb->ki_pos;
4576 const ssize_t count = iov_iter_count(from);
4577 unsigned int dio_flags;
4578 struct iomap_dio *dio;
4581 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4583 if (iocb->ki_flags & IOCB_NOWAIT) {
4584 /* f2fs_convert_inline_inode() and block allocation can block */
4585 if (f2fs_has_inline_data(inode) ||
4586 !f2fs_overwrite_io(inode, pos, count)) {
4591 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4595 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4596 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4601 ret = f2fs_convert_inline_inode(inode);
4605 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4607 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4611 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4612 * the higher-level function iomap_dio_rw() in order to ensure that the
4613 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4615 inc_page_count(sbi, F2FS_DIO_WRITE);
4617 if (pos + count > inode->i_size)
4618 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4619 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4620 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4621 if (IS_ERR_OR_NULL(dio)) {
4622 ret = PTR_ERR_OR_ZERO(dio);
4623 if (ret == -ENOTBLK)
4625 if (ret != -EIOCBQUEUED)
4626 dec_page_count(sbi, F2FS_DIO_WRITE);
4628 ret = iomap_dio_complete(dio);
4632 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4633 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4637 if (pos + ret > inode->i_size)
4638 f2fs_i_size_write(inode, pos + ret);
4640 set_inode_flag(inode, FI_UPDATE_WRITE);
4642 if (iov_iter_count(from)) {
4644 loff_t bufio_start_pos = iocb->ki_pos;
4647 * The direct write was partial, so we need to fall back to a
4648 * buffered write for the remainder.
4651 ret2 = f2fs_buffered_write_iter(iocb, from);
4652 if (iov_iter_count(from))
4653 f2fs_write_failed(inode, iocb->ki_pos);
4658 * Ensure that the pagecache pages are written to disk and
4659 * invalidated to preserve the expected O_DIRECT semantics.
4662 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4666 ret2 = filemap_write_and_wait_range(file->f_mapping,
4671 invalidate_mapping_pages(file->f_mapping,
4672 bufio_start_pos >> PAGE_SHIFT,
4673 bufio_end_pos >> PAGE_SHIFT);
4676 /* iomap_dio_rw() already handled the generic_write_sync(). */
4677 *may_need_sync = false;
4680 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4684 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4686 struct inode *inode = file_inode(iocb->ki_filp);
4687 const loff_t orig_pos = iocb->ki_pos;
4688 const size_t orig_count = iov_iter_count(from);
4691 bool may_need_sync = true;
4695 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4700 if (!f2fs_is_compress_backend_ready(inode)) {
4705 if (iocb->ki_flags & IOCB_NOWAIT) {
4706 if (!inode_trylock(inode)) {
4714 ret = f2fs_write_checks(iocb, from);
4718 /* Determine whether we will do a direct write or a buffered write. */
4719 dio = f2fs_should_use_dio(inode, iocb, from);
4721 /* Possibly preallocate the blocks for the write. */
4722 target_size = iocb->ki_pos + iov_iter_count(from);
4723 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4724 if (preallocated < 0) {
4727 if (trace_f2fs_datawrite_start_enabled()) {
4728 char *p = f2fs_kmalloc(F2FS_I_SB(inode),
4729 PATH_MAX, GFP_KERNEL);
4733 goto skip_write_trace;
4734 path = dentry_path_raw(file_dentry(iocb->ki_filp),
4738 goto skip_write_trace;
4740 trace_f2fs_datawrite_start(inode, orig_pos, orig_count,
4741 current->pid, path, current->comm);
4745 /* Do the actual write. */
4747 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
4748 f2fs_buffered_write_iter(iocb, from);
4750 if (trace_f2fs_datawrite_end_enabled())
4751 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4754 /* Don't leave any preallocated blocks around past i_size. */
4755 if (preallocated && i_size_read(inode) < target_size) {
4756 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4757 filemap_invalidate_lock(inode->i_mapping);
4758 if (!f2fs_truncate(inode))
4759 file_dont_truncate(inode);
4760 filemap_invalidate_unlock(inode->i_mapping);
4761 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4763 file_dont_truncate(inode);
4766 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4768 inode_unlock(inode);
4770 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4771 if (ret > 0 && may_need_sync)
4772 ret = generic_write_sync(iocb, ret);
4776 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4779 struct address_space *mapping;
4780 struct backing_dev_info *bdi;
4781 struct inode *inode = file_inode(filp);
4784 if (advice == POSIX_FADV_SEQUENTIAL) {
4785 if (S_ISFIFO(inode->i_mode))
4788 mapping = filp->f_mapping;
4789 if (!mapping || len < 0)
4792 bdi = inode_to_bdi(mapping->host);
4793 filp->f_ra.ra_pages = bdi->ra_pages *
4794 F2FS_I_SB(inode)->seq_file_ra_mul;
4795 spin_lock(&filp->f_lock);
4796 filp->f_mode &= ~FMODE_RANDOM;
4797 spin_unlock(&filp->f_lock);
4801 err = generic_fadvise(filp, offset, len, advice);
4802 if (!err && advice == POSIX_FADV_DONTNEED &&
4803 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4804 f2fs_compressed_file(inode))
4805 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4810 #ifdef CONFIG_COMPAT
4811 struct compat_f2fs_gc_range {
4816 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4817 struct compat_f2fs_gc_range)
4819 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4821 struct compat_f2fs_gc_range __user *urange;
4822 struct f2fs_gc_range range;
4825 urange = compat_ptr(arg);
4826 err = get_user(range.sync, &urange->sync);
4827 err |= get_user(range.start, &urange->start);
4828 err |= get_user(range.len, &urange->len);
4832 return __f2fs_ioc_gc_range(file, &range);
4835 struct compat_f2fs_move_range {
4841 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4842 struct compat_f2fs_move_range)
4844 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4846 struct compat_f2fs_move_range __user *urange;
4847 struct f2fs_move_range range;
4850 urange = compat_ptr(arg);
4851 err = get_user(range.dst_fd, &urange->dst_fd);
4852 err |= get_user(range.pos_in, &urange->pos_in);
4853 err |= get_user(range.pos_out, &urange->pos_out);
4854 err |= get_user(range.len, &urange->len);
4858 return __f2fs_ioc_move_range(file, &range);
4861 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4863 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4865 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4869 case FS_IOC32_GETVERSION:
4870 cmd = FS_IOC_GETVERSION;
4872 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4873 return f2fs_compat_ioc_gc_range(file, arg);
4874 case F2FS_IOC32_MOVE_RANGE:
4875 return f2fs_compat_ioc_move_range(file, arg);
4876 case F2FS_IOC_START_ATOMIC_WRITE:
4877 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4878 case F2FS_IOC_START_VOLATILE_WRITE:
4879 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4880 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4881 case F2FS_IOC_SHUTDOWN:
4883 case FS_IOC_SET_ENCRYPTION_POLICY:
4884 case FS_IOC_GET_ENCRYPTION_PWSALT:
4885 case FS_IOC_GET_ENCRYPTION_POLICY:
4886 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4887 case FS_IOC_ADD_ENCRYPTION_KEY:
4888 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4889 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4890 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4891 case FS_IOC_GET_ENCRYPTION_NONCE:
4892 case F2FS_IOC_GARBAGE_COLLECT:
4893 case F2FS_IOC_WRITE_CHECKPOINT:
4894 case F2FS_IOC_DEFRAGMENT:
4895 case F2FS_IOC_FLUSH_DEVICE:
4896 case F2FS_IOC_GET_FEATURES:
4897 case F2FS_IOC_GET_PIN_FILE:
4898 case F2FS_IOC_SET_PIN_FILE:
4899 case F2FS_IOC_PRECACHE_EXTENTS:
4900 case F2FS_IOC_RESIZE_FS:
4901 case FS_IOC_ENABLE_VERITY:
4902 case FS_IOC_MEASURE_VERITY:
4903 case FS_IOC_READ_VERITY_METADATA:
4904 case FS_IOC_GETFSLABEL:
4905 case FS_IOC_SETFSLABEL:
4906 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4907 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4908 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4909 case F2FS_IOC_SEC_TRIM_FILE:
4910 case F2FS_IOC_GET_COMPRESS_OPTION:
4911 case F2FS_IOC_SET_COMPRESS_OPTION:
4912 case F2FS_IOC_DECOMPRESS_FILE:
4913 case F2FS_IOC_COMPRESS_FILE:
4916 return -ENOIOCTLCMD;
4918 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4922 const struct file_operations f2fs_file_operations = {
4923 .llseek = f2fs_llseek,
4924 .read_iter = f2fs_file_read_iter,
4925 .write_iter = f2fs_file_write_iter,
4926 .open = f2fs_file_open,
4927 .release = f2fs_release_file,
4928 .mmap = f2fs_file_mmap,
4929 .flush = f2fs_file_flush,
4930 .fsync = f2fs_sync_file,
4931 .fallocate = f2fs_fallocate,
4932 .unlocked_ioctl = f2fs_ioctl,
4933 #ifdef CONFIG_COMPAT
4934 .compat_ioctl = f2fs_compat_ioctl,
4936 .splice_read = generic_file_splice_read,
4937 .splice_write = iter_file_splice_write,
4938 .fadvise = f2fs_file_fadvise,