1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/buffer_head.h>
12 #include <linux/writeback.h>
13 #include <linux/blkdev.h>
14 #include <linux/falloc.h>
15 #include <linux/types.h>
16 #include <linux/compat.h>
17 #include <linux/uaccess.h>
18 #include <linux/mount.h>
19 #include <linux/pagevec.h>
20 #include <linux/uio.h>
21 #include <linux/uuid.h>
22 #include <linux/file.h>
23 #include <linux/nls.h>
24 #include <linux/sched/signal.h>
25 #include <linux/fileattr.h>
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(IS_IMMUTABLE(inode)))
64 return VM_FAULT_SIGBUS;
66 if (unlikely(f2fs_cp_error(sbi))) {
71 if (!f2fs_is_checkpoint_ready(sbi)) {
76 err = f2fs_convert_inline_inode(inode);
80 #ifdef CONFIG_F2FS_FS_COMPRESSION
81 if (f2fs_compressed_file(inode)) {
82 int ret = f2fs_is_compressed_cluster(inode, page->index);
88 if (ret < F2FS_I(inode)->i_cluster_size) {
96 /* should do out of any locked page */
98 f2fs_balance_fs(sbi, true);
100 sb_start_pagefault(inode->i_sb);
102 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
104 file_update_time(vmf->vma->vm_file);
105 down_read(&F2FS_I(inode)->i_mmap_sem);
107 if (unlikely(page->mapping != inode->i_mapping ||
108 page_offset(page) > i_size_read(inode) ||
109 !PageUptodate(page))) {
116 /* block allocation */
117 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
118 set_new_dnode(&dn, inode, NULL, NULL, 0);
119 err = f2fs_get_block(&dn, page->index);
121 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
124 #ifdef CONFIG_F2FS_FS_COMPRESSION
126 set_new_dnode(&dn, inode, NULL, NULL, 0);
127 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
136 f2fs_wait_on_page_writeback(page, DATA, false, true);
138 /* wait for GCed page writeback via META_MAPPING */
139 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
142 * check to see if the page is mapped already (no holes)
144 if (PageMappedToDisk(page))
147 /* page is wholly or partially inside EOF */
148 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
149 i_size_read(inode)) {
152 offset = i_size_read(inode) & ~PAGE_MASK;
153 zero_user_segment(page, offset, PAGE_SIZE);
155 set_page_dirty(page);
156 if (!PageUptodate(page))
157 SetPageUptodate(page);
159 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
160 f2fs_update_time(sbi, REQ_TIME);
162 trace_f2fs_vm_page_mkwrite(page, DATA);
164 up_read(&F2FS_I(inode)->i_mmap_sem);
166 sb_end_pagefault(inode->i_sb);
168 return block_page_mkwrite_return(err);
171 static const struct vm_operations_struct f2fs_file_vm_ops = {
172 .fault = f2fs_filemap_fault,
173 .map_pages = filemap_map_pages,
174 .page_mkwrite = f2fs_vm_page_mkwrite,
177 static int get_parent_ino(struct inode *inode, nid_t *pino)
179 struct dentry *dentry;
182 * Make sure to get the non-deleted alias. The alias associated with
183 * the open file descriptor being fsync()'ed may be deleted already.
185 dentry = d_find_alias(inode);
189 *pino = parent_ino(dentry);
194 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
196 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
197 enum cp_reason_type cp_reason = CP_NO_NEEDED;
199 if (!S_ISREG(inode->i_mode))
200 cp_reason = CP_NON_REGULAR;
201 else if (f2fs_compressed_file(inode))
202 cp_reason = CP_COMPRESSED;
203 else if (inode->i_nlink != 1)
204 cp_reason = CP_HARDLINK;
205 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
206 cp_reason = CP_SB_NEED_CP;
207 else if (file_wrong_pino(inode))
208 cp_reason = CP_WRONG_PINO;
209 else if (!f2fs_space_for_roll_forward(sbi))
210 cp_reason = CP_NO_SPC_ROLL;
211 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
212 cp_reason = CP_NODE_NEED_CP;
213 else if (test_opt(sbi, FASTBOOT))
214 cp_reason = CP_FASTBOOT_MODE;
215 else if (F2FS_OPTION(sbi).active_logs == 2)
216 cp_reason = CP_SPEC_LOG_NUM;
217 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
218 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
219 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
221 cp_reason = CP_RECOVER_DIR;
226 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
228 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
230 /* But we need to avoid that there are some inode updates */
231 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
237 static void try_to_fix_pino(struct inode *inode)
239 struct f2fs_inode_info *fi = F2FS_I(inode);
242 down_write(&fi->i_sem);
243 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
244 get_parent_ino(inode, &pino)) {
245 f2fs_i_pino_write(inode, pino);
246 file_got_pino(inode);
248 up_write(&fi->i_sem);
251 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
252 int datasync, bool atomic)
254 struct inode *inode = file->f_mapping->host;
255 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
256 nid_t ino = inode->i_ino;
258 enum cp_reason_type cp_reason = 0;
259 struct writeback_control wbc = {
260 .sync_mode = WB_SYNC_ALL,
261 .nr_to_write = LONG_MAX,
264 unsigned int seq_id = 0;
266 if (unlikely(f2fs_readonly(inode->i_sb) ||
267 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
270 trace_f2fs_sync_file_enter(inode);
272 if (S_ISDIR(inode->i_mode))
275 /* if fdatasync is triggered, let's do in-place-update */
276 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
277 set_inode_flag(inode, FI_NEED_IPU);
278 ret = file_write_and_wait_range(file, start, end);
279 clear_inode_flag(inode, FI_NEED_IPU);
282 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
286 /* if the inode is dirty, let's recover all the time */
287 if (!f2fs_skip_inode_update(inode, datasync)) {
288 f2fs_write_inode(inode, NULL);
293 * if there is no written data, don't waste time to write recovery info.
295 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
296 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
298 /* it may call write_inode just prior to fsync */
299 if (need_inode_page_update(sbi, ino))
302 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
303 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
309 * Both of fdatasync() and fsync() are able to be recovered from
312 down_read(&F2FS_I(inode)->i_sem);
313 cp_reason = need_do_checkpoint(inode);
314 up_read(&F2FS_I(inode)->i_sem);
317 /* all the dirty node pages should be flushed for POR */
318 ret = f2fs_sync_fs(inode->i_sb, 1);
321 * We've secured consistency through sync_fs. Following pino
322 * will be used only for fsynced inodes after checkpoint.
324 try_to_fix_pino(inode);
325 clear_inode_flag(inode, FI_APPEND_WRITE);
326 clear_inode_flag(inode, FI_UPDATE_WRITE);
330 atomic_inc(&sbi->wb_sync_req[NODE]);
331 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
332 atomic_dec(&sbi->wb_sync_req[NODE]);
336 /* if cp_error was enabled, we should avoid infinite loop */
337 if (unlikely(f2fs_cp_error(sbi))) {
342 if (f2fs_need_inode_block_update(sbi, ino)) {
343 f2fs_mark_inode_dirty_sync(inode, true);
344 f2fs_write_inode(inode, NULL);
349 * If it's atomic_write, it's just fine to keep write ordering. So
350 * here we don't need to wait for node write completion, since we use
351 * node chain which serializes node blocks. If one of node writes are
352 * reordered, we can see simply broken chain, resulting in stopping
353 * roll-forward recovery. It means we'll recover all or none node blocks
357 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
362 /* once recovery info is written, don't need to tack this */
363 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
364 clear_inode_flag(inode, FI_APPEND_WRITE);
366 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
367 ret = f2fs_issue_flush(sbi, inode->i_ino);
369 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
370 clear_inode_flag(inode, FI_UPDATE_WRITE);
371 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
373 f2fs_update_time(sbi, REQ_TIME);
375 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
379 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
381 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
383 return f2fs_do_sync_file(file, start, end, datasync, false);
386 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
387 pgoff_t index, int whence)
391 if (__is_valid_data_blkaddr(blkaddr))
393 if (blkaddr == NEW_ADDR &&
394 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
398 if (blkaddr == NULL_ADDR)
405 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
407 struct inode *inode = file->f_mapping->host;
408 loff_t maxbytes = inode->i_sb->s_maxbytes;
409 struct dnode_of_data dn;
410 pgoff_t pgofs, end_offset;
411 loff_t data_ofs = offset;
417 isize = i_size_read(inode);
421 /* handle inline data case */
422 if (f2fs_has_inline_data(inode)) {
423 if (whence == SEEK_HOLE) {
426 } else if (whence == SEEK_DATA) {
432 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
434 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
435 set_new_dnode(&dn, inode, NULL, NULL, 0);
436 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
437 if (err && err != -ENOENT) {
439 } else if (err == -ENOENT) {
440 /* direct node does not exists */
441 if (whence == SEEK_DATA) {
442 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
449 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
451 /* find data/hole in dnode block */
452 for (; dn.ofs_in_node < end_offset;
453 dn.ofs_in_node++, pgofs++,
454 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
457 blkaddr = f2fs_data_blkaddr(&dn);
459 if (__is_valid_data_blkaddr(blkaddr) &&
460 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
461 blkaddr, DATA_GENERIC_ENHANCE)) {
466 if (__found_offset(file->f_mapping, blkaddr,
475 if (whence == SEEK_DATA)
478 if (whence == SEEK_HOLE && data_ofs > isize)
481 return vfs_setpos(file, data_ofs, maxbytes);
487 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
489 struct inode *inode = file->f_mapping->host;
490 loff_t maxbytes = inode->i_sb->s_maxbytes;
492 if (f2fs_compressed_file(inode))
493 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
499 return generic_file_llseek_size(file, offset, whence,
500 maxbytes, i_size_read(inode));
505 return f2fs_seek_block(file, offset, whence);
511 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
513 struct inode *inode = file_inode(file);
515 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
518 if (!f2fs_is_compress_backend_ready(inode))
522 vma->vm_ops = &f2fs_file_vm_ops;
523 set_inode_flag(inode, FI_MMAP_FILE);
527 static int f2fs_file_open(struct inode *inode, struct file *filp)
529 int err = fscrypt_file_open(inode, filp);
534 if (!f2fs_is_compress_backend_ready(inode))
537 err = fsverity_file_open(inode, filp);
541 filp->f_mode |= FMODE_NOWAIT;
543 return dquot_file_open(inode, filp);
546 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
548 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
549 struct f2fs_node *raw_node;
550 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
553 bool compressed_cluster = false;
554 int cluster_index = 0, valid_blocks = 0;
555 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
556 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
558 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
559 base = get_extra_isize(dn->inode);
561 raw_node = F2FS_NODE(dn->node_page);
562 addr = blkaddr_in_node(raw_node) + base + ofs;
564 /* Assumption: truncateion starts with cluster */
565 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
566 block_t blkaddr = le32_to_cpu(*addr);
568 if (f2fs_compressed_file(dn->inode) &&
569 !(cluster_index & (cluster_size - 1))) {
570 if (compressed_cluster)
571 f2fs_i_compr_blocks_update(dn->inode,
572 valid_blocks, false);
573 compressed_cluster = (blkaddr == COMPRESS_ADDR);
577 if (blkaddr == NULL_ADDR)
580 dn->data_blkaddr = NULL_ADDR;
581 f2fs_set_data_blkaddr(dn);
583 if (__is_valid_data_blkaddr(blkaddr)) {
584 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
585 DATA_GENERIC_ENHANCE))
587 if (compressed_cluster)
591 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
592 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
594 f2fs_invalidate_blocks(sbi, blkaddr);
596 if (!released || blkaddr != COMPRESS_ADDR)
600 if (compressed_cluster)
601 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
606 * once we invalidate valid blkaddr in range [ofs, ofs + count],
607 * we will invalidate all blkaddr in the whole range.
609 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
611 f2fs_update_extent_cache_range(dn, fofs, 0, len);
612 dec_valid_block_count(sbi, dn->inode, nr_free);
614 dn->ofs_in_node = ofs;
616 f2fs_update_time(sbi, REQ_TIME);
617 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
618 dn->ofs_in_node, nr_free);
621 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
623 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
626 static int truncate_partial_data_page(struct inode *inode, u64 from,
629 loff_t offset = from & (PAGE_SIZE - 1);
630 pgoff_t index = from >> PAGE_SHIFT;
631 struct address_space *mapping = inode->i_mapping;
634 if (!offset && !cache_only)
638 page = find_lock_page(mapping, index);
639 if (page && PageUptodate(page))
641 f2fs_put_page(page, 1);
645 page = f2fs_get_lock_data_page(inode, index, true);
647 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
649 f2fs_wait_on_page_writeback(page, DATA, true, true);
650 zero_user(page, offset, PAGE_SIZE - offset);
652 /* An encrypted inode should have a key and truncate the last page. */
653 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
655 set_page_dirty(page);
656 f2fs_put_page(page, 1);
660 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
662 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
663 struct dnode_of_data dn;
665 int count = 0, err = 0;
667 bool truncate_page = false;
669 trace_f2fs_truncate_blocks_enter(inode, from);
671 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
673 if (free_from >= max_file_blocks(inode))
679 ipage = f2fs_get_node_page(sbi, inode->i_ino);
681 err = PTR_ERR(ipage);
685 if (f2fs_has_inline_data(inode)) {
686 f2fs_truncate_inline_inode(inode, ipage, from);
687 f2fs_put_page(ipage, 1);
688 truncate_page = true;
692 set_new_dnode(&dn, inode, ipage, NULL, 0);
693 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
700 count = ADDRS_PER_PAGE(dn.node_page, inode);
702 count -= dn.ofs_in_node;
703 f2fs_bug_on(sbi, count < 0);
705 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
706 f2fs_truncate_data_blocks_range(&dn, count);
712 err = f2fs_truncate_inode_blocks(inode, free_from);
717 /* lastly zero out the first data page */
719 err = truncate_partial_data_page(inode, from, truncate_page);
721 trace_f2fs_truncate_blocks_exit(inode, err);
725 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
727 u64 free_from = from;
730 #ifdef CONFIG_F2FS_FS_COMPRESSION
732 * for compressed file, only support cluster size
733 * aligned truncation.
735 if (f2fs_compressed_file(inode))
736 free_from = round_up(from,
737 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
740 err = f2fs_do_truncate_blocks(inode, free_from, lock);
744 #ifdef CONFIG_F2FS_FS_COMPRESSION
745 if (from != free_from) {
746 err = f2fs_truncate_partial_cluster(inode, from, lock);
755 int f2fs_truncate(struct inode *inode)
759 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
762 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
763 S_ISLNK(inode->i_mode)))
766 trace_f2fs_truncate(inode);
768 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
769 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
773 err = dquot_initialize(inode);
777 /* we should check inline_data size */
778 if (!f2fs_may_inline_data(inode)) {
779 err = f2fs_convert_inline_inode(inode);
784 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
788 inode->i_mtime = inode->i_ctime = current_time(inode);
789 f2fs_mark_inode_dirty_sync(inode, false);
793 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
794 struct kstat *stat, u32 request_mask, unsigned int query_flags)
796 struct inode *inode = d_inode(path->dentry);
797 struct f2fs_inode_info *fi = F2FS_I(inode);
798 struct f2fs_inode *ri;
801 if (f2fs_has_extra_attr(inode) &&
802 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
803 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
804 stat->result_mask |= STATX_BTIME;
805 stat->btime.tv_sec = fi->i_crtime.tv_sec;
806 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
810 if (flags & F2FS_COMPR_FL)
811 stat->attributes |= STATX_ATTR_COMPRESSED;
812 if (flags & F2FS_APPEND_FL)
813 stat->attributes |= STATX_ATTR_APPEND;
814 if (IS_ENCRYPTED(inode))
815 stat->attributes |= STATX_ATTR_ENCRYPTED;
816 if (flags & F2FS_IMMUTABLE_FL)
817 stat->attributes |= STATX_ATTR_IMMUTABLE;
818 if (flags & F2FS_NODUMP_FL)
819 stat->attributes |= STATX_ATTR_NODUMP;
820 if (IS_VERITY(inode))
821 stat->attributes |= STATX_ATTR_VERITY;
823 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
825 STATX_ATTR_ENCRYPTED |
826 STATX_ATTR_IMMUTABLE |
830 generic_fillattr(&init_user_ns, inode, stat);
832 /* we need to show initial sectors used for inline_data/dentries */
833 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
834 f2fs_has_inline_dentry(inode))
835 stat->blocks += (stat->size + 511) >> 9;
840 #ifdef CONFIG_F2FS_FS_POSIX_ACL
841 static void __setattr_copy(struct user_namespace *mnt_userns,
842 struct inode *inode, const struct iattr *attr)
844 unsigned int ia_valid = attr->ia_valid;
846 if (ia_valid & ATTR_UID)
847 inode->i_uid = attr->ia_uid;
848 if (ia_valid & ATTR_GID)
849 inode->i_gid = attr->ia_gid;
850 if (ia_valid & ATTR_ATIME)
851 inode->i_atime = attr->ia_atime;
852 if (ia_valid & ATTR_MTIME)
853 inode->i_mtime = attr->ia_mtime;
854 if (ia_valid & ATTR_CTIME)
855 inode->i_ctime = attr->ia_ctime;
856 if (ia_valid & ATTR_MODE) {
857 umode_t mode = attr->ia_mode;
858 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
860 if (!in_group_p(kgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
862 set_acl_inode(inode, mode);
866 #define __setattr_copy setattr_copy
869 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
872 struct inode *inode = d_inode(dentry);
875 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
878 if (unlikely(IS_IMMUTABLE(inode)))
881 if (unlikely(IS_APPEND(inode) &&
882 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
883 ATTR_GID | ATTR_TIMES_SET))))
886 if ((attr->ia_valid & ATTR_SIZE) &&
887 !f2fs_is_compress_backend_ready(inode))
890 err = setattr_prepare(&init_user_ns, dentry, attr);
894 err = fscrypt_prepare_setattr(dentry, attr);
898 err = fsverity_prepare_setattr(dentry, attr);
902 if (is_quota_modification(inode, attr)) {
903 err = dquot_initialize(inode);
907 if ((attr->ia_valid & ATTR_UID &&
908 !uid_eq(attr->ia_uid, inode->i_uid)) ||
909 (attr->ia_valid & ATTR_GID &&
910 !gid_eq(attr->ia_gid, inode->i_gid))) {
911 f2fs_lock_op(F2FS_I_SB(inode));
912 err = dquot_transfer(inode, attr);
914 set_sbi_flag(F2FS_I_SB(inode),
915 SBI_QUOTA_NEED_REPAIR);
916 f2fs_unlock_op(F2FS_I_SB(inode));
920 * update uid/gid under lock_op(), so that dquot and inode can
921 * be updated atomically.
923 if (attr->ia_valid & ATTR_UID)
924 inode->i_uid = attr->ia_uid;
925 if (attr->ia_valid & ATTR_GID)
926 inode->i_gid = attr->ia_gid;
927 f2fs_mark_inode_dirty_sync(inode, true);
928 f2fs_unlock_op(F2FS_I_SB(inode));
931 if (attr->ia_valid & ATTR_SIZE) {
932 loff_t old_size = i_size_read(inode);
934 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
936 * should convert inline inode before i_size_write to
937 * keep smaller than inline_data size with inline flag.
939 err = f2fs_convert_inline_inode(inode);
944 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
945 down_write(&F2FS_I(inode)->i_mmap_sem);
947 truncate_setsize(inode, attr->ia_size);
949 if (attr->ia_size <= old_size)
950 err = f2fs_truncate(inode);
952 * do not trim all blocks after i_size if target size is
953 * larger than i_size.
955 up_write(&F2FS_I(inode)->i_mmap_sem);
956 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
960 spin_lock(&F2FS_I(inode)->i_size_lock);
961 inode->i_mtime = inode->i_ctime = current_time(inode);
962 F2FS_I(inode)->last_disk_size = i_size_read(inode);
963 spin_unlock(&F2FS_I(inode)->i_size_lock);
966 __setattr_copy(&init_user_ns, inode, attr);
968 if (attr->ia_valid & ATTR_MODE) {
969 err = posix_acl_chmod(&init_user_ns, inode, f2fs_get_inode_mode(inode));
971 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
973 inode->i_mode = F2FS_I(inode)->i_acl_mode;
974 clear_inode_flag(inode, FI_ACL_MODE);
978 /* file size may changed here */
979 f2fs_mark_inode_dirty_sync(inode, true);
981 /* inode change will produce dirty node pages flushed by checkpoint */
982 f2fs_balance_fs(F2FS_I_SB(inode), true);
987 const struct inode_operations f2fs_file_inode_operations = {
988 .getattr = f2fs_getattr,
989 .setattr = f2fs_setattr,
990 .get_acl = f2fs_get_acl,
991 .set_acl = f2fs_set_acl,
992 .listxattr = f2fs_listxattr,
993 .fiemap = f2fs_fiemap,
994 .fileattr_get = f2fs_fileattr_get,
995 .fileattr_set = f2fs_fileattr_set,
998 static int fill_zero(struct inode *inode, pgoff_t index,
999 loff_t start, loff_t len)
1001 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1007 f2fs_balance_fs(sbi, true);
1010 page = f2fs_get_new_data_page(inode, NULL, index, false);
1011 f2fs_unlock_op(sbi);
1014 return PTR_ERR(page);
1016 f2fs_wait_on_page_writeback(page, DATA, true, true);
1017 zero_user(page, start, len);
1018 set_page_dirty(page);
1019 f2fs_put_page(page, 1);
1023 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1027 while (pg_start < pg_end) {
1028 struct dnode_of_data dn;
1029 pgoff_t end_offset, count;
1031 set_new_dnode(&dn, inode, NULL, NULL, 0);
1032 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1034 if (err == -ENOENT) {
1035 pg_start = f2fs_get_next_page_offset(&dn,
1042 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1043 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1045 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1047 f2fs_truncate_data_blocks_range(&dn, count);
1048 f2fs_put_dnode(&dn);
1055 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1057 pgoff_t pg_start, pg_end;
1058 loff_t off_start, off_end;
1061 ret = f2fs_convert_inline_inode(inode);
1065 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1066 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1068 off_start = offset & (PAGE_SIZE - 1);
1069 off_end = (offset + len) & (PAGE_SIZE - 1);
1071 if (pg_start == pg_end) {
1072 ret = fill_zero(inode, pg_start, off_start,
1073 off_end - off_start);
1078 ret = fill_zero(inode, pg_start++, off_start,
1079 PAGE_SIZE - off_start);
1084 ret = fill_zero(inode, pg_end, 0, off_end);
1089 if (pg_start < pg_end) {
1090 struct address_space *mapping = inode->i_mapping;
1091 loff_t blk_start, blk_end;
1092 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1094 f2fs_balance_fs(sbi, true);
1096 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1097 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1099 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1100 down_write(&F2FS_I(inode)->i_mmap_sem);
1102 truncate_inode_pages_range(mapping, blk_start,
1106 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1107 f2fs_unlock_op(sbi);
1109 up_write(&F2FS_I(inode)->i_mmap_sem);
1110 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1117 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1118 int *do_replace, pgoff_t off, pgoff_t len)
1120 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1121 struct dnode_of_data dn;
1125 set_new_dnode(&dn, inode, NULL, NULL, 0);
1126 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1127 if (ret && ret != -ENOENT) {
1129 } else if (ret == -ENOENT) {
1130 if (dn.max_level == 0)
1132 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1133 dn.ofs_in_node, len);
1139 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1140 dn.ofs_in_node, len);
1141 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1142 *blkaddr = f2fs_data_blkaddr(&dn);
1144 if (__is_valid_data_blkaddr(*blkaddr) &&
1145 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1146 DATA_GENERIC_ENHANCE)) {
1147 f2fs_put_dnode(&dn);
1148 return -EFSCORRUPTED;
1151 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1153 if (f2fs_lfs_mode(sbi)) {
1154 f2fs_put_dnode(&dn);
1158 /* do not invalidate this block address */
1159 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1163 f2fs_put_dnode(&dn);
1172 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1173 int *do_replace, pgoff_t off, int len)
1175 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1176 struct dnode_of_data dn;
1179 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1180 if (*do_replace == 0)
1183 set_new_dnode(&dn, inode, NULL, NULL, 0);
1184 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1186 dec_valid_block_count(sbi, inode, 1);
1187 f2fs_invalidate_blocks(sbi, *blkaddr);
1189 f2fs_update_data_blkaddr(&dn, *blkaddr);
1191 f2fs_put_dnode(&dn);
1196 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1197 block_t *blkaddr, int *do_replace,
1198 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1200 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1205 if (blkaddr[i] == NULL_ADDR && !full) {
1210 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1211 struct dnode_of_data dn;
1212 struct node_info ni;
1216 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1217 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1221 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1223 f2fs_put_dnode(&dn);
1227 ilen = min((pgoff_t)
1228 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1229 dn.ofs_in_node, len - i);
1231 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1232 f2fs_truncate_data_blocks_range(&dn, 1);
1234 if (do_replace[i]) {
1235 f2fs_i_blocks_write(src_inode,
1237 f2fs_i_blocks_write(dst_inode,
1239 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1240 blkaddr[i], ni.version, true, false);
1246 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1247 if (dst_inode->i_size < new_size)
1248 f2fs_i_size_write(dst_inode, new_size);
1249 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1251 f2fs_put_dnode(&dn);
1253 struct page *psrc, *pdst;
1255 psrc = f2fs_get_lock_data_page(src_inode,
1258 return PTR_ERR(psrc);
1259 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1262 f2fs_put_page(psrc, 1);
1263 return PTR_ERR(pdst);
1265 f2fs_copy_page(psrc, pdst);
1266 set_page_dirty(pdst);
1267 f2fs_put_page(pdst, 1);
1268 f2fs_put_page(psrc, 1);
1270 ret = f2fs_truncate_hole(src_inode,
1271 src + i, src + i + 1);
1280 static int __exchange_data_block(struct inode *src_inode,
1281 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1282 pgoff_t len, bool full)
1284 block_t *src_blkaddr;
1290 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1292 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1293 array_size(olen, sizeof(block_t)),
1298 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1299 array_size(olen, sizeof(int)),
1302 kvfree(src_blkaddr);
1306 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1307 do_replace, src, olen);
1311 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1312 do_replace, src, dst, olen, full);
1320 kvfree(src_blkaddr);
1326 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1327 kvfree(src_blkaddr);
1332 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1334 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1335 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1336 pgoff_t start = offset >> PAGE_SHIFT;
1337 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1340 f2fs_balance_fs(sbi, true);
1342 /* avoid gc operation during block exchange */
1343 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1344 down_write(&F2FS_I(inode)->i_mmap_sem);
1347 f2fs_drop_extent_tree(inode);
1348 truncate_pagecache(inode, offset);
1349 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1350 f2fs_unlock_op(sbi);
1352 up_write(&F2FS_I(inode)->i_mmap_sem);
1353 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1357 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1362 if (offset + len >= i_size_read(inode))
1365 /* collapse range should be aligned to block size of f2fs. */
1366 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1369 ret = f2fs_convert_inline_inode(inode);
1373 /* write out all dirty pages from offset */
1374 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1378 ret = f2fs_do_collapse(inode, offset, len);
1382 /* write out all moved pages, if possible */
1383 down_write(&F2FS_I(inode)->i_mmap_sem);
1384 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1385 truncate_pagecache(inode, offset);
1387 new_size = i_size_read(inode) - len;
1388 ret = f2fs_truncate_blocks(inode, new_size, true);
1389 up_write(&F2FS_I(inode)->i_mmap_sem);
1391 f2fs_i_size_write(inode, new_size);
1395 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1398 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1399 pgoff_t index = start;
1400 unsigned int ofs_in_node = dn->ofs_in_node;
1404 for (; index < end; index++, dn->ofs_in_node++) {
1405 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1409 dn->ofs_in_node = ofs_in_node;
1410 ret = f2fs_reserve_new_blocks(dn, count);
1414 dn->ofs_in_node = ofs_in_node;
1415 for (index = start; index < end; index++, dn->ofs_in_node++) {
1416 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1418 * f2fs_reserve_new_blocks will not guarantee entire block
1421 if (dn->data_blkaddr == NULL_ADDR) {
1425 if (dn->data_blkaddr != NEW_ADDR) {
1426 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1427 dn->data_blkaddr = NEW_ADDR;
1428 f2fs_set_data_blkaddr(dn);
1432 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1437 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1440 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1441 struct address_space *mapping = inode->i_mapping;
1442 pgoff_t index, pg_start, pg_end;
1443 loff_t new_size = i_size_read(inode);
1444 loff_t off_start, off_end;
1447 ret = inode_newsize_ok(inode, (len + offset));
1451 ret = f2fs_convert_inline_inode(inode);
1455 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1459 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1460 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1462 off_start = offset & (PAGE_SIZE - 1);
1463 off_end = (offset + len) & (PAGE_SIZE - 1);
1465 if (pg_start == pg_end) {
1466 ret = fill_zero(inode, pg_start, off_start,
1467 off_end - off_start);
1471 new_size = max_t(loff_t, new_size, offset + len);
1474 ret = fill_zero(inode, pg_start++, off_start,
1475 PAGE_SIZE - off_start);
1479 new_size = max_t(loff_t, new_size,
1480 (loff_t)pg_start << PAGE_SHIFT);
1483 for (index = pg_start; index < pg_end;) {
1484 struct dnode_of_data dn;
1485 unsigned int end_offset;
1488 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1489 down_write(&F2FS_I(inode)->i_mmap_sem);
1491 truncate_pagecache_range(inode,
1492 (loff_t)index << PAGE_SHIFT,
1493 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1497 set_new_dnode(&dn, inode, NULL, NULL, 0);
1498 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1500 f2fs_unlock_op(sbi);
1501 up_write(&F2FS_I(inode)->i_mmap_sem);
1502 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1506 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1507 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1509 ret = f2fs_do_zero_range(&dn, index, end);
1510 f2fs_put_dnode(&dn);
1512 f2fs_unlock_op(sbi);
1513 up_write(&F2FS_I(inode)->i_mmap_sem);
1514 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1516 f2fs_balance_fs(sbi, dn.node_changed);
1522 new_size = max_t(loff_t, new_size,
1523 (loff_t)index << PAGE_SHIFT);
1527 ret = fill_zero(inode, pg_end, 0, off_end);
1531 new_size = max_t(loff_t, new_size, offset + len);
1536 if (new_size > i_size_read(inode)) {
1537 if (mode & FALLOC_FL_KEEP_SIZE)
1538 file_set_keep_isize(inode);
1540 f2fs_i_size_write(inode, new_size);
1545 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1547 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1548 pgoff_t nr, pg_start, pg_end, delta, idx;
1552 new_size = i_size_read(inode) + len;
1553 ret = inode_newsize_ok(inode, new_size);
1557 if (offset >= i_size_read(inode))
1560 /* insert range should be aligned to block size of f2fs. */
1561 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1564 ret = f2fs_convert_inline_inode(inode);
1568 f2fs_balance_fs(sbi, true);
1570 down_write(&F2FS_I(inode)->i_mmap_sem);
1571 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1572 up_write(&F2FS_I(inode)->i_mmap_sem);
1576 /* write out all dirty pages from offset */
1577 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1581 pg_start = offset >> PAGE_SHIFT;
1582 pg_end = (offset + len) >> PAGE_SHIFT;
1583 delta = pg_end - pg_start;
1584 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1586 /* avoid gc operation during block exchange */
1587 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1588 down_write(&F2FS_I(inode)->i_mmap_sem);
1589 truncate_pagecache(inode, offset);
1591 while (!ret && idx > pg_start) {
1592 nr = idx - pg_start;
1598 f2fs_drop_extent_tree(inode);
1600 ret = __exchange_data_block(inode, inode, idx,
1601 idx + delta, nr, false);
1602 f2fs_unlock_op(sbi);
1604 up_write(&F2FS_I(inode)->i_mmap_sem);
1605 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1607 /* write out all moved pages, if possible */
1608 down_write(&F2FS_I(inode)->i_mmap_sem);
1609 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1610 truncate_pagecache(inode, offset);
1611 up_write(&F2FS_I(inode)->i_mmap_sem);
1614 f2fs_i_size_write(inode, new_size);
1618 static int expand_inode_data(struct inode *inode, loff_t offset,
1619 loff_t len, int mode)
1621 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1622 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1623 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1624 .m_may_create = true };
1625 pgoff_t pg_start, pg_end;
1626 loff_t new_size = i_size_read(inode);
1628 block_t expanded = 0;
1631 err = inode_newsize_ok(inode, (len + offset));
1635 err = f2fs_convert_inline_inode(inode);
1639 f2fs_balance_fs(sbi, true);
1641 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1642 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1643 off_end = (offset + len) & (PAGE_SIZE - 1);
1645 map.m_lblk = pg_start;
1646 map.m_len = pg_end - pg_start;
1653 if (f2fs_is_pinned_file(inode)) {
1654 block_t sec_blks = BLKS_PER_SEC(sbi);
1655 block_t sec_len = roundup(map.m_len, sec_blks);
1657 map.m_len = sec_blks;
1659 if (has_not_enough_free_secs(sbi, 0,
1660 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1661 down_write(&sbi->gc_lock);
1662 err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
1663 if (err && err != -ENODATA && err != -EAGAIN)
1667 down_write(&sbi->pin_sem);
1670 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1671 f2fs_unlock_op(sbi);
1673 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1674 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1676 up_write(&sbi->pin_sem);
1678 expanded += map.m_len;
1679 sec_len -= map.m_len;
1680 map.m_lblk += map.m_len;
1681 if (!err && sec_len)
1684 map.m_len = expanded;
1686 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1687 expanded = map.m_len;
1696 last_off = pg_start + expanded - 1;
1698 /* update new size to the failed position */
1699 new_size = (last_off == pg_end) ? offset + len :
1700 (loff_t)(last_off + 1) << PAGE_SHIFT;
1702 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1705 if (new_size > i_size_read(inode)) {
1706 if (mode & FALLOC_FL_KEEP_SIZE)
1707 file_set_keep_isize(inode);
1709 f2fs_i_size_write(inode, new_size);
1715 static long f2fs_fallocate(struct file *file, int mode,
1716 loff_t offset, loff_t len)
1718 struct inode *inode = file_inode(file);
1721 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1723 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1725 if (!f2fs_is_compress_backend_ready(inode))
1728 /* f2fs only support ->fallocate for regular file */
1729 if (!S_ISREG(inode->i_mode))
1732 if (IS_ENCRYPTED(inode) &&
1733 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1736 if (f2fs_compressed_file(inode) &&
1737 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1738 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1741 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1742 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1743 FALLOC_FL_INSERT_RANGE))
1748 if (mode & FALLOC_FL_PUNCH_HOLE) {
1749 if (offset >= inode->i_size)
1752 ret = punch_hole(inode, offset, len);
1753 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1754 ret = f2fs_collapse_range(inode, offset, len);
1755 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1756 ret = f2fs_zero_range(inode, offset, len, mode);
1757 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1758 ret = f2fs_insert_range(inode, offset, len);
1760 ret = expand_inode_data(inode, offset, len, mode);
1764 inode->i_mtime = inode->i_ctime = current_time(inode);
1765 f2fs_mark_inode_dirty_sync(inode, false);
1766 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1770 inode_unlock(inode);
1772 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1776 static int f2fs_release_file(struct inode *inode, struct file *filp)
1779 * f2fs_relase_file is called at every close calls. So we should
1780 * not drop any inmemory pages by close called by other process.
1782 if (!(filp->f_mode & FMODE_WRITE) ||
1783 atomic_read(&inode->i_writecount) != 1)
1786 /* some remained atomic pages should discarded */
1787 if (f2fs_is_atomic_file(inode))
1788 f2fs_drop_inmem_pages(inode);
1789 if (f2fs_is_volatile_file(inode)) {
1790 set_inode_flag(inode, FI_DROP_CACHE);
1791 filemap_fdatawrite(inode->i_mapping);
1792 clear_inode_flag(inode, FI_DROP_CACHE);
1793 clear_inode_flag(inode, FI_VOLATILE_FILE);
1794 stat_dec_volatile_write(inode);
1799 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1801 struct inode *inode = file_inode(file);
1804 * If the process doing a transaction is crashed, we should do
1805 * roll-back. Otherwise, other reader/write can see corrupted database
1806 * until all the writers close its file. Since this should be done
1807 * before dropping file lock, it needs to do in ->flush.
1809 if (f2fs_is_atomic_file(inode) &&
1810 F2FS_I(inode)->inmem_task == current)
1811 f2fs_drop_inmem_pages(inode);
1815 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1817 struct f2fs_inode_info *fi = F2FS_I(inode);
1818 u32 masked_flags = fi->i_flags & mask;
1820 /* mask can be shrunk by flags_valid selector */
1823 /* Is it quota file? Do not allow user to mess with it */
1824 if (IS_NOQUOTA(inode))
1827 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1828 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1830 if (!f2fs_empty_dir(inode))
1834 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1835 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1837 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1841 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1842 if (masked_flags & F2FS_COMPR_FL) {
1843 if (!f2fs_disable_compressed_file(inode))
1846 if (iflags & F2FS_NOCOMP_FL)
1848 if (iflags & F2FS_COMPR_FL) {
1849 if (!f2fs_may_compress(inode))
1851 if (S_ISREG(inode->i_mode) && inode->i_size)
1854 set_compress_context(inode);
1857 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1858 if (masked_flags & F2FS_COMPR_FL)
1862 fi->i_flags = iflags | (fi->i_flags & ~mask);
1863 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1864 (fi->i_flags & F2FS_NOCOMP_FL));
1866 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1867 set_inode_flag(inode, FI_PROJ_INHERIT);
1869 clear_inode_flag(inode, FI_PROJ_INHERIT);
1871 inode->i_ctime = current_time(inode);
1872 f2fs_set_inode_flags(inode);
1873 f2fs_mark_inode_dirty_sync(inode, true);
1877 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1880 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1881 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1882 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1883 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1885 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1886 * FS_IOC_FSSETXATTR is done by the VFS.
1889 static const struct {
1892 } f2fs_fsflags_map[] = {
1893 { F2FS_COMPR_FL, FS_COMPR_FL },
1894 { F2FS_SYNC_FL, FS_SYNC_FL },
1895 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1896 { F2FS_APPEND_FL, FS_APPEND_FL },
1897 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1898 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1899 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1900 { F2FS_INDEX_FL, FS_INDEX_FL },
1901 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1902 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1903 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1906 #define F2FS_GETTABLE_FS_FL ( \
1916 FS_PROJINHERIT_FL | \
1918 FS_INLINE_DATA_FL | \
1923 #define F2FS_SETTABLE_FS_FL ( \
1932 FS_PROJINHERIT_FL | \
1935 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1936 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1941 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1942 if (iflags & f2fs_fsflags_map[i].iflag)
1943 fsflags |= f2fs_fsflags_map[i].fsflag;
1948 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1949 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1954 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1955 if (fsflags & f2fs_fsflags_map[i].fsflag)
1956 iflags |= f2fs_fsflags_map[i].iflag;
1961 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1963 struct inode *inode = file_inode(filp);
1965 return put_user(inode->i_generation, (int __user *)arg);
1968 static int f2fs_ioc_start_atomic_write(struct file *filp)
1970 struct inode *inode = file_inode(filp);
1971 struct f2fs_inode_info *fi = F2FS_I(inode);
1972 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1975 if (!inode_owner_or_capable(&init_user_ns, inode))
1978 if (!S_ISREG(inode->i_mode))
1981 if (filp->f_flags & O_DIRECT)
1984 ret = mnt_want_write_file(filp);
1990 f2fs_disable_compressed_file(inode);
1992 if (f2fs_is_atomic_file(inode)) {
1993 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1998 ret = f2fs_convert_inline_inode(inode);
2002 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2005 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2006 * f2fs_is_atomic_file.
2008 if (get_dirty_pages(inode))
2009 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2010 inode->i_ino, get_dirty_pages(inode));
2011 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2013 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2017 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2018 if (list_empty(&fi->inmem_ilist))
2019 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
2020 sbi->atomic_files++;
2021 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2023 /* add inode in inmem_list first and set atomic_file */
2024 set_inode_flag(inode, FI_ATOMIC_FILE);
2025 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2026 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2028 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2029 F2FS_I(inode)->inmem_task = current;
2030 stat_update_max_atomic_write(inode);
2032 inode_unlock(inode);
2033 mnt_drop_write_file(filp);
2037 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2039 struct inode *inode = file_inode(filp);
2042 if (!inode_owner_or_capable(&init_user_ns, inode))
2045 ret = mnt_want_write_file(filp);
2049 f2fs_balance_fs(F2FS_I_SB(inode), true);
2053 if (f2fs_is_volatile_file(inode)) {
2058 if (f2fs_is_atomic_file(inode)) {
2059 ret = f2fs_commit_inmem_pages(inode);
2063 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2065 f2fs_drop_inmem_pages(inode);
2067 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2070 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2071 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2074 inode_unlock(inode);
2075 mnt_drop_write_file(filp);
2079 static int f2fs_ioc_start_volatile_write(struct file *filp)
2081 struct inode *inode = file_inode(filp);
2084 if (!inode_owner_or_capable(&init_user_ns, inode))
2087 if (!S_ISREG(inode->i_mode))
2090 ret = mnt_want_write_file(filp);
2096 if (f2fs_is_volatile_file(inode))
2099 ret = f2fs_convert_inline_inode(inode);
2103 stat_inc_volatile_write(inode);
2104 stat_update_max_volatile_write(inode);
2106 set_inode_flag(inode, FI_VOLATILE_FILE);
2107 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2109 inode_unlock(inode);
2110 mnt_drop_write_file(filp);
2114 static int f2fs_ioc_release_volatile_write(struct file *filp)
2116 struct inode *inode = file_inode(filp);
2119 if (!inode_owner_or_capable(&init_user_ns, inode))
2122 ret = mnt_want_write_file(filp);
2128 if (!f2fs_is_volatile_file(inode))
2131 if (!f2fs_is_first_block_written(inode)) {
2132 ret = truncate_partial_data_page(inode, 0, true);
2136 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2138 inode_unlock(inode);
2139 mnt_drop_write_file(filp);
2143 static int f2fs_ioc_abort_volatile_write(struct file *filp)
2145 struct inode *inode = file_inode(filp);
2148 if (!inode_owner_or_capable(&init_user_ns, inode))
2151 ret = mnt_want_write_file(filp);
2157 if (f2fs_is_atomic_file(inode))
2158 f2fs_drop_inmem_pages(inode);
2159 if (f2fs_is_volatile_file(inode)) {
2160 clear_inode_flag(inode, FI_VOLATILE_FILE);
2161 stat_dec_volatile_write(inode);
2162 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2165 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2167 inode_unlock(inode);
2169 mnt_drop_write_file(filp);
2170 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2174 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2176 struct inode *inode = file_inode(filp);
2177 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2178 struct super_block *sb = sbi->sb;
2182 if (!capable(CAP_SYS_ADMIN))
2185 if (get_user(in, (__u32 __user *)arg))
2188 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2189 ret = mnt_want_write_file(filp);
2191 if (ret == -EROFS) {
2193 f2fs_stop_checkpoint(sbi, false);
2194 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2195 trace_f2fs_shutdown(sbi, in, ret);
2202 case F2FS_GOING_DOWN_FULLSYNC:
2203 ret = freeze_bdev(sb->s_bdev);
2206 f2fs_stop_checkpoint(sbi, false);
2207 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2208 thaw_bdev(sb->s_bdev);
2210 case F2FS_GOING_DOWN_METASYNC:
2211 /* do checkpoint only */
2212 ret = f2fs_sync_fs(sb, 1);
2215 f2fs_stop_checkpoint(sbi, false);
2216 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2218 case F2FS_GOING_DOWN_NOSYNC:
2219 f2fs_stop_checkpoint(sbi, false);
2220 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2222 case F2FS_GOING_DOWN_METAFLUSH:
2223 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2224 f2fs_stop_checkpoint(sbi, false);
2225 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2227 case F2FS_GOING_DOWN_NEED_FSCK:
2228 set_sbi_flag(sbi, SBI_NEED_FSCK);
2229 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2230 set_sbi_flag(sbi, SBI_IS_DIRTY);
2231 /* do checkpoint only */
2232 ret = f2fs_sync_fs(sb, 1);
2239 f2fs_stop_gc_thread(sbi);
2240 f2fs_stop_discard_thread(sbi);
2242 f2fs_drop_discard_cmd(sbi);
2243 clear_opt(sbi, DISCARD);
2245 f2fs_update_time(sbi, REQ_TIME);
2247 if (in != F2FS_GOING_DOWN_FULLSYNC)
2248 mnt_drop_write_file(filp);
2250 trace_f2fs_shutdown(sbi, in, ret);
2255 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2257 struct inode *inode = file_inode(filp);
2258 struct super_block *sb = inode->i_sb;
2259 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2260 struct fstrim_range range;
2263 if (!capable(CAP_SYS_ADMIN))
2266 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2269 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2273 ret = mnt_want_write_file(filp);
2277 range.minlen = max((unsigned int)range.minlen,
2278 q->limits.discard_granularity);
2279 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2280 mnt_drop_write_file(filp);
2284 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2287 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2291 static bool uuid_is_nonzero(__u8 u[16])
2295 for (i = 0; i < 16; i++)
2301 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2303 struct inode *inode = file_inode(filp);
2305 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2308 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2310 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2313 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2315 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2317 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2320 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2322 struct inode *inode = file_inode(filp);
2323 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2326 if (!f2fs_sb_has_encrypt(sbi))
2329 err = mnt_want_write_file(filp);
2333 down_write(&sbi->sb_lock);
2335 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2338 /* update superblock with uuid */
2339 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2341 err = f2fs_commit_super(sbi, false);
2344 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2348 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2352 up_write(&sbi->sb_lock);
2353 mnt_drop_write_file(filp);
2357 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2360 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2363 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2366 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2368 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2371 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2374 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2376 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2379 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2382 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2385 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2388 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2391 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2394 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2397 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2400 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2402 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2405 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2408 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2410 struct inode *inode = file_inode(filp);
2411 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2415 if (!capable(CAP_SYS_ADMIN))
2418 if (get_user(sync, (__u32 __user *)arg))
2421 if (f2fs_readonly(sbi->sb))
2424 ret = mnt_want_write_file(filp);
2429 if (!down_write_trylock(&sbi->gc_lock)) {
2434 down_write(&sbi->gc_lock);
2437 ret = f2fs_gc(sbi, sync, true, false, NULL_SEGNO);
2439 mnt_drop_write_file(filp);
2443 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2445 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2449 if (!capable(CAP_SYS_ADMIN))
2451 if (f2fs_readonly(sbi->sb))
2454 end = range->start + range->len;
2455 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2456 end >= MAX_BLKADDR(sbi))
2459 ret = mnt_want_write_file(filp);
2465 if (!down_write_trylock(&sbi->gc_lock)) {
2470 down_write(&sbi->gc_lock);
2473 ret = f2fs_gc(sbi, range->sync, true, false,
2474 GET_SEGNO(sbi, range->start));
2480 range->start += BLKS_PER_SEC(sbi);
2481 if (range->start <= end)
2484 mnt_drop_write_file(filp);
2488 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2490 struct f2fs_gc_range range;
2492 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2495 return __f2fs_ioc_gc_range(filp, &range);
2498 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2500 struct inode *inode = file_inode(filp);
2501 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2504 if (!capable(CAP_SYS_ADMIN))
2507 if (f2fs_readonly(sbi->sb))
2510 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2511 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2515 ret = mnt_want_write_file(filp);
2519 ret = f2fs_sync_fs(sbi->sb, 1);
2521 mnt_drop_write_file(filp);
2525 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2527 struct f2fs_defragment *range)
2529 struct inode *inode = file_inode(filp);
2530 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2531 .m_seg_type = NO_CHECK_TYPE,
2532 .m_may_create = false };
2533 struct extent_info ei = {0, 0, 0};
2534 pgoff_t pg_start, pg_end, next_pgofs;
2535 unsigned int blk_per_seg = sbi->blocks_per_seg;
2536 unsigned int total = 0, sec_num;
2537 block_t blk_end = 0;
2538 bool fragmented = false;
2541 /* if in-place-update policy is enabled, don't waste time here */
2542 if (f2fs_should_update_inplace(inode, NULL))
2545 pg_start = range->start >> PAGE_SHIFT;
2546 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2548 f2fs_balance_fs(sbi, true);
2552 /* writeback all dirty pages in the range */
2553 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2554 range->start + range->len - 1);
2559 * lookup mapping info in extent cache, skip defragmenting if physical
2560 * block addresses are continuous.
2562 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2563 if (ei.fofs + ei.len >= pg_end)
2567 map.m_lblk = pg_start;
2568 map.m_next_pgofs = &next_pgofs;
2571 * lookup mapping info in dnode page cache, skip defragmenting if all
2572 * physical block addresses are continuous even if there are hole(s)
2573 * in logical blocks.
2575 while (map.m_lblk < pg_end) {
2576 map.m_len = pg_end - map.m_lblk;
2577 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2581 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2582 map.m_lblk = next_pgofs;
2586 if (blk_end && blk_end != map.m_pblk)
2589 /* record total count of block that we're going to move */
2592 blk_end = map.m_pblk + map.m_len;
2594 map.m_lblk += map.m_len;
2602 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
2605 * make sure there are enough free section for LFS allocation, this can
2606 * avoid defragment running in SSR mode when free section are allocated
2609 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2614 map.m_lblk = pg_start;
2615 map.m_len = pg_end - pg_start;
2618 while (map.m_lblk < pg_end) {
2623 map.m_len = pg_end - map.m_lblk;
2624 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2628 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2629 map.m_lblk = next_pgofs;
2633 set_inode_flag(inode, FI_DO_DEFRAG);
2636 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2639 page = f2fs_get_lock_data_page(inode, idx, true);
2641 err = PTR_ERR(page);
2645 set_page_dirty(page);
2646 f2fs_put_page(page, 1);
2655 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2658 clear_inode_flag(inode, FI_DO_DEFRAG);
2660 err = filemap_fdatawrite(inode->i_mapping);
2665 clear_inode_flag(inode, FI_DO_DEFRAG);
2667 inode_unlock(inode);
2669 range->len = (u64)total << PAGE_SHIFT;
2673 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2675 struct inode *inode = file_inode(filp);
2676 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2677 struct f2fs_defragment range;
2680 if (!capable(CAP_SYS_ADMIN))
2683 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2686 if (f2fs_readonly(sbi->sb))
2689 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2693 /* verify alignment of offset & size */
2694 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2697 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2698 max_file_blocks(inode)))
2701 err = mnt_want_write_file(filp);
2705 err = f2fs_defragment_range(sbi, filp, &range);
2706 mnt_drop_write_file(filp);
2708 f2fs_update_time(sbi, REQ_TIME);
2712 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2719 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2720 struct file *file_out, loff_t pos_out, size_t len)
2722 struct inode *src = file_inode(file_in);
2723 struct inode *dst = file_inode(file_out);
2724 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2725 size_t olen = len, dst_max_i_size = 0;
2729 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2730 src->i_sb != dst->i_sb)
2733 if (unlikely(f2fs_readonly(src->i_sb)))
2736 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2739 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2742 if (pos_out < 0 || pos_in < 0)
2746 if (pos_in == pos_out)
2748 if (pos_out > pos_in && pos_out < pos_in + len)
2755 if (!inode_trylock(dst))
2760 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2763 olen = len = src->i_size - pos_in;
2764 if (pos_in + len == src->i_size)
2765 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2771 dst_osize = dst->i_size;
2772 if (pos_out + olen > dst->i_size)
2773 dst_max_i_size = pos_out + olen;
2775 /* verify the end result is block aligned */
2776 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2777 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2778 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2781 ret = f2fs_convert_inline_inode(src);
2785 ret = f2fs_convert_inline_inode(dst);
2789 /* write out all dirty pages from offset */
2790 ret = filemap_write_and_wait_range(src->i_mapping,
2791 pos_in, pos_in + len);
2795 ret = filemap_write_and_wait_range(dst->i_mapping,
2796 pos_out, pos_out + len);
2800 f2fs_balance_fs(sbi, true);
2802 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2805 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2810 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2811 pos_out >> F2FS_BLKSIZE_BITS,
2812 len >> F2FS_BLKSIZE_BITS, false);
2816 f2fs_i_size_write(dst, dst_max_i_size);
2817 else if (dst_osize != dst->i_size)
2818 f2fs_i_size_write(dst, dst_osize);
2820 f2fs_unlock_op(sbi);
2823 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2825 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2834 static int __f2fs_ioc_move_range(struct file *filp,
2835 struct f2fs_move_range *range)
2840 if (!(filp->f_mode & FMODE_READ) ||
2841 !(filp->f_mode & FMODE_WRITE))
2844 dst = fdget(range->dst_fd);
2848 if (!(dst.file->f_mode & FMODE_WRITE)) {
2853 err = mnt_want_write_file(filp);
2857 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2858 range->pos_out, range->len);
2860 mnt_drop_write_file(filp);
2866 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2868 struct f2fs_move_range range;
2870 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2873 return __f2fs_ioc_move_range(filp, &range);
2876 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2878 struct inode *inode = file_inode(filp);
2879 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2880 struct sit_info *sm = SIT_I(sbi);
2881 unsigned int start_segno = 0, end_segno = 0;
2882 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2883 struct f2fs_flush_device range;
2886 if (!capable(CAP_SYS_ADMIN))
2889 if (f2fs_readonly(sbi->sb))
2892 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2895 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2899 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2900 __is_large_section(sbi)) {
2901 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2902 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2906 ret = mnt_want_write_file(filp);
2910 if (range.dev_num != 0)
2911 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2912 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2914 start_segno = sm->last_victim[FLUSH_DEVICE];
2915 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2916 start_segno = dev_start_segno;
2917 end_segno = min(start_segno + range.segments, dev_end_segno);
2919 while (start_segno < end_segno) {
2920 if (!down_write_trylock(&sbi->gc_lock)) {
2924 sm->last_victim[GC_CB] = end_segno + 1;
2925 sm->last_victim[GC_GREEDY] = end_segno + 1;
2926 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2927 ret = f2fs_gc(sbi, true, true, true, start_segno);
2935 mnt_drop_write_file(filp);
2939 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2941 struct inode *inode = file_inode(filp);
2942 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2944 /* Must validate to set it with SQLite behavior in Android. */
2945 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2947 return put_user(sb_feature, (u32 __user *)arg);
2951 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2953 struct dquot *transfer_to[MAXQUOTAS] = {};
2954 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2955 struct super_block *sb = sbi->sb;
2958 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2959 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2960 err = __dquot_transfer(inode, transfer_to);
2962 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2963 dqput(transfer_to[PRJQUOTA]);
2968 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
2970 struct f2fs_inode_info *fi = F2FS_I(inode);
2971 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2976 if (!f2fs_sb_has_project_quota(sbi)) {
2977 if (projid != F2FS_DEF_PROJID)
2983 if (!f2fs_has_extra_attr(inode))
2986 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2988 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2992 /* Is it quota file? Do not allow user to mess with it */
2993 if (IS_NOQUOTA(inode))
2996 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2998 return PTR_ERR(ipage);
3000 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3003 f2fs_put_page(ipage, 1);
3006 f2fs_put_page(ipage, 1);
3008 err = dquot_initialize(inode);
3013 err = f2fs_transfer_project_quota(inode, kprojid);
3017 F2FS_I(inode)->i_projid = kprojid;
3018 inode->i_ctime = current_time(inode);
3019 f2fs_mark_inode_dirty_sync(inode, true);
3021 f2fs_unlock_op(sbi);
3025 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3030 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3032 if (projid != F2FS_DEF_PROJID)
3038 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3040 struct inode *inode = d_inode(dentry);
3041 struct f2fs_inode_info *fi = F2FS_I(inode);
3042 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3044 if (IS_ENCRYPTED(inode))
3045 fsflags |= FS_ENCRYPT_FL;
3046 if (IS_VERITY(inode))
3047 fsflags |= FS_VERITY_FL;
3048 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3049 fsflags |= FS_INLINE_DATA_FL;
3050 if (is_inode_flag_set(inode, FI_PIN_FILE))
3051 fsflags |= FS_NOCOW_FL;
3053 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3055 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3056 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3061 int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3062 struct dentry *dentry, struct fileattr *fa)
3064 struct inode *inode = d_inode(dentry);
3065 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3069 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3071 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3073 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3075 fsflags &= F2FS_SETTABLE_FS_FL;
3076 if (!fa->flags_valid)
3077 mask &= FS_COMMON_FL;
3079 iflags = f2fs_fsflags_to_iflags(fsflags);
3080 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3083 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3085 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3090 int f2fs_pin_file_control(struct inode *inode, bool inc)
3092 struct f2fs_inode_info *fi = F2FS_I(inode);
3093 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3095 /* Use i_gc_failures for normal file as a risk signal. */
3097 f2fs_i_gc_failures_write(inode,
3098 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3100 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3101 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3102 __func__, inode->i_ino,
3103 fi->i_gc_failures[GC_FAILURE_PIN]);
3104 clear_inode_flag(inode, FI_PIN_FILE);
3110 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3112 struct inode *inode = file_inode(filp);
3116 if (get_user(pin, (__u32 __user *)arg))
3119 if (!S_ISREG(inode->i_mode))
3122 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3125 ret = mnt_want_write_file(filp);
3131 if (f2fs_should_update_outplace(inode, NULL)) {
3137 clear_inode_flag(inode, FI_PIN_FILE);
3138 f2fs_i_gc_failures_write(inode, 0);
3142 if (f2fs_pin_file_control(inode, false)) {
3147 ret = f2fs_convert_inline_inode(inode);
3151 if (!f2fs_disable_compressed_file(inode)) {
3156 set_inode_flag(inode, FI_PIN_FILE);
3157 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3159 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3161 inode_unlock(inode);
3162 mnt_drop_write_file(filp);
3166 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3168 struct inode *inode = file_inode(filp);
3171 if (is_inode_flag_set(inode, FI_PIN_FILE))
3172 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3173 return put_user(pin, (u32 __user *)arg);
3176 int f2fs_precache_extents(struct inode *inode)
3178 struct f2fs_inode_info *fi = F2FS_I(inode);
3179 struct f2fs_map_blocks map;
3180 pgoff_t m_next_extent;
3184 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3188 map.m_next_pgofs = NULL;
3189 map.m_next_extent = &m_next_extent;
3190 map.m_seg_type = NO_CHECK_TYPE;
3191 map.m_may_create = false;
3192 end = max_file_blocks(inode);
3194 while (map.m_lblk < end) {
3195 map.m_len = end - map.m_lblk;
3197 down_write(&fi->i_gc_rwsem[WRITE]);
3198 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3199 up_write(&fi->i_gc_rwsem[WRITE]);
3203 map.m_lblk = m_next_extent;
3209 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3211 return f2fs_precache_extents(file_inode(filp));
3214 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3216 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3219 if (!capable(CAP_SYS_ADMIN))
3222 if (f2fs_readonly(sbi->sb))
3225 if (copy_from_user(&block_count, (void __user *)arg,
3226 sizeof(block_count)))
3229 return f2fs_resize_fs(sbi, block_count);
3232 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3234 struct inode *inode = file_inode(filp);
3236 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3238 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3239 f2fs_warn(F2FS_I_SB(inode),
3240 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3245 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3248 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3250 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3253 return fsverity_ioctl_measure(filp, (void __user *)arg);
3256 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3258 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3261 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3264 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3266 struct inode *inode = file_inode(filp);
3267 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3272 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3276 down_read(&sbi->sb_lock);
3277 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3278 ARRAY_SIZE(sbi->raw_super->volume_name),
3279 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3280 up_read(&sbi->sb_lock);
3282 if (copy_to_user((char __user *)arg, vbuf,
3283 min(FSLABEL_MAX, count)))
3290 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3292 struct inode *inode = file_inode(filp);
3293 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3297 if (!capable(CAP_SYS_ADMIN))
3300 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3302 return PTR_ERR(vbuf);
3304 err = mnt_want_write_file(filp);
3308 down_write(&sbi->sb_lock);
3310 memset(sbi->raw_super->volume_name, 0,
3311 sizeof(sbi->raw_super->volume_name));
3312 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3313 sbi->raw_super->volume_name,
3314 ARRAY_SIZE(sbi->raw_super->volume_name));
3316 err = f2fs_commit_super(sbi, false);
3318 up_write(&sbi->sb_lock);
3320 mnt_drop_write_file(filp);
3326 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3328 struct inode *inode = file_inode(filp);
3331 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3334 if (!f2fs_compressed_file(inode))
3337 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3338 return put_user(blocks, (u64 __user *)arg);
3341 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3343 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3344 unsigned int released_blocks = 0;
3345 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3349 for (i = 0; i < count; i++) {
3350 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3351 dn->ofs_in_node + i);
3353 if (!__is_valid_data_blkaddr(blkaddr))
3355 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3356 DATA_GENERIC_ENHANCE)))
3357 return -EFSCORRUPTED;
3361 int compr_blocks = 0;
3363 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3364 blkaddr = f2fs_data_blkaddr(dn);
3367 if (blkaddr == COMPRESS_ADDR)
3369 dn->ofs_in_node += cluster_size;
3373 if (__is_valid_data_blkaddr(blkaddr))
3376 if (blkaddr != NEW_ADDR)
3379 dn->data_blkaddr = NULL_ADDR;
3380 f2fs_set_data_blkaddr(dn);
3383 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3384 dec_valid_block_count(sbi, dn->inode,
3385 cluster_size - compr_blocks);
3387 released_blocks += cluster_size - compr_blocks;
3389 count -= cluster_size;
3392 return released_blocks;
3395 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3397 struct inode *inode = file_inode(filp);
3398 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3399 pgoff_t page_idx = 0, last_idx;
3400 unsigned int released_blocks = 0;
3404 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3407 if (!f2fs_compressed_file(inode))
3410 if (f2fs_readonly(sbi->sb))
3413 ret = mnt_want_write_file(filp);
3417 f2fs_balance_fs(F2FS_I_SB(inode), true);
3421 writecount = atomic_read(&inode->i_writecount);
3422 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3423 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3428 if (IS_IMMUTABLE(inode)) {
3433 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3437 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3438 f2fs_set_inode_flags(inode);
3439 inode->i_ctime = current_time(inode);
3440 f2fs_mark_inode_dirty_sync(inode, true);
3442 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3445 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3446 down_write(&F2FS_I(inode)->i_mmap_sem);
3448 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3450 while (page_idx < last_idx) {
3451 struct dnode_of_data dn;
3452 pgoff_t end_offset, count;
3454 set_new_dnode(&dn, inode, NULL, NULL, 0);
3455 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3457 if (ret == -ENOENT) {
3458 page_idx = f2fs_get_next_page_offset(&dn,
3466 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3467 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3468 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3470 ret = release_compress_blocks(&dn, count);
3472 f2fs_put_dnode(&dn);
3478 released_blocks += ret;
3481 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3482 up_write(&F2FS_I(inode)->i_mmap_sem);
3484 inode_unlock(inode);
3486 mnt_drop_write_file(filp);
3489 ret = put_user(released_blocks, (u64 __user *)arg);
3490 } else if (released_blocks &&
3491 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3492 set_sbi_flag(sbi, SBI_NEED_FSCK);
3493 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3494 "iblocks=%llu, released=%u, compr_blocks=%u, "
3496 __func__, inode->i_ino, inode->i_blocks,
3498 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3504 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3506 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3507 unsigned int reserved_blocks = 0;
3508 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3512 for (i = 0; i < count; i++) {
3513 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3514 dn->ofs_in_node + i);
3516 if (!__is_valid_data_blkaddr(blkaddr))
3518 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3519 DATA_GENERIC_ENHANCE)))
3520 return -EFSCORRUPTED;
3524 int compr_blocks = 0;
3528 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3529 blkaddr = f2fs_data_blkaddr(dn);
3532 if (blkaddr == COMPRESS_ADDR)
3534 dn->ofs_in_node += cluster_size;
3538 if (__is_valid_data_blkaddr(blkaddr)) {
3543 dn->data_blkaddr = NEW_ADDR;
3544 f2fs_set_data_blkaddr(dn);
3547 reserved = cluster_size - compr_blocks;
3548 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3552 if (reserved != cluster_size - compr_blocks)
3555 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3557 reserved_blocks += reserved;
3559 count -= cluster_size;
3562 return reserved_blocks;
3565 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3567 struct inode *inode = file_inode(filp);
3568 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3569 pgoff_t page_idx = 0, last_idx;
3570 unsigned int reserved_blocks = 0;
3573 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3576 if (!f2fs_compressed_file(inode))
3579 if (f2fs_readonly(sbi->sb))
3582 ret = mnt_want_write_file(filp);
3586 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3589 f2fs_balance_fs(F2FS_I_SB(inode), true);
3593 if (!IS_IMMUTABLE(inode)) {
3598 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3599 down_write(&F2FS_I(inode)->i_mmap_sem);
3601 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3603 while (page_idx < last_idx) {
3604 struct dnode_of_data dn;
3605 pgoff_t end_offset, count;
3607 set_new_dnode(&dn, inode, NULL, NULL, 0);
3608 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3610 if (ret == -ENOENT) {
3611 page_idx = f2fs_get_next_page_offset(&dn,
3619 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3620 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3621 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3623 ret = reserve_compress_blocks(&dn, count);
3625 f2fs_put_dnode(&dn);
3631 reserved_blocks += ret;
3634 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3635 up_write(&F2FS_I(inode)->i_mmap_sem);
3638 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3639 f2fs_set_inode_flags(inode);
3640 inode->i_ctime = current_time(inode);
3641 f2fs_mark_inode_dirty_sync(inode, true);
3644 inode_unlock(inode);
3646 mnt_drop_write_file(filp);
3649 ret = put_user(reserved_blocks, (u64 __user *)arg);
3650 } else if (reserved_blocks &&
3651 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3652 set_sbi_flag(sbi, SBI_NEED_FSCK);
3653 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3654 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3656 __func__, inode->i_ino, inode->i_blocks,
3658 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3664 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3665 pgoff_t off, block_t block, block_t len, u32 flags)
3667 struct request_queue *q = bdev_get_queue(bdev);
3668 sector_t sector = SECTOR_FROM_BLOCK(block);
3669 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3675 if (flags & F2FS_TRIM_FILE_DISCARD)
3676 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3677 blk_queue_secure_erase(q) ?
3678 BLKDEV_DISCARD_SECURE : 0);
3680 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3681 if (IS_ENCRYPTED(inode))
3682 ret = fscrypt_zeroout_range(inode, off, block, len);
3684 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3691 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3693 struct inode *inode = file_inode(filp);
3694 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3695 struct address_space *mapping = inode->i_mapping;
3696 struct block_device *prev_bdev = NULL;
3697 struct f2fs_sectrim_range range;
3698 pgoff_t index, pg_end, prev_index = 0;
3699 block_t prev_block = 0, len = 0;
3701 bool to_end = false;
3704 if (!(filp->f_mode & FMODE_WRITE))
3707 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3711 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3712 !S_ISREG(inode->i_mode))
3715 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3716 !f2fs_hw_support_discard(sbi)) ||
3717 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3718 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3721 file_start_write(filp);
3724 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3725 range.start >= inode->i_size) {
3733 if (inode->i_size - range.start > range.len) {
3734 end_addr = range.start + range.len;
3736 end_addr = range.len == (u64)-1 ?
3737 sbi->sb->s_maxbytes : inode->i_size;
3741 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3742 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3747 index = F2FS_BYTES_TO_BLK(range.start);
3748 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3750 ret = f2fs_convert_inline_inode(inode);
3754 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3755 down_write(&F2FS_I(inode)->i_mmap_sem);
3757 ret = filemap_write_and_wait_range(mapping, range.start,
3758 to_end ? LLONG_MAX : end_addr - 1);
3762 truncate_inode_pages_range(mapping, range.start,
3763 to_end ? -1 : end_addr - 1);
3765 while (index < pg_end) {
3766 struct dnode_of_data dn;
3767 pgoff_t end_offset, count;
3770 set_new_dnode(&dn, inode, NULL, NULL, 0);
3771 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3773 if (ret == -ENOENT) {
3774 index = f2fs_get_next_page_offset(&dn, index);
3780 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3781 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3782 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3783 struct block_device *cur_bdev;
3784 block_t blkaddr = f2fs_data_blkaddr(&dn);
3786 if (!__is_valid_data_blkaddr(blkaddr))
3789 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3790 DATA_GENERIC_ENHANCE)) {
3791 ret = -EFSCORRUPTED;
3792 f2fs_put_dnode(&dn);
3796 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3797 if (f2fs_is_multi_device(sbi)) {
3798 int di = f2fs_target_device_index(sbi, blkaddr);
3800 blkaddr -= FDEV(di).start_blk;
3804 if (prev_bdev == cur_bdev &&
3805 index == prev_index + len &&
3806 blkaddr == prev_block + len) {
3809 ret = f2fs_secure_erase(prev_bdev,
3810 inode, prev_index, prev_block,
3813 f2fs_put_dnode(&dn);
3822 prev_bdev = cur_bdev;
3824 prev_block = blkaddr;
3829 f2fs_put_dnode(&dn);
3831 if (fatal_signal_pending(current)) {
3839 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3840 prev_block, len, range.flags);
3842 up_write(&F2FS_I(inode)->i_mmap_sem);
3843 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3845 inode_unlock(inode);
3846 file_end_write(filp);
3851 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3853 struct inode *inode = file_inode(filp);
3854 struct f2fs_comp_option option;
3856 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3859 inode_lock_shared(inode);
3861 if (!f2fs_compressed_file(inode)) {
3862 inode_unlock_shared(inode);
3866 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3867 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3869 inode_unlock_shared(inode);
3871 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3878 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3880 struct inode *inode = file_inode(filp);
3881 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3882 struct f2fs_comp_option option;
3885 if (!f2fs_sb_has_compression(sbi))
3888 if (!(filp->f_mode & FMODE_WRITE))
3891 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3895 if (!f2fs_compressed_file(inode) ||
3896 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3897 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3898 option.algorithm >= COMPRESS_MAX)
3901 file_start_write(filp);
3904 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3909 if (inode->i_size != 0) {
3914 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3915 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3916 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
3917 f2fs_mark_inode_dirty_sync(inode, true);
3919 if (!f2fs_is_compress_backend_ready(inode))
3920 f2fs_warn(sbi, "compression algorithm is successfully set, "
3921 "but current kernel doesn't support this algorithm.");
3923 inode_unlock(inode);
3924 file_end_write(filp);
3929 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
3931 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
3932 struct address_space *mapping = inode->i_mapping;
3934 pgoff_t redirty_idx = page_idx;
3935 int i, page_len = 0, ret = 0;
3937 page_cache_ra_unbounded(&ractl, len, 0);
3939 for (i = 0; i < len; i++, page_idx++) {
3940 page = read_cache_page(mapping, page_idx, NULL, NULL);
3942 ret = PTR_ERR(page);
3948 for (i = 0; i < page_len; i++, redirty_idx++) {
3949 page = find_lock_page(mapping, redirty_idx);
3954 set_page_dirty(page);
3955 f2fs_put_page(page, 1);
3956 f2fs_put_page(page, 0);
3962 static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
3964 struct inode *inode = file_inode(filp);
3965 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3966 struct f2fs_inode_info *fi = F2FS_I(inode);
3967 pgoff_t page_idx = 0, last_idx;
3968 unsigned int blk_per_seg = sbi->blocks_per_seg;
3969 int cluster_size = F2FS_I(inode)->i_cluster_size;
3972 if (!f2fs_sb_has_compression(sbi) ||
3973 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
3976 if (!(filp->f_mode & FMODE_WRITE))
3979 if (!f2fs_compressed_file(inode))
3982 f2fs_balance_fs(F2FS_I_SB(inode), true);
3984 file_start_write(filp);
3987 if (!f2fs_is_compress_backend_ready(inode)) {
3992 if (f2fs_is_mmap_file(inode)) {
3997 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4001 if (!atomic_read(&fi->i_compr_blocks))
4004 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4006 count = last_idx - page_idx;
4008 int len = min(cluster_size, count);
4010 ret = redirty_blocks(inode, page_idx, len);
4014 if (get_dirty_pages(inode) >= blk_per_seg)
4015 filemap_fdatawrite(inode->i_mapping);
4022 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4026 f2fs_warn(sbi, "%s: The file might be partially decompressed "
4027 "(errno=%d). Please delete the file.\n",
4030 inode_unlock(inode);
4031 file_end_write(filp);
4036 static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4038 struct inode *inode = file_inode(filp);
4039 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4040 pgoff_t page_idx = 0, last_idx;
4041 unsigned int blk_per_seg = sbi->blocks_per_seg;
4042 int cluster_size = F2FS_I(inode)->i_cluster_size;
4045 if (!f2fs_sb_has_compression(sbi) ||
4046 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4049 if (!(filp->f_mode & FMODE_WRITE))
4052 if (!f2fs_compressed_file(inode))
4055 f2fs_balance_fs(F2FS_I_SB(inode), true);
4057 file_start_write(filp);
4060 if (!f2fs_is_compress_backend_ready(inode)) {
4065 if (f2fs_is_mmap_file(inode)) {
4070 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4074 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4076 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4078 count = last_idx - page_idx;
4080 int len = min(cluster_size, count);
4082 ret = redirty_blocks(inode, page_idx, len);
4086 if (get_dirty_pages(inode) >= blk_per_seg)
4087 filemap_fdatawrite(inode->i_mapping);
4094 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4097 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4100 f2fs_warn(sbi, "%s: The file might be partially compressed "
4101 "(errno=%d). Please delete the file.\n",
4104 inode_unlock(inode);
4105 file_end_write(filp);
4110 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4113 case FS_IOC_GETVERSION:
4114 return f2fs_ioc_getversion(filp, arg);
4115 case F2FS_IOC_START_ATOMIC_WRITE:
4116 return f2fs_ioc_start_atomic_write(filp);
4117 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4118 return f2fs_ioc_commit_atomic_write(filp);
4119 case F2FS_IOC_START_VOLATILE_WRITE:
4120 return f2fs_ioc_start_volatile_write(filp);
4121 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4122 return f2fs_ioc_release_volatile_write(filp);
4123 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4124 return f2fs_ioc_abort_volatile_write(filp);
4125 case F2FS_IOC_SHUTDOWN:
4126 return f2fs_ioc_shutdown(filp, arg);
4128 return f2fs_ioc_fitrim(filp, arg);
4129 case FS_IOC_SET_ENCRYPTION_POLICY:
4130 return f2fs_ioc_set_encryption_policy(filp, arg);
4131 case FS_IOC_GET_ENCRYPTION_POLICY:
4132 return f2fs_ioc_get_encryption_policy(filp, arg);
4133 case FS_IOC_GET_ENCRYPTION_PWSALT:
4134 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4135 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4136 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4137 case FS_IOC_ADD_ENCRYPTION_KEY:
4138 return f2fs_ioc_add_encryption_key(filp, arg);
4139 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4140 return f2fs_ioc_remove_encryption_key(filp, arg);
4141 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4142 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4143 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4144 return f2fs_ioc_get_encryption_key_status(filp, arg);
4145 case FS_IOC_GET_ENCRYPTION_NONCE:
4146 return f2fs_ioc_get_encryption_nonce(filp, arg);
4147 case F2FS_IOC_GARBAGE_COLLECT:
4148 return f2fs_ioc_gc(filp, arg);
4149 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4150 return f2fs_ioc_gc_range(filp, arg);
4151 case F2FS_IOC_WRITE_CHECKPOINT:
4152 return f2fs_ioc_write_checkpoint(filp, arg);
4153 case F2FS_IOC_DEFRAGMENT:
4154 return f2fs_ioc_defragment(filp, arg);
4155 case F2FS_IOC_MOVE_RANGE:
4156 return f2fs_ioc_move_range(filp, arg);
4157 case F2FS_IOC_FLUSH_DEVICE:
4158 return f2fs_ioc_flush_device(filp, arg);
4159 case F2FS_IOC_GET_FEATURES:
4160 return f2fs_ioc_get_features(filp, arg);
4161 case F2FS_IOC_GET_PIN_FILE:
4162 return f2fs_ioc_get_pin_file(filp, arg);
4163 case F2FS_IOC_SET_PIN_FILE:
4164 return f2fs_ioc_set_pin_file(filp, arg);
4165 case F2FS_IOC_PRECACHE_EXTENTS:
4166 return f2fs_ioc_precache_extents(filp, arg);
4167 case F2FS_IOC_RESIZE_FS:
4168 return f2fs_ioc_resize_fs(filp, arg);
4169 case FS_IOC_ENABLE_VERITY:
4170 return f2fs_ioc_enable_verity(filp, arg);
4171 case FS_IOC_MEASURE_VERITY:
4172 return f2fs_ioc_measure_verity(filp, arg);
4173 case FS_IOC_READ_VERITY_METADATA:
4174 return f2fs_ioc_read_verity_metadata(filp, arg);
4175 case FS_IOC_GETFSLABEL:
4176 return f2fs_ioc_getfslabel(filp, arg);
4177 case FS_IOC_SETFSLABEL:
4178 return f2fs_ioc_setfslabel(filp, arg);
4179 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4180 return f2fs_get_compress_blocks(filp, arg);
4181 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4182 return f2fs_release_compress_blocks(filp, arg);
4183 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4184 return f2fs_reserve_compress_blocks(filp, arg);
4185 case F2FS_IOC_SEC_TRIM_FILE:
4186 return f2fs_sec_trim_file(filp, arg);
4187 case F2FS_IOC_GET_COMPRESS_OPTION:
4188 return f2fs_ioc_get_compress_option(filp, arg);
4189 case F2FS_IOC_SET_COMPRESS_OPTION:
4190 return f2fs_ioc_set_compress_option(filp, arg);
4191 case F2FS_IOC_DECOMPRESS_FILE:
4192 return f2fs_ioc_decompress_file(filp, arg);
4193 case F2FS_IOC_COMPRESS_FILE:
4194 return f2fs_ioc_compress_file(filp, arg);
4200 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4202 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4204 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4207 return __f2fs_ioctl(filp, cmd, arg);
4210 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4212 struct file *file = iocb->ki_filp;
4213 struct inode *inode = file_inode(file);
4216 if (!f2fs_is_compress_backend_ready(inode))
4219 ret = generic_file_read_iter(iocb, iter);
4222 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4227 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4229 struct file *file = iocb->ki_filp;
4230 struct inode *inode = file_inode(file);
4233 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4238 if (!f2fs_is_compress_backend_ready(inode)) {
4243 if (iocb->ki_flags & IOCB_NOWAIT) {
4244 if (!inode_trylock(inode)) {
4252 if (unlikely(IS_IMMUTABLE(inode))) {
4257 ret = generic_write_checks(iocb, from);
4259 bool preallocated = false;
4260 size_t target_size = 0;
4263 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4264 set_inode_flag(inode, FI_NO_PREALLOC);
4266 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4267 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
4268 iov_iter_count(from)) ||
4269 f2fs_has_inline_data(inode) ||
4270 f2fs_force_buffered_io(inode, iocb, from)) {
4271 clear_inode_flag(inode, FI_NO_PREALLOC);
4272 inode_unlock(inode);
4279 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4282 if (iocb->ki_flags & IOCB_DIRECT) {
4284 * Convert inline data for Direct I/O before entering
4287 err = f2fs_convert_inline_inode(inode);
4291 * If force_buffere_io() is true, we have to allocate
4292 * blocks all the time, since f2fs_direct_IO will fall
4293 * back to buffered IO.
4295 if (!f2fs_force_buffered_io(inode, iocb, from) &&
4296 allow_outplace_dio(inode, iocb, from))
4299 preallocated = true;
4300 target_size = iocb->ki_pos + iov_iter_count(from);
4302 err = f2fs_preallocate_blocks(iocb, from);
4305 clear_inode_flag(inode, FI_NO_PREALLOC);
4306 inode_unlock(inode);
4311 ret = __generic_file_write_iter(iocb, from);
4312 clear_inode_flag(inode, FI_NO_PREALLOC);
4314 /* if we couldn't write data, we should deallocate blocks. */
4315 if (preallocated && i_size_read(inode) < target_size) {
4316 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4317 down_write(&F2FS_I(inode)->i_mmap_sem);
4318 f2fs_truncate(inode);
4319 up_write(&F2FS_I(inode)->i_mmap_sem);
4320 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4324 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
4327 inode_unlock(inode);
4329 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4330 iov_iter_count(from), ret);
4332 ret = generic_write_sync(iocb, ret);
4336 #ifdef CONFIG_COMPAT
4337 struct compat_f2fs_gc_range {
4342 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4343 struct compat_f2fs_gc_range)
4345 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4347 struct compat_f2fs_gc_range __user *urange;
4348 struct f2fs_gc_range range;
4351 urange = compat_ptr(arg);
4352 err = get_user(range.sync, &urange->sync);
4353 err |= get_user(range.start, &urange->start);
4354 err |= get_user(range.len, &urange->len);
4358 return __f2fs_ioc_gc_range(file, &range);
4361 struct compat_f2fs_move_range {
4367 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4368 struct compat_f2fs_move_range)
4370 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4372 struct compat_f2fs_move_range __user *urange;
4373 struct f2fs_move_range range;
4376 urange = compat_ptr(arg);
4377 err = get_user(range.dst_fd, &urange->dst_fd);
4378 err |= get_user(range.pos_in, &urange->pos_in);
4379 err |= get_user(range.pos_out, &urange->pos_out);
4380 err |= get_user(range.len, &urange->len);
4384 return __f2fs_ioc_move_range(file, &range);
4387 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4389 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4391 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4395 case FS_IOC32_GETVERSION:
4396 cmd = FS_IOC_GETVERSION;
4398 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4399 return f2fs_compat_ioc_gc_range(file, arg);
4400 case F2FS_IOC32_MOVE_RANGE:
4401 return f2fs_compat_ioc_move_range(file, arg);
4402 case F2FS_IOC_START_ATOMIC_WRITE:
4403 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4404 case F2FS_IOC_START_VOLATILE_WRITE:
4405 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4406 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4407 case F2FS_IOC_SHUTDOWN:
4409 case FS_IOC_SET_ENCRYPTION_POLICY:
4410 case FS_IOC_GET_ENCRYPTION_PWSALT:
4411 case FS_IOC_GET_ENCRYPTION_POLICY:
4412 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4413 case FS_IOC_ADD_ENCRYPTION_KEY:
4414 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4415 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4416 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4417 case FS_IOC_GET_ENCRYPTION_NONCE:
4418 case F2FS_IOC_GARBAGE_COLLECT:
4419 case F2FS_IOC_WRITE_CHECKPOINT:
4420 case F2FS_IOC_DEFRAGMENT:
4421 case F2FS_IOC_FLUSH_DEVICE:
4422 case F2FS_IOC_GET_FEATURES:
4423 case F2FS_IOC_GET_PIN_FILE:
4424 case F2FS_IOC_SET_PIN_FILE:
4425 case F2FS_IOC_PRECACHE_EXTENTS:
4426 case F2FS_IOC_RESIZE_FS:
4427 case FS_IOC_ENABLE_VERITY:
4428 case FS_IOC_MEASURE_VERITY:
4429 case FS_IOC_READ_VERITY_METADATA:
4430 case FS_IOC_GETFSLABEL:
4431 case FS_IOC_SETFSLABEL:
4432 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4433 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4434 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4435 case F2FS_IOC_SEC_TRIM_FILE:
4436 case F2FS_IOC_GET_COMPRESS_OPTION:
4437 case F2FS_IOC_SET_COMPRESS_OPTION:
4438 case F2FS_IOC_DECOMPRESS_FILE:
4439 case F2FS_IOC_COMPRESS_FILE:
4442 return -ENOIOCTLCMD;
4444 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4448 const struct file_operations f2fs_file_operations = {
4449 .llseek = f2fs_llseek,
4450 .read_iter = f2fs_file_read_iter,
4451 .write_iter = f2fs_file_write_iter,
4452 .open = f2fs_file_open,
4453 .release = f2fs_release_file,
4454 .mmap = f2fs_file_mmap,
4455 .flush = f2fs_file_flush,
4456 .fsync = f2fs_sync_file,
4457 .fallocate = f2fs_fallocate,
4458 .unlocked_ioctl = f2fs_ioctl,
4459 #ifdef CONFIG_COMPAT
4460 .compat_ioctl = f2fs_compat_ioctl,
4462 .splice_read = generic_file_splice_read,
4463 .splice_write = iter_file_splice_write,