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>
33 #include <trace/events/f2fs.h>
34 #include <uapi/linux/f2fs.h>
36 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
38 struct inode *inode = file_inode(vmf->vma->vm_file);
41 down_read(&F2FS_I(inode)->i_mmap_sem);
42 ret = filemap_fault(vmf);
43 up_read(&F2FS_I(inode)->i_mmap_sem);
46 f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
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(f2fs_cp_error(sbi))) {
68 if (!f2fs_is_checkpoint_ready(sbi)) {
73 #ifdef CONFIG_F2FS_FS_COMPRESSION
74 if (f2fs_compressed_file(inode)) {
75 int ret = f2fs_is_compressed_cluster(inode, page->index);
81 if (ret < F2FS_I(inode)->i_cluster_size) {
89 /* should do out of any locked page */
91 f2fs_balance_fs(sbi, true);
93 sb_start_pagefault(inode->i_sb);
95 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
97 file_update_time(vmf->vma->vm_file);
98 down_read(&F2FS_I(inode)->i_mmap_sem);
100 if (unlikely(page->mapping != inode->i_mapping ||
101 page_offset(page) > i_size_read(inode) ||
102 !PageUptodate(page))) {
109 /* block allocation */
110 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
111 set_new_dnode(&dn, inode, NULL, NULL, 0);
112 err = f2fs_get_block(&dn, page->index);
114 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
117 #ifdef CONFIG_F2FS_FS_COMPRESSION
119 set_new_dnode(&dn, inode, NULL, NULL, 0);
120 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
129 f2fs_wait_on_page_writeback(page, DATA, false, true);
131 /* wait for GCed page writeback via META_MAPPING */
132 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
135 * check to see if the page is mapped already (no holes)
137 if (PageMappedToDisk(page))
140 /* page is wholly or partially inside EOF */
141 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
142 i_size_read(inode)) {
145 offset = i_size_read(inode) & ~PAGE_MASK;
146 zero_user_segment(page, offset, PAGE_SIZE);
148 set_page_dirty(page);
149 if (!PageUptodate(page))
150 SetPageUptodate(page);
152 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
153 f2fs_update_time(sbi, REQ_TIME);
155 trace_f2fs_vm_page_mkwrite(page, DATA);
157 up_read(&F2FS_I(inode)->i_mmap_sem);
159 sb_end_pagefault(inode->i_sb);
161 return block_page_mkwrite_return(err);
164 static const struct vm_operations_struct f2fs_file_vm_ops = {
165 .fault = f2fs_filemap_fault,
166 .map_pages = filemap_map_pages,
167 .page_mkwrite = f2fs_vm_page_mkwrite,
170 static int get_parent_ino(struct inode *inode, nid_t *pino)
172 struct dentry *dentry;
175 * Make sure to get the non-deleted alias. The alias associated with
176 * the open file descriptor being fsync()'ed may be deleted already.
178 dentry = d_find_alias(inode);
182 *pino = parent_ino(dentry);
187 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
189 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
190 enum cp_reason_type cp_reason = CP_NO_NEEDED;
192 if (!S_ISREG(inode->i_mode))
193 cp_reason = CP_NON_REGULAR;
194 else if (f2fs_compressed_file(inode))
195 cp_reason = CP_COMPRESSED;
196 else if (inode->i_nlink != 1)
197 cp_reason = CP_HARDLINK;
198 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
199 cp_reason = CP_SB_NEED_CP;
200 else if (file_wrong_pino(inode))
201 cp_reason = CP_WRONG_PINO;
202 else if (!f2fs_space_for_roll_forward(sbi))
203 cp_reason = CP_NO_SPC_ROLL;
204 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
205 cp_reason = CP_NODE_NEED_CP;
206 else if (test_opt(sbi, FASTBOOT))
207 cp_reason = CP_FASTBOOT_MODE;
208 else if (F2FS_OPTION(sbi).active_logs == 2)
209 cp_reason = CP_SPEC_LOG_NUM;
210 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
211 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
212 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
214 cp_reason = CP_RECOVER_DIR;
219 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
221 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
223 /* But we need to avoid that there are some inode updates */
224 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
230 static void try_to_fix_pino(struct inode *inode)
232 struct f2fs_inode_info *fi = F2FS_I(inode);
235 down_write(&fi->i_sem);
236 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
237 get_parent_ino(inode, &pino)) {
238 f2fs_i_pino_write(inode, pino);
239 file_got_pino(inode);
241 up_write(&fi->i_sem);
244 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
245 int datasync, bool atomic)
247 struct inode *inode = file->f_mapping->host;
248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
249 nid_t ino = inode->i_ino;
251 enum cp_reason_type cp_reason = 0;
252 struct writeback_control wbc = {
253 .sync_mode = WB_SYNC_ALL,
254 .nr_to_write = LONG_MAX,
257 unsigned int seq_id = 0;
259 if (unlikely(f2fs_readonly(inode->i_sb) ||
260 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
263 trace_f2fs_sync_file_enter(inode);
265 if (S_ISDIR(inode->i_mode))
268 /* if fdatasync is triggered, let's do in-place-update */
269 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
270 set_inode_flag(inode, FI_NEED_IPU);
271 ret = file_write_and_wait_range(file, start, end);
272 clear_inode_flag(inode, FI_NEED_IPU);
275 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
279 /* if the inode is dirty, let's recover all the time */
280 if (!f2fs_skip_inode_update(inode, datasync)) {
281 f2fs_write_inode(inode, NULL);
286 * if there is no written data, don't waste time to write recovery info.
288 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
289 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
291 /* it may call write_inode just prior to fsync */
292 if (need_inode_page_update(sbi, ino))
295 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
296 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
302 * Both of fdatasync() and fsync() are able to be recovered from
305 down_read(&F2FS_I(inode)->i_sem);
306 cp_reason = need_do_checkpoint(inode);
307 up_read(&F2FS_I(inode)->i_sem);
310 /* all the dirty node pages should be flushed for POR */
311 ret = f2fs_sync_fs(inode->i_sb, 1);
314 * We've secured consistency through sync_fs. Following pino
315 * will be used only for fsynced inodes after checkpoint.
317 try_to_fix_pino(inode);
318 clear_inode_flag(inode, FI_APPEND_WRITE);
319 clear_inode_flag(inode, FI_UPDATE_WRITE);
323 atomic_inc(&sbi->wb_sync_req[NODE]);
324 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
325 atomic_dec(&sbi->wb_sync_req[NODE]);
329 /* if cp_error was enabled, we should avoid infinite loop */
330 if (unlikely(f2fs_cp_error(sbi))) {
335 if (f2fs_need_inode_block_update(sbi, ino)) {
336 f2fs_mark_inode_dirty_sync(inode, true);
337 f2fs_write_inode(inode, NULL);
342 * If it's atomic_write, it's just fine to keep write ordering. So
343 * here we don't need to wait for node write completion, since we use
344 * node chain which serializes node blocks. If one of node writes are
345 * reordered, we can see simply broken chain, resulting in stopping
346 * roll-forward recovery. It means we'll recover all or none node blocks
350 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
355 /* once recovery info is written, don't need to tack this */
356 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
357 clear_inode_flag(inode, FI_APPEND_WRITE);
359 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
360 ret = f2fs_issue_flush(sbi, inode->i_ino);
362 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
363 clear_inode_flag(inode, FI_UPDATE_WRITE);
364 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
366 f2fs_update_time(sbi, REQ_TIME);
368 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
369 f2fs_trace_ios(NULL, 1);
373 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
375 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
377 return f2fs_do_sync_file(file, start, end, datasync, false);
380 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
381 pgoff_t index, int whence)
385 if (__is_valid_data_blkaddr(blkaddr))
387 if (blkaddr == NEW_ADDR &&
388 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
392 if (blkaddr == NULL_ADDR)
399 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
401 struct inode *inode = file->f_mapping->host;
402 loff_t maxbytes = inode->i_sb->s_maxbytes;
403 struct dnode_of_data dn;
404 pgoff_t pgofs, end_offset;
405 loff_t data_ofs = offset;
411 isize = i_size_read(inode);
415 /* handle inline data case */
416 if (f2fs_has_inline_data(inode)) {
417 if (whence == SEEK_HOLE) {
420 } else if (whence == SEEK_DATA) {
426 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
428 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
429 set_new_dnode(&dn, inode, NULL, NULL, 0);
430 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
431 if (err && err != -ENOENT) {
433 } else if (err == -ENOENT) {
434 /* direct node does not exists */
435 if (whence == SEEK_DATA) {
436 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
443 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
445 /* find data/hole in dnode block */
446 for (; dn.ofs_in_node < end_offset;
447 dn.ofs_in_node++, pgofs++,
448 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
451 blkaddr = f2fs_data_blkaddr(&dn);
453 if (__is_valid_data_blkaddr(blkaddr) &&
454 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
455 blkaddr, DATA_GENERIC_ENHANCE)) {
460 if (__found_offset(file->f_mapping, blkaddr,
469 if (whence == SEEK_DATA)
472 if (whence == SEEK_HOLE && data_ofs > isize)
475 return vfs_setpos(file, data_ofs, maxbytes);
481 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
483 struct inode *inode = file->f_mapping->host;
484 loff_t maxbytes = inode->i_sb->s_maxbytes;
490 return generic_file_llseek_size(file, offset, whence,
491 maxbytes, i_size_read(inode));
496 return f2fs_seek_block(file, offset, whence);
502 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
504 struct inode *inode = file_inode(file);
507 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
510 if (!f2fs_is_compress_backend_ready(inode))
513 /* we don't need to use inline_data strictly */
514 err = f2fs_convert_inline_inode(inode);
519 vma->vm_ops = &f2fs_file_vm_ops;
520 set_inode_flag(inode, FI_MMAP_FILE);
524 static int f2fs_file_open(struct inode *inode, struct file *filp)
526 int err = fscrypt_file_open(inode, filp);
531 if (!f2fs_is_compress_backend_ready(inode))
534 err = fsverity_file_open(inode, filp);
538 filp->f_mode |= FMODE_NOWAIT;
540 return dquot_file_open(inode, filp);
543 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
545 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
546 struct f2fs_node *raw_node;
547 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
550 bool compressed_cluster = false;
551 int cluster_index = 0, valid_blocks = 0;
552 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
553 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
555 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
556 base = get_extra_isize(dn->inode);
558 raw_node = F2FS_NODE(dn->node_page);
559 addr = blkaddr_in_node(raw_node) + base + ofs;
561 /* Assumption: truncateion starts with cluster */
562 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
563 block_t blkaddr = le32_to_cpu(*addr);
565 if (f2fs_compressed_file(dn->inode) &&
566 !(cluster_index & (cluster_size - 1))) {
567 if (compressed_cluster)
568 f2fs_i_compr_blocks_update(dn->inode,
569 valid_blocks, false);
570 compressed_cluster = (blkaddr == COMPRESS_ADDR);
574 if (blkaddr == NULL_ADDR)
577 dn->data_blkaddr = NULL_ADDR;
578 f2fs_set_data_blkaddr(dn);
580 if (__is_valid_data_blkaddr(blkaddr)) {
581 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
582 DATA_GENERIC_ENHANCE))
584 if (compressed_cluster)
588 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
589 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
591 f2fs_invalidate_blocks(sbi, blkaddr);
593 if (!released || blkaddr != COMPRESS_ADDR)
597 if (compressed_cluster)
598 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
603 * once we invalidate valid blkaddr in range [ofs, ofs + count],
604 * we will invalidate all blkaddr in the whole range.
606 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
608 f2fs_update_extent_cache_range(dn, fofs, 0, len);
609 dec_valid_block_count(sbi, dn->inode, nr_free);
611 dn->ofs_in_node = ofs;
613 f2fs_update_time(sbi, REQ_TIME);
614 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
615 dn->ofs_in_node, nr_free);
618 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
620 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
623 static int truncate_partial_data_page(struct inode *inode, u64 from,
626 loff_t offset = from & (PAGE_SIZE - 1);
627 pgoff_t index = from >> PAGE_SHIFT;
628 struct address_space *mapping = inode->i_mapping;
631 if (!offset && !cache_only)
635 page = find_lock_page(mapping, index);
636 if (page && PageUptodate(page))
638 f2fs_put_page(page, 1);
642 page = f2fs_get_lock_data_page(inode, index, true);
644 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
646 f2fs_wait_on_page_writeback(page, DATA, true, true);
647 zero_user(page, offset, PAGE_SIZE - offset);
649 /* An encrypted inode should have a key and truncate the last page. */
650 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
652 set_page_dirty(page);
653 f2fs_put_page(page, 1);
657 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
659 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
660 struct dnode_of_data dn;
662 int count = 0, err = 0;
664 bool truncate_page = false;
666 trace_f2fs_truncate_blocks_enter(inode, from);
668 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
670 if (free_from >= sbi->max_file_blocks)
676 ipage = f2fs_get_node_page(sbi, inode->i_ino);
678 err = PTR_ERR(ipage);
682 if (f2fs_has_inline_data(inode)) {
683 f2fs_truncate_inline_inode(inode, ipage, from);
684 f2fs_put_page(ipage, 1);
685 truncate_page = true;
689 set_new_dnode(&dn, inode, ipage, NULL, 0);
690 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
697 count = ADDRS_PER_PAGE(dn.node_page, inode);
699 count -= dn.ofs_in_node;
700 f2fs_bug_on(sbi, count < 0);
702 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
703 f2fs_truncate_data_blocks_range(&dn, count);
709 err = f2fs_truncate_inode_blocks(inode, free_from);
714 /* lastly zero out the first data page */
716 err = truncate_partial_data_page(inode, from, truncate_page);
718 trace_f2fs_truncate_blocks_exit(inode, err);
722 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
724 u64 free_from = from;
727 #ifdef CONFIG_F2FS_FS_COMPRESSION
729 * for compressed file, only support cluster size
730 * aligned truncation.
732 if (f2fs_compressed_file(inode))
733 free_from = round_up(from,
734 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
737 err = f2fs_do_truncate_blocks(inode, free_from, lock);
741 #ifdef CONFIG_F2FS_FS_COMPRESSION
742 if (from != free_from) {
743 err = f2fs_truncate_partial_cluster(inode, from, lock);
752 int f2fs_truncate(struct inode *inode)
756 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
759 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
760 S_ISLNK(inode->i_mode)))
763 trace_f2fs_truncate(inode);
765 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
766 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
770 /* we should check inline_data size */
771 if (!f2fs_may_inline_data(inode)) {
772 err = f2fs_convert_inline_inode(inode);
777 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
781 inode->i_mtime = inode->i_ctime = current_time(inode);
782 f2fs_mark_inode_dirty_sync(inode, false);
786 int f2fs_getattr(const struct path *path, struct kstat *stat,
787 u32 request_mask, unsigned int query_flags)
789 struct inode *inode = d_inode(path->dentry);
790 struct f2fs_inode_info *fi = F2FS_I(inode);
791 struct f2fs_inode *ri;
794 if (f2fs_has_extra_attr(inode) &&
795 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
796 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
797 stat->result_mask |= STATX_BTIME;
798 stat->btime.tv_sec = fi->i_crtime.tv_sec;
799 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
803 if (flags & F2FS_COMPR_FL)
804 stat->attributes |= STATX_ATTR_COMPRESSED;
805 if (flags & F2FS_APPEND_FL)
806 stat->attributes |= STATX_ATTR_APPEND;
807 if (IS_ENCRYPTED(inode))
808 stat->attributes |= STATX_ATTR_ENCRYPTED;
809 if (flags & F2FS_IMMUTABLE_FL)
810 stat->attributes |= STATX_ATTR_IMMUTABLE;
811 if (flags & F2FS_NODUMP_FL)
812 stat->attributes |= STATX_ATTR_NODUMP;
813 if (IS_VERITY(inode))
814 stat->attributes |= STATX_ATTR_VERITY;
816 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
818 STATX_ATTR_ENCRYPTED |
819 STATX_ATTR_IMMUTABLE |
823 generic_fillattr(inode, stat);
825 /* we need to show initial sectors used for inline_data/dentries */
826 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
827 f2fs_has_inline_dentry(inode))
828 stat->blocks += (stat->size + 511) >> 9;
833 #ifdef CONFIG_F2FS_FS_POSIX_ACL
834 static void __setattr_copy(struct user_namespace *mnt_userns,
835 struct inode *inode, const struct iattr *attr)
837 unsigned int ia_valid = attr->ia_valid;
839 if (ia_valid & ATTR_UID)
840 inode->i_uid = attr->ia_uid;
841 if (ia_valid & ATTR_GID)
842 inode->i_gid = attr->ia_gid;
843 if (ia_valid & ATTR_ATIME)
844 inode->i_atime = attr->ia_atime;
845 if (ia_valid & ATTR_MTIME)
846 inode->i_mtime = attr->ia_mtime;
847 if (ia_valid & ATTR_CTIME)
848 inode->i_ctime = attr->ia_ctime;
849 if (ia_valid & ATTR_MODE) {
850 umode_t mode = attr->ia_mode;
851 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
853 if (!in_group_p(kgid) && !capable(CAP_FSETID))
855 set_acl_inode(inode, mode);
859 #define __setattr_copy setattr_copy
862 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
864 struct inode *inode = d_inode(dentry);
867 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
870 if ((attr->ia_valid & ATTR_SIZE) &&
871 !f2fs_is_compress_backend_ready(inode))
874 err = setattr_prepare(&init_user_ns, dentry, attr);
878 err = fscrypt_prepare_setattr(dentry, attr);
882 err = fsverity_prepare_setattr(dentry, attr);
886 if (is_quota_modification(inode, attr)) {
887 err = dquot_initialize(inode);
891 if ((attr->ia_valid & ATTR_UID &&
892 !uid_eq(attr->ia_uid, inode->i_uid)) ||
893 (attr->ia_valid & ATTR_GID &&
894 !gid_eq(attr->ia_gid, inode->i_gid))) {
895 f2fs_lock_op(F2FS_I_SB(inode));
896 err = dquot_transfer(inode, attr);
898 set_sbi_flag(F2FS_I_SB(inode),
899 SBI_QUOTA_NEED_REPAIR);
900 f2fs_unlock_op(F2FS_I_SB(inode));
904 * update uid/gid under lock_op(), so that dquot and inode can
905 * be updated atomically.
907 if (attr->ia_valid & ATTR_UID)
908 inode->i_uid = attr->ia_uid;
909 if (attr->ia_valid & ATTR_GID)
910 inode->i_gid = attr->ia_gid;
911 f2fs_mark_inode_dirty_sync(inode, true);
912 f2fs_unlock_op(F2FS_I_SB(inode));
915 if (attr->ia_valid & ATTR_SIZE) {
916 loff_t old_size = i_size_read(inode);
918 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
920 * should convert inline inode before i_size_write to
921 * keep smaller than inline_data size with inline flag.
923 err = f2fs_convert_inline_inode(inode);
928 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
929 down_write(&F2FS_I(inode)->i_mmap_sem);
931 truncate_setsize(inode, attr->ia_size);
933 if (attr->ia_size <= old_size)
934 err = f2fs_truncate(inode);
936 * do not trim all blocks after i_size if target size is
937 * larger than i_size.
939 up_write(&F2FS_I(inode)->i_mmap_sem);
940 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
944 spin_lock(&F2FS_I(inode)->i_size_lock);
945 inode->i_mtime = inode->i_ctime = current_time(inode);
946 F2FS_I(inode)->last_disk_size = i_size_read(inode);
947 spin_unlock(&F2FS_I(inode)->i_size_lock);
950 __setattr_copy(&init_user_ns, inode, attr);
952 if (attr->ia_valid & ATTR_MODE) {
953 err = posix_acl_chmod(&init_user_ns, inode,
954 f2fs_get_inode_mode(inode));
955 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
956 inode->i_mode = F2FS_I(inode)->i_acl_mode;
957 clear_inode_flag(inode, FI_ACL_MODE);
961 /* file size may changed here */
962 f2fs_mark_inode_dirty_sync(inode, true);
964 /* inode change will produce dirty node pages flushed by checkpoint */
965 f2fs_balance_fs(F2FS_I_SB(inode), true);
970 const struct inode_operations f2fs_file_inode_operations = {
971 .getattr = f2fs_getattr,
972 .setattr = f2fs_setattr,
973 .get_acl = f2fs_get_acl,
974 .set_acl = f2fs_set_acl,
975 .listxattr = f2fs_listxattr,
976 .fiemap = f2fs_fiemap,
979 static int fill_zero(struct inode *inode, pgoff_t index,
980 loff_t start, loff_t len)
982 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
988 f2fs_balance_fs(sbi, true);
991 page = f2fs_get_new_data_page(inode, NULL, index, false);
995 return PTR_ERR(page);
997 f2fs_wait_on_page_writeback(page, DATA, true, true);
998 zero_user(page, start, len);
999 set_page_dirty(page);
1000 f2fs_put_page(page, 1);
1004 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1008 while (pg_start < pg_end) {
1009 struct dnode_of_data dn;
1010 pgoff_t end_offset, count;
1012 set_new_dnode(&dn, inode, NULL, NULL, 0);
1013 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1015 if (err == -ENOENT) {
1016 pg_start = f2fs_get_next_page_offset(&dn,
1023 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1024 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1026 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1028 f2fs_truncate_data_blocks_range(&dn, count);
1029 f2fs_put_dnode(&dn);
1036 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1038 pgoff_t pg_start, pg_end;
1039 loff_t off_start, off_end;
1042 ret = f2fs_convert_inline_inode(inode);
1046 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1047 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1049 off_start = offset & (PAGE_SIZE - 1);
1050 off_end = (offset + len) & (PAGE_SIZE - 1);
1052 if (pg_start == pg_end) {
1053 ret = fill_zero(inode, pg_start, off_start,
1054 off_end - off_start);
1059 ret = fill_zero(inode, pg_start++, off_start,
1060 PAGE_SIZE - off_start);
1065 ret = fill_zero(inode, pg_end, 0, off_end);
1070 if (pg_start < pg_end) {
1071 struct address_space *mapping = inode->i_mapping;
1072 loff_t blk_start, blk_end;
1073 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1075 f2fs_balance_fs(sbi, true);
1077 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1078 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1080 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1081 down_write(&F2FS_I(inode)->i_mmap_sem);
1083 truncate_inode_pages_range(mapping, blk_start,
1087 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1088 f2fs_unlock_op(sbi);
1090 up_write(&F2FS_I(inode)->i_mmap_sem);
1091 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1098 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1099 int *do_replace, pgoff_t off, pgoff_t len)
1101 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1102 struct dnode_of_data dn;
1106 set_new_dnode(&dn, inode, NULL, NULL, 0);
1107 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1108 if (ret && ret != -ENOENT) {
1110 } else if (ret == -ENOENT) {
1111 if (dn.max_level == 0)
1113 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1114 dn.ofs_in_node, len);
1120 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1121 dn.ofs_in_node, len);
1122 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1123 *blkaddr = f2fs_data_blkaddr(&dn);
1125 if (__is_valid_data_blkaddr(*blkaddr) &&
1126 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1127 DATA_GENERIC_ENHANCE)) {
1128 f2fs_put_dnode(&dn);
1129 return -EFSCORRUPTED;
1132 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1134 if (f2fs_lfs_mode(sbi)) {
1135 f2fs_put_dnode(&dn);
1139 /* do not invalidate this block address */
1140 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1144 f2fs_put_dnode(&dn);
1153 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1154 int *do_replace, pgoff_t off, int len)
1156 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1157 struct dnode_of_data dn;
1160 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1161 if (*do_replace == 0)
1164 set_new_dnode(&dn, inode, NULL, NULL, 0);
1165 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1167 dec_valid_block_count(sbi, inode, 1);
1168 f2fs_invalidate_blocks(sbi, *blkaddr);
1170 f2fs_update_data_blkaddr(&dn, *blkaddr);
1172 f2fs_put_dnode(&dn);
1177 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1178 block_t *blkaddr, int *do_replace,
1179 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1181 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1186 if (blkaddr[i] == NULL_ADDR && !full) {
1191 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1192 struct dnode_of_data dn;
1193 struct node_info ni;
1197 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1198 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1202 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1204 f2fs_put_dnode(&dn);
1208 ilen = min((pgoff_t)
1209 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1210 dn.ofs_in_node, len - i);
1212 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1213 f2fs_truncate_data_blocks_range(&dn, 1);
1215 if (do_replace[i]) {
1216 f2fs_i_blocks_write(src_inode,
1218 f2fs_i_blocks_write(dst_inode,
1220 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1221 blkaddr[i], ni.version, true, false);
1227 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1228 if (dst_inode->i_size < new_size)
1229 f2fs_i_size_write(dst_inode, new_size);
1230 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1232 f2fs_put_dnode(&dn);
1234 struct page *psrc, *pdst;
1236 psrc = f2fs_get_lock_data_page(src_inode,
1239 return PTR_ERR(psrc);
1240 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1243 f2fs_put_page(psrc, 1);
1244 return PTR_ERR(pdst);
1246 f2fs_copy_page(psrc, pdst);
1247 set_page_dirty(pdst);
1248 f2fs_put_page(pdst, 1);
1249 f2fs_put_page(psrc, 1);
1251 ret = f2fs_truncate_hole(src_inode,
1252 src + i, src + i + 1);
1261 static int __exchange_data_block(struct inode *src_inode,
1262 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1263 pgoff_t len, bool full)
1265 block_t *src_blkaddr;
1271 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1273 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1274 array_size(olen, sizeof(block_t)),
1279 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1280 array_size(olen, sizeof(int)),
1283 kvfree(src_blkaddr);
1287 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1288 do_replace, src, olen);
1292 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1293 do_replace, src, dst, olen, full);
1301 kvfree(src_blkaddr);
1307 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1308 kvfree(src_blkaddr);
1313 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1315 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1316 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1317 pgoff_t start = offset >> PAGE_SHIFT;
1318 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1321 f2fs_balance_fs(sbi, true);
1323 /* avoid gc operation during block exchange */
1324 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1325 down_write(&F2FS_I(inode)->i_mmap_sem);
1328 f2fs_drop_extent_tree(inode);
1329 truncate_pagecache(inode, offset);
1330 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1331 f2fs_unlock_op(sbi);
1333 up_write(&F2FS_I(inode)->i_mmap_sem);
1334 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1338 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1343 if (offset + len >= i_size_read(inode))
1346 /* collapse range should be aligned to block size of f2fs. */
1347 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1350 ret = f2fs_convert_inline_inode(inode);
1354 /* write out all dirty pages from offset */
1355 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1359 ret = f2fs_do_collapse(inode, offset, len);
1363 /* write out all moved pages, if possible */
1364 down_write(&F2FS_I(inode)->i_mmap_sem);
1365 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1366 truncate_pagecache(inode, offset);
1368 new_size = i_size_read(inode) - len;
1369 ret = f2fs_truncate_blocks(inode, new_size, true);
1370 up_write(&F2FS_I(inode)->i_mmap_sem);
1372 f2fs_i_size_write(inode, new_size);
1376 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1379 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1380 pgoff_t index = start;
1381 unsigned int ofs_in_node = dn->ofs_in_node;
1385 for (; index < end; index++, dn->ofs_in_node++) {
1386 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1390 dn->ofs_in_node = ofs_in_node;
1391 ret = f2fs_reserve_new_blocks(dn, count);
1395 dn->ofs_in_node = ofs_in_node;
1396 for (index = start; index < end; index++, dn->ofs_in_node++) {
1397 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1399 * f2fs_reserve_new_blocks will not guarantee entire block
1402 if (dn->data_blkaddr == NULL_ADDR) {
1406 if (dn->data_blkaddr != NEW_ADDR) {
1407 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1408 dn->data_blkaddr = NEW_ADDR;
1409 f2fs_set_data_blkaddr(dn);
1413 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1418 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1421 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1422 struct address_space *mapping = inode->i_mapping;
1423 pgoff_t index, pg_start, pg_end;
1424 loff_t new_size = i_size_read(inode);
1425 loff_t off_start, off_end;
1428 ret = inode_newsize_ok(inode, (len + offset));
1432 ret = f2fs_convert_inline_inode(inode);
1436 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1440 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1441 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1443 off_start = offset & (PAGE_SIZE - 1);
1444 off_end = (offset + len) & (PAGE_SIZE - 1);
1446 if (pg_start == pg_end) {
1447 ret = fill_zero(inode, pg_start, off_start,
1448 off_end - off_start);
1452 new_size = max_t(loff_t, new_size, offset + len);
1455 ret = fill_zero(inode, pg_start++, off_start,
1456 PAGE_SIZE - off_start);
1460 new_size = max_t(loff_t, new_size,
1461 (loff_t)pg_start << PAGE_SHIFT);
1464 for (index = pg_start; index < pg_end;) {
1465 struct dnode_of_data dn;
1466 unsigned int end_offset;
1469 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1470 down_write(&F2FS_I(inode)->i_mmap_sem);
1472 truncate_pagecache_range(inode,
1473 (loff_t)index << PAGE_SHIFT,
1474 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1478 set_new_dnode(&dn, inode, NULL, NULL, 0);
1479 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1481 f2fs_unlock_op(sbi);
1482 up_write(&F2FS_I(inode)->i_mmap_sem);
1483 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1487 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1488 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1490 ret = f2fs_do_zero_range(&dn, index, end);
1491 f2fs_put_dnode(&dn);
1493 f2fs_unlock_op(sbi);
1494 up_write(&F2FS_I(inode)->i_mmap_sem);
1495 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1497 f2fs_balance_fs(sbi, dn.node_changed);
1503 new_size = max_t(loff_t, new_size,
1504 (loff_t)index << PAGE_SHIFT);
1508 ret = fill_zero(inode, pg_end, 0, off_end);
1512 new_size = max_t(loff_t, new_size, offset + len);
1517 if (new_size > i_size_read(inode)) {
1518 if (mode & FALLOC_FL_KEEP_SIZE)
1519 file_set_keep_isize(inode);
1521 f2fs_i_size_write(inode, new_size);
1526 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1528 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1529 pgoff_t nr, pg_start, pg_end, delta, idx;
1533 new_size = i_size_read(inode) + len;
1534 ret = inode_newsize_ok(inode, new_size);
1538 if (offset >= i_size_read(inode))
1541 /* insert range should be aligned to block size of f2fs. */
1542 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1545 ret = f2fs_convert_inline_inode(inode);
1549 f2fs_balance_fs(sbi, true);
1551 down_write(&F2FS_I(inode)->i_mmap_sem);
1552 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1553 up_write(&F2FS_I(inode)->i_mmap_sem);
1557 /* write out all dirty pages from offset */
1558 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1562 pg_start = offset >> PAGE_SHIFT;
1563 pg_end = (offset + len) >> PAGE_SHIFT;
1564 delta = pg_end - pg_start;
1565 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1567 /* avoid gc operation during block exchange */
1568 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1569 down_write(&F2FS_I(inode)->i_mmap_sem);
1570 truncate_pagecache(inode, offset);
1572 while (!ret && idx > pg_start) {
1573 nr = idx - pg_start;
1579 f2fs_drop_extent_tree(inode);
1581 ret = __exchange_data_block(inode, inode, idx,
1582 idx + delta, nr, false);
1583 f2fs_unlock_op(sbi);
1585 up_write(&F2FS_I(inode)->i_mmap_sem);
1586 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1588 /* write out all moved pages, if possible */
1589 down_write(&F2FS_I(inode)->i_mmap_sem);
1590 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1591 truncate_pagecache(inode, offset);
1592 up_write(&F2FS_I(inode)->i_mmap_sem);
1595 f2fs_i_size_write(inode, new_size);
1599 static int expand_inode_data(struct inode *inode, loff_t offset,
1600 loff_t len, int mode)
1602 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1603 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1604 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1605 .m_may_create = true };
1607 loff_t new_size = i_size_read(inode);
1611 err = inode_newsize_ok(inode, (len + offset));
1615 err = f2fs_convert_inline_inode(inode);
1619 f2fs_balance_fs(sbi, true);
1621 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1622 off_end = (offset + len) & (PAGE_SIZE - 1);
1624 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1625 map.m_len = pg_end - map.m_lblk;
1632 if (f2fs_is_pinned_file(inode)) {
1633 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1634 sbi->log_blocks_per_seg;
1637 if (map.m_len % sbi->blocks_per_seg)
1638 len += sbi->blocks_per_seg;
1640 map.m_len = sbi->blocks_per_seg;
1642 if (has_not_enough_free_secs(sbi, 0,
1643 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1644 down_write(&sbi->gc_lock);
1645 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1646 if (err && err != -ENODATA && err != -EAGAIN)
1650 down_write(&sbi->pin_sem);
1653 f2fs_allocate_new_segment(sbi, CURSEG_COLD_DATA_PINNED);
1654 f2fs_unlock_op(sbi);
1656 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1657 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1659 up_write(&sbi->pin_sem);
1663 map.m_lblk += map.m_len;
1669 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1678 last_off = map.m_lblk + map.m_len - 1;
1680 /* update new size to the failed position */
1681 new_size = (last_off == pg_end) ? offset + len :
1682 (loff_t)(last_off + 1) << PAGE_SHIFT;
1684 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1687 if (new_size > i_size_read(inode)) {
1688 if (mode & FALLOC_FL_KEEP_SIZE)
1689 file_set_keep_isize(inode);
1691 f2fs_i_size_write(inode, new_size);
1697 static long f2fs_fallocate(struct file *file, int mode,
1698 loff_t offset, loff_t len)
1700 struct inode *inode = file_inode(file);
1703 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1705 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1707 if (!f2fs_is_compress_backend_ready(inode))
1710 /* f2fs only support ->fallocate for regular file */
1711 if (!S_ISREG(inode->i_mode))
1714 if (IS_ENCRYPTED(inode) &&
1715 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1718 if (f2fs_compressed_file(inode) &&
1719 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1720 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1723 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1724 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1725 FALLOC_FL_INSERT_RANGE))
1730 if (mode & FALLOC_FL_PUNCH_HOLE) {
1731 if (offset >= inode->i_size)
1734 ret = punch_hole(inode, offset, len);
1735 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1736 ret = f2fs_collapse_range(inode, offset, len);
1737 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1738 ret = f2fs_zero_range(inode, offset, len, mode);
1739 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1740 ret = f2fs_insert_range(inode, offset, len);
1742 ret = expand_inode_data(inode, offset, len, mode);
1746 inode->i_mtime = inode->i_ctime = current_time(inode);
1747 f2fs_mark_inode_dirty_sync(inode, false);
1748 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1752 inode_unlock(inode);
1754 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1758 static int f2fs_release_file(struct inode *inode, struct file *filp)
1761 * f2fs_relase_file is called at every close calls. So we should
1762 * not drop any inmemory pages by close called by other process.
1764 if (!(filp->f_mode & FMODE_WRITE) ||
1765 atomic_read(&inode->i_writecount) != 1)
1768 /* some remained atomic pages should discarded */
1769 if (f2fs_is_atomic_file(inode))
1770 f2fs_drop_inmem_pages(inode);
1771 if (f2fs_is_volatile_file(inode)) {
1772 set_inode_flag(inode, FI_DROP_CACHE);
1773 filemap_fdatawrite(inode->i_mapping);
1774 clear_inode_flag(inode, FI_DROP_CACHE);
1775 clear_inode_flag(inode, FI_VOLATILE_FILE);
1776 stat_dec_volatile_write(inode);
1781 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1783 struct inode *inode = file_inode(file);
1786 * If the process doing a transaction is crashed, we should do
1787 * roll-back. Otherwise, other reader/write can see corrupted database
1788 * until all the writers close its file. Since this should be done
1789 * before dropping file lock, it needs to do in ->flush.
1791 if (f2fs_is_atomic_file(inode) &&
1792 F2FS_I(inode)->inmem_task == current)
1793 f2fs_drop_inmem_pages(inode);
1797 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1799 struct f2fs_inode_info *fi = F2FS_I(inode);
1800 u32 masked_flags = fi->i_flags & mask;
1802 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
1804 /* Is it quota file? Do not allow user to mess with it */
1805 if (IS_NOQUOTA(inode))
1808 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1809 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1811 if (!f2fs_empty_dir(inode))
1815 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1816 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1818 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1822 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1823 if (masked_flags & F2FS_COMPR_FL) {
1824 if (!f2fs_disable_compressed_file(inode))
1827 if (iflags & F2FS_NOCOMP_FL)
1829 if (iflags & F2FS_COMPR_FL) {
1830 if (!f2fs_may_compress(inode))
1832 if (S_ISREG(inode->i_mode) && inode->i_size)
1835 set_compress_context(inode);
1838 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1839 if (masked_flags & F2FS_COMPR_FL)
1843 fi->i_flags = iflags | (fi->i_flags & ~mask);
1844 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1845 (fi->i_flags & F2FS_NOCOMP_FL));
1847 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1848 set_inode_flag(inode, FI_PROJ_INHERIT);
1850 clear_inode_flag(inode, FI_PROJ_INHERIT);
1852 inode->i_ctime = current_time(inode);
1853 f2fs_set_inode_flags(inode);
1854 f2fs_mark_inode_dirty_sync(inode, true);
1858 /* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1861 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1862 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1863 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1864 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1867 static const struct {
1870 } f2fs_fsflags_map[] = {
1871 { F2FS_COMPR_FL, FS_COMPR_FL },
1872 { F2FS_SYNC_FL, FS_SYNC_FL },
1873 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1874 { F2FS_APPEND_FL, FS_APPEND_FL },
1875 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1876 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1877 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1878 { F2FS_INDEX_FL, FS_INDEX_FL },
1879 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1880 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1881 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1884 #define F2FS_GETTABLE_FS_FL ( \
1894 FS_PROJINHERIT_FL | \
1896 FS_INLINE_DATA_FL | \
1901 #define F2FS_SETTABLE_FS_FL ( \
1910 FS_PROJINHERIT_FL | \
1913 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1914 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1919 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1920 if (iflags & f2fs_fsflags_map[i].iflag)
1921 fsflags |= f2fs_fsflags_map[i].fsflag;
1926 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1927 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1932 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1933 if (fsflags & f2fs_fsflags_map[i].fsflag)
1934 iflags |= f2fs_fsflags_map[i].iflag;
1939 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1941 struct inode *inode = file_inode(filp);
1942 struct f2fs_inode_info *fi = F2FS_I(inode);
1943 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1945 if (IS_ENCRYPTED(inode))
1946 fsflags |= FS_ENCRYPT_FL;
1947 if (IS_VERITY(inode))
1948 fsflags |= FS_VERITY_FL;
1949 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1950 fsflags |= FS_INLINE_DATA_FL;
1951 if (is_inode_flag_set(inode, FI_PIN_FILE))
1952 fsflags |= FS_NOCOW_FL;
1954 fsflags &= F2FS_GETTABLE_FS_FL;
1956 return put_user(fsflags, (int __user *)arg);
1959 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1961 struct inode *inode = file_inode(filp);
1962 struct f2fs_inode_info *fi = F2FS_I(inode);
1963 u32 fsflags, old_fsflags;
1967 if (!inode_owner_or_capable(&init_user_ns, inode))
1970 if (get_user(fsflags, (int __user *)arg))
1973 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1975 fsflags &= F2FS_SETTABLE_FS_FL;
1977 iflags = f2fs_fsflags_to_iflags(fsflags);
1978 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1981 ret = mnt_want_write_file(filp);
1987 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1988 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1992 ret = f2fs_setflags_common(inode, iflags,
1993 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
1995 inode_unlock(inode);
1996 mnt_drop_write_file(filp);
2000 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2002 struct inode *inode = file_inode(filp);
2004 return put_user(inode->i_generation, (int __user *)arg);
2007 static int f2fs_ioc_start_atomic_write(struct file *filp)
2009 struct inode *inode = file_inode(filp);
2010 struct f2fs_inode_info *fi = F2FS_I(inode);
2011 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2014 if (!inode_owner_or_capable(&init_user_ns, inode))
2017 if (!S_ISREG(inode->i_mode))
2020 if (filp->f_flags & O_DIRECT)
2023 ret = mnt_want_write_file(filp);
2029 f2fs_disable_compressed_file(inode);
2031 if (f2fs_is_atomic_file(inode)) {
2032 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2037 ret = f2fs_convert_inline_inode(inode);
2041 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2044 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2045 * f2fs_is_atomic_file.
2047 if (get_dirty_pages(inode))
2048 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2049 inode->i_ino, get_dirty_pages(inode));
2050 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2052 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2056 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2057 if (list_empty(&fi->inmem_ilist))
2058 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
2059 sbi->atomic_files++;
2060 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2062 /* add inode in inmem_list first and set atomic_file */
2063 set_inode_flag(inode, FI_ATOMIC_FILE);
2064 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2065 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2067 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2068 F2FS_I(inode)->inmem_task = current;
2069 stat_update_max_atomic_write(inode);
2071 inode_unlock(inode);
2072 mnt_drop_write_file(filp);
2076 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2078 struct inode *inode = file_inode(filp);
2081 if (!inode_owner_or_capable(&init_user_ns, inode))
2084 ret = mnt_want_write_file(filp);
2088 f2fs_balance_fs(F2FS_I_SB(inode), true);
2092 if (f2fs_is_volatile_file(inode)) {
2097 if (f2fs_is_atomic_file(inode)) {
2098 ret = f2fs_commit_inmem_pages(inode);
2102 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2104 f2fs_drop_inmem_pages(inode);
2106 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2109 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2110 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2113 inode_unlock(inode);
2114 mnt_drop_write_file(filp);
2118 static int f2fs_ioc_start_volatile_write(struct file *filp)
2120 struct inode *inode = file_inode(filp);
2123 if (!inode_owner_or_capable(&init_user_ns, inode))
2126 if (!S_ISREG(inode->i_mode))
2129 ret = mnt_want_write_file(filp);
2135 if (f2fs_is_volatile_file(inode))
2138 ret = f2fs_convert_inline_inode(inode);
2142 stat_inc_volatile_write(inode);
2143 stat_update_max_volatile_write(inode);
2145 set_inode_flag(inode, FI_VOLATILE_FILE);
2146 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2148 inode_unlock(inode);
2149 mnt_drop_write_file(filp);
2153 static int f2fs_ioc_release_volatile_write(struct file *filp)
2155 struct inode *inode = file_inode(filp);
2158 if (!inode_owner_or_capable(&init_user_ns, inode))
2161 ret = mnt_want_write_file(filp);
2167 if (!f2fs_is_volatile_file(inode))
2170 if (!f2fs_is_first_block_written(inode)) {
2171 ret = truncate_partial_data_page(inode, 0, true);
2175 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2177 inode_unlock(inode);
2178 mnt_drop_write_file(filp);
2182 static int f2fs_ioc_abort_volatile_write(struct file *filp)
2184 struct inode *inode = file_inode(filp);
2187 if (!inode_owner_or_capable(&init_user_ns, inode))
2190 ret = mnt_want_write_file(filp);
2196 if (f2fs_is_atomic_file(inode))
2197 f2fs_drop_inmem_pages(inode);
2198 if (f2fs_is_volatile_file(inode)) {
2199 clear_inode_flag(inode, FI_VOLATILE_FILE);
2200 stat_dec_volatile_write(inode);
2201 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2204 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2206 inode_unlock(inode);
2208 mnt_drop_write_file(filp);
2209 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2213 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2215 struct inode *inode = file_inode(filp);
2216 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2217 struct super_block *sb = sbi->sb;
2221 if (!capable(CAP_SYS_ADMIN))
2224 if (get_user(in, (__u32 __user *)arg))
2227 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2228 ret = mnt_want_write_file(filp);
2230 if (ret == -EROFS) {
2232 f2fs_stop_checkpoint(sbi, false);
2233 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2234 trace_f2fs_shutdown(sbi, in, ret);
2241 case F2FS_GOING_DOWN_FULLSYNC:
2242 ret = freeze_bdev(sb->s_bdev);
2245 f2fs_stop_checkpoint(sbi, false);
2246 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2247 thaw_bdev(sb->s_bdev);
2249 case F2FS_GOING_DOWN_METASYNC:
2250 /* do checkpoint only */
2251 ret = f2fs_sync_fs(sb, 1);
2254 f2fs_stop_checkpoint(sbi, false);
2255 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2257 case F2FS_GOING_DOWN_NOSYNC:
2258 f2fs_stop_checkpoint(sbi, false);
2259 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2261 case F2FS_GOING_DOWN_METAFLUSH:
2262 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2263 f2fs_stop_checkpoint(sbi, false);
2264 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2266 case F2FS_GOING_DOWN_NEED_FSCK:
2267 set_sbi_flag(sbi, SBI_NEED_FSCK);
2268 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2269 set_sbi_flag(sbi, SBI_IS_DIRTY);
2270 /* do checkpoint only */
2271 ret = f2fs_sync_fs(sb, 1);
2278 f2fs_stop_gc_thread(sbi);
2279 f2fs_stop_discard_thread(sbi);
2281 f2fs_drop_discard_cmd(sbi);
2282 clear_opt(sbi, DISCARD);
2284 f2fs_update_time(sbi, REQ_TIME);
2286 if (in != F2FS_GOING_DOWN_FULLSYNC)
2287 mnt_drop_write_file(filp);
2289 trace_f2fs_shutdown(sbi, in, ret);
2294 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2296 struct inode *inode = file_inode(filp);
2297 struct super_block *sb = inode->i_sb;
2298 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2299 struct fstrim_range range;
2302 if (!capable(CAP_SYS_ADMIN))
2305 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2308 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2312 ret = mnt_want_write_file(filp);
2316 range.minlen = max((unsigned int)range.minlen,
2317 q->limits.discard_granularity);
2318 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2319 mnt_drop_write_file(filp);
2323 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2326 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2330 static bool uuid_is_nonzero(__u8 u[16])
2334 for (i = 0; i < 16; i++)
2340 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2342 struct inode *inode = file_inode(filp);
2344 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2347 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2349 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2352 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2354 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2356 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2359 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2361 struct inode *inode = file_inode(filp);
2362 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2365 if (!f2fs_sb_has_encrypt(sbi))
2368 err = mnt_want_write_file(filp);
2372 down_write(&sbi->sb_lock);
2374 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2377 /* update superblock with uuid */
2378 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2380 err = f2fs_commit_super(sbi, false);
2383 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2387 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2391 up_write(&sbi->sb_lock);
2392 mnt_drop_write_file(filp);
2396 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2399 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2402 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2405 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2407 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2410 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2413 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2415 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2418 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2421 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2424 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2427 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2430 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2433 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2436 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2439 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2441 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2444 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2447 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2449 struct inode *inode = file_inode(filp);
2450 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2454 if (!capable(CAP_SYS_ADMIN))
2457 if (get_user(sync, (__u32 __user *)arg))
2460 if (f2fs_readonly(sbi->sb))
2463 ret = mnt_want_write_file(filp);
2468 if (!down_write_trylock(&sbi->gc_lock)) {
2473 down_write(&sbi->gc_lock);
2476 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
2478 mnt_drop_write_file(filp);
2482 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2484 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2488 if (!capable(CAP_SYS_ADMIN))
2490 if (f2fs_readonly(sbi->sb))
2493 end = range->start + range->len;
2494 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2495 end >= MAX_BLKADDR(sbi))
2498 ret = mnt_want_write_file(filp);
2504 if (!down_write_trylock(&sbi->gc_lock)) {
2509 down_write(&sbi->gc_lock);
2512 ret = f2fs_gc(sbi, range->sync, true, GET_SEGNO(sbi, range->start));
2518 range->start += BLKS_PER_SEC(sbi);
2519 if (range->start <= end)
2522 mnt_drop_write_file(filp);
2526 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2528 struct f2fs_gc_range range;
2530 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2533 return __f2fs_ioc_gc_range(filp, &range);
2536 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2538 struct inode *inode = file_inode(filp);
2539 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2542 if (!capable(CAP_SYS_ADMIN))
2545 if (f2fs_readonly(sbi->sb))
2548 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2549 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2553 ret = mnt_want_write_file(filp);
2557 ret = f2fs_sync_fs(sbi->sb, 1);
2559 mnt_drop_write_file(filp);
2563 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2565 struct f2fs_defragment *range)
2567 struct inode *inode = file_inode(filp);
2568 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2569 .m_seg_type = NO_CHECK_TYPE ,
2570 .m_may_create = false };
2571 struct extent_info ei = {0, 0, 0};
2572 pgoff_t pg_start, pg_end, next_pgofs;
2573 unsigned int blk_per_seg = sbi->blocks_per_seg;
2574 unsigned int total = 0, sec_num;
2575 block_t blk_end = 0;
2576 bool fragmented = false;
2579 /* if in-place-update policy is enabled, don't waste time here */
2580 if (f2fs_should_update_inplace(inode, NULL))
2583 pg_start = range->start >> PAGE_SHIFT;
2584 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2586 f2fs_balance_fs(sbi, true);
2590 /* writeback all dirty pages in the range */
2591 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2592 range->start + range->len - 1);
2597 * lookup mapping info in extent cache, skip defragmenting if physical
2598 * block addresses are continuous.
2600 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2601 if (ei.fofs + ei.len >= pg_end)
2605 map.m_lblk = pg_start;
2606 map.m_next_pgofs = &next_pgofs;
2609 * lookup mapping info in dnode page cache, skip defragmenting if all
2610 * physical block addresses are continuous even if there are hole(s)
2611 * in logical blocks.
2613 while (map.m_lblk < pg_end) {
2614 map.m_len = pg_end - map.m_lblk;
2615 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2619 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2620 map.m_lblk = next_pgofs;
2624 if (blk_end && blk_end != map.m_pblk)
2627 /* record total count of block that we're going to move */
2630 blk_end = map.m_pblk + map.m_len;
2632 map.m_lblk += map.m_len;
2640 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
2643 * make sure there are enough free section for LFS allocation, this can
2644 * avoid defragment running in SSR mode when free section are allocated
2647 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2652 map.m_lblk = pg_start;
2653 map.m_len = pg_end - pg_start;
2656 while (map.m_lblk < pg_end) {
2661 map.m_len = pg_end - map.m_lblk;
2662 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2666 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2667 map.m_lblk = next_pgofs;
2671 set_inode_flag(inode, FI_DO_DEFRAG);
2674 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2677 page = f2fs_get_lock_data_page(inode, idx, true);
2679 err = PTR_ERR(page);
2683 set_page_dirty(page);
2684 f2fs_put_page(page, 1);
2693 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2696 clear_inode_flag(inode, FI_DO_DEFRAG);
2698 err = filemap_fdatawrite(inode->i_mapping);
2703 clear_inode_flag(inode, FI_DO_DEFRAG);
2705 inode_unlock(inode);
2707 range->len = (u64)total << PAGE_SHIFT;
2711 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2713 struct inode *inode = file_inode(filp);
2714 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2715 struct f2fs_defragment range;
2718 if (!capable(CAP_SYS_ADMIN))
2721 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2724 if (f2fs_readonly(sbi->sb))
2727 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2731 /* verify alignment of offset & size */
2732 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2735 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2736 sbi->max_file_blocks))
2739 err = mnt_want_write_file(filp);
2743 err = f2fs_defragment_range(sbi, filp, &range);
2744 mnt_drop_write_file(filp);
2746 f2fs_update_time(sbi, REQ_TIME);
2750 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2757 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2758 struct file *file_out, loff_t pos_out, size_t len)
2760 struct inode *src = file_inode(file_in);
2761 struct inode *dst = file_inode(file_out);
2762 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2763 size_t olen = len, dst_max_i_size = 0;
2767 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2768 src->i_sb != dst->i_sb)
2771 if (unlikely(f2fs_readonly(src->i_sb)))
2774 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2777 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2780 if (pos_out < 0 || pos_in < 0)
2784 if (pos_in == pos_out)
2786 if (pos_out > pos_in && pos_out < pos_in + len)
2793 if (!inode_trylock(dst))
2798 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2801 olen = len = src->i_size - pos_in;
2802 if (pos_in + len == src->i_size)
2803 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2809 dst_osize = dst->i_size;
2810 if (pos_out + olen > dst->i_size)
2811 dst_max_i_size = pos_out + olen;
2813 /* verify the end result is block aligned */
2814 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2815 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2816 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2819 ret = f2fs_convert_inline_inode(src);
2823 ret = f2fs_convert_inline_inode(dst);
2827 /* write out all dirty pages from offset */
2828 ret = filemap_write_and_wait_range(src->i_mapping,
2829 pos_in, pos_in + len);
2833 ret = filemap_write_and_wait_range(dst->i_mapping,
2834 pos_out, pos_out + len);
2838 f2fs_balance_fs(sbi, true);
2840 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2843 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2848 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2849 pos_out >> F2FS_BLKSIZE_BITS,
2850 len >> F2FS_BLKSIZE_BITS, false);
2854 f2fs_i_size_write(dst, dst_max_i_size);
2855 else if (dst_osize != dst->i_size)
2856 f2fs_i_size_write(dst, dst_osize);
2858 f2fs_unlock_op(sbi);
2861 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2863 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2872 static int __f2fs_ioc_move_range(struct file *filp,
2873 struct f2fs_move_range *range)
2878 if (!(filp->f_mode & FMODE_READ) ||
2879 !(filp->f_mode & FMODE_WRITE))
2882 dst = fdget(range->dst_fd);
2886 if (!(dst.file->f_mode & FMODE_WRITE)) {
2891 err = mnt_want_write_file(filp);
2895 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2896 range->pos_out, range->len);
2898 mnt_drop_write_file(filp);
2904 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2906 struct f2fs_move_range range;
2908 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2911 return __f2fs_ioc_move_range(filp, &range);
2914 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2916 struct inode *inode = file_inode(filp);
2917 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2918 struct sit_info *sm = SIT_I(sbi);
2919 unsigned int start_segno = 0, end_segno = 0;
2920 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2921 struct f2fs_flush_device range;
2924 if (!capable(CAP_SYS_ADMIN))
2927 if (f2fs_readonly(sbi->sb))
2930 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2933 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2937 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2938 __is_large_section(sbi)) {
2939 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2940 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2944 ret = mnt_want_write_file(filp);
2948 if (range.dev_num != 0)
2949 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2950 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2952 start_segno = sm->last_victim[FLUSH_DEVICE];
2953 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2954 start_segno = dev_start_segno;
2955 end_segno = min(start_segno + range.segments, dev_end_segno);
2957 while (start_segno < end_segno) {
2958 if (!down_write_trylock(&sbi->gc_lock)) {
2962 sm->last_victim[GC_CB] = end_segno + 1;
2963 sm->last_victim[GC_GREEDY] = end_segno + 1;
2964 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2965 ret = f2fs_gc(sbi, true, true, start_segno);
2973 mnt_drop_write_file(filp);
2977 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2979 struct inode *inode = file_inode(filp);
2980 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2982 /* Must validate to set it with SQLite behavior in Android. */
2983 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2985 return put_user(sb_feature, (u32 __user *)arg);
2989 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2991 struct dquot *transfer_to[MAXQUOTAS] = {};
2992 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2993 struct super_block *sb = sbi->sb;
2996 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2997 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2998 err = __dquot_transfer(inode, transfer_to);
3000 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3001 dqput(transfer_to[PRJQUOTA]);
3006 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3008 struct inode *inode = file_inode(filp);
3009 struct f2fs_inode_info *fi = F2FS_I(inode);
3010 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3015 if (!f2fs_sb_has_project_quota(sbi)) {
3016 if (projid != F2FS_DEF_PROJID)
3022 if (!f2fs_has_extra_attr(inode))
3025 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3027 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3031 /* Is it quota file? Do not allow user to mess with it */
3032 if (IS_NOQUOTA(inode))
3035 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3037 return PTR_ERR(ipage);
3039 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3042 f2fs_put_page(ipage, 1);
3045 f2fs_put_page(ipage, 1);
3047 err = dquot_initialize(inode);
3052 err = f2fs_transfer_project_quota(inode, kprojid);
3056 F2FS_I(inode)->i_projid = kprojid;
3057 inode->i_ctime = current_time(inode);
3058 f2fs_mark_inode_dirty_sync(inode, true);
3060 f2fs_unlock_op(sbi);
3064 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3069 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3071 if (projid != F2FS_DEF_PROJID)
3077 /* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
3080 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3081 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3082 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3085 static const struct {
3088 } f2fs_xflags_map[] = {
3089 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3090 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3091 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3092 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3093 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3094 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3097 #define F2FS_SUPPORTED_XFLAGS ( \
3099 FS_XFLAG_IMMUTABLE | \
3102 FS_XFLAG_NOATIME | \
3103 FS_XFLAG_PROJINHERIT)
3105 /* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3106 static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3111 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3112 if (iflags & f2fs_xflags_map[i].iflag)
3113 xflags |= f2fs_xflags_map[i].xflag;
3118 /* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3119 static inline u32 f2fs_xflags_to_iflags(u32 xflags)
3124 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3125 if (xflags & f2fs_xflags_map[i].xflag)
3126 iflags |= f2fs_xflags_map[i].iflag;
3131 static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3133 struct f2fs_inode_info *fi = F2FS_I(inode);
3135 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3137 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3138 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3141 static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3143 struct inode *inode = file_inode(filp);
3146 f2fs_fill_fsxattr(inode, &fa);
3148 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3153 static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3155 struct inode *inode = file_inode(filp);
3156 struct fsxattr fa, old_fa;
3160 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3163 /* Make sure caller has proper permission */
3164 if (!inode_owner_or_capable(&init_user_ns, inode))
3167 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
3170 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3171 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3174 err = mnt_want_write_file(filp);
3180 f2fs_fill_fsxattr(inode, &old_fa);
3181 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
3185 err = f2fs_setflags_common(inode, iflags,
3186 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
3190 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
3192 inode_unlock(inode);
3193 mnt_drop_write_file(filp);
3197 int f2fs_pin_file_control(struct inode *inode, bool inc)
3199 struct f2fs_inode_info *fi = F2FS_I(inode);
3200 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3202 /* Use i_gc_failures for normal file as a risk signal. */
3204 f2fs_i_gc_failures_write(inode,
3205 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3207 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3208 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3209 __func__, inode->i_ino,
3210 fi->i_gc_failures[GC_FAILURE_PIN]);
3211 clear_inode_flag(inode, FI_PIN_FILE);
3217 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3219 struct inode *inode = file_inode(filp);
3223 if (get_user(pin, (__u32 __user *)arg))
3226 if (!S_ISREG(inode->i_mode))
3229 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3232 ret = mnt_want_write_file(filp);
3238 if (f2fs_should_update_outplace(inode, NULL)) {
3244 clear_inode_flag(inode, FI_PIN_FILE);
3245 f2fs_i_gc_failures_write(inode, 0);
3249 if (f2fs_pin_file_control(inode, false)) {
3254 ret = f2fs_convert_inline_inode(inode);
3258 if (!f2fs_disable_compressed_file(inode)) {
3263 set_inode_flag(inode, FI_PIN_FILE);
3264 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3266 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3268 inode_unlock(inode);
3269 mnt_drop_write_file(filp);
3273 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3275 struct inode *inode = file_inode(filp);
3278 if (is_inode_flag_set(inode, FI_PIN_FILE))
3279 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3280 return put_user(pin, (u32 __user *)arg);
3283 int f2fs_precache_extents(struct inode *inode)
3285 struct f2fs_inode_info *fi = F2FS_I(inode);
3286 struct f2fs_map_blocks map;
3287 pgoff_t m_next_extent;
3291 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3295 map.m_next_pgofs = NULL;
3296 map.m_next_extent = &m_next_extent;
3297 map.m_seg_type = NO_CHECK_TYPE;
3298 map.m_may_create = false;
3299 end = F2FS_I_SB(inode)->max_file_blocks;
3301 while (map.m_lblk < end) {
3302 map.m_len = end - map.m_lblk;
3304 down_write(&fi->i_gc_rwsem[WRITE]);
3305 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3306 up_write(&fi->i_gc_rwsem[WRITE]);
3310 map.m_lblk = m_next_extent;
3316 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3318 return f2fs_precache_extents(file_inode(filp));
3321 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3323 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3326 if (!capable(CAP_SYS_ADMIN))
3329 if (f2fs_readonly(sbi->sb))
3332 if (copy_from_user(&block_count, (void __user *)arg,
3333 sizeof(block_count)))
3336 return f2fs_resize_fs(sbi, block_count);
3339 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3341 struct inode *inode = file_inode(filp);
3343 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3345 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3346 f2fs_warn(F2FS_I_SB(inode),
3347 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3352 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3355 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3357 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3360 return fsverity_ioctl_measure(filp, (void __user *)arg);
3363 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3365 struct inode *inode = file_inode(filp);
3366 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3371 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3375 down_read(&sbi->sb_lock);
3376 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3377 ARRAY_SIZE(sbi->raw_super->volume_name),
3378 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3379 up_read(&sbi->sb_lock);
3381 if (copy_to_user((char __user *)arg, vbuf,
3382 min(FSLABEL_MAX, count)))
3389 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3391 struct inode *inode = file_inode(filp);
3392 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3396 if (!capable(CAP_SYS_ADMIN))
3399 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3401 return PTR_ERR(vbuf);
3403 err = mnt_want_write_file(filp);
3407 down_write(&sbi->sb_lock);
3409 memset(sbi->raw_super->volume_name, 0,
3410 sizeof(sbi->raw_super->volume_name));
3411 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3412 sbi->raw_super->volume_name,
3413 ARRAY_SIZE(sbi->raw_super->volume_name));
3415 err = f2fs_commit_super(sbi, false);
3417 up_write(&sbi->sb_lock);
3419 mnt_drop_write_file(filp);
3425 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3427 struct inode *inode = file_inode(filp);
3430 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3433 if (!f2fs_compressed_file(inode))
3436 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3437 return put_user(blocks, (u64 __user *)arg);
3440 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3442 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3443 unsigned int released_blocks = 0;
3444 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3448 for (i = 0; i < count; i++) {
3449 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3450 dn->ofs_in_node + i);
3452 if (!__is_valid_data_blkaddr(blkaddr))
3454 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3455 DATA_GENERIC_ENHANCE)))
3456 return -EFSCORRUPTED;
3460 int compr_blocks = 0;
3462 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3463 blkaddr = f2fs_data_blkaddr(dn);
3466 if (blkaddr == COMPRESS_ADDR)
3468 dn->ofs_in_node += cluster_size;
3472 if (__is_valid_data_blkaddr(blkaddr))
3475 if (blkaddr != NEW_ADDR)
3478 dn->data_blkaddr = NULL_ADDR;
3479 f2fs_set_data_blkaddr(dn);
3482 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3483 dec_valid_block_count(sbi, dn->inode,
3484 cluster_size - compr_blocks);
3486 released_blocks += cluster_size - compr_blocks;
3488 count -= cluster_size;
3491 return released_blocks;
3494 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3496 struct inode *inode = file_inode(filp);
3497 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3498 pgoff_t page_idx = 0, last_idx;
3499 unsigned int released_blocks = 0;
3503 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3506 if (!f2fs_compressed_file(inode))
3509 if (f2fs_readonly(sbi->sb))
3512 ret = mnt_want_write_file(filp);
3516 f2fs_balance_fs(F2FS_I_SB(inode), true);
3520 writecount = atomic_read(&inode->i_writecount);
3521 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3522 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3527 if (IS_IMMUTABLE(inode)) {
3532 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3536 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3537 f2fs_set_inode_flags(inode);
3538 inode->i_ctime = current_time(inode);
3539 f2fs_mark_inode_dirty_sync(inode, true);
3541 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3544 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3545 down_write(&F2FS_I(inode)->i_mmap_sem);
3547 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3549 while (page_idx < last_idx) {
3550 struct dnode_of_data dn;
3551 pgoff_t end_offset, count;
3553 set_new_dnode(&dn, inode, NULL, NULL, 0);
3554 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3556 if (ret == -ENOENT) {
3557 page_idx = f2fs_get_next_page_offset(&dn,
3565 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3566 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3567 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3569 ret = release_compress_blocks(&dn, count);
3571 f2fs_put_dnode(&dn);
3577 released_blocks += ret;
3580 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3581 up_write(&F2FS_I(inode)->i_mmap_sem);
3583 inode_unlock(inode);
3585 mnt_drop_write_file(filp);
3588 ret = put_user(released_blocks, (u64 __user *)arg);
3589 } else if (released_blocks &&
3590 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3591 set_sbi_flag(sbi, SBI_NEED_FSCK);
3592 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3593 "iblocks=%llu, released=%u, compr_blocks=%u, "
3595 __func__, inode->i_ino, inode->i_blocks,
3597 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3603 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3605 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3606 unsigned int reserved_blocks = 0;
3607 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3611 for (i = 0; i < count; i++) {
3612 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3613 dn->ofs_in_node + i);
3615 if (!__is_valid_data_blkaddr(blkaddr))
3617 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3618 DATA_GENERIC_ENHANCE)))
3619 return -EFSCORRUPTED;
3623 int compr_blocks = 0;
3627 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3628 blkaddr = f2fs_data_blkaddr(dn);
3631 if (blkaddr == COMPRESS_ADDR)
3633 dn->ofs_in_node += cluster_size;
3637 if (__is_valid_data_blkaddr(blkaddr)) {
3642 dn->data_blkaddr = NEW_ADDR;
3643 f2fs_set_data_blkaddr(dn);
3646 reserved = cluster_size - compr_blocks;
3647 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3651 if (reserved != cluster_size - compr_blocks)
3654 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3656 reserved_blocks += reserved;
3658 count -= cluster_size;
3661 return reserved_blocks;
3664 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3666 struct inode *inode = file_inode(filp);
3667 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3668 pgoff_t page_idx = 0, last_idx;
3669 unsigned int reserved_blocks = 0;
3672 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3675 if (!f2fs_compressed_file(inode))
3678 if (f2fs_readonly(sbi->sb))
3681 ret = mnt_want_write_file(filp);
3685 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3688 f2fs_balance_fs(F2FS_I_SB(inode), true);
3692 if (!IS_IMMUTABLE(inode)) {
3697 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3698 down_write(&F2FS_I(inode)->i_mmap_sem);
3700 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3702 while (page_idx < last_idx) {
3703 struct dnode_of_data dn;
3704 pgoff_t end_offset, count;
3706 set_new_dnode(&dn, inode, NULL, NULL, 0);
3707 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3709 if (ret == -ENOENT) {
3710 page_idx = f2fs_get_next_page_offset(&dn,
3718 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3719 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3720 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3722 ret = reserve_compress_blocks(&dn, count);
3724 f2fs_put_dnode(&dn);
3730 reserved_blocks += ret;
3733 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3734 up_write(&F2FS_I(inode)->i_mmap_sem);
3737 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3738 f2fs_set_inode_flags(inode);
3739 inode->i_ctime = current_time(inode);
3740 f2fs_mark_inode_dirty_sync(inode, true);
3743 inode_unlock(inode);
3745 mnt_drop_write_file(filp);
3748 ret = put_user(reserved_blocks, (u64 __user *)arg);
3749 } else if (reserved_blocks &&
3750 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3751 set_sbi_flag(sbi, SBI_NEED_FSCK);
3752 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3753 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3755 __func__, inode->i_ino, inode->i_blocks,
3757 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3763 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3764 pgoff_t off, block_t block, block_t len, u32 flags)
3766 struct request_queue *q = bdev_get_queue(bdev);
3767 sector_t sector = SECTOR_FROM_BLOCK(block);
3768 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3774 if (flags & F2FS_TRIM_FILE_DISCARD)
3775 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3776 blk_queue_secure_erase(q) ?
3777 BLKDEV_DISCARD_SECURE : 0);
3779 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3780 if (IS_ENCRYPTED(inode))
3781 ret = fscrypt_zeroout_range(inode, off, block, len);
3783 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3790 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3792 struct inode *inode = file_inode(filp);
3793 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3794 struct address_space *mapping = inode->i_mapping;
3795 struct block_device *prev_bdev = NULL;
3796 struct f2fs_sectrim_range range;
3797 pgoff_t index, pg_end, prev_index = 0;
3798 block_t prev_block = 0, len = 0;
3800 bool to_end = false;
3803 if (!(filp->f_mode & FMODE_WRITE))
3806 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3810 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3811 !S_ISREG(inode->i_mode))
3814 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3815 !f2fs_hw_support_discard(sbi)) ||
3816 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3817 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3820 file_start_write(filp);
3823 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3824 range.start >= inode->i_size) {
3832 if (inode->i_size - range.start > range.len) {
3833 end_addr = range.start + range.len;
3835 end_addr = range.len == (u64)-1 ?
3836 sbi->sb->s_maxbytes : inode->i_size;
3840 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3841 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3846 index = F2FS_BYTES_TO_BLK(range.start);
3847 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3849 ret = f2fs_convert_inline_inode(inode);
3853 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3854 down_write(&F2FS_I(inode)->i_mmap_sem);
3856 ret = filemap_write_and_wait_range(mapping, range.start,
3857 to_end ? LLONG_MAX : end_addr - 1);
3861 truncate_inode_pages_range(mapping, range.start,
3862 to_end ? -1 : end_addr - 1);
3864 while (index < pg_end) {
3865 struct dnode_of_data dn;
3866 pgoff_t end_offset, count;
3869 set_new_dnode(&dn, inode, NULL, NULL, 0);
3870 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3872 if (ret == -ENOENT) {
3873 index = f2fs_get_next_page_offset(&dn, index);
3879 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3880 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3881 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3882 struct block_device *cur_bdev;
3883 block_t blkaddr = f2fs_data_blkaddr(&dn);
3885 if (!__is_valid_data_blkaddr(blkaddr))
3888 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3889 DATA_GENERIC_ENHANCE)) {
3890 ret = -EFSCORRUPTED;
3891 f2fs_put_dnode(&dn);
3895 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3896 if (f2fs_is_multi_device(sbi)) {
3897 int di = f2fs_target_device_index(sbi, blkaddr);
3899 blkaddr -= FDEV(di).start_blk;
3903 if (prev_bdev == cur_bdev &&
3904 index == prev_index + len &&
3905 blkaddr == prev_block + len) {
3908 ret = f2fs_secure_erase(prev_bdev,
3909 inode, prev_index, prev_block,
3912 f2fs_put_dnode(&dn);
3921 prev_bdev = cur_bdev;
3923 prev_block = blkaddr;
3928 f2fs_put_dnode(&dn);
3930 if (fatal_signal_pending(current)) {
3938 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3939 prev_block, len, range.flags);
3941 up_write(&F2FS_I(inode)->i_mmap_sem);
3942 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3944 inode_unlock(inode);
3945 file_end_write(filp);
3950 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3952 struct inode *inode = file_inode(filp);
3953 struct f2fs_comp_option option;
3955 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3958 inode_lock_shared(inode);
3960 if (!f2fs_compressed_file(inode)) {
3961 inode_unlock_shared(inode);
3965 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3966 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3968 inode_unlock_shared(inode);
3970 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3977 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3979 struct inode *inode = file_inode(filp);
3980 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3981 struct f2fs_comp_option option;
3984 if (!f2fs_sb_has_compression(sbi))
3987 if (!(filp->f_mode & FMODE_WRITE))
3990 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3994 if (!f2fs_compressed_file(inode) ||
3995 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3996 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3997 option.algorithm >= COMPRESS_MAX)
4000 file_start_write(filp);
4003 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4008 if (inode->i_size != 0) {
4013 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
4014 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
4015 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
4016 f2fs_mark_inode_dirty_sync(inode, true);
4018 if (!f2fs_is_compress_backend_ready(inode))
4019 f2fs_warn(sbi, "compression algorithm is successfully set, "
4020 "but current kernel doesn't support this algorithm.");
4022 inode_unlock(inode);
4023 file_end_write(filp);
4028 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4030 DEFINE_READAHEAD(ractl, NULL, inode->i_mapping, page_idx);
4031 struct address_space *mapping = inode->i_mapping;
4033 pgoff_t redirty_idx = page_idx;
4034 int i, page_len = 0, ret = 0;
4036 page_cache_ra_unbounded(&ractl, len, 0);
4038 for (i = 0; i < len; i++, page_idx++) {
4039 page = read_cache_page(mapping, page_idx, NULL, NULL);
4041 ret = PTR_ERR(page);
4047 for (i = 0; i < page_len; i++, redirty_idx++) {
4048 page = find_lock_page(mapping, redirty_idx);
4051 set_page_dirty(page);
4052 f2fs_put_page(page, 1);
4053 f2fs_put_page(page, 0);
4059 static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
4061 struct inode *inode = file_inode(filp);
4062 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4063 struct f2fs_inode_info *fi = F2FS_I(inode);
4064 pgoff_t page_idx = 0, last_idx;
4065 unsigned int blk_per_seg = sbi->blocks_per_seg;
4066 int cluster_size = F2FS_I(inode)->i_cluster_size;
4069 if (!f2fs_sb_has_compression(sbi) ||
4070 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4073 if (!(filp->f_mode & FMODE_WRITE))
4076 if (!f2fs_compressed_file(inode))
4079 f2fs_balance_fs(F2FS_I_SB(inode), true);
4081 file_start_write(filp);
4084 if (!f2fs_is_compress_backend_ready(inode)) {
4089 if (f2fs_is_mmap_file(inode)) {
4094 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4098 if (!atomic_read(&fi->i_compr_blocks))
4101 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4103 count = last_idx - page_idx;
4105 int len = min(cluster_size, count);
4107 ret = redirty_blocks(inode, page_idx, len);
4111 if (get_dirty_pages(inode) >= blk_per_seg)
4112 filemap_fdatawrite(inode->i_mapping);
4119 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4123 f2fs_warn(sbi, "%s: The file might be partially decompressed "
4124 "(errno=%d). Please delete the file.\n",
4127 inode_unlock(inode);
4128 file_end_write(filp);
4133 static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4135 struct inode *inode = file_inode(filp);
4136 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4137 pgoff_t page_idx = 0, last_idx;
4138 unsigned int blk_per_seg = sbi->blocks_per_seg;
4139 int cluster_size = F2FS_I(inode)->i_cluster_size;
4142 if (!f2fs_sb_has_compression(sbi) ||
4143 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4146 if (!(filp->f_mode & FMODE_WRITE))
4149 if (!f2fs_compressed_file(inode))
4152 f2fs_balance_fs(F2FS_I_SB(inode), true);
4154 file_start_write(filp);
4157 if (!f2fs_is_compress_backend_ready(inode)) {
4162 if (f2fs_is_mmap_file(inode)) {
4167 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4171 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4173 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4175 count = last_idx - page_idx;
4177 int len = min(cluster_size, count);
4179 ret = redirty_blocks(inode, page_idx, len);
4183 if (get_dirty_pages(inode) >= blk_per_seg)
4184 filemap_fdatawrite(inode->i_mapping);
4191 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4194 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4197 f2fs_warn(sbi, "%s: The file might be partially compressed "
4198 "(errno=%d). Please delete the file.\n",
4201 inode_unlock(inode);
4202 file_end_write(filp);
4207 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4210 case FS_IOC_GETFLAGS:
4211 return f2fs_ioc_getflags(filp, arg);
4212 case FS_IOC_SETFLAGS:
4213 return f2fs_ioc_setflags(filp, arg);
4214 case FS_IOC_GETVERSION:
4215 return f2fs_ioc_getversion(filp, arg);
4216 case F2FS_IOC_START_ATOMIC_WRITE:
4217 return f2fs_ioc_start_atomic_write(filp);
4218 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4219 return f2fs_ioc_commit_atomic_write(filp);
4220 case F2FS_IOC_START_VOLATILE_WRITE:
4221 return f2fs_ioc_start_volatile_write(filp);
4222 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4223 return f2fs_ioc_release_volatile_write(filp);
4224 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4225 return f2fs_ioc_abort_volatile_write(filp);
4226 case F2FS_IOC_SHUTDOWN:
4227 return f2fs_ioc_shutdown(filp, arg);
4229 return f2fs_ioc_fitrim(filp, arg);
4230 case FS_IOC_SET_ENCRYPTION_POLICY:
4231 return f2fs_ioc_set_encryption_policy(filp, arg);
4232 case FS_IOC_GET_ENCRYPTION_POLICY:
4233 return f2fs_ioc_get_encryption_policy(filp, arg);
4234 case FS_IOC_GET_ENCRYPTION_PWSALT:
4235 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4236 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4237 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4238 case FS_IOC_ADD_ENCRYPTION_KEY:
4239 return f2fs_ioc_add_encryption_key(filp, arg);
4240 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4241 return f2fs_ioc_remove_encryption_key(filp, arg);
4242 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4243 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4244 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4245 return f2fs_ioc_get_encryption_key_status(filp, arg);
4246 case FS_IOC_GET_ENCRYPTION_NONCE:
4247 return f2fs_ioc_get_encryption_nonce(filp, arg);
4248 case F2FS_IOC_GARBAGE_COLLECT:
4249 return f2fs_ioc_gc(filp, arg);
4250 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4251 return f2fs_ioc_gc_range(filp, arg);
4252 case F2FS_IOC_WRITE_CHECKPOINT:
4253 return f2fs_ioc_write_checkpoint(filp, arg);
4254 case F2FS_IOC_DEFRAGMENT:
4255 return f2fs_ioc_defragment(filp, arg);
4256 case F2FS_IOC_MOVE_RANGE:
4257 return f2fs_ioc_move_range(filp, arg);
4258 case F2FS_IOC_FLUSH_DEVICE:
4259 return f2fs_ioc_flush_device(filp, arg);
4260 case F2FS_IOC_GET_FEATURES:
4261 return f2fs_ioc_get_features(filp, arg);
4262 case FS_IOC_FSGETXATTR:
4263 return f2fs_ioc_fsgetxattr(filp, arg);
4264 case FS_IOC_FSSETXATTR:
4265 return f2fs_ioc_fssetxattr(filp, arg);
4266 case F2FS_IOC_GET_PIN_FILE:
4267 return f2fs_ioc_get_pin_file(filp, arg);
4268 case F2FS_IOC_SET_PIN_FILE:
4269 return f2fs_ioc_set_pin_file(filp, arg);
4270 case F2FS_IOC_PRECACHE_EXTENTS:
4271 return f2fs_ioc_precache_extents(filp, arg);
4272 case F2FS_IOC_RESIZE_FS:
4273 return f2fs_ioc_resize_fs(filp, arg);
4274 case FS_IOC_ENABLE_VERITY:
4275 return f2fs_ioc_enable_verity(filp, arg);
4276 case FS_IOC_MEASURE_VERITY:
4277 return f2fs_ioc_measure_verity(filp, arg);
4278 case FS_IOC_GETFSLABEL:
4279 return f2fs_ioc_getfslabel(filp, arg);
4280 case FS_IOC_SETFSLABEL:
4281 return f2fs_ioc_setfslabel(filp, arg);
4282 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4283 return f2fs_get_compress_blocks(filp, arg);
4284 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4285 return f2fs_release_compress_blocks(filp, arg);
4286 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4287 return f2fs_reserve_compress_blocks(filp, arg);
4288 case F2FS_IOC_SEC_TRIM_FILE:
4289 return f2fs_sec_trim_file(filp, arg);
4290 case F2FS_IOC_GET_COMPRESS_OPTION:
4291 return f2fs_ioc_get_compress_option(filp, arg);
4292 case F2FS_IOC_SET_COMPRESS_OPTION:
4293 return f2fs_ioc_set_compress_option(filp, arg);
4294 case F2FS_IOC_DECOMPRESS_FILE:
4295 return f2fs_ioc_decompress_file(filp, arg);
4296 case F2FS_IOC_COMPRESS_FILE:
4297 return f2fs_ioc_compress_file(filp, arg);
4303 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4305 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4307 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4310 return __f2fs_ioctl(filp, cmd, arg);
4313 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4315 struct file *file = iocb->ki_filp;
4316 struct inode *inode = file_inode(file);
4319 if (!f2fs_is_compress_backend_ready(inode))
4322 ret = generic_file_read_iter(iocb, iter);
4325 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4330 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4332 struct file *file = iocb->ki_filp;
4333 struct inode *inode = file_inode(file);
4336 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4341 if (!f2fs_is_compress_backend_ready(inode)) {
4346 if (iocb->ki_flags & IOCB_NOWAIT) {
4347 if (!inode_trylock(inode)) {
4355 ret = generic_write_checks(iocb, from);
4357 bool preallocated = false;
4358 size_t target_size = 0;
4361 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4362 set_inode_flag(inode, FI_NO_PREALLOC);
4364 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4365 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
4366 iov_iter_count(from)) ||
4367 f2fs_has_inline_data(inode) ||
4368 f2fs_force_buffered_io(inode, iocb, from)) {
4369 clear_inode_flag(inode, FI_NO_PREALLOC);
4370 inode_unlock(inode);
4377 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4380 if (iocb->ki_flags & IOCB_DIRECT) {
4382 * Convert inline data for Direct I/O before entering
4385 err = f2fs_convert_inline_inode(inode);
4389 * If force_buffere_io() is true, we have to allocate
4390 * blocks all the time, since f2fs_direct_IO will fall
4391 * back to buffered IO.
4393 if (!f2fs_force_buffered_io(inode, iocb, from) &&
4394 allow_outplace_dio(inode, iocb, from))
4397 preallocated = true;
4398 target_size = iocb->ki_pos + iov_iter_count(from);
4400 err = f2fs_preallocate_blocks(iocb, from);
4403 clear_inode_flag(inode, FI_NO_PREALLOC);
4404 inode_unlock(inode);
4409 ret = __generic_file_write_iter(iocb, from);
4410 clear_inode_flag(inode, FI_NO_PREALLOC);
4412 /* if we couldn't write data, we should deallocate blocks. */
4413 if (preallocated && i_size_read(inode) < target_size)
4414 f2fs_truncate(inode);
4417 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
4419 inode_unlock(inode);
4421 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4422 iov_iter_count(from), ret);
4424 ret = generic_write_sync(iocb, ret);
4428 #ifdef CONFIG_COMPAT
4429 struct compat_f2fs_gc_range {
4434 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4435 struct compat_f2fs_gc_range)
4437 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4439 struct compat_f2fs_gc_range __user *urange;
4440 struct f2fs_gc_range range;
4443 urange = compat_ptr(arg);
4444 err = get_user(range.sync, &urange->sync);
4445 err |= get_user(range.start, &urange->start);
4446 err |= get_user(range.len, &urange->len);
4450 return __f2fs_ioc_gc_range(file, &range);
4453 struct compat_f2fs_move_range {
4459 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4460 struct compat_f2fs_move_range)
4462 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4464 struct compat_f2fs_move_range __user *urange;
4465 struct f2fs_move_range range;
4468 urange = compat_ptr(arg);
4469 err = get_user(range.dst_fd, &urange->dst_fd);
4470 err |= get_user(range.pos_in, &urange->pos_in);
4471 err |= get_user(range.pos_out, &urange->pos_out);
4472 err |= get_user(range.len, &urange->len);
4476 return __f2fs_ioc_move_range(file, &range);
4479 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4481 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4483 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4487 case FS_IOC32_GETFLAGS:
4488 cmd = FS_IOC_GETFLAGS;
4490 case FS_IOC32_SETFLAGS:
4491 cmd = FS_IOC_SETFLAGS;
4493 case FS_IOC32_GETVERSION:
4494 cmd = FS_IOC_GETVERSION;
4496 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4497 return f2fs_compat_ioc_gc_range(file, arg);
4498 case F2FS_IOC32_MOVE_RANGE:
4499 return f2fs_compat_ioc_move_range(file, arg);
4500 case F2FS_IOC_START_ATOMIC_WRITE:
4501 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4502 case F2FS_IOC_START_VOLATILE_WRITE:
4503 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4504 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4505 case F2FS_IOC_SHUTDOWN:
4507 case FS_IOC_SET_ENCRYPTION_POLICY:
4508 case FS_IOC_GET_ENCRYPTION_PWSALT:
4509 case FS_IOC_GET_ENCRYPTION_POLICY:
4510 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4511 case FS_IOC_ADD_ENCRYPTION_KEY:
4512 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4513 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4514 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4515 case FS_IOC_GET_ENCRYPTION_NONCE:
4516 case F2FS_IOC_GARBAGE_COLLECT:
4517 case F2FS_IOC_WRITE_CHECKPOINT:
4518 case F2FS_IOC_DEFRAGMENT:
4519 case F2FS_IOC_FLUSH_DEVICE:
4520 case F2FS_IOC_GET_FEATURES:
4521 case FS_IOC_FSGETXATTR:
4522 case FS_IOC_FSSETXATTR:
4523 case F2FS_IOC_GET_PIN_FILE:
4524 case F2FS_IOC_SET_PIN_FILE:
4525 case F2FS_IOC_PRECACHE_EXTENTS:
4526 case F2FS_IOC_RESIZE_FS:
4527 case FS_IOC_ENABLE_VERITY:
4528 case FS_IOC_MEASURE_VERITY:
4529 case FS_IOC_GETFSLABEL:
4530 case FS_IOC_SETFSLABEL:
4531 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4532 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4533 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4534 case F2FS_IOC_SEC_TRIM_FILE:
4535 case F2FS_IOC_GET_COMPRESS_OPTION:
4536 case F2FS_IOC_SET_COMPRESS_OPTION:
4537 case F2FS_IOC_DECOMPRESS_FILE:
4538 case F2FS_IOC_COMPRESS_FILE:
4541 return -ENOIOCTLCMD;
4543 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4547 const struct file_operations f2fs_file_operations = {
4548 .llseek = f2fs_llseek,
4549 .read_iter = f2fs_file_read_iter,
4550 .write_iter = f2fs_file_write_iter,
4551 .open = f2fs_file_open,
4552 .release = f2fs_release_file,
4553 .mmap = f2fs_file_mmap,
4554 .flush = f2fs_file_flush,
4555 .fsync = f2fs_sync_file,
4556 .fallocate = f2fs_fallocate,
4557 .unlocked_ioctl = f2fs_ioctl,
4558 #ifdef CONFIG_COMPAT
4559 .compat_ioctl = f2fs_compat_ioctl,
4561 .splice_read = generic_file_splice_read,
4562 .splice_write = iter_file_splice_write,