1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/buffer_head.h>
12 #include <linux/writeback.h>
13 #include <linux/blkdev.h>
14 #include <linux/falloc.h>
15 #include <linux/types.h>
16 #include <linux/compat.h>
17 #include <linux/uaccess.h>
18 #include <linux/mount.h>
19 #include <linux/pagevec.h>
20 #include <linux/uio.h>
21 #include <linux/uuid.h>
22 #include <linux/file.h>
23 #include <linux/nls.h>
24 #include <linux/sched/signal.h>
25 #include <linux/fileattr.h>
26 #include <linux/fadvise.h>
27 #include <linux/iomap.h>
36 #include <trace/events/f2fs.h>
37 #include <uapi/linux/f2fs.h>
39 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
41 struct inode *inode = file_inode(vmf->vma->vm_file);
44 ret = filemap_fault(vmf);
46 f2fs_update_iostat(F2FS_I_SB(inode), inode,
47 APP_MAPPED_READ_IO, F2FS_BLKSIZE);
49 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
54 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
56 struct page *page = vmf->page;
57 struct inode *inode = file_inode(vmf->vma->vm_file);
58 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
59 struct dnode_of_data dn;
60 bool need_alloc = true;
63 if (unlikely(IS_IMMUTABLE(inode)))
64 return VM_FAULT_SIGBUS;
66 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
67 return VM_FAULT_SIGBUS;
69 if (unlikely(f2fs_cp_error(sbi))) {
74 if (!f2fs_is_checkpoint_ready(sbi)) {
79 err = f2fs_convert_inline_inode(inode);
83 #ifdef CONFIG_F2FS_FS_COMPRESSION
84 if (f2fs_compressed_file(inode)) {
85 int ret = f2fs_is_compressed_cluster(inode, page->index);
95 /* should do out of any locked page */
97 f2fs_balance_fs(sbi, true);
99 sb_start_pagefault(inode->i_sb);
101 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
103 file_update_time(vmf->vma->vm_file);
104 filemap_invalidate_lock_shared(inode->i_mapping);
106 if (unlikely(page->mapping != inode->i_mapping ||
107 page_offset(page) > i_size_read(inode) ||
108 !PageUptodate(page))) {
115 /* block allocation */
116 set_new_dnode(&dn, inode, NULL, NULL, 0);
117 err = f2fs_get_block_locked(&dn, page->index);
120 #ifdef CONFIG_F2FS_FS_COMPRESSION
122 set_new_dnode(&dn, inode, NULL, NULL, 0);
123 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
132 f2fs_wait_on_page_writeback(page, DATA, false, true);
134 /* wait for GCed page writeback via META_MAPPING */
135 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
138 * check to see if the page is mapped already (no holes)
140 if (PageMappedToDisk(page))
143 /* page is wholly or partially inside EOF */
144 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
145 i_size_read(inode)) {
148 offset = i_size_read(inode) & ~PAGE_MASK;
149 zero_user_segment(page, offset, PAGE_SIZE);
151 set_page_dirty(page);
152 if (!PageUptodate(page))
153 SetPageUptodate(page);
155 f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
156 f2fs_update_time(sbi, REQ_TIME);
158 trace_f2fs_vm_page_mkwrite(page, DATA);
160 filemap_invalidate_unlock_shared(inode->i_mapping);
162 sb_end_pagefault(inode->i_sb);
164 return block_page_mkwrite_return(err);
167 static const struct vm_operations_struct f2fs_file_vm_ops = {
168 .fault = f2fs_filemap_fault,
169 .map_pages = filemap_map_pages,
170 .page_mkwrite = f2fs_vm_page_mkwrite,
173 static int get_parent_ino(struct inode *inode, nid_t *pino)
175 struct dentry *dentry;
178 * Make sure to get the non-deleted alias. The alias associated with
179 * the open file descriptor being fsync()'ed may be deleted already.
181 dentry = d_find_alias(inode);
185 *pino = parent_ino(dentry);
190 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
192 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
193 enum cp_reason_type cp_reason = CP_NO_NEEDED;
195 if (!S_ISREG(inode->i_mode))
196 cp_reason = CP_NON_REGULAR;
197 else if (f2fs_compressed_file(inode))
198 cp_reason = CP_COMPRESSED;
199 else if (inode->i_nlink != 1)
200 cp_reason = CP_HARDLINK;
201 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
202 cp_reason = CP_SB_NEED_CP;
203 else if (file_wrong_pino(inode))
204 cp_reason = CP_WRONG_PINO;
205 else if (!f2fs_space_for_roll_forward(sbi))
206 cp_reason = CP_NO_SPC_ROLL;
207 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
208 cp_reason = CP_NODE_NEED_CP;
209 else if (test_opt(sbi, FASTBOOT))
210 cp_reason = CP_FASTBOOT_MODE;
211 else if (F2FS_OPTION(sbi).active_logs == 2)
212 cp_reason = CP_SPEC_LOG_NUM;
213 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
214 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
215 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
217 cp_reason = CP_RECOVER_DIR;
222 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
224 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
226 /* But we need to avoid that there are some inode updates */
227 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
233 static void try_to_fix_pino(struct inode *inode)
235 struct f2fs_inode_info *fi = F2FS_I(inode);
238 f2fs_down_write(&fi->i_sem);
239 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
240 get_parent_ino(inode, &pino)) {
241 f2fs_i_pino_write(inode, pino);
242 file_got_pino(inode);
244 f2fs_up_write(&fi->i_sem);
247 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
248 int datasync, bool atomic)
250 struct inode *inode = file->f_mapping->host;
251 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
252 nid_t ino = inode->i_ino;
254 enum cp_reason_type cp_reason = 0;
255 struct writeback_control wbc = {
256 .sync_mode = WB_SYNC_ALL,
257 .nr_to_write = LONG_MAX,
260 unsigned int seq_id = 0;
262 if (unlikely(f2fs_readonly(inode->i_sb)))
265 trace_f2fs_sync_file_enter(inode);
267 if (S_ISDIR(inode->i_mode))
270 /* if fdatasync is triggered, let's do in-place-update */
271 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
272 set_inode_flag(inode, FI_NEED_IPU);
273 ret = file_write_and_wait_range(file, start, end);
274 clear_inode_flag(inode, FI_NEED_IPU);
276 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
277 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
281 /* if the inode is dirty, let's recover all the time */
282 if (!f2fs_skip_inode_update(inode, datasync)) {
283 f2fs_write_inode(inode, NULL);
288 * if there is no written data, don't waste time to write recovery info.
290 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
291 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
293 /* it may call write_inode just prior to fsync */
294 if (need_inode_page_update(sbi, ino))
297 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
298 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
303 * for OPU case, during fsync(), node can be persisted before
304 * data when lower device doesn't support write barrier, result
305 * in data corruption after SPO.
306 * So for strict fsync mode, force to use atomic write semantics
307 * to keep write order in between data/node and last node to
308 * avoid potential data corruption.
310 if (F2FS_OPTION(sbi).fsync_mode ==
311 FSYNC_MODE_STRICT && !atomic)
316 * Both of fdatasync() and fsync() are able to be recovered from
319 f2fs_down_read(&F2FS_I(inode)->i_sem);
320 cp_reason = need_do_checkpoint(inode);
321 f2fs_up_read(&F2FS_I(inode)->i_sem);
324 /* all the dirty node pages should be flushed for POR */
325 ret = f2fs_sync_fs(inode->i_sb, 1);
328 * We've secured consistency through sync_fs. Following pino
329 * will be used only for fsynced inodes after checkpoint.
331 try_to_fix_pino(inode);
332 clear_inode_flag(inode, FI_APPEND_WRITE);
333 clear_inode_flag(inode, FI_UPDATE_WRITE);
337 atomic_inc(&sbi->wb_sync_req[NODE]);
338 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
339 atomic_dec(&sbi->wb_sync_req[NODE]);
343 /* if cp_error was enabled, we should avoid infinite loop */
344 if (unlikely(f2fs_cp_error(sbi))) {
349 if (f2fs_need_inode_block_update(sbi, ino)) {
350 f2fs_mark_inode_dirty_sync(inode, true);
351 f2fs_write_inode(inode, NULL);
356 * If it's atomic_write, it's just fine to keep write ordering. So
357 * here we don't need to wait for node write completion, since we use
358 * node chain which serializes node blocks. If one of node writes are
359 * reordered, we can see simply broken chain, resulting in stopping
360 * roll-forward recovery. It means we'll recover all or none node blocks
364 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
369 /* once recovery info is written, don't need to tack this */
370 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
371 clear_inode_flag(inode, FI_APPEND_WRITE);
373 if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) ||
374 (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi)))
375 ret = f2fs_issue_flush(sbi, inode->i_ino);
377 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
378 clear_inode_flag(inode, FI_UPDATE_WRITE);
379 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
381 f2fs_update_time(sbi, REQ_TIME);
383 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
387 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
389 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
391 return f2fs_do_sync_file(file, start, end, datasync, false);
394 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
395 pgoff_t index, int whence)
399 if (__is_valid_data_blkaddr(blkaddr))
401 if (blkaddr == NEW_ADDR &&
402 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
406 if (blkaddr == NULL_ADDR)
413 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
415 struct inode *inode = file->f_mapping->host;
416 loff_t maxbytes = inode->i_sb->s_maxbytes;
417 struct dnode_of_data dn;
418 pgoff_t pgofs, end_offset;
419 loff_t data_ofs = offset;
425 isize = i_size_read(inode);
429 /* handle inline data case */
430 if (f2fs_has_inline_data(inode)) {
431 if (whence == SEEK_HOLE) {
434 } else if (whence == SEEK_DATA) {
440 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
442 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
443 set_new_dnode(&dn, inode, NULL, NULL, 0);
444 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
445 if (err && err != -ENOENT) {
447 } else if (err == -ENOENT) {
448 /* direct node does not exists */
449 if (whence == SEEK_DATA) {
450 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
457 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
459 /* find data/hole in dnode block */
460 for (; dn.ofs_in_node < end_offset;
461 dn.ofs_in_node++, pgofs++,
462 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
465 blkaddr = f2fs_data_blkaddr(&dn);
467 if (__is_valid_data_blkaddr(blkaddr) &&
468 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
469 blkaddr, DATA_GENERIC_ENHANCE)) {
474 if (__found_offset(file->f_mapping, blkaddr,
483 if (whence == SEEK_DATA)
486 if (whence == SEEK_HOLE && data_ofs > isize)
489 return vfs_setpos(file, data_ofs, maxbytes);
495 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
497 struct inode *inode = file->f_mapping->host;
498 loff_t maxbytes = inode->i_sb->s_maxbytes;
500 if (f2fs_compressed_file(inode))
501 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
507 return generic_file_llseek_size(file, offset, whence,
508 maxbytes, i_size_read(inode));
513 return f2fs_seek_block(file, offset, whence);
519 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
521 struct inode *inode = file_inode(file);
523 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
526 if (!f2fs_is_compress_backend_ready(inode))
530 vma->vm_ops = &f2fs_file_vm_ops;
531 set_inode_flag(inode, FI_MMAP_FILE);
535 static int f2fs_file_open(struct inode *inode, struct file *filp)
537 int err = fscrypt_file_open(inode, filp);
542 if (!f2fs_is_compress_backend_ready(inode))
545 err = fsverity_file_open(inode, filp);
549 filp->f_mode |= FMODE_NOWAIT;
551 return dquot_file_open(inode, filp);
554 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
556 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
557 struct f2fs_node *raw_node;
558 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
561 bool compressed_cluster = false;
562 int cluster_index = 0, valid_blocks = 0;
563 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
564 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
566 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
567 base = get_extra_isize(dn->inode);
569 raw_node = F2FS_NODE(dn->node_page);
570 addr = blkaddr_in_node(raw_node) + base + ofs;
572 /* Assumption: truncation 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 dn->data_blkaddr = NULL_ADDR;
589 f2fs_set_data_blkaddr(dn);
591 if (__is_valid_data_blkaddr(blkaddr)) {
592 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
593 DATA_GENERIC_ENHANCE))
595 if (compressed_cluster)
599 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
600 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
602 f2fs_invalidate_blocks(sbi, blkaddr);
604 if (!released || blkaddr != COMPRESS_ADDR)
608 if (compressed_cluster)
609 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
614 * once we invalidate valid blkaddr in range [ofs, ofs + count],
615 * we will invalidate all blkaddr in the whole range.
617 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
619 f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
620 f2fs_update_age_extent_cache_range(dn, fofs, len);
621 dec_valid_block_count(sbi, dn->inode, nr_free);
623 dn->ofs_in_node = ofs;
625 f2fs_update_time(sbi, REQ_TIME);
626 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
627 dn->ofs_in_node, nr_free);
630 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
632 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
635 static int truncate_partial_data_page(struct inode *inode, u64 from,
638 loff_t offset = from & (PAGE_SIZE - 1);
639 pgoff_t index = from >> PAGE_SHIFT;
640 struct address_space *mapping = inode->i_mapping;
643 if (!offset && !cache_only)
647 page = find_lock_page(mapping, index);
648 if (page && PageUptodate(page))
650 f2fs_put_page(page, 1);
654 page = f2fs_get_lock_data_page(inode, index, true);
656 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
658 f2fs_wait_on_page_writeback(page, DATA, true, true);
659 zero_user(page, offset, PAGE_SIZE - offset);
661 /* An encrypted inode should have a key and truncate the last page. */
662 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
664 set_page_dirty(page);
665 f2fs_put_page(page, 1);
669 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
671 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
672 struct dnode_of_data dn;
674 int count = 0, err = 0;
676 bool truncate_page = false;
678 trace_f2fs_truncate_blocks_enter(inode, from);
680 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
682 if (free_from >= max_file_blocks(inode))
688 ipage = f2fs_get_node_page(sbi, inode->i_ino);
690 err = PTR_ERR(ipage);
694 if (f2fs_has_inline_data(inode)) {
695 f2fs_truncate_inline_inode(inode, ipage, from);
696 f2fs_put_page(ipage, 1);
697 truncate_page = true;
701 set_new_dnode(&dn, inode, ipage, NULL, 0);
702 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
709 count = ADDRS_PER_PAGE(dn.node_page, inode);
711 count -= dn.ofs_in_node;
712 f2fs_bug_on(sbi, count < 0);
714 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
715 f2fs_truncate_data_blocks_range(&dn, count);
721 err = f2fs_truncate_inode_blocks(inode, free_from);
726 /* lastly zero out the first data page */
728 err = truncate_partial_data_page(inode, from, truncate_page);
730 trace_f2fs_truncate_blocks_exit(inode, err);
734 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
736 u64 free_from = from;
739 #ifdef CONFIG_F2FS_FS_COMPRESSION
741 * for compressed file, only support cluster size
742 * aligned truncation.
744 if (f2fs_compressed_file(inode))
745 free_from = round_up(from,
746 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
749 err = f2fs_do_truncate_blocks(inode, free_from, lock);
753 #ifdef CONFIG_F2FS_FS_COMPRESSION
755 * For compressed file, after release compress blocks, don't allow write
756 * direct, but we should allow write direct after truncate to zero.
758 if (f2fs_compressed_file(inode) && !free_from
759 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
760 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
762 if (from != free_from) {
763 err = f2fs_truncate_partial_cluster(inode, from, lock);
772 int f2fs_truncate(struct inode *inode)
776 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
779 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
780 S_ISLNK(inode->i_mode)))
783 trace_f2fs_truncate(inode);
785 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
788 err = f2fs_dquot_initialize(inode);
792 /* we should check inline_data size */
793 if (!f2fs_may_inline_data(inode)) {
794 err = f2fs_convert_inline_inode(inode);
799 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
803 inode->i_mtime = inode->i_ctime = current_time(inode);
804 f2fs_mark_inode_dirty_sync(inode, false);
808 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
810 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
812 if (!fscrypt_dio_supported(inode))
814 if (fsverity_active(inode))
816 if (f2fs_compressed_file(inode))
819 /* disallow direct IO if any of devices has unaligned blksize */
820 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
823 * for blkzoned device, fallback direct IO to buffered IO, so
824 * all IOs can be serialized by log-structured write.
826 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE))
828 if (f2fs_lfs_mode(sbi) && rw == WRITE && F2FS_IO_ALIGNED(sbi))
830 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
836 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
837 struct kstat *stat, u32 request_mask, unsigned int query_flags)
839 struct inode *inode = d_inode(path->dentry);
840 struct f2fs_inode_info *fi = F2FS_I(inode);
841 struct f2fs_inode *ri = NULL;
844 if (f2fs_has_extra_attr(inode) &&
845 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
846 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
847 stat->result_mask |= STATX_BTIME;
848 stat->btime.tv_sec = fi->i_crtime.tv_sec;
849 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
853 * Return the DIO alignment restrictions if requested. We only return
854 * this information when requested, since on encrypted files it might
855 * take a fair bit of work to get if the file wasn't opened recently.
857 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
858 * cannot represent that, so in that case we report no DIO support.
860 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
861 unsigned int bsize = i_blocksize(inode);
863 stat->result_mask |= STATX_DIOALIGN;
864 if (!f2fs_force_buffered_io(inode, WRITE)) {
865 stat->dio_mem_align = bsize;
866 stat->dio_offset_align = bsize;
871 if (flags & F2FS_COMPR_FL)
872 stat->attributes |= STATX_ATTR_COMPRESSED;
873 if (flags & F2FS_APPEND_FL)
874 stat->attributes |= STATX_ATTR_APPEND;
875 if (IS_ENCRYPTED(inode))
876 stat->attributes |= STATX_ATTR_ENCRYPTED;
877 if (flags & F2FS_IMMUTABLE_FL)
878 stat->attributes |= STATX_ATTR_IMMUTABLE;
879 if (flags & F2FS_NODUMP_FL)
880 stat->attributes |= STATX_ATTR_NODUMP;
881 if (IS_VERITY(inode))
882 stat->attributes |= STATX_ATTR_VERITY;
884 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
886 STATX_ATTR_ENCRYPTED |
887 STATX_ATTR_IMMUTABLE |
891 generic_fillattr(mnt_userns, inode, stat);
893 /* we need to show initial sectors used for inline_data/dentries */
894 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
895 f2fs_has_inline_dentry(inode))
896 stat->blocks += (stat->size + 511) >> 9;
901 #ifdef CONFIG_F2FS_FS_POSIX_ACL
902 static void __setattr_copy(struct user_namespace *mnt_userns,
903 struct inode *inode, const struct iattr *attr)
905 unsigned int ia_valid = attr->ia_valid;
907 i_uid_update(mnt_userns, attr, inode);
908 i_gid_update(mnt_userns, attr, inode);
909 if (ia_valid & ATTR_ATIME)
910 inode->i_atime = attr->ia_atime;
911 if (ia_valid & ATTR_MTIME)
912 inode->i_mtime = attr->ia_mtime;
913 if (ia_valid & ATTR_CTIME)
914 inode->i_ctime = attr->ia_ctime;
915 if (ia_valid & ATTR_MODE) {
916 umode_t mode = attr->ia_mode;
917 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
919 if (!vfsgid_in_group_p(vfsgid) &&
920 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
922 set_acl_inode(inode, mode);
926 #define __setattr_copy setattr_copy
929 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
932 struct inode *inode = d_inode(dentry);
935 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
938 if (unlikely(IS_IMMUTABLE(inode)))
941 if (unlikely(IS_APPEND(inode) &&
942 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
943 ATTR_GID | ATTR_TIMES_SET))))
946 if ((attr->ia_valid & ATTR_SIZE) &&
947 !f2fs_is_compress_backend_ready(inode))
950 err = setattr_prepare(mnt_userns, dentry, attr);
954 err = fscrypt_prepare_setattr(dentry, attr);
958 err = fsverity_prepare_setattr(dentry, attr);
962 if (is_quota_modification(mnt_userns, inode, attr)) {
963 err = f2fs_dquot_initialize(inode);
967 if (i_uid_needs_update(mnt_userns, attr, inode) ||
968 i_gid_needs_update(mnt_userns, attr, inode)) {
969 f2fs_lock_op(F2FS_I_SB(inode));
970 err = dquot_transfer(mnt_userns, inode, attr);
972 set_sbi_flag(F2FS_I_SB(inode),
973 SBI_QUOTA_NEED_REPAIR);
974 f2fs_unlock_op(F2FS_I_SB(inode));
978 * update uid/gid under lock_op(), so that dquot and inode can
979 * be updated atomically.
981 i_uid_update(mnt_userns, attr, inode);
982 i_gid_update(mnt_userns, attr, inode);
983 f2fs_mark_inode_dirty_sync(inode, true);
984 f2fs_unlock_op(F2FS_I_SB(inode));
987 if (attr->ia_valid & ATTR_SIZE) {
988 loff_t old_size = i_size_read(inode);
990 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
992 * should convert inline inode before i_size_write to
993 * keep smaller than inline_data size with inline flag.
995 err = f2fs_convert_inline_inode(inode);
1000 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1001 filemap_invalidate_lock(inode->i_mapping);
1003 truncate_setsize(inode, attr->ia_size);
1005 if (attr->ia_size <= old_size)
1006 err = f2fs_truncate(inode);
1008 * do not trim all blocks after i_size if target size is
1009 * larger than i_size.
1011 filemap_invalidate_unlock(inode->i_mapping);
1012 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1016 spin_lock(&F2FS_I(inode)->i_size_lock);
1017 inode->i_mtime = inode->i_ctime = current_time(inode);
1018 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1019 spin_unlock(&F2FS_I(inode)->i_size_lock);
1022 __setattr_copy(mnt_userns, inode, attr);
1024 if (attr->ia_valid & ATTR_MODE) {
1025 err = posix_acl_chmod(mnt_userns, dentry, f2fs_get_inode_mode(inode));
1027 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1029 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1030 clear_inode_flag(inode, FI_ACL_MODE);
1034 /* file size may changed here */
1035 f2fs_mark_inode_dirty_sync(inode, true);
1037 /* inode change will produce dirty node pages flushed by checkpoint */
1038 f2fs_balance_fs(F2FS_I_SB(inode), true);
1043 const struct inode_operations f2fs_file_inode_operations = {
1044 .getattr = f2fs_getattr,
1045 .setattr = f2fs_setattr,
1046 .get_inode_acl = f2fs_get_acl,
1047 .set_acl = f2fs_set_acl,
1048 .listxattr = f2fs_listxattr,
1049 .fiemap = f2fs_fiemap,
1050 .fileattr_get = f2fs_fileattr_get,
1051 .fileattr_set = f2fs_fileattr_set,
1054 static int fill_zero(struct inode *inode, pgoff_t index,
1055 loff_t start, loff_t len)
1057 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1063 f2fs_balance_fs(sbi, true);
1066 page = f2fs_get_new_data_page(inode, NULL, index, false);
1067 f2fs_unlock_op(sbi);
1070 return PTR_ERR(page);
1072 f2fs_wait_on_page_writeback(page, DATA, true, true);
1073 zero_user(page, start, len);
1074 set_page_dirty(page);
1075 f2fs_put_page(page, 1);
1079 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1083 while (pg_start < pg_end) {
1084 struct dnode_of_data dn;
1085 pgoff_t end_offset, count;
1087 set_new_dnode(&dn, inode, NULL, NULL, 0);
1088 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1090 if (err == -ENOENT) {
1091 pg_start = f2fs_get_next_page_offset(&dn,
1098 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1099 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1101 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1103 f2fs_truncate_data_blocks_range(&dn, count);
1104 f2fs_put_dnode(&dn);
1111 static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1113 pgoff_t pg_start, pg_end;
1114 loff_t off_start, off_end;
1117 ret = f2fs_convert_inline_inode(inode);
1121 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1122 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1124 off_start = offset & (PAGE_SIZE - 1);
1125 off_end = (offset + len) & (PAGE_SIZE - 1);
1127 if (pg_start == pg_end) {
1128 ret = fill_zero(inode, pg_start, off_start,
1129 off_end - off_start);
1134 ret = fill_zero(inode, pg_start++, off_start,
1135 PAGE_SIZE - off_start);
1140 ret = fill_zero(inode, pg_end, 0, off_end);
1145 if (pg_start < pg_end) {
1146 loff_t blk_start, blk_end;
1147 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1149 f2fs_balance_fs(sbi, true);
1151 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1152 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1154 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1155 filemap_invalidate_lock(inode->i_mapping);
1157 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1160 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1161 f2fs_unlock_op(sbi);
1163 filemap_invalidate_unlock(inode->i_mapping);
1164 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1171 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1172 int *do_replace, pgoff_t off, pgoff_t len)
1174 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1175 struct dnode_of_data dn;
1179 set_new_dnode(&dn, inode, NULL, NULL, 0);
1180 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1181 if (ret && ret != -ENOENT) {
1183 } else if (ret == -ENOENT) {
1184 if (dn.max_level == 0)
1186 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1187 dn.ofs_in_node, len);
1193 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1194 dn.ofs_in_node, len);
1195 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1196 *blkaddr = f2fs_data_blkaddr(&dn);
1198 if (__is_valid_data_blkaddr(*blkaddr) &&
1199 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1200 DATA_GENERIC_ENHANCE)) {
1201 f2fs_put_dnode(&dn);
1202 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1203 return -EFSCORRUPTED;
1206 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1208 if (f2fs_lfs_mode(sbi)) {
1209 f2fs_put_dnode(&dn);
1213 /* do not invalidate this block address */
1214 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1218 f2fs_put_dnode(&dn);
1227 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1228 int *do_replace, pgoff_t off, int len)
1230 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1231 struct dnode_of_data dn;
1234 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1235 if (*do_replace == 0)
1238 set_new_dnode(&dn, inode, NULL, NULL, 0);
1239 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1241 dec_valid_block_count(sbi, inode, 1);
1242 f2fs_invalidate_blocks(sbi, *blkaddr);
1244 f2fs_update_data_blkaddr(&dn, *blkaddr);
1246 f2fs_put_dnode(&dn);
1251 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1252 block_t *blkaddr, int *do_replace,
1253 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1255 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1260 if (blkaddr[i] == NULL_ADDR && !full) {
1265 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1266 struct dnode_of_data dn;
1267 struct node_info ni;
1271 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1272 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1276 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1278 f2fs_put_dnode(&dn);
1282 ilen = min((pgoff_t)
1283 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1284 dn.ofs_in_node, len - i);
1286 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1287 f2fs_truncate_data_blocks_range(&dn, 1);
1289 if (do_replace[i]) {
1290 f2fs_i_blocks_write(src_inode,
1292 f2fs_i_blocks_write(dst_inode,
1294 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1295 blkaddr[i], ni.version, true, false);
1301 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1302 if (dst_inode->i_size < new_size)
1303 f2fs_i_size_write(dst_inode, new_size);
1304 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1306 f2fs_put_dnode(&dn);
1308 struct page *psrc, *pdst;
1310 psrc = f2fs_get_lock_data_page(src_inode,
1313 return PTR_ERR(psrc);
1314 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1317 f2fs_put_page(psrc, 1);
1318 return PTR_ERR(pdst);
1320 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1321 set_page_dirty(pdst);
1322 f2fs_put_page(pdst, 1);
1323 f2fs_put_page(psrc, 1);
1325 ret = f2fs_truncate_hole(src_inode,
1326 src + i, src + i + 1);
1335 static int __exchange_data_block(struct inode *src_inode,
1336 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1337 pgoff_t len, bool full)
1339 block_t *src_blkaddr;
1345 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1347 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1348 array_size(olen, sizeof(block_t)),
1353 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1354 array_size(olen, sizeof(int)),
1357 kvfree(src_blkaddr);
1361 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1362 do_replace, src, olen);
1366 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1367 do_replace, src, dst, olen, full);
1375 kvfree(src_blkaddr);
1381 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1382 kvfree(src_blkaddr);
1387 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1389 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1390 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1391 pgoff_t start = offset >> PAGE_SHIFT;
1392 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1395 f2fs_balance_fs(sbi, true);
1397 /* avoid gc operation during block exchange */
1398 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1399 filemap_invalidate_lock(inode->i_mapping);
1402 f2fs_drop_extent_tree(inode);
1403 truncate_pagecache(inode, offset);
1404 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1405 f2fs_unlock_op(sbi);
1407 filemap_invalidate_unlock(inode->i_mapping);
1408 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1412 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1417 if (offset + len >= i_size_read(inode))
1420 /* collapse range should be aligned to block size of f2fs. */
1421 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1424 ret = f2fs_convert_inline_inode(inode);
1428 /* write out all dirty pages from offset */
1429 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1433 ret = f2fs_do_collapse(inode, offset, len);
1437 /* write out all moved pages, if possible */
1438 filemap_invalidate_lock(inode->i_mapping);
1439 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1440 truncate_pagecache(inode, offset);
1442 new_size = i_size_read(inode) - len;
1443 ret = f2fs_truncate_blocks(inode, new_size, true);
1444 filemap_invalidate_unlock(inode->i_mapping);
1446 f2fs_i_size_write(inode, new_size);
1450 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1453 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1454 pgoff_t index = start;
1455 unsigned int ofs_in_node = dn->ofs_in_node;
1459 for (; index < end; index++, dn->ofs_in_node++) {
1460 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1464 dn->ofs_in_node = ofs_in_node;
1465 ret = f2fs_reserve_new_blocks(dn, count);
1469 dn->ofs_in_node = ofs_in_node;
1470 for (index = start; index < end; index++, dn->ofs_in_node++) {
1471 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1473 * f2fs_reserve_new_blocks will not guarantee entire block
1476 if (dn->data_blkaddr == NULL_ADDR) {
1481 if (dn->data_blkaddr == NEW_ADDR)
1484 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1485 DATA_GENERIC_ENHANCE)) {
1486 ret = -EFSCORRUPTED;
1487 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1491 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1492 dn->data_blkaddr = NEW_ADDR;
1493 f2fs_set_data_blkaddr(dn);
1496 f2fs_update_read_extent_cache_range(dn, start, 0, index - start);
1497 f2fs_update_age_extent_cache_range(dn, start, index - start);
1502 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1505 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1506 struct address_space *mapping = inode->i_mapping;
1507 pgoff_t index, pg_start, pg_end;
1508 loff_t new_size = i_size_read(inode);
1509 loff_t off_start, off_end;
1512 ret = inode_newsize_ok(inode, (len + offset));
1516 ret = f2fs_convert_inline_inode(inode);
1520 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1524 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1525 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1527 off_start = offset & (PAGE_SIZE - 1);
1528 off_end = (offset + len) & (PAGE_SIZE - 1);
1530 if (pg_start == pg_end) {
1531 ret = fill_zero(inode, pg_start, off_start,
1532 off_end - off_start);
1536 new_size = max_t(loff_t, new_size, offset + len);
1539 ret = fill_zero(inode, pg_start++, off_start,
1540 PAGE_SIZE - off_start);
1544 new_size = max_t(loff_t, new_size,
1545 (loff_t)pg_start << PAGE_SHIFT);
1548 for (index = pg_start; index < pg_end;) {
1549 struct dnode_of_data dn;
1550 unsigned int end_offset;
1553 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1554 filemap_invalidate_lock(mapping);
1556 truncate_pagecache_range(inode,
1557 (loff_t)index << PAGE_SHIFT,
1558 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1562 set_new_dnode(&dn, inode, NULL, NULL, 0);
1563 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1565 f2fs_unlock_op(sbi);
1566 filemap_invalidate_unlock(mapping);
1567 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1571 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1572 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1574 ret = f2fs_do_zero_range(&dn, index, end);
1575 f2fs_put_dnode(&dn);
1577 f2fs_unlock_op(sbi);
1578 filemap_invalidate_unlock(mapping);
1579 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1581 f2fs_balance_fs(sbi, dn.node_changed);
1587 new_size = max_t(loff_t, new_size,
1588 (loff_t)index << PAGE_SHIFT);
1592 ret = fill_zero(inode, pg_end, 0, off_end);
1596 new_size = max_t(loff_t, new_size, offset + len);
1601 if (new_size > i_size_read(inode)) {
1602 if (mode & FALLOC_FL_KEEP_SIZE)
1603 file_set_keep_isize(inode);
1605 f2fs_i_size_write(inode, new_size);
1610 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1612 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1613 struct address_space *mapping = inode->i_mapping;
1614 pgoff_t nr, pg_start, pg_end, delta, idx;
1618 new_size = i_size_read(inode) + len;
1619 ret = inode_newsize_ok(inode, new_size);
1623 if (offset >= i_size_read(inode))
1626 /* insert range should be aligned to block size of f2fs. */
1627 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1630 ret = f2fs_convert_inline_inode(inode);
1634 f2fs_balance_fs(sbi, true);
1636 filemap_invalidate_lock(mapping);
1637 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1638 filemap_invalidate_unlock(mapping);
1642 /* write out all dirty pages from offset */
1643 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1647 pg_start = offset >> PAGE_SHIFT;
1648 pg_end = (offset + len) >> PAGE_SHIFT;
1649 delta = pg_end - pg_start;
1650 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1652 /* avoid gc operation during block exchange */
1653 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1654 filemap_invalidate_lock(mapping);
1655 truncate_pagecache(inode, offset);
1657 while (!ret && idx > pg_start) {
1658 nr = idx - pg_start;
1664 f2fs_drop_extent_tree(inode);
1666 ret = __exchange_data_block(inode, inode, idx,
1667 idx + delta, nr, false);
1668 f2fs_unlock_op(sbi);
1670 filemap_invalidate_unlock(mapping);
1671 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1673 /* write out all moved pages, if possible */
1674 filemap_invalidate_lock(mapping);
1675 filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1676 truncate_pagecache(inode, offset);
1677 filemap_invalidate_unlock(mapping);
1680 f2fs_i_size_write(inode, new_size);
1684 static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
1685 loff_t len, int mode)
1687 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1688 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1689 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1690 .m_may_create = true };
1691 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1692 .init_gc_type = FG_GC,
1693 .should_migrate_blocks = false,
1694 .err_gc_skipped = true,
1695 .nr_free_secs = 0 };
1696 pgoff_t pg_start, pg_end;
1699 block_t expanded = 0;
1702 err = inode_newsize_ok(inode, (len + offset));
1706 err = f2fs_convert_inline_inode(inode);
1710 f2fs_balance_fs(sbi, true);
1712 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1713 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1714 off_end = (offset + len) & (PAGE_SIZE - 1);
1716 map.m_lblk = pg_start;
1717 map.m_len = pg_end - pg_start;
1724 if (f2fs_is_pinned_file(inode)) {
1725 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1726 block_t sec_len = roundup(map.m_len, sec_blks);
1728 map.m_len = sec_blks;
1730 if (has_not_enough_free_secs(sbi, 0,
1731 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1732 f2fs_down_write(&sbi->gc_lock);
1733 err = f2fs_gc(sbi, &gc_control);
1734 if (err && err != -ENODATA)
1738 f2fs_down_write(&sbi->pin_sem);
1741 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1742 f2fs_unlock_op(sbi);
1744 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1745 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
1746 file_dont_truncate(inode);
1748 f2fs_up_write(&sbi->pin_sem);
1750 expanded += map.m_len;
1751 sec_len -= map.m_len;
1752 map.m_lblk += map.m_len;
1753 if (!err && sec_len)
1756 map.m_len = expanded;
1758 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
1759 expanded = map.m_len;
1768 last_off = pg_start + expanded - 1;
1770 /* update new size to the failed position */
1771 new_size = (last_off == pg_end) ? offset + len :
1772 (loff_t)(last_off + 1) << PAGE_SHIFT;
1774 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1777 if (new_size > i_size_read(inode)) {
1778 if (mode & FALLOC_FL_KEEP_SIZE)
1779 file_set_keep_isize(inode);
1781 f2fs_i_size_write(inode, new_size);
1787 static long f2fs_fallocate(struct file *file, int mode,
1788 loff_t offset, loff_t len)
1790 struct inode *inode = file_inode(file);
1793 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1795 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1797 if (!f2fs_is_compress_backend_ready(inode))
1800 /* f2fs only support ->fallocate for regular file */
1801 if (!S_ISREG(inode->i_mode))
1804 if (IS_ENCRYPTED(inode) &&
1805 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1809 * Pinned file should not support partial truncation since the block
1810 * can be used by applications.
1812 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1813 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1814 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1817 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1818 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1819 FALLOC_FL_INSERT_RANGE))
1824 ret = file_modified(file);
1828 if (mode & FALLOC_FL_PUNCH_HOLE) {
1829 if (offset >= inode->i_size)
1832 ret = f2fs_punch_hole(inode, offset, len);
1833 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1834 ret = f2fs_collapse_range(inode, offset, len);
1835 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1836 ret = f2fs_zero_range(inode, offset, len, mode);
1837 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1838 ret = f2fs_insert_range(inode, offset, len);
1840 ret = f2fs_expand_inode_data(inode, offset, len, mode);
1844 inode->i_mtime = inode->i_ctime = current_time(inode);
1845 f2fs_mark_inode_dirty_sync(inode, false);
1846 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1850 inode_unlock(inode);
1852 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1856 static int f2fs_release_file(struct inode *inode, struct file *filp)
1859 * f2fs_release_file is called at every close calls. So we should
1860 * not drop any inmemory pages by close called by other process.
1862 if (!(filp->f_mode & FMODE_WRITE) ||
1863 atomic_read(&inode->i_writecount) != 1)
1867 f2fs_abort_atomic_write(inode, true);
1868 inode_unlock(inode);
1873 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1875 struct inode *inode = file_inode(file);
1878 * If the process doing a transaction is crashed, we should do
1879 * roll-back. Otherwise, other reader/write can see corrupted database
1880 * until all the writers close its file. Since this should be done
1881 * before dropping file lock, it needs to do in ->flush.
1883 if (F2FS_I(inode)->atomic_write_task == current &&
1884 (current->flags & PF_EXITING)) {
1886 f2fs_abort_atomic_write(inode, true);
1887 inode_unlock(inode);
1893 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1895 struct f2fs_inode_info *fi = F2FS_I(inode);
1896 u32 masked_flags = fi->i_flags & mask;
1898 /* mask can be shrunk by flags_valid selector */
1901 /* Is it quota file? Do not allow user to mess with it */
1902 if (IS_NOQUOTA(inode))
1905 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1906 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1908 if (!f2fs_empty_dir(inode))
1912 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1913 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1915 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1919 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1920 if (masked_flags & F2FS_COMPR_FL) {
1921 if (!f2fs_disable_compressed_file(inode))
1924 /* try to convert inline_data to support compression */
1925 int err = f2fs_convert_inline_inode(inode);
1928 if (!f2fs_may_compress(inode))
1930 if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
1932 if (set_compress_context(inode))
1937 fi->i_flags = iflags | (fi->i_flags & ~mask);
1938 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1939 (fi->i_flags & F2FS_NOCOMP_FL));
1941 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1942 set_inode_flag(inode, FI_PROJ_INHERIT);
1944 clear_inode_flag(inode, FI_PROJ_INHERIT);
1946 inode->i_ctime = current_time(inode);
1947 f2fs_set_inode_flags(inode);
1948 f2fs_mark_inode_dirty_sync(inode, true);
1952 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1955 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1956 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1957 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1958 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1960 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1961 * FS_IOC_FSSETXATTR is done by the VFS.
1964 static const struct {
1967 } f2fs_fsflags_map[] = {
1968 { F2FS_COMPR_FL, FS_COMPR_FL },
1969 { F2FS_SYNC_FL, FS_SYNC_FL },
1970 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1971 { F2FS_APPEND_FL, FS_APPEND_FL },
1972 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1973 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1974 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1975 { F2FS_INDEX_FL, FS_INDEX_FL },
1976 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1977 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1978 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1981 #define F2FS_GETTABLE_FS_FL ( \
1991 FS_PROJINHERIT_FL | \
1993 FS_INLINE_DATA_FL | \
1998 #define F2FS_SETTABLE_FS_FL ( \
2007 FS_PROJINHERIT_FL | \
2010 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2011 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2016 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2017 if (iflags & f2fs_fsflags_map[i].iflag)
2018 fsflags |= f2fs_fsflags_map[i].fsflag;
2023 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2024 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2029 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2030 if (fsflags & f2fs_fsflags_map[i].fsflag)
2031 iflags |= f2fs_fsflags_map[i].iflag;
2036 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2038 struct inode *inode = file_inode(filp);
2040 return put_user(inode->i_generation, (int __user *)arg);
2043 static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
2045 struct inode *inode = file_inode(filp);
2046 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
2047 struct f2fs_inode_info *fi = F2FS_I(inode);
2048 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2049 struct inode *pinode;
2053 if (!inode_owner_or_capable(mnt_userns, inode))
2056 if (!S_ISREG(inode->i_mode))
2059 if (filp->f_flags & O_DIRECT)
2062 ret = mnt_want_write_file(filp);
2068 if (!f2fs_disable_compressed_file(inode)) {
2073 if (f2fs_is_atomic_file(inode))
2076 ret = f2fs_convert_inline_inode(inode);
2080 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2083 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2084 * f2fs_is_atomic_file.
2086 if (get_dirty_pages(inode))
2087 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2088 inode->i_ino, get_dirty_pages(inode));
2089 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2091 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2095 /* Check if the inode already has a COW inode */
2096 if (fi->cow_inode == NULL) {
2097 /* Create a COW inode for atomic write */
2098 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2099 if (IS_ERR(pinode)) {
2100 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2101 ret = PTR_ERR(pinode);
2105 ret = f2fs_get_tmpfile(mnt_userns, pinode, &fi->cow_inode);
2108 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2112 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2113 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2115 /* Reuse the already created COW inode */
2116 f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2119 f2fs_write_inode(inode, NULL);
2121 stat_inc_atomic_inode(inode);
2123 set_inode_flag(inode, FI_ATOMIC_FILE);
2125 isize = i_size_read(inode);
2126 fi->original_i_size = isize;
2128 set_inode_flag(inode, FI_ATOMIC_REPLACE);
2129 truncate_inode_pages_final(inode->i_mapping);
2130 f2fs_i_size_write(inode, 0);
2133 f2fs_i_size_write(fi->cow_inode, isize);
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)
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 = {};
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 in-place-update policy is enabled, don't waste time here */
2593 set_inode_flag(inode, FI_OPU_WRITE);
2594 if (f2fs_should_update_inplace(inode, NULL)) {
2599 /* writeback all dirty pages in the range */
2600 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2601 range->start + range->len - 1);
2606 * lookup mapping info in extent cache, skip defragmenting if physical
2607 * block addresses are continuous.
2609 if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
2610 if (ei.fofs + ei.len >= pg_end)
2614 map.m_lblk = pg_start;
2615 map.m_next_pgofs = &next_pgofs;
2618 * lookup mapping info in dnode page cache, skip defragmenting if all
2619 * physical block addresses are continuous even if there are hole(s)
2620 * in logical blocks.
2622 while (map.m_lblk < pg_end) {
2623 map.m_len = pg_end - map.m_lblk;
2624 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2628 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2629 map.m_lblk = next_pgofs;
2633 if (blk_end && blk_end != map.m_pblk)
2636 /* record total count of block that we're going to move */
2639 blk_end = map.m_pblk + map.m_len;
2641 map.m_lblk += map.m_len;
2649 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2652 * make sure there are enough free section for LFS allocation, this can
2653 * avoid defragment running in SSR mode when free section are allocated
2656 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2661 map.m_lblk = pg_start;
2662 map.m_len = pg_end - pg_start;
2665 while (map.m_lblk < pg_end) {
2670 map.m_len = pg_end - map.m_lblk;
2671 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2675 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2676 map.m_lblk = next_pgofs;
2680 set_inode_flag(inode, FI_SKIP_WRITES);
2683 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2686 page = f2fs_get_lock_data_page(inode, idx, true);
2688 err = PTR_ERR(page);
2692 set_page_dirty(page);
2693 set_page_private_gcing(page);
2694 f2fs_put_page(page, 1);
2703 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2706 clear_inode_flag(inode, FI_SKIP_WRITES);
2708 err = filemap_fdatawrite(inode->i_mapping);
2713 clear_inode_flag(inode, FI_SKIP_WRITES);
2715 clear_inode_flag(inode, FI_OPU_WRITE);
2716 inode_unlock(inode);
2718 range->len = (u64)total << PAGE_SHIFT;
2722 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2724 struct inode *inode = file_inode(filp);
2725 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2726 struct f2fs_defragment range;
2729 if (!capable(CAP_SYS_ADMIN))
2732 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2735 if (f2fs_readonly(sbi->sb))
2738 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2742 /* verify alignment of offset & size */
2743 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2746 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2747 max_file_blocks(inode)))
2750 err = mnt_want_write_file(filp);
2754 err = f2fs_defragment_range(sbi, filp, &range);
2755 mnt_drop_write_file(filp);
2757 f2fs_update_time(sbi, REQ_TIME);
2761 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2768 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2769 struct file *file_out, loff_t pos_out, size_t len)
2771 struct inode *src = file_inode(file_in);
2772 struct inode *dst = file_inode(file_out);
2773 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2774 size_t olen = len, dst_max_i_size = 0;
2778 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2779 src->i_sb != dst->i_sb)
2782 if (unlikely(f2fs_readonly(src->i_sb)))
2785 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2788 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2791 if (pos_out < 0 || pos_in < 0)
2795 if (pos_in == pos_out)
2797 if (pos_out > pos_in && pos_out < pos_in + len)
2804 if (!inode_trylock(dst))
2809 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2812 olen = len = src->i_size - pos_in;
2813 if (pos_in + len == src->i_size)
2814 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2820 dst_osize = dst->i_size;
2821 if (pos_out + olen > dst->i_size)
2822 dst_max_i_size = pos_out + olen;
2824 /* verify the end result is block aligned */
2825 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2826 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2827 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2830 ret = f2fs_convert_inline_inode(src);
2834 ret = f2fs_convert_inline_inode(dst);
2838 /* write out all dirty pages from offset */
2839 ret = filemap_write_and_wait_range(src->i_mapping,
2840 pos_in, pos_in + len);
2844 ret = filemap_write_and_wait_range(dst->i_mapping,
2845 pos_out, pos_out + len);
2849 f2fs_balance_fs(sbi, true);
2851 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2854 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2859 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2860 pos_out >> F2FS_BLKSIZE_BITS,
2861 len >> F2FS_BLKSIZE_BITS, false);
2865 f2fs_i_size_write(dst, dst_max_i_size);
2866 else if (dst_osize != dst->i_size)
2867 f2fs_i_size_write(dst, dst_osize);
2869 f2fs_unlock_op(sbi);
2872 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2874 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2883 static int __f2fs_ioc_move_range(struct file *filp,
2884 struct f2fs_move_range *range)
2889 if (!(filp->f_mode & FMODE_READ) ||
2890 !(filp->f_mode & FMODE_WRITE))
2893 dst = fdget(range->dst_fd);
2897 if (!(dst.file->f_mode & FMODE_WRITE)) {
2902 err = mnt_want_write_file(filp);
2906 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2907 range->pos_out, range->len);
2909 mnt_drop_write_file(filp);
2915 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2917 struct f2fs_move_range range;
2919 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2922 return __f2fs_ioc_move_range(filp, &range);
2925 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2927 struct inode *inode = file_inode(filp);
2928 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2929 struct sit_info *sm = SIT_I(sbi);
2930 unsigned int start_segno = 0, end_segno = 0;
2931 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2932 struct f2fs_flush_device range;
2933 struct f2fs_gc_control gc_control = {
2934 .init_gc_type = FG_GC,
2935 .should_migrate_blocks = true,
2936 .err_gc_skipped = true,
2937 .nr_free_secs = 0 };
2940 if (!capable(CAP_SYS_ADMIN))
2943 if (f2fs_readonly(sbi->sb))
2946 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2949 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2953 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2954 __is_large_section(sbi)) {
2955 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2956 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2960 ret = mnt_want_write_file(filp);
2964 if (range.dev_num != 0)
2965 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2966 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2968 start_segno = sm->last_victim[FLUSH_DEVICE];
2969 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2970 start_segno = dev_start_segno;
2971 end_segno = min(start_segno + range.segments, dev_end_segno);
2973 while (start_segno < end_segno) {
2974 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2978 sm->last_victim[GC_CB] = end_segno + 1;
2979 sm->last_victim[GC_GREEDY] = end_segno + 1;
2980 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2982 gc_control.victim_segno = start_segno;
2983 ret = f2fs_gc(sbi, &gc_control);
2991 mnt_drop_write_file(filp);
2995 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2997 struct inode *inode = file_inode(filp);
2998 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3000 /* Must validate to set it with SQLite behavior in Android. */
3001 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3003 return put_user(sb_feature, (u32 __user *)arg);
3007 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3009 struct dquot *transfer_to[MAXQUOTAS] = {};
3010 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3011 struct super_block *sb = sbi->sb;
3014 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3015 if (!IS_ERR(transfer_to[PRJQUOTA])) {
3016 err = __dquot_transfer(inode, transfer_to);
3018 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3019 dqput(transfer_to[PRJQUOTA]);
3024 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3026 struct f2fs_inode_info *fi = F2FS_I(inode);
3027 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3028 struct f2fs_inode *ri = NULL;
3032 if (!f2fs_sb_has_project_quota(sbi)) {
3033 if (projid != F2FS_DEF_PROJID)
3039 if (!f2fs_has_extra_attr(inode))
3042 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3044 if (projid_eq(kprojid, fi->i_projid))
3048 /* Is it quota file? Do not allow user to mess with it */
3049 if (IS_NOQUOTA(inode))
3052 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3055 err = f2fs_dquot_initialize(inode);
3060 err = f2fs_transfer_project_quota(inode, kprojid);
3064 fi->i_projid = kprojid;
3065 inode->i_ctime = current_time(inode);
3066 f2fs_mark_inode_dirty_sync(inode, true);
3068 f2fs_unlock_op(sbi);
3072 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3077 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3079 if (projid != F2FS_DEF_PROJID)
3085 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3087 struct inode *inode = d_inode(dentry);
3088 struct f2fs_inode_info *fi = F2FS_I(inode);
3089 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3091 if (IS_ENCRYPTED(inode))
3092 fsflags |= FS_ENCRYPT_FL;
3093 if (IS_VERITY(inode))
3094 fsflags |= FS_VERITY_FL;
3095 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3096 fsflags |= FS_INLINE_DATA_FL;
3097 if (is_inode_flag_set(inode, FI_PIN_FILE))
3098 fsflags |= FS_NOCOW_FL;
3100 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3102 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3103 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3108 int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3109 struct dentry *dentry, struct fileattr *fa)
3111 struct inode *inode = d_inode(dentry);
3112 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3116 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3118 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3120 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3122 fsflags &= F2FS_SETTABLE_FS_FL;
3123 if (!fa->flags_valid)
3124 mask &= FS_COMMON_FL;
3126 iflags = f2fs_fsflags_to_iflags(fsflags);
3127 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3130 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3132 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3137 int f2fs_pin_file_control(struct inode *inode, bool inc)
3139 struct f2fs_inode_info *fi = F2FS_I(inode);
3140 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3142 /* Use i_gc_failures for normal file as a risk signal. */
3144 f2fs_i_gc_failures_write(inode,
3145 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3147 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3148 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3149 __func__, inode->i_ino,
3150 fi->i_gc_failures[GC_FAILURE_PIN]);
3151 clear_inode_flag(inode, FI_PIN_FILE);
3157 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3159 struct inode *inode = file_inode(filp);
3163 if (get_user(pin, (__u32 __user *)arg))
3166 if (!S_ISREG(inode->i_mode))
3169 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3172 ret = mnt_want_write_file(filp);
3179 clear_inode_flag(inode, FI_PIN_FILE);
3180 f2fs_i_gc_failures_write(inode, 0);
3184 if (f2fs_should_update_outplace(inode, NULL)) {
3189 if (f2fs_pin_file_control(inode, false)) {
3194 ret = f2fs_convert_inline_inode(inode);
3198 if (!f2fs_disable_compressed_file(inode)) {
3203 set_inode_flag(inode, FI_PIN_FILE);
3204 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3206 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3208 inode_unlock(inode);
3209 mnt_drop_write_file(filp);
3213 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3215 struct inode *inode = file_inode(filp);
3218 if (is_inode_flag_set(inode, FI_PIN_FILE))
3219 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3220 return put_user(pin, (u32 __user *)arg);
3223 int f2fs_precache_extents(struct inode *inode)
3225 struct f2fs_inode_info *fi = F2FS_I(inode);
3226 struct f2fs_map_blocks map;
3227 pgoff_t m_next_extent;
3231 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3235 map.m_next_pgofs = NULL;
3236 map.m_next_extent = &m_next_extent;
3237 map.m_seg_type = NO_CHECK_TYPE;
3238 map.m_may_create = false;
3239 end = max_file_blocks(inode);
3241 while (map.m_lblk < end) {
3242 map.m_len = end - map.m_lblk;
3244 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3245 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
3246 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3250 map.m_lblk = m_next_extent;
3256 static int f2fs_ioc_precache_extents(struct file *filp)
3258 return f2fs_precache_extents(file_inode(filp));
3261 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3263 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3266 if (!capable(CAP_SYS_ADMIN))
3269 if (f2fs_readonly(sbi->sb))
3272 if (copy_from_user(&block_count, (void __user *)arg,
3273 sizeof(block_count)))
3276 return f2fs_resize_fs(sbi, block_count);
3279 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3281 struct inode *inode = file_inode(filp);
3283 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3285 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3286 f2fs_warn(F2FS_I_SB(inode),
3287 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3292 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3295 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3297 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3300 return fsverity_ioctl_measure(filp, (void __user *)arg);
3303 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3305 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3308 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3311 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3313 struct inode *inode = file_inode(filp);
3314 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3319 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3323 f2fs_down_read(&sbi->sb_lock);
3324 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3325 ARRAY_SIZE(sbi->raw_super->volume_name),
3326 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3327 f2fs_up_read(&sbi->sb_lock);
3329 if (copy_to_user((char __user *)arg, vbuf,
3330 min(FSLABEL_MAX, count)))
3337 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3339 struct inode *inode = file_inode(filp);
3340 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3344 if (!capable(CAP_SYS_ADMIN))
3347 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3349 return PTR_ERR(vbuf);
3351 err = mnt_want_write_file(filp);
3355 f2fs_down_write(&sbi->sb_lock);
3357 memset(sbi->raw_super->volume_name, 0,
3358 sizeof(sbi->raw_super->volume_name));
3359 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3360 sbi->raw_super->volume_name,
3361 ARRAY_SIZE(sbi->raw_super->volume_name));
3363 err = f2fs_commit_super(sbi, false);
3365 f2fs_up_write(&sbi->sb_lock);
3367 mnt_drop_write_file(filp);
3373 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3375 struct inode *inode = file_inode(filp);
3378 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3381 if (!f2fs_compressed_file(inode))
3384 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3385 return put_user(blocks, (u64 __user *)arg);
3388 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3390 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3391 unsigned int released_blocks = 0;
3392 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3396 for (i = 0; i < count; i++) {
3397 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3398 dn->ofs_in_node + i);
3400 if (!__is_valid_data_blkaddr(blkaddr))
3402 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3403 DATA_GENERIC_ENHANCE))) {
3404 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3405 return -EFSCORRUPTED;
3410 int compr_blocks = 0;
3412 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3413 blkaddr = f2fs_data_blkaddr(dn);
3416 if (blkaddr == COMPRESS_ADDR)
3418 dn->ofs_in_node += cluster_size;
3422 if (__is_valid_data_blkaddr(blkaddr))
3425 if (blkaddr != NEW_ADDR)
3428 dn->data_blkaddr = NULL_ADDR;
3429 f2fs_set_data_blkaddr(dn);
3432 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3433 dec_valid_block_count(sbi, dn->inode,
3434 cluster_size - compr_blocks);
3436 released_blocks += cluster_size - compr_blocks;
3438 count -= cluster_size;
3441 return released_blocks;
3444 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3446 struct inode *inode = file_inode(filp);
3447 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3448 pgoff_t page_idx = 0, last_idx;
3449 unsigned int released_blocks = 0;
3453 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3456 if (!f2fs_compressed_file(inode))
3459 if (f2fs_readonly(sbi->sb))
3462 ret = mnt_want_write_file(filp);
3466 f2fs_balance_fs(F2FS_I_SB(inode), true);
3470 writecount = atomic_read(&inode->i_writecount);
3471 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3472 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3477 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3482 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3486 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3487 inode->i_ctime = current_time(inode);
3488 f2fs_mark_inode_dirty_sync(inode, true);
3490 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3493 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3494 filemap_invalidate_lock(inode->i_mapping);
3496 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3498 while (page_idx < last_idx) {
3499 struct dnode_of_data dn;
3500 pgoff_t end_offset, count;
3502 set_new_dnode(&dn, inode, NULL, NULL, 0);
3503 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3505 if (ret == -ENOENT) {
3506 page_idx = f2fs_get_next_page_offset(&dn,
3514 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3515 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3516 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3518 ret = release_compress_blocks(&dn, count);
3520 f2fs_put_dnode(&dn);
3526 released_blocks += ret;
3529 filemap_invalidate_unlock(inode->i_mapping);
3530 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3532 inode_unlock(inode);
3534 mnt_drop_write_file(filp);
3537 ret = put_user(released_blocks, (u64 __user *)arg);
3538 } else if (released_blocks &&
3539 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3540 set_sbi_flag(sbi, SBI_NEED_FSCK);
3541 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3542 "iblocks=%llu, released=%u, compr_blocks=%u, "
3544 __func__, inode->i_ino, inode->i_blocks,
3546 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3552 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3554 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3555 unsigned int reserved_blocks = 0;
3556 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3560 for (i = 0; i < count; i++) {
3561 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3562 dn->ofs_in_node + i);
3564 if (!__is_valid_data_blkaddr(blkaddr))
3566 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3567 DATA_GENERIC_ENHANCE))) {
3568 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3569 return -EFSCORRUPTED;
3574 int compr_blocks = 0;
3578 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3579 blkaddr = f2fs_data_blkaddr(dn);
3582 if (blkaddr == COMPRESS_ADDR)
3584 dn->ofs_in_node += cluster_size;
3588 if (__is_valid_data_blkaddr(blkaddr)) {
3593 dn->data_blkaddr = NEW_ADDR;
3594 f2fs_set_data_blkaddr(dn);
3597 reserved = cluster_size - compr_blocks;
3598 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3602 if (reserved != cluster_size - compr_blocks)
3605 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3607 reserved_blocks += reserved;
3609 count -= cluster_size;
3612 return reserved_blocks;
3615 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3617 struct inode *inode = file_inode(filp);
3618 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3619 pgoff_t page_idx = 0, last_idx;
3620 unsigned int reserved_blocks = 0;
3623 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3626 if (!f2fs_compressed_file(inode))
3629 if (f2fs_readonly(sbi->sb))
3632 ret = mnt_want_write_file(filp);
3636 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3639 f2fs_balance_fs(F2FS_I_SB(inode), true);
3643 if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3648 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3649 filemap_invalidate_lock(inode->i_mapping);
3651 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3653 while (page_idx < last_idx) {
3654 struct dnode_of_data dn;
3655 pgoff_t end_offset, count;
3657 set_new_dnode(&dn, inode, NULL, NULL, 0);
3658 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3660 if (ret == -ENOENT) {
3661 page_idx = f2fs_get_next_page_offset(&dn,
3669 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3670 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3671 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3673 ret = reserve_compress_blocks(&dn, count);
3675 f2fs_put_dnode(&dn);
3681 reserved_blocks += ret;
3684 filemap_invalidate_unlock(inode->i_mapping);
3685 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3688 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3689 inode->i_ctime = current_time(inode);
3690 f2fs_mark_inode_dirty_sync(inode, true);
3693 inode_unlock(inode);
3695 mnt_drop_write_file(filp);
3698 ret = put_user(reserved_blocks, (u64 __user *)arg);
3699 } else if (reserved_blocks &&
3700 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3701 set_sbi_flag(sbi, SBI_NEED_FSCK);
3702 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3703 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3705 __func__, inode->i_ino, inode->i_blocks,
3707 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3713 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3714 pgoff_t off, block_t block, block_t len, u32 flags)
3716 sector_t sector = SECTOR_FROM_BLOCK(block);
3717 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3720 if (flags & F2FS_TRIM_FILE_DISCARD) {
3721 if (bdev_max_secure_erase_sectors(bdev))
3722 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3725 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3729 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3730 if (IS_ENCRYPTED(inode))
3731 ret = fscrypt_zeroout_range(inode, off, block, len);
3733 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3740 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3742 struct inode *inode = file_inode(filp);
3743 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3744 struct address_space *mapping = inode->i_mapping;
3745 struct block_device *prev_bdev = NULL;
3746 struct f2fs_sectrim_range range;
3747 pgoff_t index, pg_end, prev_index = 0;
3748 block_t prev_block = 0, len = 0;
3750 bool to_end = false;
3753 if (!(filp->f_mode & FMODE_WRITE))
3756 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3760 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3761 !S_ISREG(inode->i_mode))
3764 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3765 !f2fs_hw_support_discard(sbi)) ||
3766 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3767 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3770 file_start_write(filp);
3773 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3774 range.start >= inode->i_size) {
3782 if (inode->i_size - range.start > range.len) {
3783 end_addr = range.start + range.len;
3785 end_addr = range.len == (u64)-1 ?
3786 sbi->sb->s_maxbytes : inode->i_size;
3790 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3791 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3796 index = F2FS_BYTES_TO_BLK(range.start);
3797 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3799 ret = f2fs_convert_inline_inode(inode);
3803 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3804 filemap_invalidate_lock(mapping);
3806 ret = filemap_write_and_wait_range(mapping, range.start,
3807 to_end ? LLONG_MAX : end_addr - 1);
3811 truncate_inode_pages_range(mapping, range.start,
3812 to_end ? -1 : end_addr - 1);
3814 while (index < pg_end) {
3815 struct dnode_of_data dn;
3816 pgoff_t end_offset, count;
3819 set_new_dnode(&dn, inode, NULL, NULL, 0);
3820 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3822 if (ret == -ENOENT) {
3823 index = f2fs_get_next_page_offset(&dn, index);
3829 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3830 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3831 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3832 struct block_device *cur_bdev;
3833 block_t blkaddr = f2fs_data_blkaddr(&dn);
3835 if (!__is_valid_data_blkaddr(blkaddr))
3838 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3839 DATA_GENERIC_ENHANCE)) {
3840 ret = -EFSCORRUPTED;
3841 f2fs_put_dnode(&dn);
3842 f2fs_handle_error(sbi,
3843 ERROR_INVALID_BLKADDR);
3847 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3848 if (f2fs_is_multi_device(sbi)) {
3849 int di = f2fs_target_device_index(sbi, blkaddr);
3851 blkaddr -= FDEV(di).start_blk;
3855 if (prev_bdev == cur_bdev &&
3856 index == prev_index + len &&
3857 blkaddr == prev_block + len) {
3860 ret = f2fs_secure_erase(prev_bdev,
3861 inode, prev_index, prev_block,
3864 f2fs_put_dnode(&dn);
3873 prev_bdev = cur_bdev;
3875 prev_block = blkaddr;
3880 f2fs_put_dnode(&dn);
3882 if (fatal_signal_pending(current)) {
3890 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3891 prev_block, len, range.flags);
3893 filemap_invalidate_unlock(mapping);
3894 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3896 inode_unlock(inode);
3897 file_end_write(filp);
3902 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3904 struct inode *inode = file_inode(filp);
3905 struct f2fs_comp_option option;
3907 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3910 inode_lock_shared(inode);
3912 if (!f2fs_compressed_file(inode)) {
3913 inode_unlock_shared(inode);
3917 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3918 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3920 inode_unlock_shared(inode);
3922 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3929 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3931 struct inode *inode = file_inode(filp);
3932 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3933 struct f2fs_comp_option option;
3936 if (!f2fs_sb_has_compression(sbi))
3939 if (!(filp->f_mode & FMODE_WRITE))
3942 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3946 if (!f2fs_compressed_file(inode) ||
3947 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3948 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3949 option.algorithm >= COMPRESS_MAX)
3952 file_start_write(filp);
3955 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3960 if (F2FS_HAS_BLOCKS(inode)) {
3965 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3966 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3967 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
3968 f2fs_mark_inode_dirty_sync(inode, true);
3970 if (!f2fs_is_compress_backend_ready(inode))
3971 f2fs_warn(sbi, "compression algorithm is successfully set, "
3972 "but current kernel doesn't support this algorithm.");
3974 inode_unlock(inode);
3975 file_end_write(filp);
3980 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
3982 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
3983 struct address_space *mapping = inode->i_mapping;
3985 pgoff_t redirty_idx = page_idx;
3986 int i, page_len = 0, ret = 0;
3988 page_cache_ra_unbounded(&ractl, len, 0);
3990 for (i = 0; i < len; i++, page_idx++) {
3991 page = read_cache_page(mapping, page_idx, NULL, NULL);
3993 ret = PTR_ERR(page);
3999 for (i = 0; i < page_len; i++, redirty_idx++) {
4000 page = find_lock_page(mapping, redirty_idx);
4002 /* It will never fail, when page has pinned above */
4003 f2fs_bug_on(F2FS_I_SB(inode), !page);
4005 set_page_dirty(page);
4006 f2fs_put_page(page, 1);
4007 f2fs_put_page(page, 0);
4013 static int f2fs_ioc_decompress_file(struct file *filp)
4015 struct inode *inode = file_inode(filp);
4016 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4017 struct f2fs_inode_info *fi = F2FS_I(inode);
4018 pgoff_t page_idx = 0, last_idx;
4019 unsigned int blk_per_seg = sbi->blocks_per_seg;
4020 int cluster_size = fi->i_cluster_size;
4023 if (!f2fs_sb_has_compression(sbi) ||
4024 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4027 if (!(filp->f_mode & FMODE_WRITE))
4030 if (!f2fs_compressed_file(inode))
4033 f2fs_balance_fs(F2FS_I_SB(inode), true);
4035 file_start_write(filp);
4038 if (!f2fs_is_compress_backend_ready(inode)) {
4043 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4048 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4052 if (!atomic_read(&fi->i_compr_blocks))
4055 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4057 count = last_idx - page_idx;
4059 int len = min(cluster_size, count);
4061 ret = redirty_blocks(inode, page_idx, len);
4065 if (get_dirty_pages(inode) >= blk_per_seg)
4066 filemap_fdatawrite(inode->i_mapping);
4073 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4077 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4080 inode_unlock(inode);
4081 file_end_write(filp);
4086 static int f2fs_ioc_compress_file(struct file *filp)
4088 struct inode *inode = file_inode(filp);
4089 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4090 pgoff_t page_idx = 0, last_idx;
4091 unsigned int blk_per_seg = sbi->blocks_per_seg;
4092 int cluster_size = F2FS_I(inode)->i_cluster_size;
4095 if (!f2fs_sb_has_compression(sbi) ||
4096 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4099 if (!(filp->f_mode & FMODE_WRITE))
4102 if (!f2fs_compressed_file(inode))
4105 f2fs_balance_fs(F2FS_I_SB(inode), true);
4107 file_start_write(filp);
4110 if (!f2fs_is_compress_backend_ready(inode)) {
4115 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4120 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4124 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4126 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4128 count = last_idx - page_idx;
4130 int len = min(cluster_size, count);
4132 ret = redirty_blocks(inode, page_idx, len);
4136 if (get_dirty_pages(inode) >= blk_per_seg)
4137 filemap_fdatawrite(inode->i_mapping);
4144 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4147 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4150 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4153 inode_unlock(inode);
4154 file_end_write(filp);
4159 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4162 case FS_IOC_GETVERSION:
4163 return f2fs_ioc_getversion(filp, arg);
4164 case F2FS_IOC_START_ATOMIC_WRITE:
4165 return f2fs_ioc_start_atomic_write(filp, false);
4166 case F2FS_IOC_START_ATOMIC_REPLACE:
4167 return f2fs_ioc_start_atomic_write(filp, true);
4168 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4169 return f2fs_ioc_commit_atomic_write(filp);
4170 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4171 return f2fs_ioc_abort_atomic_write(filp);
4172 case F2FS_IOC_START_VOLATILE_WRITE:
4173 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4175 case F2FS_IOC_SHUTDOWN:
4176 return f2fs_ioc_shutdown(filp, arg);
4178 return f2fs_ioc_fitrim(filp, arg);
4179 case FS_IOC_SET_ENCRYPTION_POLICY:
4180 return f2fs_ioc_set_encryption_policy(filp, arg);
4181 case FS_IOC_GET_ENCRYPTION_POLICY:
4182 return f2fs_ioc_get_encryption_policy(filp, arg);
4183 case FS_IOC_GET_ENCRYPTION_PWSALT:
4184 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4185 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4186 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4187 case FS_IOC_ADD_ENCRYPTION_KEY:
4188 return f2fs_ioc_add_encryption_key(filp, arg);
4189 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4190 return f2fs_ioc_remove_encryption_key(filp, arg);
4191 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4192 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4193 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4194 return f2fs_ioc_get_encryption_key_status(filp, arg);
4195 case FS_IOC_GET_ENCRYPTION_NONCE:
4196 return f2fs_ioc_get_encryption_nonce(filp, arg);
4197 case F2FS_IOC_GARBAGE_COLLECT:
4198 return f2fs_ioc_gc(filp, arg);
4199 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4200 return f2fs_ioc_gc_range(filp, arg);
4201 case F2FS_IOC_WRITE_CHECKPOINT:
4202 return f2fs_ioc_write_checkpoint(filp);
4203 case F2FS_IOC_DEFRAGMENT:
4204 return f2fs_ioc_defragment(filp, arg);
4205 case F2FS_IOC_MOVE_RANGE:
4206 return f2fs_ioc_move_range(filp, arg);
4207 case F2FS_IOC_FLUSH_DEVICE:
4208 return f2fs_ioc_flush_device(filp, arg);
4209 case F2FS_IOC_GET_FEATURES:
4210 return f2fs_ioc_get_features(filp, arg);
4211 case F2FS_IOC_GET_PIN_FILE:
4212 return f2fs_ioc_get_pin_file(filp, arg);
4213 case F2FS_IOC_SET_PIN_FILE:
4214 return f2fs_ioc_set_pin_file(filp, arg);
4215 case F2FS_IOC_PRECACHE_EXTENTS:
4216 return f2fs_ioc_precache_extents(filp);
4217 case F2FS_IOC_RESIZE_FS:
4218 return f2fs_ioc_resize_fs(filp, arg);
4219 case FS_IOC_ENABLE_VERITY:
4220 return f2fs_ioc_enable_verity(filp, arg);
4221 case FS_IOC_MEASURE_VERITY:
4222 return f2fs_ioc_measure_verity(filp, arg);
4223 case FS_IOC_READ_VERITY_METADATA:
4224 return f2fs_ioc_read_verity_metadata(filp, arg);
4225 case FS_IOC_GETFSLABEL:
4226 return f2fs_ioc_getfslabel(filp, arg);
4227 case FS_IOC_SETFSLABEL:
4228 return f2fs_ioc_setfslabel(filp, arg);
4229 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4230 return f2fs_get_compress_blocks(filp, arg);
4231 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4232 return f2fs_release_compress_blocks(filp, arg);
4233 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4234 return f2fs_reserve_compress_blocks(filp, arg);
4235 case F2FS_IOC_SEC_TRIM_FILE:
4236 return f2fs_sec_trim_file(filp, arg);
4237 case F2FS_IOC_GET_COMPRESS_OPTION:
4238 return f2fs_ioc_get_compress_option(filp, arg);
4239 case F2FS_IOC_SET_COMPRESS_OPTION:
4240 return f2fs_ioc_set_compress_option(filp, arg);
4241 case F2FS_IOC_DECOMPRESS_FILE:
4242 return f2fs_ioc_decompress_file(filp);
4243 case F2FS_IOC_COMPRESS_FILE:
4244 return f2fs_ioc_compress_file(filp);
4250 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4252 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4254 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4257 return __f2fs_ioctl(filp, cmd, arg);
4261 * Return %true if the given read or write request should use direct I/O, or
4262 * %false if it should use buffered I/O.
4264 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4265 struct iov_iter *iter)
4269 if (!(iocb->ki_flags & IOCB_DIRECT))
4272 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4276 * Direct I/O not aligned to the disk's logical_block_size will be
4277 * attempted, but will fail with -EINVAL.
4279 * f2fs additionally requires that direct I/O be aligned to the
4280 * filesystem block size, which is often a stricter requirement.
4281 * However, f2fs traditionally falls back to buffered I/O on requests
4282 * that are logical_block_size-aligned but not fs-block aligned.
4284 * The below logic implements this behavior.
4286 align = iocb->ki_pos | iov_iter_alignment(iter);
4287 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4288 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4294 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4297 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4299 dec_page_count(sbi, F2FS_DIO_READ);
4302 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4306 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4307 .end_io = f2fs_dio_read_end_io,
4310 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4312 struct file *file = iocb->ki_filp;
4313 struct inode *inode = file_inode(file);
4314 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4315 struct f2fs_inode_info *fi = F2FS_I(inode);
4316 const loff_t pos = iocb->ki_pos;
4317 const size_t count = iov_iter_count(to);
4318 struct iomap_dio *dio;
4322 return 0; /* skip atime update */
4324 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4326 if (iocb->ki_flags & IOCB_NOWAIT) {
4327 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4332 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4336 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4337 * the higher-level function iomap_dio_rw() in order to ensure that the
4338 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4340 inc_page_count(sbi, F2FS_DIO_READ);
4341 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4342 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4343 if (IS_ERR_OR_NULL(dio)) {
4344 ret = PTR_ERR_OR_ZERO(dio);
4345 if (ret != -EIOCBQUEUED)
4346 dec_page_count(sbi, F2FS_DIO_READ);
4348 ret = iomap_dio_complete(dio);
4351 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4353 file_accessed(file);
4355 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4359 static void f2fs_trace_rw_file_path(struct kiocb *iocb, size_t count, int rw)
4361 struct inode *inode = file_inode(iocb->ki_filp);
4364 buf = f2fs_kmalloc(F2FS_I_SB(inode), PATH_MAX, GFP_KERNEL);
4367 path = dentry_path_raw(file_dentry(iocb->ki_filp), buf, PATH_MAX);
4371 trace_f2fs_datawrite_start(inode, iocb->ki_pos, count,
4372 current->pid, path, current->comm);
4374 trace_f2fs_dataread_start(inode, iocb->ki_pos, count,
4375 current->pid, path, current->comm);
4380 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4382 struct inode *inode = file_inode(iocb->ki_filp);
4383 const loff_t pos = iocb->ki_pos;
4386 if (!f2fs_is_compress_backend_ready(inode))
4389 if (trace_f2fs_dataread_start_enabled())
4390 f2fs_trace_rw_file_path(iocb, iov_iter_count(to), READ);
4392 if (f2fs_should_use_dio(inode, iocb, to)) {
4393 ret = f2fs_dio_read_iter(iocb, to);
4395 ret = filemap_read(iocb, to, 0);
4397 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4398 APP_BUFFERED_READ_IO, ret);
4400 if (trace_f2fs_dataread_end_enabled())
4401 trace_f2fs_dataread_end(inode, pos, ret);
4405 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4407 struct file *file = iocb->ki_filp;
4408 struct inode *inode = file_inode(file);
4412 if (IS_IMMUTABLE(inode))
4415 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4418 count = generic_write_checks(iocb, from);
4422 err = file_modified(file);
4429 * Preallocate blocks for a write request, if it is possible and helpful to do
4430 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4431 * blocks were preallocated, or a negative errno value if something went
4432 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4433 * requested blocks (not just some of them) have been allocated.
4435 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4438 struct inode *inode = file_inode(iocb->ki_filp);
4439 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4440 const loff_t pos = iocb->ki_pos;
4441 const size_t count = iov_iter_count(iter);
4442 struct f2fs_map_blocks map = {};
4446 /* If it will be an out-of-place direct write, don't bother. */
4447 if (dio && f2fs_lfs_mode(sbi))
4450 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4451 * buffered IO, if DIO meets any holes.
4453 if (dio && i_size_read(inode) &&
4454 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4457 /* No-wait I/O can't allocate blocks. */
4458 if (iocb->ki_flags & IOCB_NOWAIT)
4461 /* If it will be a short write, don't bother. */
4462 if (fault_in_iov_iter_readable(iter, count))
4465 if (f2fs_has_inline_data(inode)) {
4466 /* If the data will fit inline, don't bother. */
4467 if (pos + count <= MAX_INLINE_DATA(inode))
4469 ret = f2fs_convert_inline_inode(inode);
4474 /* Do not preallocate blocks that will be written partially in 4KB. */
4475 map.m_lblk = F2FS_BLK_ALIGN(pos);
4476 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4477 if (map.m_len > map.m_lblk)
4478 map.m_len -= map.m_lblk;
4481 map.m_may_create = true;
4483 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4484 flag = F2FS_GET_BLOCK_PRE_DIO;
4486 map.m_seg_type = NO_CHECK_TYPE;
4487 flag = F2FS_GET_BLOCK_PRE_AIO;
4490 ret = f2fs_map_blocks(inode, &map, flag);
4491 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4492 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4495 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4499 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4500 struct iov_iter *from)
4502 struct file *file = iocb->ki_filp;
4503 struct inode *inode = file_inode(file);
4506 if (iocb->ki_flags & IOCB_NOWAIT)
4509 current->backing_dev_info = inode_to_bdi(inode);
4510 ret = generic_perform_write(iocb, from);
4511 current->backing_dev_info = NULL;
4514 iocb->ki_pos += ret;
4515 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4516 APP_BUFFERED_IO, ret);
4521 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4524 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4526 dec_page_count(sbi, F2FS_DIO_WRITE);
4529 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4533 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4534 .end_io = f2fs_dio_write_end_io,
4537 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4538 bool *may_need_sync)
4540 struct file *file = iocb->ki_filp;
4541 struct inode *inode = file_inode(file);
4542 struct f2fs_inode_info *fi = F2FS_I(inode);
4543 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4544 const bool do_opu = f2fs_lfs_mode(sbi);
4545 const loff_t pos = iocb->ki_pos;
4546 const ssize_t count = iov_iter_count(from);
4547 unsigned int dio_flags;
4548 struct iomap_dio *dio;
4551 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4553 if (iocb->ki_flags & IOCB_NOWAIT) {
4554 /* f2fs_convert_inline_inode() and block allocation can block */
4555 if (f2fs_has_inline_data(inode) ||
4556 !f2fs_overwrite_io(inode, pos, count)) {
4561 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4565 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4566 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4571 ret = f2fs_convert_inline_inode(inode);
4575 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4577 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4581 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4582 * the higher-level function iomap_dio_rw() in order to ensure that the
4583 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4585 inc_page_count(sbi, F2FS_DIO_WRITE);
4587 if (pos + count > inode->i_size)
4588 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4589 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4590 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4591 if (IS_ERR_OR_NULL(dio)) {
4592 ret = PTR_ERR_OR_ZERO(dio);
4593 if (ret == -ENOTBLK)
4595 if (ret != -EIOCBQUEUED)
4596 dec_page_count(sbi, F2FS_DIO_WRITE);
4598 ret = iomap_dio_complete(dio);
4602 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4603 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4607 if (pos + ret > inode->i_size)
4608 f2fs_i_size_write(inode, pos + ret);
4610 set_inode_flag(inode, FI_UPDATE_WRITE);
4612 if (iov_iter_count(from)) {
4614 loff_t bufio_start_pos = iocb->ki_pos;
4617 * The direct write was partial, so we need to fall back to a
4618 * buffered write for the remainder.
4621 ret2 = f2fs_buffered_write_iter(iocb, from);
4622 if (iov_iter_count(from))
4623 f2fs_write_failed(inode, iocb->ki_pos);
4628 * Ensure that the pagecache pages are written to disk and
4629 * invalidated to preserve the expected O_DIRECT semantics.
4632 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4636 ret2 = filemap_write_and_wait_range(file->f_mapping,
4641 invalidate_mapping_pages(file->f_mapping,
4642 bufio_start_pos >> PAGE_SHIFT,
4643 bufio_end_pos >> PAGE_SHIFT);
4646 /* iomap_dio_rw() already handled the generic_write_sync(). */
4647 *may_need_sync = false;
4650 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4654 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4656 struct inode *inode = file_inode(iocb->ki_filp);
4657 const loff_t orig_pos = iocb->ki_pos;
4658 const size_t orig_count = iov_iter_count(from);
4661 bool may_need_sync = true;
4665 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4670 if (!f2fs_is_compress_backend_ready(inode)) {
4675 if (iocb->ki_flags & IOCB_NOWAIT) {
4676 if (!inode_trylock(inode)) {
4684 ret = f2fs_write_checks(iocb, from);
4688 /* Determine whether we will do a direct write or a buffered write. */
4689 dio = f2fs_should_use_dio(inode, iocb, from);
4691 /* Possibly preallocate the blocks for the write. */
4692 target_size = iocb->ki_pos + iov_iter_count(from);
4693 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4694 if (preallocated < 0) {
4697 if (trace_f2fs_datawrite_start_enabled())
4698 f2fs_trace_rw_file_path(iocb, orig_count, WRITE);
4700 /* Do the actual write. */
4702 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
4703 f2fs_buffered_write_iter(iocb, from);
4705 if (trace_f2fs_datawrite_end_enabled())
4706 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4709 /* Don't leave any preallocated blocks around past i_size. */
4710 if (preallocated && i_size_read(inode) < target_size) {
4711 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4712 filemap_invalidate_lock(inode->i_mapping);
4713 if (!f2fs_truncate(inode))
4714 file_dont_truncate(inode);
4715 filemap_invalidate_unlock(inode->i_mapping);
4716 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4718 file_dont_truncate(inode);
4721 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4723 inode_unlock(inode);
4725 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4726 if (ret > 0 && may_need_sync)
4727 ret = generic_write_sync(iocb, ret);
4731 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4734 struct address_space *mapping;
4735 struct backing_dev_info *bdi;
4736 struct inode *inode = file_inode(filp);
4739 if (advice == POSIX_FADV_SEQUENTIAL) {
4740 if (S_ISFIFO(inode->i_mode))
4743 mapping = filp->f_mapping;
4744 if (!mapping || len < 0)
4747 bdi = inode_to_bdi(mapping->host);
4748 filp->f_ra.ra_pages = bdi->ra_pages *
4749 F2FS_I_SB(inode)->seq_file_ra_mul;
4750 spin_lock(&filp->f_lock);
4751 filp->f_mode &= ~FMODE_RANDOM;
4752 spin_unlock(&filp->f_lock);
4756 err = generic_fadvise(filp, offset, len, advice);
4757 if (!err && advice == POSIX_FADV_DONTNEED &&
4758 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4759 f2fs_compressed_file(inode))
4760 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4765 #ifdef CONFIG_COMPAT
4766 struct compat_f2fs_gc_range {
4771 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4772 struct compat_f2fs_gc_range)
4774 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4776 struct compat_f2fs_gc_range __user *urange;
4777 struct f2fs_gc_range range;
4780 urange = compat_ptr(arg);
4781 err = get_user(range.sync, &urange->sync);
4782 err |= get_user(range.start, &urange->start);
4783 err |= get_user(range.len, &urange->len);
4787 return __f2fs_ioc_gc_range(file, &range);
4790 struct compat_f2fs_move_range {
4796 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4797 struct compat_f2fs_move_range)
4799 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4801 struct compat_f2fs_move_range __user *urange;
4802 struct f2fs_move_range range;
4805 urange = compat_ptr(arg);
4806 err = get_user(range.dst_fd, &urange->dst_fd);
4807 err |= get_user(range.pos_in, &urange->pos_in);
4808 err |= get_user(range.pos_out, &urange->pos_out);
4809 err |= get_user(range.len, &urange->len);
4813 return __f2fs_ioc_move_range(file, &range);
4816 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4818 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4820 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4824 case FS_IOC32_GETVERSION:
4825 cmd = FS_IOC_GETVERSION;
4827 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4828 return f2fs_compat_ioc_gc_range(file, arg);
4829 case F2FS_IOC32_MOVE_RANGE:
4830 return f2fs_compat_ioc_move_range(file, arg);
4831 case F2FS_IOC_START_ATOMIC_WRITE:
4832 case F2FS_IOC_START_ATOMIC_REPLACE:
4833 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4834 case F2FS_IOC_START_VOLATILE_WRITE:
4835 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4836 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4837 case F2FS_IOC_SHUTDOWN:
4839 case FS_IOC_SET_ENCRYPTION_POLICY:
4840 case FS_IOC_GET_ENCRYPTION_PWSALT:
4841 case FS_IOC_GET_ENCRYPTION_POLICY:
4842 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4843 case FS_IOC_ADD_ENCRYPTION_KEY:
4844 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4845 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4846 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4847 case FS_IOC_GET_ENCRYPTION_NONCE:
4848 case F2FS_IOC_GARBAGE_COLLECT:
4849 case F2FS_IOC_WRITE_CHECKPOINT:
4850 case F2FS_IOC_DEFRAGMENT:
4851 case F2FS_IOC_FLUSH_DEVICE:
4852 case F2FS_IOC_GET_FEATURES:
4853 case F2FS_IOC_GET_PIN_FILE:
4854 case F2FS_IOC_SET_PIN_FILE:
4855 case F2FS_IOC_PRECACHE_EXTENTS:
4856 case F2FS_IOC_RESIZE_FS:
4857 case FS_IOC_ENABLE_VERITY:
4858 case FS_IOC_MEASURE_VERITY:
4859 case FS_IOC_READ_VERITY_METADATA:
4860 case FS_IOC_GETFSLABEL:
4861 case FS_IOC_SETFSLABEL:
4862 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4863 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4864 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4865 case F2FS_IOC_SEC_TRIM_FILE:
4866 case F2FS_IOC_GET_COMPRESS_OPTION:
4867 case F2FS_IOC_SET_COMPRESS_OPTION:
4868 case F2FS_IOC_DECOMPRESS_FILE:
4869 case F2FS_IOC_COMPRESS_FILE:
4872 return -ENOIOCTLCMD;
4874 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4878 const struct file_operations f2fs_file_operations = {
4879 .llseek = f2fs_llseek,
4880 .read_iter = f2fs_file_read_iter,
4881 .write_iter = f2fs_file_write_iter,
4882 .open = f2fs_file_open,
4883 .release = f2fs_release_file,
4884 .mmap = f2fs_file_mmap,
4885 .flush = f2fs_file_flush,
4886 .fsync = f2fs_sync_file,
4887 .fallocate = f2fs_fallocate,
4888 .unlocked_ioctl = f2fs_ioctl,
4889 #ifdef CONFIG_COMPAT
4890 .compat_ioctl = f2fs_compat_ioctl,
4892 .splice_read = generic_file_splice_read,
4893 .splice_write = iter_file_splice_write,
4894 .fadvise = f2fs_file_fadvise,