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);
120 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
123 #ifdef CONFIG_F2FS_FS_COMPRESSION
125 set_new_dnode(&dn, inode, NULL, NULL, 0);
126 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
135 f2fs_wait_on_page_writeback(page, DATA, false, true);
137 /* wait for GCed page writeback via META_MAPPING */
138 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
141 * check to see if the page is mapped already (no holes)
143 if (PageMappedToDisk(page))
146 /* page is wholly or partially inside EOF */
147 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
148 i_size_read(inode)) {
151 offset = i_size_read(inode) & ~PAGE_MASK;
152 zero_user_segment(page, offset, PAGE_SIZE);
154 set_page_dirty(page);
155 if (!PageUptodate(page))
156 SetPageUptodate(page);
158 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
159 f2fs_update_time(sbi, REQ_TIME);
161 trace_f2fs_vm_page_mkwrite(page, DATA);
163 up_read(&F2FS_I(inode)->i_mmap_sem);
165 sb_end_pagefault(inode->i_sb);
167 return block_page_mkwrite_return(err);
170 static const struct vm_operations_struct f2fs_file_vm_ops = {
171 .fault = f2fs_filemap_fault,
172 .map_pages = filemap_map_pages,
173 .page_mkwrite = f2fs_vm_page_mkwrite,
176 static int get_parent_ino(struct inode *inode, nid_t *pino)
178 struct dentry *dentry;
181 * Make sure to get the non-deleted alias. The alias associated with
182 * the open file descriptor being fsync()'ed may be deleted already.
184 dentry = d_find_alias(inode);
188 *pino = parent_ino(dentry);
193 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
195 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
196 enum cp_reason_type cp_reason = CP_NO_NEEDED;
198 if (!S_ISREG(inode->i_mode))
199 cp_reason = CP_NON_REGULAR;
200 else if (f2fs_compressed_file(inode))
201 cp_reason = CP_COMPRESSED;
202 else if (inode->i_nlink != 1)
203 cp_reason = CP_HARDLINK;
204 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
205 cp_reason = CP_SB_NEED_CP;
206 else if (file_wrong_pino(inode))
207 cp_reason = CP_WRONG_PINO;
208 else if (!f2fs_space_for_roll_forward(sbi))
209 cp_reason = CP_NO_SPC_ROLL;
210 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
211 cp_reason = CP_NODE_NEED_CP;
212 else if (test_opt(sbi, FASTBOOT))
213 cp_reason = CP_FASTBOOT_MODE;
214 else if (F2FS_OPTION(sbi).active_logs == 2)
215 cp_reason = CP_SPEC_LOG_NUM;
216 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
217 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
218 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
220 cp_reason = CP_RECOVER_DIR;
225 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
227 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
229 /* But we need to avoid that there are some inode updates */
230 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
236 static void try_to_fix_pino(struct inode *inode)
238 struct f2fs_inode_info *fi = F2FS_I(inode);
241 down_write(&fi->i_sem);
242 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
243 get_parent_ino(inode, &pino)) {
244 f2fs_i_pino_write(inode, pino);
245 file_got_pino(inode);
247 up_write(&fi->i_sem);
250 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
251 int datasync, bool atomic)
253 struct inode *inode = file->f_mapping->host;
254 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
255 nid_t ino = inode->i_ino;
257 enum cp_reason_type cp_reason = 0;
258 struct writeback_control wbc = {
259 .sync_mode = WB_SYNC_ALL,
260 .nr_to_write = LONG_MAX,
263 unsigned int seq_id = 0;
265 if (unlikely(f2fs_readonly(inode->i_sb) ||
266 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
269 trace_f2fs_sync_file_enter(inode);
271 if (S_ISDIR(inode->i_mode))
274 /* if fdatasync is triggered, let's do in-place-update */
275 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
276 set_inode_flag(inode, FI_NEED_IPU);
277 ret = file_write_and_wait_range(file, start, end);
278 clear_inode_flag(inode, FI_NEED_IPU);
281 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
285 /* if the inode is dirty, let's recover all the time */
286 if (!f2fs_skip_inode_update(inode, datasync)) {
287 f2fs_write_inode(inode, NULL);
292 * if there is no written data, don't waste time to write recovery info.
294 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
295 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
297 /* it may call write_inode just prior to fsync */
298 if (need_inode_page_update(sbi, ino))
301 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
302 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
308 * Both of fdatasync() and fsync() are able to be recovered from
311 down_read(&F2FS_I(inode)->i_sem);
312 cp_reason = need_do_checkpoint(inode);
313 up_read(&F2FS_I(inode)->i_sem);
316 /* all the dirty node pages should be flushed for POR */
317 ret = f2fs_sync_fs(inode->i_sb, 1);
320 * We've secured consistency through sync_fs. Following pino
321 * will be used only for fsynced inodes after checkpoint.
323 try_to_fix_pino(inode);
324 clear_inode_flag(inode, FI_APPEND_WRITE);
325 clear_inode_flag(inode, FI_UPDATE_WRITE);
329 atomic_inc(&sbi->wb_sync_req[NODE]);
330 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
331 atomic_dec(&sbi->wb_sync_req[NODE]);
335 /* if cp_error was enabled, we should avoid infinite loop */
336 if (unlikely(f2fs_cp_error(sbi))) {
341 if (f2fs_need_inode_block_update(sbi, ino)) {
342 f2fs_mark_inode_dirty_sync(inode, true);
343 f2fs_write_inode(inode, NULL);
348 * If it's atomic_write, it's just fine to keep write ordering. So
349 * here we don't need to wait for node write completion, since we use
350 * node chain which serializes node blocks. If one of node writes are
351 * reordered, we can see simply broken chain, resulting in stopping
352 * roll-forward recovery. It means we'll recover all or none node blocks
356 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
361 /* once recovery info is written, don't need to tack this */
362 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
363 clear_inode_flag(inode, FI_APPEND_WRITE);
365 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
366 ret = f2fs_issue_flush(sbi, inode->i_ino);
368 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
369 clear_inode_flag(inode, FI_UPDATE_WRITE);
370 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
372 f2fs_update_time(sbi, REQ_TIME);
374 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
378 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
380 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
382 return f2fs_do_sync_file(file, start, end, datasync, false);
385 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
386 pgoff_t index, int whence)
390 if (__is_valid_data_blkaddr(blkaddr))
392 if (blkaddr == NEW_ADDR &&
393 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
397 if (blkaddr == NULL_ADDR)
404 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
406 struct inode *inode = file->f_mapping->host;
407 loff_t maxbytes = inode->i_sb->s_maxbytes;
408 struct dnode_of_data dn;
409 pgoff_t pgofs, end_offset;
410 loff_t data_ofs = offset;
416 isize = i_size_read(inode);
420 /* handle inline data case */
421 if (f2fs_has_inline_data(inode)) {
422 if (whence == SEEK_HOLE) {
425 } else if (whence == SEEK_DATA) {
431 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
433 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
434 set_new_dnode(&dn, inode, NULL, NULL, 0);
435 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
436 if (err && err != -ENOENT) {
438 } else if (err == -ENOENT) {
439 /* direct node does not exists */
440 if (whence == SEEK_DATA) {
441 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
448 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
450 /* find data/hole in dnode block */
451 for (; dn.ofs_in_node < end_offset;
452 dn.ofs_in_node++, pgofs++,
453 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
456 blkaddr = f2fs_data_blkaddr(&dn);
458 if (__is_valid_data_blkaddr(blkaddr) &&
459 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
460 blkaddr, DATA_GENERIC_ENHANCE)) {
465 if (__found_offset(file->f_mapping, blkaddr,
474 if (whence == SEEK_DATA)
477 if (whence == SEEK_HOLE && data_ofs > isize)
480 return vfs_setpos(file, data_ofs, maxbytes);
486 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
488 struct inode *inode = file->f_mapping->host;
489 loff_t maxbytes = inode->i_sb->s_maxbytes;
491 if (f2fs_compressed_file(inode))
492 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
498 return generic_file_llseek_size(file, offset, whence,
499 maxbytes, i_size_read(inode));
504 return f2fs_seek_block(file, offset, whence);
510 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
512 struct inode *inode = file_inode(file);
514 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
517 if (!f2fs_is_compress_backend_ready(inode))
521 vma->vm_ops = &f2fs_file_vm_ops;
522 set_inode_flag(inode, FI_MMAP_FILE);
526 static int f2fs_file_open(struct inode *inode, struct file *filp)
528 int err = fscrypt_file_open(inode, filp);
533 if (!f2fs_is_compress_backend_ready(inode))
536 err = fsverity_file_open(inode, filp);
540 filp->f_mode |= FMODE_NOWAIT;
542 return dquot_file_open(inode, filp);
545 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
547 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
548 struct f2fs_node *raw_node;
549 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
552 bool compressed_cluster = false;
553 int cluster_index = 0, valid_blocks = 0;
554 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
555 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
557 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
558 base = get_extra_isize(dn->inode);
560 raw_node = F2FS_NODE(dn->node_page);
561 addr = blkaddr_in_node(raw_node) + base + ofs;
563 /* Assumption: truncateion starts with cluster */
564 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
565 block_t blkaddr = le32_to_cpu(*addr);
567 if (f2fs_compressed_file(dn->inode) &&
568 !(cluster_index & (cluster_size - 1))) {
569 if (compressed_cluster)
570 f2fs_i_compr_blocks_update(dn->inode,
571 valid_blocks, false);
572 compressed_cluster = (blkaddr == COMPRESS_ADDR);
576 if (blkaddr == NULL_ADDR)
579 dn->data_blkaddr = NULL_ADDR;
580 f2fs_set_data_blkaddr(dn);
582 if (__is_valid_data_blkaddr(blkaddr)) {
583 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
584 DATA_GENERIC_ENHANCE))
586 if (compressed_cluster)
590 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
591 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
593 f2fs_invalidate_blocks(sbi, blkaddr);
595 if (!released || blkaddr != COMPRESS_ADDR)
599 if (compressed_cluster)
600 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
605 * once we invalidate valid blkaddr in range [ofs, ofs + count],
606 * we will invalidate all blkaddr in the whole range.
608 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
610 f2fs_update_extent_cache_range(dn, fofs, 0, len);
611 dec_valid_block_count(sbi, dn->inode, nr_free);
613 dn->ofs_in_node = ofs;
615 f2fs_update_time(sbi, REQ_TIME);
616 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
617 dn->ofs_in_node, nr_free);
620 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
622 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
625 static int truncate_partial_data_page(struct inode *inode, u64 from,
628 loff_t offset = from & (PAGE_SIZE - 1);
629 pgoff_t index = from >> PAGE_SHIFT;
630 struct address_space *mapping = inode->i_mapping;
633 if (!offset && !cache_only)
637 page = find_lock_page(mapping, index);
638 if (page && PageUptodate(page))
640 f2fs_put_page(page, 1);
644 page = f2fs_get_lock_data_page(inode, index, true);
646 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
648 f2fs_wait_on_page_writeback(page, DATA, true, true);
649 zero_user(page, offset, PAGE_SIZE - offset);
651 /* An encrypted inode should have a key and truncate the last page. */
652 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
654 set_page_dirty(page);
655 f2fs_put_page(page, 1);
659 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
661 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
662 struct dnode_of_data dn;
664 int count = 0, err = 0;
666 bool truncate_page = false;
668 trace_f2fs_truncate_blocks_enter(inode, from);
670 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
672 if (free_from >= max_file_blocks(inode))
678 ipage = f2fs_get_node_page(sbi, inode->i_ino);
680 err = PTR_ERR(ipage);
684 if (f2fs_has_inline_data(inode)) {
685 f2fs_truncate_inline_inode(inode, ipage, from);
686 f2fs_put_page(ipage, 1);
687 truncate_page = true;
691 set_new_dnode(&dn, inode, ipage, NULL, 0);
692 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
699 count = ADDRS_PER_PAGE(dn.node_page, inode);
701 count -= dn.ofs_in_node;
702 f2fs_bug_on(sbi, count < 0);
704 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
705 f2fs_truncate_data_blocks_range(&dn, count);
711 err = f2fs_truncate_inode_blocks(inode, free_from);
716 /* lastly zero out the first data page */
718 err = truncate_partial_data_page(inode, from, truncate_page);
720 trace_f2fs_truncate_blocks_exit(inode, err);
724 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
726 u64 free_from = from;
729 #ifdef CONFIG_F2FS_FS_COMPRESSION
731 * for compressed file, only support cluster size
732 * aligned truncation.
734 if (f2fs_compressed_file(inode))
735 free_from = round_up(from,
736 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
739 err = f2fs_do_truncate_blocks(inode, free_from, lock);
743 #ifdef CONFIG_F2FS_FS_COMPRESSION
744 if (from != free_from) {
745 err = f2fs_truncate_partial_cluster(inode, from, lock);
754 int f2fs_truncate(struct inode *inode)
758 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
761 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
762 S_ISLNK(inode->i_mode)))
765 trace_f2fs_truncate(inode);
767 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
768 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
772 err = dquot_initialize(inode);
776 /* we should check inline_data size */
777 if (!f2fs_may_inline_data(inode)) {
778 err = f2fs_convert_inline_inode(inode);
783 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
787 inode->i_mtime = inode->i_ctime = current_time(inode);
788 f2fs_mark_inode_dirty_sync(inode, false);
792 int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
793 struct kstat *stat, u32 request_mask, unsigned int query_flags)
795 struct inode *inode = d_inode(path->dentry);
796 struct f2fs_inode_info *fi = F2FS_I(inode);
797 struct f2fs_inode *ri;
800 if (f2fs_has_extra_attr(inode) &&
801 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
802 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
803 stat->result_mask |= STATX_BTIME;
804 stat->btime.tv_sec = fi->i_crtime.tv_sec;
805 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
809 if (flags & F2FS_COMPR_FL)
810 stat->attributes |= STATX_ATTR_COMPRESSED;
811 if (flags & F2FS_APPEND_FL)
812 stat->attributes |= STATX_ATTR_APPEND;
813 if (IS_ENCRYPTED(inode))
814 stat->attributes |= STATX_ATTR_ENCRYPTED;
815 if (flags & F2FS_IMMUTABLE_FL)
816 stat->attributes |= STATX_ATTR_IMMUTABLE;
817 if (flags & F2FS_NODUMP_FL)
818 stat->attributes |= STATX_ATTR_NODUMP;
819 if (IS_VERITY(inode))
820 stat->attributes |= STATX_ATTR_VERITY;
822 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
824 STATX_ATTR_ENCRYPTED |
825 STATX_ATTR_IMMUTABLE |
829 generic_fillattr(&init_user_ns, inode, stat);
831 /* we need to show initial sectors used for inline_data/dentries */
832 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
833 f2fs_has_inline_dentry(inode))
834 stat->blocks += (stat->size + 511) >> 9;
839 #ifdef CONFIG_F2FS_FS_POSIX_ACL
840 static void __setattr_copy(struct user_namespace *mnt_userns,
841 struct inode *inode, const struct iattr *attr)
843 unsigned int ia_valid = attr->ia_valid;
845 if (ia_valid & ATTR_UID)
846 inode->i_uid = attr->ia_uid;
847 if (ia_valid & ATTR_GID)
848 inode->i_gid = attr->ia_gid;
849 if (ia_valid & ATTR_ATIME)
850 inode->i_atime = attr->ia_atime;
851 if (ia_valid & ATTR_MTIME)
852 inode->i_mtime = attr->ia_mtime;
853 if (ia_valid & ATTR_CTIME)
854 inode->i_ctime = attr->ia_ctime;
855 if (ia_valid & ATTR_MODE) {
856 umode_t mode = attr->ia_mode;
857 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
859 if (!in_group_p(kgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
861 set_acl_inode(inode, mode);
865 #define __setattr_copy setattr_copy
868 int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
871 struct inode *inode = d_inode(dentry);
874 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
877 if (unlikely(IS_IMMUTABLE(inode)))
880 if (unlikely(IS_APPEND(inode) &&
881 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
882 ATTR_GID | ATTR_TIMES_SET))))
885 if ((attr->ia_valid & ATTR_SIZE) &&
886 !f2fs_is_compress_backend_ready(inode))
889 err = setattr_prepare(&init_user_ns, dentry, attr);
893 err = fscrypt_prepare_setattr(dentry, attr);
897 err = fsverity_prepare_setattr(dentry, attr);
901 if (is_quota_modification(inode, attr)) {
902 err = dquot_initialize(inode);
906 if ((attr->ia_valid & ATTR_UID &&
907 !uid_eq(attr->ia_uid, inode->i_uid)) ||
908 (attr->ia_valid & ATTR_GID &&
909 !gid_eq(attr->ia_gid, inode->i_gid))) {
910 f2fs_lock_op(F2FS_I_SB(inode));
911 err = dquot_transfer(inode, attr);
913 set_sbi_flag(F2FS_I_SB(inode),
914 SBI_QUOTA_NEED_REPAIR);
915 f2fs_unlock_op(F2FS_I_SB(inode));
919 * update uid/gid under lock_op(), so that dquot and inode can
920 * be updated atomically.
922 if (attr->ia_valid & ATTR_UID)
923 inode->i_uid = attr->ia_uid;
924 if (attr->ia_valid & ATTR_GID)
925 inode->i_gid = attr->ia_gid;
926 f2fs_mark_inode_dirty_sync(inode, true);
927 f2fs_unlock_op(F2FS_I_SB(inode));
930 if (attr->ia_valid & ATTR_SIZE) {
931 loff_t old_size = i_size_read(inode);
933 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
935 * should convert inline inode before i_size_write to
936 * keep smaller than inline_data size with inline flag.
938 err = f2fs_convert_inline_inode(inode);
943 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
944 down_write(&F2FS_I(inode)->i_mmap_sem);
946 truncate_setsize(inode, attr->ia_size);
948 if (attr->ia_size <= old_size)
949 err = f2fs_truncate(inode);
951 * do not trim all blocks after i_size if target size is
952 * larger than i_size.
954 up_write(&F2FS_I(inode)->i_mmap_sem);
955 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
959 spin_lock(&F2FS_I(inode)->i_size_lock);
960 inode->i_mtime = inode->i_ctime = current_time(inode);
961 F2FS_I(inode)->last_disk_size = i_size_read(inode);
962 spin_unlock(&F2FS_I(inode)->i_size_lock);
965 __setattr_copy(&init_user_ns, inode, attr);
967 if (attr->ia_valid & ATTR_MODE) {
968 err = posix_acl_chmod(&init_user_ns, inode, f2fs_get_inode_mode(inode));
970 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
972 inode->i_mode = F2FS_I(inode)->i_acl_mode;
973 clear_inode_flag(inode, FI_ACL_MODE);
977 /* file size may changed here */
978 f2fs_mark_inode_dirty_sync(inode, true);
980 /* inode change will produce dirty node pages flushed by checkpoint */
981 f2fs_balance_fs(F2FS_I_SB(inode), true);
986 const struct inode_operations f2fs_file_inode_operations = {
987 .getattr = f2fs_getattr,
988 .setattr = f2fs_setattr,
989 .get_acl = f2fs_get_acl,
990 .set_acl = f2fs_set_acl,
991 .listxattr = f2fs_listxattr,
992 .fiemap = f2fs_fiemap,
993 .fileattr_get = f2fs_fileattr_get,
994 .fileattr_set = f2fs_fileattr_set,
997 static int fill_zero(struct inode *inode, pgoff_t index,
998 loff_t start, loff_t len)
1000 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1006 f2fs_balance_fs(sbi, true);
1009 page = f2fs_get_new_data_page(inode, NULL, index, false);
1010 f2fs_unlock_op(sbi);
1013 return PTR_ERR(page);
1015 f2fs_wait_on_page_writeback(page, DATA, true, true);
1016 zero_user(page, start, len);
1017 set_page_dirty(page);
1018 f2fs_put_page(page, 1);
1022 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1026 while (pg_start < pg_end) {
1027 struct dnode_of_data dn;
1028 pgoff_t end_offset, count;
1030 set_new_dnode(&dn, inode, NULL, NULL, 0);
1031 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1033 if (err == -ENOENT) {
1034 pg_start = f2fs_get_next_page_offset(&dn,
1041 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1042 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1044 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1046 f2fs_truncate_data_blocks_range(&dn, count);
1047 f2fs_put_dnode(&dn);
1054 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1056 pgoff_t pg_start, pg_end;
1057 loff_t off_start, off_end;
1060 ret = f2fs_convert_inline_inode(inode);
1064 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1065 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1067 off_start = offset & (PAGE_SIZE - 1);
1068 off_end = (offset + len) & (PAGE_SIZE - 1);
1070 if (pg_start == pg_end) {
1071 ret = fill_zero(inode, pg_start, off_start,
1072 off_end - off_start);
1077 ret = fill_zero(inode, pg_start++, off_start,
1078 PAGE_SIZE - off_start);
1083 ret = fill_zero(inode, pg_end, 0, off_end);
1088 if (pg_start < pg_end) {
1089 struct address_space *mapping = inode->i_mapping;
1090 loff_t blk_start, blk_end;
1091 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1093 f2fs_balance_fs(sbi, true);
1095 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1096 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1098 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1099 down_write(&F2FS_I(inode)->i_mmap_sem);
1101 truncate_inode_pages_range(mapping, blk_start,
1105 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1106 f2fs_unlock_op(sbi);
1108 up_write(&F2FS_I(inode)->i_mmap_sem);
1109 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1116 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1117 int *do_replace, pgoff_t off, pgoff_t len)
1119 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1120 struct dnode_of_data dn;
1124 set_new_dnode(&dn, inode, NULL, NULL, 0);
1125 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1126 if (ret && ret != -ENOENT) {
1128 } else if (ret == -ENOENT) {
1129 if (dn.max_level == 0)
1131 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1132 dn.ofs_in_node, len);
1138 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1139 dn.ofs_in_node, len);
1140 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1141 *blkaddr = f2fs_data_blkaddr(&dn);
1143 if (__is_valid_data_blkaddr(*blkaddr) &&
1144 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1145 DATA_GENERIC_ENHANCE)) {
1146 f2fs_put_dnode(&dn);
1147 return -EFSCORRUPTED;
1150 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1152 if (f2fs_lfs_mode(sbi)) {
1153 f2fs_put_dnode(&dn);
1157 /* do not invalidate this block address */
1158 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1162 f2fs_put_dnode(&dn);
1171 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1172 int *do_replace, pgoff_t off, int len)
1174 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1175 struct dnode_of_data dn;
1178 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1179 if (*do_replace == 0)
1182 set_new_dnode(&dn, inode, NULL, NULL, 0);
1183 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1185 dec_valid_block_count(sbi, inode, 1);
1186 f2fs_invalidate_blocks(sbi, *blkaddr);
1188 f2fs_update_data_blkaddr(&dn, *blkaddr);
1190 f2fs_put_dnode(&dn);
1195 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1196 block_t *blkaddr, int *do_replace,
1197 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1199 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1204 if (blkaddr[i] == NULL_ADDR && !full) {
1209 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1210 struct dnode_of_data dn;
1211 struct node_info ni;
1215 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1216 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1220 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1222 f2fs_put_dnode(&dn);
1226 ilen = min((pgoff_t)
1227 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1228 dn.ofs_in_node, len - i);
1230 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1231 f2fs_truncate_data_blocks_range(&dn, 1);
1233 if (do_replace[i]) {
1234 f2fs_i_blocks_write(src_inode,
1236 f2fs_i_blocks_write(dst_inode,
1238 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1239 blkaddr[i], ni.version, true, false);
1245 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1246 if (dst_inode->i_size < new_size)
1247 f2fs_i_size_write(dst_inode, new_size);
1248 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1250 f2fs_put_dnode(&dn);
1252 struct page *psrc, *pdst;
1254 psrc = f2fs_get_lock_data_page(src_inode,
1257 return PTR_ERR(psrc);
1258 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1261 f2fs_put_page(psrc, 1);
1262 return PTR_ERR(pdst);
1264 f2fs_copy_page(psrc, pdst);
1265 set_page_dirty(pdst);
1266 f2fs_put_page(pdst, 1);
1267 f2fs_put_page(psrc, 1);
1269 ret = f2fs_truncate_hole(src_inode,
1270 src + i, src + i + 1);
1279 static int __exchange_data_block(struct inode *src_inode,
1280 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1281 pgoff_t len, bool full)
1283 block_t *src_blkaddr;
1289 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1291 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1292 array_size(olen, sizeof(block_t)),
1297 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1298 array_size(olen, sizeof(int)),
1301 kvfree(src_blkaddr);
1305 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1306 do_replace, src, olen);
1310 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1311 do_replace, src, dst, olen, full);
1319 kvfree(src_blkaddr);
1325 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1326 kvfree(src_blkaddr);
1331 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1333 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1334 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1335 pgoff_t start = offset >> PAGE_SHIFT;
1336 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1339 f2fs_balance_fs(sbi, true);
1341 /* avoid gc operation during block exchange */
1342 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1343 down_write(&F2FS_I(inode)->i_mmap_sem);
1346 f2fs_drop_extent_tree(inode);
1347 truncate_pagecache(inode, offset);
1348 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1349 f2fs_unlock_op(sbi);
1351 up_write(&F2FS_I(inode)->i_mmap_sem);
1352 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1356 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1361 if (offset + len >= i_size_read(inode))
1364 /* collapse range should be aligned to block size of f2fs. */
1365 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1368 ret = f2fs_convert_inline_inode(inode);
1372 /* write out all dirty pages from offset */
1373 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1377 ret = f2fs_do_collapse(inode, offset, len);
1381 /* write out all moved pages, if possible */
1382 down_write(&F2FS_I(inode)->i_mmap_sem);
1383 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1384 truncate_pagecache(inode, offset);
1386 new_size = i_size_read(inode) - len;
1387 ret = f2fs_truncate_blocks(inode, new_size, true);
1388 up_write(&F2FS_I(inode)->i_mmap_sem);
1390 f2fs_i_size_write(inode, new_size);
1394 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1397 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1398 pgoff_t index = start;
1399 unsigned int ofs_in_node = dn->ofs_in_node;
1403 for (; index < end; index++, dn->ofs_in_node++) {
1404 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1408 dn->ofs_in_node = ofs_in_node;
1409 ret = f2fs_reserve_new_blocks(dn, count);
1413 dn->ofs_in_node = ofs_in_node;
1414 for (index = start; index < end; index++, dn->ofs_in_node++) {
1415 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1417 * f2fs_reserve_new_blocks will not guarantee entire block
1420 if (dn->data_blkaddr == NULL_ADDR) {
1424 if (dn->data_blkaddr != NEW_ADDR) {
1425 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1426 dn->data_blkaddr = NEW_ADDR;
1427 f2fs_set_data_blkaddr(dn);
1431 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1436 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1439 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1440 struct address_space *mapping = inode->i_mapping;
1441 pgoff_t index, pg_start, pg_end;
1442 loff_t new_size = i_size_read(inode);
1443 loff_t off_start, off_end;
1446 ret = inode_newsize_ok(inode, (len + offset));
1450 ret = f2fs_convert_inline_inode(inode);
1454 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1458 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1459 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1461 off_start = offset & (PAGE_SIZE - 1);
1462 off_end = (offset + len) & (PAGE_SIZE - 1);
1464 if (pg_start == pg_end) {
1465 ret = fill_zero(inode, pg_start, off_start,
1466 off_end - off_start);
1470 new_size = max_t(loff_t, new_size, offset + len);
1473 ret = fill_zero(inode, pg_start++, off_start,
1474 PAGE_SIZE - off_start);
1478 new_size = max_t(loff_t, new_size,
1479 (loff_t)pg_start << PAGE_SHIFT);
1482 for (index = pg_start; index < pg_end;) {
1483 struct dnode_of_data dn;
1484 unsigned int end_offset;
1487 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1488 down_write(&F2FS_I(inode)->i_mmap_sem);
1490 truncate_pagecache_range(inode,
1491 (loff_t)index << PAGE_SHIFT,
1492 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1496 set_new_dnode(&dn, inode, NULL, NULL, 0);
1497 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1499 f2fs_unlock_op(sbi);
1500 up_write(&F2FS_I(inode)->i_mmap_sem);
1501 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1505 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1506 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1508 ret = f2fs_do_zero_range(&dn, index, end);
1509 f2fs_put_dnode(&dn);
1511 f2fs_unlock_op(sbi);
1512 up_write(&F2FS_I(inode)->i_mmap_sem);
1513 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1515 f2fs_balance_fs(sbi, dn.node_changed);
1521 new_size = max_t(loff_t, new_size,
1522 (loff_t)index << PAGE_SHIFT);
1526 ret = fill_zero(inode, pg_end, 0, off_end);
1530 new_size = max_t(loff_t, new_size, offset + len);
1535 if (new_size > i_size_read(inode)) {
1536 if (mode & FALLOC_FL_KEEP_SIZE)
1537 file_set_keep_isize(inode);
1539 f2fs_i_size_write(inode, new_size);
1544 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1546 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1547 pgoff_t nr, pg_start, pg_end, delta, idx;
1551 new_size = i_size_read(inode) + len;
1552 ret = inode_newsize_ok(inode, new_size);
1556 if (offset >= i_size_read(inode))
1559 /* insert range should be aligned to block size of f2fs. */
1560 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1563 ret = f2fs_convert_inline_inode(inode);
1567 f2fs_balance_fs(sbi, true);
1569 down_write(&F2FS_I(inode)->i_mmap_sem);
1570 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1571 up_write(&F2FS_I(inode)->i_mmap_sem);
1575 /* write out all dirty pages from offset */
1576 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1580 pg_start = offset >> PAGE_SHIFT;
1581 pg_end = (offset + len) >> PAGE_SHIFT;
1582 delta = pg_end - pg_start;
1583 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1585 /* avoid gc operation during block exchange */
1586 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1587 down_write(&F2FS_I(inode)->i_mmap_sem);
1588 truncate_pagecache(inode, offset);
1590 while (!ret && idx > pg_start) {
1591 nr = idx - pg_start;
1597 f2fs_drop_extent_tree(inode);
1599 ret = __exchange_data_block(inode, inode, idx,
1600 idx + delta, nr, false);
1601 f2fs_unlock_op(sbi);
1603 up_write(&F2FS_I(inode)->i_mmap_sem);
1604 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1606 /* write out all moved pages, if possible */
1607 down_write(&F2FS_I(inode)->i_mmap_sem);
1608 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1609 truncate_pagecache(inode, offset);
1610 up_write(&F2FS_I(inode)->i_mmap_sem);
1613 f2fs_i_size_write(inode, new_size);
1617 static int expand_inode_data(struct inode *inode, loff_t offset,
1618 loff_t len, int mode)
1620 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1621 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1622 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1623 .m_may_create = true };
1624 pgoff_t pg_start, pg_end;
1625 loff_t new_size = i_size_read(inode);
1627 block_t expanded = 0;
1630 err = inode_newsize_ok(inode, (len + offset));
1634 err = f2fs_convert_inline_inode(inode);
1638 f2fs_balance_fs(sbi, true);
1640 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1641 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1642 off_end = (offset + len) & (PAGE_SIZE - 1);
1644 map.m_lblk = pg_start;
1645 map.m_len = pg_end - pg_start;
1652 if (f2fs_is_pinned_file(inode)) {
1653 block_t sec_blks = BLKS_PER_SEC(sbi);
1654 block_t sec_len = roundup(map.m_len, sec_blks);
1656 map.m_len = sec_blks;
1658 if (has_not_enough_free_secs(sbi, 0,
1659 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1660 down_write(&sbi->gc_lock);
1661 err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
1662 if (err && err != -ENODATA && err != -EAGAIN)
1666 down_write(&sbi->pin_sem);
1669 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
1670 f2fs_unlock_op(sbi);
1672 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1673 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1675 up_write(&sbi->pin_sem);
1677 expanded += map.m_len;
1678 sec_len -= map.m_len;
1679 map.m_lblk += map.m_len;
1680 if (!err && sec_len)
1683 map.m_len = expanded;
1685 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1686 expanded = map.m_len;
1695 last_off = pg_start + expanded - 1;
1697 /* update new size to the failed position */
1698 new_size = (last_off == pg_end) ? offset + len :
1699 (loff_t)(last_off + 1) << PAGE_SHIFT;
1701 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1704 if (new_size > i_size_read(inode)) {
1705 if (mode & FALLOC_FL_KEEP_SIZE)
1706 file_set_keep_isize(inode);
1708 f2fs_i_size_write(inode, new_size);
1714 static long f2fs_fallocate(struct file *file, int mode,
1715 loff_t offset, loff_t len)
1717 struct inode *inode = file_inode(file);
1720 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1722 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1724 if (!f2fs_is_compress_backend_ready(inode))
1727 /* f2fs only support ->fallocate for regular file */
1728 if (!S_ISREG(inode->i_mode))
1731 if (IS_ENCRYPTED(inode) &&
1732 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1735 if (f2fs_compressed_file(inode) &&
1736 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1737 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1740 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1741 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1742 FALLOC_FL_INSERT_RANGE))
1747 if (mode & FALLOC_FL_PUNCH_HOLE) {
1748 if (offset >= inode->i_size)
1751 ret = punch_hole(inode, offset, len);
1752 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1753 ret = f2fs_collapse_range(inode, offset, len);
1754 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1755 ret = f2fs_zero_range(inode, offset, len, mode);
1756 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1757 ret = f2fs_insert_range(inode, offset, len);
1759 ret = expand_inode_data(inode, offset, len, mode);
1763 inode->i_mtime = inode->i_ctime = current_time(inode);
1764 f2fs_mark_inode_dirty_sync(inode, false);
1765 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1769 inode_unlock(inode);
1771 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1775 static int f2fs_release_file(struct inode *inode, struct file *filp)
1778 * f2fs_relase_file is called at every close calls. So we should
1779 * not drop any inmemory pages by close called by other process.
1781 if (!(filp->f_mode & FMODE_WRITE) ||
1782 atomic_read(&inode->i_writecount) != 1)
1785 /* some remained atomic pages should discarded */
1786 if (f2fs_is_atomic_file(inode))
1787 f2fs_drop_inmem_pages(inode);
1788 if (f2fs_is_volatile_file(inode)) {
1789 set_inode_flag(inode, FI_DROP_CACHE);
1790 filemap_fdatawrite(inode->i_mapping);
1791 clear_inode_flag(inode, FI_DROP_CACHE);
1792 clear_inode_flag(inode, FI_VOLATILE_FILE);
1793 stat_dec_volatile_write(inode);
1798 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1800 struct inode *inode = file_inode(file);
1803 * If the process doing a transaction is crashed, we should do
1804 * roll-back. Otherwise, other reader/write can see corrupted database
1805 * until all the writers close its file. Since this should be done
1806 * before dropping file lock, it needs to do in ->flush.
1808 if (f2fs_is_atomic_file(inode) &&
1809 F2FS_I(inode)->inmem_task == current)
1810 f2fs_drop_inmem_pages(inode);
1814 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1816 struct f2fs_inode_info *fi = F2FS_I(inode);
1817 u32 masked_flags = fi->i_flags & mask;
1819 /* mask can be shrunk by flags_valid selector */
1822 /* Is it quota file? Do not allow user to mess with it */
1823 if (IS_NOQUOTA(inode))
1826 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1827 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1829 if (!f2fs_empty_dir(inode))
1833 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1834 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1836 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1840 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1841 if (masked_flags & F2FS_COMPR_FL) {
1842 if (!f2fs_disable_compressed_file(inode))
1845 if (iflags & F2FS_NOCOMP_FL)
1847 if (iflags & F2FS_COMPR_FL) {
1848 if (!f2fs_may_compress(inode))
1850 if (S_ISREG(inode->i_mode) && inode->i_size)
1853 set_compress_context(inode);
1856 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1857 if (masked_flags & F2FS_COMPR_FL)
1861 fi->i_flags = iflags | (fi->i_flags & ~mask);
1862 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1863 (fi->i_flags & F2FS_NOCOMP_FL));
1865 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1866 set_inode_flag(inode, FI_PROJ_INHERIT);
1868 clear_inode_flag(inode, FI_PROJ_INHERIT);
1870 inode->i_ctime = current_time(inode);
1871 f2fs_set_inode_flags(inode);
1872 f2fs_mark_inode_dirty_sync(inode, true);
1876 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1879 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1880 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1881 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1882 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1884 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1885 * FS_IOC_FSSETXATTR is done by the VFS.
1888 static const struct {
1891 } f2fs_fsflags_map[] = {
1892 { F2FS_COMPR_FL, FS_COMPR_FL },
1893 { F2FS_SYNC_FL, FS_SYNC_FL },
1894 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1895 { F2FS_APPEND_FL, FS_APPEND_FL },
1896 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1897 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1898 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1899 { F2FS_INDEX_FL, FS_INDEX_FL },
1900 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1901 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1902 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1905 #define F2FS_GETTABLE_FS_FL ( \
1915 FS_PROJINHERIT_FL | \
1917 FS_INLINE_DATA_FL | \
1922 #define F2FS_SETTABLE_FS_FL ( \
1931 FS_PROJINHERIT_FL | \
1934 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1935 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1940 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1941 if (iflags & f2fs_fsflags_map[i].iflag)
1942 fsflags |= f2fs_fsflags_map[i].fsflag;
1947 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1948 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1953 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1954 if (fsflags & f2fs_fsflags_map[i].fsflag)
1955 iflags |= f2fs_fsflags_map[i].iflag;
1960 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1962 struct inode *inode = file_inode(filp);
1964 return put_user(inode->i_generation, (int __user *)arg);
1967 static int f2fs_ioc_start_atomic_write(struct file *filp)
1969 struct inode *inode = file_inode(filp);
1970 struct f2fs_inode_info *fi = F2FS_I(inode);
1971 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1974 if (!inode_owner_or_capable(&init_user_ns, inode))
1977 if (!S_ISREG(inode->i_mode))
1980 if (filp->f_flags & O_DIRECT)
1983 ret = mnt_want_write_file(filp);
1989 f2fs_disable_compressed_file(inode);
1991 if (f2fs_is_atomic_file(inode)) {
1992 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1997 ret = f2fs_convert_inline_inode(inode);
2001 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2004 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2005 * f2fs_is_atomic_file.
2007 if (get_dirty_pages(inode))
2008 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2009 inode->i_ino, get_dirty_pages(inode));
2010 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2012 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2016 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2017 if (list_empty(&fi->inmem_ilist))
2018 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
2019 sbi->atomic_files++;
2020 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2022 /* add inode in inmem_list first and set atomic_file */
2023 set_inode_flag(inode, FI_ATOMIC_FILE);
2024 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2025 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2027 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2028 F2FS_I(inode)->inmem_task = current;
2029 stat_update_max_atomic_write(inode);
2031 inode_unlock(inode);
2032 mnt_drop_write_file(filp);
2036 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2038 struct inode *inode = file_inode(filp);
2041 if (!inode_owner_or_capable(&init_user_ns, inode))
2044 ret = mnt_want_write_file(filp);
2048 f2fs_balance_fs(F2FS_I_SB(inode), true);
2052 if (f2fs_is_volatile_file(inode)) {
2057 if (f2fs_is_atomic_file(inode)) {
2058 ret = f2fs_commit_inmem_pages(inode);
2062 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2064 f2fs_drop_inmem_pages(inode);
2066 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2069 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2070 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2073 inode_unlock(inode);
2074 mnt_drop_write_file(filp);
2078 static int f2fs_ioc_start_volatile_write(struct file *filp)
2080 struct inode *inode = file_inode(filp);
2083 if (!inode_owner_or_capable(&init_user_ns, inode))
2086 if (!S_ISREG(inode->i_mode))
2089 ret = mnt_want_write_file(filp);
2095 if (f2fs_is_volatile_file(inode))
2098 ret = f2fs_convert_inline_inode(inode);
2102 stat_inc_volatile_write(inode);
2103 stat_update_max_volatile_write(inode);
2105 set_inode_flag(inode, FI_VOLATILE_FILE);
2106 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2108 inode_unlock(inode);
2109 mnt_drop_write_file(filp);
2113 static int f2fs_ioc_release_volatile_write(struct file *filp)
2115 struct inode *inode = file_inode(filp);
2118 if (!inode_owner_or_capable(&init_user_ns, inode))
2121 ret = mnt_want_write_file(filp);
2127 if (!f2fs_is_volatile_file(inode))
2130 if (!f2fs_is_first_block_written(inode)) {
2131 ret = truncate_partial_data_page(inode, 0, true);
2135 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2137 inode_unlock(inode);
2138 mnt_drop_write_file(filp);
2142 static int f2fs_ioc_abort_volatile_write(struct file *filp)
2144 struct inode *inode = file_inode(filp);
2147 if (!inode_owner_or_capable(&init_user_ns, inode))
2150 ret = mnt_want_write_file(filp);
2156 if (f2fs_is_atomic_file(inode))
2157 f2fs_drop_inmem_pages(inode);
2158 if (f2fs_is_volatile_file(inode)) {
2159 clear_inode_flag(inode, FI_VOLATILE_FILE);
2160 stat_dec_volatile_write(inode);
2161 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2164 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2166 inode_unlock(inode);
2168 mnt_drop_write_file(filp);
2169 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2173 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2175 struct inode *inode = file_inode(filp);
2176 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2177 struct super_block *sb = sbi->sb;
2181 if (!capable(CAP_SYS_ADMIN))
2184 if (get_user(in, (__u32 __user *)arg))
2187 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2188 ret = mnt_want_write_file(filp);
2190 if (ret == -EROFS) {
2192 f2fs_stop_checkpoint(sbi, false);
2193 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2194 trace_f2fs_shutdown(sbi, in, ret);
2201 case F2FS_GOING_DOWN_FULLSYNC:
2202 ret = freeze_bdev(sb->s_bdev);
2205 f2fs_stop_checkpoint(sbi, false);
2206 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2207 thaw_bdev(sb->s_bdev);
2209 case F2FS_GOING_DOWN_METASYNC:
2210 /* do checkpoint only */
2211 ret = f2fs_sync_fs(sb, 1);
2214 f2fs_stop_checkpoint(sbi, false);
2215 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2217 case F2FS_GOING_DOWN_NOSYNC:
2218 f2fs_stop_checkpoint(sbi, false);
2219 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2221 case F2FS_GOING_DOWN_METAFLUSH:
2222 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2223 f2fs_stop_checkpoint(sbi, false);
2224 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2226 case F2FS_GOING_DOWN_NEED_FSCK:
2227 set_sbi_flag(sbi, SBI_NEED_FSCK);
2228 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2229 set_sbi_flag(sbi, SBI_IS_DIRTY);
2230 /* do checkpoint only */
2231 ret = f2fs_sync_fs(sb, 1);
2238 f2fs_stop_gc_thread(sbi);
2239 f2fs_stop_discard_thread(sbi);
2241 f2fs_drop_discard_cmd(sbi);
2242 clear_opt(sbi, DISCARD);
2244 f2fs_update_time(sbi, REQ_TIME);
2246 if (in != F2FS_GOING_DOWN_FULLSYNC)
2247 mnt_drop_write_file(filp);
2249 trace_f2fs_shutdown(sbi, in, ret);
2254 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2256 struct inode *inode = file_inode(filp);
2257 struct super_block *sb = inode->i_sb;
2258 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2259 struct fstrim_range range;
2262 if (!capable(CAP_SYS_ADMIN))
2265 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2268 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2272 ret = mnt_want_write_file(filp);
2276 range.minlen = max((unsigned int)range.minlen,
2277 q->limits.discard_granularity);
2278 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2279 mnt_drop_write_file(filp);
2283 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2286 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2290 static bool uuid_is_nonzero(__u8 u[16])
2294 for (i = 0; i < 16; i++)
2300 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2302 struct inode *inode = file_inode(filp);
2304 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2307 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2309 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2312 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2314 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2316 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2319 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2321 struct inode *inode = file_inode(filp);
2322 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2325 if (!f2fs_sb_has_encrypt(sbi))
2328 err = mnt_want_write_file(filp);
2332 down_write(&sbi->sb_lock);
2334 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2337 /* update superblock with uuid */
2338 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2340 err = f2fs_commit_super(sbi, false);
2343 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2347 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2351 up_write(&sbi->sb_lock);
2352 mnt_drop_write_file(filp);
2356 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2359 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2362 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2365 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2367 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2370 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2373 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2375 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2378 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2381 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2384 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2387 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2390 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2393 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2396 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2399 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2401 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2404 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2407 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2409 struct inode *inode = file_inode(filp);
2410 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2414 if (!capable(CAP_SYS_ADMIN))
2417 if (get_user(sync, (__u32 __user *)arg))
2420 if (f2fs_readonly(sbi->sb))
2423 ret = mnt_want_write_file(filp);
2428 if (!down_write_trylock(&sbi->gc_lock)) {
2433 down_write(&sbi->gc_lock);
2436 ret = f2fs_gc(sbi, sync, true, false, NULL_SEGNO);
2438 mnt_drop_write_file(filp);
2442 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2444 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2448 if (!capable(CAP_SYS_ADMIN))
2450 if (f2fs_readonly(sbi->sb))
2453 end = range->start + range->len;
2454 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2455 end >= MAX_BLKADDR(sbi))
2458 ret = mnt_want_write_file(filp);
2464 if (!down_write_trylock(&sbi->gc_lock)) {
2469 down_write(&sbi->gc_lock);
2472 ret = f2fs_gc(sbi, range->sync, true, false,
2473 GET_SEGNO(sbi, range->start));
2479 range->start += BLKS_PER_SEC(sbi);
2480 if (range->start <= end)
2483 mnt_drop_write_file(filp);
2487 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2489 struct f2fs_gc_range range;
2491 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2494 return __f2fs_ioc_gc_range(filp, &range);
2497 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2499 struct inode *inode = file_inode(filp);
2500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2503 if (!capable(CAP_SYS_ADMIN))
2506 if (f2fs_readonly(sbi->sb))
2509 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2510 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2514 ret = mnt_want_write_file(filp);
2518 ret = f2fs_sync_fs(sbi->sb, 1);
2520 mnt_drop_write_file(filp);
2524 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2526 struct f2fs_defragment *range)
2528 struct inode *inode = file_inode(filp);
2529 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2530 .m_seg_type = NO_CHECK_TYPE,
2531 .m_may_create = false };
2532 struct extent_info ei = {0, 0, 0};
2533 pgoff_t pg_start, pg_end, next_pgofs;
2534 unsigned int blk_per_seg = sbi->blocks_per_seg;
2535 unsigned int total = 0, sec_num;
2536 block_t blk_end = 0;
2537 bool fragmented = false;
2540 /* if in-place-update policy is enabled, don't waste time here */
2541 if (f2fs_should_update_inplace(inode, NULL))
2544 pg_start = range->start >> PAGE_SHIFT;
2545 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2547 f2fs_balance_fs(sbi, true);
2551 /* writeback all dirty pages in the range */
2552 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2553 range->start + range->len - 1);
2558 * lookup mapping info in extent cache, skip defragmenting if physical
2559 * block addresses are continuous.
2561 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2562 if (ei.fofs + ei.len >= pg_end)
2566 map.m_lblk = pg_start;
2567 map.m_next_pgofs = &next_pgofs;
2570 * lookup mapping info in dnode page cache, skip defragmenting if all
2571 * physical block addresses are continuous even if there are hole(s)
2572 * in logical blocks.
2574 while (map.m_lblk < pg_end) {
2575 map.m_len = pg_end - map.m_lblk;
2576 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2580 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2581 map.m_lblk = next_pgofs;
2585 if (blk_end && blk_end != map.m_pblk)
2588 /* record total count of block that we're going to move */
2591 blk_end = map.m_pblk + map.m_len;
2593 map.m_lblk += map.m_len;
2601 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
2604 * make sure there are enough free section for LFS allocation, this can
2605 * avoid defragment running in SSR mode when free section are allocated
2608 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2613 map.m_lblk = pg_start;
2614 map.m_len = pg_end - pg_start;
2617 while (map.m_lblk < pg_end) {
2622 map.m_len = pg_end - map.m_lblk;
2623 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2627 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2628 map.m_lblk = next_pgofs;
2632 set_inode_flag(inode, FI_DO_DEFRAG);
2635 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2638 page = f2fs_get_lock_data_page(inode, idx, true);
2640 err = PTR_ERR(page);
2644 set_page_dirty(page);
2645 f2fs_put_page(page, 1);
2654 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2657 clear_inode_flag(inode, FI_DO_DEFRAG);
2659 err = filemap_fdatawrite(inode->i_mapping);
2664 clear_inode_flag(inode, FI_DO_DEFRAG);
2666 inode_unlock(inode);
2668 range->len = (u64)total << PAGE_SHIFT;
2672 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2674 struct inode *inode = file_inode(filp);
2675 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2676 struct f2fs_defragment range;
2679 if (!capable(CAP_SYS_ADMIN))
2682 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2685 if (f2fs_readonly(sbi->sb))
2688 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2692 /* verify alignment of offset & size */
2693 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2696 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2697 max_file_blocks(inode)))
2700 err = mnt_want_write_file(filp);
2704 err = f2fs_defragment_range(sbi, filp, &range);
2705 mnt_drop_write_file(filp);
2707 f2fs_update_time(sbi, REQ_TIME);
2711 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2718 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2719 struct file *file_out, loff_t pos_out, size_t len)
2721 struct inode *src = file_inode(file_in);
2722 struct inode *dst = file_inode(file_out);
2723 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2724 size_t olen = len, dst_max_i_size = 0;
2728 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2729 src->i_sb != dst->i_sb)
2732 if (unlikely(f2fs_readonly(src->i_sb)))
2735 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2738 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2741 if (pos_out < 0 || pos_in < 0)
2745 if (pos_in == pos_out)
2747 if (pos_out > pos_in && pos_out < pos_in + len)
2754 if (!inode_trylock(dst))
2759 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2762 olen = len = src->i_size - pos_in;
2763 if (pos_in + len == src->i_size)
2764 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2770 dst_osize = dst->i_size;
2771 if (pos_out + olen > dst->i_size)
2772 dst_max_i_size = pos_out + olen;
2774 /* verify the end result is block aligned */
2775 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2776 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2777 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2780 ret = f2fs_convert_inline_inode(src);
2784 ret = f2fs_convert_inline_inode(dst);
2788 /* write out all dirty pages from offset */
2789 ret = filemap_write_and_wait_range(src->i_mapping,
2790 pos_in, pos_in + len);
2794 ret = filemap_write_and_wait_range(dst->i_mapping,
2795 pos_out, pos_out + len);
2799 f2fs_balance_fs(sbi, true);
2801 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2804 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2809 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2810 pos_out >> F2FS_BLKSIZE_BITS,
2811 len >> F2FS_BLKSIZE_BITS, false);
2815 f2fs_i_size_write(dst, dst_max_i_size);
2816 else if (dst_osize != dst->i_size)
2817 f2fs_i_size_write(dst, dst_osize);
2819 f2fs_unlock_op(sbi);
2822 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2824 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2833 static int __f2fs_ioc_move_range(struct file *filp,
2834 struct f2fs_move_range *range)
2839 if (!(filp->f_mode & FMODE_READ) ||
2840 !(filp->f_mode & FMODE_WRITE))
2843 dst = fdget(range->dst_fd);
2847 if (!(dst.file->f_mode & FMODE_WRITE)) {
2852 err = mnt_want_write_file(filp);
2856 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2857 range->pos_out, range->len);
2859 mnt_drop_write_file(filp);
2865 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2867 struct f2fs_move_range range;
2869 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2872 return __f2fs_ioc_move_range(filp, &range);
2875 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2877 struct inode *inode = file_inode(filp);
2878 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2879 struct sit_info *sm = SIT_I(sbi);
2880 unsigned int start_segno = 0, end_segno = 0;
2881 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2882 struct f2fs_flush_device range;
2885 if (!capable(CAP_SYS_ADMIN))
2888 if (f2fs_readonly(sbi->sb))
2891 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2894 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2898 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2899 __is_large_section(sbi)) {
2900 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2901 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2905 ret = mnt_want_write_file(filp);
2909 if (range.dev_num != 0)
2910 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2911 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2913 start_segno = sm->last_victim[FLUSH_DEVICE];
2914 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2915 start_segno = dev_start_segno;
2916 end_segno = min(start_segno + range.segments, dev_end_segno);
2918 while (start_segno < end_segno) {
2919 if (!down_write_trylock(&sbi->gc_lock)) {
2923 sm->last_victim[GC_CB] = end_segno + 1;
2924 sm->last_victim[GC_GREEDY] = end_segno + 1;
2925 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2926 ret = f2fs_gc(sbi, true, true, true, start_segno);
2934 mnt_drop_write_file(filp);
2938 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2940 struct inode *inode = file_inode(filp);
2941 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2943 /* Must validate to set it with SQLite behavior in Android. */
2944 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2946 return put_user(sb_feature, (u32 __user *)arg);
2950 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2952 struct dquot *transfer_to[MAXQUOTAS] = {};
2953 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2954 struct super_block *sb = sbi->sb;
2957 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2958 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2959 err = __dquot_transfer(inode, transfer_to);
2961 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2962 dqput(transfer_to[PRJQUOTA]);
2967 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
2969 struct f2fs_inode_info *fi = F2FS_I(inode);
2970 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2975 if (!f2fs_sb_has_project_quota(sbi)) {
2976 if (projid != F2FS_DEF_PROJID)
2982 if (!f2fs_has_extra_attr(inode))
2985 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2987 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2991 /* Is it quota file? Do not allow user to mess with it */
2992 if (IS_NOQUOTA(inode))
2995 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2997 return PTR_ERR(ipage);
2999 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3002 f2fs_put_page(ipage, 1);
3005 f2fs_put_page(ipage, 1);
3007 err = dquot_initialize(inode);
3012 err = f2fs_transfer_project_quota(inode, kprojid);
3016 F2FS_I(inode)->i_projid = kprojid;
3017 inode->i_ctime = current_time(inode);
3018 f2fs_mark_inode_dirty_sync(inode, true);
3020 f2fs_unlock_op(sbi);
3024 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3029 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3031 if (projid != F2FS_DEF_PROJID)
3037 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3039 struct inode *inode = d_inode(dentry);
3040 struct f2fs_inode_info *fi = F2FS_I(inode);
3041 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3043 if (IS_ENCRYPTED(inode))
3044 fsflags |= FS_ENCRYPT_FL;
3045 if (IS_VERITY(inode))
3046 fsflags |= FS_VERITY_FL;
3047 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3048 fsflags |= FS_INLINE_DATA_FL;
3049 if (is_inode_flag_set(inode, FI_PIN_FILE))
3050 fsflags |= FS_NOCOW_FL;
3052 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3054 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3055 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3060 int f2fs_fileattr_set(struct user_namespace *mnt_userns,
3061 struct dentry *dentry, struct fileattr *fa)
3063 struct inode *inode = d_inode(dentry);
3064 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3068 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3070 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3072 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3074 fsflags &= F2FS_SETTABLE_FS_FL;
3075 if (!fa->flags_valid)
3076 mask &= FS_COMMON_FL;
3078 iflags = f2fs_fsflags_to_iflags(fsflags);
3079 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3082 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3084 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3089 int f2fs_pin_file_control(struct inode *inode, bool inc)
3091 struct f2fs_inode_info *fi = F2FS_I(inode);
3092 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3094 /* Use i_gc_failures for normal file as a risk signal. */
3096 f2fs_i_gc_failures_write(inode,
3097 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3099 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3100 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3101 __func__, inode->i_ino,
3102 fi->i_gc_failures[GC_FAILURE_PIN]);
3103 clear_inode_flag(inode, FI_PIN_FILE);
3109 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3111 struct inode *inode = file_inode(filp);
3115 if (get_user(pin, (__u32 __user *)arg))
3118 if (!S_ISREG(inode->i_mode))
3121 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3124 ret = mnt_want_write_file(filp);
3130 if (f2fs_should_update_outplace(inode, NULL)) {
3136 clear_inode_flag(inode, FI_PIN_FILE);
3137 f2fs_i_gc_failures_write(inode, 0);
3141 if (f2fs_pin_file_control(inode, false)) {
3146 ret = f2fs_convert_inline_inode(inode);
3150 if (!f2fs_disable_compressed_file(inode)) {
3155 set_inode_flag(inode, FI_PIN_FILE);
3156 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3158 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3160 inode_unlock(inode);
3161 mnt_drop_write_file(filp);
3165 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3167 struct inode *inode = file_inode(filp);
3170 if (is_inode_flag_set(inode, FI_PIN_FILE))
3171 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3172 return put_user(pin, (u32 __user *)arg);
3175 int f2fs_precache_extents(struct inode *inode)
3177 struct f2fs_inode_info *fi = F2FS_I(inode);
3178 struct f2fs_map_blocks map;
3179 pgoff_t m_next_extent;
3183 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3187 map.m_next_pgofs = NULL;
3188 map.m_next_extent = &m_next_extent;
3189 map.m_seg_type = NO_CHECK_TYPE;
3190 map.m_may_create = false;
3191 end = max_file_blocks(inode);
3193 while (map.m_lblk < end) {
3194 map.m_len = end - map.m_lblk;
3196 down_write(&fi->i_gc_rwsem[WRITE]);
3197 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3198 up_write(&fi->i_gc_rwsem[WRITE]);
3202 map.m_lblk = m_next_extent;
3208 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3210 return f2fs_precache_extents(file_inode(filp));
3213 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3215 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3218 if (!capable(CAP_SYS_ADMIN))
3221 if (f2fs_readonly(sbi->sb))
3224 if (copy_from_user(&block_count, (void __user *)arg,
3225 sizeof(block_count)))
3228 return f2fs_resize_fs(sbi, block_count);
3231 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3233 struct inode *inode = file_inode(filp);
3235 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3237 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3238 f2fs_warn(F2FS_I_SB(inode),
3239 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3244 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3247 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3249 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3252 return fsverity_ioctl_measure(filp, (void __user *)arg);
3255 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3257 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3260 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3263 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3265 struct inode *inode = file_inode(filp);
3266 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3271 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3275 down_read(&sbi->sb_lock);
3276 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3277 ARRAY_SIZE(sbi->raw_super->volume_name),
3278 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3279 up_read(&sbi->sb_lock);
3281 if (copy_to_user((char __user *)arg, vbuf,
3282 min(FSLABEL_MAX, count)))
3289 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3291 struct inode *inode = file_inode(filp);
3292 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3296 if (!capable(CAP_SYS_ADMIN))
3299 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3301 return PTR_ERR(vbuf);
3303 err = mnt_want_write_file(filp);
3307 down_write(&sbi->sb_lock);
3309 memset(sbi->raw_super->volume_name, 0,
3310 sizeof(sbi->raw_super->volume_name));
3311 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3312 sbi->raw_super->volume_name,
3313 ARRAY_SIZE(sbi->raw_super->volume_name));
3315 err = f2fs_commit_super(sbi, false);
3317 up_write(&sbi->sb_lock);
3319 mnt_drop_write_file(filp);
3325 static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3327 struct inode *inode = file_inode(filp);
3330 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3333 if (!f2fs_compressed_file(inode))
3336 blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3337 return put_user(blocks, (u64 __user *)arg);
3340 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3342 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3343 unsigned int released_blocks = 0;
3344 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3348 for (i = 0; i < count; i++) {
3349 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3350 dn->ofs_in_node + i);
3352 if (!__is_valid_data_blkaddr(blkaddr))
3354 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3355 DATA_GENERIC_ENHANCE)))
3356 return -EFSCORRUPTED;
3360 int compr_blocks = 0;
3362 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3363 blkaddr = f2fs_data_blkaddr(dn);
3366 if (blkaddr == COMPRESS_ADDR)
3368 dn->ofs_in_node += cluster_size;
3372 if (__is_valid_data_blkaddr(blkaddr))
3375 if (blkaddr != NEW_ADDR)
3378 dn->data_blkaddr = NULL_ADDR;
3379 f2fs_set_data_blkaddr(dn);
3382 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3383 dec_valid_block_count(sbi, dn->inode,
3384 cluster_size - compr_blocks);
3386 released_blocks += cluster_size - compr_blocks;
3388 count -= cluster_size;
3391 return released_blocks;
3394 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3396 struct inode *inode = file_inode(filp);
3397 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3398 pgoff_t page_idx = 0, last_idx;
3399 unsigned int released_blocks = 0;
3403 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3406 if (!f2fs_compressed_file(inode))
3409 if (f2fs_readonly(sbi->sb))
3412 ret = mnt_want_write_file(filp);
3416 f2fs_balance_fs(F2FS_I_SB(inode), true);
3420 writecount = atomic_read(&inode->i_writecount);
3421 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3422 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3427 if (IS_IMMUTABLE(inode)) {
3432 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3436 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3437 f2fs_set_inode_flags(inode);
3438 inode->i_ctime = current_time(inode);
3439 f2fs_mark_inode_dirty_sync(inode, true);
3441 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
3444 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3445 down_write(&F2FS_I(inode)->i_mmap_sem);
3447 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3449 while (page_idx < last_idx) {
3450 struct dnode_of_data dn;
3451 pgoff_t end_offset, count;
3453 set_new_dnode(&dn, inode, NULL, NULL, 0);
3454 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3456 if (ret == -ENOENT) {
3457 page_idx = f2fs_get_next_page_offset(&dn,
3465 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3466 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3467 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3469 ret = release_compress_blocks(&dn, count);
3471 f2fs_put_dnode(&dn);
3477 released_blocks += ret;
3480 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3481 up_write(&F2FS_I(inode)->i_mmap_sem);
3483 inode_unlock(inode);
3485 mnt_drop_write_file(filp);
3488 ret = put_user(released_blocks, (u64 __user *)arg);
3489 } else if (released_blocks &&
3490 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3491 set_sbi_flag(sbi, SBI_NEED_FSCK);
3492 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3493 "iblocks=%llu, released=%u, compr_blocks=%u, "
3495 __func__, inode->i_ino, inode->i_blocks,
3497 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3503 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3505 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3506 unsigned int reserved_blocks = 0;
3507 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3511 for (i = 0; i < count; i++) {
3512 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3513 dn->ofs_in_node + i);
3515 if (!__is_valid_data_blkaddr(blkaddr))
3517 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3518 DATA_GENERIC_ENHANCE)))
3519 return -EFSCORRUPTED;
3523 int compr_blocks = 0;
3527 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3528 blkaddr = f2fs_data_blkaddr(dn);
3531 if (blkaddr == COMPRESS_ADDR)
3533 dn->ofs_in_node += cluster_size;
3537 if (__is_valid_data_blkaddr(blkaddr)) {
3542 dn->data_blkaddr = NEW_ADDR;
3543 f2fs_set_data_blkaddr(dn);
3546 reserved = cluster_size - compr_blocks;
3547 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3551 if (reserved != cluster_size - compr_blocks)
3554 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3556 reserved_blocks += reserved;
3558 count -= cluster_size;
3561 return reserved_blocks;
3564 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3566 struct inode *inode = file_inode(filp);
3567 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3568 pgoff_t page_idx = 0, last_idx;
3569 unsigned int reserved_blocks = 0;
3572 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3575 if (!f2fs_compressed_file(inode))
3578 if (f2fs_readonly(sbi->sb))
3581 ret = mnt_want_write_file(filp);
3585 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3588 f2fs_balance_fs(F2FS_I_SB(inode), true);
3592 if (!IS_IMMUTABLE(inode)) {
3597 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3598 down_write(&F2FS_I(inode)->i_mmap_sem);
3600 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3602 while (page_idx < last_idx) {
3603 struct dnode_of_data dn;
3604 pgoff_t end_offset, count;
3606 set_new_dnode(&dn, inode, NULL, NULL, 0);
3607 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3609 if (ret == -ENOENT) {
3610 page_idx = f2fs_get_next_page_offset(&dn,
3618 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3619 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3620 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3622 ret = reserve_compress_blocks(&dn, count);
3624 f2fs_put_dnode(&dn);
3630 reserved_blocks += ret;
3633 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3634 up_write(&F2FS_I(inode)->i_mmap_sem);
3637 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3638 f2fs_set_inode_flags(inode);
3639 inode->i_ctime = current_time(inode);
3640 f2fs_mark_inode_dirty_sync(inode, true);
3643 inode_unlock(inode);
3645 mnt_drop_write_file(filp);
3648 ret = put_user(reserved_blocks, (u64 __user *)arg);
3649 } else if (reserved_blocks &&
3650 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3651 set_sbi_flag(sbi, SBI_NEED_FSCK);
3652 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3653 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3655 __func__, inode->i_ino, inode->i_blocks,
3657 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3663 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3664 pgoff_t off, block_t block, block_t len, u32 flags)
3666 struct request_queue *q = bdev_get_queue(bdev);
3667 sector_t sector = SECTOR_FROM_BLOCK(block);
3668 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3674 if (flags & F2FS_TRIM_FILE_DISCARD)
3675 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3676 blk_queue_secure_erase(q) ?
3677 BLKDEV_DISCARD_SECURE : 0);
3679 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3680 if (IS_ENCRYPTED(inode))
3681 ret = fscrypt_zeroout_range(inode, off, block, len);
3683 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3690 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3692 struct inode *inode = file_inode(filp);
3693 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3694 struct address_space *mapping = inode->i_mapping;
3695 struct block_device *prev_bdev = NULL;
3696 struct f2fs_sectrim_range range;
3697 pgoff_t index, pg_end, prev_index = 0;
3698 block_t prev_block = 0, len = 0;
3700 bool to_end = false;
3703 if (!(filp->f_mode & FMODE_WRITE))
3706 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3710 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3711 !S_ISREG(inode->i_mode))
3714 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3715 !f2fs_hw_support_discard(sbi)) ||
3716 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3717 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3720 file_start_write(filp);
3723 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3724 range.start >= inode->i_size) {
3732 if (inode->i_size - range.start > range.len) {
3733 end_addr = range.start + range.len;
3735 end_addr = range.len == (u64)-1 ?
3736 sbi->sb->s_maxbytes : inode->i_size;
3740 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3741 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3746 index = F2FS_BYTES_TO_BLK(range.start);
3747 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3749 ret = f2fs_convert_inline_inode(inode);
3753 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3754 down_write(&F2FS_I(inode)->i_mmap_sem);
3756 ret = filemap_write_and_wait_range(mapping, range.start,
3757 to_end ? LLONG_MAX : end_addr - 1);
3761 truncate_inode_pages_range(mapping, range.start,
3762 to_end ? -1 : end_addr - 1);
3764 while (index < pg_end) {
3765 struct dnode_of_data dn;
3766 pgoff_t end_offset, count;
3769 set_new_dnode(&dn, inode, NULL, NULL, 0);
3770 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3772 if (ret == -ENOENT) {
3773 index = f2fs_get_next_page_offset(&dn, index);
3779 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3780 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3781 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3782 struct block_device *cur_bdev;
3783 block_t blkaddr = f2fs_data_blkaddr(&dn);
3785 if (!__is_valid_data_blkaddr(blkaddr))
3788 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3789 DATA_GENERIC_ENHANCE)) {
3790 ret = -EFSCORRUPTED;
3791 f2fs_put_dnode(&dn);
3795 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3796 if (f2fs_is_multi_device(sbi)) {
3797 int di = f2fs_target_device_index(sbi, blkaddr);
3799 blkaddr -= FDEV(di).start_blk;
3803 if (prev_bdev == cur_bdev &&
3804 index == prev_index + len &&
3805 blkaddr == prev_block + len) {
3808 ret = f2fs_secure_erase(prev_bdev,
3809 inode, prev_index, prev_block,
3812 f2fs_put_dnode(&dn);
3821 prev_bdev = cur_bdev;
3823 prev_block = blkaddr;
3828 f2fs_put_dnode(&dn);
3830 if (fatal_signal_pending(current)) {
3838 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3839 prev_block, len, range.flags);
3841 up_write(&F2FS_I(inode)->i_mmap_sem);
3842 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3844 inode_unlock(inode);
3845 file_end_write(filp);
3850 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3852 struct inode *inode = file_inode(filp);
3853 struct f2fs_comp_option option;
3855 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3858 inode_lock_shared(inode);
3860 if (!f2fs_compressed_file(inode)) {
3861 inode_unlock_shared(inode);
3865 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3866 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3868 inode_unlock_shared(inode);
3870 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3877 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3879 struct inode *inode = file_inode(filp);
3880 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3881 struct f2fs_comp_option option;
3884 if (!f2fs_sb_has_compression(sbi))
3887 if (!(filp->f_mode & FMODE_WRITE))
3890 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3894 if (!f2fs_compressed_file(inode) ||
3895 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3896 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3897 option.algorithm >= COMPRESS_MAX)
3900 file_start_write(filp);
3903 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
3908 if (inode->i_size != 0) {
3913 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
3914 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
3915 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
3916 f2fs_mark_inode_dirty_sync(inode, true);
3918 if (!f2fs_is_compress_backend_ready(inode))
3919 f2fs_warn(sbi, "compression algorithm is successfully set, "
3920 "but current kernel doesn't support this algorithm.");
3922 inode_unlock(inode);
3923 file_end_write(filp);
3928 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
3930 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
3931 struct address_space *mapping = inode->i_mapping;
3933 pgoff_t redirty_idx = page_idx;
3934 int i, page_len = 0, ret = 0;
3936 page_cache_ra_unbounded(&ractl, len, 0);
3938 for (i = 0; i < len; i++, page_idx++) {
3939 page = read_cache_page(mapping, page_idx, NULL, NULL);
3941 ret = PTR_ERR(page);
3947 for (i = 0; i < page_len; i++, redirty_idx++) {
3948 page = find_lock_page(mapping, redirty_idx);
3953 set_page_dirty(page);
3954 f2fs_put_page(page, 1);
3955 f2fs_put_page(page, 0);
3961 static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
3963 struct inode *inode = file_inode(filp);
3964 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3965 struct f2fs_inode_info *fi = F2FS_I(inode);
3966 pgoff_t page_idx = 0, last_idx;
3967 unsigned int blk_per_seg = sbi->blocks_per_seg;
3968 int cluster_size = F2FS_I(inode)->i_cluster_size;
3971 if (!f2fs_sb_has_compression(sbi) ||
3972 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
3975 if (!(filp->f_mode & FMODE_WRITE))
3978 if (!f2fs_compressed_file(inode))
3981 f2fs_balance_fs(F2FS_I_SB(inode), true);
3983 file_start_write(filp);
3986 if (!f2fs_is_compress_backend_ready(inode)) {
3991 if (f2fs_is_mmap_file(inode)) {
3996 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4000 if (!atomic_read(&fi->i_compr_blocks))
4003 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4005 count = last_idx - page_idx;
4007 int len = min(cluster_size, count);
4009 ret = redirty_blocks(inode, page_idx, len);
4013 if (get_dirty_pages(inode) >= blk_per_seg)
4014 filemap_fdatawrite(inode->i_mapping);
4021 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4025 f2fs_warn(sbi, "%s: The file might be partially decompressed "
4026 "(errno=%d). Please delete the file.\n",
4029 inode_unlock(inode);
4030 file_end_write(filp);
4035 static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4037 struct inode *inode = file_inode(filp);
4038 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4039 pgoff_t page_idx = 0, last_idx;
4040 unsigned int blk_per_seg = sbi->blocks_per_seg;
4041 int cluster_size = F2FS_I(inode)->i_cluster_size;
4044 if (!f2fs_sb_has_compression(sbi) ||
4045 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4048 if (!(filp->f_mode & FMODE_WRITE))
4051 if (!f2fs_compressed_file(inode))
4054 f2fs_balance_fs(F2FS_I_SB(inode), true);
4056 file_start_write(filp);
4059 if (!f2fs_is_compress_backend_ready(inode)) {
4064 if (f2fs_is_mmap_file(inode)) {
4069 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4073 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4075 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4077 count = last_idx - page_idx;
4079 int len = min(cluster_size, count);
4081 ret = redirty_blocks(inode, page_idx, len);
4085 if (get_dirty_pages(inode) >= blk_per_seg)
4086 filemap_fdatawrite(inode->i_mapping);
4093 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4096 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4099 f2fs_warn(sbi, "%s: The file might be partially compressed "
4100 "(errno=%d). Please delete the file.\n",
4103 inode_unlock(inode);
4104 file_end_write(filp);
4109 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4112 case FS_IOC_GETVERSION:
4113 return f2fs_ioc_getversion(filp, arg);
4114 case F2FS_IOC_START_ATOMIC_WRITE:
4115 return f2fs_ioc_start_atomic_write(filp);
4116 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4117 return f2fs_ioc_commit_atomic_write(filp);
4118 case F2FS_IOC_START_VOLATILE_WRITE:
4119 return f2fs_ioc_start_volatile_write(filp);
4120 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4121 return f2fs_ioc_release_volatile_write(filp);
4122 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4123 return f2fs_ioc_abort_volatile_write(filp);
4124 case F2FS_IOC_SHUTDOWN:
4125 return f2fs_ioc_shutdown(filp, arg);
4127 return f2fs_ioc_fitrim(filp, arg);
4128 case FS_IOC_SET_ENCRYPTION_POLICY:
4129 return f2fs_ioc_set_encryption_policy(filp, arg);
4130 case FS_IOC_GET_ENCRYPTION_POLICY:
4131 return f2fs_ioc_get_encryption_policy(filp, arg);
4132 case FS_IOC_GET_ENCRYPTION_PWSALT:
4133 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4134 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4135 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4136 case FS_IOC_ADD_ENCRYPTION_KEY:
4137 return f2fs_ioc_add_encryption_key(filp, arg);
4138 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4139 return f2fs_ioc_remove_encryption_key(filp, arg);
4140 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4141 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4142 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4143 return f2fs_ioc_get_encryption_key_status(filp, arg);
4144 case FS_IOC_GET_ENCRYPTION_NONCE:
4145 return f2fs_ioc_get_encryption_nonce(filp, arg);
4146 case F2FS_IOC_GARBAGE_COLLECT:
4147 return f2fs_ioc_gc(filp, arg);
4148 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4149 return f2fs_ioc_gc_range(filp, arg);
4150 case F2FS_IOC_WRITE_CHECKPOINT:
4151 return f2fs_ioc_write_checkpoint(filp, arg);
4152 case F2FS_IOC_DEFRAGMENT:
4153 return f2fs_ioc_defragment(filp, arg);
4154 case F2FS_IOC_MOVE_RANGE:
4155 return f2fs_ioc_move_range(filp, arg);
4156 case F2FS_IOC_FLUSH_DEVICE:
4157 return f2fs_ioc_flush_device(filp, arg);
4158 case F2FS_IOC_GET_FEATURES:
4159 return f2fs_ioc_get_features(filp, arg);
4160 case F2FS_IOC_GET_PIN_FILE:
4161 return f2fs_ioc_get_pin_file(filp, arg);
4162 case F2FS_IOC_SET_PIN_FILE:
4163 return f2fs_ioc_set_pin_file(filp, arg);
4164 case F2FS_IOC_PRECACHE_EXTENTS:
4165 return f2fs_ioc_precache_extents(filp, arg);
4166 case F2FS_IOC_RESIZE_FS:
4167 return f2fs_ioc_resize_fs(filp, arg);
4168 case FS_IOC_ENABLE_VERITY:
4169 return f2fs_ioc_enable_verity(filp, arg);
4170 case FS_IOC_MEASURE_VERITY:
4171 return f2fs_ioc_measure_verity(filp, arg);
4172 case FS_IOC_READ_VERITY_METADATA:
4173 return f2fs_ioc_read_verity_metadata(filp, arg);
4174 case FS_IOC_GETFSLABEL:
4175 return f2fs_ioc_getfslabel(filp, arg);
4176 case FS_IOC_SETFSLABEL:
4177 return f2fs_ioc_setfslabel(filp, arg);
4178 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4179 return f2fs_get_compress_blocks(filp, arg);
4180 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4181 return f2fs_release_compress_blocks(filp, arg);
4182 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4183 return f2fs_reserve_compress_blocks(filp, arg);
4184 case F2FS_IOC_SEC_TRIM_FILE:
4185 return f2fs_sec_trim_file(filp, arg);
4186 case F2FS_IOC_GET_COMPRESS_OPTION:
4187 return f2fs_ioc_get_compress_option(filp, arg);
4188 case F2FS_IOC_SET_COMPRESS_OPTION:
4189 return f2fs_ioc_set_compress_option(filp, arg);
4190 case F2FS_IOC_DECOMPRESS_FILE:
4191 return f2fs_ioc_decompress_file(filp, arg);
4192 case F2FS_IOC_COMPRESS_FILE:
4193 return f2fs_ioc_compress_file(filp, arg);
4199 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4201 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4203 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4206 return __f2fs_ioctl(filp, cmd, arg);
4209 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4211 struct file *file = iocb->ki_filp;
4212 struct inode *inode = file_inode(file);
4215 if (!f2fs_is_compress_backend_ready(inode))
4218 ret = generic_file_read_iter(iocb, iter);
4221 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4226 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4228 struct file *file = iocb->ki_filp;
4229 struct inode *inode = file_inode(file);
4232 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4237 if (!f2fs_is_compress_backend_ready(inode)) {
4242 if (iocb->ki_flags & IOCB_NOWAIT) {
4243 if (!inode_trylock(inode)) {
4251 if (unlikely(IS_IMMUTABLE(inode))) {
4256 ret = generic_write_checks(iocb, from);
4258 bool preallocated = false;
4259 size_t target_size = 0;
4262 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4263 set_inode_flag(inode, FI_NO_PREALLOC);
4265 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4266 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
4267 iov_iter_count(from)) ||
4268 f2fs_has_inline_data(inode) ||
4269 f2fs_force_buffered_io(inode, iocb, from)) {
4270 clear_inode_flag(inode, FI_NO_PREALLOC);
4271 inode_unlock(inode);
4278 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4281 if (iocb->ki_flags & IOCB_DIRECT) {
4283 * Convert inline data for Direct I/O before entering
4286 err = f2fs_convert_inline_inode(inode);
4290 * If force_buffere_io() is true, we have to allocate
4291 * blocks all the time, since f2fs_direct_IO will fall
4292 * back to buffered IO.
4294 if (!f2fs_force_buffered_io(inode, iocb, from) &&
4295 allow_outplace_dio(inode, iocb, from))
4298 preallocated = true;
4299 target_size = iocb->ki_pos + iov_iter_count(from);
4301 err = f2fs_preallocate_blocks(iocb, from);
4304 clear_inode_flag(inode, FI_NO_PREALLOC);
4305 inode_unlock(inode);
4310 ret = __generic_file_write_iter(iocb, from);
4311 clear_inode_flag(inode, FI_NO_PREALLOC);
4313 /* if we couldn't write data, we should deallocate blocks. */
4314 if (preallocated && i_size_read(inode) < target_size) {
4315 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4316 down_write(&F2FS_I(inode)->i_mmap_sem);
4317 f2fs_truncate(inode);
4318 up_write(&F2FS_I(inode)->i_mmap_sem);
4319 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4323 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
4326 inode_unlock(inode);
4328 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4329 iov_iter_count(from), ret);
4331 ret = generic_write_sync(iocb, ret);
4335 #ifdef CONFIG_COMPAT
4336 struct compat_f2fs_gc_range {
4341 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4342 struct compat_f2fs_gc_range)
4344 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4346 struct compat_f2fs_gc_range __user *urange;
4347 struct f2fs_gc_range range;
4350 urange = compat_ptr(arg);
4351 err = get_user(range.sync, &urange->sync);
4352 err |= get_user(range.start, &urange->start);
4353 err |= get_user(range.len, &urange->len);
4357 return __f2fs_ioc_gc_range(file, &range);
4360 struct compat_f2fs_move_range {
4366 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4367 struct compat_f2fs_move_range)
4369 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4371 struct compat_f2fs_move_range __user *urange;
4372 struct f2fs_move_range range;
4375 urange = compat_ptr(arg);
4376 err = get_user(range.dst_fd, &urange->dst_fd);
4377 err |= get_user(range.pos_in, &urange->pos_in);
4378 err |= get_user(range.pos_out, &urange->pos_out);
4379 err |= get_user(range.len, &urange->len);
4383 return __f2fs_ioc_move_range(file, &range);
4386 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4388 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4390 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4394 case FS_IOC32_GETVERSION:
4395 cmd = FS_IOC_GETVERSION;
4397 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4398 return f2fs_compat_ioc_gc_range(file, arg);
4399 case F2FS_IOC32_MOVE_RANGE:
4400 return f2fs_compat_ioc_move_range(file, arg);
4401 case F2FS_IOC_START_ATOMIC_WRITE:
4402 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4403 case F2FS_IOC_START_VOLATILE_WRITE:
4404 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4405 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4406 case F2FS_IOC_SHUTDOWN:
4408 case FS_IOC_SET_ENCRYPTION_POLICY:
4409 case FS_IOC_GET_ENCRYPTION_PWSALT:
4410 case FS_IOC_GET_ENCRYPTION_POLICY:
4411 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4412 case FS_IOC_ADD_ENCRYPTION_KEY:
4413 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4414 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4415 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4416 case FS_IOC_GET_ENCRYPTION_NONCE:
4417 case F2FS_IOC_GARBAGE_COLLECT:
4418 case F2FS_IOC_WRITE_CHECKPOINT:
4419 case F2FS_IOC_DEFRAGMENT:
4420 case F2FS_IOC_FLUSH_DEVICE:
4421 case F2FS_IOC_GET_FEATURES:
4422 case F2FS_IOC_GET_PIN_FILE:
4423 case F2FS_IOC_SET_PIN_FILE:
4424 case F2FS_IOC_PRECACHE_EXTENTS:
4425 case F2FS_IOC_RESIZE_FS:
4426 case FS_IOC_ENABLE_VERITY:
4427 case FS_IOC_MEASURE_VERITY:
4428 case FS_IOC_READ_VERITY_METADATA:
4429 case FS_IOC_GETFSLABEL:
4430 case FS_IOC_SETFSLABEL:
4431 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4432 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4433 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4434 case F2FS_IOC_SEC_TRIM_FILE:
4435 case F2FS_IOC_GET_COMPRESS_OPTION:
4436 case F2FS_IOC_SET_COMPRESS_OPTION:
4437 case F2FS_IOC_DECOMPRESS_FILE:
4438 case F2FS_IOC_COMPRESS_FILE:
4441 return -ENOIOCTLCMD;
4443 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4447 const struct file_operations f2fs_file_operations = {
4448 .llseek = f2fs_llseek,
4449 .read_iter = f2fs_file_read_iter,
4450 .write_iter = f2fs_file_write_iter,
4451 .open = f2fs_file_open,
4452 .release = f2fs_release_file,
4453 .mmap = f2fs_file_mmap,
4454 .flush = f2fs_file_flush,
4455 .fsync = f2fs_sync_file,
4456 .fallocate = f2fs_fallocate,
4457 .unlocked_ioctl = f2fs_ioctl,
4458 #ifdef CONFIG_COMPAT
4459 .compat_ioctl = f2fs_compat_ioctl,
4461 .splice_read = generic_file_splice_read,
4462 .splice_write = iter_file_splice_write,