blob: fe8398f1d627bbd577d1adac0b73d069a14b22ba [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>
Jaegeuk Kimf424f662015-04-20 15:19:06 -070023#include <linux/random.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090024
25#include "f2fs.h"
26#include "node.h"
27#include "segment.h"
28#include "xattr.h"
29#include "acl.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080030#include "trace.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090031#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090032
33static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
34 struct vm_fault *vmf)
35{
36 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050037 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kim40813632014-09-02 15:31:18 -070038 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090039 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080040 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090041
42 f2fs_balance_fs(sbi);
43
44 sb_start_pagefault(inode->i_sb);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070045
46 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -070047
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090048 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080049 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070050 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -070051 err = f2fs_reserve_block(&dn, page->index);
52 if (err) {
53 f2fs_unlock_op(sbi);
54 goto out;
55 }
56 f2fs_put_dnode(&dn);
57 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090058
Namjae Jeon9851e6e2013-04-28 09:04:18 +090059 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090060 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090061 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090062 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090063 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090064 unlock_page(page);
65 err = -EFAULT;
66 goto out;
67 }
68
69 /*
70 * check to see if the page is mapped already (no holes)
71 */
72 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090073 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090074
75 /* page is wholly or partially inside EOF */
76 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
77 unsigned offset;
78 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
79 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
80 }
81 set_page_dirty(page);
82 SetPageUptodate(page);
83
Jaegeuk Kime943a102013-10-25 14:26:31 +090084 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090085mapped:
86 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090087 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090088out:
89 sb_end_pagefault(inode->i_sb);
90 return block_page_mkwrite_return(err);
91}
92
93static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090094 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070095 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090096 .page_mkwrite = f2fs_vm_page_mkwrite,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090097};
98
Jaegeuk Kim354a3392013-06-14 08:52:35 +090099static int get_parent_ino(struct inode *inode, nid_t *pino)
100{
101 struct dentry *dentry;
102
103 inode = igrab(inode);
104 dentry = d_find_any_alias(inode);
105 iput(inode);
106 if (!dentry)
107 return 0;
108
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700109 if (update_dent_inode(inode, inode, &dentry->d_name)) {
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900110 dput(dentry);
111 return 0;
112 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900113
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900114 *pino = parent_ino(dentry);
115 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900116 return 1;
117}
118
Chao Yu9d1589e2014-08-20 18:37:35 +0800119static inline bool need_do_checkpoint(struct inode *inode)
120{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700121 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800122 bool need_cp = false;
123
124 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
125 need_cp = true;
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700126 else if (file_enc_name(inode) && need_dentry_mark(sbi, inode->i_ino))
127 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800128 else if (file_wrong_pino(inode))
129 need_cp = true;
130 else if (!space_for_roll_forward(sbi))
131 need_cp = true;
132 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
133 need_cp = true;
134 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
135 need_cp = true;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700136 else if (test_opt(sbi, FASTBOOT))
137 need_cp = true;
Jaegeuk Kima344b9f2014-11-05 20:05:53 -0800138 else if (sbi->active_logs == 2)
139 need_cp = true;
Chao Yu9d1589e2014-08-20 18:37:35 +0800140
141 return need_cp;
142}
143
Changman Lee9c7bb702014-12-08 15:29:40 +0900144static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
145{
146 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
147 bool ret = false;
148 /* But we need to avoid that there are some inode updates */
149 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
150 ret = true;
151 f2fs_put_page(i, 0);
152 return ret;
153}
154
Changman Lee51455b12014-12-08 15:29:41 +0900155static void try_to_fix_pino(struct inode *inode)
156{
157 struct f2fs_inode_info *fi = F2FS_I(inode);
158 nid_t pino;
159
160 down_write(&fi->i_sem);
161 fi->xattr_ver = 0;
162 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
163 get_parent_ino(inode, &pino)) {
164 fi->i_pino = pino;
165 file_got_pino(inode);
166 up_write(&fi->i_sem);
167
168 mark_inode_dirty_sync(inode);
169 f2fs_write_inode(inode, NULL);
170 } else {
171 up_write(&fi->i_sem);
172 }
173}
174
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900175int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
176{
177 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900178 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700179 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700180 nid_t ino = inode->i_ino;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900181 int ret = 0;
182 bool need_cp = false;
183 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900184 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900185 .nr_to_write = LONG_MAX,
186 .for_reclaim = 0,
187 };
188
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900189 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900190 return 0;
191
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900192 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700193
194 /* if fdatasync is triggered, let's do in-place-update */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700195 if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700196 set_inode_flag(fi, FI_NEED_IPU);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900197 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700198 clear_inode_flag(fi, FI_NEED_IPU);
199
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900200 if (ret) {
201 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900202 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900203 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900204
Changman Lee9c7bb702014-12-08 15:29:40 +0900205 /* if the inode is dirty, let's recover all the time */
206 if (!datasync && is_inode_flag_set(fi, FI_DIRTY_INODE)) {
207 update_inode_page(inode);
208 goto go_write;
209 }
210
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700211 /*
212 * if there is no written data, don't waste time to write recovery info.
213 */
214 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700215 !exist_written_data(sbi, ino, APPEND_INO)) {
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700216
Changman Lee9c7bb702014-12-08 15:29:40 +0900217 /* it may call write_inode just prior to fsync */
218 if (need_inode_page_update(sbi, ino))
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700219 goto go_write;
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700220
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700221 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
Jaegeuk Kim2403c152014-09-10 14:58:18 -0700222 exist_written_data(sbi, ino, UPDATE_INO))
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700223 goto flush_out;
224 goto out;
225 }
Jaegeuk Kim19c9c462014-09-10 15:04:03 -0700226go_write:
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900227 /* guarantee free sections for fsync */
228 f2fs_balance_fs(sbi);
229
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900230 /*
231 * Both of fdatasync() and fsync() are able to be recovered from
232 * sudden-power-off.
233 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800234 down_read(&fi->i_sem);
235 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900236 up_read(&fi->i_sem);
237
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900238 if (need_cp) {
239 /* all the dirty node pages should be flushed for POR */
240 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900241
Changman Lee51455b12014-12-08 15:29:41 +0900242 /*
243 * We've secured consistency through sync_fs. Following pino
244 * will be used only for fsynced inodes after checkpoint.
245 */
246 try_to_fix_pino(inode);
Jaegeuk Kimcff28522015-03-02 10:48:56 -0800247 clear_inode_flag(fi, FI_APPEND_WRITE);
248 clear_inode_flag(fi, FI_UPDATE_WRITE);
Changman Lee51455b12014-12-08 15:29:41 +0900249 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900250 }
Changman Lee51455b12014-12-08 15:29:41 +0900251sync_nodes:
252 sync_node_pages(sbi, ino, &wbc);
253
Jaegeuk Kim871f5992015-01-09 16:27:17 -0800254 /* if cp_error was enabled, we should avoid infinite loop */
255 if (unlikely(f2fs_cp_error(sbi)))
256 goto out;
257
Changman Lee51455b12014-12-08 15:29:41 +0900258 if (need_inode_block_update(sbi, ino)) {
259 mark_inode_dirty_sync(inode);
260 f2fs_write_inode(inode, NULL);
261 goto sync_nodes;
262 }
263
264 ret = wait_on_node_pages_writeback(sbi, ino);
265 if (ret)
266 goto out;
267
268 /* once recovery info is written, don't need to tack this */
269 remove_dirty_inode(sbi, ino, APPEND_INO);
270 clear_inode_flag(fi, FI_APPEND_WRITE);
271flush_out:
272 remove_dirty_inode(sbi, ino, UPDATE_INO);
273 clear_inode_flag(fi, FI_UPDATE_WRITE);
274 ret = f2fs_issue_flush(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900275out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900276 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700277 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900278 return ret;
279}
280
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900281static pgoff_t __get_first_dirty_index(struct address_space *mapping,
282 pgoff_t pgofs, int whence)
283{
284 struct pagevec pvec;
285 int nr_pages;
286
287 if (whence != SEEK_DATA)
288 return 0;
289
290 /* find first dirty page index */
291 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700292 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
293 PAGECACHE_TAG_DIRTY, 1);
294 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900295 pagevec_release(&pvec);
296 return pgofs;
297}
298
299static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
300 int whence)
301{
302 switch (whence) {
303 case SEEK_DATA:
304 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
305 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
306 return true;
307 break;
308 case SEEK_HOLE:
309 if (blkaddr == NULL_ADDR)
310 return true;
311 break;
312 }
313 return false;
314}
315
Chao Yu267378d2014-04-23 14:10:24 +0800316static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
317{
318 struct inode *inode = file->f_mapping->host;
319 loff_t maxbytes = inode->i_sb->s_maxbytes;
320 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900321 pgoff_t pgofs, end_offset, dirty;
322 loff_t data_ofs = offset;
323 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800324 int err = 0;
325
326 mutex_lock(&inode->i_mutex);
327
328 isize = i_size_read(inode);
329 if (offset >= isize)
330 goto fail;
331
332 /* handle inline data case */
Chao Yu622f28a2014-09-24 18:19:10 +0800333 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Chao Yu267378d2014-04-23 14:10:24 +0800334 if (whence == SEEK_HOLE)
335 data_ofs = isize;
336 goto found;
337 }
338
339 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
340
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900341 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
342
Chao Yu267378d2014-04-23 14:10:24 +0800343 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
344 set_new_dnode(&dn, inode, NULL, NULL, 0);
345 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
346 if (err && err != -ENOENT) {
347 goto fail;
348 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900349 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800350 if (whence == SEEK_DATA) {
351 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
352 F2FS_I(inode));
353 continue;
354 } else {
355 goto found;
356 }
357 }
358
Chao Yu6b2920a2014-07-07 11:21:59 +0800359 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800360
361 /* find data/hole in dnode block */
362 for (; dn.ofs_in_node < end_offset;
363 dn.ofs_in_node++, pgofs++,
Chao Yu2e023172015-02-09 11:23:58 +0800364 data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
Chao Yu267378d2014-04-23 14:10:24 +0800365 block_t blkaddr;
366 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
367
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900368 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800369 f2fs_put_dnode(&dn);
370 goto found;
371 }
372 }
373 f2fs_put_dnode(&dn);
374 }
375
376 if (whence == SEEK_DATA)
377 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800378found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900379 if (whence == SEEK_HOLE && data_ofs > isize)
380 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800381 mutex_unlock(&inode->i_mutex);
382 return vfs_setpos(file, data_ofs, maxbytes);
383fail:
384 mutex_unlock(&inode->i_mutex);
385 return -ENXIO;
386}
387
388static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
389{
390 struct inode *inode = file->f_mapping->host;
391 loff_t maxbytes = inode->i_sb->s_maxbytes;
392
393 switch (whence) {
394 case SEEK_SET:
395 case SEEK_CUR:
396 case SEEK_END:
397 return generic_file_llseek_size(file, offset, whence,
398 maxbytes, i_size_read(inode));
399 case SEEK_DATA:
400 case SEEK_HOLE:
Jaegeuk Kim0b4c5afde2014-09-08 10:59:43 -0700401 if (offset < 0)
402 return -ENXIO;
Chao Yu267378d2014-04-23 14:10:24 +0800403 return f2fs_seek_block(file, offset, whence);
404 }
405
406 return -EINVAL;
407}
408
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900409static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
410{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700411 struct inode *inode = file_inode(file);
412
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700413 if (f2fs_encrypted_inode(inode)) {
414 int err = f2fs_get_encryption_info(inode);
415 if (err)
416 return 0;
417 }
418
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700419 /* we don't need to use inline_data strictly */
420 if (f2fs_has_inline_data(inode)) {
421 int err = f2fs_convert_inline_inode(inode);
422 if (err)
423 return err;
424 }
425
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900426 file_accessed(file);
427 vma->vm_ops = &f2fs_file_vm_ops;
428 return 0;
429}
430
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700431static int f2fs_file_open(struct inode *inode, struct file *filp)
432{
433 int ret = generic_file_open(inode, filp);
434
435 if (!ret && f2fs_encrypted_inode(inode)) {
436 ret = f2fs_get_encryption_info(inode);
437 if (ret)
438 ret = -EACCES;
439 }
440 return ret;
441}
442
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900443int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900444{
445 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700446 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900447 struct f2fs_node *raw_node;
448 __le32 *addr;
449
Gu Zheng45590712013-07-15 17:57:38 +0800450 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900451 addr = blkaddr_in_node(raw_node) + ofs;
452
Chris Fries6c311ec2014-01-17 14:44:39 -0600453 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900454 block_t blkaddr = le32_to_cpu(*addr);
455 if (blkaddr == NULL_ADDR)
456 continue;
457
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800458 dn->data_blkaddr = NULL_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800459 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800460 f2fs_update_extent_cache(dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900461 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700462 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
463 clear_inode_flag(F2FS_I(dn->inode),
464 FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900465 nr_free++;
466 }
467 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900468 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900469 set_page_dirty(dn->node_page);
470 sync_inode_page(dn);
471 }
472 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900473
474 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
475 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900476 return nr_free;
477}
478
479void truncate_data_blocks(struct dnode_of_data *dn)
480{
481 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
482}
483
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800484static int truncate_partial_data_page(struct inode *inode, u64 from,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700485 bool cache_only)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900486{
487 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700488 pgoff_t index = from >> PAGE_CACHE_SHIFT;
489 struct address_space *mapping = inode->i_mapping;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900490 struct page *page;
491
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700492 if (!offset && !cache_only)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700493 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900494
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700495 if (cache_only) {
496 page = grab_cache_page(mapping, index);
497 if (page && PageUptodate(page))
498 goto truncate_out;
499 f2fs_put_page(page, 1);
500 return 0;
501 }
502
503 page = get_lock_data_page(inode, index);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900504 if (IS_ERR(page))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700505 return 0;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700506truncate_out:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900507 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900508 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700509 if (!cache_only || !f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800510 set_page_dirty(page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900511 f2fs_put_page(page, 1);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700512 return 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900513}
514
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700515int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900516{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700517 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900518 unsigned int blocksize = inode->i_sb->s_blocksize;
519 struct dnode_of_data dn;
520 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800521 int count = 0, err = 0;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700522 struct page *ipage;
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800523 bool truncate_page = false;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900524
Namjae Jeon51dd6242013-04-20 01:28:52 +0900525 trace_f2fs_truncate_blocks_enter(inode, from);
526
Jaegeuk Kimf7ef9b82015-02-09 12:02:44 -0800527 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900528
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700529 if (lock)
530 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800531
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700532 ipage = get_node_page(sbi, inode->i_ino);
533 if (IS_ERR(ipage)) {
534 err = PTR_ERR(ipage);
535 goto out;
536 }
537
538 if (f2fs_has_inline_data(inode)) {
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800539 if (truncate_inline_inode(ipage, from))
540 set_page_dirty(ipage);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700541 f2fs_put_page(ipage, 1);
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800542 truncate_page = true;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700543 goto out;
544 }
545
546 set_new_dnode(&dn, inode, ipage, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900547 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900548 if (err) {
549 if (err == -ENOENT)
550 goto free_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700551 goto out;
Jaegeuk Kim1ce86bf2014-10-15 10:24:34 -0700552 }
553
Chao Yu6403eb12014-04-26 19:59:52 +0800554 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900555
556 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700557 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900558
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900559 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
560 truncate_data_blocks_range(&dn, count);
561 free_from += count;
562 }
563
564 f2fs_put_dnode(&dn);
565free_next:
566 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764d2c82014-11-11 11:01:01 -0800567out:
568 if (lock)
569 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700570
571 /* lastly zero out the first data page */
572 if (!err)
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800573 err = truncate_partial_data_page(inode, from, truncate_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900574
Namjae Jeon51dd6242013-04-20 01:28:52 +0900575 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900576 return err;
577}
578
579void f2fs_truncate(struct inode *inode)
580{
581 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
582 S_ISLNK(inode->i_mode)))
583 return;
584
Namjae Jeon51dd6242013-04-20 01:28:52 +0900585 trace_f2fs_truncate(inode);
586
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800587 /* we should check inline_data size */
Jaegeuk Kim01b960e2015-04-23 10:27:21 -0700588 if (f2fs_has_inline_data(inode) && !f2fs_may_inline_data(inode)) {
Jaegeuk Kim92dffd02014-11-11 14:10:01 -0800589 if (f2fs_convert_inline_inode(inode))
590 return;
591 }
592
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700593 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900594 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
595 mark_inode_dirty(inode);
596 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900597}
598
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900599int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900600 struct dentry *dentry, struct kstat *stat)
601{
David Howells2b0143b2015-03-17 22:25:59 +0000602 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900603 generic_fillattr(inode, stat);
604 stat->blocks <<= 3;
605 return 0;
606}
607
608#ifdef CONFIG_F2FS_FS_POSIX_ACL
609static void __setattr_copy(struct inode *inode, const struct iattr *attr)
610{
611 struct f2fs_inode_info *fi = F2FS_I(inode);
612 unsigned int ia_valid = attr->ia_valid;
613
614 if (ia_valid & ATTR_UID)
615 inode->i_uid = attr->ia_uid;
616 if (ia_valid & ATTR_GID)
617 inode->i_gid = attr->ia_gid;
618 if (ia_valid & ATTR_ATIME)
619 inode->i_atime = timespec_trunc(attr->ia_atime,
620 inode->i_sb->s_time_gran);
621 if (ia_valid & ATTR_MTIME)
622 inode->i_mtime = timespec_trunc(attr->ia_mtime,
623 inode->i_sb->s_time_gran);
624 if (ia_valid & ATTR_CTIME)
625 inode->i_ctime = timespec_trunc(attr->ia_ctime,
626 inode->i_sb->s_time_gran);
627 if (ia_valid & ATTR_MODE) {
628 umode_t mode = attr->ia_mode;
629
630 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
631 mode &= ~S_ISGID;
632 set_acl_inode(fi, mode);
633 }
634}
635#else
636#define __setattr_copy setattr_copy
637#endif
638
639int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
640{
David Howells2b0143b2015-03-17 22:25:59 +0000641 struct inode *inode = d_inode(dentry);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900642 struct f2fs_inode_info *fi = F2FS_I(inode);
643 int err;
644
645 err = inode_change_ok(inode, attr);
646 if (err)
647 return err;
648
Chao Yu09db6a22014-09-15 18:02:09 +0800649 if (attr->ia_valid & ATTR_SIZE) {
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700650 if (f2fs_encrypted_inode(inode) &&
651 f2fs_get_encryption_info(inode))
652 return -EACCES;
653
Chao Yu3c454142015-06-05 18:34:02 +0800654 if (attr->ia_size <= i_size_read(inode)) {
Chao Yu09db6a22014-09-15 18:02:09 +0800655 truncate_setsize(inode, attr->ia_size);
656 f2fs_truncate(inode);
657 f2fs_balance_fs(F2FS_I_SB(inode));
658 } else {
659 /*
Chao Yu3c454142015-06-05 18:34:02 +0800660 * do not trim all blocks after i_size if target size is
661 * larger than i_size.
Chao Yu09db6a22014-09-15 18:02:09 +0800662 */
Chao Yu3c454142015-06-05 18:34:02 +0800663 truncate_setsize(inode, attr->ia_size);
Chao Yu09db6a22014-09-15 18:02:09 +0800664 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665 }
666
667 __setattr_copy(inode, attr);
668
669 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800670 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900671 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
672 inode->i_mode = fi->i_acl_mode;
673 clear_inode_flag(fi, FI_ACL_MODE);
674 }
675 }
676
677 mark_inode_dirty(inode);
678 return err;
679}
680
681const struct inode_operations f2fs_file_inode_operations = {
682 .getattr = f2fs_getattr,
683 .setattr = f2fs_setattr,
684 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800685 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900686#ifdef CONFIG_F2FS_FS_XATTR
687 .setxattr = generic_setxattr,
688 .getxattr = generic_getxattr,
689 .listxattr = f2fs_listxattr,
690 .removexattr = generic_removexattr,
691#endif
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900692 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900693};
694
695static void fill_zero(struct inode *inode, pgoff_t index,
696 loff_t start, loff_t len)
697{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700698 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900699 struct page *page;
700
701 if (!len)
702 return;
703
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900704 f2fs_balance_fs(sbi);
705
Gu Zhenge4795562013-09-27 18:08:30 +0800706 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900707 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800708 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900709
710 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900711 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900712 zero_user(page, start, len);
713 set_page_dirty(page);
714 f2fs_put_page(page, 1);
715 }
716}
717
718int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
719{
720 pgoff_t index;
721 int err;
722
723 for (index = pg_start; index < pg_end; index++) {
724 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900725
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900727 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900728 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900729 if (err == -ENOENT)
730 continue;
731 return err;
732 }
733
734 if (dn.data_blkaddr != NULL_ADDR)
735 truncate_data_blocks_range(&dn, 1);
736 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900737 }
738 return 0;
739}
740
Chao Yua66c7b22013-11-22 16:52:50 +0800741static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900742{
743 pgoff_t pg_start, pg_end;
744 loff_t off_start, off_end;
745 int ret = 0;
746
Chao Yu14cecc52014-09-15 18:03:32 +0800747 if (!S_ISREG(inode->i_mode))
748 return -EOPNOTSUPP;
749
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700750 if (f2fs_has_inline_data(inode)) {
751 ret = f2fs_convert_inline_inode(inode);
752 if (ret)
753 return ret;
754 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800755
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900756 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
757 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
758
759 off_start = offset & (PAGE_CACHE_SIZE - 1);
760 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
761
762 if (pg_start == pg_end) {
763 fill_zero(inode, pg_start, off_start,
764 off_end - off_start);
765 } else {
766 if (off_start)
767 fill_zero(inode, pg_start++, off_start,
768 PAGE_CACHE_SIZE - off_start);
769 if (off_end)
770 fill_zero(inode, pg_end, 0, off_end);
771
772 if (pg_start < pg_end) {
773 struct address_space *mapping = inode->i_mapping;
774 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700775 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500776
777 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900778
779 blk_start = pg_start << PAGE_CACHE_SHIFT;
780 blk_end = pg_end << PAGE_CACHE_SHIFT;
781 truncate_inode_pages_range(mapping, blk_start,
782 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900783
Gu Zhenge4795562013-09-27 18:08:30 +0800784 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900785 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800786 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900787 }
788 }
789
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900790 return ret;
791}
792
Chao Yub4ace332015-05-06 13:09:46 +0800793static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
794{
795 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
796 struct dnode_of_data dn;
797 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
798 int ret = 0;
799
800 f2fs_lock_op(sbi);
801
802 for (; end < nrpages; start++, end++) {
803 block_t new_addr, old_addr;
804
805 set_new_dnode(&dn, inode, NULL, NULL, 0);
806 ret = get_dnode_of_data(&dn, end, LOOKUP_NODE_RA);
807 if (ret && ret != -ENOENT) {
808 goto out;
809 } else if (ret == -ENOENT) {
810 new_addr = NULL_ADDR;
811 } else {
812 new_addr = dn.data_blkaddr;
813 truncate_data_blocks_range(&dn, 1);
814 f2fs_put_dnode(&dn);
815 }
816
817 if (new_addr == NULL_ADDR) {
818 set_new_dnode(&dn, inode, NULL, NULL, 0);
819 ret = get_dnode_of_data(&dn, start, LOOKUP_NODE_RA);
820 if (ret && ret != -ENOENT)
821 goto out;
822 else if (ret == -ENOENT)
823 continue;
824
825 if (dn.data_blkaddr == NULL_ADDR) {
826 f2fs_put_dnode(&dn);
827 continue;
828 } else {
829 truncate_data_blocks_range(&dn, 1);
830 }
831
832 f2fs_put_dnode(&dn);
833 } else {
834 struct page *ipage;
835
836 ipage = get_node_page(sbi, inode->i_ino);
837 if (IS_ERR(ipage)) {
838 ret = PTR_ERR(ipage);
839 goto out;
840 }
841
842 set_new_dnode(&dn, inode, ipage, NULL, 0);
843 ret = f2fs_reserve_block(&dn, start);
844 if (ret)
845 goto out;
846
847 old_addr = dn.data_blkaddr;
848 if (old_addr != NEW_ADDR && new_addr == NEW_ADDR) {
849 dn.data_blkaddr = NULL_ADDR;
850 f2fs_update_extent_cache(&dn);
851 invalidate_blocks(sbi, old_addr);
852
853 dn.data_blkaddr = new_addr;
854 set_data_blkaddr(&dn);
855 } else if (new_addr != NEW_ADDR) {
856 struct node_info ni;
Chao Yub4ace332015-05-06 13:09:46 +0800857
858 get_node_info(sbi, dn.nid, &ni);
Chao Yu528e3452015-05-28 19:15:35 +0800859 f2fs_replace_block(sbi, &dn, old_addr, new_addr,
860 ni.version, true);
Chao Yub4ace332015-05-06 13:09:46 +0800861 }
862
863 f2fs_put_dnode(&dn);
864 }
865 }
866 ret = 0;
867out:
868 f2fs_unlock_op(sbi);
869 return ret;
870}
871
872static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
873{
874 pgoff_t pg_start, pg_end;
875 loff_t new_size;
876 int ret;
877
878 if (!S_ISREG(inode->i_mode))
879 return -EINVAL;
880
881 if (offset + len >= i_size_read(inode))
882 return -EINVAL;
883
884 /* collapse range should be aligned to block size of f2fs. */
885 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
886 return -EINVAL;
887
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -0700888 f2fs_balance_fs(F2FS_I_SB(inode));
889
890 if (f2fs_has_inline_data(inode)) {
891 ret = f2fs_convert_inline_inode(inode);
892 if (ret)
893 return ret;
894 }
895
Chao Yub4ace332015-05-06 13:09:46 +0800896 pg_start = offset >> PAGE_CACHE_SHIFT;
897 pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
898
899 /* write out all dirty pages from offset */
900 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
901 if (ret)
902 return ret;
903
904 truncate_pagecache(inode, offset);
905
906 ret = f2fs_do_collapse(inode, pg_start, pg_end);
907 if (ret)
908 return ret;
909
910 new_size = i_size_read(inode) - len;
911
912 ret = truncate_blocks(inode, new_size, true);
913 if (!ret)
914 i_size_write(inode, new_size);
915
916 return ret;
917}
918
Chao Yu75cd4e02015-05-06 13:11:13 +0800919static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
920 int mode)
921{
922 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
923 struct address_space *mapping = inode->i_mapping;
924 pgoff_t index, pg_start, pg_end;
925 loff_t new_size = i_size_read(inode);
926 loff_t off_start, off_end;
927 int ret = 0;
928
929 if (!S_ISREG(inode->i_mode))
930 return -EINVAL;
931
932 ret = inode_newsize_ok(inode, (len + offset));
933 if (ret)
934 return ret;
935
936 f2fs_balance_fs(sbi);
937
938 if (f2fs_has_inline_data(inode)) {
939 ret = f2fs_convert_inline_inode(inode);
940 if (ret)
941 return ret;
942 }
943
944 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
945 if (ret)
946 return ret;
947
948 truncate_pagecache_range(inode, offset, offset + len - 1);
949
950 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
951 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
952
953 off_start = offset & (PAGE_CACHE_SIZE - 1);
954 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
955
956 if (pg_start == pg_end) {
957 fill_zero(inode, pg_start, off_start, off_end - off_start);
958 if (offset + len > new_size)
959 new_size = offset + len;
960 new_size = max_t(loff_t, new_size, offset + len);
961 } else {
962 if (off_start) {
963 fill_zero(inode, pg_start++, off_start,
964 PAGE_CACHE_SIZE - off_start);
965 new_size = max_t(loff_t, new_size,
966 pg_start << PAGE_CACHE_SHIFT);
967 }
968
969 for (index = pg_start; index < pg_end; index++) {
970 struct dnode_of_data dn;
971 struct page *ipage;
972
973 f2fs_lock_op(sbi);
974
975 ipage = get_node_page(sbi, inode->i_ino);
976 if (IS_ERR(ipage)) {
977 ret = PTR_ERR(ipage);
978 f2fs_unlock_op(sbi);
979 goto out;
980 }
981
982 set_new_dnode(&dn, inode, ipage, NULL, 0);
983 ret = f2fs_reserve_block(&dn, index);
984 if (ret) {
985 f2fs_unlock_op(sbi);
986 goto out;
987 }
988
989 if (dn.data_blkaddr != NEW_ADDR) {
990 invalidate_blocks(sbi, dn.data_blkaddr);
991
992 dn.data_blkaddr = NEW_ADDR;
993 set_data_blkaddr(&dn);
994
995 dn.data_blkaddr = NULL_ADDR;
996 f2fs_update_extent_cache(&dn);
997 }
998 f2fs_put_dnode(&dn);
999 f2fs_unlock_op(sbi);
1000
1001 new_size = max_t(loff_t, new_size,
1002 (index + 1) << PAGE_CACHE_SHIFT);
1003 }
1004
1005 if (off_end) {
1006 fill_zero(inode, pg_end, 0, off_end);
1007 new_size = max_t(loff_t, new_size, offset + len);
1008 }
1009 }
1010
1011out:
1012 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) {
1013 i_size_write(inode, new_size);
1014 mark_inode_dirty(inode);
1015 update_inode_page(inode);
1016 }
1017
1018 return ret;
1019}
1020
Chao Yuf62185d2015-05-28 19:16:57 +08001021static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1022{
1023 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1024 pgoff_t pg_start, pg_end, delta, nrpages, idx;
1025 loff_t new_size;
1026 int ret;
1027
1028 if (!S_ISREG(inode->i_mode))
1029 return -EINVAL;
1030
1031 new_size = i_size_read(inode) + len;
1032 if (new_size > inode->i_sb->s_maxbytes)
1033 return -EFBIG;
1034
1035 if (offset >= i_size_read(inode))
1036 return -EINVAL;
1037
1038 /* insert range should be aligned to block size of f2fs. */
1039 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1040 return -EINVAL;
1041
1042 f2fs_balance_fs(sbi);
1043
Jaegeuk Kim97a7b2c2015-06-17 13:59:05 -07001044 if (f2fs_has_inline_data(inode)) {
1045 ret = f2fs_convert_inline_inode(inode);
1046 if (ret)
1047 return ret;
1048 }
1049
Chao Yuf62185d2015-05-28 19:16:57 +08001050 ret = truncate_blocks(inode, i_size_read(inode), true);
1051 if (ret)
1052 return ret;
1053
1054 /* write out all dirty pages from offset */
1055 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1056 if (ret)
1057 return ret;
1058
1059 truncate_pagecache(inode, offset);
1060
1061 pg_start = offset >> PAGE_CACHE_SHIFT;
1062 pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
1063 delta = pg_end - pg_start;
1064 nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1065
1066 for (idx = nrpages - 1; idx >= pg_start && idx != -1; idx--) {
1067 struct dnode_of_data dn;
1068 struct page *ipage;
1069 block_t new_addr, old_addr;
1070
1071 f2fs_lock_op(sbi);
1072
1073 set_new_dnode(&dn, inode, NULL, NULL, 0);
1074 ret = get_dnode_of_data(&dn, idx, LOOKUP_NODE_RA);
1075 if (ret && ret != -ENOENT) {
1076 goto out;
1077 } else if (ret == -ENOENT) {
1078 goto next;
1079 } else if (dn.data_blkaddr == NULL_ADDR) {
1080 f2fs_put_dnode(&dn);
1081 goto next;
1082 } else {
1083 new_addr = dn.data_blkaddr;
1084 truncate_data_blocks_range(&dn, 1);
1085 f2fs_put_dnode(&dn);
1086 }
1087
1088 ipage = get_node_page(sbi, inode->i_ino);
1089 if (IS_ERR(ipage)) {
1090 ret = PTR_ERR(ipage);
1091 goto out;
1092 }
1093
1094 set_new_dnode(&dn, inode, ipage, NULL, 0);
1095 ret = f2fs_reserve_block(&dn, idx + delta);
1096 if (ret)
1097 goto out;
1098
1099 old_addr = dn.data_blkaddr;
1100 f2fs_bug_on(sbi, old_addr != NEW_ADDR);
1101
1102 if (new_addr != NEW_ADDR) {
1103 struct node_info ni;
1104
1105 get_node_info(sbi, dn.nid, &ni);
1106 f2fs_replace_block(sbi, &dn, old_addr, new_addr,
1107 ni.version, true);
1108 }
1109 f2fs_put_dnode(&dn);
1110next:
1111 f2fs_unlock_op(sbi);
1112 }
1113
1114 i_size_write(inode, new_size);
1115 return 0;
1116out:
1117 f2fs_unlock_op(sbi);
1118 return ret;
1119}
1120
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001121static int expand_inode_data(struct inode *inode, loff_t offset,
1122 loff_t len, int mode)
1123{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001124 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001125 pgoff_t index, pg_start, pg_end;
1126 loff_t new_size = i_size_read(inode);
1127 loff_t off_start, off_end;
1128 int ret = 0;
1129
Chao Yu497a0932014-08-04 10:11:17 +08001130 f2fs_balance_fs(sbi);
1131
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001132 ret = inode_newsize_ok(inode, (len + offset));
1133 if (ret)
1134 return ret;
1135
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001136 if (f2fs_has_inline_data(inode)) {
1137 ret = f2fs_convert_inline_inode(inode);
1138 if (ret)
1139 return ret;
1140 }
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +09001141
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001142 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
1143 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
1144
1145 off_start = offset & (PAGE_CACHE_SIZE - 1);
1146 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
1147
Jaegeuk Kimead43272014-06-13 13:05:55 +09001148 f2fs_lock_op(sbi);
1149
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001150 for (index = pg_start; index <= pg_end; index++) {
1151 struct dnode_of_data dn;
1152
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001153 if (index == pg_end && !off_end)
1154 goto noalloc;
1155
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001156 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001157 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +08001158 if (ret)
1159 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +09001160noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001161 if (pg_start == pg_end)
1162 new_size = offset + len;
1163 else if (index == pg_start && off_start)
1164 new_size = (index + 1) << PAGE_CACHE_SHIFT;
1165 else if (index == pg_end)
1166 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
1167 else
1168 new_size += PAGE_CACHE_SIZE;
1169 }
1170
1171 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
1172 i_size_read(inode) < new_size) {
1173 i_size_write(inode, new_size);
1174 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +09001175 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001176 }
Jaegeuk Kimead43272014-06-13 13:05:55 +09001177 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001178
1179 return ret;
1180}
1181
1182static long f2fs_fallocate(struct file *file, int mode,
1183 loff_t offset, loff_t len)
1184{
Al Viro6131ffa2013-02-27 16:59:05 -05001185 struct inode *inode = file_inode(file);
Taehee Yoo587c0a42015-04-21 15:59:12 +09001186 long ret = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001187
Chao Yuf62185d2015-05-28 19:16:57 +08001188 if (f2fs_encrypted_inode(inode) &&
1189 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001190 return -EOPNOTSUPP;
1191
Chao Yub4ace332015-05-06 13:09:46 +08001192 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Chao Yuf62185d2015-05-28 19:16:57 +08001193 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1194 FALLOC_FL_INSERT_RANGE))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001195 return -EOPNOTSUPP;
1196
Chao Yu3375f692014-01-28 10:29:26 +08001197 mutex_lock(&inode->i_mutex);
1198
Taehee Yoo587c0a42015-04-21 15:59:12 +09001199 if (mode & FALLOC_FL_PUNCH_HOLE) {
1200 if (offset >= inode->i_size)
1201 goto out;
1202
Chao Yua66c7b22013-11-22 16:52:50 +08001203 ret = punch_hole(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001204 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1205 ret = f2fs_collapse_range(inode, offset, len);
Chao Yu75cd4e02015-05-06 13:11:13 +08001206 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1207 ret = f2fs_zero_range(inode, offset, len, mode);
Chao Yuf62185d2015-05-28 19:16:57 +08001208 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1209 ret = f2fs_insert_range(inode, offset, len);
Chao Yub4ace332015-05-06 13:09:46 +08001210 } else {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001211 ret = expand_inode_data(inode, offset, len, mode);
Chao Yub4ace332015-05-06 13:09:46 +08001212 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001213
Namjae Jeon3af60a42012-12-30 14:52:37 +09001214 if (!ret) {
1215 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1216 mark_inode_dirty(inode);
1217 }
Chao Yu3375f692014-01-28 10:29:26 +08001218
Taehee Yoo587c0a42015-04-21 15:59:12 +09001219out:
Chao Yu3375f692014-01-28 10:29:26 +08001220 mutex_unlock(&inode->i_mutex);
1221
Namjae Jeonc01e2852013-04-23 17:00:52 +09001222 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001223 return ret;
1224}
1225
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001226static int f2fs_release_file(struct inode *inode, struct file *filp)
1227{
1228 /* some remained atomic pages should discarded */
1229 if (f2fs_is_atomic_file(inode))
1230 commit_inmem_pages(inode, true);
1231 if (f2fs_is_volatile_file(inode)) {
1232 set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1233 filemap_fdatawrite(inode->i_mapping);
1234 clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1235 }
1236 return 0;
1237}
1238
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001239#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1240#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1241
1242static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1243{
1244 if (S_ISDIR(mode))
1245 return flags;
1246 else if (S_ISREG(mode))
1247 return flags & F2FS_REG_FLMASK;
1248 else
1249 return flags & F2FS_OTHER_FLMASK;
1250}
1251
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001252static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001253{
Al Viro6131ffa2013-02-27 16:59:05 -05001254 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001255 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001256 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1257 return put_user(flags, (int __user *)arg);
1258}
1259
1260static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1261{
1262 struct inode *inode = file_inode(filp);
1263 struct f2fs_inode_info *fi = F2FS_I(inode);
1264 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1265 unsigned int oldflags;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001266 int ret;
1267
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001268 ret = mnt_want_write_file(filp);
1269 if (ret)
1270 return ret;
1271
1272 if (!inode_owner_or_capable(inode)) {
1273 ret = -EACCES;
1274 goto out;
1275 }
1276
1277 if (get_user(flags, (int __user *)arg)) {
1278 ret = -EFAULT;
1279 goto out;
1280 }
1281
1282 flags = f2fs_mask_flags(inode->i_mode, flags);
1283
1284 mutex_lock(&inode->i_mutex);
1285
1286 oldflags = fi->i_flags;
1287
1288 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1289 if (!capable(CAP_LINUX_IMMUTABLE)) {
1290 mutex_unlock(&inode->i_mutex);
1291 ret = -EPERM;
1292 goto out;
1293 }
1294 }
1295
1296 flags = flags & FS_FL_USER_MODIFIABLE;
1297 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1298 fi->i_flags = flags;
1299 mutex_unlock(&inode->i_mutex);
1300
1301 f2fs_set_inode_flags(inode);
1302 inode->i_ctime = CURRENT_TIME;
1303 mark_inode_dirty(inode);
1304out:
1305 mnt_drop_write_file(filp);
1306 return ret;
1307}
1308
Chao Yud49f3e82015-01-23 20:36:04 +08001309static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1310{
1311 struct inode *inode = file_inode(filp);
1312
1313 return put_user(inode->i_generation, (int __user *)arg);
1314}
1315
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001316static int f2fs_ioc_start_atomic_write(struct file *filp)
1317{
1318 struct inode *inode = file_inode(filp);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001319
1320 if (!inode_owner_or_capable(inode))
1321 return -EACCES;
1322
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001323 f2fs_balance_fs(F2FS_I_SB(inode));
1324
1325 if (f2fs_is_atomic_file(inode))
1326 return 0;
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001327
1328 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1329
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001330 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001331}
1332
1333static int f2fs_ioc_commit_atomic_write(struct file *filp)
1334{
1335 struct inode *inode = file_inode(filp);
1336 int ret;
1337
1338 if (!inode_owner_or_capable(inode))
1339 return -EACCES;
1340
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001341 if (f2fs_is_volatile_file(inode))
1342 return 0;
1343
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001344 ret = mnt_want_write_file(filp);
1345 if (ret)
1346 return ret;
1347
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001348 if (f2fs_is_atomic_file(inode)) {
1349 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001350 commit_inmem_pages(inode, false);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001351 }
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001352
1353 ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
1354 mnt_drop_write_file(filp);
1355 return ret;
1356}
1357
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001358static int f2fs_ioc_start_volatile_write(struct file *filp)
1359{
1360 struct inode *inode = file_inode(filp);
1361
1362 if (!inode_owner_or_capable(inode))
1363 return -EACCES;
1364
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001365 if (f2fs_is_volatile_file(inode))
1366 return 0;
1367
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001368 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001369
1370 return f2fs_convert_inline_inode(inode);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001371}
1372
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001373static int f2fs_ioc_release_volatile_write(struct file *filp)
1374{
1375 struct inode *inode = file_inode(filp);
1376
1377 if (!inode_owner_or_capable(inode))
1378 return -EACCES;
1379
1380 if (!f2fs_is_volatile_file(inode))
1381 return 0;
1382
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001383 if (!f2fs_is_first_block_written(inode))
1384 return truncate_partial_data_page(inode, 0, true);
1385
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001386 punch_hole(inode, 0, F2FS_BLKSIZE);
1387 return 0;
1388}
1389
1390static int f2fs_ioc_abort_volatile_write(struct file *filp)
1391{
1392 struct inode *inode = file_inode(filp);
1393 int ret;
1394
1395 if (!inode_owner_or_capable(inode))
1396 return -EACCES;
1397
1398 ret = mnt_want_write_file(filp);
1399 if (ret)
1400 return ret;
1401
1402 f2fs_balance_fs(F2FS_I_SB(inode));
1403
1404 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001405 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -07001406 commit_inmem_pages(inode, false);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001407 }
1408
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001409 if (f2fs_is_volatile_file(inode))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001410 clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
Jaegeuk Kimde6a8ec2015-06-08 17:51:10 -07001411
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001412 mnt_drop_write_file(filp);
1413 return ret;
1414}
1415
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001416static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1417{
1418 struct inode *inode = file_inode(filp);
1419 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1420 struct super_block *sb = sbi->sb;
1421 __u32 in;
1422
1423 if (!capable(CAP_SYS_ADMIN))
1424 return -EPERM;
1425
1426 if (get_user(in, (__u32 __user *)arg))
1427 return -EFAULT;
1428
1429 switch (in) {
1430 case F2FS_GOING_DOWN_FULLSYNC:
1431 sb = freeze_bdev(sb->s_bdev);
1432 if (sb && !IS_ERR(sb)) {
1433 f2fs_stop_checkpoint(sbi);
1434 thaw_bdev(sb->s_bdev, sb);
1435 }
1436 break;
1437 case F2FS_GOING_DOWN_METASYNC:
1438 /* do checkpoint only */
1439 f2fs_sync_fs(sb, 1);
1440 f2fs_stop_checkpoint(sbi);
1441 break;
1442 case F2FS_GOING_DOWN_NOSYNC:
1443 f2fs_stop_checkpoint(sbi);
1444 break;
1445 default:
1446 return -EINVAL;
1447 }
1448 return 0;
1449}
1450
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001451static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1452{
1453 struct inode *inode = file_inode(filp);
1454 struct super_block *sb = inode->i_sb;
1455 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1456 struct fstrim_range range;
1457 int ret;
1458
1459 if (!capable(CAP_SYS_ADMIN))
1460 return -EPERM;
1461
1462 if (!blk_queue_discard(q))
1463 return -EOPNOTSUPP;
1464
1465 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1466 sizeof(range)))
1467 return -EFAULT;
1468
1469 range.minlen = max((unsigned int)range.minlen,
1470 q->limits.discard_granularity);
1471 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1472 if (ret < 0)
1473 return ret;
1474
1475 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1476 sizeof(range)))
1477 return -EFAULT;
1478 return 0;
1479}
1480
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001481static bool uuid_is_nonzero(__u8 u[16])
1482{
1483 int i;
1484
1485 for (i = 0; i < 16; i++)
1486 if (u[i])
1487 return true;
1488 return false;
1489}
1490
1491static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1492{
1493#ifdef CONFIG_F2FS_FS_ENCRYPTION
1494 struct f2fs_encryption_policy policy;
1495 struct inode *inode = file_inode(filp);
1496
1497 if (copy_from_user(&policy, (struct f2fs_encryption_policy __user *)arg,
1498 sizeof(policy)))
1499 return -EFAULT;
1500
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001501 return f2fs_process_policy(&policy, inode);
1502#else
1503 return -EOPNOTSUPP;
1504#endif
1505}
1506
1507static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1508{
1509#ifdef CONFIG_F2FS_FS_ENCRYPTION
1510 struct f2fs_encryption_policy policy;
1511 struct inode *inode = file_inode(filp);
1512 int err;
1513
1514 err = f2fs_get_policy(inode, &policy);
1515 if (err)
1516 return err;
1517
1518 if (copy_to_user((struct f2fs_encryption_policy __user *)arg, &policy,
1519 sizeof(policy)))
1520 return -EFAULT;
1521 return 0;
1522#else
1523 return -EOPNOTSUPP;
1524#endif
1525}
1526
1527static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1528{
1529 struct inode *inode = file_inode(filp);
1530 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1531 int err;
1532
1533 if (!f2fs_sb_has_crypto(inode->i_sb))
1534 return -EOPNOTSUPP;
1535
1536 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1537 goto got_it;
1538
1539 err = mnt_want_write_file(filp);
1540 if (err)
1541 return err;
1542
1543 /* update superblock with uuid */
1544 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1545
Chao Yuc5bda1c2015-06-08 13:28:03 +08001546 err = f2fs_commit_super(sbi, false);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001547
1548 mnt_drop_write_file(filp);
1549 if (err) {
1550 /* undo new data */
1551 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1552 return err;
1553 }
1554got_it:
1555 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1556 16))
1557 return -EFAULT;
1558 return 0;
1559}
1560
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001561long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1562{
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001563 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001564 case F2FS_IOC_GETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001565 return f2fs_ioc_getflags(filp, arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +09001566 case F2FS_IOC_SETFLAGS:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001567 return f2fs_ioc_setflags(filp, arg);
Chao Yud49f3e82015-01-23 20:36:04 +08001568 case F2FS_IOC_GETVERSION:
1569 return f2fs_ioc_getversion(filp, arg);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001570 case F2FS_IOC_START_ATOMIC_WRITE:
1571 return f2fs_ioc_start_atomic_write(filp);
1572 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1573 return f2fs_ioc_commit_atomic_write(filp);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001574 case F2FS_IOC_START_VOLATILE_WRITE:
1575 return f2fs_ioc_start_volatile_write(filp);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001576 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1577 return f2fs_ioc_release_volatile_write(filp);
1578 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1579 return f2fs_ioc_abort_volatile_write(filp);
Jaegeuk Kim1abff932015-01-08 19:15:53 -08001580 case F2FS_IOC_SHUTDOWN:
1581 return f2fs_ioc_shutdown(filp, arg);
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -07001582 case FITRIM:
Jaegeuk Kim52656e62014-09-24 15:37:02 -07001583 return f2fs_ioc_fitrim(filp, arg);
Jaegeuk Kimf424f662015-04-20 15:19:06 -07001584 case F2FS_IOC_SET_ENCRYPTION_POLICY:
1585 return f2fs_ioc_set_encryption_policy(filp, arg);
1586 case F2FS_IOC_GET_ENCRYPTION_POLICY:
1587 return f2fs_ioc_get_encryption_policy(filp, arg);
1588 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
1589 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001590 default:
1591 return -ENOTTY;
1592 }
1593}
1594
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001595static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1596{
1597 struct inode *inode = file_inode(iocb->ki_filp);
1598
1599 if (f2fs_encrypted_inode(inode) &&
1600 !f2fs_has_encryption_key(inode) &&
1601 f2fs_get_encryption_info(inode))
1602 return -EACCES;
1603
1604 return generic_file_write_iter(iocb, from);
1605}
1606
Namjae Jeone9750822013-02-04 23:41:41 +09001607#ifdef CONFIG_COMPAT
1608long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1609{
1610 switch (cmd) {
1611 case F2FS_IOC32_GETFLAGS:
1612 cmd = F2FS_IOC_GETFLAGS;
1613 break;
1614 case F2FS_IOC32_SETFLAGS:
1615 cmd = F2FS_IOC_SETFLAGS;
1616 break;
1617 default:
1618 return -ENOIOCTLCMD;
1619 }
1620 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1621}
1622#endif
1623
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001624const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +08001625 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04001626 .read_iter = generic_file_read_iter,
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001627 .write_iter = f2fs_file_write_iter,
1628 .open = f2fs_file_open,
Jaegeuk Kim12662232014-12-05 14:37:37 -08001629 .release = f2fs_release_file,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001630 .mmap = f2fs_file_mmap,
1631 .fsync = f2fs_sync_file,
1632 .fallocate = f2fs_fallocate,
1633 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +09001634#ifdef CONFIG_COMPAT
1635 .compat_ioctl = f2fs_compat_ioctl,
1636#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001637 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001638 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09001639};