4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/stat.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/blkdev.h>
17 #include <linux/falloc.h>
18 #include <linux/types.h>
19 #include <linux/compat.h>
20 #include <linux/uaccess.h>
21 #include <linux/mount.h>
22 #include <linux/pagevec.h>
23 #include <linux/uio.h>
24 #include <linux/uuid.h>
25 #include <linux/file.h>
34 #include <trace/events/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);
48 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
50 struct page *page = vmf->page;
51 struct inode *inode = file_inode(vmf->vma->vm_file);
52 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
53 struct dnode_of_data dn;
56 if (unlikely(f2fs_cp_error(sbi))) {
61 sb_start_pagefault(inode->i_sb);
63 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
65 /* block allocation */
67 set_new_dnode(&dn, inode, NULL, NULL, 0);
68 err = f2fs_reserve_block(&dn, page->index);
76 f2fs_balance_fs(sbi, dn.node_changed);
78 file_update_time(vmf->vma->vm_file);
79 down_read(&F2FS_I(inode)->i_mmap_sem);
81 if (unlikely(page->mapping != inode->i_mapping ||
82 page_offset(page) > i_size_read(inode) ||
83 !PageUptodate(page))) {
90 * check to see if the page is mapped already (no holes)
92 if (PageMappedToDisk(page))
95 /* page is wholly or partially inside EOF */
96 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
100 offset = i_size_read(inode) & ~PAGE_MASK;
101 zero_user_segment(page, offset, PAGE_SIZE);
103 set_page_dirty(page);
104 if (!PageUptodate(page))
105 SetPageUptodate(page);
107 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
109 trace_f2fs_vm_page_mkwrite(page, DATA);
112 f2fs_wait_on_page_writeback(page, DATA, false);
114 /* wait for GCed page writeback via META_MAPPING */
115 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
118 up_read(&F2FS_I(inode)->i_mmap_sem);
120 sb_end_pagefault(inode->i_sb);
121 f2fs_update_time(sbi, REQ_TIME);
123 return block_page_mkwrite_return(err);
126 static const struct vm_operations_struct f2fs_file_vm_ops = {
127 .fault = f2fs_filemap_fault,
128 .map_pages = filemap_map_pages,
129 .page_mkwrite = f2fs_vm_page_mkwrite,
132 static int get_parent_ino(struct inode *inode, nid_t *pino)
134 struct dentry *dentry;
136 inode = igrab(inode);
137 dentry = d_find_any_alias(inode);
142 *pino = parent_ino(dentry);
147 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
149 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
150 enum cp_reason_type cp_reason = CP_NO_NEEDED;
152 if (!S_ISREG(inode->i_mode))
153 cp_reason = CP_NON_REGULAR;
154 else if (inode->i_nlink != 1)
155 cp_reason = CP_HARDLINK;
156 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
157 cp_reason = CP_SB_NEED_CP;
158 else if (file_wrong_pino(inode))
159 cp_reason = CP_WRONG_PINO;
160 else if (!f2fs_space_for_roll_forward(sbi))
161 cp_reason = CP_NO_SPC_ROLL;
162 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
163 cp_reason = CP_NODE_NEED_CP;
164 else if (test_opt(sbi, FASTBOOT))
165 cp_reason = CP_FASTBOOT_MODE;
166 else if (F2FS_OPTION(sbi).active_logs == 2)
167 cp_reason = CP_SPEC_LOG_NUM;
168 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
169 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
170 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
172 cp_reason = CP_RECOVER_DIR;
177 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
179 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
181 /* But we need to avoid that there are some inode updates */
182 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
188 static void try_to_fix_pino(struct inode *inode)
190 struct f2fs_inode_info *fi = F2FS_I(inode);
193 down_write(&fi->i_sem);
194 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
195 get_parent_ino(inode, &pino)) {
196 f2fs_i_pino_write(inode, pino);
197 file_got_pino(inode);
199 up_write(&fi->i_sem);
202 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
203 int datasync, bool atomic)
205 struct inode *inode = file->f_mapping->host;
206 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
207 nid_t ino = inode->i_ino;
209 enum cp_reason_type cp_reason = 0;
210 struct writeback_control wbc = {
211 .sync_mode = WB_SYNC_ALL,
212 .nr_to_write = LONG_MAX,
215 unsigned int seq_id = 0;
217 if (unlikely(f2fs_readonly(inode->i_sb)))
220 trace_f2fs_sync_file_enter(inode);
222 if (S_ISDIR(inode->i_mode))
225 /* if fdatasync is triggered, let's do in-place-update */
226 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
227 set_inode_flag(inode, FI_NEED_IPU);
228 ret = file_write_and_wait_range(file, start, end);
229 clear_inode_flag(inode, FI_NEED_IPU);
232 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
236 /* if the inode is dirty, let's recover all the time */
237 if (!f2fs_skip_inode_update(inode, datasync)) {
238 f2fs_write_inode(inode, NULL);
243 * if there is no written data, don't waste time to write recovery info.
245 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
246 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
248 /* it may call write_inode just prior to fsync */
249 if (need_inode_page_update(sbi, ino))
252 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
253 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
259 * Both of fdatasync() and fsync() are able to be recovered from
262 down_read(&F2FS_I(inode)->i_sem);
263 cp_reason = need_do_checkpoint(inode);
264 up_read(&F2FS_I(inode)->i_sem);
267 /* all the dirty node pages should be flushed for POR */
268 ret = f2fs_sync_fs(inode->i_sb, 1);
271 * We've secured consistency through sync_fs. Following pino
272 * will be used only for fsynced inodes after checkpoint.
274 try_to_fix_pino(inode);
275 clear_inode_flag(inode, FI_APPEND_WRITE);
276 clear_inode_flag(inode, FI_UPDATE_WRITE);
280 atomic_inc(&sbi->wb_sync_req[NODE]);
281 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
282 atomic_dec(&sbi->wb_sync_req[NODE]);
286 /* if cp_error was enabled, we should avoid infinite loop */
287 if (unlikely(f2fs_cp_error(sbi))) {
292 if (f2fs_need_inode_block_update(sbi, ino)) {
293 f2fs_mark_inode_dirty_sync(inode, true);
294 f2fs_write_inode(inode, NULL);
299 * If it's atomic_write, it's just fine to keep write ordering. So
300 * here we don't need to wait for node write completion, since we use
301 * node chain which serializes node blocks. If one of node writes are
302 * reordered, we can see simply broken chain, resulting in stopping
303 * roll-forward recovery. It means we'll recover all or none node blocks
307 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
312 /* once recovery info is written, don't need to tack this */
313 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
314 clear_inode_flag(inode, FI_APPEND_WRITE);
316 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
317 ret = f2fs_issue_flush(sbi, inode->i_ino);
319 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
320 clear_inode_flag(inode, FI_UPDATE_WRITE);
321 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
323 f2fs_update_time(sbi, REQ_TIME);
325 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
326 f2fs_trace_ios(NULL, 1);
330 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
332 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
334 return f2fs_do_sync_file(file, start, end, datasync, false);
337 static pgoff_t __get_first_dirty_index(struct address_space *mapping,
338 pgoff_t pgofs, int whence)
343 if (whence != SEEK_DATA)
346 /* find first dirty page index */
347 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
356 static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
357 pgoff_t dirty, pgoff_t pgofs, int whence)
361 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
362 is_valid_data_blkaddr(sbi, blkaddr))
366 if (blkaddr == NULL_ADDR)
373 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
375 struct inode *inode = file->f_mapping->host;
376 loff_t maxbytes = inode->i_sb->s_maxbytes;
377 struct dnode_of_data dn;
378 pgoff_t pgofs, end_offset, dirty;
379 loff_t data_ofs = offset;
385 isize = i_size_read(inode);
389 /* handle inline data case */
390 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
391 if (whence == SEEK_HOLE)
396 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
398 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
400 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
401 set_new_dnode(&dn, inode, NULL, NULL, 0);
402 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
403 if (err && err != -ENOENT) {
405 } else if (err == -ENOENT) {
406 /* direct node does not exists */
407 if (whence == SEEK_DATA) {
408 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
415 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
417 /* find data/hole in dnode block */
418 for (; dn.ofs_in_node < end_offset;
419 dn.ofs_in_node++, pgofs++,
420 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
423 blkaddr = datablock_addr(dn.inode,
424 dn.node_page, dn.ofs_in_node);
426 if (__is_valid_data_blkaddr(blkaddr) &&
427 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
428 blkaddr, DATA_GENERIC)) {
433 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
442 if (whence == SEEK_DATA)
445 if (whence == SEEK_HOLE && data_ofs > isize)
448 return vfs_setpos(file, data_ofs, maxbytes);
454 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
456 struct inode *inode = file->f_mapping->host;
457 loff_t maxbytes = inode->i_sb->s_maxbytes;
463 return generic_file_llseek_size(file, offset, whence,
464 maxbytes, i_size_read(inode));
469 return f2fs_seek_block(file, offset, whence);
475 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
477 struct inode *inode = file_inode(file);
480 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
483 /* we don't need to use inline_data strictly */
484 err = f2fs_convert_inline_inode(inode);
489 vma->vm_ops = &f2fs_file_vm_ops;
493 static int f2fs_file_open(struct inode *inode, struct file *filp)
495 int err = fscrypt_file_open(inode, filp);
500 filp->f_mode |= FMODE_NOWAIT;
502 return dquot_file_open(inode, filp);
505 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
507 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
508 struct f2fs_node *raw_node;
509 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
513 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
514 base = get_extra_isize(dn->inode);
516 raw_node = F2FS_NODE(dn->node_page);
517 addr = blkaddr_in_node(raw_node) + base + ofs;
519 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
520 block_t blkaddr = le32_to_cpu(*addr);
522 if (blkaddr == NULL_ADDR)
525 dn->data_blkaddr = NULL_ADDR;
526 f2fs_set_data_blkaddr(dn);
528 if (__is_valid_data_blkaddr(blkaddr) &&
529 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
532 f2fs_invalidate_blocks(sbi, blkaddr);
533 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
534 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
541 * once we invalidate valid blkaddr in range [ofs, ofs + count],
542 * we will invalidate all blkaddr in the whole range.
544 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
546 f2fs_update_extent_cache_range(dn, fofs, 0, len);
547 dec_valid_block_count(sbi, dn->inode, nr_free);
549 dn->ofs_in_node = ofs;
551 f2fs_update_time(sbi, REQ_TIME);
552 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
553 dn->ofs_in_node, nr_free);
556 void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
558 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
561 static int truncate_partial_data_page(struct inode *inode, u64 from,
564 loff_t offset = from & (PAGE_SIZE - 1);
565 pgoff_t index = from >> PAGE_SHIFT;
566 struct address_space *mapping = inode->i_mapping;
569 if (!offset && !cache_only)
573 page = find_lock_page(mapping, index);
574 if (page && PageUptodate(page))
576 f2fs_put_page(page, 1);
580 page = f2fs_get_lock_data_page(inode, index, true);
582 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
584 f2fs_wait_on_page_writeback(page, DATA, true);
585 zero_user(page, offset, PAGE_SIZE - offset);
587 /* An encrypted inode should have a key and truncate the last page. */
588 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
590 set_page_dirty(page);
591 f2fs_put_page(page, 1);
595 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
597 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
598 struct dnode_of_data dn;
600 int count = 0, err = 0;
602 bool truncate_page = false;
604 trace_f2fs_truncate_blocks_enter(inode, from);
606 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
608 if (free_from >= sbi->max_file_blocks)
614 ipage = f2fs_get_node_page(sbi, inode->i_ino);
616 err = PTR_ERR(ipage);
620 if (f2fs_has_inline_data(inode)) {
621 f2fs_truncate_inline_inode(inode, ipage, from);
622 f2fs_put_page(ipage, 1);
623 truncate_page = true;
627 set_new_dnode(&dn, inode, ipage, NULL, 0);
628 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
635 count = ADDRS_PER_PAGE(dn.node_page, inode);
637 count -= dn.ofs_in_node;
638 f2fs_bug_on(sbi, count < 0);
640 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
641 f2fs_truncate_data_blocks_range(&dn, count);
647 err = f2fs_truncate_inode_blocks(inode, free_from);
652 /* lastly zero out the first data page */
654 err = truncate_partial_data_page(inode, from, truncate_page);
656 trace_f2fs_truncate_blocks_exit(inode, err);
660 int f2fs_truncate(struct inode *inode)
664 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
667 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
668 S_ISLNK(inode->i_mode)))
671 trace_f2fs_truncate(inode);
673 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
674 f2fs_show_injection_info(FAULT_TRUNCATE);
678 err = dquot_initialize(inode);
682 /* we should check inline_data size */
683 if (!f2fs_may_inline_data(inode)) {
684 err = f2fs_convert_inline_inode(inode);
689 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
693 inode->i_mtime = inode->i_ctime = current_time(inode);
694 f2fs_mark_inode_dirty_sync(inode, false);
698 int f2fs_getattr(const struct path *path, struct kstat *stat,
699 u32 request_mask, unsigned int query_flags)
701 struct inode *inode = d_inode(path->dentry);
702 struct f2fs_inode_info *fi = F2FS_I(inode);
703 struct f2fs_inode *ri;
706 if (f2fs_has_extra_attr(inode) &&
707 f2fs_sb_has_inode_crtime(inode->i_sb) &&
708 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
709 stat->result_mask |= STATX_BTIME;
710 stat->btime.tv_sec = fi->i_crtime.tv_sec;
711 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
714 flags = fi->i_flags & F2FS_FL_USER_VISIBLE;
715 if (flags & F2FS_APPEND_FL)
716 stat->attributes |= STATX_ATTR_APPEND;
717 if (flags & F2FS_COMPR_FL)
718 stat->attributes |= STATX_ATTR_COMPRESSED;
719 if (f2fs_encrypted_inode(inode))
720 stat->attributes |= STATX_ATTR_ENCRYPTED;
721 if (flags & F2FS_IMMUTABLE_FL)
722 stat->attributes |= STATX_ATTR_IMMUTABLE;
723 if (flags & F2FS_NODUMP_FL)
724 stat->attributes |= STATX_ATTR_NODUMP;
726 stat->attributes_mask |= (STATX_ATTR_APPEND |
727 STATX_ATTR_COMPRESSED |
728 STATX_ATTR_ENCRYPTED |
729 STATX_ATTR_IMMUTABLE |
732 generic_fillattr(inode, stat);
734 /* we need to show initial sectors used for inline_data/dentries */
735 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
736 f2fs_has_inline_dentry(inode))
737 stat->blocks += (stat->size + 511) >> 9;
742 #ifdef CONFIG_F2FS_FS_POSIX_ACL
743 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
745 unsigned int ia_valid = attr->ia_valid;
747 if (ia_valid & ATTR_UID)
748 inode->i_uid = attr->ia_uid;
749 if (ia_valid & ATTR_GID)
750 inode->i_gid = attr->ia_gid;
751 if (ia_valid & ATTR_ATIME)
752 inode->i_atime = timespec64_trunc(attr->ia_atime,
753 inode->i_sb->s_time_gran);
754 if (ia_valid & ATTR_MTIME)
755 inode->i_mtime = timespec64_trunc(attr->ia_mtime,
756 inode->i_sb->s_time_gran);
757 if (ia_valid & ATTR_CTIME)
758 inode->i_ctime = timespec64_trunc(attr->ia_ctime,
759 inode->i_sb->s_time_gran);
760 if (ia_valid & ATTR_MODE) {
761 umode_t mode = attr->ia_mode;
763 if (!in_group_p(inode->i_gid) &&
764 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
766 set_acl_inode(inode, mode);
770 #define __setattr_copy setattr_copy
773 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
775 struct inode *inode = d_inode(dentry);
778 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
781 err = setattr_prepare(dentry, attr);
785 err = fscrypt_prepare_setattr(dentry, attr);
789 if (is_quota_modification(inode, attr)) {
790 err = dquot_initialize(inode);
794 if ((attr->ia_valid & ATTR_UID &&
795 !uid_eq(attr->ia_uid, inode->i_uid)) ||
796 (attr->ia_valid & ATTR_GID &&
797 !gid_eq(attr->ia_gid, inode->i_gid))) {
798 err = dquot_transfer(inode, attr);
803 if (attr->ia_valid & ATTR_SIZE) {
804 bool to_smaller = (attr->ia_size <= i_size_read(inode));
806 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
807 down_write(&F2FS_I(inode)->i_mmap_sem);
809 truncate_setsize(inode, attr->ia_size);
812 err = f2fs_truncate(inode);
814 * do not trim all blocks after i_size if target size is
815 * larger than i_size.
817 up_write(&F2FS_I(inode)->i_mmap_sem);
818 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
824 /* should convert inline inode here */
825 if (!f2fs_may_inline_data(inode)) {
826 err = f2fs_convert_inline_inode(inode);
830 inode->i_mtime = inode->i_ctime = current_time(inode);
833 down_write(&F2FS_I(inode)->i_sem);
834 F2FS_I(inode)->last_disk_size = i_size_read(inode);
835 up_write(&F2FS_I(inode)->i_sem);
838 __setattr_copy(inode, attr);
840 if (attr->ia_valid & ATTR_MODE) {
841 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
842 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
843 inode->i_mode = F2FS_I(inode)->i_acl_mode;
844 clear_inode_flag(inode, FI_ACL_MODE);
848 /* file size may changed here */
849 f2fs_mark_inode_dirty_sync(inode, true);
851 /* inode change will produce dirty node pages flushed by checkpoint */
852 f2fs_balance_fs(F2FS_I_SB(inode), true);
857 const struct inode_operations f2fs_file_inode_operations = {
858 .getattr = f2fs_getattr,
859 .setattr = f2fs_setattr,
860 .get_acl = f2fs_get_acl,
861 .set_acl = f2fs_set_acl,
862 #ifdef CONFIG_F2FS_FS_XATTR
863 .listxattr = f2fs_listxattr,
865 .fiemap = f2fs_fiemap,
868 static int fill_zero(struct inode *inode, pgoff_t index,
869 loff_t start, loff_t len)
871 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
877 f2fs_balance_fs(sbi, true);
880 page = f2fs_get_new_data_page(inode, NULL, index, false);
884 return PTR_ERR(page);
886 f2fs_wait_on_page_writeback(page, DATA, true);
887 zero_user(page, start, len);
888 set_page_dirty(page);
889 f2fs_put_page(page, 1);
893 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
897 while (pg_start < pg_end) {
898 struct dnode_of_data dn;
899 pgoff_t end_offset, count;
901 set_new_dnode(&dn, inode, NULL, NULL, 0);
902 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
904 if (err == -ENOENT) {
905 pg_start = f2fs_get_next_page_offset(&dn,
912 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
913 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
915 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
917 f2fs_truncate_data_blocks_range(&dn, count);
925 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
927 pgoff_t pg_start, pg_end;
928 loff_t off_start, off_end;
931 ret = f2fs_convert_inline_inode(inode);
935 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
936 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
938 off_start = offset & (PAGE_SIZE - 1);
939 off_end = (offset + len) & (PAGE_SIZE - 1);
941 if (pg_start == pg_end) {
942 ret = fill_zero(inode, pg_start, off_start,
943 off_end - off_start);
948 ret = fill_zero(inode, pg_start++, off_start,
949 PAGE_SIZE - off_start);
954 ret = fill_zero(inode, pg_end, 0, off_end);
959 if (pg_start < pg_end) {
960 loff_t blk_start, blk_end;
961 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
963 f2fs_balance_fs(sbi, true);
965 blk_start = (loff_t)pg_start << PAGE_SHIFT;
966 blk_end = (loff_t)pg_end << PAGE_SHIFT;
968 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
969 down_write(&F2FS_I(inode)->i_mmap_sem);
971 truncate_pagecache_range(inode, blk_start, blk_end - 1);
974 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
977 up_write(&F2FS_I(inode)->i_mmap_sem);
978 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
985 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
986 int *do_replace, pgoff_t off, pgoff_t len)
988 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
989 struct dnode_of_data dn;
993 set_new_dnode(&dn, inode, NULL, NULL, 0);
994 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
995 if (ret && ret != -ENOENT) {
997 } else if (ret == -ENOENT) {
998 if (dn.max_level == 0)
1000 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
1006 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1007 dn.ofs_in_node, len);
1008 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1009 *blkaddr = datablock_addr(dn.inode,
1010 dn.node_page, dn.ofs_in_node);
1011 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1013 if (test_opt(sbi, LFS)) {
1014 f2fs_put_dnode(&dn);
1018 /* do not invalidate this block address */
1019 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1023 f2fs_put_dnode(&dn);
1032 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1033 int *do_replace, pgoff_t off, int len)
1035 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1036 struct dnode_of_data dn;
1039 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1040 if (*do_replace == 0)
1043 set_new_dnode(&dn, inode, NULL, NULL, 0);
1044 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1046 dec_valid_block_count(sbi, inode, 1);
1047 f2fs_invalidate_blocks(sbi, *blkaddr);
1049 f2fs_update_data_blkaddr(&dn, *blkaddr);
1051 f2fs_put_dnode(&dn);
1056 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1057 block_t *blkaddr, int *do_replace,
1058 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1060 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1065 if (blkaddr[i] == NULL_ADDR && !full) {
1070 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1071 struct dnode_of_data dn;
1072 struct node_info ni;
1076 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1077 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1081 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1083 f2fs_put_dnode(&dn);
1087 ilen = min((pgoff_t)
1088 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1089 dn.ofs_in_node, len - i);
1091 dn.data_blkaddr = datablock_addr(dn.inode,
1092 dn.node_page, dn.ofs_in_node);
1093 f2fs_truncate_data_blocks_range(&dn, 1);
1095 if (do_replace[i]) {
1096 f2fs_i_blocks_write(src_inode,
1098 f2fs_i_blocks_write(dst_inode,
1100 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1101 blkaddr[i], ni.version, true, false);
1107 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1108 if (dst_inode->i_size < new_size)
1109 f2fs_i_size_write(dst_inode, new_size);
1110 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1112 f2fs_put_dnode(&dn);
1114 struct page *psrc, *pdst;
1116 psrc = f2fs_get_lock_data_page(src_inode,
1119 return PTR_ERR(psrc);
1120 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1123 f2fs_put_page(psrc, 1);
1124 return PTR_ERR(pdst);
1126 f2fs_copy_page(psrc, pdst);
1127 set_page_dirty(pdst);
1128 f2fs_put_page(pdst, 1);
1129 f2fs_put_page(psrc, 1);
1131 ret = f2fs_truncate_hole(src_inode,
1132 src + i, src + i + 1);
1141 static int __exchange_data_block(struct inode *src_inode,
1142 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1143 pgoff_t len, bool full)
1145 block_t *src_blkaddr;
1151 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
1153 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1154 array_size(olen, sizeof(block_t)),
1159 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1160 array_size(olen, sizeof(int)),
1163 kvfree(src_blkaddr);
1167 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1168 do_replace, src, olen);
1172 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1173 do_replace, src, dst, olen, full);
1181 kvfree(src_blkaddr);
1187 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1188 kvfree(src_blkaddr);
1193 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1195 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1196 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1197 pgoff_t start = offset >> PAGE_SHIFT;
1198 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1201 f2fs_balance_fs(sbi, true);
1203 /* avoid gc operation during block exchange */
1204 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1205 down_write(&F2FS_I(inode)->i_mmap_sem);
1208 f2fs_drop_extent_tree(inode);
1209 truncate_pagecache(inode, offset);
1210 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1211 f2fs_unlock_op(sbi);
1213 up_write(&F2FS_I(inode)->i_mmap_sem);
1214 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1218 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1223 if (offset + len >= i_size_read(inode))
1226 /* collapse range should be aligned to block size of f2fs. */
1227 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1230 ret = f2fs_convert_inline_inode(inode);
1234 /* write out all dirty pages from offset */
1235 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1239 ret = f2fs_do_collapse(inode, offset, len);
1243 /* write out all moved pages, if possible */
1244 down_write(&F2FS_I(inode)->i_mmap_sem);
1245 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1246 truncate_pagecache(inode, offset);
1248 new_size = i_size_read(inode) - len;
1249 truncate_pagecache(inode, new_size);
1251 ret = f2fs_truncate_blocks(inode, new_size, true);
1252 up_write(&F2FS_I(inode)->i_mmap_sem);
1254 f2fs_i_size_write(inode, new_size);
1258 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1261 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1262 pgoff_t index = start;
1263 unsigned int ofs_in_node = dn->ofs_in_node;
1267 for (; index < end; index++, dn->ofs_in_node++) {
1268 if (datablock_addr(dn->inode, dn->node_page,
1269 dn->ofs_in_node) == NULL_ADDR)
1273 dn->ofs_in_node = ofs_in_node;
1274 ret = f2fs_reserve_new_blocks(dn, count);
1278 dn->ofs_in_node = ofs_in_node;
1279 for (index = start; index < end; index++, dn->ofs_in_node++) {
1280 dn->data_blkaddr = datablock_addr(dn->inode,
1281 dn->node_page, dn->ofs_in_node);
1283 * f2fs_reserve_new_blocks will not guarantee entire block
1286 if (dn->data_blkaddr == NULL_ADDR) {
1290 if (dn->data_blkaddr != NEW_ADDR) {
1291 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1292 dn->data_blkaddr = NEW_ADDR;
1293 f2fs_set_data_blkaddr(dn);
1297 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1302 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1305 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1306 struct address_space *mapping = inode->i_mapping;
1307 pgoff_t index, pg_start, pg_end;
1308 loff_t new_size = i_size_read(inode);
1309 loff_t off_start, off_end;
1312 ret = inode_newsize_ok(inode, (len + offset));
1316 ret = f2fs_convert_inline_inode(inode);
1320 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1324 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1325 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1327 off_start = offset & (PAGE_SIZE - 1);
1328 off_end = (offset + len) & (PAGE_SIZE - 1);
1330 if (pg_start == pg_end) {
1331 ret = fill_zero(inode, pg_start, off_start,
1332 off_end - off_start);
1336 new_size = max_t(loff_t, new_size, offset + len);
1339 ret = fill_zero(inode, pg_start++, off_start,
1340 PAGE_SIZE - off_start);
1344 new_size = max_t(loff_t, new_size,
1345 (loff_t)pg_start << PAGE_SHIFT);
1348 for (index = pg_start; index < pg_end;) {
1349 struct dnode_of_data dn;
1350 unsigned int end_offset;
1353 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1354 down_write(&F2FS_I(inode)->i_mmap_sem);
1356 truncate_pagecache_range(inode,
1357 (loff_t)index << PAGE_SHIFT,
1358 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1362 set_new_dnode(&dn, inode, NULL, NULL, 0);
1363 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1365 f2fs_unlock_op(sbi);
1366 up_write(&F2FS_I(inode)->i_mmap_sem);
1367 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1371 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1372 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1374 ret = f2fs_do_zero_range(&dn, index, end);
1375 f2fs_put_dnode(&dn);
1377 f2fs_unlock_op(sbi);
1378 up_write(&F2FS_I(inode)->i_mmap_sem);
1379 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1381 f2fs_balance_fs(sbi, dn.node_changed);
1387 new_size = max_t(loff_t, new_size,
1388 (loff_t)index << PAGE_SHIFT);
1392 ret = fill_zero(inode, pg_end, 0, off_end);
1396 new_size = max_t(loff_t, new_size, offset + len);
1401 if (new_size > i_size_read(inode)) {
1402 if (mode & FALLOC_FL_KEEP_SIZE)
1403 file_set_keep_isize(inode);
1405 f2fs_i_size_write(inode, new_size);
1410 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1412 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1413 pgoff_t nr, pg_start, pg_end, delta, idx;
1417 new_size = i_size_read(inode) + len;
1418 ret = inode_newsize_ok(inode, new_size);
1422 if (offset >= i_size_read(inode))
1425 /* insert range should be aligned to block size of f2fs. */
1426 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1429 ret = f2fs_convert_inline_inode(inode);
1433 f2fs_balance_fs(sbi, true);
1435 down_write(&F2FS_I(inode)->i_mmap_sem);
1436 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1437 up_write(&F2FS_I(inode)->i_mmap_sem);
1441 /* write out all dirty pages from offset */
1442 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1446 pg_start = offset >> PAGE_SHIFT;
1447 pg_end = (offset + len) >> PAGE_SHIFT;
1448 delta = pg_end - pg_start;
1449 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1451 /* avoid gc operation during block exchange */
1452 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1453 down_write(&F2FS_I(inode)->i_mmap_sem);
1454 truncate_pagecache(inode, offset);
1456 while (!ret && idx > pg_start) {
1457 nr = idx - pg_start;
1463 f2fs_drop_extent_tree(inode);
1465 ret = __exchange_data_block(inode, inode, idx,
1466 idx + delta, nr, false);
1467 f2fs_unlock_op(sbi);
1469 up_write(&F2FS_I(inode)->i_mmap_sem);
1470 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1472 /* write out all moved pages, if possible */
1473 down_write(&F2FS_I(inode)->i_mmap_sem);
1474 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1475 truncate_pagecache(inode, offset);
1476 up_write(&F2FS_I(inode)->i_mmap_sem);
1479 f2fs_i_size_write(inode, new_size);
1483 static int expand_inode_data(struct inode *inode, loff_t offset,
1484 loff_t len, int mode)
1486 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1487 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1488 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE };
1490 loff_t new_size = i_size_read(inode);
1494 err = inode_newsize_ok(inode, (len + offset));
1498 err = f2fs_convert_inline_inode(inode);
1502 f2fs_balance_fs(sbi, true);
1504 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1505 off_end = (offset + len) & (PAGE_SIZE - 1);
1507 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1508 map.m_len = pg_end - map.m_lblk;
1512 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1519 last_off = map.m_lblk + map.m_len - 1;
1521 /* update new size to the failed position */
1522 new_size = (last_off == pg_end) ? offset + len :
1523 (loff_t)(last_off + 1) << PAGE_SHIFT;
1525 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1528 if (new_size > i_size_read(inode)) {
1529 if (mode & FALLOC_FL_KEEP_SIZE)
1530 file_set_keep_isize(inode);
1532 f2fs_i_size_write(inode, new_size);
1538 static long f2fs_fallocate(struct file *file, int mode,
1539 loff_t offset, loff_t len)
1541 struct inode *inode = file_inode(file);
1544 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1547 /* f2fs only support ->fallocate for regular file */
1548 if (!S_ISREG(inode->i_mode))
1551 if (f2fs_encrypted_inode(inode) &&
1552 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1555 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1556 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1557 FALLOC_FL_INSERT_RANGE))
1562 if (mode & FALLOC_FL_PUNCH_HOLE) {
1563 if (offset >= inode->i_size)
1566 ret = punch_hole(inode, offset, len);
1567 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1568 ret = f2fs_collapse_range(inode, offset, len);
1569 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1570 ret = f2fs_zero_range(inode, offset, len, mode);
1571 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1572 ret = f2fs_insert_range(inode, offset, len);
1574 ret = expand_inode_data(inode, offset, len, mode);
1578 inode->i_mtime = inode->i_ctime = current_time(inode);
1579 f2fs_mark_inode_dirty_sync(inode, false);
1580 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1584 inode_unlock(inode);
1586 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1590 static int f2fs_release_file(struct inode *inode, struct file *filp)
1593 * f2fs_relase_file is called at every close calls. So we should
1594 * not drop any inmemory pages by close called by other process.
1596 if (!(filp->f_mode & FMODE_WRITE) ||
1597 atomic_read(&inode->i_writecount) != 1)
1600 /* some remained atomic pages should discarded */
1601 if (f2fs_is_atomic_file(inode))
1602 f2fs_drop_inmem_pages(inode);
1603 if (f2fs_is_volatile_file(inode)) {
1604 set_inode_flag(inode, FI_DROP_CACHE);
1605 filemap_fdatawrite(inode->i_mapping);
1606 clear_inode_flag(inode, FI_DROP_CACHE);
1607 clear_inode_flag(inode, FI_VOLATILE_FILE);
1608 stat_dec_volatile_write(inode);
1613 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1615 struct inode *inode = file_inode(file);
1618 * If the process doing a transaction is crashed, we should do
1619 * roll-back. Otherwise, other reader/write can see corrupted database
1620 * until all the writers close its file. Since this should be done
1621 * before dropping file lock, it needs to do in ->flush.
1623 if (f2fs_is_atomic_file(inode) &&
1624 F2FS_I(inode)->inmem_task == current)
1625 f2fs_drop_inmem_pages(inode);
1629 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1631 struct inode *inode = file_inode(filp);
1632 struct f2fs_inode_info *fi = F2FS_I(inode);
1633 unsigned int flags = fi->i_flags;
1635 if (f2fs_encrypted_inode(inode))
1636 flags |= F2FS_ENCRYPT_FL;
1637 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1638 flags |= F2FS_INLINE_DATA_FL;
1640 flags &= F2FS_FL_USER_VISIBLE;
1642 return put_user(flags, (int __user *)arg);
1645 static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1647 struct f2fs_inode_info *fi = F2FS_I(inode);
1648 unsigned int oldflags;
1650 /* Is it quota file? Do not allow user to mess with it */
1651 if (IS_NOQUOTA(inode))
1654 flags = f2fs_mask_flags(inode->i_mode, flags);
1656 oldflags = fi->i_flags;
1658 if ((flags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
1659 if (!capable(CAP_LINUX_IMMUTABLE))
1662 flags = flags & F2FS_FL_USER_MODIFIABLE;
1663 flags |= oldflags & ~F2FS_FL_USER_MODIFIABLE;
1664 fi->i_flags = flags;
1666 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1667 set_inode_flag(inode, FI_PROJ_INHERIT);
1669 clear_inode_flag(inode, FI_PROJ_INHERIT);
1671 inode->i_ctime = current_time(inode);
1672 f2fs_set_inode_flags(inode);
1673 f2fs_mark_inode_dirty_sync(inode, true);
1677 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1679 struct inode *inode = file_inode(filp);
1683 if (!inode_owner_or_capable(inode))
1686 if (get_user(flags, (int __user *)arg))
1689 ret = mnt_want_write_file(filp);
1695 ret = __f2fs_ioc_setflags(inode, flags);
1697 inode_unlock(inode);
1698 mnt_drop_write_file(filp);
1702 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1704 struct inode *inode = file_inode(filp);
1706 return put_user(inode->i_generation, (int __user *)arg);
1709 static int f2fs_ioc_start_atomic_write(struct file *filp)
1711 struct inode *inode = file_inode(filp);
1714 if (!inode_owner_or_capable(inode))
1717 if (!S_ISREG(inode->i_mode))
1720 ret = mnt_want_write_file(filp);
1726 if (f2fs_is_atomic_file(inode)) {
1727 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
1732 ret = f2fs_convert_inline_inode(inode);
1736 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1739 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
1740 * f2fs_is_atomic_file.
1742 if (get_dirty_pages(inode))
1743 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
1744 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1745 inode->i_ino, get_dirty_pages(inode));
1746 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1748 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1752 set_inode_flag(inode, FI_ATOMIC_FILE);
1753 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1754 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1756 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1757 F2FS_I(inode)->inmem_task = current;
1758 stat_inc_atomic_write(inode);
1759 stat_update_max_atomic_write(inode);
1761 inode_unlock(inode);
1762 mnt_drop_write_file(filp);
1766 static int f2fs_ioc_commit_atomic_write(struct file *filp)
1768 struct inode *inode = file_inode(filp);
1771 if (!inode_owner_or_capable(inode))
1774 ret = mnt_want_write_file(filp);
1778 f2fs_balance_fs(F2FS_I_SB(inode), true);
1782 if (f2fs_is_volatile_file(inode)) {
1787 if (f2fs_is_atomic_file(inode)) {
1788 ret = f2fs_commit_inmem_pages(inode);
1792 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1794 clear_inode_flag(inode, FI_ATOMIC_FILE);
1795 F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
1796 stat_dec_atomic_write(inode);
1799 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
1802 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
1803 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1806 inode_unlock(inode);
1807 mnt_drop_write_file(filp);
1811 static int f2fs_ioc_start_volatile_write(struct file *filp)
1813 struct inode *inode = file_inode(filp);
1816 if (!inode_owner_or_capable(inode))
1819 if (!S_ISREG(inode->i_mode))
1822 ret = mnt_want_write_file(filp);
1828 if (f2fs_is_volatile_file(inode))
1831 ret = f2fs_convert_inline_inode(inode);
1835 stat_inc_volatile_write(inode);
1836 stat_update_max_volatile_write(inode);
1838 set_inode_flag(inode, FI_VOLATILE_FILE);
1839 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1841 inode_unlock(inode);
1842 mnt_drop_write_file(filp);
1846 static int f2fs_ioc_release_volatile_write(struct file *filp)
1848 struct inode *inode = file_inode(filp);
1851 if (!inode_owner_or_capable(inode))
1854 ret = mnt_want_write_file(filp);
1860 if (!f2fs_is_volatile_file(inode))
1863 if (!f2fs_is_first_block_written(inode)) {
1864 ret = truncate_partial_data_page(inode, 0, true);
1868 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1870 inode_unlock(inode);
1871 mnt_drop_write_file(filp);
1875 static int f2fs_ioc_abort_volatile_write(struct file *filp)
1877 struct inode *inode = file_inode(filp);
1880 if (!inode_owner_or_capable(inode))
1883 ret = mnt_want_write_file(filp);
1889 if (f2fs_is_atomic_file(inode))
1890 f2fs_drop_inmem_pages(inode);
1891 if (f2fs_is_volatile_file(inode)) {
1892 clear_inode_flag(inode, FI_VOLATILE_FILE);
1893 stat_dec_volatile_write(inode);
1894 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1897 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
1899 inode_unlock(inode);
1901 mnt_drop_write_file(filp);
1902 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1906 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1908 struct inode *inode = file_inode(filp);
1909 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1910 struct super_block *sb = sbi->sb;
1914 if (!capable(CAP_SYS_ADMIN))
1917 if (get_user(in, (__u32 __user *)arg))
1920 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1921 ret = mnt_want_write_file(filp);
1927 case F2FS_GOING_DOWN_FULLSYNC:
1928 sb = freeze_bdev(sb->s_bdev);
1934 f2fs_stop_checkpoint(sbi, false);
1935 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
1936 thaw_bdev(sb->s_bdev, sb);
1939 case F2FS_GOING_DOWN_METASYNC:
1940 /* do checkpoint only */
1941 ret = f2fs_sync_fs(sb, 1);
1944 f2fs_stop_checkpoint(sbi, false);
1945 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
1947 case F2FS_GOING_DOWN_NOSYNC:
1948 f2fs_stop_checkpoint(sbi, false);
1949 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
1951 case F2FS_GOING_DOWN_METAFLUSH:
1952 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
1953 f2fs_stop_checkpoint(sbi, false);
1954 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
1961 f2fs_stop_gc_thread(sbi);
1962 f2fs_stop_discard_thread(sbi);
1964 f2fs_drop_discard_cmd(sbi);
1965 clear_opt(sbi, DISCARD);
1967 f2fs_update_time(sbi, REQ_TIME);
1969 if (in != F2FS_GOING_DOWN_FULLSYNC)
1970 mnt_drop_write_file(filp);
1974 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1976 struct inode *inode = file_inode(filp);
1977 struct super_block *sb = inode->i_sb;
1978 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1979 struct fstrim_range range;
1982 if (!capable(CAP_SYS_ADMIN))
1985 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
1988 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1992 ret = mnt_want_write_file(filp);
1996 range.minlen = max((unsigned int)range.minlen,
1997 q->limits.discard_granularity);
1998 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1999 mnt_drop_write_file(filp);
2003 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2006 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2010 static bool uuid_is_nonzero(__u8 u[16])
2014 for (i = 0; i < 16; i++)
2020 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2022 struct inode *inode = file_inode(filp);
2024 if (!f2fs_sb_has_encrypt(inode->i_sb))
2027 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2029 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2032 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2034 if (!f2fs_sb_has_encrypt(file_inode(filp)->i_sb))
2036 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2039 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2041 struct inode *inode = file_inode(filp);
2042 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2045 if (!f2fs_sb_has_encrypt(inode->i_sb))
2048 err = mnt_want_write_file(filp);
2052 down_write(&sbi->sb_lock);
2054 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2057 /* update superblock with uuid */
2058 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2060 err = f2fs_commit_super(sbi, false);
2063 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2067 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2071 up_write(&sbi->sb_lock);
2072 mnt_drop_write_file(filp);
2076 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2078 struct inode *inode = file_inode(filp);
2079 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2083 if (!capable(CAP_SYS_ADMIN))
2086 if (get_user(sync, (__u32 __user *)arg))
2089 if (f2fs_readonly(sbi->sb))
2092 ret = mnt_want_write_file(filp);
2097 if (!mutex_trylock(&sbi->gc_mutex)) {
2102 mutex_lock(&sbi->gc_mutex);
2105 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
2107 mnt_drop_write_file(filp);
2111 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2113 struct inode *inode = file_inode(filp);
2114 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2115 struct f2fs_gc_range range;
2119 if (!capable(CAP_SYS_ADMIN))
2122 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2126 if (f2fs_readonly(sbi->sb))
2129 end = range.start + range.len;
2130 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
2134 ret = mnt_want_write_file(filp);
2140 if (!mutex_trylock(&sbi->gc_mutex)) {
2145 mutex_lock(&sbi->gc_mutex);
2148 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2149 range.start += BLKS_PER_SEC(sbi);
2150 if (range.start <= end)
2153 mnt_drop_write_file(filp);
2157 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2159 struct inode *inode = file_inode(filp);
2160 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2163 if (!capable(CAP_SYS_ADMIN))
2166 if (f2fs_readonly(sbi->sb))
2169 ret = mnt_want_write_file(filp);
2173 ret = f2fs_sync_fs(sbi->sb, 1);
2175 mnt_drop_write_file(filp);
2179 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2181 struct f2fs_defragment *range)
2183 struct inode *inode = file_inode(filp);
2184 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2185 .m_seg_type = NO_CHECK_TYPE };
2186 struct extent_info ei = {0, 0, 0};
2187 pgoff_t pg_start, pg_end, next_pgofs;
2188 unsigned int blk_per_seg = sbi->blocks_per_seg;
2189 unsigned int total = 0, sec_num;
2190 block_t blk_end = 0;
2191 bool fragmented = false;
2194 /* if in-place-update policy is enabled, don't waste time here */
2195 if (f2fs_should_update_inplace(inode, NULL))
2198 pg_start = range->start >> PAGE_SHIFT;
2199 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2201 f2fs_balance_fs(sbi, true);
2205 /* writeback all dirty pages in the range */
2206 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2207 range->start + range->len - 1);
2212 * lookup mapping info in extent cache, skip defragmenting if physical
2213 * block addresses are continuous.
2215 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2216 if (ei.fofs + ei.len >= pg_end)
2220 map.m_lblk = pg_start;
2221 map.m_next_pgofs = &next_pgofs;
2224 * lookup mapping info in dnode page cache, skip defragmenting if all
2225 * physical block addresses are continuous even if there are hole(s)
2226 * in logical blocks.
2228 while (map.m_lblk < pg_end) {
2229 map.m_len = pg_end - map.m_lblk;
2230 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2234 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2235 map.m_lblk = next_pgofs;
2239 if (blk_end && blk_end != map.m_pblk)
2242 /* record total count of block that we're going to move */
2245 blk_end = map.m_pblk + map.m_len;
2247 map.m_lblk += map.m_len;
2253 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
2256 * make sure there are enough free section for LFS allocation, this can
2257 * avoid defragment running in SSR mode when free section are allocated
2260 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2265 map.m_lblk = pg_start;
2266 map.m_len = pg_end - pg_start;
2269 while (map.m_lblk < pg_end) {
2274 map.m_len = pg_end - map.m_lblk;
2275 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2279 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2280 map.m_lblk = next_pgofs;
2284 set_inode_flag(inode, FI_DO_DEFRAG);
2287 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2290 page = f2fs_get_lock_data_page(inode, idx, true);
2292 err = PTR_ERR(page);
2296 set_page_dirty(page);
2297 f2fs_put_page(page, 1);
2306 if (idx < pg_end && cnt < blk_per_seg)
2309 clear_inode_flag(inode, FI_DO_DEFRAG);
2311 err = filemap_fdatawrite(inode->i_mapping);
2316 clear_inode_flag(inode, FI_DO_DEFRAG);
2318 inode_unlock(inode);
2320 range->len = (u64)total << PAGE_SHIFT;
2324 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2326 struct inode *inode = file_inode(filp);
2327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2328 struct f2fs_defragment range;
2331 if (!capable(CAP_SYS_ADMIN))
2334 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2337 if (f2fs_readonly(sbi->sb))
2340 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2344 /* verify alignment of offset & size */
2345 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2348 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2349 sbi->max_file_blocks))
2352 err = mnt_want_write_file(filp);
2356 err = f2fs_defragment_range(sbi, filp, &range);
2357 mnt_drop_write_file(filp);
2359 f2fs_update_time(sbi, REQ_TIME);
2363 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2370 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2371 struct file *file_out, loff_t pos_out, size_t len)
2373 struct inode *src = file_inode(file_in);
2374 struct inode *dst = file_inode(file_out);
2375 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2376 size_t olen = len, dst_max_i_size = 0;
2380 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2381 src->i_sb != dst->i_sb)
2384 if (unlikely(f2fs_readonly(src->i_sb)))
2387 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2390 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2394 if (pos_in == pos_out)
2396 if (pos_out > pos_in && pos_out < pos_in + len)
2403 if (!inode_trylock(dst))
2408 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2411 olen = len = src->i_size - pos_in;
2412 if (pos_in + len == src->i_size)
2413 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2419 dst_osize = dst->i_size;
2420 if (pos_out + olen > dst->i_size)
2421 dst_max_i_size = pos_out + olen;
2423 /* verify the end result is block aligned */
2424 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2425 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2426 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2429 ret = f2fs_convert_inline_inode(src);
2433 ret = f2fs_convert_inline_inode(dst);
2437 /* write out all dirty pages from offset */
2438 ret = filemap_write_and_wait_range(src->i_mapping,
2439 pos_in, pos_in + len);
2443 ret = filemap_write_and_wait_range(dst->i_mapping,
2444 pos_out, pos_out + len);
2448 f2fs_balance_fs(sbi, true);
2450 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2453 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2458 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2459 pos_out >> F2FS_BLKSIZE_BITS,
2460 len >> F2FS_BLKSIZE_BITS, false);
2464 f2fs_i_size_write(dst, dst_max_i_size);
2465 else if (dst_osize != dst->i_size)
2466 f2fs_i_size_write(dst, dst_osize);
2468 f2fs_unlock_op(sbi);
2471 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2473 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2482 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2484 struct f2fs_move_range range;
2488 if (!(filp->f_mode & FMODE_READ) ||
2489 !(filp->f_mode & FMODE_WRITE))
2492 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2496 dst = fdget(range.dst_fd);
2500 if (!(dst.file->f_mode & FMODE_WRITE)) {
2505 err = mnt_want_write_file(filp);
2509 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2510 range.pos_out, range.len);
2512 mnt_drop_write_file(filp);
2516 if (copy_to_user((struct f2fs_move_range __user *)arg,
2517 &range, sizeof(range)))
2524 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2526 struct inode *inode = file_inode(filp);
2527 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2528 struct sit_info *sm = SIT_I(sbi);
2529 unsigned int start_segno = 0, end_segno = 0;
2530 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2531 struct f2fs_flush_device range;
2534 if (!capable(CAP_SYS_ADMIN))
2537 if (f2fs_readonly(sbi->sb))
2540 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2544 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2545 sbi->segs_per_sec != 1) {
2546 f2fs_msg(sbi->sb, KERN_WARNING,
2547 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2548 range.dev_num, sbi->s_ndevs,
2553 ret = mnt_want_write_file(filp);
2557 if (range.dev_num != 0)
2558 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2559 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2561 start_segno = sm->last_victim[FLUSH_DEVICE];
2562 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2563 start_segno = dev_start_segno;
2564 end_segno = min(start_segno + range.segments, dev_end_segno);
2566 while (start_segno < end_segno) {
2567 if (!mutex_trylock(&sbi->gc_mutex)) {
2571 sm->last_victim[GC_CB] = end_segno + 1;
2572 sm->last_victim[GC_GREEDY] = end_segno + 1;
2573 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2574 ret = f2fs_gc(sbi, true, true, start_segno);
2582 mnt_drop_write_file(filp);
2586 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2588 struct inode *inode = file_inode(filp);
2589 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2591 /* Must validate to set it with SQLite behavior in Android. */
2592 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2594 return put_user(sb_feature, (u32 __user *)arg);
2598 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2600 struct inode *inode = file_inode(filp);
2601 struct f2fs_inode_info *fi = F2FS_I(inode);
2602 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2603 struct super_block *sb = sbi->sb;
2604 struct dquot *transfer_to[MAXQUOTAS] = {};
2609 if (!f2fs_sb_has_project_quota(sb)) {
2610 if (projid != F2FS_DEF_PROJID)
2616 if (!f2fs_has_extra_attr(inode))
2619 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2621 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2625 /* Is it quota file? Do not allow user to mess with it */
2626 if (IS_NOQUOTA(inode))
2629 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2631 return PTR_ERR(ipage);
2633 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2636 f2fs_put_page(ipage, 1);
2639 f2fs_put_page(ipage, 1);
2641 err = dquot_initialize(inode);
2645 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2646 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2647 err = __dquot_transfer(inode, transfer_to);
2648 dqput(transfer_to[PRJQUOTA]);
2653 F2FS_I(inode)->i_projid = kprojid;
2654 inode->i_ctime = current_time(inode);
2656 f2fs_mark_inode_dirty_sync(inode, true);
2660 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2662 if (projid != F2FS_DEF_PROJID)
2668 /* Transfer internal flags to xflags */
2669 static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2673 if (iflags & F2FS_SYNC_FL)
2674 xflags |= FS_XFLAG_SYNC;
2675 if (iflags & F2FS_IMMUTABLE_FL)
2676 xflags |= FS_XFLAG_IMMUTABLE;
2677 if (iflags & F2FS_APPEND_FL)
2678 xflags |= FS_XFLAG_APPEND;
2679 if (iflags & F2FS_NODUMP_FL)
2680 xflags |= FS_XFLAG_NODUMP;
2681 if (iflags & F2FS_NOATIME_FL)
2682 xflags |= FS_XFLAG_NOATIME;
2683 if (iflags & F2FS_PROJINHERIT_FL)
2684 xflags |= FS_XFLAG_PROJINHERIT;
2688 #define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2689 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2690 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2692 /* Transfer xflags flags to internal */
2693 static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2695 unsigned long iflags = 0;
2697 if (xflags & FS_XFLAG_SYNC)
2698 iflags |= F2FS_SYNC_FL;
2699 if (xflags & FS_XFLAG_IMMUTABLE)
2700 iflags |= F2FS_IMMUTABLE_FL;
2701 if (xflags & FS_XFLAG_APPEND)
2702 iflags |= F2FS_APPEND_FL;
2703 if (xflags & FS_XFLAG_NODUMP)
2704 iflags |= F2FS_NODUMP_FL;
2705 if (xflags & FS_XFLAG_NOATIME)
2706 iflags |= F2FS_NOATIME_FL;
2707 if (xflags & FS_XFLAG_PROJINHERIT)
2708 iflags |= F2FS_PROJINHERIT_FL;
2713 static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2715 struct inode *inode = file_inode(filp);
2716 struct f2fs_inode_info *fi = F2FS_I(inode);
2719 memset(&fa, 0, sizeof(struct fsxattr));
2720 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
2721 F2FS_FL_USER_VISIBLE);
2723 if (f2fs_sb_has_project_quota(inode->i_sb))
2724 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2727 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2732 static int f2fs_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
2735 * Project Quota ID state is only allowed to change from within the init
2736 * namespace. Enforce that restriction only if we are trying to change
2737 * the quota ID state. Everything else is allowed in user namespaces.
2739 if (current_user_ns() == &init_user_ns)
2742 if (__kprojid_val(F2FS_I(inode)->i_projid) != fa->fsx_projid)
2745 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) {
2746 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
2749 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
2756 static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2758 struct inode *inode = file_inode(filp);
2759 struct f2fs_inode_info *fi = F2FS_I(inode);
2764 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2767 /* Make sure caller has proper permission */
2768 if (!inode_owner_or_capable(inode))
2771 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2774 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2775 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2778 err = mnt_want_write_file(filp);
2783 err = f2fs_ioctl_check_project(inode, &fa);
2786 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2787 (flags & F2FS_FL_XFLAG_VISIBLE);
2788 err = __f2fs_ioc_setflags(inode, flags);
2792 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2794 inode_unlock(inode);
2795 mnt_drop_write_file(filp);
2799 int f2fs_pin_file_control(struct inode *inode, bool inc)
2801 struct f2fs_inode_info *fi = F2FS_I(inode);
2802 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2804 /* Use i_gc_failures for normal file as a risk signal. */
2806 f2fs_i_gc_failures_write(inode,
2807 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
2809 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
2810 f2fs_msg(sbi->sb, KERN_WARNING,
2811 "%s: Enable GC = ino %lx after %x GC trials\n",
2812 __func__, inode->i_ino,
2813 fi->i_gc_failures[GC_FAILURE_PIN]);
2814 clear_inode_flag(inode, FI_PIN_FILE);
2820 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2822 struct inode *inode = file_inode(filp);
2826 if (!inode_owner_or_capable(inode))
2829 if (get_user(pin, (__u32 __user *)arg))
2832 if (!S_ISREG(inode->i_mode))
2835 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2838 ret = mnt_want_write_file(filp);
2844 if (f2fs_should_update_outplace(inode, NULL)) {
2850 clear_inode_flag(inode, FI_PIN_FILE);
2851 f2fs_i_gc_failures_write(inode, 0);
2855 if (f2fs_pin_file_control(inode, false)) {
2859 ret = f2fs_convert_inline_inode(inode);
2863 set_inode_flag(inode, FI_PIN_FILE);
2864 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
2866 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2868 inode_unlock(inode);
2869 mnt_drop_write_file(filp);
2873 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2875 struct inode *inode = file_inode(filp);
2878 if (is_inode_flag_set(inode, FI_PIN_FILE))
2879 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
2880 return put_user(pin, (u32 __user *)arg);
2883 int f2fs_precache_extents(struct inode *inode)
2885 struct f2fs_inode_info *fi = F2FS_I(inode);
2886 struct f2fs_map_blocks map;
2887 pgoff_t m_next_extent;
2891 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2895 map.m_next_pgofs = NULL;
2896 map.m_next_extent = &m_next_extent;
2897 map.m_seg_type = NO_CHECK_TYPE;
2898 end = F2FS_I_SB(inode)->max_file_blocks;
2900 while (map.m_lblk < end) {
2901 map.m_len = end - map.m_lblk;
2903 down_write(&fi->i_gc_rwsem[WRITE]);
2904 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
2905 up_write(&fi->i_gc_rwsem[WRITE]);
2909 map.m_lblk = m_next_extent;
2915 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2917 return f2fs_precache_extents(file_inode(filp));
2920 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2922 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2926 case F2FS_IOC_GETFLAGS:
2927 return f2fs_ioc_getflags(filp, arg);
2928 case F2FS_IOC_SETFLAGS:
2929 return f2fs_ioc_setflags(filp, arg);
2930 case F2FS_IOC_GETVERSION:
2931 return f2fs_ioc_getversion(filp, arg);
2932 case F2FS_IOC_START_ATOMIC_WRITE:
2933 return f2fs_ioc_start_atomic_write(filp);
2934 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2935 return f2fs_ioc_commit_atomic_write(filp);
2936 case F2FS_IOC_START_VOLATILE_WRITE:
2937 return f2fs_ioc_start_volatile_write(filp);
2938 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2939 return f2fs_ioc_release_volatile_write(filp);
2940 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2941 return f2fs_ioc_abort_volatile_write(filp);
2942 case F2FS_IOC_SHUTDOWN:
2943 return f2fs_ioc_shutdown(filp, arg);
2945 return f2fs_ioc_fitrim(filp, arg);
2946 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2947 return f2fs_ioc_set_encryption_policy(filp, arg);
2948 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2949 return f2fs_ioc_get_encryption_policy(filp, arg);
2950 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2951 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
2952 case F2FS_IOC_GARBAGE_COLLECT:
2953 return f2fs_ioc_gc(filp, arg);
2954 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2955 return f2fs_ioc_gc_range(filp, arg);
2956 case F2FS_IOC_WRITE_CHECKPOINT:
2957 return f2fs_ioc_write_checkpoint(filp, arg);
2958 case F2FS_IOC_DEFRAGMENT:
2959 return f2fs_ioc_defragment(filp, arg);
2960 case F2FS_IOC_MOVE_RANGE:
2961 return f2fs_ioc_move_range(filp, arg);
2962 case F2FS_IOC_FLUSH_DEVICE:
2963 return f2fs_ioc_flush_device(filp, arg);
2964 case F2FS_IOC_GET_FEATURES:
2965 return f2fs_ioc_get_features(filp, arg);
2966 case F2FS_IOC_FSGETXATTR:
2967 return f2fs_ioc_fsgetxattr(filp, arg);
2968 case F2FS_IOC_FSSETXATTR:
2969 return f2fs_ioc_fssetxattr(filp, arg);
2970 case F2FS_IOC_GET_PIN_FILE:
2971 return f2fs_ioc_get_pin_file(filp, arg);
2972 case F2FS_IOC_SET_PIN_FILE:
2973 return f2fs_ioc_set_pin_file(filp, arg);
2974 case F2FS_IOC_PRECACHE_EXTENTS:
2975 return f2fs_ioc_precache_extents(filp, arg);
2981 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2983 struct file *file = iocb->ki_filp;
2984 struct inode *inode = file_inode(file);
2987 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2990 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
2993 if (!inode_trylock(inode)) {
2994 if (iocb->ki_flags & IOCB_NOWAIT)
2999 ret = generic_write_checks(iocb, from);
3001 bool preallocated = false;
3002 size_t target_size = 0;
3005 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3006 set_inode_flag(inode, FI_NO_PREALLOC);
3008 if ((iocb->ki_flags & IOCB_NOWAIT) &&
3009 (iocb->ki_flags & IOCB_DIRECT)) {
3010 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
3011 iov_iter_count(from)) ||
3012 f2fs_has_inline_data(inode) ||
3013 f2fs_force_buffered_io(inode, WRITE)) {
3014 clear_inode_flag(inode,
3016 inode_unlock(inode);
3021 preallocated = true;
3022 target_size = iocb->ki_pos + iov_iter_count(from);
3024 err = f2fs_preallocate_blocks(iocb, from);
3026 clear_inode_flag(inode, FI_NO_PREALLOC);
3027 inode_unlock(inode);
3031 ret = __generic_file_write_iter(iocb, from);
3032 clear_inode_flag(inode, FI_NO_PREALLOC);
3034 /* if we couldn't write data, we should deallocate blocks. */
3035 if (preallocated && i_size_read(inode) < target_size)
3036 f2fs_truncate(inode);
3039 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
3041 inode_unlock(inode);
3044 ret = generic_write_sync(iocb, ret);
3048 #ifdef CONFIG_COMPAT
3049 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3052 case F2FS_IOC32_GETFLAGS:
3053 cmd = F2FS_IOC_GETFLAGS;
3055 case F2FS_IOC32_SETFLAGS:
3056 cmd = F2FS_IOC_SETFLAGS;
3058 case F2FS_IOC32_GETVERSION:
3059 cmd = F2FS_IOC_GETVERSION;
3061 case F2FS_IOC_START_ATOMIC_WRITE:
3062 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3063 case F2FS_IOC_START_VOLATILE_WRITE:
3064 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3065 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3066 case F2FS_IOC_SHUTDOWN:
3067 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3068 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3069 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3070 case F2FS_IOC_GARBAGE_COLLECT:
3071 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3072 case F2FS_IOC_WRITE_CHECKPOINT:
3073 case F2FS_IOC_DEFRAGMENT:
3074 case F2FS_IOC_MOVE_RANGE:
3075 case F2FS_IOC_FLUSH_DEVICE:
3076 case F2FS_IOC_GET_FEATURES:
3077 case F2FS_IOC_FSGETXATTR:
3078 case F2FS_IOC_FSSETXATTR:
3079 case F2FS_IOC_GET_PIN_FILE:
3080 case F2FS_IOC_SET_PIN_FILE:
3081 case F2FS_IOC_PRECACHE_EXTENTS:
3084 return -ENOIOCTLCMD;
3086 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3090 const struct file_operations f2fs_file_operations = {
3091 .llseek = f2fs_llseek,
3092 .read_iter = generic_file_read_iter,
3093 .write_iter = f2fs_file_write_iter,
3094 .open = f2fs_file_open,
3095 .release = f2fs_release_file,
3096 .mmap = f2fs_file_mmap,
3097 .flush = f2fs_file_flush,
3098 .fsync = f2fs_sync_file,
3099 .fallocate = f2fs_fallocate,
3100 .unlocked_ioctl = f2fs_ioctl,
3101 #ifdef CONFIG_COMPAT
3102 .compat_ioctl = f2fs_compat_ioctl,
3104 .splice_read = generic_file_splice_read,
3105 .splice_write = iter_file_splice_write,