blob: 020cfaaf6c7f5d87b5ca63fc2be097d865532cd8 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002 * fs/f2fs/file.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/stat.h>
14#include <linux/buffer_head.h>
15#include <linux/writeback.h>
Jaegeuk Kimae51fb32013-03-16 11:13:04 +090016#include <linux/blkdev.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090017#include <linux/falloc.h>
18#include <linux/types.h>
Namjae Jeone9750822013-02-04 23:41:41 +090019#include <linux/compat.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090020#include <linux/uaccess.h>
21#include <linux/mount.h>
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +090022#include <linux/pagevec.h>
Hyojun Kim63da4202017-10-06 17:10:08 -070023#include <linux/uio.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070024#include <linux/uuid.h>
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -070025#include <linux/file.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
30#include "xattr.h"
31#include "acl.h"
Chao Yuc1c1b582015-07-10 18:08:10 +080032#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080033#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090034#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090035
Hyojun Kim63da4202017-10-06 17:10:08 -070036static int f2fs_filemap_fault(struct vm_area_struct *vma,
37 struct vm_fault *vmf)
38{
39 struct inode *inode = file_inode(vma->vm_file);
40 int err;
41
42 down_read(&F2FS_I(inode)->i_mmap_sem);
43 err = filemap_fault(vma, vmf);
44 up_read(&F2FS_I(inode)->i_mmap_sem);
45
46 return err;
47}
48
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
50 struct vm_fault *vmf)
51{
52 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050053 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070054 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090055 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080056 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090057
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +080058 if (unlikely(f2fs_cp_error(sbi))) {
59 err = -EIO;
60 goto err;
61 }
62
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090063 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070064
65 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba12014-08-07 16:32:25 -070066
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090067 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080068 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070069 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070070 err = f2fs_reserve_block(&dn, page->index);
71 if (err) {
72 f2fs_unlock_op(sbi);
73 goto out;
74 }
75 f2fs_put_dnode(&dn);
76 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090077
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -080078 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -080079
Namjae Jeon9851e6e2013-04-28 09:04:18 +090080 file_update_time(vma->vm_file);
Hyojun Kim63da4202017-10-06 17:10:08 -070081 down_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090082 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090083 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090084 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090085 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090086 unlock_page(page);
87 err = -EFAULT;
Hyojun Kim63da4202017-10-06 17:10:08 -070088 goto out_sem;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090089 }
90
91 /*
92 * check to see if the page is mapped already (no holes)
93 */
94 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090095 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090096
97 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030098 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
Chao Yu9edcdab2015-09-11 14:43:52 +080099 i_size_read(inode)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900100 unsigned offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300101 offset = i_size_read(inode) & ~PAGE_MASK;
102 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900103 }
104 set_page_dirty(page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700105 if (!PageUptodate(page))
106 SetPageUptodate(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900107
Hyojun Kim63da4202017-10-06 17:10:08 -0700108 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
109
Jaegeuk Kime943a102013-10-25 14:26:31 +0900110 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +0900111mapped:
112 /* fill the page */
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800113 f2fs_wait_on_page_writeback(page, DATA, false);
Chao Yu08b39fb2015-10-08 13:27:34 +0800114
115 /* wait for GCed encrypted page writeback */
Hyojun Kim63da4202017-10-06 17:10:08 -0700116 if (f2fs_encrypted_file(inode))
117 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +0800118
Hyojun Kim63da4202017-10-06 17:10:08 -0700119out_sem:
120 up_read(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900121out:
122 sb_end_pagefault(inode->i_sb);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800123 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800124err:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900125 return block_page_mkwrite_return(err);
126}
127
128static const struct vm_operations_struct f2fs_file_vm_ops = {
Hyojun Kim63da4202017-10-06 17:10:08 -0700129 .fault = f2fs_filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700130 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +0900131 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900132};
133
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900134static int get_parent_ino(struct inode *inode, nid_t *pino)
135{
136 struct dentry *dentry;
137
138 inode = igrab(inode);
139 dentry = d_find_any_alias(inode);
140 iput(inode);
141 if (!dentry)
142 return 0;
143
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900144 *pino = parent_ino(dentry);
145 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900146 return 1;
147}
148
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800149static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
Chao Yu9d1589e2014-08-20 18:37:35 +0800150{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700151 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800152 enum cp_reason_type cp_reason = CP_NO_NEEDED;
Chao Yu9d1589e2014-08-20 18:37:35 +0800153
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800154 if (!S_ISREG(inode->i_mode))
155 cp_reason = CP_NON_REGULAR;
156 else if (inode->i_nlink != 1)
157 cp_reason = CP_HARDLINK;
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700158 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800159 cp_reason = CP_SB_NEED_CP;
Chao Yu9d1589e2014-08-20 18:37:35 +0800160 else if (file_wrong_pino(inode))
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800161 cp_reason = CP_WRONG_PINO;
Chao Yu9d1589e2014-08-20 18:37:35 +0800162 else if (!space_for_roll_forward(sbi))
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800163 cp_reason = CP_NO_SPC_ROLL;
Chao Yu9d1589e2014-08-20 18:37:35 +0800164 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800165 cp_reason = CP_NODE_NEED_CP;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700166 else if (test_opt(sbi, FASTBOOT))
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800167 cp_reason = CP_FASTBOOT_MODE;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800168 else if (sbi->active_logs == 2)
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800169 cp_reason = CP_SPEC_LOG_NUM;
Chao Yu9d1589e2014-08-20 18:37:35 +0800170
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800171 return cp_reason;
Chao Yu9d1589e2014-08-20 18:37:35 +0800172}
173
Changman Lee9c7bb702014-12-08 15:29:40 +0900174static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
175{
176 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
177 bool ret = false;
178 /* But we need to avoid that there are some inode updates */
179 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
180 ret = true;
181 f2fs_put_page(i, 0);
182 return ret;
183}
184
Changman Lee51455b12014-12-08 15:29:41 +0900185static void try_to_fix_pino(struct inode *inode)
186{
187 struct f2fs_inode_info *fi = F2FS_I(inode);
188 nid_t pino;
189
190 down_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900191 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
192 get_parent_ino(inode, &pino)) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700193 f2fs_i_pino_write(inode, pino);
Changman Lee51455b12014-12-08 15:29:41 +0900194 file_got_pino(inode);
Changman Lee51455b12014-12-08 15:29:41 +0900195 }
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700196 up_write(&fi->i_sem);
Changman Lee51455b12014-12-08 15:29:41 +0900197}
198
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700199static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
200 int datasync, bool atomic)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900201{
202 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700203 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700204 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900205 int ret = 0;
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800206 enum cp_reason_type cp_reason = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900207 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900208 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900209 .nr_to_write = LONG_MAX,
210 .for_reclaim = 0,
211 };
212
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900213 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900214 return 0;
215
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900216 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700217
218 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc46a1552015-12-31 13:49:17 -0800219 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700220 set_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900221 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700222 clear_inode_flag(inode, FI_NEED_IPU);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700223
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900224 if (ret) {
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800225 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900226 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900227 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900228
Changman Lee9c7bb702014-12-08 15:29:40 +0900229 /* if the inode is dirty, let's recover all the time */
Hyojun Kim63da4202017-10-06 17:10:08 -0700230 if (!f2fs_skip_inode_update(inode, datasync)) {
Jaegeuk Kim2286c022015-08-15 21:51:05 -0700231 f2fs_write_inode(inode, NULL);
Changman Lee9c7bb702014-12-08 15:29:40 +0900232 goto go_write;
233 }
234
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700235 /*
236 * if there is no written data, don't waste time to write recovery info.
237 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700238 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700239 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700240
Changman Lee9c7bb702014-12-08 15:29:40 +0900241 /* it may call write_inode just prior to fsync */
242 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700243 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700244
Jaegeuk Kim91942322016-05-20 10:13:22 -0700245 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700246 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700247 goto flush_out;
248 goto out;
249 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700250go_write:
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900251 /*
252 * Both of fdatasync() and fsync() are able to be recovered from
253 * sudden-power-off.
254 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700255 down_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800256 cp_reason = need_do_checkpoint(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700257 up_read(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900258
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800259 if (cp_reason) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900260 /* all the dirty node pages should be flushed for POR */
261 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900262
Changman Lee51455b12014-12-08 15:29:41 +0900263 /*
264 * We've secured consistency through sync_fs. Following pino
265 * will be used only for fsynced inodes after checkpoint.
266 */
267 try_to_fix_pino(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700268 clear_inode_flag(inode, FI_APPEND_WRITE);
269 clear_inode_flag(inode, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900270 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900271 }
Changman Lee51455b12014-12-08 15:29:41 +0900272sync_nodes:
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700273 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -0700274 if (ret)
275 goto out;
Changman Lee51455b12014-12-08 15:29:41 +0900276
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800277 /* if cp_error was enabled, we should avoid infinite loop */
Chao Yu6d5a1492015-12-24 18:04:56 +0800278 if (unlikely(f2fs_cp_error(sbi))) {
279 ret = -EIO;
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800280 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800281 }
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800282
Changman Lee51455b12014-12-08 15:29:41 +0900283 if (need_inode_block_update(sbi, ino)) {
Hyojun Kim63da4202017-10-06 17:10:08 -0700284 f2fs_mark_inode_dirty_sync(inode, true);
Changman Lee51455b12014-12-08 15:29:41 +0900285 f2fs_write_inode(inode, NULL);
286 goto sync_nodes;
287 }
288
Hyojun Kim63da4202017-10-06 17:10:08 -0700289 /*
290 * If it's atomic_write, it's just fine to keep write ordering. So
291 * here we don't need to wait for node write completion, since we use
292 * node chain which serializes node blocks. If one of node writes are
293 * reordered, we can see simply broken chain, resulting in stopping
294 * roll-forward recovery. It means we'll recover all or none node blocks
295 * given fsync mark.
296 */
297 if (!atomic) {
298 ret = wait_on_node_pages_writeback(sbi, ino);
299 if (ret)
300 goto out;
301 }
Changman Lee51455b12014-12-08 15:29:41 +0900302
303 /* once recovery info is written, don't need to tack this */
Chao Yua49324f2015-12-15 13:29:47 +0800304 remove_ino_entry(sbi, ino, APPEND_INO);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700305 clear_inode_flag(inode, FI_APPEND_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900306flush_out:
Hyojun Kim63da4202017-10-06 17:10:08 -0700307 if (!atomic)
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800308 ret = f2fs_issue_flush(sbi, inode->i_ino);
309 if (!ret) {
310 remove_ino_entry(sbi, ino, UPDATE_INO);
311 clear_inode_flag(inode, FI_UPDATE_WRITE);
312 remove_ino_entry(sbi, ino, FLUSH_INO);
313 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800314 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900315out:
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800316 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700317 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900318 return ret;
319}
320
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700321int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
322{
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800323 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
324 return -EIO;
Jaegeuk Kim608514d2016-04-15 09:43:17 -0700325 return f2fs_do_sync_file(file, start, end, datasync, false);
326}
327
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900328static pgoff_t __get_first_dirty_index(struct address_space *mapping,
329 pgoff_t pgofs, int whence)
330{
331 struct pagevec pvec;
332 int nr_pages;
333
334 if (whence != SEEK_DATA)
335 return 0;
336
337 /* find first dirty page index */
338 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700339 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
340 PAGECACHE_TAG_DIRTY, 1);
Chao Yu80dd9c02016-02-24 17:20:44 +0800341 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900342 pagevec_release(&pvec);
343 return pgofs;
344}
345
346static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
347 int whence)
348{
349 switch (whence) {
350 case SEEK_DATA:
351 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
352 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
353 return true;
354 break;
355 case SEEK_HOLE:
356 if (blkaddr == NULL_ADDR)
357 return true;
358 break;
359 }
360 return false;
361}
362
Chao Yu267378d2014-04-23 14:10:24 +0800363static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
364{
365 struct inode *inode = file->f_mapping->host;
366 loff_t maxbytes = inode->i_sb->s_maxbytes;
367 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900368 pgoff_t pgofs, end_offset, dirty;
369 loff_t data_ofs = offset;
370 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800371 int err = 0;
372
Al Viro59551022016-01-22 15:40:57 -0500373 inode_lock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800374
375 isize = i_size_read(inode);
376 if (offset >= isize)
377 goto fail;
378
379 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800380 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800381 if (whence == SEEK_HOLE)
382 data_ofs = isize;
383 goto found;
384 }
385
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300386 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
Chao Yu267378d2014-04-23 14:10:24 +0800387
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900388 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
389
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300390 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800391 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700392 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
Chao Yu267378d2014-04-23 14:10:24 +0800393 if (err && err != -ENOENT) {
394 goto fail;
395 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900396 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800397 if (whence == SEEK_DATA) {
Chao Yu3cf45742016-01-26 15:40:44 +0800398 pgofs = get_next_page_offset(&dn, pgofs);
Chao Yu267378d2014-04-23 14:10:24 +0800399 continue;
400 } else {
401 goto found;
402 }
403 }
404
Chao Yu81ca7352016-01-26 15:39:35 +0800405 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yu267378d2014-04-23 14:10:24 +0800406
407 /* find data/hole in dnode block */
408 for (; dn.ofs_in_node < end_offset;
409 dn.ofs_in_node++, pgofs++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300410 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800411 block_t blkaddr;
Hyojun Kim63da4202017-10-06 17:10:08 -0700412 blkaddr = datablock_addr(dn.inode,
413 dn.node_page, dn.ofs_in_node);
Chao Yu267378d2014-04-23 14:10:24 +0800414
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900415 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800416 f2fs_put_dnode(&dn);
417 goto found;
418 }
419 }
420 f2fs_put_dnode(&dn);
421 }
422
423 if (whence == SEEK_DATA)
424 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800425found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900426 if (whence == SEEK_HOLE && data_ofs > isize)
427 data_ofs = isize;
Al Viro59551022016-01-22 15:40:57 -0500428 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800429 return vfs_setpos(file, data_ofs, maxbytes);
430fail:
Al Viro59551022016-01-22 15:40:57 -0500431 inode_unlock(inode);
Chao Yu267378d2014-04-23 14:10:24 +0800432 return -ENXIO;
433}
434
435static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
436{
437 struct inode *inode = file->f_mapping->host;
438 loff_t maxbytes = inode->i_sb->s_maxbytes;
439
440 switch (whence) {
441 case SEEK_SET:
442 case SEEK_CUR:
443 case SEEK_END:
444 return generic_file_llseek_size(file, offset, whence,
445 maxbytes, i_size_read(inode));
446 case SEEK_DATA:
447 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700448 if (offset < 0)
449 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800450 return f2fs_seek_block(file, offset, whence);
451 }
452
453 return -EINVAL;
454}
455
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900456static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
457{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700458 struct inode *inode = file_inode(file);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800459 int err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700460
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800461 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
462 return -EIO;
463
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700464 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800465 err = f2fs_convert_inline_inode(inode);
466 if (err)
467 return err;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700468
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900469 file_accessed(file);
470 vma->vm_ops = &f2fs_file_vm_ops;
471 return 0;
472}
473
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700474static int f2fs_file_open(struct inode *inode, struct file *filp)
475{
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700476 struct dentry *dir;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700477
Hyojun Kim63da4202017-10-06 17:10:08 -0700478 if (f2fs_encrypted_inode(inode)) {
479 int ret = fscrypt_get_encryption_info(inode);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700480 if (ret)
Chao Yuae108662016-02-14 18:58:35 +0800481 return -EACCES;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700482 if (!fscrypt_has_encryption_key(inode))
Chao Yuae108662016-02-14 18:58:35 +0800483 return -ENOKEY;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700484 }
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700485 dir = dget_parent(file_dentry(filp));
486 if (f2fs_encrypted_inode(d_inode(dir)) &&
487 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
488 dput(dir);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800489 return -EPERM;
Jaegeuk Kim33b13952016-04-11 15:15:38 -0700490 }
491 dput(dir);
Hyojun Kim63da4202017-10-06 17:10:08 -0700492 return dquot_file_open(inode, filp);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700493}
494
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900495int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900496{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700497 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900498 struct f2fs_node *raw_node;
Chao Yu19b2c302015-08-26 20:34:48 +0800499 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900500 __le32 *addr;
Hyojun Kim63da4202017-10-06 17:10:08 -0700501 int base = 0;
502
503 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
504 base = get_extra_isize(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900505
Gu Zheng45590712013-07-15 17:57:38 +0800506 raw_node = F2FS_NODE(dn->node_page);
Hyojun Kim63da4202017-10-06 17:10:08 -0700507 addr = blkaddr_in_node(raw_node) + base + ofs;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900508
Chris Fries6c311ec2014-01-17 14:44:39 -0600509 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900510 block_t blkaddr = le32_to_cpu(*addr);
511 if (blkaddr == NULL_ADDR)
512 continue;
513
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800514 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800515 set_data_blkaddr(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900516 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700517 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700518 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900519 nr_free++;
520 }
Chao Yu19b2c302015-08-26 20:34:48 +0800521
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900522 if (nr_free) {
Chao Yu19b2c302015-08-26 20:34:48 +0800523 pgoff_t fofs;
524 /*
525 * once we invalidate valid blkaddr in range [ofs, ofs + count],
526 * we will invalidate all blkaddr in the whole range.
527 */
528 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
Chao Yu81ca7352016-01-26 15:39:35 +0800529 dn->inode) + ofs;
Chao Yu19b2c302015-08-26 20:34:48 +0800530 f2fs_update_extent_cache_range(dn, fofs, 0, len);
Namjae Jeond7cc9502013-06-08 21:25:40 +0900531 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900532 }
533 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900534
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800535 f2fs_update_time(sbi, REQ_TIME);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900536 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
537 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900538 return nr_free;
539}
540
541void truncate_data_blocks(struct dnode_of_data *dn)
542{
543 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
544}
545
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800546static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700547 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900548{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300549 unsigned offset = from & (PAGE_SIZE - 1);
550 pgoff_t index = from >> PAGE_SHIFT;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700551 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900552 struct page *page;
553
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700554 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700555 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900556
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700557 if (cache_only) {
Jaegeuk Kim34b5d5c2016-09-06 15:55:54 -0700558 page = find_lock_page(mapping, index);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700559 if (page && PageUptodate(page))
560 goto truncate_out;
561 f2fs_put_page(page, 1);
562 return 0;
563 }
564
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700565 page = get_lock_data_page(inode, index, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900566 if (IS_ERR(page))
Hyojun Kim63da4202017-10-06 17:10:08 -0700567 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700568truncate_out:
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800569 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300570 zero_user(page, offset, PAGE_SIZE - offset);
Hyojun Kim63da4202017-10-06 17:10:08 -0700571
572 /* An encrypted inode should have a key and truncate the last page. */
573 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
574 if (!cache_only)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800575 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900576 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700577 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900578}
579
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700580int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900581{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700582 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900583 unsigned int blocksize = inode->i_sb->s_blocksize;
584 struct dnode_of_data dn;
585 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800586 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700587 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800588 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900589
Namjae Jeon51dd6242013-04-20 01:28:52 +0900590 trace_f2fs_truncate_blocks_enter(inode, from);
591
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800592 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900593
Chao Yu09210c92016-05-05 19:13:03 +0800594 if (free_from >= sbi->max_file_blocks)
595 goto free_partial;
596
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700597 if (lock)
598 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800599
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700600 ipage = get_node_page(sbi, inode->i_ino);
601 if (IS_ERR(ipage)) {
602 err = PTR_ERR(ipage);
603 goto out;
604 }
605
606 if (f2fs_has_inline_data(inode)) {
Hyojun Kim63da4202017-10-06 17:10:08 -0700607 truncate_inline_inode(inode, ipage, from);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700608 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800609 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700610 goto out;
611 }
612
613 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700614 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900615 if (err) {
616 if (err == -ENOENT)
617 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700618 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700619 }
620
Chao Yu81ca7352016-01-26 15:39:35 +0800621 count = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900622
623 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700624 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900625
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900626 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
627 truncate_data_blocks_range(&dn, count);
628 free_from += count;
629 }
630
631 f2fs_put_dnode(&dn);
632free_next:
633 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800634out:
635 if (lock)
636 f2fs_unlock_op(sbi);
Chao Yu09210c92016-05-05 19:13:03 +0800637free_partial:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700638 /* lastly zero out the first data page */
639 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800640 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900641
Namjae Jeon51dd6242013-04-20 01:28:52 +0900642 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900643 return err;
644}
645
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700646int f2fs_truncate(struct inode *inode)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900647{
Chao Yub0154892015-08-24 17:39:42 +0800648 int err;
649
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800650 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
651 return -EIO;
652
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900653 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
654 S_ISLNK(inode->i_mode)))
Chao Yub0154892015-08-24 17:39:42 +0800655 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656
Namjae Jeon51dd6242013-04-20 01:28:52 +0900657 trace_f2fs_truncate(inode);
658
Hyojun Kim63da4202017-10-06 17:10:08 -0700659#ifdef CONFIG_F2FS_FAULT_INJECTION
660 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
661 f2fs_show_injection_info(FAULT_TRUNCATE);
662 return -EIO;
663 }
664#endif
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800665 /* we should check inline_data size */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800666 if (!f2fs_may_inline_data(inode)) {
Chao Yub0154892015-08-24 17:39:42 +0800667 err = f2fs_convert_inline_inode(inode);
668 if (err)
669 return err;
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800670 }
671
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700672 err = truncate_blocks(inode, i_size_read(inode), true);
Chao Yub0154892015-08-24 17:39:42 +0800673 if (err)
674 return err;
675
Deepa Dinamani078cd822016-09-14 07:48:04 -0700676 inode->i_mtime = inode->i_ctime = current_time(inode);
Hyojun Kim63da4202017-10-06 17:10:08 -0700677 f2fs_mark_inode_dirty_sync(inode, false);
Chao Yub0154892015-08-24 17:39:42 +0800678 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900679}
680
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900681int f2fs_getattr(struct vfsmount *mnt,
Hyojun Kim63da4202017-10-06 17:10:08 -0700682 struct dentry *dentry, struct kstat *stat)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900683{
David Howells2b0143b2015-03-17 22:25:59 +0000684 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900685 generic_fillattr(inode, stat);
Jaegeuk Kim5c735942017-10-13 10:27:45 -0700686
687 /* we need to show initial sectors used for inline_data/dentries */
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800688 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
689 f2fs_has_inline_dentry(inode))
Jaegeuk Kim5c735942017-10-13 10:27:45 -0700690 stat->blocks += (stat->size + 511) >> 9;
691
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900692 return 0;
693}
694
695#ifdef CONFIG_F2FS_FS_POSIX_ACL
696static void __setattr_copy(struct inode *inode, const struct iattr *attr)
697{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900698 unsigned int ia_valid = attr->ia_valid;
699
700 if (ia_valid & ATTR_UID)
701 inode->i_uid = attr->ia_uid;
702 if (ia_valid & ATTR_GID)
703 inode->i_gid = attr->ia_gid;
704 if (ia_valid & ATTR_ATIME)
705 inode->i_atime = timespec_trunc(attr->ia_atime,
706 inode->i_sb->s_time_gran);
707 if (ia_valid & ATTR_MTIME)
708 inode->i_mtime = timespec_trunc(attr->ia_mtime,
709 inode->i_sb->s_time_gran);
710 if (ia_valid & ATTR_CTIME)
711 inode->i_ctime = timespec_trunc(attr->ia_ctime,
712 inode->i_sb->s_time_gran);
713 if (ia_valid & ATTR_MODE) {
714 umode_t mode = attr->ia_mode;
715
716 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
717 mode &= ~S_ISGID;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700718 set_acl_inode(inode, mode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719 }
720}
721#else
722#define __setattr_copy setattr_copy
723#endif
724
725int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
726{
David Howells2b0143b2015-03-17 22:25:59 +0000727 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900728 int err;
Hyojun Kim63da4202017-10-06 17:10:08 -0700729 bool size_changed = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900730
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800731 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
732 return -EIO;
733
Jan Kara31051c82016-05-26 16:55:18 +0200734 err = setattr_prepare(dentry, attr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900735 if (err)
736 return err;
737
Hyojun Kim63da4202017-10-06 17:10:08 -0700738 if (is_quota_modification(inode, attr)) {
739 err = dquot_initialize(inode);
740 if (err)
741 return err;
742 }
743 if ((attr->ia_valid & ATTR_UID &&
744 !uid_eq(attr->ia_uid, inode->i_uid)) ||
745 (attr->ia_valid & ATTR_GID &&
746 !gid_eq(attr->ia_gid, inode->i_gid))) {
747 err = dquot_transfer(inode, attr);
748 if (err)
749 return err;
750 }
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700751
Hyojun Kim63da4202017-10-06 17:10:08 -0700752 if (attr->ia_valid & ATTR_SIZE) {
753 if (f2fs_encrypted_inode(inode)) {
754 err = fscrypt_get_encryption_info(inode);
Chao Yub0154892015-08-24 17:39:42 +0800755 if (err)
756 return err;
Hyojun Kim63da4202017-10-06 17:10:08 -0700757 if (!fscrypt_has_encryption_key(inode))
758 return -ENOKEY;
759 }
760
761 if (attr->ia_size <= i_size_read(inode)) {
762 down_write(&F2FS_I(inode)->i_mmap_sem);
763 truncate_setsize(inode, attr->ia_size);
764 err = f2fs_truncate(inode);
765 up_write(&F2FS_I(inode)->i_mmap_sem);
766 if (err)
767 return err;
Chao Yu09db6a22014-09-15 18:02:09 +0800768 } else {
769 /*
Chao Yu3c454142015-06-05 18:34:02 +0800770 * do not trim all blocks after i_size if target size is
771 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800772 */
Hyojun Kim63da4202017-10-06 17:10:08 -0700773 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3c454142015-06-05 18:34:02 +0800774 truncate_setsize(inode, attr->ia_size);
Hyojun Kim63da4202017-10-06 17:10:08 -0700775 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu0cab80e2015-12-01 11:36:16 +0800776
777 /* should convert inline inode here */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800778 if (!f2fs_may_inline_data(inode)) {
Chao Yu0cab80e2015-12-01 11:36:16 +0800779 err = f2fs_convert_inline_inode(inode);
780 if (err)
781 return err;
782 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700783 inode->i_mtime = inode->i_ctime = current_time(inode);
Chao Yu09db6a22014-09-15 18:02:09 +0800784 }
Hyojun Kim63da4202017-10-06 17:10:08 -0700785
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800786 down_write(&F2FS_I(inode)->i_sem);
787 F2FS_I(inode)->last_disk_size = i_size_read(inode);
788 up_write(&F2FS_I(inode)->i_sem);
789
Hyojun Kim63da4202017-10-06 17:10:08 -0700790 size_changed = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900791 }
792
793 __setattr_copy(inode, attr);
794
795 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800796 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kim91942322016-05-20 10:13:22 -0700797 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
798 inode->i_mode = F2FS_I(inode)->i_acl_mode;
799 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900800 }
801 }
802
Hyojun Kim63da4202017-10-06 17:10:08 -0700803 /* file size may changed here */
804 f2fs_mark_inode_dirty_sync(inode, size_changed);
805
806 /* inode change will produce dirty node pages flushed by checkpoint */
807 f2fs_balance_fs(F2FS_I_SB(inode), true);
808
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900809 return err;
810}
811
812const struct inode_operations f2fs_file_inode_operations = {
813 .getattr = f2fs_getattr,
814 .setattr = f2fs_setattr,
815 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800816 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900817#ifdef CONFIG_F2FS_FS_XATTR
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900818 .listxattr = f2fs_listxattr,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900819#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900820 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900821};
822
Chao Yu63943282015-08-07 18:36:06 +0800823static int fill_zero(struct inode *inode, pgoff_t index,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900824 loff_t start, loff_t len)
825{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700826 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900827 struct page *page;
828
829 if (!len)
Chao Yu63943282015-08-07 18:36:06 +0800830 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900831
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800832 f2fs_balance_fs(sbi, true);
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900833
Gu Zhenge4795562013-09-27 18:08:30 +0800834 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900835 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800836 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900837
Chao Yu63943282015-08-07 18:36:06 +0800838 if (IS_ERR(page))
839 return PTR_ERR(page);
840
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800841 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu63943282015-08-07 18:36:06 +0800842 zero_user(page, start, len);
843 set_page_dirty(page);
844 f2fs_put_page(page, 1);
845 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900846}
847
848int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
849{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900850 int err;
851
Chao Yuea587112015-09-17 20:22:44 +0800852 while (pg_start < pg_end) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900853 struct dnode_of_data dn;
Chao Yuea587112015-09-17 20:22:44 +0800854 pgoff_t end_offset, count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900855
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900856 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yuea587112015-09-17 20:22:44 +0800857 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900858 if (err) {
Chao Yuea587112015-09-17 20:22:44 +0800859 if (err == -ENOENT) {
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +0800860 pg_start = get_next_page_offset(&dn, pg_start);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900861 continue;
Chao Yuea587112015-09-17 20:22:44 +0800862 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900863 return err;
864 }
865
Chao Yu81ca7352016-01-26 15:39:35 +0800866 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Chao Yuea587112015-09-17 20:22:44 +0800867 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
868
869 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
870
871 truncate_data_blocks_range(&dn, count);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900872 f2fs_put_dnode(&dn);
Chao Yuea587112015-09-17 20:22:44 +0800873
874 pg_start += count;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900875 }
876 return 0;
877}
878
Chao Yua66c7b22013-11-22 16:52:50 +0800879static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900880{
881 pgoff_t pg_start, pg_end;
882 loff_t off_start, off_end;
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800883 int ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900884
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800885 ret = f2fs_convert_inline_inode(inode);
886 if (ret)
887 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800888
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300889 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
890 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900891
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300892 off_start = offset & (PAGE_SIZE - 1);
893 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900894
895 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +0800896 ret = fill_zero(inode, pg_start, off_start,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900897 off_end - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800898 if (ret)
899 return ret;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900900 } else {
Chao Yu63943282015-08-07 18:36:06 +0800901 if (off_start) {
902 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300903 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +0800904 if (ret)
905 return ret;
906 }
907 if (off_end) {
908 ret = fill_zero(inode, pg_end, 0, off_end);
909 if (ret)
910 return ret;
911 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900912
913 if (pg_start < pg_end) {
914 struct address_space *mapping = inode->i_mapping;
915 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700916 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500917
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800918 f2fs_balance_fs(sbi, true);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900919
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300920 blk_start = (loff_t)pg_start << PAGE_SHIFT;
921 blk_end = (loff_t)pg_end << PAGE_SHIFT;
Hyojun Kim63da4202017-10-06 17:10:08 -0700922 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900923 truncate_inode_pages_range(mapping, blk_start,
924 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900925
Gu Zhenge4795562013-09-27 18:08:30 +0800926 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900927 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800928 f2fs_unlock_op(sbi);
Hyojun Kim63da4202017-10-06 17:10:08 -0700929 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900930 }
931 }
932
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900933 return ret;
934}
935
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700936static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
937 int *do_replace, pgoff_t off, pgoff_t len)
Chao Yub4ace332015-05-06 13:09:46 +0800938{
939 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
940 struct dnode_of_data dn;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700941 int ret, done, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700942
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700943next_dnode:
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700944 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700945 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700946 if (ret && ret != -ENOENT) {
947 return ret;
948 } else if (ret == -ENOENT) {
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700949 if (dn.max_level == 0)
950 return -ENOENT;
951 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
952 blkaddr += done;
953 do_replace += done;
954 goto next;
955 }
956
957 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
958 dn.ofs_in_node, len);
959 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
Hyojun Kim63da4202017-10-06 17:10:08 -0700960 *blkaddr = datablock_addr(dn.inode,
961 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700962 if (!is_checkpointed_data(sbi, *blkaddr)) {
963
964 if (test_opt(sbi, LFS)) {
965 f2fs_put_dnode(&dn);
966 return -ENOTSUPP;
967 }
968
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700969 /* do not invalidate this block address */
Chao Yuf28b3432016-02-24 17:16:47 +0800970 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700971 *do_replace = 1;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700972 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700973 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700974 f2fs_put_dnode(&dn);
975next:
976 len -= done;
977 off += done;
978 if (len)
979 goto next_dnode;
980 return 0;
981}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700982
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700983static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
984 int *do_replace, pgoff_t off, int len)
985{
986 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
987 struct dnode_of_data dn;
988 int ret, i;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700989
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700990 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
991 if (*do_replace == 0)
992 continue;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -0700993
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700994 set_new_dnode(&dn, inode, NULL, NULL, 0);
995 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
996 if (ret) {
997 dec_valid_block_count(sbi, inode, 1);
998 invalidate_blocks(sbi, *blkaddr);
999 } else {
1000 f2fs_update_data_blkaddr(&dn, *blkaddr);
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001001 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001002 f2fs_put_dnode(&dn);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001003 }
1004 return 0;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001005}
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001006
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001007static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1008 block_t *blkaddr, int *do_replace,
1009 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1010{
1011 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1012 pgoff_t i = 0;
1013 int ret;
1014
1015 while (i < len) {
1016 if (blkaddr[i] == NULL_ADDR && !full) {
1017 i++;
1018 continue;
1019 }
1020
1021 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1022 struct dnode_of_data dn;
1023 struct node_info ni;
1024 size_t new_size;
1025 pgoff_t ilen;
1026
1027 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1028 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1029 if (ret)
1030 return ret;
1031
1032 get_node_info(sbi, dn.nid, &ni);
1033 ilen = min((pgoff_t)
1034 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1035 dn.ofs_in_node, len - i);
1036 do {
Hyojun Kim63da4202017-10-06 17:10:08 -07001037 dn.data_blkaddr = datablock_addr(dn.inode,
1038 dn.node_page, dn.ofs_in_node);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001039 truncate_data_blocks_range(&dn, 1);
1040
1041 if (do_replace[i]) {
1042 f2fs_i_blocks_write(src_inode,
Hyojun Kim63da4202017-10-06 17:10:08 -07001043 1, false, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001044 f2fs_i_blocks_write(dst_inode,
Hyojun Kim63da4202017-10-06 17:10:08 -07001045 1, true, false);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001046 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1047 blkaddr[i], ni.version, true, false);
1048
1049 do_replace[i] = 0;
1050 }
1051 dn.ofs_in_node++;
1052 i++;
1053 new_size = (dst + i) << PAGE_SHIFT;
1054 if (dst_inode->i_size < new_size)
1055 f2fs_i_size_write(dst_inode, new_size);
Jaegeuk Kim027611e2016-11-23 10:51:17 -08001056 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001057
1058 f2fs_put_dnode(&dn);
1059 } else {
1060 struct page *psrc, *pdst;
1061
1062 psrc = get_lock_data_page(src_inode, src + i, true);
1063 if (IS_ERR(psrc))
1064 return PTR_ERR(psrc);
1065 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1066 true);
1067 if (IS_ERR(pdst)) {
1068 f2fs_put_page(psrc, 1);
1069 return PTR_ERR(pdst);
1070 }
1071 f2fs_copy_page(psrc, pdst);
1072 set_page_dirty(pdst);
1073 f2fs_put_page(pdst, 1);
1074 f2fs_put_page(psrc, 1);
1075
1076 ret = truncate_hole(src_inode, src + i, src + i + 1);
1077 if (ret)
1078 return ret;
1079 i++;
1080 }
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001081 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001082 return 0;
1083}
1084
1085static int __exchange_data_block(struct inode *src_inode,
1086 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001087 pgoff_t len, bool full)
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001088{
1089 block_t *src_blkaddr;
1090 int *do_replace;
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001091 pgoff_t olen;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001092 int ret;
1093
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001094 while (len) {
1095 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001096
Hyojun Kim63da4202017-10-06 17:10:08 -07001097 src_blkaddr = kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001098 if (!src_blkaddr)
1099 return -ENOMEM;
1100
Hyojun Kim63da4202017-10-06 17:10:08 -07001101 do_replace = kvzalloc(sizeof(int) * olen, GFP_KERNEL);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001102 if (!do_replace) {
1103 kvfree(src_blkaddr);
1104 return -ENOMEM;
1105 }
1106
1107 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1108 do_replace, src, olen);
1109 if (ret)
1110 goto roll_back;
1111
1112 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1113 do_replace, src, dst, olen, full);
1114 if (ret)
1115 goto roll_back;
1116
1117 src += olen;
1118 dst += olen;
1119 len -= olen;
1120
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001121 kvfree(src_blkaddr);
Jaegeuk Kim363cad72016-07-16 21:59:22 -07001122 kvfree(do_replace);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001123 }
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001124 return 0;
1125
1126roll_back:
1127 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1128 kvfree(src_blkaddr);
1129 kvfree(do_replace);
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001130 return ret;
1131}
1132
1133static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1134{
1135 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub4ace332015-05-06 13:09:46 +08001136 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001137 int ret;
Chao Yub4ace332015-05-06 13:09:46 +08001138
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001139 f2fs_balance_fs(sbi, true);
1140 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001141
1142 f2fs_drop_extent_tree(inode);
1143
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001144 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1145 f2fs_unlock_op(sbi);
Chao Yub4ace332015-05-06 13:09:46 +08001146 return ret;
1147}
1148
1149static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1150{
1151 pgoff_t pg_start, pg_end;
1152 loff_t new_size;
1153 int ret;
1154
Chao Yub4ace332015-05-06 13:09:46 +08001155 if (offset + len >= i_size_read(inode))
1156 return -EINVAL;
1157
1158 /* collapse range should be aligned to block size of f2fs. */
1159 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1160 return -EINVAL;
1161
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001162 ret = f2fs_convert_inline_inode(inode);
1163 if (ret)
1164 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001165
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001166 pg_start = offset >> PAGE_SHIFT;
1167 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yub4ace332015-05-06 13:09:46 +08001168
Hyojun Kim63da4202017-10-06 17:10:08 -07001169 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001170 /* write out all dirty pages from offset */
1171 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1172 if (ret)
Hyojun Kim63da4202017-10-06 17:10:08 -07001173 goto out;
Chao Yub4ace332015-05-06 13:09:46 +08001174
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001175 /* avoid gc operation during block exchange */
1176 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1177
Chao Yub4ace332015-05-06 13:09:46 +08001178 truncate_pagecache(inode, offset);
1179
1180 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1181 if (ret)
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001182 goto out_unlock;
Chao Yub4ace332015-05-06 13:09:46 +08001183
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001184 /* write out all moved pages, if possible */
1185 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1186 truncate_pagecache(inode, offset);
1187
Chao Yub4ace332015-05-06 13:09:46 +08001188 new_size = i_size_read(inode) - len;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001189 truncate_pagecache(inode, new_size);
Chao Yub4ace332015-05-06 13:09:46 +08001190
1191 ret = truncate_blocks(inode, new_size, true);
1192 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001193 f2fs_i_size_write(inode, new_size);
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001194out_unlock:
1195 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Hyojun Kim63da4202017-10-06 17:10:08 -07001196out:
1197 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yub4ace332015-05-06 13:09:46 +08001198 return ret;
1199}
1200
Chao Yu6e961942016-05-09 19:56:31 +08001201static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1202 pgoff_t end)
1203{
1204 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1205 pgoff_t index = start;
1206 unsigned int ofs_in_node = dn->ofs_in_node;
1207 blkcnt_t count = 0;
1208 int ret;
1209
1210 for (; index < end; index++, dn->ofs_in_node++) {
Hyojun Kim63da4202017-10-06 17:10:08 -07001211 if (datablock_addr(dn->inode, dn->node_page,
1212 dn->ofs_in_node) == NULL_ADDR)
Chao Yu6e961942016-05-09 19:56:31 +08001213 count++;
1214 }
1215
1216 dn->ofs_in_node = ofs_in_node;
1217 ret = reserve_new_blocks(dn, count);
1218 if (ret)
1219 return ret;
1220
1221 dn->ofs_in_node = ofs_in_node;
1222 for (index = start; index < end; index++, dn->ofs_in_node++) {
Hyojun Kim63da4202017-10-06 17:10:08 -07001223 dn->data_blkaddr = datablock_addr(dn->inode,
1224 dn->node_page, dn->ofs_in_node);
Chao Yu6e961942016-05-09 19:56:31 +08001225 /*
1226 * reserve_new_blocks will not guarantee entire block
1227 * allocation.
1228 */
1229 if (dn->data_blkaddr == NULL_ADDR) {
1230 ret = -ENOSPC;
1231 break;
1232 }
1233 if (dn->data_blkaddr != NEW_ADDR) {
1234 invalidate_blocks(sbi, dn->data_blkaddr);
1235 dn->data_blkaddr = NEW_ADDR;
1236 set_data_blkaddr(dn);
1237 }
1238 }
1239
1240 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1241
1242 return ret;
1243}
1244
Chao Yu75cd4e02015-05-06 13:11:13 +08001245static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1246 int mode)
1247{
1248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1249 struct address_space *mapping = inode->i_mapping;
1250 pgoff_t index, pg_start, pg_end;
1251 loff_t new_size = i_size_read(inode);
1252 loff_t off_start, off_end;
1253 int ret = 0;
1254
Chao Yu75cd4e02015-05-06 13:11:13 +08001255 ret = inode_newsize_ok(inode, (len + offset));
1256 if (ret)
1257 return ret;
1258
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001259 ret = f2fs_convert_inline_inode(inode);
1260 if (ret)
1261 return ret;
Chao Yu75cd4e02015-05-06 13:11:13 +08001262
Hyojun Kim63da4202017-10-06 17:10:08 -07001263 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001264 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1265 if (ret)
Hyojun Kim63da4202017-10-06 17:10:08 -07001266 goto out_sem;
Chao Yu75cd4e02015-05-06 13:11:13 +08001267
1268 truncate_pagecache_range(inode, offset, offset + len - 1);
1269
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001270 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1271 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
Chao Yu75cd4e02015-05-06 13:11:13 +08001272
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001273 off_start = offset & (PAGE_SIZE - 1);
1274 off_end = (offset + len) & (PAGE_SIZE - 1);
Chao Yu75cd4e02015-05-06 13:11:13 +08001275
1276 if (pg_start == pg_end) {
Chao Yu63943282015-08-07 18:36:06 +08001277 ret = fill_zero(inode, pg_start, off_start,
1278 off_end - off_start);
1279 if (ret)
Hyojun Kim63da4202017-10-06 17:10:08 -07001280 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001281
Chao Yu75cd4e02015-05-06 13:11:13 +08001282 new_size = max_t(loff_t, new_size, offset + len);
1283 } else {
1284 if (off_start) {
Chao Yu63943282015-08-07 18:36:06 +08001285 ret = fill_zero(inode, pg_start++, off_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001286 PAGE_SIZE - off_start);
Chao Yu63943282015-08-07 18:36:06 +08001287 if (ret)
Hyojun Kim63da4202017-10-06 17:10:08 -07001288 goto out_sem;
Chao Yu63943282015-08-07 18:36:06 +08001289
Chao Yu75cd4e02015-05-06 13:11:13 +08001290 new_size = max_t(loff_t, new_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001291 (loff_t)pg_start << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001292 }
1293
Chao Yu6e961942016-05-09 19:56:31 +08001294 for (index = pg_start; index < pg_end;) {
Chao Yu75cd4e02015-05-06 13:11:13 +08001295 struct dnode_of_data dn;
Chao Yu6e961942016-05-09 19:56:31 +08001296 unsigned int end_offset;
1297 pgoff_t end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001298
1299 f2fs_lock_op(sbi);
1300
Chao Yu6e961942016-05-09 19:56:31 +08001301 set_new_dnode(&dn, inode, NULL, NULL, 0);
1302 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
Chao Yu75cd4e02015-05-06 13:11:13 +08001303 if (ret) {
1304 f2fs_unlock_op(sbi);
1305 goto out;
1306 }
1307
Chao Yu6e961942016-05-09 19:56:31 +08001308 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1309 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1310
1311 ret = f2fs_do_zero_range(&dn, index, end);
Chao Yu75cd4e02015-05-06 13:11:13 +08001312 f2fs_put_dnode(&dn);
1313 f2fs_unlock_op(sbi);
Hyojun Kim63da4202017-10-06 17:10:08 -07001314
1315 f2fs_balance_fs(sbi, dn.node_changed);
1316
Chao Yu6e961942016-05-09 19:56:31 +08001317 if (ret)
1318 goto out;
Chao Yu75cd4e02015-05-06 13:11:13 +08001319
Chao Yu6e961942016-05-09 19:56:31 +08001320 index = end;
Chao Yu75cd4e02015-05-06 13:11:13 +08001321 new_size = max_t(loff_t, new_size,
Chao Yu6e961942016-05-09 19:56:31 +08001322 (loff_t)index << PAGE_SHIFT);
Chao Yu75cd4e02015-05-06 13:11:13 +08001323 }
1324
1325 if (off_end) {
Chao Yu63943282015-08-07 18:36:06 +08001326 ret = fill_zero(inode, pg_end, 0, off_end);
1327 if (ret)
1328 goto out;
1329
Chao Yu75cd4e02015-05-06 13:11:13 +08001330 new_size = max_t(loff_t, new_size, offset + len);
1331 }
1332 }
1333
1334out:
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001335 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001336 f2fs_i_size_write(inode, new_size);
Hyojun Kim63da4202017-10-06 17:10:08 -07001337out_sem:
1338 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu75cd4e02015-05-06 13:11:13 +08001339
1340 return ret;
1341}
1342
Chao Yuf62185d2015-05-28 19:16:57 +08001343static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1344{
1345 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001346 pgoff_t nr, pg_start, pg_end, delta, idx;
Chao Yuf62185d2015-05-28 19:16:57 +08001347 loff_t new_size;
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001348 int ret = 0;
Chao Yuf62185d2015-05-28 19:16:57 +08001349
Chao Yuf62185d2015-05-28 19:16:57 +08001350 new_size = i_size_read(inode) + len;
Hyojun Kim63da4202017-10-06 17:10:08 -07001351 ret = inode_newsize_ok(inode, new_size);
1352 if (ret)
1353 return ret;
Chao Yuf62185d2015-05-28 19:16:57 +08001354
1355 if (offset >= i_size_read(inode))
1356 return -EINVAL;
1357
1358 /* insert range should be aligned to block size of f2fs. */
1359 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1360 return -EINVAL;
1361
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001362 ret = f2fs_convert_inline_inode(inode);
1363 if (ret)
1364 return ret;
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001365
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001366 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001367
Hyojun Kim63da4202017-10-06 17:10:08 -07001368 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001369 ret = truncate_blocks(inode, i_size_read(inode), true);
1370 if (ret)
Hyojun Kim63da4202017-10-06 17:10:08 -07001371 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001372
1373 /* write out all dirty pages from offset */
1374 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1375 if (ret)
Hyojun Kim63da4202017-10-06 17:10:08 -07001376 goto out;
Chao Yuf62185d2015-05-28 19:16:57 +08001377
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001378 /* avoid gc operation during block exchange */
1379 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1380
Chao Yuf62185d2015-05-28 19:16:57 +08001381 truncate_pagecache(inode, offset);
1382
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001383 pg_start = offset >> PAGE_SHIFT;
1384 pg_end = (offset + len) >> PAGE_SHIFT;
Chao Yuf62185d2015-05-28 19:16:57 +08001385 delta = pg_end - pg_start;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001386 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
Chao Yuf62185d2015-05-28 19:16:57 +08001387
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001388 while (!ret && idx > pg_start) {
1389 nr = idx - pg_start;
1390 if (nr > delta)
1391 nr = delta;
1392 idx -= nr;
1393
Chao Yuf62185d2015-05-28 19:16:57 +08001394 f2fs_lock_op(sbi);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -07001395 f2fs_drop_extent_tree(inode);
1396
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -07001397 ret = __exchange_data_block(inode, inode, idx,
1398 idx + delta, nr, false);
Chao Yuf62185d2015-05-28 19:16:57 +08001399 f2fs_unlock_op(sbi);
1400 }
1401
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001402 /* write out all moved pages, if possible */
1403 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1404 truncate_pagecache(inode, offset);
1405
1406 if (!ret)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001407 f2fs_i_size_write(inode, new_size);
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001408
1409 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
Hyojun Kim63da4202017-10-06 17:10:08 -07001410out:
1411 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yuf62185d2015-05-28 19:16:57 +08001412 return ret;
1413}
1414
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001415static int expand_inode_data(struct inode *inode, loff_t offset,
1416 loff_t len, int mode)
1417{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001418 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001419 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1420 pgoff_t pg_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001421 loff_t new_size = i_size_read(inode);
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001422 loff_t off_end;
Hyojun Kim63da4202017-10-06 17:10:08 -07001423 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001424
Hyojun Kim63da4202017-10-06 17:10:08 -07001425 err = inode_newsize_ok(inode, (len + offset));
1426 if (err)
1427 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001428
Hyojun Kim63da4202017-10-06 17:10:08 -07001429 err = f2fs_convert_inline_inode(inode);
1430 if (err)
1431 return err;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001432
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001433 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001434
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001435 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001436 off_end = (offset + len) & (PAGE_SIZE - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001437
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001438 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1439 map.m_len = pg_end - map.m_lblk;
1440 if (off_end)
1441 map.m_len++;
Jaegeuk Kimead43272014-06-13 13:05:55 +09001442
Hyojun Kim63da4202017-10-06 17:10:08 -07001443 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1444 if (err) {
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001445 pgoff_t last_off;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001446
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001447 if (!map.m_len)
Hyojun Kim63da4202017-10-06 17:10:08 -07001448 return err;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001449
Jaegeuk Kime12dd7b2016-05-06 15:30:38 -07001450 last_off = map.m_lblk + map.m_len - 1;
1451
1452 /* update new size to the failed position */
1453 new_size = (last_off == pg_end) ? offset + len:
1454 (loff_t)(last_off + 1) << PAGE_SHIFT;
1455 } else {
1456 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001457 }
1458
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001459 if (new_size > i_size_read(inode)) {
1460 if (mode & FALLOC_FL_KEEP_SIZE)
1461 file_set_keep_isize(inode);
1462 else
1463 f2fs_i_size_write(inode, new_size);
1464 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001465
Hyojun Kim63da4202017-10-06 17:10:08 -07001466 return err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001467}
1468
1469static long f2fs_fallocate(struct file *file, int mode,
1470 loff_t offset, loff_t len)
1471{
Al Viro6131ffa2013-02-27 16:59:05 -05001472 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001473 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001474
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001475 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1476 return -EIO;
1477
Chao Yuc9980122015-09-11 14:39:02 +08001478 /* f2fs only support ->fallocate for regular file */
1479 if (!S_ISREG(inode->i_mode))
1480 return -EINVAL;
1481
Chao Yuf62185d2015-05-28 19:16:57 +08001482 if (f2fs_encrypted_inode(inode) &&
1483 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001484 return -EOPNOTSUPP;
1485
Chao Yub4ace332015-05-06 13:09:46 +08001486 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001487 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1488 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001489 return -EOPNOTSUPP;
1490
Al Viro59551022016-01-22 15:40:57 -05001491 inode_lock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001492
Taehee Yoo587c0a42015-04-21 15:59:12 +09001493 if (mode & FALLOC_FL_PUNCH_HOLE) {
1494 if (offset >= inode->i_size)
1495 goto out;
1496
Chao Yua66c7b22013-11-22 16:52:50 +08001497 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001498 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1499 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001500 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1501 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001502 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1503 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001504 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001505 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001506 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001507
Namjae Jeon3af60a42012-12-30 14:52:37 +09001508 if (!ret) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001509 inode->i_mtime = inode->i_ctime = current_time(inode);
Hyojun Kim63da4202017-10-06 17:10:08 -07001510 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001511 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Namjae Jeon3af60a42012-12-30 14:52:37 +09001512 }
Chao Yu3375f692014-01-28 10:29:26 +08001513
Taehee Yoo587c0a42015-04-21 15:59:12 +09001514out:
Al Viro59551022016-01-22 15:40:57 -05001515 inode_unlock(inode);
Chao Yu3375f692014-01-28 10:29:26 +08001516
Namjae Jeonc01e2852013-04-23 17:00:52 +09001517 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001518 return ret;
1519}
1520
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001521static int f2fs_release_file(struct inode *inode, struct file *filp)
1522{
Jaegeuk Kimde5307e2016-04-11 11:51:51 -07001523 /*
1524 * f2fs_relase_file is called at every close calls. So we should
1525 * not drop any inmemory pages by close called by other process.
1526 */
1527 if (!(filp->f_mode & FMODE_WRITE) ||
1528 atomic_read(&inode->i_writecount) != 1)
1529 return 0;
1530
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001531 /* some remained atomic pages should discarded */
1532 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001533 drop_inmem_pages(inode);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001534 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001535 clear_inode_flag(inode, FI_VOLATILE_FILE);
Hyojun Kim63da4202017-10-06 17:10:08 -07001536 stat_dec_volatile_write(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001537 set_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001538 filemap_fdatawrite(inode->i_mapping);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001539 clear_inode_flag(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001540 }
1541 return 0;
1542}
1543
Hyojun Kim63da4202017-10-06 17:10:08 -07001544static int f2fs_file_flush(struct file *file, fl_owner_t id)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001545{
Hyojun Kim63da4202017-10-06 17:10:08 -07001546 struct inode *inode = file_inode(file);
1547
1548 /*
1549 * If the process doing a transaction is crashed, we should do
1550 * roll-back. Otherwise, other reader/write can see corrupted database
1551 * until all the writers close its file. Since this should be done
1552 * before dropping file lock, it needs to do in ->flush.
1553 */
1554 if (f2fs_is_atomic_file(inode) &&
1555 F2FS_I(inode)->inmem_task == current)
1556 drop_inmem_pages(inode);
1557 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001558}
1559
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001560static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001561{
Al Viro6131ffa2013-02-27 16:59:05 -05001562 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001563 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001564 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1565 return put_user(flags, (int __user *)arg);
1566}
1567
1568static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1569{
1570 struct inode *inode = file_inode(filp);
1571 struct f2fs_inode_info *fi = F2FS_I(inode);
Sheng Yong69494222016-08-23 20:10:47 +08001572 unsigned int flags;
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001573 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001574 int ret;
1575
Chao Yu7fb17fe2016-05-09 19:56:32 +08001576 if (!inode_owner_or_capable(inode))
1577 return -EACCES;
1578
1579 if (get_user(flags, (int __user *)arg))
1580 return -EFAULT;
1581
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001582 ret = mnt_want_write_file(filp);
1583 if (ret)
1584 return ret;
1585
Al Viro59551022016-01-22 15:40:57 -05001586 inode_lock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001587
Hyojun Kim63da4202017-10-06 17:10:08 -07001588 /* Is it quota file? Do not allow user to mess with it */
1589 if (IS_NOQUOTA(inode)) {
1590 ret = -EPERM;
1591 goto unlock_out;
1592 }
1593
1594 flags = f2fs_mask_flags(inode->i_mode, flags);
1595
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001596 oldflags = fi->i_flags;
1597
1598 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1599 if (!capable(CAP_LINUX_IMMUTABLE)) {
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001600 ret = -EPERM;
Hyojun Kim63da4202017-10-06 17:10:08 -07001601 goto unlock_out;
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001602 }
1603 }
1604
1605 flags = flags & FS_FL_USER_MODIFIABLE;
1606 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1607 fi->i_flags = flags;
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001608
Deepa Dinamani078cd822016-09-14 07:48:04 -07001609 inode->i_ctime = current_time(inode);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001610 f2fs_set_inode_flags(inode);
Hyojun Kim63da4202017-10-06 17:10:08 -07001611 f2fs_mark_inode_dirty_sync(inode, false);
1612unlock_out:
1613 inode_unlock(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001614 mnt_drop_write_file(filp);
1615 return ret;
1616}
1617
Chao Yud49f3e82015-01-23 20:36:04 +08001618static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1619{
1620 struct inode *inode = file_inode(filp);
1621
1622 return put_user(inode->i_generation, (int __user *)arg);
1623}
1624
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001625static int f2fs_ioc_start_atomic_write(struct file *filp)
1626{
1627 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001628 int ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001629
1630 if (!inode_owner_or_capable(inode))
1631 return -EACCES;
1632
Hyojun Kim63da4202017-10-06 17:10:08 -07001633 if (!S_ISREG(inode->i_mode))
1634 return -EINVAL;
1635
Chao Yu7fb17fe2016-05-09 19:56:32 +08001636 ret = mnt_want_write_file(filp);
1637 if (ret)
1638 return ret;
1639
Chao Yu0fac5582016-05-09 19:56:33 +08001640 inode_lock(inode);
1641
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001642 if (f2fs_is_atomic_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001643 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001644
Chao Yuf4c9c742015-07-17 18:06:35 +08001645 ret = f2fs_convert_inline_inode(inode);
1646 if (ret)
Chao Yu7fb17fe2016-05-09 19:56:32 +08001647 goto out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001648
Jaegeuk Kim91942322016-05-20 10:13:22 -07001649 set_inode_flag(inode, FI_ATOMIC_FILE);
Hyojun Kim63da4202017-10-06 17:10:08 -07001650 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001651 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1652
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001653 if (!get_dirty_pages(inode))
Hyojun Kim63da4202017-10-06 17:10:08 -07001654 goto inc_stat;
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001655
1656 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001657 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001658 inode->i_ino, get_dirty_pages(inode));
1659 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
Hyojun Kim63da4202017-10-06 17:10:08 -07001660 if (ret) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001661 clear_inode_flag(inode, FI_ATOMIC_FILE);
Hyojun Kim63da4202017-10-06 17:10:08 -07001662 clear_inode_flag(inode, FI_HOT_DATA);
1663 goto out;
1664 }
1665
1666inc_stat:
1667 F2FS_I(inode)->inmem_task = current;
1668 stat_inc_atomic_write(inode);
1669 stat_update_max_atomic_write(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001670out:
Chao Yu0fac5582016-05-09 19:56:33 +08001671 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001672 mnt_drop_write_file(filp);
Jaegeuk Kimc27753d2016-04-12 14:36:11 -07001673 return ret;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001674}
1675
1676static int f2fs_ioc_commit_atomic_write(struct file *filp)
1677{
1678 struct inode *inode = file_inode(filp);
1679 int ret;
1680
1681 if (!inode_owner_or_capable(inode))
1682 return -EACCES;
1683
1684 ret = mnt_want_write_file(filp);
1685 if (ret)
1686 return ret;
1687
Chao Yu0fac5582016-05-09 19:56:33 +08001688 inode_lock(inode);
1689
Chao Yu7fb17fe2016-05-09 19:56:32 +08001690 if (f2fs_is_volatile_file(inode))
1691 goto err_out;
1692
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001693 if (f2fs_is_atomic_file(inode)) {
Chao Yu29b96b52016-02-06 14:38:29 +08001694 ret = commit_inmem_pages(inode);
Hyojun Kim63da4202017-10-06 17:10:08 -07001695 if (ret)
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001696 goto err_out;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001697
Hyojun Kim63da4202017-10-06 17:10:08 -07001698 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1699 if (!ret) {
1700 clear_inode_flag(inode, FI_ATOMIC_FILE);
1701 clear_inode_flag(inode, FI_HOT_DATA);
1702 stat_dec_atomic_write(inode);
1703 }
1704 } else {
1705 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
1706 }
Jaegeuk Kimedb27de2015-07-25 00:52:52 -07001707err_out:
Chao Yu0fac5582016-05-09 19:56:33 +08001708 inode_unlock(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001709 mnt_drop_write_file(filp);
1710 return ret;
1711}
1712
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001713static int f2fs_ioc_start_volatile_write(struct file *filp)
1714{
1715 struct inode *inode = file_inode(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001716 int ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001717
1718 if (!inode_owner_or_capable(inode))
1719 return -EACCES;
1720
Hyojun Kim63da4202017-10-06 17:10:08 -07001721 if (!S_ISREG(inode->i_mode))
1722 return -EINVAL;
1723
Chao Yu7fb17fe2016-05-09 19:56:32 +08001724 ret = mnt_want_write_file(filp);
Chao Yuf4c9c742015-07-17 18:06:35 +08001725 if (ret)
1726 return ret;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001727
Chao Yu0fac5582016-05-09 19:56:33 +08001728 inode_lock(inode);
1729
Chao Yu7fb17fe2016-05-09 19:56:32 +08001730 if (f2fs_is_volatile_file(inode))
1731 goto out;
1732
1733 ret = f2fs_convert_inline_inode(inode);
1734 if (ret)
1735 goto out;
1736
Hyojun Kim63da4202017-10-06 17:10:08 -07001737 stat_inc_volatile_write(inode);
1738 stat_update_max_volatile_write(inode);
1739
Jaegeuk Kim91942322016-05-20 10:13:22 -07001740 set_inode_flag(inode, FI_VOLATILE_FILE);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001741 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001742out:
Chao Yu0fac5582016-05-09 19:56:33 +08001743 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001744 mnt_drop_write_file(filp);
1745 return ret;
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001746}
1747
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001748static int f2fs_ioc_release_volatile_write(struct file *filp)
1749{
1750 struct inode *inode = file_inode(filp);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001751 int ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001752
1753 if (!inode_owner_or_capable(inode))
1754 return -EACCES;
1755
Chao Yu7fb17fe2016-05-09 19:56:32 +08001756 ret = mnt_want_write_file(filp);
1757 if (ret)
1758 return ret;
1759
Chao Yu0fac5582016-05-09 19:56:33 +08001760 inode_lock(inode);
1761
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001762 if (!f2fs_is_volatile_file(inode))
Chao Yu7fb17fe2016-05-09 19:56:32 +08001763 goto out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001764
Chao Yu7fb17fe2016-05-09 19:56:32 +08001765 if (!f2fs_is_first_block_written(inode)) {
1766 ret = truncate_partial_data_page(inode, 0, true);
1767 goto out;
1768 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001769
Chao Yu7fb17fe2016-05-09 19:56:32 +08001770 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1771out:
Chao Yu0fac5582016-05-09 19:56:33 +08001772 inode_unlock(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001773 mnt_drop_write_file(filp);
1774 return ret;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001775}
1776
1777static int f2fs_ioc_abort_volatile_write(struct file *filp)
1778{
1779 struct inode *inode = file_inode(filp);
1780 int ret;
1781
1782 if (!inode_owner_or_capable(inode))
1783 return -EACCES;
1784
1785 ret = mnt_want_write_file(filp);
1786 if (ret)
1787 return ret;
1788
Chao Yu0fac5582016-05-09 19:56:33 +08001789 inode_lock(inode);
1790
Jaegeuk Kim26dc3d42016-04-11 13:15:10 -07001791 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +08001792 drop_inmem_pages(inode);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001793 if (f2fs_is_volatile_file(inode)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07001794 clear_inode_flag(inode, FI_VOLATILE_FILE);
Hyojun Kim63da4202017-10-06 17:10:08 -07001795 stat_dec_volatile_write(inode);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001796 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
Jaegeuk Kim732d5642015-12-29 15:46:33 -08001797 }
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001798
Chao Yu0fac5582016-05-09 19:56:33 +08001799 inode_unlock(inode);
1800
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001801 mnt_drop_write_file(filp);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001802 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001803 return ret;
1804}
1805
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001806static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1807{
1808 struct inode *inode = file_inode(filp);
1809 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1810 struct super_block *sb = sbi->sb;
1811 __u32 in;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001812 int ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001813
1814 if (!capable(CAP_SYS_ADMIN))
1815 return -EPERM;
1816
1817 if (get_user(in, (__u32 __user *)arg))
1818 return -EFAULT;
1819
Chao Yu7fb17fe2016-05-09 19:56:32 +08001820 ret = mnt_want_write_file(filp);
1821 if (ret)
1822 return ret;
1823
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001824 switch (in) {
1825 case F2FS_GOING_DOWN_FULLSYNC:
1826 sb = freeze_bdev(sb->s_bdev);
1827 if (sb && !IS_ERR(sb)) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001828 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001829 thaw_bdev(sb->s_bdev, sb);
1830 }
1831 break;
1832 case F2FS_GOING_DOWN_METASYNC:
1833 /* do checkpoint only */
1834 f2fs_sync_fs(sb, 1);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001835 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001836 break;
1837 case F2FS_GOING_DOWN_NOSYNC:
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001838 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001839 break;
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001840 case F2FS_GOING_DOWN_METAFLUSH:
Hyojun Kim63da4202017-10-06 17:10:08 -07001841 sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -07001842 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kimc912a822015-10-07 09:46:37 -07001843 break;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001844 default:
Chao Yu7fb17fe2016-05-09 19:56:32 +08001845 ret = -EINVAL;
1846 goto out;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001847 }
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001848 f2fs_update_time(sbi, REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001849out:
1850 mnt_drop_write_file(filp);
1851 return ret;
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001852}
1853
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001854static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1855{
1856 struct inode *inode = file_inode(filp);
1857 struct super_block *sb = inode->i_sb;
1858 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1859 struct fstrim_range range;
1860 int ret;
1861
1862 if (!capable(CAP_SYS_ADMIN))
1863 return -EPERM;
1864
1865 if (!blk_queue_discard(q))
1866 return -EOPNOTSUPP;
1867
1868 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1869 sizeof(range)))
1870 return -EFAULT;
1871
Chao Yu7fb17fe2016-05-09 19:56:32 +08001872 ret = mnt_want_write_file(filp);
1873 if (ret)
1874 return ret;
1875
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001876 range.minlen = max((unsigned int)range.minlen,
1877 q->limits.discard_granularity);
1878 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001879 mnt_drop_write_file(filp);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001880 if (ret < 0)
1881 return ret;
1882
1883 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1884 sizeof(range)))
1885 return -EFAULT;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001886 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001887 return 0;
1888}
1889
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001890static bool uuid_is_nonzero(__u8 u[16])
1891{
1892 int i;
1893
1894 for (i = 0; i < 16; i++)
1895 if (u[i])
1896 return true;
1897 return false;
1898}
1899
1900static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1901{
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001902 struct inode *inode = file_inode(filp);
1903
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001904 if (!f2fs_sb_has_crypto(inode->i_sb))
1905 return -EOPNOTSUPP;
1906
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001907 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu7fb17fe2016-05-09 19:56:32 +08001908
Hyojun Kim63da4202017-10-06 17:10:08 -07001909 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001910}
1911
1912static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1913{
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08001914 if (!f2fs_sb_has_crypto(file_inode(filp)->i_sb))
1915 return -EOPNOTSUPP;
Hyojun Kim63da4202017-10-06 17:10:08 -07001916 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001917}
1918
1919static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1920{
1921 struct inode *inode = file_inode(filp);
1922 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1923 int err;
1924
1925 if (!f2fs_sb_has_crypto(inode->i_sb))
1926 return -EOPNOTSUPP;
1927
1928 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1929 goto got_it;
1930
1931 err = mnt_want_write_file(filp);
1932 if (err)
1933 return err;
1934
1935 /* update superblock with uuid */
1936 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1937
Chao Yuc5bda1c2015-06-08 13:28:03 +08001938 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001939 if (err) {
1940 /* undo new data */
1941 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
Chao Yue8240f62015-12-15 17:19:26 +08001942 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001943 return err;
1944 }
Chao Yue8240f62015-12-15 17:19:26 +08001945 mnt_drop_write_file(filp);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001946got_it:
1947 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1948 16))
1949 return -EFAULT;
1950 return 0;
1951}
1952
Chao Yuc1c1b582015-07-10 18:08:10 +08001953static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1954{
1955 struct inode *inode = file_inode(filp);
1956 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yud530d4d2015-10-05 22:22:44 +08001957 __u32 sync;
Chao Yu7fb17fe2016-05-09 19:56:32 +08001958 int ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08001959
1960 if (!capable(CAP_SYS_ADMIN))
1961 return -EPERM;
1962
Chao Yud530d4d2015-10-05 22:22:44 +08001963 if (get_user(sync, (__u32 __user *)arg))
Chao Yuc1c1b582015-07-10 18:08:10 +08001964 return -EFAULT;
1965
Chao Yud530d4d2015-10-05 22:22:44 +08001966 if (f2fs_readonly(sbi->sb))
1967 return -EROFS;
Chao Yuc1c1b582015-07-10 18:08:10 +08001968
Chao Yu7fb17fe2016-05-09 19:56:32 +08001969 ret = mnt_want_write_file(filp);
1970 if (ret)
1971 return ret;
1972
Chao Yud530d4d2015-10-05 22:22:44 +08001973 if (!sync) {
Chao Yu7fb17fe2016-05-09 19:56:32 +08001974 if (!mutex_trylock(&sbi->gc_mutex)) {
1975 ret = -EBUSY;
1976 goto out;
1977 }
Chao Yud530d4d2015-10-05 22:22:44 +08001978 } else {
1979 mutex_lock(&sbi->gc_mutex);
Chao Yuc1c1b582015-07-10 18:08:10 +08001980 }
1981
Hyojun Kim63da4202017-10-06 17:10:08 -07001982 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
1983out:
1984 mnt_drop_write_file(filp);
1985 return ret;
1986}
1987
1988static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
1989{
1990 struct inode *inode = file_inode(filp);
1991 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1992 struct f2fs_gc_range range;
1993 u64 end;
1994 int ret;
1995
1996 if (!capable(CAP_SYS_ADMIN))
1997 return -EPERM;
1998
1999 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2000 sizeof(range)))
2001 return -EFAULT;
2002
2003 if (f2fs_readonly(sbi->sb))
2004 return -EROFS;
2005
2006 ret = mnt_want_write_file(filp);
2007 if (ret)
2008 return ret;
2009
2010 end = range.start + range.len;
2011 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi))
2012 return -EINVAL;
2013do_more:
2014 if (!range.sync) {
2015 if (!mutex_trylock(&sbi->gc_mutex)) {
2016 ret = -EBUSY;
2017 goto out;
2018 }
2019 } else {
2020 mutex_lock(&sbi->gc_mutex);
2021 }
2022
2023 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2024 range.start += sbi->blocks_per_seg;
2025 if (range.start <= end)
2026 goto do_more;
Chao Yu7fb17fe2016-05-09 19:56:32 +08002027out:
2028 mnt_drop_write_file(filp);
2029 return ret;
Chao Yuc1c1b582015-07-10 18:08:10 +08002030}
2031
Chao Yu456b88e2015-10-05 22:24:19 +08002032static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2033{
2034 struct inode *inode = file_inode(filp);
2035 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu7fb17fe2016-05-09 19:56:32 +08002036 int ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002037
2038 if (!capable(CAP_SYS_ADMIN))
2039 return -EPERM;
2040
2041 if (f2fs_readonly(sbi->sb))
2042 return -EROFS;
2043
Chao Yu7fb17fe2016-05-09 19:56:32 +08002044 ret = mnt_want_write_file(filp);
2045 if (ret)
2046 return ret;
2047
2048 ret = f2fs_sync_fs(sbi->sb, 1);
2049
2050 mnt_drop_write_file(filp);
2051 return ret;
Chao Yu456b88e2015-10-05 22:24:19 +08002052}
2053
Chao Yud323d002015-10-27 09:53:45 +08002054static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2055 struct file *filp,
2056 struct f2fs_defragment *range)
2057{
2058 struct inode *inode = file_inode(filp);
Chao Yuda859852016-01-26 15:42:58 +08002059 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
Hyojun Kim63da4202017-10-06 17:10:08 -07002060 struct extent_info ei = {0,0,0};
Chao Yud323d002015-10-27 09:53:45 +08002061 pgoff_t pg_start, pg_end;
Chao Yu3519e3f2015-12-01 11:56:52 +08002062 unsigned int blk_per_seg = sbi->blocks_per_seg;
Chao Yud323d002015-10-27 09:53:45 +08002063 unsigned int total = 0, sec_num;
Chao Yud323d002015-10-27 09:53:45 +08002064 block_t blk_end = 0;
2065 bool fragmented = false;
2066 int err;
2067
2068 /* if in-place-update policy is enabled, don't waste time here */
Hyojun Kim63da4202017-10-06 17:10:08 -07002069 if (need_inplace_update_policy(inode, NULL))
Chao Yud323d002015-10-27 09:53:45 +08002070 return -EINVAL;
2071
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 pg_start = range->start >> PAGE_SHIFT;
2073 pg_end = (range->start + range->len) >> PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002074
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002075 f2fs_balance_fs(sbi, true);
Chao Yud323d002015-10-27 09:53:45 +08002076
Al Viro59551022016-01-22 15:40:57 -05002077 inode_lock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002078
2079 /* writeback all dirty pages in the range */
2080 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
Fan Lid8fe4f02015-12-14 13:34:00 +08002081 range->start + range->len - 1);
Chao Yud323d002015-10-27 09:53:45 +08002082 if (err)
2083 goto out;
2084
2085 /*
2086 * lookup mapping info in extent cache, skip defragmenting if physical
2087 * block addresses are continuous.
2088 */
2089 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2090 if (ei.fofs + ei.len >= pg_end)
2091 goto out;
2092 }
2093
2094 map.m_lblk = pg_start;
Chao Yud323d002015-10-27 09:53:45 +08002095
2096 /*
2097 * lookup mapping info in dnode page cache, skip defragmenting if all
2098 * physical block addresses are continuous even if there are hole(s)
2099 * in logical blocks.
2100 */
2101 while (map.m_lblk < pg_end) {
Fan Lia1c1e9b2015-12-15 17:02:41 +08002102 map.m_len = pg_end - map.m_lblk;
Hyojun Kim63da4202017-10-06 17:10:08 -07002103 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002104 if (err)
2105 goto out;
2106
2107 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2108 map.m_lblk++;
Chao Yud323d002015-10-27 09:53:45 +08002109 continue;
2110 }
2111
2112 if (blk_end && blk_end != map.m_pblk) {
2113 fragmented = true;
2114 break;
2115 }
2116 blk_end = map.m_pblk + map.m_len;
2117
2118 map.m_lblk += map.m_len;
Chao Yud323d002015-10-27 09:53:45 +08002119 }
2120
2121 if (!fragmented)
2122 goto out;
2123
2124 map.m_lblk = pg_start;
2125 map.m_len = pg_end - pg_start;
2126
Hyojun Kim63da4202017-10-06 17:10:08 -07002127 sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
Chao Yud323d002015-10-27 09:53:45 +08002128
2129 /*
2130 * make sure there are enough free section for LFS allocation, this can
2131 * avoid defragment running in SSR mode when free section are allocated
2132 * intensively
2133 */
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002134 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
Chao Yud323d002015-10-27 09:53:45 +08002135 err = -EAGAIN;
2136 goto out;
2137 }
2138
2139 while (map.m_lblk < pg_end) {
2140 pgoff_t idx;
2141 int cnt = 0;
2142
2143do_map:
Fan Lia1c1e9b2015-12-15 17:02:41 +08002144 map.m_len = pg_end - map.m_lblk;
Hyojun Kim63da4202017-10-06 17:10:08 -07002145 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
Chao Yud323d002015-10-27 09:53:45 +08002146 if (err)
2147 goto clear_out;
2148
2149 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2150 map.m_lblk++;
2151 continue;
2152 }
2153
Jaegeuk Kim91942322016-05-20 10:13:22 -07002154 set_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002155
2156 idx = map.m_lblk;
2157 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2158 struct page *page;
2159
2160 page = get_lock_data_page(inode, idx, true);
2161 if (IS_ERR(page)) {
2162 err = PTR_ERR(page);
2163 goto clear_out;
2164 }
2165
2166 set_page_dirty(page);
2167 f2fs_put_page(page, 1);
2168
2169 idx++;
2170 cnt++;
2171 total++;
2172 }
2173
2174 map.m_lblk = idx;
Chao Yud323d002015-10-27 09:53:45 +08002175
2176 if (idx < pg_end && cnt < blk_per_seg)
2177 goto do_map;
2178
Jaegeuk Kim91942322016-05-20 10:13:22 -07002179 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002180
2181 err = filemap_fdatawrite(inode->i_mapping);
2182 if (err)
2183 goto out;
2184 }
2185clear_out:
Jaegeuk Kim91942322016-05-20 10:13:22 -07002186 clear_inode_flag(inode, FI_DO_DEFRAG);
Chao Yud323d002015-10-27 09:53:45 +08002187out:
Al Viro59551022016-01-22 15:40:57 -05002188 inode_unlock(inode);
Chao Yud323d002015-10-27 09:53:45 +08002189 if (!err)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002190 range->len = (u64)total << PAGE_SHIFT;
Chao Yud323d002015-10-27 09:53:45 +08002191 return err;
2192}
2193
2194static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2195{
2196 struct inode *inode = file_inode(filp);
2197 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2198 struct f2fs_defragment range;
2199 int err;
2200
2201 if (!capable(CAP_SYS_ADMIN))
2202 return -EPERM;
2203
Hyojun Kim63da4202017-10-06 17:10:08 -07002204 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2205 return -EINVAL;
2206
2207 if (f2fs_readonly(sbi->sb))
2208 return -EROFS;
2209
2210 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2211 sizeof(range)))
2212 return -EFAULT;
2213
2214 /* verify alignment of offset & size */
2215 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2216 return -EINVAL;
2217
2218 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2219 sbi->max_file_blocks))
Chao Yud323d002015-10-27 09:53:45 +08002220 return -EINVAL;
2221
2222 err = mnt_want_write_file(filp);
2223 if (err)
2224 return err;
2225
Chao Yud323d002015-10-27 09:53:45 +08002226 err = f2fs_defragment_range(sbi, filp, &range);
Hyojun Kim63da4202017-10-06 17:10:08 -07002227 mnt_drop_write_file(filp);
2228
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002229 f2fs_update_time(sbi, REQ_TIME);
Chao Yud323d002015-10-27 09:53:45 +08002230 if (err < 0)
Hyojun Kim63da4202017-10-06 17:10:08 -07002231 return err;
Chao Yud323d002015-10-27 09:53:45 +08002232
2233 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2234 sizeof(range)))
Hyojun Kim63da4202017-10-06 17:10:08 -07002235 return -EFAULT;
2236
2237 return 0;
Chao Yud323d002015-10-27 09:53:45 +08002238}
2239
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002240static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2241 struct file *file_out, loff_t pos_out, size_t len)
2242{
2243 struct inode *src = file_inode(file_in);
2244 struct inode *dst = file_inode(file_out);
2245 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2246 size_t olen = len, dst_max_i_size = 0;
2247 size_t dst_osize;
2248 int ret;
2249
2250 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2251 src->i_sb != dst->i_sb)
2252 return -EXDEV;
2253
2254 if (unlikely(f2fs_readonly(src->i_sb)))
2255 return -EROFS;
2256
Chao Yufe8494b2016-08-04 20:13:02 +08002257 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2258 return -EINVAL;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002259
2260 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2261 return -EOPNOTSUPP;
2262
Fan Lid95fd912016-09-13 11:35:42 +08002263 if (src == dst) {
2264 if (pos_in == pos_out)
2265 return 0;
2266 if (pos_out > pos_in && pos_out < pos_in + len)
2267 return -EINVAL;
2268 }
2269
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002270 inode_lock(src);
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002271 down_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Chao Yu20a3d612016-08-04 20:13:03 +08002272 if (src != dst) {
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002273 ret = -EBUSY;
2274 if (!inode_trylock(dst))
2275 goto out;
2276 if (!down_write_trylock(&F2FS_I(dst)->dio_rwsem[WRITE])) {
2277 inode_unlock(dst);
Chao Yu20a3d612016-08-04 20:13:03 +08002278 goto out;
2279 }
2280 }
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002281
2282 ret = -EINVAL;
2283 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2284 goto out_unlock;
2285 if (len == 0)
2286 olen = len = src->i_size - pos_in;
2287 if (pos_in + len == src->i_size)
2288 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2289 if (len == 0) {
2290 ret = 0;
2291 goto out_unlock;
2292 }
2293
2294 dst_osize = dst->i_size;
2295 if (pos_out + olen > dst->i_size)
2296 dst_max_i_size = pos_out + olen;
2297
2298 /* verify the end result is block aligned */
2299 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2300 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2301 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2302 goto out_unlock;
2303
2304 ret = f2fs_convert_inline_inode(src);
2305 if (ret)
2306 goto out_unlock;
2307
2308 ret = f2fs_convert_inline_inode(dst);
2309 if (ret)
2310 goto out_unlock;
2311
2312 /* write out all dirty pages from offset */
2313 ret = filemap_write_and_wait_range(src->i_mapping,
2314 pos_in, pos_in + len);
2315 if (ret)
2316 goto out_unlock;
2317
2318 ret = filemap_write_and_wait_range(dst->i_mapping,
2319 pos_out, pos_out + len);
2320 if (ret)
2321 goto out_unlock;
2322
2323 f2fs_balance_fs(sbi, true);
2324 f2fs_lock_op(sbi);
Fan Li61e4da12016-09-10 11:19:37 +08002325 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2326 pos_out >> F2FS_BLKSIZE_BITS,
2327 len >> F2FS_BLKSIZE_BITS, false);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002328
2329 if (!ret) {
2330 if (dst_max_i_size)
2331 f2fs_i_size_write(dst, dst_max_i_size);
2332 else if (dst_osize != dst->i_size)
2333 f2fs_i_size_write(dst, dst_osize);
2334 }
2335 f2fs_unlock_op(sbi);
2336out_unlock:
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002337 if (src != dst) {
2338 up_write(&F2FS_I(dst)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002339 inode_unlock(dst);
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002340 }
Chao Yu20a3d612016-08-04 20:13:03 +08002341out:
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002342 up_write(&F2FS_I(src)->dio_rwsem[WRITE]);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002343 inode_unlock(src);
2344 return ret;
2345}
2346
2347static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2348{
2349 struct f2fs_move_range range;
2350 struct fd dst;
2351 int err;
2352
2353 if (!(filp->f_mode & FMODE_READ) ||
2354 !(filp->f_mode & FMODE_WRITE))
2355 return -EBADF;
2356
2357 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2358 sizeof(range)))
2359 return -EFAULT;
2360
2361 dst = fdget(range.dst_fd);
2362 if (!dst.file)
2363 return -EBADF;
2364
2365 if (!(dst.file->f_mode & FMODE_WRITE)) {
2366 err = -EBADF;
2367 goto err_out;
2368 }
2369
2370 err = mnt_want_write_file(filp);
2371 if (err)
2372 goto err_out;
2373
2374 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2375 range.pos_out, range.len);
2376
2377 mnt_drop_write_file(filp);
Hyojun Kim63da4202017-10-06 17:10:08 -07002378 if (err)
2379 goto err_out;
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002380
2381 if (copy_to_user((struct f2fs_move_range __user *)arg,
2382 &range, sizeof(range)))
2383 err = -EFAULT;
2384err_out:
2385 fdput(dst);
2386 return err;
2387}
2388
Hyojun Kim63da4202017-10-06 17:10:08 -07002389static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2390{
2391 struct inode *inode = file_inode(filp);
2392 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2393 struct sit_info *sm = SIT_I(sbi);
2394 unsigned int start_segno = 0, end_segno = 0;
2395 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2396 struct f2fs_flush_device range;
2397 int ret;
2398
2399 if (!capable(CAP_SYS_ADMIN))
2400 return -EPERM;
2401
2402 if (f2fs_readonly(sbi->sb))
2403 return -EROFS;
2404
2405 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2406 sizeof(range)))
2407 return -EFAULT;
2408
2409 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2410 sbi->segs_per_sec != 1) {
2411 f2fs_msg(sbi->sb, KERN_WARNING,
2412 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2413 range.dev_num, sbi->s_ndevs,
2414 sbi->segs_per_sec);
2415 return -EINVAL;
2416 }
2417
2418 ret = mnt_want_write_file(filp);
2419 if (ret)
2420 return ret;
2421
2422 if (range.dev_num != 0)
2423 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2424 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2425
2426 start_segno = sm->last_victim[FLUSH_DEVICE];
2427 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2428 start_segno = dev_start_segno;
2429 end_segno = min(start_segno + range.segments, dev_end_segno);
2430
2431 while (start_segno < end_segno) {
2432 if (!mutex_trylock(&sbi->gc_mutex)) {
2433 ret = -EBUSY;
2434 goto out;
2435 }
2436 sm->last_victim[GC_CB] = end_segno + 1;
2437 sm->last_victim[GC_GREEDY] = end_segno + 1;
2438 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2439 ret = f2fs_gc(sbi, true, true, start_segno);
2440 if (ret == -EAGAIN)
2441 ret = 0;
2442 else if (ret < 0)
2443 break;
2444 start_segno++;
2445 }
2446out:
2447 mnt_drop_write_file(filp);
2448 return ret;
2449}
2450
2451static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2452{
2453 struct inode *inode = file_inode(filp);
2454 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2455
2456 /* Must validate to set it with SQLite behavior in Android. */
2457 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2458
2459 return put_user(sb_feature, (u32 __user *)arg);
2460}
2461
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002462long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2463{
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002464 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2465 return -EIO;
2466
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002467 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002468 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002469 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09002470 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002471 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08002472 case F2FS_IOC_GETVERSION:
2473 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002474 case F2FS_IOC_START_ATOMIC_WRITE:
2475 return f2fs_ioc_start_atomic_write(filp);
2476 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2477 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002478 case F2FS_IOC_START_VOLATILE_WRITE:
2479 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002480 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2481 return f2fs_ioc_release_volatile_write(filp);
2482 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2483 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08002484 case F2FS_IOC_SHUTDOWN:
2485 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07002486 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07002487 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002488 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2489 return f2fs_ioc_set_encryption_policy(filp, arg);
2490 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2491 return f2fs_ioc_get_encryption_policy(filp, arg);
2492 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2493 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Chao Yuc1c1b582015-07-10 18:08:10 +08002494 case F2FS_IOC_GARBAGE_COLLECT:
2495 return f2fs_ioc_gc(filp, arg);
Hyojun Kim63da4202017-10-06 17:10:08 -07002496 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2497 return f2fs_ioc_gc_range(filp, arg);
Chao Yu456b88e2015-10-05 22:24:19 +08002498 case F2FS_IOC_WRITE_CHECKPOINT:
2499 return f2fs_ioc_write_checkpoint(filp, arg);
Chao Yud323d002015-10-27 09:53:45 +08002500 case F2FS_IOC_DEFRAGMENT:
2501 return f2fs_ioc_defragment(filp, arg);
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002502 case F2FS_IOC_MOVE_RANGE:
2503 return f2fs_ioc_move_range(filp, arg);
Hyojun Kim63da4202017-10-06 17:10:08 -07002504 case F2FS_IOC_FLUSH_DEVICE:
2505 return f2fs_ioc_flush_device(filp, arg);
2506 case F2FS_IOC_GET_FEATURES:
2507 return f2fs_ioc_get_features(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002508 default:
2509 return -ENOTTY;
2510 }
2511}
2512
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002513static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2514{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002515 struct file *file = iocb->ki_filp;
2516 struct inode *inode = file_inode(file);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002517 struct blk_plug plug;
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002518 ssize_t ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002519
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002520 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2521 return -EIO;
2522
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002523 inode_lock(inode);
2524 ret = generic_write_checks(iocb, from);
2525 if (ret > 0) {
Hyojun Kim63da4202017-10-06 17:10:08 -07002526 int err;
2527
2528 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2529 set_inode_flag(inode, FI_NO_PREALLOC);
2530
2531 err = f2fs_preallocate_blocks(iocb, from);
2532 if (err) {
Jaegeuk Kim8ae26d12017-09-23 17:02:18 +08002533 clear_inode_flag(inode, FI_NO_PREALLOC);
Hyojun Kim63da4202017-10-06 17:10:08 -07002534 inode_unlock(inode);
2535 return err;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002536 }
Hyojun Kim63da4202017-10-06 17:10:08 -07002537 blk_start_plug(&plug);
2538 ret = __generic_file_write_iter(iocb, from);
2539 blk_finish_plug(&plug);
2540 clear_inode_flag(inode, FI_NO_PREALLOC);
2541
2542 if (ret > 0)
2543 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002544 }
2545 inode_unlock(inode);
2546
Christoph Hellwige2592212016-04-07 08:52:01 -07002547 if (ret > 0)
2548 ret = generic_write_sync(iocb, ret);
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002549 return ret;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002550}
2551
Namjae Jeone9750822013-02-04 23:41:41 +09002552#ifdef CONFIG_COMPAT
2553long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2554{
2555 switch (cmd) {
2556 case F2FS_IOC32_GETFLAGS:
2557 cmd = F2FS_IOC_GETFLAGS;
2558 break;
2559 case F2FS_IOC32_SETFLAGS:
2560 cmd = F2FS_IOC_SETFLAGS;
2561 break;
Chao Yu04ef4b62015-11-10 18:44:20 +08002562 case F2FS_IOC32_GETVERSION:
2563 cmd = F2FS_IOC_GETVERSION;
2564 break;
2565 case F2FS_IOC_START_ATOMIC_WRITE:
2566 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2567 case F2FS_IOC_START_VOLATILE_WRITE:
2568 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2569 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2570 case F2FS_IOC_SHUTDOWN:
2571 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2572 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2573 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2574 case F2FS_IOC_GARBAGE_COLLECT:
Hyojun Kim63da4202017-10-06 17:10:08 -07002575 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
Chao Yu04ef4b62015-11-10 18:44:20 +08002576 case F2FS_IOC_WRITE_CHECKPOINT:
2577 case F2FS_IOC_DEFRAGMENT:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002578 case F2FS_IOC_MOVE_RANGE:
Hyojun Kim63da4202017-10-06 17:10:08 -07002579 case F2FS_IOC_FLUSH_DEVICE:
2580 case F2FS_IOC_GET_FEATURES:
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -07002581 break;
Namjae Jeone9750822013-02-04 23:41:41 +09002582 default:
2583 return -ENOIOCTLCMD;
2584 }
2585 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2586}
2587#endif
2588
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002589const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08002590 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04002591 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002592 .write_iter = f2fs_file_write_iter,
2593 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08002594 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002595 .mmap = f2fs_file_mmap,
Hyojun Kim63da4202017-10-06 17:10:08 -07002596 .flush = f2fs_file_flush,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002597 .fsync = f2fs_sync_file,
2598 .fallocate = f2fs_fallocate,
2599 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09002600#ifdef CONFIG_COMPAT
2601 .compat_ioctl = f2fs_compat_ioctl,
2602#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002603 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002604 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002605};