blob: 0dff22566a1d1fa545c0a91fbd29b175add5166e [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/stat.h>
11#include <linux/buffer_head.h>
12#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090013#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090014#include <linux/falloc.h>
15#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090016#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/uaccess.h>
18#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090019#include <linux/pagevec.h>
Jaegeuk Kimdc91de72017-01-13 13:12:29 -080020#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070021#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070022#include <linux/file.h>
Chao Yu4507847c2019-07-17 17:06:11 +080023#include <linux/nls.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090024
25#include "f2fs.h"
26#include "node.h"
27#include "segment.h"
28#include "xattr.h"
29#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080030#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080031#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090032#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090033
Souptick Joarderea4d4792018-04-15 01:40:02 +053034static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080035{
36 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarderea4d4792018-04-15 01:40:02 +053037 vm_fault_t ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080038
39 down_read(&F2FS_I(inode)->i_mmap_sem);
Souptick Joarderea4d4792018-04-15 01:40:02 +053040 ret = filemap_fault(vmf);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080041 up_read(&F2FS_I(inode)->i_mmap_sem);
42
Chao Yud7648342019-04-15 15:22:19 +080043 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
44
Souptick Joarderea4d4792018-04-15 01:40:02 +053045 return ret;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080046}
47
Souptick Joarderea4d4792018-04-15 01:40:02 +053048static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049{
50 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -080051 struct inode *inode = file_inode(vmf->vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080053 struct dnode_of_data dn;
Chao Yu4c8ff702019-11-01 18:07:14 +080054 bool need_alloc = true;
55 int err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090056
Jaegeuk Kim1f227a32017-10-23 23:48:49 +020057 if (unlikely(f2fs_cp_error(sbi))) {
58 err = -EIO;
59 goto err;
60 }
61
Chao Yu00e09c02019-08-23 17:58:36 +080062 if (!f2fs_is_checkpoint_ready(sbi)) {
63 err = -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +080064 goto err;
Chao Yu00e09c02019-08-23 17:58:36 +080065 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090066
Chao Yu4c8ff702019-11-01 18:07:14 +080067#ifdef CONFIG_F2FS_FS_COMPRESSION
68 if (f2fs_compressed_file(inode)) {
69 int ret = f2fs_is_compressed_cluster(inode, page->index);
70
71 if (ret < 0) {
72 err = ret;
73 goto err;
74 } else if (ret) {
75 if (ret < F2FS_I(inode)->i_cluster_size) {
76 err = -EAGAIN;
77 goto err;
78 }
79 need_alloc = false;
80 }
81 }
82#endif
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080083 /* should do out of any locked page */
Chao Yu4c8ff702019-11-01 18:07:14 +080084 if (need_alloc)
85 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbdf03292019-12-03 15:53:16 -080086
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090087 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070088
89 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070090
Dave Jiang11bac802017-02-24 14:56:41 -080091 file_update_time(vmf->vma->vm_file);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080092 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090093 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090094 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090095 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090096 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090097 unlock_page(page);
98 err = -EFAULT;
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +080099 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900100 }
101
Chao Yu4c8ff702019-11-01 18:07:14 +0800102 if (need_alloc) {
103 /* block allocation */
104 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
105 set_new_dnode(&dn, inode, NULL, NULL, 0);
106 err = f2fs_get_block(&dn, page->index);
107 f2fs_put_dnode(&dn);
108 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
109 if (err) {
110 unlock_page(page);
111 goto out_sem;
112 }
Chao Yu39a86952018-09-27 18:33:18 +0800113 }
114
115 /* fill the page */
Chao Yubae0ee72018-12-25 17:43:42 +0800116 f2fs_wait_on_page_writeback(page, DATA, false, true);
Chao Yu39a86952018-09-27 18:33:18 +0800117
118 /* wait for GCed page writeback via META_MAPPING */
119 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
120
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900121 /*
122 * check to see if the page is mapped already (no holes)
123 */
124 if (PageMappedToDisk(page))
Chao Yu39a86952018-09-27 18:33:18 +0800125 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900126
127 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300128 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +0800129 i_size_read(inode)) {
youngjun yoo193bea12018-05-30 04:21:14 +0900130 loff_t offset;
youngjun yoof11e98b2018-05-30 04:33:07 +0900131
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300132 offset = i_size_read(inode) & ~PAGE_MASK;
133 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900134 }
135 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700136 if (!PageUptodate(page))
137 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900138
Chao Yub0af6d42017-08-02 23:21:48 +0800139 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530140 f2fs_update_time(sbi, REQ_TIME);
Chao Yub0af6d42017-08-02 23:21:48 +0800141
Jaegeuk Kime943a102013-10-25 14:26:31 +0900142 trace_f2fs_vm_page_mkwrite(page, DATA);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800143out_sem:
144 up_read(&F2FS_I(inode)->i_mmap_sem);
Chao Yu39a86952018-09-27 18:33:18 +0800145
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900146 sb_end_pagefault(inode->i_sb);
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200147err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900148 return block_page_mkwrite_return(err);
149}
150
151static const struct vm_operations_struct f2fs_file_vm_ops = {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800152 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700153 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900154 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900155};
156
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900157static int get_parent_ino(struct inode *inode, nid_t *pino)
158{
159 struct dentry *dentry;
160
161 inode = igrab(inode);
162 dentry = d_find_any_alias(inode);
163 iput(inode);
164 if (!dentry)
165 return 0;
166
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900167 *pino = parent_ino(dentry);
168 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900169 return 1;
170}
171
Chao Yua5fd5052017-11-06 22:51:45 +0800172static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800173{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700174 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yua5fd5052017-11-06 22:51:45 +0800175 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800176
Chao Yua5fd5052017-11-06 22:51:45 +0800177 if (!S_ISREG(inode->i_mode))
178 cp_reason = CP_NON_REGULAR;
Chao Yu4c8ff702019-11-01 18:07:14 +0800179 else if (f2fs_compressed_file(inode))
180 cp_reason = CP_COMPRESSED;
Chao Yua5fd5052017-11-06 22:51:45 +0800181 else if (inode->i_nlink != 1)
182 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700183 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Chao Yua5fd5052017-11-06 22:51:45 +0800184 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800185 else if (file_wrong_pino(inode))
Chao Yua5fd5052017-11-06 22:51:45 +0800186 cp_reason = CP_WRONG_PINO;
Chao Yu4d57b862018-05-30 00:20:41 +0800187 else if (!f2fs_space_for_roll_forward(sbi))
Chao Yua5fd5052017-11-06 22:51:45 +0800188 cp_reason = CP_NO_SPC_ROLL;
Chao Yu4d57b862018-05-30 00:20:41 +0800189 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Chao Yua5fd5052017-11-06 22:51:45 +0800190 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700191 else if (test_opt(sbi, FASTBOOT))
Chao Yua5fd5052017-11-06 22:51:45 +0800192 cp_reason = CP_FASTBOOT_MODE;
Chao Yu63189b72018-03-08 14:22:56 +0800193 else if (F2FS_OPTION(sbi).active_logs == 2)
Chao Yua5fd5052017-11-06 22:51:45 +0800194 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu63189b72018-03-08 14:22:56 +0800195 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
Chao Yu4d57b862018-05-30 00:20:41 +0800196 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
197 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
198 TRANS_DIR_INO))
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800199 cp_reason = CP_RECOVER_DIR;
Chao Yu9d1589e2014-08-20 18:37:35 +0800200
Chao Yua5fd5052017-11-06 22:51:45 +0800201 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800202}
203
Changman Lee9c7bb702014-12-08 15:29:40 +0900204static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
205{
206 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
207 bool ret = false;
208 /* But we need to avoid that there are some inode updates */
Chao Yu4d57b862018-05-30 00:20:41 +0800209 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
Changman Lee9c7bb702014-12-08 15:29:40 +0900210 ret = true;
211 f2fs_put_page(i, 0);
212 return ret;
213}
214
Changman Lee51455b12014-12-08 15:29:41 +0900215static void try_to_fix_pino(struct inode *inode)
216{
217 struct f2fs_inode_info *fi = F2FS_I(inode);
218 nid_t pino;
219
220 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900221 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
222 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700223 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900224 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900225 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700226 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900227}
228
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700229static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
230 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900231{
232 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700233 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700234 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900235 int ret = 0;
Chao Yua5fd5052017-11-06 22:51:45 +0800236 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900237 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900238 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900239 .nr_to_write = LONG_MAX,
240 .for_reclaim = 0,
241 };
Chao Yu50fa53e2018-08-02 23:03:19 +0800242 unsigned int seq_id = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900243
Daniel Rosenberg43549942018-08-20 19:21:43 -0700244 if (unlikely(f2fs_readonly(inode->i_sb) ||
245 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900246 return 0;
247
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900248 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700249
Yunlei Heb61ac5b72018-11-06 10:25:29 +0800250 if (S_ISDIR(inode->i_mode))
251 goto go_write;
252
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700253 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800254 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700255 set_inode_flag(inode, FI_NEED_IPU);
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400256 ret = file_write_and_wait_range(file, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700257 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700258
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900259 if (ret) {
Chao Yua5fd5052017-11-06 22:51:45 +0800260 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900261 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900262 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900263
Changman Lee9c7bb702014-12-08 15:29:40 +0900264 /* if the inode is dirty, let's recover all the time */
Chao Yu281518c2016-11-17 20:53:31 +0800265 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700266 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900267 goto go_write;
268 }
269
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700270 /*
271 * if there is no written data, don't waste time to write recovery info.
272 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700273 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Chao Yu4d57b862018-05-30 00:20:41 +0800274 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700275
Changman Lee9c7bb702014-12-08 15:29:40 +0900276 /* it may call write_inode just prior to fsync */
277 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700278 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700279
Jaegeuk Kim91942322016-05-20 10:13:22 -0700280 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Chao Yu4d57b862018-05-30 00:20:41 +0800281 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700282 goto flush_out;
283 goto out;
284 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700285go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900286 /*
287 * Both of fdatasync() and fsync() are able to be recovered from
288 * sudden-power-off.
289 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700290 down_read(&F2FS_I(inode)->i_sem);
Chao Yua5fd5052017-11-06 22:51:45 +0800291 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700292 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900293
Chao Yua5fd5052017-11-06 22:51:45 +0800294 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900295 /* all the dirty node pages should be flushed for POR */
296 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900297
Changman Lee51455b12014-12-08 15:29:41 +0900298 /*
299 * We've secured consistency through sync_fs. Following pino
300 * will be used only for fsynced inodes after checkpoint.
301 */
302 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700303 clear_inode_flag(inode, FI_APPEND_WRITE);
304 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900305 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900306 }
Changman Lee51455b12014-12-08 15:29:41 +0900307sync_nodes:
Chao Yuc29fd0c2018-06-04 23:20:36 +0800308 atomic_inc(&sbi->wb_sync_req[NODE]);
Chao Yu50fa53e2018-08-02 23:03:19 +0800309 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
Chao Yuc29fd0c2018-06-04 23:20:36 +0800310 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700311 if (ret)
312 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900313
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800314 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800315 if (unlikely(f2fs_cp_error(sbi))) {
316 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800317 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800318 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800319
Chao Yu4d57b862018-05-30 00:20:41 +0800320 if (f2fs_need_inode_block_update(sbi, ino)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700321 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900322 f2fs_write_inode(inode, NULL);
323 goto sync_nodes;
324 }
325
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700326 /*
327 * If it's atomic_write, it's just fine to keep write ordering. So
328 * here we don't need to wait for node write completion, since we use
329 * node chain which serializes node blocks. If one of node writes are
330 * reordered, we can see simply broken chain, resulting in stopping
331 * roll-forward recovery. It means we'll recover all or none node blocks
332 * given fsync mark.
333 */
334 if (!atomic) {
Chao Yu50fa53e2018-08-02 23:03:19 +0800335 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
Jaegeuk Kimb6a245e2017-07-28 02:29:12 -0700336 if (ret)
337 goto out;
338 }
Changman Lee51455b12014-12-08 15:29:41 +0900339
340 /* once recovery info is written, don't need to tack this */
Chao Yu4d57b862018-05-30 00:20:41 +0800341 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700342 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900343flush_out:
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700344 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
Chao Yu39d787b2017-09-29 13:59:38 +0800345 ret = f2fs_issue_flush(sbi, inode->i_ino);
Chao Yu3f062522017-09-29 13:59:36 +0800346 if (!ret) {
Chao Yu4d57b862018-05-30 00:20:41 +0800347 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800348 clear_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu4d57b862018-05-30 00:20:41 +0800349 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
Chao Yu3f062522017-09-29 13:59:36 +0800350 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800351 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900352out:
Chao Yua5fd5052017-11-06 22:51:45 +0800353 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700354 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900355 return ret;
356}
357
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700358int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
359{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200360 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
361 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700362 return f2fs_do_sync_file(file, start, end, datasync, false);
363}
364
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900365static pgoff_t __get_first_dirty_index(struct address_space *mapping,
366 pgoff_t pgofs, int whence)
367{
Jan Kara8faab642017-11-15 17:34:55 -0800368 struct page *page;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900369 int nr_pages;
370
371 if (whence != SEEK_DATA)
372 return 0;
373
374 /* find first dirty page index */
Jan Kara8faab642017-11-15 17:34:55 -0800375 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
376 1, &page);
377 if (!nr_pages)
378 return ULONG_MAX;
379 pgofs = page->index;
380 put_page(page);
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900381 return pgofs;
382}
383
Chao Yue1da7872018-06-05 17:44:11 +0800384static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
385 pgoff_t dirty, pgoff_t pgofs, int whence)
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900386{
387 switch (whence) {
388 case SEEK_DATA:
389 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
Chao Yu93770ab2019-04-15 15:26:32 +0800390 __is_valid_data_blkaddr(blkaddr))
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900391 return true;
392 break;
393 case SEEK_HOLE:
394 if (blkaddr == NULL_ADDR)
395 return true;
396 break;
397 }
398 return false;
399}
400
Chao Yu267378d2014-04-23 14:10:24 +0800401static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
402{
403 struct inode *inode = file->f_mapping->host;
404 loff_t maxbytes = inode->i_sb->s_maxbytes;
405 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900406 pgoff_t pgofs, end_offset, dirty;
407 loff_t data_ofs = offset;
408 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800409 int err = 0;
410
Al Viro59551022016-01-22 15:40:57 -0500411 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800412
413 isize = i_size_read(inode);
414 if (offset >= isize)
415 goto fail;
416
417 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800418 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800419 if (whence == SEEK_HOLE)
420 data_ofs = isize;
421 goto found;
422 }
423
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300424 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800425
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900426 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
427
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300428 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800429 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800430 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800431 if (err && err != -ENOENT) {
432 goto fail;
433 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900434 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800435 if (whence == SEEK_DATA) {
Chao Yu4d57b862018-05-30 00:20:41 +0800436 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800437 continue;
438 } else {
439 goto found;
440 }
441 }
442
Chao Yu81ca7352016-01-26 15:39:35 +0800443 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800444
445 /* find data/hole in dnode block */
446 for (; dn.ofs_in_node < end_offset;
447 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300448 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800449 block_t blkaddr;
youngjun yoof11e98b2018-05-30 04:33:07 +0900450
Chao Yu7a2af762017-07-19 00:19:06 +0800451 blkaddr = datablock_addr(dn.inode,
452 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800453
Chao Yuc9b60782018-08-01 19:13:44 +0800454 if (__is_valid_data_blkaddr(blkaddr) &&
455 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
Chao Yu93770ab2019-04-15 15:26:32 +0800456 blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yuc9b60782018-08-01 19:13:44 +0800457 f2fs_put_dnode(&dn);
458 goto fail;
459 }
460
Chao Yue1da7872018-06-05 17:44:11 +0800461 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
462 pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800463 f2fs_put_dnode(&dn);
464 goto found;
465 }
466 }
467 f2fs_put_dnode(&dn);
468 }
469
470 if (whence == SEEK_DATA)
471 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800472found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900473 if (whence == SEEK_HOLE && data_ofs > isize)
474 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500475 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800476 return vfs_setpos(file, data_ofs, maxbytes);
477fail:
Al Viro59551022016-01-22 15:40:57 -0500478 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800479 return -ENXIO;
480}
481
482static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
483{
484 struct inode *inode = file->f_mapping->host;
485 loff_t maxbytes = inode->i_sb->s_maxbytes;
486
487 switch (whence) {
488 case SEEK_SET:
489 case SEEK_CUR:
490 case SEEK_END:
491 return generic_file_llseek_size(file, offset, whence,
492 maxbytes, i_size_read(inode));
493 case SEEK_DATA:
494 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700495 if (offset < 0)
496 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800497 return f2fs_seek_block(file, offset, whence);
498 }
499
500 return -EINVAL;
501}
502
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900503static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
504{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700505 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800506 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700507
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200508 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
509 return -EIO;
510
Chao Yu4c8ff702019-11-01 18:07:14 +0800511 if (!f2fs_is_compress_backend_ready(inode))
512 return -EOPNOTSUPP;
513
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700514 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800515 err = f2fs_convert_inline_inode(inode);
516 if (err)
517 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700518
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900519 file_accessed(file);
520 vma->vm_ops = &f2fs_file_vm_ops;
Chao Yu4c8ff702019-11-01 18:07:14 +0800521 set_inode_flag(inode, FI_MMAP_FILE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522 return 0;
523}
524
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700525static int f2fs_file_open(struct inode *inode, struct file *filp)
526{
Eric Biggers2e168c82017-11-29 12:35:28 -0800527 int err = fscrypt_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700528
Eric Biggers2e168c82017-11-29 12:35:28 -0800529 if (err)
530 return err;
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900531
Chao Yu4c8ff702019-11-01 18:07:14 +0800532 if (!f2fs_is_compress_backend_ready(inode))
533 return -EOPNOTSUPP;
534
Eric Biggers95ae2512019-07-22 09:26:24 -0700535 err = fsverity_file_open(inode, filp);
536 if (err)
537 return err;
538
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900539 filp->f_mode |= FMODE_NOWAIT;
540
Chao Yu0abd6752017-07-09 00:13:07 +0800541 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700542}
543
Chao Yu4d57b862018-05-30 00:20:41 +0800544void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700546 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900547 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800548 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900549 __le32 *addr;
Chao Yu7a2af762017-07-19 00:19:06 +0800550 int base = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800551 bool compressed_cluster = false;
552 int cluster_index = 0, valid_blocks = 0;
553 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
Chao Yu7a2af762017-07-19 00:19:06 +0800554
555 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
556 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900557
Gu Zheng45590712013-07-15 17:57:38 +0800558 raw_node = F2FS_NODE(dn->node_page);
Chao Yu7a2af762017-07-19 00:19:06 +0800559 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900560
Chao Yu4c8ff702019-11-01 18:07:14 +0800561 /* Assumption: truncateion starts with cluster */
562 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900563 block_t blkaddr = le32_to_cpu(*addr);
youngjun yoof11e98b2018-05-30 04:33:07 +0900564
Chao Yu4c8ff702019-11-01 18:07:14 +0800565 if (f2fs_compressed_file(dn->inode) &&
566 !(cluster_index & (cluster_size - 1))) {
567 if (compressed_cluster)
568 f2fs_i_compr_blocks_update(dn->inode,
569 valid_blocks, false);
570 compressed_cluster = (blkaddr == COMPRESS_ADDR);
571 valid_blocks = 0;
572 }
573
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900574 if (blkaddr == NULL_ADDR)
575 continue;
576
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800577 dn->data_blkaddr = NULL_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +0800578 f2fs_set_data_blkaddr(dn);
Chao Yuc9b60782018-08-01 19:13:44 +0800579
Chao Yu4c8ff702019-11-01 18:07:14 +0800580 if (__is_valid_data_blkaddr(blkaddr)) {
581 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800582 DATA_GENERIC_ENHANCE))
Chao Yu4c8ff702019-11-01 18:07:14 +0800583 continue;
584 if (compressed_cluster)
585 valid_blocks++;
586 }
Chao Yuc9b60782018-08-01 19:13:44 +0800587
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700588 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700589 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu4c8ff702019-11-01 18:07:14 +0800590
591 f2fs_invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900592 nr_free++;
593 }
Chao Yu19b2c302015-08-26 20:34:48 +0800594
Chao Yu4c8ff702019-11-01 18:07:14 +0800595 if (compressed_cluster)
596 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
597
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900598 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800599 pgoff_t fofs;
600 /*
601 * once we invalidate valid blkaddr in range [ofs, ofs + count],
602 * we will invalidate all blkaddr in the whole range.
603 */
Chao Yu4d57b862018-05-30 00:20:41 +0800604 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800605 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800606 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900607 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900608 }
609 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900610
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800611 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900612 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
613 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900614}
615
Chao Yu4d57b862018-05-30 00:20:41 +0800616void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900617{
Chao Yud02a6e62019-03-25 21:08:19 +0800618 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900619}
620
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800621static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700622 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900623{
youngjun yoo193bea12018-05-30 04:21:14 +0900624 loff_t offset = from & (PAGE_SIZE - 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300625 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700626 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900627 struct page *page;
628
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700629 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700630 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900631
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700632 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700633 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700634 if (page && PageUptodate(page))
635 goto truncate_out;
636 f2fs_put_page(page, 1);
637 return 0;
638 }
639
Chao Yu4c8ff702019-11-01 18:07:14 +0800640 if (f2fs_compressed_file(inode))
641 return 0;
642
Chao Yu4d57b862018-05-30 00:20:41 +0800643 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900644 if (IS_ERR(page))
Yunlei Hea78aaa22017-02-28 20:32:41 +0800645 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700646truncate_out:
Chao Yubae0ee72018-12-25 17:43:42 +0800647 f2fs_wait_on_page_writeback(page, DATA, true, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300648 zero_user(page, offset, PAGE_SIZE - offset);
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700649
650 /* An encrypted inode should have a key and truncate the last page. */
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530651 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
Jaegeuk Kima9bcf9b2017-06-14 08:05:32 -0700652 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800653 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900654 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700655 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656}
657
Chao Yu4c8ff702019-11-01 18:07:14 +0800658static int do_truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900659{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700660 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900661 struct dnode_of_data dn;
662 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800663 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700664 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800665 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900666
Namjae Jeon51dd6242013-04-20 01:28:52 +0900667 trace_f2fs_truncate_blocks_enter(inode, from);
668
Chao Yudf033ca2018-03-20 23:08:29 +0800669 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900670
Chao Yu09210c92016-05-05 19:13:03 +0800671 if (free_from >= sbi->max_file_blocks)
672 goto free_partial;
673
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700674 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800675 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800676
Chao Yu4d57b862018-05-30 00:20:41 +0800677 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700678 if (IS_ERR(ipage)) {
679 err = PTR_ERR(ipage);
680 goto out;
681 }
682
683 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800684 f2fs_truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700685 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800686 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700687 goto out;
688 }
689
690 set_new_dnode(&dn, inode, ipage, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800691 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900692 if (err) {
693 if (err == -ENOENT)
694 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700695 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700696 }
697
Chao Yu81ca7352016-01-26 15:39:35 +0800698 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900699
700 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700701 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900702
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900703 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800704 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900705 free_from += count;
706 }
707
708 f2fs_put_dnode(&dn);
709free_next:
Chao Yu4d57b862018-05-30 00:20:41 +0800710 err = f2fs_truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800711out:
712 if (lock)
Chao Yuc42d28c2019-02-02 17:33:01 +0800713 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800714free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700715 /* lastly zero out the first data page */
716 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800717 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900718
Namjae Jeon51dd6242013-04-20 01:28:52 +0900719 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900720 return err;
721}
722
Chao Yu4c8ff702019-11-01 18:07:14 +0800723int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
724{
725 u64 free_from = from;
726
727 /*
728 * for compressed file, only support cluster size
729 * aligned truncation.
730 */
731 if (f2fs_compressed_file(inode)) {
732 size_t cluster_shift = PAGE_SHIFT +
733 F2FS_I(inode)->i_log_cluster_size;
734 size_t cluster_mask = (1 << cluster_shift) - 1;
735
736 free_from = from >> cluster_shift;
737 if (from & cluster_mask)
738 free_from++;
739 free_from <<= cluster_shift;
740 }
741
742 return do_truncate_blocks(inode, free_from, lock);
743}
744
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700745int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900746{
Chao Yub0154892015-08-24 17:39:42 +0800747 int err;
748
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200749 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
750 return -EIO;
751
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900752 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
753 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800754 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900755
Namjae Jeon51dd6242013-04-20 01:28:52 +0900756 trace_f2fs_truncate(inode);
757
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800758 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800759 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
Jaegeuk Kim14b44d22017-03-09 15:24:24 -0800760 return -EIO;
761 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200762
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800763 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800764 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800765 err = f2fs_convert_inline_inode(inode);
766 if (err)
767 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800768 }
769
Chao Yuc42d28c2019-02-02 17:33:01 +0800770 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800771 if (err)
772 return err;
773
Deepa Dinamani078cd822016-09-14 07:48:04 -0700774 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700775 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800776 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900777}
778
David Howellsa528d352017-01-31 16:46:22 +0000779int f2fs_getattr(const struct path *path, struct kstat *stat,
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800780 u32 request_mask, unsigned int query_flags)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900781{
David Howellsa528d352017-01-31 16:46:22 +0000782 struct inode *inode = d_inode(path->dentry);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800783 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu1c1d35d2018-01-25 14:54:42 +0800784 struct f2fs_inode *ri;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800785 unsigned int flags;
786
Chao Yu1c1d35d2018-01-25 14:54:42 +0800787 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800788 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800789 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
790 stat->result_mask |= STATX_BTIME;
791 stat->btime.tv_sec = fi->i_crtime.tv_sec;
792 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
793 }
794
Eric Biggers36098552019-06-04 22:59:04 -0700795 flags = fi->i_flags;
Chao Yu59c84402018-04-03 15:08:17 +0800796 if (flags & F2FS_APPEND_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800797 stat->attributes |= STATX_ATTR_APPEND;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530798 if (IS_ENCRYPTED(inode))
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800799 stat->attributes |= STATX_ATTR_ENCRYPTED;
Chao Yu59c84402018-04-03 15:08:17 +0800800 if (flags & F2FS_IMMUTABLE_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800801 stat->attributes |= STATX_ATTR_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +0800802 if (flags & F2FS_NODUMP_FL)
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800803 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers924e3192019-10-29 13:41:40 -0700804 if (IS_VERITY(inode))
805 stat->attributes |= STATX_ATTR_VERITY;
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800806
807 stat->attributes_mask |= (STATX_ATTR_APPEND |
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800808 STATX_ATTR_ENCRYPTED |
809 STATX_ATTR_IMMUTABLE |
Eric Biggers924e3192019-10-29 13:41:40 -0700810 STATX_ATTR_NODUMP |
811 STATX_ATTR_VERITY);
Chao Yu1c6d8ee2017-05-03 23:59:12 +0800812
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900813 generic_fillattr(inode, stat);
Jaegeuk Kim5b4267d2017-10-13 10:27:45 -0700814
815 /* we need to show initial sectors used for inline_data/dentries */
816 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
817 f2fs_has_inline_dentry(inode))
818 stat->blocks += (stat->size + 511) >> 9;
819
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900820 return 0;
821}
822
823#ifdef CONFIG_F2FS_FS_POSIX_ACL
824static void __setattr_copy(struct inode *inode, const struct iattr *attr)
825{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900826 unsigned int ia_valid = attr->ia_valid;
827
828 if (ia_valid & ATTR_UID)
829 inode->i_uid = attr->ia_uid;
830 if (ia_valid & ATTR_GID)
831 inode->i_gid = attr->ia_gid;
Deepa Dinamani3818c192019-06-15 09:00:05 -0700832 if (ia_valid & ATTR_ATIME) {
833 inode->i_atime = timestamp_truncate(attr->ia_atime,
834 inode);
835 }
836 if (ia_valid & ATTR_MTIME) {
837 inode->i_mtime = timestamp_truncate(attr->ia_mtime,
838 inode);
839 }
840 if (ia_valid & ATTR_CTIME) {
841 inode->i_ctime = timestamp_truncate(attr->ia_ctime,
842 inode);
843 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900844 if (ia_valid & ATTR_MODE) {
845 umode_t mode = attr->ia_mode;
846
847 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
848 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700849 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900850 }
851}
852#else
853#define __setattr_copy setattr_copy
854#endif
855
856int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
857{
David Howells2b0143b2015-03-17 22:25:59 +0000858 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900859 int err;
860
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200861 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
862 return -EIO;
863
Chao Yu4c8ff702019-11-01 18:07:14 +0800864 if ((attr->ia_valid & ATTR_SIZE) &&
865 !f2fs_is_compress_backend_ready(inode))
866 return -EOPNOTSUPP;
867
Jan Kara31051c82016-05-26 16:55:18 +0200868 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900869 if (err)
870 return err;
871
Eric Biggers20bb2472017-11-29 12:35:32 -0800872 err = fscrypt_prepare_setattr(dentry, attr);
873 if (err)
874 return err;
875
Eric Biggers95ae2512019-07-22 09:26:24 -0700876 err = fsverity_prepare_setattr(dentry, attr);
877 if (err)
878 return err;
879
Chao Yu0abd6752017-07-09 00:13:07 +0800880 if (is_quota_modification(inode, attr)) {
881 err = dquot_initialize(inode);
882 if (err)
883 return err;
884 }
885 if ((attr->ia_valid & ATTR_UID &&
886 !uid_eq(attr->ia_uid, inode->i_uid)) ||
887 (attr->ia_valid & ATTR_GID &&
888 !gid_eq(attr->ia_gid, inode->i_gid))) {
Chao Yuaf033b22018-09-20 20:05:00 +0800889 f2fs_lock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800890 err = dquot_transfer(inode, attr);
Chao Yuaf033b22018-09-20 20:05:00 +0800891 if (err) {
892 set_sbi_flag(F2FS_I_SB(inode),
893 SBI_QUOTA_NEED_REPAIR);
894 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800895 return err;
Chao Yuaf033b22018-09-20 20:05:00 +0800896 }
897 /*
898 * update uid/gid under lock_op(), so that dquot and inode can
899 * be updated atomically.
900 */
901 if (attr->ia_valid & ATTR_UID)
902 inode->i_uid = attr->ia_uid;
903 if (attr->ia_valid & ATTR_GID)
904 inode->i_gid = attr->ia_gid;
905 f2fs_mark_inode_dirty_sync(inode, true);
906 f2fs_unlock_op(F2FS_I_SB(inode));
Chao Yu0abd6752017-07-09 00:13:07 +0800907 }
908
Chao Yu09db6a22014-09-15 18:02:09 +0800909 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800910 loff_t old_size = i_size_read(inode);
911
912 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
913 /*
914 * should convert inline inode before i_size_write to
915 * keep smaller than inline_data size with inline flag.
916 */
917 err = f2fs_convert_inline_inode(inode);
918 if (err)
919 return err;
920 }
Chao Yu0cab80e2015-12-01 11:36:16 +0800921
Chao Yua33c1502018-08-05 23:04:25 +0800922 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900923 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +0800924
925 truncate_setsize(inode, attr->ia_size);
926
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800927 if (attr->ia_size <= old_size)
Chao Yua33c1502018-08-05 23:04:25 +0800928 err = f2fs_truncate(inode);
929 /*
930 * do not trim all blocks after i_size if target size is
931 * larger than i_size.
932 */
Chao Yua33c1502018-08-05 23:04:25 +0800933 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +0900934 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yua33c1502018-08-05 23:04:25 +0800935 if (err)
936 return err;
937
Chao Yua0d00fa2017-10-09 17:55:19 +0800938 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimcfb9a342019-09-03 10:06:26 +0800939 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yua0d00fa2017-10-09 17:55:19 +0800940 F2FS_I(inode)->last_disk_size = i_size_read(inode);
941 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900942 }
943
944 __setattr_copy(inode, attr);
945
946 if (attr->ia_valid & ATTR_MODE) {
Chao Yu4d57b862018-05-30 00:20:41 +0800947 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700948 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
949 inode->i_mode = F2FS_I(inode)->i_acl_mode;
950 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900951 }
952 }
953
Yunlei Hec0ed4402016-12-11 15:35:15 +0800954 /* file size may changed here */
Chao Yuca597bd2019-02-23 09:48:27 +0800955 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim15d04352016-10-14 13:30:31 -0700956
957 /* inode change will produce dirty node pages flushed by checkpoint */
958 f2fs_balance_fs(F2FS_I_SB(inode), true);
959
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900960 return err;
961}
962
963const struct inode_operations f2fs_file_inode_operations = {
964 .getattr = f2fs_getattr,
965 .setattr = f2fs_setattr,
966 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800967 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900968#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900969 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900970#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900971 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900972};
973
Chao Yu63943282015-08-07 18:36:06 +0800974static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900975 loff_t start, loff_t len)
976{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700977 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900978 struct page *page;
979
980 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800981 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900982
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800983 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900984
Gu Zhenge4795562013-09-27 18:08:30 +0800985 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800986 page = f2fs_get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800987 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900988
Chao Yu63943282015-08-07 18:36:06 +0800989 if (IS_ERR(page))
990 return PTR_ERR(page);
991
Chao Yubae0ee72018-12-25 17:43:42 +0800992 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu63943282015-08-07 18:36:06 +0800993 zero_user(page, start, len);
994 set_page_dirty(page);
995 f2fs_put_page(page, 1);
996 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900997}
998
Chao Yu4d57b862018-05-30 00:20:41 +0800999int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001000{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001001 int err;
1002
Chao Yuea587112015-09-17 20:22:44 +08001003 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001004 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +08001005 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001006
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001007 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001008 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001009 if (err) {
Chao Yuea587112015-09-17 20:22:44 +08001010 if (err == -ENOENT) {
Chao Yu4d57b862018-05-30 00:20:41 +08001011 pg_start = f2fs_get_next_page_offset(&dn,
1012 pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001013 continue;
Chao Yuea587112015-09-17 20:22:44 +08001014 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001015 return err;
1016 }
1017
Chao Yu81ca7352016-01-26 15:39:35 +08001018 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +08001019 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1020
1021 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1022
Chao Yu4d57b862018-05-30 00:20:41 +08001023 f2fs_truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001024 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +08001025
1026 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001027 }
1028 return 0;
1029}
1030
Chao Yua66c7b22013-11-22 16:52:50 +08001031static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001032{
1033 pgoff_t pg_start, pg_end;
1034 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001035 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001036
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001037 ret = f2fs_convert_inline_inode(inode);
1038 if (ret)
1039 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001040
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1042 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001043
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001044 off_start = offset & (PAGE_SIZE - 1);
1045 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001046
1047 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001048 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001049 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001050 if (ret)
1051 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001052 } else {
Chao Yu63943282015-08-07 18:36:06 +08001053 if (off_start) {
1054 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001055 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001056 if (ret)
1057 return ret;
1058 }
1059 if (off_end) {
1060 ret = fill_zero(inode, pg_end, 0, off_end);
1061 if (ret)
1062 return ret;
1063 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001064
1065 if (pg_start < pg_end) {
1066 struct address_space *mapping = inode->i_mapping;
1067 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001068 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -05001069
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001070 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001071
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001072 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1073 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Chao Yua33c1502018-08-05 23:04:25 +08001074
Chao Yua33c1502018-08-05 23:04:25 +08001075 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001076 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08001077
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001078 truncate_inode_pages_range(mapping, blk_start,
1079 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001080
Gu Zhenge4795562013-09-27 18:08:30 +08001081 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001082 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +08001083 f2fs_unlock_op(sbi);
Chao Yua33c1502018-08-05 23:04:25 +08001084
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001085 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001086 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001087 }
1088 }
1089
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001090 return ret;
1091}
1092
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001093static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1094 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +08001095{
1096 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1097 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001098 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001099
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001100next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001101 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001102 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001103 if (ret && ret != -ENOENT) {
1104 return ret;
1105 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001106 if (dn.max_level == 0)
1107 return -ENOENT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001108 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1109 dn.ofs_in_node, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001110 blkaddr += done;
1111 do_replace += done;
1112 goto next;
1113 }
1114
1115 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1116 dn.ofs_in_node, len);
1117 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001118 *blkaddr = datablock_addr(dn.inode,
1119 dn.node_page, dn.ofs_in_node);
Chao Yu93770ab2019-04-15 15:26:32 +08001120
1121 if (__is_valid_data_blkaddr(*blkaddr) &&
1122 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1123 DATA_GENERIC_ENHANCE)) {
1124 f2fs_put_dnode(&dn);
Chao Yu10f966b2019-06-20 11:36:14 +08001125 return -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001126 }
1127
Chao Yu4d57b862018-05-30 00:20:41 +08001128 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001129
1130 if (test_opt(sbi, LFS)) {
1131 f2fs_put_dnode(&dn);
Chao Yufd114ab2019-08-15 19:45:36 +08001132 return -EOPNOTSUPP;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001133 }
1134
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001135 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +08001136 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001137 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001138 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001139 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001140 f2fs_put_dnode(&dn);
1141next:
1142 len -= done;
1143 off += done;
1144 if (len)
1145 goto next_dnode;
1146 return 0;
1147}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001148
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001149static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1150 int *do_replace, pgoff_t off, int len)
1151{
1152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1153 struct dnode_of_data dn;
1154 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001155
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001156 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1157 if (*do_replace == 0)
1158 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001159
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001160 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001161 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001162 if (ret) {
1163 dec_valid_block_count(sbi, inode, 1);
Chao Yu4d57b862018-05-30 00:20:41 +08001164 f2fs_invalidate_blocks(sbi, *blkaddr);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001165 } else {
1166 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001167 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001168 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001169 }
1170 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001171}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001172
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001173static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1174 block_t *blkaddr, int *do_replace,
1175 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1176{
1177 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1178 pgoff_t i = 0;
1179 int ret;
1180
1181 while (i < len) {
1182 if (blkaddr[i] == NULL_ADDR && !full) {
1183 i++;
1184 continue;
1185 }
1186
1187 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1188 struct dnode_of_data dn;
1189 struct node_info ni;
1190 size_t new_size;
1191 pgoff_t ilen;
1192
1193 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001194 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001195 if (ret)
1196 return ret;
1197
Chao Yu77357302018-07-17 00:02:17 +08001198 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1199 if (ret) {
1200 f2fs_put_dnode(&dn);
1201 return ret;
1202 }
1203
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001204 ilen = min((pgoff_t)
1205 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1206 dn.ofs_in_node, len - i);
1207 do {
Chao Yu7a2af762017-07-19 00:19:06 +08001208 dn.data_blkaddr = datablock_addr(dn.inode,
1209 dn.node_page, dn.ofs_in_node);
Chao Yu4d57b862018-05-30 00:20:41 +08001210 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001211
1212 if (do_replace[i]) {
1213 f2fs_i_blocks_write(src_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001214 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001215 f2fs_i_blocks_write(dst_inode,
Chao Yu0abd6752017-07-09 00:13:07 +08001216 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001217 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1218 blkaddr[i], ni.version, true, false);
1219
1220 do_replace[i] = 0;
1221 }
1222 dn.ofs_in_node++;
1223 i++;
Chao Yu1f0d5c92019-11-07 17:29:00 +08001224 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001225 if (dst_inode->i_size < new_size)
1226 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kime87f7322016-11-23 10:51:17 -08001227 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001228
1229 f2fs_put_dnode(&dn);
1230 } else {
1231 struct page *psrc, *pdst;
1232
Chao Yu4d57b862018-05-30 00:20:41 +08001233 psrc = f2fs_get_lock_data_page(src_inode,
1234 src + i, true);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001235 if (IS_ERR(psrc))
1236 return PTR_ERR(psrc);
Chao Yu4d57b862018-05-30 00:20:41 +08001237 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001238 true);
1239 if (IS_ERR(pdst)) {
1240 f2fs_put_page(psrc, 1);
1241 return PTR_ERR(pdst);
1242 }
1243 f2fs_copy_page(psrc, pdst);
1244 set_page_dirty(pdst);
1245 f2fs_put_page(pdst, 1);
1246 f2fs_put_page(psrc, 1);
1247
Chao Yu4d57b862018-05-30 00:20:41 +08001248 ret = f2fs_truncate_hole(src_inode,
1249 src + i, src + i + 1);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001250 if (ret)
1251 return ret;
1252 i++;
1253 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001254 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001255 return 0;
1256}
1257
1258static int __exchange_data_block(struct inode *src_inode,
1259 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001260 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001261{
1262 block_t *src_blkaddr;
1263 int *do_replace;
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001264 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001265 int ret;
1266
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001267 while (len) {
Chao Yud02a6e62019-03-25 21:08:19 +08001268 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001269
Chao Yu628b3d12017-11-30 19:28:18 +08001270 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001271 array_size(olen, sizeof(block_t)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001272 GFP_NOFS);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001273 if (!src_blkaddr)
1274 return -ENOMEM;
1275
Chao Yu628b3d12017-11-30 19:28:18 +08001276 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
Kees Cook9d2a7892018-06-12 14:28:35 -07001277 array_size(olen, sizeof(int)),
Jaegeuk Kim4f4460c2019-12-03 19:02:15 -08001278 GFP_NOFS);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001279 if (!do_replace) {
1280 kvfree(src_blkaddr);
1281 return -ENOMEM;
1282 }
1283
1284 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1285 do_replace, src, olen);
1286 if (ret)
1287 goto roll_back;
1288
1289 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1290 do_replace, src, dst, olen, full);
1291 if (ret)
1292 goto roll_back;
1293
1294 src += olen;
1295 dst += olen;
1296 len -= olen;
1297
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001298 kvfree(src_blkaddr);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001299 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001300 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001301 return 0;
1302
1303roll_back:
Chao Yu9fd62602018-05-28 23:47:19 +08001304 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001305 kvfree(src_blkaddr);
1306 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001307 return ret;
1308}
1309
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001310static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001311{
1312 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001313 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001314 pgoff_t start = offset >> PAGE_SHIFT;
1315 pgoff_t end = (offset + len) >> PAGE_SHIFT;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001316 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001317
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001318 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001319
1320 /* avoid gc operation during block exchange */
1321 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1322 down_write(&F2FS_I(inode)->i_mmap_sem);
1323
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001324 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001325 f2fs_drop_extent_tree(inode);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001326 truncate_pagecache(inode, offset);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001327 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1328 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001329
1330 up_write(&F2FS_I(inode)->i_mmap_sem);
1331 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yub4ace332015-05-06 13:09:46 +08001332 return ret;
1333}
1334
1335static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1336{
Chao Yub4ace332015-05-06 13:09:46 +08001337 loff_t new_size;
1338 int ret;
1339
Chao Yub4ace332015-05-06 13:09:46 +08001340 if (offset + len >= i_size_read(inode))
1341 return -EINVAL;
1342
1343 /* collapse range should be aligned to block size of f2fs. */
1344 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1345 return -EINVAL;
1346
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001347 ret = f2fs_convert_inline_inode(inode);
1348 if (ret)
1349 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001350
Chao Yub4ace332015-05-06 13:09:46 +08001351 /* write out all dirty pages from offset */
1352 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1353 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001354 return ret;
Chao Yubb066642017-11-03 10:21:05 +08001355
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001356 ret = f2fs_do_collapse(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001357 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001358 return ret;
Chao Yub4ace332015-05-06 13:09:46 +08001359
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001360 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001361 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001362 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1363 truncate_pagecache(inode, offset);
1364
Chao Yub4ace332015-05-06 13:09:46 +08001365 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001366 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001367
Chao Yuc42d28c2019-02-02 17:33:01 +08001368 ret = f2fs_truncate_blocks(inode, new_size, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001369 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001370 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001371 f2fs_i_size_write(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001372 return ret;
1373}
1374
Chao Yu6e961942016-05-09 19:56:31 +08001375static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1376 pgoff_t end)
1377{
1378 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1379 pgoff_t index = start;
1380 unsigned int ofs_in_node = dn->ofs_in_node;
1381 blkcnt_t count = 0;
1382 int ret;
1383
1384 for (; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001385 if (datablock_addr(dn->inode, dn->node_page,
1386 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001387 count++;
1388 }
1389
1390 dn->ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001391 ret = f2fs_reserve_new_blocks(dn, count);
Chao Yu6e961942016-05-09 19:56:31 +08001392 if (ret)
1393 return ret;
1394
1395 dn->ofs_in_node = ofs_in_node;
1396 for (index = start; index < end; index++, dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001397 dn->data_blkaddr = datablock_addr(dn->inode,
1398 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001399 /*
Chao Yu4d57b862018-05-30 00:20:41 +08001400 * f2fs_reserve_new_blocks will not guarantee entire block
Chao Yu6e961942016-05-09 19:56:31 +08001401 * allocation.
1402 */
1403 if (dn->data_blkaddr == NULL_ADDR) {
1404 ret = -ENOSPC;
1405 break;
1406 }
1407 if (dn->data_blkaddr != NEW_ADDR) {
Chao Yu4d57b862018-05-30 00:20:41 +08001408 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
Chao Yu6e961942016-05-09 19:56:31 +08001409 dn->data_blkaddr = NEW_ADDR;
Chao Yu4d57b862018-05-30 00:20:41 +08001410 f2fs_set_data_blkaddr(dn);
Chao Yu6e961942016-05-09 19:56:31 +08001411 }
1412 }
1413
1414 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1415
1416 return ret;
1417}
1418
Chao Yu75cd4e02015-05-06 13:11:13 +08001419static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1420 int mode)
1421{
1422 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1423 struct address_space *mapping = inode->i_mapping;
1424 pgoff_t index, pg_start, pg_end;
1425 loff_t new_size = i_size_read(inode);
1426 loff_t off_start, off_end;
1427 int ret = 0;
1428
Chao Yu75cd4e02015-05-06 13:11:13 +08001429 ret = inode_newsize_ok(inode, (len + offset));
1430 if (ret)
1431 return ret;
1432
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001433 ret = f2fs_convert_inline_inode(inode);
1434 if (ret)
1435 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001436
1437 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1438 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001439 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001440
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001441 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1442 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001443
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001444 off_start = offset & (PAGE_SIZE - 1);
1445 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001446
1447 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001448 ret = fill_zero(inode, pg_start, off_start,
1449 off_end - off_start);
1450 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001451 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001452
Chao Yu75cd4e02015-05-06 13:11:13 +08001453 new_size = max_t(loff_t, new_size, offset + len);
1454 } else {
1455 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001456 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001457 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001458 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001459 return ret;
Chao Yu63943282015-08-07 18:36:06 +08001460
Chao Yu75cd4e02015-05-06 13:11:13 +08001461 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001462 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001463 }
1464
Chao Yu6e961942016-05-09 19:56:31 +08001465 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001466 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001467 unsigned int end_offset;
1468 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001469
Chao Yuc70798532018-08-05 23:02:22 +08001470 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001471 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001472
1473 truncate_pagecache_range(inode,
1474 (loff_t)index << PAGE_SHIFT,
1475 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1476
Chao Yu75cd4e02015-05-06 13:11:13 +08001477 f2fs_lock_op(sbi);
1478
Chao Yu6e961942016-05-09 19:56:31 +08001479 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001480 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001481 if (ret) {
1482 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001483 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001484 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu75cd4e02015-05-06 13:11:13 +08001485 goto out;
1486 }
1487
Chao Yu6e961942016-05-09 19:56:31 +08001488 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1489 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1490
1491 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001492 f2fs_put_dnode(&dn);
Chao Yuc70798532018-08-05 23:02:22 +08001493
Chao Yu75cd4e02015-05-06 13:11:13 +08001494 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001495 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc70798532018-08-05 23:02:22 +08001496 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu9434fcd2016-10-11 22:57:02 +08001497
1498 f2fs_balance_fs(sbi, dn.node_changed);
1499
Chao Yu6e961942016-05-09 19:56:31 +08001500 if (ret)
1501 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001502
Chao Yu6e961942016-05-09 19:56:31 +08001503 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001504 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001505 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001506 }
1507
1508 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001509 ret = fill_zero(inode, pg_end, 0, off_end);
1510 if (ret)
1511 goto out;
1512
Chao Yu75cd4e02015-05-06 13:11:13 +08001513 new_size = max_t(loff_t, new_size, offset + len);
1514 }
1515 }
1516
1517out:
Chao Yu17cd07a2018-02-25 23:38:21 +08001518 if (new_size > i_size_read(inode)) {
1519 if (mode & FALLOC_FL_KEEP_SIZE)
1520 file_set_keep_isize(inode);
1521 else
1522 f2fs_i_size_write(inode, new_size);
1523 }
Chao Yu75cd4e02015-05-06 13:11:13 +08001524 return ret;
1525}
1526
Chao Yuf62185d2015-05-28 19:16:57 +08001527static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1528{
1529 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001530 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001531 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001532 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001533
Chao Yuf62185d2015-05-28 19:16:57 +08001534 new_size = i_size_read(inode) + len;
Kinglong Mee46e82fb2017-03-10 17:54:52 +08001535 ret = inode_newsize_ok(inode, new_size);
1536 if (ret)
1537 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001538
1539 if (offset >= i_size_read(inode))
1540 return -EINVAL;
1541
1542 /* insert range should be aligned to block size of f2fs. */
1543 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1544 return -EINVAL;
1545
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001546 ret = f2fs_convert_inline_inode(inode);
1547 if (ret)
1548 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001549
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001550 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001551
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001552 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuc42d28c2019-02-02 17:33:01 +08001553 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001554 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001555 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001556 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001557
1558 /* write out all dirty pages from offset */
1559 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1560 if (ret)
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001561 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001562
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001563 pg_start = offset >> PAGE_SHIFT;
1564 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001565 delta = pg_end - pg_start;
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02001566 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Chao Yuf62185d2015-05-28 19:16:57 +08001567
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001568 /* avoid gc operation during block exchange */
1569 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1570 down_write(&F2FS_I(inode)->i_mmap_sem);
1571 truncate_pagecache(inode, offset);
1572
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001573 while (!ret && idx > pg_start) {
1574 nr = idx - pg_start;
1575 if (nr > delta)
1576 nr = delta;
1577 idx -= nr;
1578
Chao Yuf62185d2015-05-28 19:16:57 +08001579 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001580 f2fs_drop_extent_tree(inode);
1581
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001582 ret = __exchange_data_block(inode, inode, idx,
1583 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001584 f2fs_unlock_op(sbi);
1585 }
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001586 up_write(&F2FS_I(inode)->i_mmap_sem);
1587 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yuf62185d2015-05-28 19:16:57 +08001588
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001589 /* write out all moved pages, if possible */
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001590 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001591 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1592 truncate_pagecache(inode, offset);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09001593 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001594
1595 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001596 f2fs_i_size_write(inode, new_size);
Chao Yuf62185d2015-05-28 19:16:57 +08001597 return ret;
1598}
1599
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001600static int expand_inode_data(struct inode *inode, loff_t offset,
1601 loff_t len, int mode)
1602{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001603 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001604 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001605 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1606 .m_may_create = true };
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001607 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001608 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001609 loff_t off_end;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001610 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001611
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001612 err = inode_newsize_ok(inode, (len + offset));
1613 if (err)
1614 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001615
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001616 err = f2fs_convert_inline_inode(inode);
1617 if (err)
1618 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001619
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001620 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001621
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001622 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001623 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001624
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001625 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1626 map.m_len = pg_end - map.m_lblk;
1627 if (off_end)
1628 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001629
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001630 if (!map.m_len)
1631 return 0;
Jaegeuk Kimcad38362019-06-26 18:23:05 -07001632
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07001633 if (f2fs_is_pinned_file(inode)) {
1634 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1635 sbi->log_blocks_per_seg;
1636 block_t done = 0;
1637
1638 if (map.m_len % sbi->blocks_per_seg)
1639 len += sbi->blocks_per_seg;
1640
1641 map.m_len = sbi->blocks_per_seg;
1642next_alloc:
1643 if (has_not_enough_free_secs(sbi, 0,
1644 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1645 mutex_lock(&sbi->gc_mutex);
1646 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1647 if (err && err != -ENODATA && err != -EAGAIN)
1648 goto out_err;
1649 }
1650
1651 down_write(&sbi->pin_sem);
1652 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1653 f2fs_allocate_new_segments(sbi, CURSEG_COLD_DATA);
1654 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1655 up_write(&sbi->pin_sem);
1656
1657 done += map.m_len;
1658 len -= map.m_len;
1659 map.m_lblk += map.m_len;
1660 if (!err && len)
1661 goto next_alloc;
1662
1663 map.m_len = done;
1664 } else {
1665 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1666 }
1667out_err:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001668 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001669 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001670
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001671 if (!map.m_len)
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001672 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001673
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001674 last_off = map.m_lblk + map.m_len - 1;
1675
1676 /* update new size to the failed position */
youngjun yoo1061fd42018-05-30 04:34:58 +09001677 new_size = (last_off == pg_end) ? offset + len :
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001678 (loff_t)(last_off + 1) << PAGE_SHIFT;
1679 } else {
1680 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001681 }
1682
Chao Yue8ed90a2017-11-05 21:53:30 +08001683 if (new_size > i_size_read(inode)) {
1684 if (mode & FALLOC_FL_KEEP_SIZE)
1685 file_set_keep_isize(inode);
1686 else
1687 f2fs_i_size_write(inode, new_size);
1688 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001689
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001690 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001691}
1692
1693static long f2fs_fallocate(struct file *file, int mode,
1694 loff_t offset, loff_t len)
1695{
Al Viro6131ffa2013-02-27 16:59:05 -05001696 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001697 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001698
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001699 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1700 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08001701 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1702 return -ENOSPC;
Chao Yu4c8ff702019-11-01 18:07:14 +08001703 if (!f2fs_is_compress_backend_ready(inode))
1704 return -EOPNOTSUPP;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001705
Chao Yuc9980122015-09-11 14:39:02 +08001706 /* f2fs only support ->fallocate for regular file */
1707 if (!S_ISREG(inode->i_mode))
1708 return -EINVAL;
1709
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301710 if (IS_ENCRYPTED(inode) &&
Chao Yuf62185d2015-05-28 19:16:57 +08001711 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001712 return -EOPNOTSUPP;
1713
Chao Yu4c8ff702019-11-01 18:07:14 +08001714 if (f2fs_compressed_file(inode) &&
1715 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1716 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1717 return -EOPNOTSUPP;
1718
Chao Yub4ace332015-05-06 13:09:46 +08001719 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001720 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1721 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001722 return -EOPNOTSUPP;
1723
Al Viro59551022016-01-22 15:40:57 -05001724 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001725
Taehee Yoo587c0a42015-04-21 15:59:12 +09001726 if (mode & FALLOC_FL_PUNCH_HOLE) {
1727 if (offset >= inode->i_size)
1728 goto out;
1729
Chao Yua66c7b22013-11-22 16:52:50 +08001730 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001731 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1732 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001733 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1734 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001735 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1736 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001737 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001738 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001739 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001740
Namjae Jeon3af60a42012-12-30 14:52:37 +09001741 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001742 inode->i_mtime = inode->i_ctime = current_time(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001743 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001744 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001745 }
Chao Yu3375f692014-01-28 10:29:26 +08001746
Taehee Yoo587c0a42015-04-21 15:59:12 +09001747out:
Al Viro59551022016-01-22 15:40:57 -05001748 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001749
Namjae Jeonc01e2852013-04-23 17:00:52 +09001750 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001751 return ret;
1752}
1753
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001754static int f2fs_release_file(struct inode *inode, struct file *filp)
1755{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001756 /*
1757 * f2fs_relase_file is called at every close calls. So we should
1758 * not drop any inmemory pages by close called by other process.
1759 */
1760 if (!(filp->f_mode & FMODE_WRITE) ||
1761 atomic_read(&inode->i_writecount) != 1)
1762 return 0;
1763
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001764 /* some remained atomic pages should discarded */
1765 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001766 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001767 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001768 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001769 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001770 clear_inode_flag(inode, FI_DROP_CACHE);
Chao Yudfa74282018-06-04 23:20:51 +08001771 clear_inode_flag(inode, FI_VOLATILE_FILE);
1772 stat_dec_volatile_write(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001773 }
1774 return 0;
1775}
1776
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001777static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001778{
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001779 struct inode *inode = file_inode(file);
1780
1781 /*
1782 * If the process doing a transaction is crashed, we should do
1783 * roll-back. Otherwise, other reader/write can see corrupted database
1784 * until all the writers close its file. Since this should be done
1785 * before dropping file lock, it needs to do in ->flush.
1786 */
1787 if (f2fs_is_atomic_file(inode) &&
1788 F2FS_I(inode)->inmem_task == current)
Chao Yu4d57b862018-05-30 00:20:41 +08001789 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07001790 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001791}
1792
Eric Biggers36098552019-06-04 22:59:04 -07001793static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
Chao Yu2c1d0302017-07-29 00:32:52 +08001794{
1795 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08001796
1797 /* Is it quota file? Do not allow user to mess with it */
1798 if (IS_NOQUOTA(inode))
1799 return -EPERM;
1800
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001801 if ((iflags ^ fi->i_flags) & F2FS_CASEFOLD_FL) {
1802 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1803 return -EOPNOTSUPP;
1804 if (!f2fs_empty_dir(inode))
1805 return -ENOTEMPTY;
1806 }
1807
Chao Yu4c8ff702019-11-01 18:07:14 +08001808 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1809 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1810 return -EOPNOTSUPP;
1811 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1812 return -EINVAL;
1813 }
1814
1815 if ((iflags ^ fi->i_flags) & F2FS_COMPR_FL) {
1816 if (S_ISREG(inode->i_mode) &&
1817 (fi->i_flags & F2FS_COMPR_FL || i_size_read(inode) ||
1818 F2FS_HAS_BLOCKS(inode)))
1819 return -EINVAL;
1820 if (iflags & F2FS_NOCOMP_FL)
1821 return -EINVAL;
1822 if (iflags & F2FS_COMPR_FL) {
1823 int err = f2fs_convert_inline_inode(inode);
1824
1825 if (err)
1826 return err;
1827
1828 if (!f2fs_may_compress(inode))
1829 return -EINVAL;
1830
1831 set_compress_context(inode);
1832 }
1833 }
1834 if ((iflags ^ fi->i_flags) & F2FS_NOCOMP_FL) {
1835 if (fi->i_flags & F2FS_COMPR_FL)
1836 return -EINVAL;
1837 }
1838
Eric Biggersd5e5efa2019-07-01 13:26:30 -07001839 fi->i_flags = iflags | (fi->i_flags & ~mask);
Chao Yu4c8ff702019-11-01 18:07:14 +08001840 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1841 (fi->i_flags & F2FS_NOCOMP_FL));
Chao Yu2c1d0302017-07-29 00:32:52 +08001842
Chao Yu59c84402018-04-03 15:08:17 +08001843 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu2c1d0302017-07-29 00:32:52 +08001844 set_inode_flag(inode, FI_PROJ_INHERIT);
1845 else
1846 clear_inode_flag(inode, FI_PROJ_INHERIT);
1847
1848 inode->i_ctime = current_time(inode);
1849 f2fs_set_inode_flags(inode);
Chao Yub32e0192018-12-18 19:20:17 +08001850 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu2c1d0302017-07-29 00:32:52 +08001851 return 0;
1852}
1853
Eric Biggers36098552019-06-04 22:59:04 -07001854/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1855
1856/*
1857 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1858 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1859 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1860 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1861 */
1862
1863static const struct {
1864 u32 iflag;
1865 u32 fsflag;
1866} f2fs_fsflags_map[] = {
Chao Yu4c8ff702019-11-01 18:07:14 +08001867 { F2FS_COMPR_FL, FS_COMPR_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001868 { F2FS_SYNC_FL, FS_SYNC_FL },
1869 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1870 { F2FS_APPEND_FL, FS_APPEND_FL },
1871 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1872 { F2FS_NOATIME_FL, FS_NOATIME_FL },
Chao Yu4c8ff702019-11-01 18:07:14 +08001873 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001874 { F2FS_INDEX_FL, FS_INDEX_FL },
1875 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1876 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001877 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
Eric Biggers36098552019-06-04 22:59:04 -07001878};
1879
1880#define F2FS_GETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001881 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001882 FS_SYNC_FL | \
1883 FS_IMMUTABLE_FL | \
1884 FS_APPEND_FL | \
1885 FS_NODUMP_FL | \
1886 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001887 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001888 FS_INDEX_FL | \
1889 FS_DIRSYNC_FL | \
1890 FS_PROJINHERIT_FL | \
1891 FS_ENCRYPT_FL | \
1892 FS_INLINE_DATA_FL | \
Eric Biggers95ae2512019-07-22 09:26:24 -07001893 FS_NOCOW_FL | \
Linus Torvaldsfbc246a2019-09-21 14:26:33 -07001894 FS_VERITY_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001895 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001896
1897#define F2FS_SETTABLE_FS_FL ( \
Chao Yu4c8ff702019-11-01 18:07:14 +08001898 FS_COMPR_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001899 FS_SYNC_FL | \
1900 FS_IMMUTABLE_FL | \
1901 FS_APPEND_FL | \
1902 FS_NODUMP_FL | \
1903 FS_NOATIME_FL | \
Chao Yu4c8ff702019-11-01 18:07:14 +08001904 FS_NOCOMP_FL | \
Eric Biggers36098552019-06-04 22:59:04 -07001905 FS_DIRSYNC_FL | \
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001906 FS_PROJINHERIT_FL | \
1907 FS_CASEFOLD_FL)
Eric Biggers36098552019-06-04 22:59:04 -07001908
1909/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1910static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1911{
1912 u32 fsflags = 0;
1913 int i;
1914
1915 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1916 if (iflags & f2fs_fsflags_map[i].iflag)
1917 fsflags |= f2fs_fsflags_map[i].fsflag;
1918
1919 return fsflags;
1920}
1921
1922/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1923static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1924{
1925 u32 iflags = 0;
1926 int i;
1927
1928 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1929 if (fsflags & f2fs_fsflags_map[i].fsflag)
1930 iflags |= f2fs_fsflags_map[i].iflag;
1931
1932 return iflags;
1933}
1934
1935static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1936{
1937 struct inode *inode = file_inode(filp);
1938 struct f2fs_inode_info *fi = F2FS_I(inode);
1939 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1940
1941 if (IS_ENCRYPTED(inode))
1942 fsflags |= FS_ENCRYPT_FL;
Eric Biggers95ae2512019-07-22 09:26:24 -07001943 if (IS_VERITY(inode))
1944 fsflags |= FS_VERITY_FL;
Eric Biggers36098552019-06-04 22:59:04 -07001945 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1946 fsflags |= FS_INLINE_DATA_FL;
1947 if (is_inode_flag_set(inode, FI_PIN_FILE))
1948 fsflags |= FS_NOCOW_FL;
1949
1950 fsflags &= F2FS_GETTABLE_FS_FL;
1951
1952 return put_user(fsflags, (int __user *)arg);
1953}
1954
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001955static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1956{
1957 struct inode *inode = file_inode(filp);
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001958 struct f2fs_inode_info *fi = F2FS_I(inode);
1959 u32 fsflags, old_fsflags;
Eric Biggers36098552019-06-04 22:59:04 -07001960 u32 iflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001961 int ret;
1962
Chao Yu7fb17fe2016-05-09 19:56:32 +08001963 if (!inode_owner_or_capable(inode))
1964 return -EACCES;
1965
Eric Biggers36098552019-06-04 22:59:04 -07001966 if (get_user(fsflags, (int __user *)arg))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001967 return -EFAULT;
1968
Eric Biggers36098552019-06-04 22:59:04 -07001969 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1970 return -EOPNOTSUPP;
1971 fsflags &= F2FS_SETTABLE_FS_FL;
1972
1973 iflags = f2fs_fsflags_to_iflags(fsflags);
1974 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1975 return -EOPNOTSUPP;
1976
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001977 ret = mnt_want_write_file(filp);
1978 if (ret)
1979 return ret;
1980
Al Viro59551022016-01-22 15:40:57 -05001981 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001982
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001983 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1984 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1985 if (ret)
1986 goto out;
1987
Eric Biggers36098552019-06-04 22:59:04 -07001988 ret = f2fs_setflags_common(inode, iflags,
1989 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
Eric Biggersa1f32ee2019-07-01 13:26:28 -07001990out:
Chao Yua72d4b92017-05-03 23:59:13 +08001991 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001992 mnt_drop_write_file(filp);
1993 return ret;
1994}
1995
Chao Yud49f3e82015-01-23 20:36:04 +08001996static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1997{
1998 struct inode *inode = file_inode(filp);
1999
2000 return put_user(inode->i_generation, (int __user *)arg);
2001}
2002
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002003static int f2fs_ioc_start_atomic_write(struct file *filp)
2004{
2005 struct inode *inode = file_inode(filp);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002006 struct f2fs_inode_info *fi = F2FS_I(inode);
2007 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf4c9c742015-07-17 18:06:35 +08002008 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002009
2010 if (!inode_owner_or_capable(inode))
2011 return -EACCES;
2012
Jaegeuk Kime8118982017-03-17 10:04:15 +08002013 if (!S_ISREG(inode->i_mode))
2014 return -EINVAL;
2015
Chao Yu038d0692019-07-25 22:39:11 +08002016 if (filp->f_flags & O_DIRECT)
2017 return -EINVAL;
2018
Chao Yu7fb17fe2016-05-09 19:56:32 +08002019 ret = mnt_want_write_file(filp);
2020 if (ret)
2021 return ret;
2022
Chao Yu0fac5582016-05-09 19:56:33 +08002023 inode_lock(inode);
2024
Chao Yu4c8ff702019-11-01 18:07:14 +08002025 f2fs_disable_compressed_file(inode);
2026
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002027 if (f2fs_is_atomic_file(inode)) {
2028 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2029 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002030 goto out;
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002031 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002032
Chao Yuf4c9c742015-07-17 18:06:35 +08002033 ret = f2fs_convert_inline_inode(inode);
2034 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08002035 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002036
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002037 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2038
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002039 /*
2040 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2041 * f2fs_is_atomic_file.
2042 */
2043 if (get_dirty_pages(inode))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002044 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2045 inode->i_ino, get_dirty_pages(inode));
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002046 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002047 if (ret) {
2048 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002049 goto out;
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002050 }
Jaegeuk Kim31867b22018-12-28 11:00:38 -08002051
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002052 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2053 if (list_empty(&fi->inmem_ilist))
2054 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
Sahitya Tummala677017d2019-11-13 16:01:03 +05302055 sbi->atomic_files++;
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002056 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2057
2058 /* add inode in inmem_list first and set atomic_file */
Yunlei He054afda2018-04-18 11:06:39 +08002059 set_inode_flag(inode, FI_ATOMIC_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08002060 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002061 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002062
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002063 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07002064 F2FS_I(inode)->inmem_task = current;
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002065 stat_update_max_atomic_write(inode);
Kinglong Mee684ca7e2017-03-18 09:20:55 +08002066out:
Chao Yu0fac5582016-05-09 19:56:33 +08002067 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002068 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07002069 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002070}
2071
2072static int f2fs_ioc_commit_atomic_write(struct file *filp)
2073{
2074 struct inode *inode = file_inode(filp);
2075 int ret;
2076
2077 if (!inode_owner_or_capable(inode))
2078 return -EACCES;
2079
2080 ret = mnt_want_write_file(filp);
2081 if (ret)
2082 return ret;
2083
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002084 f2fs_balance_fs(F2FS_I_SB(inode), true);
Chao Yu0fac5582016-05-09 19:56:33 +08002085
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002086 inode_lock(inode);
Chao Yu1dc0f892018-02-27 22:45:24 +08002087
Chao Yub169c3c2018-04-18 17:45:02 +08002088 if (f2fs_is_volatile_file(inode)) {
2089 ret = -EINVAL;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002090 goto err_out;
Chao Yub169c3c2018-04-18 17:45:02 +08002091 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002092
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07002093 if (f2fs_is_atomic_file(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002094 ret = f2fs_commit_inmem_pages(inode);
Chao Yu5fe45742017-01-07 18:50:26 +08002095 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002096 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002097
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002098 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim743b6202019-09-09 13:10:59 +01002099 if (!ret)
2100 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim26a28a02016-12-28 13:55:09 -08002101 } else {
Chao Yu774e1b72017-08-23 18:23:25 +08002102 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002103 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07002104err_out:
Chao Yu2ef79ec2018-05-07 20:28:54 +08002105 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2106 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2107 ret = -EINVAL;
2108 }
Chao Yu0fac5582016-05-09 19:56:33 +08002109 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002110 mnt_drop_write_file(filp);
2111 return ret;
2112}
2113
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002114static int f2fs_ioc_start_volatile_write(struct file *filp)
2115{
2116 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002117 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002118
2119 if (!inode_owner_or_capable(inode))
2120 return -EACCES;
2121
Chao Yu8ff09712017-03-17 15:43:57 +08002122 if (!S_ISREG(inode->i_mode))
2123 return -EINVAL;
2124
Chao Yu7fb17fe2016-05-09 19:56:32 +08002125 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08002126 if (ret)
2127 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002128
Chao Yu0fac5582016-05-09 19:56:33 +08002129 inode_lock(inode);
2130
Chao Yu7fb17fe2016-05-09 19:56:32 +08002131 if (f2fs_is_volatile_file(inode))
2132 goto out;
2133
2134 ret = f2fs_convert_inline_inode(inode);
2135 if (ret)
2136 goto out;
2137
Chao Yu648d50b2017-03-22 17:23:45 +08002138 stat_inc_volatile_write(inode);
2139 stat_update_max_volatile_write(inode);
2140
Jaegeuk Kim91942322016-05-20 10:13:22 -07002141 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002142 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002143out:
Chao Yu0fac5582016-05-09 19:56:33 +08002144 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002145 mnt_drop_write_file(filp);
2146 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002147}
2148
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002149static int f2fs_ioc_release_volatile_write(struct file *filp)
2150{
2151 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002152 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002153
2154 if (!inode_owner_or_capable(inode))
2155 return -EACCES;
2156
Chao Yu7fb17fe2016-05-09 19:56:32 +08002157 ret = mnt_want_write_file(filp);
2158 if (ret)
2159 return ret;
2160
Chao Yu0fac5582016-05-09 19:56:33 +08002161 inode_lock(inode);
2162
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002163 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08002164 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002165
Chao Yu7fb17fe2016-05-09 19:56:32 +08002166 if (!f2fs_is_first_block_written(inode)) {
2167 ret = truncate_partial_data_page(inode, 0, true);
2168 goto out;
2169 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002170
Chao Yu7fb17fe2016-05-09 19:56:32 +08002171 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2172out:
Chao Yu0fac5582016-05-09 19:56:33 +08002173 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002174 mnt_drop_write_file(filp);
2175 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002176}
2177
2178static int f2fs_ioc_abort_volatile_write(struct file *filp)
2179{
2180 struct inode *inode = file_inode(filp);
2181 int ret;
2182
2183 if (!inode_owner_or_capable(inode))
2184 return -EACCES;
2185
2186 ret = mnt_want_write_file(filp);
2187 if (ret)
2188 return ret;
2189
Chao Yu0fac5582016-05-09 19:56:33 +08002190 inode_lock(inode);
2191
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07002192 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08002193 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002194 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002195 clear_inode_flag(inode, FI_VOLATILE_FILE);
Chao Yu648d50b2017-03-22 17:23:45 +08002196 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07002197 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08002198 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07002199
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002200 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2201
Chao Yu0fac5582016-05-09 19:56:33 +08002202 inode_unlock(inode);
2203
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002204 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002205 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002206 return ret;
2207}
2208
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002209static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2210{
2211 struct inode *inode = file_inode(filp);
2212 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2213 struct super_block *sb = sbi->sb;
2214 __u32 in;
Dan Carpenter2a96d8a2018-06-20 13:39:53 +03002215 int ret = 0;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002216
2217 if (!capable(CAP_SYS_ADMIN))
2218 return -EPERM;
2219
2220 if (get_user(in, (__u32 __user *)arg))
2221 return -EFAULT;
2222
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302223 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2224 ret = mnt_want_write_file(filp);
2225 if (ret)
2226 return ret;
2227 }
Chao Yu7fb17fe2016-05-09 19:56:32 +08002228
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002229 switch (in) {
2230 case F2FS_GOING_DOWN_FULLSYNC:
2231 sb = freeze_bdev(sb->s_bdev);
Chao Yud027c482018-01-17 22:28:52 +08002232 if (IS_ERR(sb)) {
2233 ret = PTR_ERR(sb);
2234 goto out;
2235 }
2236 if (sb) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002237 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002238 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002239 thaw_bdev(sb->s_bdev, sb);
2240 }
2241 break;
2242 case F2FS_GOING_DOWN_METASYNC:
2243 /* do checkpoint only */
Chao Yud027c482018-01-17 22:28:52 +08002244 ret = f2fs_sync_fs(sb, 1);
2245 if (ret)
2246 goto out;
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002247 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002248 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002249 break;
2250 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002251 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002252 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002253 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002254 case F2FS_GOING_DOWN_METAFLUSH:
Chao Yu4d57b862018-05-30 00:20:41 +08002255 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07002256 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07002257 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07002258 break;
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002259 case F2FS_GOING_DOWN_NEED_FSCK:
2260 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002261 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2262 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim0cd6d9b2018-11-28 13:26:03 -08002263 /* do checkpoint only */
2264 ret = f2fs_sync_fs(sb, 1);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08002265 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002266 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08002267 ret = -EINVAL;
2268 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002269 }
Chao Yu7950e9a2018-01-18 17:23:29 +08002270
Chao Yu4d57b862018-05-30 00:20:41 +08002271 f2fs_stop_gc_thread(sbi);
2272 f2fs_stop_discard_thread(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002273
Chao Yu4d57b862018-05-30 00:20:41 +08002274 f2fs_drop_discard_cmd(sbi);
Chao Yu7950e9a2018-01-18 17:23:29 +08002275 clear_opt(sbi, DISCARD);
2276
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002277 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002278out:
Sahitya Tummala60b2b4e2018-05-18 11:51:52 +05302279 if (in != F2FS_GOING_DOWN_FULLSYNC)
2280 mnt_drop_write_file(filp);
Chao Yu559e87c2019-02-26 19:01:15 +08002281
2282 trace_f2fs_shutdown(sbi, in, ret);
2283
Chao Yu7fb17fe2016-05-09 19:56:32 +08002284 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002285}
2286
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002287static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2288{
2289 struct inode *inode = file_inode(filp);
2290 struct super_block *sb = inode->i_sb;
2291 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2292 struct fstrim_range range;
2293 int ret;
2294
2295 if (!capable(CAP_SYS_ADMIN))
2296 return -EPERM;
2297
Chao Yu7d20c8a2018-09-04 03:52:17 +08002298 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002299 return -EOPNOTSUPP;
2300
2301 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2302 sizeof(range)))
2303 return -EFAULT;
2304
Chao Yu7fb17fe2016-05-09 19:56:32 +08002305 ret = mnt_want_write_file(filp);
2306 if (ret)
2307 return ret;
2308
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002309 range.minlen = max((unsigned int)range.minlen,
2310 q->limits.discard_granularity);
2311 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002312 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002313 if (ret < 0)
2314 return ret;
2315
2316 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2317 sizeof(range)))
2318 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002319 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002320 return 0;
2321}
2322
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002323static bool uuid_is_nonzero(__u8 u[16])
2324{
2325 int i;
2326
2327 for (i = 0; i < 16; i++)
2328 if (u[i])
2329 return true;
2330 return false;
2331}
2332
2333static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2334{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002335 struct inode *inode = file_inode(filp);
2336
Chao Yu7beb01f2018-10-24 18:34:26 +08002337 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
Chao Yuead710b2017-11-14 19:28:42 +08002338 return -EOPNOTSUPP;
2339
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002340 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002341
Eric Biggersdb717d82016-11-26 19:07:49 -05002342 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002343}
2344
2345static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2346{
Chao Yu7beb01f2018-10-24 18:34:26 +08002347 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
Chao Yuead710b2017-11-14 19:28:42 +08002348 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -05002349 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002350}
2351
2352static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2353{
2354 struct inode *inode = file_inode(filp);
2355 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2356 int err;
2357
Chao Yu7beb01f2018-10-24 18:34:26 +08002358 if (!f2fs_sb_has_encrypt(sbi))
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002359 return -EOPNOTSUPP;
2360
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002361 err = mnt_want_write_file(filp);
2362 if (err)
2363 return err;
2364
Chao Yu846ae672018-02-26 22:04:13 +08002365 down_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002366
2367 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2368 goto got_it;
2369
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002370 /* update superblock with uuid */
2371 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2372
Chao Yuc5bda1c2015-06-08 13:28:03 +08002373 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002374 if (err) {
2375 /* undo new data */
2376 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002377 goto out_err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002378 }
2379got_it:
2380 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2381 16))
Chao Yud0d3f1b2018-02-11 22:53:20 +08002382 err = -EFAULT;
2383out_err:
Chao Yu846ae672018-02-26 22:04:13 +08002384 up_write(&sbi->sb_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002385 mnt_drop_write_file(filp);
2386 return err;
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002387}
2388
Eric Biggers8ce589c2019-08-04 19:35:48 -07002389static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2390 unsigned long arg)
2391{
2392 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2393 return -EOPNOTSUPP;
2394
2395 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2396}
2397
2398static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2399{
2400 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2401 return -EOPNOTSUPP;
2402
2403 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2404}
2405
2406static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2407{
2408 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2409 return -EOPNOTSUPP;
2410
2411 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2412}
2413
2414static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2415 unsigned long arg)
2416{
2417 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2418 return -EOPNOTSUPP;
2419
2420 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2421}
2422
2423static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2424 unsigned long arg)
2425{
2426 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2427 return -EOPNOTSUPP;
2428
2429 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2430}
2431
Chao Yuc1c1b582015-07-10 18:08:10 +08002432static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2433{
2434 struct inode *inode = file_inode(filp);
2435 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08002436 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002437 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002438
2439 if (!capable(CAP_SYS_ADMIN))
2440 return -EPERM;
2441
Chao Yud530d4d2015-10-05 22:22:44 +08002442 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08002443 return -EFAULT;
2444
Chao Yud530d4d2015-10-05 22:22:44 +08002445 if (f2fs_readonly(sbi->sb))
2446 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08002447
Chao Yu7fb17fe2016-05-09 19:56:32 +08002448 ret = mnt_want_write_file(filp);
2449 if (ret)
2450 return ret;
2451
Chao Yud530d4d2015-10-05 22:22:44 +08002452 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08002453 if (!mutex_trylock(&sbi->gc_mutex)) {
2454 ret = -EBUSY;
2455 goto out;
2456 }
Chao Yud530d4d2015-10-05 22:22:44 +08002457 } else {
2458 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08002459 }
2460
Jaegeuk Kime066b832017-04-13 15:17:00 -07002461 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002462out:
2463 mnt_drop_write_file(filp);
2464 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002465}
2466
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002467static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2468{
2469 struct inode *inode = file_inode(filp);
2470 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2471 struct f2fs_gc_range range;
2472 u64 end;
2473 int ret;
2474
2475 if (!capable(CAP_SYS_ADMIN))
2476 return -EPERM;
2477
2478 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2479 sizeof(range)))
2480 return -EFAULT;
2481
2482 if (f2fs_readonly(sbi->sb))
2483 return -EROFS;
2484
Yunlei Heb82f6e32018-04-24 11:40:30 +08002485 end = range.start + range.len;
Sahitya Tummalafbbf7792019-09-17 10:19:23 +05302486 if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
2487 end >= MAX_BLKADDR(sbi))
Yunlei Heb82f6e32018-04-24 11:40:30 +08002488 return -EINVAL;
Yunlei Heb82f6e32018-04-24 11:40:30 +08002489
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002490 ret = mnt_want_write_file(filp);
2491 if (ret)
2492 return ret;
2493
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002494do_more:
2495 if (!range.sync) {
2496 if (!mutex_trylock(&sbi->gc_mutex)) {
2497 ret = -EBUSY;
2498 goto out;
2499 }
2500 } else {
2501 mutex_lock(&sbi->gc_mutex);
2502 }
2503
2504 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
Yunlong Song67b0e422018-10-30 20:37:55 +08002505 range.start += BLKS_PER_SEC(sbi);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07002506 if (range.start <= end)
2507 goto do_more;
2508out:
2509 mnt_drop_write_file(filp);
2510 return ret;
2511}
2512
Chao Yu059c0642018-07-17 20:41:49 +08002513static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
Chao Yu456b88e2015-10-05 22:24:19 +08002514{
2515 struct inode *inode = file_inode(filp);
2516 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002517 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002518
2519 if (!capable(CAP_SYS_ADMIN))
2520 return -EPERM;
2521
2522 if (f2fs_readonly(sbi->sb))
2523 return -EROFS;
2524
Daniel Rosenberg43549942018-08-20 19:21:43 -07002525 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002526 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002527 return -EINVAL;
2528 }
2529
Chao Yu7fb17fe2016-05-09 19:56:32 +08002530 ret = mnt_want_write_file(filp);
2531 if (ret)
2532 return ret;
2533
2534 ret = f2fs_sync_fs(sbi->sb, 1);
2535
2536 mnt_drop_write_file(filp);
2537 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002538}
2539
Chao Yud323d002015-10-27 09:53:45 +08002540static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2541 struct file *filp,
2542 struct f2fs_defragment *range)
2543{
2544 struct inode *inode = file_inode(filp);
Chao Yuf3d98e72018-01-10 18:18:52 +08002545 struct f2fs_map_blocks map = { .m_next_extent = NULL,
Jia Zhuf4f0b672018-11-20 04:29:35 +08002546 .m_seg_type = NO_CHECK_TYPE ,
2547 .m_may_create = false };
youngjun yoo1061fd42018-05-30 04:34:58 +09002548 struct extent_info ei = {0, 0, 0};
Chao Yuf3d98e72018-01-10 18:18:52 +08002549 pgoff_t pg_start, pg_end, next_pgofs;
Chao Yu3519e3f2015-12-01 11:56:52 +08002550 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002551 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002552 block_t blk_end = 0;
2553 bool fragmented = false;
2554 int err;
2555
2556 /* if in-place-update policy is enabled, don't waste time here */
Chao Yu4d57b862018-05-30 00:20:41 +08002557 if (f2fs_should_update_inplace(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002558 return -EINVAL;
2559
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002560 pg_start = range->start >> PAGE_SHIFT;
2561 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002562
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002563 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002564
Al Viro59551022016-01-22 15:40:57 -05002565 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002566
2567 /* writeback all dirty pages in the range */
2568 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002569 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002570 if (err)
2571 goto out;
2572
2573 /*
2574 * lookup mapping info in extent cache, skip defragmenting if physical
2575 * block addresses are continuous.
2576 */
2577 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2578 if (ei.fofs + ei.len >= pg_end)
2579 goto out;
2580 }
2581
2582 map.m_lblk = pg_start;
Chao Yuf3d98e72018-01-10 18:18:52 +08002583 map.m_next_pgofs = &next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002584
2585 /*
2586 * lookup mapping info in dnode page cache, skip defragmenting if all
2587 * physical block addresses are continuous even if there are hole(s)
2588 * in logical blocks.
2589 */
2590 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002591 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002592 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002593 if (err)
2594 goto out;
2595
2596 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002597 map.m_lblk = next_pgofs;
Chao Yud323d002015-10-27 09:53:45 +08002598 continue;
2599 }
2600
Chao Yu25a912e2018-01-10 18:18:51 +08002601 if (blk_end && blk_end != map.m_pblk)
Chao Yud323d002015-10-27 09:53:45 +08002602 fragmented = true;
Chao Yu25a912e2018-01-10 18:18:51 +08002603
2604 /* record total count of block that we're going to move */
2605 total += map.m_len;
2606
Chao Yud323d002015-10-27 09:53:45 +08002607 blk_end = map.m_pblk + map.m_len;
2608
2609 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002610 }
2611
Chao Yud3a1a0e2019-07-29 23:02:29 +08002612 if (!fragmented) {
2613 total = 0;
Chao Yud323d002015-10-27 09:53:45 +08002614 goto out;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002615 }
Chao Yud323d002015-10-27 09:53:45 +08002616
Geert Uytterhoevenf91108b2019-06-20 16:42:08 +02002617 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
Chao Yud323d002015-10-27 09:53:45 +08002618
2619 /*
2620 * make sure there are enough free section for LFS allocation, this can
2621 * avoid defragment running in SSR mode when free section are allocated
2622 * intensively
2623 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002624 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002625 err = -EAGAIN;
2626 goto out;
2627 }
2628
Chao Yu25a912e2018-01-10 18:18:51 +08002629 map.m_lblk = pg_start;
2630 map.m_len = pg_end - pg_start;
2631 total = 0;
2632
Chao Yud323d002015-10-27 09:53:45 +08002633 while (map.m_lblk < pg_end) {
2634 pgoff_t idx;
2635 int cnt = 0;
2636
2637do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002638 map.m_len = pg_end - map.m_lblk;
Qiuyang Sunf2220c72017-08-09 17:27:30 +08002639 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002640 if (err)
2641 goto clear_out;
2642
2643 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuf3d98e72018-01-10 18:18:52 +08002644 map.m_lblk = next_pgofs;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002645 goto check;
Chao Yud323d002015-10-27 09:53:45 +08002646 }
2647
Jaegeuk Kim91942322016-05-20 10:13:22 -07002648 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002649
2650 idx = map.m_lblk;
2651 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2652 struct page *page;
2653
Chao Yu4d57b862018-05-30 00:20:41 +08002654 page = f2fs_get_lock_data_page(inode, idx, true);
Chao Yud323d002015-10-27 09:53:45 +08002655 if (IS_ERR(page)) {
2656 err = PTR_ERR(page);
2657 goto clear_out;
2658 }
2659
2660 set_page_dirty(page);
2661 f2fs_put_page(page, 1);
2662
2663 idx++;
2664 cnt++;
2665 total++;
2666 }
2667
2668 map.m_lblk = idx;
Chao Yud3a1a0e2019-07-29 23:02:29 +08002669check:
2670 if (map.m_lblk < pg_end && cnt < blk_per_seg)
Chao Yud323d002015-10-27 09:53:45 +08002671 goto do_map;
2672
Jaegeuk Kim91942322016-05-20 10:13:22 -07002673 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002674
2675 err = filemap_fdatawrite(inode->i_mapping);
2676 if (err)
2677 goto out;
2678 }
2679clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002680 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002681out:
Al Viro59551022016-01-22 15:40:57 -05002682 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002683 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002684 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002685 return err;
2686}
2687
2688static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2689{
2690 struct inode *inode = file_inode(filp);
2691 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2692 struct f2fs_defragment range;
2693 int err;
2694
2695 if (!capable(CAP_SYS_ADMIN))
2696 return -EPERM;
2697
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002698 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
Chao Yud323d002015-10-27 09:53:45 +08002699 return -EINVAL;
2700
Kinglong Meed7563862017-03-10 17:55:07 +08002701 if (f2fs_readonly(sbi->sb))
2702 return -EROFS;
2703
2704 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2705 sizeof(range)))
2706 return -EFAULT;
2707
2708 /* verify alignment of offset & size */
2709 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2710 return -EINVAL;
2711
2712 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2713 sbi->max_file_blocks))
2714 return -EINVAL;
2715
Chao Yud323d002015-10-27 09:53:45 +08002716 err = mnt_want_write_file(filp);
2717 if (err)
2718 return err;
2719
Chao Yud323d002015-10-27 09:53:45 +08002720 err = f2fs_defragment_range(sbi, filp, &range);
Kinglong Meed7563862017-03-10 17:55:07 +08002721 mnt_drop_write_file(filp);
2722
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002723 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002724 if (err < 0)
Kinglong Meed7563862017-03-10 17:55:07 +08002725 return err;
Chao Yud323d002015-10-27 09:53:45 +08002726
2727 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2728 sizeof(range)))
Kinglong Meed7563862017-03-10 17:55:07 +08002729 return -EFAULT;
2730
2731 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002732}
2733
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002734static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2735 struct file *file_out, loff_t pos_out, size_t len)
2736{
2737 struct inode *src = file_inode(file_in);
2738 struct inode *dst = file_inode(file_out);
2739 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2740 size_t olen = len, dst_max_i_size = 0;
2741 size_t dst_osize;
2742 int ret;
2743
2744 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2745 src->i_sb != dst->i_sb)
2746 return -EXDEV;
2747
2748 if (unlikely(f2fs_readonly(src->i_sb)))
2749 return -EROFS;
2750
Chao Yufe8494b2016-08-04 20:13:02 +08002751 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2752 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002753
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302754 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002755 return -EOPNOTSUPP;
2756
Fan Lid95fd912016-09-13 11:35:42 +08002757 if (src == dst) {
2758 if (pos_in == pos_out)
2759 return 0;
2760 if (pos_out > pos_in && pos_out < pos_in + len)
2761 return -EINVAL;
2762 }
2763
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002764 inode_lock(src);
Chao Yu20a3d612016-08-04 20:13:03 +08002765 if (src != dst) {
Chao Yubb066642017-11-03 10:21:05 +08002766 ret = -EBUSY;
2767 if (!inode_trylock(dst))
2768 goto out;
Chao Yu20a3d612016-08-04 20:13:03 +08002769 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002770
2771 ret = -EINVAL;
2772 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2773 goto out_unlock;
2774 if (len == 0)
2775 olen = len = src->i_size - pos_in;
2776 if (pos_in + len == src->i_size)
2777 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2778 if (len == 0) {
2779 ret = 0;
2780 goto out_unlock;
2781 }
2782
2783 dst_osize = dst->i_size;
2784 if (pos_out + olen > dst->i_size)
2785 dst_max_i_size = pos_out + olen;
2786
2787 /* verify the end result is block aligned */
2788 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2789 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2790 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2791 goto out_unlock;
2792
2793 ret = f2fs_convert_inline_inode(src);
2794 if (ret)
2795 goto out_unlock;
2796
2797 ret = f2fs_convert_inline_inode(dst);
2798 if (ret)
2799 goto out_unlock;
2800
2801 /* write out all dirty pages from offset */
2802 ret = filemap_write_and_wait_range(src->i_mapping,
2803 pos_in, pos_in + len);
2804 if (ret)
2805 goto out_unlock;
2806
2807 ret = filemap_write_and_wait_range(dst->i_mapping,
2808 pos_out, pos_out + len);
2809 if (ret)
2810 goto out_unlock;
2811
2812 f2fs_balance_fs(sbi, true);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002813
2814 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2815 if (src != dst) {
2816 ret = -EBUSY;
2817 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2818 goto out_src;
2819 }
2820
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002821 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002822 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2823 pos_out >> F2FS_BLKSIZE_BITS,
2824 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002825
2826 if (!ret) {
2827 if (dst_max_i_size)
2828 f2fs_i_size_write(dst, dst_max_i_size);
2829 else if (dst_osize != dst->i_size)
2830 f2fs_i_size_write(dst, dst_osize);
2831 }
2832 f2fs_unlock_op(sbi);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002833
2834 if (src != dst)
Chao Yub2532c62018-04-24 10:55:28 +08002835 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002836out_src:
Chao Yub2532c62018-04-24 10:55:28 +08002837 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002838out_unlock:
2839 if (src != dst)
2840 inode_unlock(dst);
2841out:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002842 inode_unlock(src);
2843 return ret;
2844}
2845
2846static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2847{
2848 struct f2fs_move_range range;
2849 struct fd dst;
2850 int err;
2851
2852 if (!(filp->f_mode & FMODE_READ) ||
2853 !(filp->f_mode & FMODE_WRITE))
2854 return -EBADF;
2855
2856 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2857 sizeof(range)))
2858 return -EFAULT;
2859
2860 dst = fdget(range.dst_fd);
2861 if (!dst.file)
2862 return -EBADF;
2863
2864 if (!(dst.file->f_mode & FMODE_WRITE)) {
2865 err = -EBADF;
2866 goto err_out;
2867 }
2868
2869 err = mnt_want_write_file(filp);
2870 if (err)
2871 goto err_out;
2872
2873 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2874 range.pos_out, range.len);
2875
2876 mnt_drop_write_file(filp);
Kinglong Mee3cecfa52017-03-10 17:54:26 +08002877 if (err)
2878 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002879
2880 if (copy_to_user((struct f2fs_move_range __user *)arg,
2881 &range, sizeof(range)))
2882 err = -EFAULT;
2883err_out:
2884 fdput(dst);
2885 return err;
2886}
2887
Jaegeuk Kime066b832017-04-13 15:17:00 -07002888static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2889{
2890 struct inode *inode = file_inode(filp);
2891 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2892 struct sit_info *sm = SIT_I(sbi);
2893 unsigned int start_segno = 0, end_segno = 0;
2894 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2895 struct f2fs_flush_device range;
2896 int ret;
2897
2898 if (!capable(CAP_SYS_ADMIN))
2899 return -EPERM;
2900
2901 if (f2fs_readonly(sbi->sb))
2902 return -EROFS;
2903
Daniel Rosenberg43549942018-08-20 19:21:43 -07002904 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2905 return -EINVAL;
2906
Jaegeuk Kime066b832017-04-13 15:17:00 -07002907 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2908 sizeof(range)))
2909 return -EFAULT;
2910
Damien Le Moal09168782019-03-16 09:13:06 +09002911 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
Chao Yu2c70c5e2018-10-24 18:37:26 +08002912 __is_large_section(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002913 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2914 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
Jaegeuk Kime066b832017-04-13 15:17:00 -07002915 return -EINVAL;
2916 }
2917
2918 ret = mnt_want_write_file(filp);
2919 if (ret)
2920 return ret;
2921
2922 if (range.dev_num != 0)
2923 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2924 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2925
2926 start_segno = sm->last_victim[FLUSH_DEVICE];
2927 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2928 start_segno = dev_start_segno;
2929 end_segno = min(start_segno + range.segments, dev_end_segno);
2930
2931 while (start_segno < end_segno) {
2932 if (!mutex_trylock(&sbi->gc_mutex)) {
2933 ret = -EBUSY;
2934 goto out;
2935 }
2936 sm->last_victim[GC_CB] = end_segno + 1;
2937 sm->last_victim[GC_GREEDY] = end_segno + 1;
2938 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2939 ret = f2fs_gc(sbi, true, true, start_segno);
2940 if (ret == -EAGAIN)
2941 ret = 0;
2942 else if (ret < 0)
2943 break;
2944 start_segno++;
2945 }
2946out:
2947 mnt_drop_write_file(filp);
2948 return ret;
2949}
2950
Jaegeuk Kime65ef202017-07-21 12:58:59 -07002951static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2952{
2953 struct inode *inode = file_inode(filp);
2954 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2955
2956 /* Must validate to set it with SQLite behavior in Android. */
2957 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2958
2959 return put_user(sb_feature, (u32 __user *)arg);
2960}
Jaegeuk Kime066b832017-04-13 15:17:00 -07002961
Chao Yu2c1d0302017-07-29 00:32:52 +08002962#ifdef CONFIG_QUOTA
Chao Yu78130812018-09-25 15:36:02 +08002963int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2964{
2965 struct dquot *transfer_to[MAXQUOTAS] = {};
2966 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2967 struct super_block *sb = sbi->sb;
2968 int err = 0;
2969
2970 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2971 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2972 err = __dquot_transfer(inode, transfer_to);
2973 if (err)
2974 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2975 dqput(transfer_to[PRJQUOTA]);
2976 }
2977 return err;
2978}
2979
Chao Yu2c1d0302017-07-29 00:32:52 +08002980static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2981{
2982 struct inode *inode = file_inode(filp);
2983 struct f2fs_inode_info *fi = F2FS_I(inode);
2984 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08002985 struct page *ipage;
2986 kprojid_t kprojid;
2987 int err;
2988
Chao Yu7beb01f2018-10-24 18:34:26 +08002989 if (!f2fs_sb_has_project_quota(sbi)) {
Chao Yu2c1d0302017-07-29 00:32:52 +08002990 if (projid != F2FS_DEF_PROJID)
2991 return -EOPNOTSUPP;
2992 else
2993 return 0;
2994 }
2995
2996 if (!f2fs_has_extra_attr(inode))
2997 return -EOPNOTSUPP;
2998
2999 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3000
3001 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3002 return 0;
3003
Chao Yu2c1d0302017-07-29 00:32:52 +08003004 err = -EPERM;
Chao Yu2c1d0302017-07-29 00:32:52 +08003005 /* Is it quota file? Do not allow user to mess with it */
3006 if (IS_NOQUOTA(inode))
Wang Shilongc8e92752018-09-11 08:54:21 +09003007 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003008
Chao Yu4d57b862018-05-30 00:20:41 +08003009 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Wang Shilongc8e92752018-09-11 08:54:21 +09003010 if (IS_ERR(ipage))
3011 return PTR_ERR(ipage);
Chao Yu2c1d0302017-07-29 00:32:52 +08003012
3013 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3014 i_projid)) {
3015 err = -EOVERFLOW;
3016 f2fs_put_page(ipage, 1);
Wang Shilongc8e92752018-09-11 08:54:21 +09003017 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003018 }
3019 f2fs_put_page(ipage, 1);
3020
Chao Yuc22aecd2018-04-21 17:53:52 +08003021 err = dquot_initialize(inode);
3022 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003023 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003024
Chao Yu78130812018-09-25 15:36:02 +08003025 f2fs_lock_op(sbi);
3026 err = f2fs_transfer_project_quota(inode, kprojid);
3027 if (err)
3028 goto out_unlock;
Chao Yu2c1d0302017-07-29 00:32:52 +08003029
3030 F2FS_I(inode)->i_projid = kprojid;
3031 inode->i_ctime = current_time(inode);
Chao Yu2c1d0302017-07-29 00:32:52 +08003032 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yu78130812018-09-25 15:36:02 +08003033out_unlock:
3034 f2fs_unlock_op(sbi);
Chao Yu2c1d0302017-07-29 00:32:52 +08003035 return err;
3036}
3037#else
Chao Yu78130812018-09-25 15:36:02 +08003038int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3039{
3040 return 0;
3041}
3042
Chao Yu2c1d0302017-07-29 00:32:52 +08003043static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3044{
3045 if (projid != F2FS_DEF_PROJID)
3046 return -EOPNOTSUPP;
3047 return 0;
3048}
3049#endif
3050
Eric Biggers36098552019-06-04 22:59:04 -07003051/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
Chao Yu2c1d0302017-07-29 00:32:52 +08003052
Eric Biggers36098552019-06-04 22:59:04 -07003053/*
3054 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3055 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3056 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3057 */
3058
3059static const struct {
3060 u32 iflag;
3061 u32 xflag;
3062} f2fs_xflags_map[] = {
3063 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3064 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3065 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3066 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3067 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3068 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3069};
3070
3071#define F2FS_SUPPORTED_XFLAGS ( \
3072 FS_XFLAG_SYNC | \
3073 FS_XFLAG_IMMUTABLE | \
3074 FS_XFLAG_APPEND | \
3075 FS_XFLAG_NODUMP | \
3076 FS_XFLAG_NOATIME | \
3077 FS_XFLAG_PROJINHERIT)
3078
3079/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3080static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3081{
3082 u32 xflags = 0;
3083 int i;
3084
3085 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3086 if (iflags & f2fs_xflags_map[i].iflag)
3087 xflags |= f2fs_xflags_map[i].xflag;
3088
Chao Yu2c1d0302017-07-29 00:32:52 +08003089 return xflags;
3090}
3091
Eric Biggers36098552019-06-04 22:59:04 -07003092/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3093static inline u32 f2fs_xflags_to_iflags(u32 xflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003094{
Eric Biggers36098552019-06-04 22:59:04 -07003095 u32 iflags = 0;
3096 int i;
Chao Yu2c1d0302017-07-29 00:32:52 +08003097
Eric Biggers36098552019-06-04 22:59:04 -07003098 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3099 if (xflags & f2fs_xflags_map[i].xflag)
3100 iflags |= f2fs_xflags_map[i].iflag;
Chao Yu2c1d0302017-07-29 00:32:52 +08003101
3102 return iflags;
3103}
3104
Eric Biggers6fc93c42019-07-01 13:26:29 -07003105static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3106{
3107 struct f2fs_inode_info *fi = F2FS_I(inode);
3108
3109 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3110
3111 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3112 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3113}
3114
Chao Yu2c1d0302017-07-29 00:32:52 +08003115static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3116{
3117 struct inode *inode = file_inode(filp);
Chao Yu2c1d0302017-07-29 00:32:52 +08003118 struct fsxattr fa;
3119
Eric Biggers6fc93c42019-07-01 13:26:29 -07003120 f2fs_fill_fsxattr(inode, &fa);
Chao Yu2c1d0302017-07-29 00:32:52 +08003121
3122 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3123 return -EFAULT;
3124 return 0;
3125}
3126
3127static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3128{
3129 struct inode *inode = file_inode(filp);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003130 struct fsxattr fa, old_fa;
Eric Biggers36098552019-06-04 22:59:04 -07003131 u32 iflags;
Chao Yu2c1d0302017-07-29 00:32:52 +08003132 int err;
3133
3134 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3135 return -EFAULT;
3136
3137 /* Make sure caller has proper permission */
3138 if (!inode_owner_or_capable(inode))
3139 return -EACCES;
3140
Eric Biggers36098552019-06-04 22:59:04 -07003141 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
Chao Yu2c1d0302017-07-29 00:32:52 +08003142 return -EOPNOTSUPP;
3143
Eric Biggers36098552019-06-04 22:59:04 -07003144 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3145 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
Chao Yu2c1d0302017-07-29 00:32:52 +08003146 return -EOPNOTSUPP;
3147
3148 err = mnt_want_write_file(filp);
3149 if (err)
3150 return err;
3151
3152 inode_lock(inode);
Eric Biggers6fc93c42019-07-01 13:26:29 -07003153
3154 f2fs_fill_fsxattr(inode, &old_fa);
3155 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
Wang Shilongc8e92752018-09-11 08:54:21 +09003156 if (err)
3157 goto out;
Eric Biggers6fc93c42019-07-01 13:26:29 -07003158
Eric Biggers36098552019-06-04 22:59:04 -07003159 err = f2fs_setflags_common(inode, iflags,
3160 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
Chao Yu2c1d0302017-07-29 00:32:52 +08003161 if (err)
Wang Shilongc8e92752018-09-11 08:54:21 +09003162 goto out;
Chao Yu2c1d0302017-07-29 00:32:52 +08003163
3164 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
Wang Shilongc8e92752018-09-11 08:54:21 +09003165out:
3166 inode_unlock(inode);
3167 mnt_drop_write_file(filp);
3168 return err;
Chao Yu2c1d0302017-07-29 00:32:52 +08003169}
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003170
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003171int f2fs_pin_file_control(struct inode *inode, bool inc)
3172{
3173 struct f2fs_inode_info *fi = F2FS_I(inode);
3174 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3175
3176 /* Use i_gc_failures for normal file as a risk signal. */
3177 if (inc)
Chao Yu2ef79ec2018-05-07 20:28:54 +08003178 f2fs_i_gc_failures_write(inode,
3179 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003180
Chao Yu2ef79ec2018-05-07 20:28:54 +08003181 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003182 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3183 __func__, inode->i_ino,
3184 fi->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003185 clear_inode_flag(inode, FI_PIN_FILE);
3186 return -EAGAIN;
3187 }
3188 return 0;
3189}
3190
3191static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3192{
3193 struct inode *inode = file_inode(filp);
3194 __u32 pin;
3195 int ret = 0;
3196
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003197 if (get_user(pin, (__u32 __user *)arg))
3198 return -EFAULT;
3199
3200 if (!S_ISREG(inode->i_mode))
3201 return -EINVAL;
3202
3203 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3204 return -EROFS;
3205
3206 ret = mnt_want_write_file(filp);
3207 if (ret)
3208 return ret;
3209
3210 inode_lock(inode);
3211
Chao Yu4d57b862018-05-30 00:20:41 +08003212 if (f2fs_should_update_outplace(inode, NULL)) {
Chao Yubb9e3bb2018-01-17 16:31:38 +08003213 ret = -EINVAL;
3214 goto out;
3215 }
3216
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003217 if (!pin) {
3218 clear_inode_flag(inode, FI_PIN_FILE);
Chao Yu30933362018-07-28 18:37:58 +08003219 f2fs_i_gc_failures_write(inode, 0);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003220 goto done;
3221 }
3222
3223 if (f2fs_pin_file_control(inode, false)) {
3224 ret = -EAGAIN;
3225 goto out;
3226 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003227
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003228 ret = f2fs_convert_inline_inode(inode);
3229 if (ret)
3230 goto out;
3231
Chao Yu4c8ff702019-11-01 18:07:14 +08003232 if (f2fs_disable_compressed_file(inode)) {
3233 ret = -EOPNOTSUPP;
3234 goto out;
3235 }
3236
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003237 set_inode_flag(inode, FI_PIN_FILE);
Chao Yu2ef79ec2018-05-07 20:28:54 +08003238 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003239done:
3240 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3241out:
3242 inode_unlock(inode);
3243 mnt_drop_write_file(filp);
3244 return ret;
3245}
3246
3247static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3248{
3249 struct inode *inode = file_inode(filp);
3250 __u32 pin = 0;
3251
3252 if (is_inode_flag_set(inode, FI_PIN_FILE))
Chao Yu2ef79ec2018-05-07 20:28:54 +08003253 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003254 return put_user(pin, (u32 __user *)arg);
3255}
3256
Chao Yuc4020b22018-01-11 14:42:30 +08003257int f2fs_precache_extents(struct inode *inode)
3258{
3259 struct f2fs_inode_info *fi = F2FS_I(inode);
3260 struct f2fs_map_blocks map;
3261 pgoff_t m_next_extent;
3262 loff_t end;
3263 int err;
3264
3265 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3266 return -EOPNOTSUPP;
3267
3268 map.m_lblk = 0;
3269 map.m_next_pgofs = NULL;
3270 map.m_next_extent = &m_next_extent;
3271 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08003272 map.m_may_create = false;
Chao Yuc4020b22018-01-11 14:42:30 +08003273 end = F2FS_I_SB(inode)->max_file_blocks;
3274
3275 while (map.m_lblk < end) {
3276 map.m_len = end - map.m_lblk;
3277
Chao Yub2532c62018-04-24 10:55:28 +08003278 down_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003279 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
Chao Yub2532c62018-04-24 10:55:28 +08003280 up_write(&fi->i_gc_rwsem[WRITE]);
Chao Yuc4020b22018-01-11 14:42:30 +08003281 if (err)
3282 return err;
3283
3284 map.m_lblk = m_next_extent;
3285 }
3286
3287 return err;
3288}
3289
3290static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3291{
3292 return f2fs_precache_extents(file_inode(filp));
3293}
3294
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003295static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3296{
3297 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3298 __u64 block_count;
3299 int ret;
3300
3301 if (!capable(CAP_SYS_ADMIN))
3302 return -EPERM;
3303
3304 if (f2fs_readonly(sbi->sb))
3305 return -EROFS;
3306
3307 if (copy_from_user(&block_count, (void __user *)arg,
3308 sizeof(block_count)))
3309 return -EFAULT;
3310
3311 ret = f2fs_resize_fs(sbi, block_count);
3312
3313 return ret;
3314}
3315
Eric Biggers95ae2512019-07-22 09:26:24 -07003316static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3317{
3318 struct inode *inode = file_inode(filp);
3319
3320 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3321
3322 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3323 f2fs_warn(F2FS_I_SB(inode),
3324 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3325 inode->i_ino);
3326 return -EOPNOTSUPP;
3327 }
3328
3329 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3330}
3331
3332static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3333{
3334 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3335 return -EOPNOTSUPP;
3336
3337 return fsverity_ioctl_measure(filp, (void __user *)arg);
3338}
3339
Chao Yu4507847c2019-07-17 17:06:11 +08003340static int f2fs_get_volume_name(struct file *filp, unsigned long arg)
3341{
3342 struct inode *inode = file_inode(filp);
3343 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3344 char *vbuf;
3345 int count;
3346 int err = 0;
3347
3348 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3349 if (!vbuf)
3350 return -ENOMEM;
3351
3352 down_read(&sbi->sb_lock);
3353 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3354 ARRAY_SIZE(sbi->raw_super->volume_name),
3355 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3356 up_read(&sbi->sb_lock);
3357
3358 if (copy_to_user((char __user *)arg, vbuf,
3359 min(FSLABEL_MAX, count)))
3360 err = -EFAULT;
3361
3362 kvfree(vbuf);
3363 return err;
3364}
3365
3366static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
3367{
3368 struct inode *inode = file_inode(filp);
3369 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3370 char *vbuf;
3371 int err = 0;
3372
3373 if (!capable(CAP_SYS_ADMIN))
3374 return -EPERM;
3375
3376 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3377 if (IS_ERR(vbuf))
3378 return PTR_ERR(vbuf);
3379
3380 err = mnt_want_write_file(filp);
3381 if (err)
3382 goto out;
3383
3384 down_write(&sbi->sb_lock);
3385
3386 memset(sbi->raw_super->volume_name, 0,
3387 sizeof(sbi->raw_super->volume_name));
3388 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3389 sbi->raw_super->volume_name,
3390 ARRAY_SIZE(sbi->raw_super->volume_name));
3391
3392 err = f2fs_commit_super(sbi, false);
3393
3394 up_write(&sbi->sb_lock);
3395
3396 mnt_drop_write_file(filp);
3397out:
3398 kfree(vbuf);
3399 return err;
3400}
3401
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003402long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3403{
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003404 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3405 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +08003406 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
3407 return -ENOSPC;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003408
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003409 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003410 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003411 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09003412 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003413 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08003414 case F2FS_IOC_GETVERSION:
3415 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07003416 case F2FS_IOC_START_ATOMIC_WRITE:
3417 return f2fs_ioc_start_atomic_write(filp);
3418 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3419 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07003420 case F2FS_IOC_START_VOLATILE_WRITE:
3421 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08003422 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3423 return f2fs_ioc_release_volatile_write(filp);
3424 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3425 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08003426 case F2FS_IOC_SHUTDOWN:
3427 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07003428 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07003429 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003430 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3431 return f2fs_ioc_set_encryption_policy(filp, arg);
3432 case F2FS_IOC_GET_ENCRYPTION_POLICY:
3433 return f2fs_ioc_get_encryption_policy(filp, arg);
3434 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3435 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Eric Biggers8ce589c2019-08-04 19:35:48 -07003436 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3437 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3438 case FS_IOC_ADD_ENCRYPTION_KEY:
3439 return f2fs_ioc_add_encryption_key(filp, arg);
3440 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3441 return f2fs_ioc_remove_encryption_key(filp, arg);
3442 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3443 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
3444 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3445 return f2fs_ioc_get_encryption_key_status(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08003446 case F2FS_IOC_GARBAGE_COLLECT:
3447 return f2fs_ioc_gc(filp, arg);
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003448 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3449 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08003450 case F2FS_IOC_WRITE_CHECKPOINT:
Chao Yu059c0642018-07-17 20:41:49 +08003451 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08003452 case F2FS_IOC_DEFRAGMENT:
3453 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003454 case F2FS_IOC_MOVE_RANGE:
3455 return f2fs_ioc_move_range(filp, arg);
Jaegeuk Kime066b832017-04-13 15:17:00 -07003456 case F2FS_IOC_FLUSH_DEVICE:
3457 return f2fs_ioc_flush_device(filp, arg);
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003458 case F2FS_IOC_GET_FEATURES:
3459 return f2fs_ioc_get_features(filp, arg);
Chao Yu2c1d0302017-07-29 00:32:52 +08003460 case F2FS_IOC_FSGETXATTR:
3461 return f2fs_ioc_fsgetxattr(filp, arg);
3462 case F2FS_IOC_FSSETXATTR:
3463 return f2fs_ioc_fssetxattr(filp, arg);
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003464 case F2FS_IOC_GET_PIN_FILE:
3465 return f2fs_ioc_get_pin_file(filp, arg);
3466 case F2FS_IOC_SET_PIN_FILE:
3467 return f2fs_ioc_set_pin_file(filp, arg);
Chao Yuc4020b22018-01-11 14:42:30 +08003468 case F2FS_IOC_PRECACHE_EXTENTS:
3469 return f2fs_ioc_precache_extents(filp, arg);
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003470 case F2FS_IOC_RESIZE_FS:
3471 return f2fs_ioc_resize_fs(filp, arg);
Eric Biggers95ae2512019-07-22 09:26:24 -07003472 case FS_IOC_ENABLE_VERITY:
3473 return f2fs_ioc_enable_verity(filp, arg);
3474 case FS_IOC_MEASURE_VERITY:
3475 return f2fs_ioc_measure_verity(filp, arg);
Chao Yu4507847c2019-07-17 17:06:11 +08003476 case F2FS_IOC_GET_VOLUME_NAME:
3477 return f2fs_get_volume_name(filp, arg);
3478 case F2FS_IOC_SET_VOLUME_NAME:
3479 return f2fs_set_volume_name(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003480 default:
3481 return -ENOTTY;
3482 }
3483}
3484
Chao Yu4c8ff702019-11-01 18:07:14 +08003485static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
3486{
3487 struct file *file = iocb->ki_filp;
3488 struct inode *inode = file_inode(file);
3489
3490 if (!f2fs_is_compress_backend_ready(inode))
3491 return -EOPNOTSUPP;
3492
3493 return generic_file_read_iter(iocb, iter);
3494}
3495
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003496static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3497{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003498 struct file *file = iocb->ki_filp;
3499 struct inode *inode = file_inode(file);
3500 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003501
Chao Yu126ce722019-04-02 18:52:22 +08003502 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
3503 ret = -EIO;
3504 goto out;
3505 }
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02003506
Chao Yu4c8ff702019-11-01 18:07:14 +08003507 if (!f2fs_is_compress_backend_ready(inode))
3508 return -EOPNOTSUPP;
3509
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003510 if (iocb->ki_flags & IOCB_NOWAIT) {
3511 if (!inode_trylock(inode)) {
Chao Yu126ce722019-04-02 18:52:22 +08003512 ret = -EAGAIN;
3513 goto out;
3514 }
Goldwyn Rodriguescb8434f2019-09-11 11:45:17 -05003515 } else {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003516 inode_lock(inode);
3517 }
3518
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003519 ret = generic_write_checks(iocb, from);
3520 if (ret > 0) {
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003521 bool preallocated = false;
3522 size_t target_size = 0;
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003523 int err;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003524
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003525 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
3526 set_inode_flag(inode, FI_NO_PREALLOC);
3527
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003528 if ((iocb->ki_flags & IOCB_NOWAIT)) {
3529 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003530 iov_iter_count(from)) ||
Chengguang Xud5d5f0c2019-04-23 13:08:35 +08003531 f2fs_has_inline_data(inode) ||
3532 f2fs_force_buffered_io(inode, iocb, from)) {
3533 clear_inode_flag(inode, FI_NO_PREALLOC);
3534 inode_unlock(inode);
3535 ret = -EAGAIN;
3536 goto out;
3537 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003538 goto write;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003539 }
Jaegeuk Kim47501f82019-11-26 15:01:42 -08003540
3541 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
3542 goto write;
3543
3544 if (iocb->ki_flags & IOCB_DIRECT) {
3545 /*
3546 * Convert inline data for Direct I/O before entering
3547 * f2fs_direct_IO().
3548 */
3549 err = f2fs_convert_inline_inode(inode);
3550 if (err)
3551 goto out_err;
3552 /*
3553 * If force_buffere_io() is true, we have to allocate
3554 * blocks all the time, since f2fs_direct_IO will fall
3555 * back to buffered IO.
3556 */
3557 if (!f2fs_force_buffered_io(inode, iocb, from) &&
3558 allow_outplace_dio(inode, iocb, from))
3559 goto write;
3560 }
3561 preallocated = true;
3562 target_size = iocb->ki_pos + iov_iter_count(from);
3563
3564 err = f2fs_preallocate_blocks(iocb, from);
3565 if (err) {
3566out_err:
3567 clear_inode_flag(inode, FI_NO_PREALLOC);
3568 inode_unlock(inode);
3569 ret = err;
3570 goto out;
3571 }
3572write:
Jaegeuk Kima7de6082016-11-11 16:31:56 -08003573 ret = __generic_file_write_iter(iocb, from);
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003574 clear_inode_flag(inode, FI_NO_PREALLOC);
Chao Yub0af6d42017-08-02 23:21:48 +08003575
Jaegeuk Kimdc7a10d2018-03-30 17:58:13 -07003576 /* if we couldn't write data, we should deallocate blocks. */
3577 if (preallocated && i_size_read(inode) < target_size)
3578 f2fs_truncate(inode);
3579
Chao Yub0af6d42017-08-02 23:21:48 +08003580 if (ret > 0)
3581 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003582 }
3583 inode_unlock(inode);
Chao Yu126ce722019-04-02 18:52:22 +08003584out:
3585 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
3586 iov_iter_count(from), ret);
Christoph Hellwige2592212016-04-07 08:52:01 -07003587 if (ret > 0)
3588 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003589 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003590}
3591
Namjae Jeone9750822013-02-04 23:41:41 +09003592#ifdef CONFIG_COMPAT
3593long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3594{
3595 switch (cmd) {
3596 case F2FS_IOC32_GETFLAGS:
3597 cmd = F2FS_IOC_GETFLAGS;
3598 break;
3599 case F2FS_IOC32_SETFLAGS:
3600 cmd = F2FS_IOC_SETFLAGS;
3601 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08003602 case F2FS_IOC32_GETVERSION:
3603 cmd = F2FS_IOC_GETVERSION;
3604 break;
3605 case F2FS_IOC_START_ATOMIC_WRITE:
3606 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3607 case F2FS_IOC_START_VOLATILE_WRITE:
3608 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3609 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3610 case F2FS_IOC_SHUTDOWN:
Arnd Bergmann314999d2019-06-03 13:51:58 +02003611 case FITRIM:
Chao Yu04ef4b62015-11-10 18:44:20 +08003612 case F2FS_IOC_SET_ENCRYPTION_POLICY:
3613 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
3614 case F2FS_IOC_GET_ENCRYPTION_POLICY:
Eric Biggers8ce589c2019-08-04 19:35:48 -07003615 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3616 case FS_IOC_ADD_ENCRYPTION_KEY:
3617 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3618 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3619 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
Chao Yu04ef4b62015-11-10 18:44:20 +08003620 case F2FS_IOC_GARBAGE_COLLECT:
Jaegeuk Kim34dc77a2017-06-15 16:44:42 -07003621 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08003622 case F2FS_IOC_WRITE_CHECKPOINT:
3623 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003624 case F2FS_IOC_MOVE_RANGE:
Jaegeuk Kime066b832017-04-13 15:17:00 -07003625 case F2FS_IOC_FLUSH_DEVICE:
Jaegeuk Kime65ef202017-07-21 12:58:59 -07003626 case F2FS_IOC_GET_FEATURES:
Chao Yu2c1d0302017-07-29 00:32:52 +08003627 case F2FS_IOC_FSGETXATTR:
3628 case F2FS_IOC_FSSETXATTR:
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08003629 case F2FS_IOC_GET_PIN_FILE:
3630 case F2FS_IOC_SET_PIN_FILE:
Chao Yuc4020b22018-01-11 14:42:30 +08003631 case F2FS_IOC_PRECACHE_EXTENTS:
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003632 case F2FS_IOC_RESIZE_FS:
Eric Biggers95ae2512019-07-22 09:26:24 -07003633 case FS_IOC_ENABLE_VERITY:
3634 case FS_IOC_MEASURE_VERITY:
Chao Yu4507847c2019-07-17 17:06:11 +08003635 case F2FS_IOC_GET_VOLUME_NAME:
3636 case F2FS_IOC_SET_VOLUME_NAME:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07003637 break;
Namjae Jeone9750822013-02-04 23:41:41 +09003638 default:
3639 return -ENOIOCTLCMD;
3640 }
3641 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
3642}
3643#endif
3644
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003645const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08003646 .llseek = f2fs_llseek,
Chao Yu4c8ff702019-11-01 18:07:14 +08003647 .read_iter = f2fs_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003648 .write_iter = f2fs_file_write_iter,
3649 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08003650 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003651 .mmap = f2fs_file_mmap,
Jaegeuk Kim7a10f012017-07-24 19:46:29 -07003652 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003653 .fsync = f2fs_sync_file,
3654 .fallocate = f2fs_fallocate,
3655 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09003656#ifdef CONFIG_COMPAT
3657 .compat_ioctl = f2fs_compat_ioctl,
3658#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003659 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04003660 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09003661};