blob: ecbdf6a6381c5de1726ff400a770f393cb4baf8b [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 Kimfbfa2cc2012-11-02 17:09:44 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
27#include "xattr.h"
28#include "acl.h"
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090029#include <trace/events/f2fs.h>
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090030
31static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
32 struct vm_fault *vmf)
33{
34 struct page *page = vmf->page;
Al Viro6131ffa2013-02-27 16:59:05 -050035 struct inode *inode = file_inode(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090036 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090037 struct dnode_of_data dn;
Gu Zhenge4795562013-09-27 18:08:30 +080038 int err;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090039
40 f2fs_balance_fs(sbi);
41
42 sb_start_pagefault(inode->i_sb);
43
Jaegeuk Kimb067ba12014-08-07 16:32:25 -070044 /* force to convert with normal data indices */
45 err = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, page);
46 if (err)
47 goto out;
48
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090049 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080050 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090051 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +080052 err = f2fs_reserve_block(&dn, page->index);
Gu Zhenge4795562013-09-27 18:08:30 +080053 f2fs_unlock_op(sbi);
Huajun Lib6009652013-11-10 23:13:18 +080054 if (err)
55 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090056
Namjae Jeon9851e6e2013-04-28 09:04:18 +090057 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090058 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090059 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090060 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090061 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090062 unlock_page(page);
63 err = -EFAULT;
64 goto out;
65 }
66
67 /*
68 * check to see if the page is mapped already (no holes)
69 */
70 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090071 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090072
73 /* page is wholly or partially inside EOF */
74 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
75 unsigned offset;
76 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
77 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
78 }
79 set_page_dirty(page);
80 SetPageUptodate(page);
81
Jaegeuk Kime943a102013-10-25 14:26:31 +090082 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090083mapped:
84 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090085 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090086out:
87 sb_end_pagefault(inode->i_sb);
88 return block_page_mkwrite_return(err);
89}
90
91static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090092 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070093 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090094 .page_mkwrite = f2fs_vm_page_mkwrite,
95 .remap_pages = generic_file_remap_pages,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090096};
97
Jaegeuk Kim354a3392013-06-14 08:52:35 +090098static int get_parent_ino(struct inode *inode, nid_t *pino)
99{
100 struct dentry *dentry;
101
102 inode = igrab(inode);
103 dentry = d_find_any_alias(inode);
104 iput(inode);
105 if (!dentry)
106 return 0;
107
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900108 if (update_dent_inode(inode, &dentry->d_name)) {
109 dput(dentry);
110 return 0;
111 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900112
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900113 *pino = parent_ino(dentry);
114 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900115 return 1;
116}
117
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900118int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
119{
120 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900121 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900122 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900123 int ret = 0;
124 bool need_cp = false;
125 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900126 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900127 .nr_to_write = LONG_MAX,
128 .for_reclaim = 0,
129 };
130
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900131 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900132 return 0;
133
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900134 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700135
136 /* if fdatasync is triggered, let's do in-place-update */
137 if (datasync)
138 set_inode_flag(fi, FI_NEED_IPU);
139
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900140 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700141 if (datasync)
142 clear_inode_flag(fi, FI_NEED_IPU);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900143 if (ret) {
144 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900145 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900146 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900147
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700148 /*
149 * if there is no written data, don't waste time to write recovery info.
150 */
151 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
152 !exist_written_data(sbi, inode->i_ino, APPEND_INO)) {
153 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
154 exist_written_data(sbi, inode->i_ino, UPDATE_INO))
155 goto flush_out;
156 goto out;
157 }
158
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900159 /* guarantee free sections for fsync */
160 f2fs_balance_fs(sbi);
161
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900162 down_read(&fi->i_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900163
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900164 /*
165 * Both of fdatasync() and fsync() are able to be recovered from
166 * sudden-power-off.
167 */
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900168 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
169 need_cp = true;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900170 else if (file_wrong_pino(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900171 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900172 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900173 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900174 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900175 need_cp = true;
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900176 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900177 need_cp = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900178
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900179 up_read(&fi->i_sem);
180
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900181 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900182 nid_t pino;
183
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900184 /* all the dirty node pages should be flushed for POR */
185 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900186
187 down_write(&fi->i_sem);
188 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900189 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
190 get_parent_ino(inode, &pino)) {
191 F2FS_I(inode)->i_pino = pino;
192 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900193 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900194 mark_inode_dirty_sync(inode);
195 ret = f2fs_write_inode(inode, NULL);
196 if (ret)
197 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900198 } else {
199 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900200 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900201 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900202 /* if there is no written node page, write its inode page */
203 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900204 if (fsync_mark_done(sbi, inode->i_ino))
205 goto out;
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900206 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900207 ret = f2fs_write_inode(inode, NULL);
208 if (ret)
209 goto out;
210 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900211 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
212 if (ret)
213 goto out;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700214
215 /* once recovery info is written, don't need to tack this */
216 remove_dirty_inode(sbi, inode->i_ino, APPEND_INO);
217 clear_inode_flag(fi, FI_APPEND_WRITE);
218flush_out:
219 remove_dirty_inode(sbi, inode->i_ino, UPDATE_INO);
220 clear_inode_flag(fi, FI_UPDATE_WRITE);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900221 ret = f2fs_issue_flush(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900222 }
223out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900224 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900225 return ret;
226}
227
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900228static pgoff_t __get_first_dirty_index(struct address_space *mapping,
229 pgoff_t pgofs, int whence)
230{
231 struct pagevec pvec;
232 int nr_pages;
233
234 if (whence != SEEK_DATA)
235 return 0;
236
237 /* find first dirty page index */
238 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700239 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
240 PAGECACHE_TAG_DIRTY, 1);
241 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900242 pagevec_release(&pvec);
243 return pgofs;
244}
245
246static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
247 int whence)
248{
249 switch (whence) {
250 case SEEK_DATA:
251 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
252 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
253 return true;
254 break;
255 case SEEK_HOLE:
256 if (blkaddr == NULL_ADDR)
257 return true;
258 break;
259 }
260 return false;
261}
262
Chao Yu267378d2014-04-23 14:10:24 +0800263static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
264{
265 struct inode *inode = file->f_mapping->host;
266 loff_t maxbytes = inode->i_sb->s_maxbytes;
267 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900268 pgoff_t pgofs, end_offset, dirty;
269 loff_t data_ofs = offset;
270 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800271 int err = 0;
272
273 mutex_lock(&inode->i_mutex);
274
275 isize = i_size_read(inode);
276 if (offset >= isize)
277 goto fail;
278
279 /* handle inline data case */
280 if (f2fs_has_inline_data(inode)) {
281 if (whence == SEEK_HOLE)
282 data_ofs = isize;
283 goto found;
284 }
285
286 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
287
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900288 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
289
Chao Yu267378d2014-04-23 14:10:24 +0800290 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
291 set_new_dnode(&dn, inode, NULL, NULL, 0);
292 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
293 if (err && err != -ENOENT) {
294 goto fail;
295 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900296 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800297 if (whence == SEEK_DATA) {
298 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
299 F2FS_I(inode));
300 continue;
301 } else {
302 goto found;
303 }
304 }
305
Chao Yu6b2920a2014-07-07 11:21:59 +0800306 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800307
308 /* find data/hole in dnode block */
309 for (; dn.ofs_in_node < end_offset;
310 dn.ofs_in_node++, pgofs++,
311 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
312 block_t blkaddr;
313 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
314
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900315 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800316 f2fs_put_dnode(&dn);
317 goto found;
318 }
319 }
320 f2fs_put_dnode(&dn);
321 }
322
323 if (whence == SEEK_DATA)
324 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800325found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900326 if (whence == SEEK_HOLE && data_ofs > isize)
327 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800328 mutex_unlock(&inode->i_mutex);
329 return vfs_setpos(file, data_ofs, maxbytes);
330fail:
331 mutex_unlock(&inode->i_mutex);
332 return -ENXIO;
333}
334
335static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
336{
337 struct inode *inode = file->f_mapping->host;
338 loff_t maxbytes = inode->i_sb->s_maxbytes;
339
340 switch (whence) {
341 case SEEK_SET:
342 case SEEK_CUR:
343 case SEEK_END:
344 return generic_file_llseek_size(file, offset, whence,
345 maxbytes, i_size_read(inode));
346 case SEEK_DATA:
347 case SEEK_HOLE:
348 return f2fs_seek_block(file, offset, whence);
349 }
350
351 return -EINVAL;
352}
353
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900354static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
355{
356 file_accessed(file);
357 vma->vm_ops = &f2fs_file_vm_ops;
358 return 0;
359}
360
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900361int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900362{
363 int nr_free = 0, ofs = dn->ofs_in_node;
364 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
365 struct f2fs_node *raw_node;
366 __le32 *addr;
367
Gu Zheng45590712013-07-15 17:57:38 +0800368 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900369 addr = blkaddr_in_node(raw_node) + ofs;
370
Chris Fries6c311ec2014-01-17 14:44:39 -0600371 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900372 block_t blkaddr = le32_to_cpu(*addr);
373 if (blkaddr == NULL_ADDR)
374 continue;
375
376 update_extent_cache(NULL_ADDR, dn);
377 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900378 nr_free++;
379 }
380 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900381 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900382 set_page_dirty(dn->node_page);
383 sync_inode_page(dn);
384 }
385 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900386
387 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
388 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900389 return nr_free;
390}
391
392void truncate_data_blocks(struct dnode_of_data *dn)
393{
394 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
395}
396
397static void truncate_partial_data_page(struct inode *inode, u64 from)
398{
399 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
400 struct page *page;
401
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800402 if (f2fs_has_inline_data(inode))
403 return truncate_inline_data(inode, from);
404
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900405 if (!offset)
406 return;
407
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900408 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900409 if (IS_ERR(page))
410 return;
411
412 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800413 if (unlikely(!PageUptodate(page) ||
414 page->mapping != inode->i_mapping))
415 goto out;
416
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900417 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900418 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
419 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800420
421out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900422 f2fs_put_page(page, 1);
423}
424
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900425int truncate_blocks(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900426{
427 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
428 unsigned int blocksize = inode->i_sb->s_blocksize;
429 struct dnode_of_data dn;
430 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800431 int count = 0, err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900432
Namjae Jeon51dd6242013-04-20 01:28:52 +0900433 trace_f2fs_truncate_blocks_enter(inode, from);
434
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900435 if (f2fs_has_inline_data(inode))
436 goto done;
437
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900438 free_from = (pgoff_t)
439 ((from + blocksize - 1) >> (sbi->log_blocksize));
440
Gu Zhenge4795562013-09-27 18:08:30 +0800441 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800442
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900443 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900444 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900445 if (err) {
446 if (err == -ENOENT)
447 goto free_next;
Gu Zhenge4795562013-09-27 18:08:30 +0800448 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900449 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900450 return err;
451 }
452
Chao Yu6403eb12014-04-26 19:59:52 +0800453 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900454
455 count -= dn.ofs_in_node;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900456 f2fs_bug_on(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900457
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900458 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
459 truncate_data_blocks_range(&dn, count);
460 free_from += count;
461 }
462
463 f2fs_put_dnode(&dn);
464free_next:
465 err = truncate_inode_blocks(inode, free_from);
Gu Zhenge4795562013-09-27 18:08:30 +0800466 f2fs_unlock_op(sbi);
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900467done:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900468 /* lastly zero out the first data page */
469 truncate_partial_data_page(inode, from);
470
Namjae Jeon51dd6242013-04-20 01:28:52 +0900471 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900472 return err;
473}
474
475void f2fs_truncate(struct inode *inode)
476{
477 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
478 S_ISLNK(inode->i_mode)))
479 return;
480
Namjae Jeon51dd6242013-04-20 01:28:52 +0900481 trace_f2fs_truncate(inode);
482
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900483 if (!truncate_blocks(inode, i_size_read(inode))) {
484 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
485 mark_inode_dirty(inode);
486 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900487}
488
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900489int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900490 struct dentry *dentry, struct kstat *stat)
491{
492 struct inode *inode = dentry->d_inode;
493 generic_fillattr(inode, stat);
494 stat->blocks <<= 3;
495 return 0;
496}
497
498#ifdef CONFIG_F2FS_FS_POSIX_ACL
499static void __setattr_copy(struct inode *inode, const struct iattr *attr)
500{
501 struct f2fs_inode_info *fi = F2FS_I(inode);
502 unsigned int ia_valid = attr->ia_valid;
503
504 if (ia_valid & ATTR_UID)
505 inode->i_uid = attr->ia_uid;
506 if (ia_valid & ATTR_GID)
507 inode->i_gid = attr->ia_gid;
508 if (ia_valid & ATTR_ATIME)
509 inode->i_atime = timespec_trunc(attr->ia_atime,
510 inode->i_sb->s_time_gran);
511 if (ia_valid & ATTR_MTIME)
512 inode->i_mtime = timespec_trunc(attr->ia_mtime,
513 inode->i_sb->s_time_gran);
514 if (ia_valid & ATTR_CTIME)
515 inode->i_ctime = timespec_trunc(attr->ia_ctime,
516 inode->i_sb->s_time_gran);
517 if (ia_valid & ATTR_MODE) {
518 umode_t mode = attr->ia_mode;
519
520 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
521 mode &= ~S_ISGID;
522 set_acl_inode(fi, mode);
523 }
524}
525#else
526#define __setattr_copy setattr_copy
527#endif
528
529int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
530{
531 struct inode *inode = dentry->d_inode;
532 struct f2fs_inode_info *fi = F2FS_I(inode);
533 int err;
534
535 err = inode_change_ok(inode, attr);
536 if (err)
537 return err;
538
539 if ((attr->ia_valid & ATTR_SIZE) &&
540 attr->ia_size != i_size_read(inode)) {
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700541 err = f2fs_convert_inline_data(inode, attr->ia_size, NULL);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900542 if (err)
543 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800544
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900546 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900547 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900548 }
549
550 __setattr_copy(inode, attr);
551
552 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800553 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900554 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
555 inode->i_mode = fi->i_acl_mode;
556 clear_inode_flag(fi, FI_ACL_MODE);
557 }
558 }
559
560 mark_inode_dirty(inode);
561 return err;
562}
563
564const struct inode_operations f2fs_file_inode_operations = {
565 .getattr = f2fs_getattr,
566 .setattr = f2fs_setattr,
567 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800568 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900569#ifdef CONFIG_F2FS_FS_XATTR
570 .setxattr = generic_setxattr,
571 .getxattr = generic_getxattr,
572 .listxattr = f2fs_listxattr,
573 .removexattr = generic_removexattr,
574#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900575 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900576};
577
578static void fill_zero(struct inode *inode, pgoff_t index,
579 loff_t start, loff_t len)
580{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900581 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900582 struct page *page;
583
584 if (!len)
585 return;
586
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900587 f2fs_balance_fs(sbi);
588
Gu Zhenge4795562013-09-27 18:08:30 +0800589 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900590 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800591 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900592
593 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900594 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900595 zero_user(page, start, len);
596 set_page_dirty(page);
597 f2fs_put_page(page, 1);
598 }
599}
600
601int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
602{
603 pgoff_t index;
604 int err;
605
606 for (index = pg_start; index < pg_end; index++) {
607 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900608
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900609 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900610 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900611 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900612 if (err == -ENOENT)
613 continue;
614 return err;
615 }
616
617 if (dn.data_blkaddr != NULL_ADDR)
618 truncate_data_blocks_range(&dn, 1);
619 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620 }
621 return 0;
622}
623
Chao Yua66c7b22013-11-22 16:52:50 +0800624static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900625{
626 pgoff_t pg_start, pg_end;
627 loff_t off_start, off_end;
628 int ret = 0;
629
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700630 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, NULL);
Jaegeuk Kim8230a0a2013-12-27 11:13:21 +0900631 if (ret)
632 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800633
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900634 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
635 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
636
637 off_start = offset & (PAGE_CACHE_SIZE - 1);
638 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
639
640 if (pg_start == pg_end) {
641 fill_zero(inode, pg_start, off_start,
642 off_end - off_start);
643 } else {
644 if (off_start)
645 fill_zero(inode, pg_start++, off_start,
646 PAGE_CACHE_SIZE - off_start);
647 if (off_end)
648 fill_zero(inode, pg_end, 0, off_end);
649
650 if (pg_start < pg_end) {
651 struct address_space *mapping = inode->i_mapping;
652 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500653 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
654
655 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900656
657 blk_start = pg_start << PAGE_CACHE_SHIFT;
658 blk_end = pg_end << PAGE_CACHE_SHIFT;
659 truncate_inode_pages_range(mapping, blk_start,
660 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900661
Gu Zhenge4795562013-09-27 18:08:30 +0800662 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900663 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800664 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665 }
666 }
667
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900668 return ret;
669}
670
671static int expand_inode_data(struct inode *inode, loff_t offset,
672 loff_t len, int mode)
673{
674 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
675 pgoff_t index, pg_start, pg_end;
676 loff_t new_size = i_size_read(inode);
677 loff_t off_start, off_end;
678 int ret = 0;
679
Chao Yu497a0932014-08-04 10:11:17 +0800680 f2fs_balance_fs(sbi);
681
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900682 ret = inode_newsize_ok(inode, (len + offset));
683 if (ret)
684 return ret;
685
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700686 ret = f2fs_convert_inline_data(inode, offset + len, NULL);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900687 if (ret)
688 return ret;
689
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900690 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
691 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
692
693 off_start = offset & (PAGE_CACHE_SIZE - 1);
694 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
695
Jaegeuk Kimead43272014-06-13 13:05:55 +0900696 f2fs_lock_op(sbi);
697
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900698 for (index = pg_start; index <= pg_end; index++) {
699 struct dnode_of_data dn;
700
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900701 if (index == pg_end && !off_end)
702 goto noalloc;
703
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900704 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800705 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800706 if (ret)
707 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900708noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900709 if (pg_start == pg_end)
710 new_size = offset + len;
711 else if (index == pg_start && off_start)
712 new_size = (index + 1) << PAGE_CACHE_SHIFT;
713 else if (index == pg_end)
714 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
715 else
716 new_size += PAGE_CACHE_SIZE;
717 }
718
719 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
720 i_size_read(inode) < new_size) {
721 i_size_write(inode, new_size);
722 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900723 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900724 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900725 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900726
727 return ret;
728}
729
730static long f2fs_fallocate(struct file *file, int mode,
731 loff_t offset, loff_t len)
732{
Al Viro6131ffa2013-02-27 16:59:05 -0500733 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900734 long ret;
735
736 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
737 return -EOPNOTSUPP;
738
Chao Yu3375f692014-01-28 10:29:26 +0800739 mutex_lock(&inode->i_mutex);
740
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900741 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800742 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900743 else
744 ret = expand_inode_data(inode, offset, len, mode);
745
Namjae Jeon3af60a42012-12-30 14:52:37 +0900746 if (!ret) {
747 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
748 mark_inode_dirty(inode);
749 }
Chao Yu3375f692014-01-28 10:29:26 +0800750
751 mutex_unlock(&inode->i_mutex);
752
Namjae Jeonc01e2852013-04-23 17:00:52 +0900753 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 return ret;
755}
756
757#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
758#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
759
760static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
761{
762 if (S_ISDIR(mode))
763 return flags;
764 else if (S_ISREG(mode))
765 return flags & F2FS_REG_FLMASK;
766 else
767 return flags & F2FS_OTHER_FLMASK;
768}
769
770long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
771{
Al Viro6131ffa2013-02-27 16:59:05 -0500772 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900773 struct f2fs_inode_info *fi = F2FS_I(inode);
774 unsigned int flags;
775 int ret;
776
777 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900778 case F2FS_IOC_GETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900779 flags = fi->i_flags & FS_FL_USER_VISIBLE;
780 return put_user(flags, (int __user *) arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900781 case F2FS_IOC_SETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900782 {
783 unsigned int oldflags;
784
Al Virobdaec332013-03-20 09:33:23 -0400785 ret = mnt_want_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900786 if (ret)
787 return ret;
788
789 if (!inode_owner_or_capable(inode)) {
790 ret = -EACCES;
791 goto out;
792 }
793
794 if (get_user(flags, (int __user *) arg)) {
795 ret = -EFAULT;
796 goto out;
797 }
798
799 flags = f2fs_mask_flags(inode->i_mode, flags);
800
801 mutex_lock(&inode->i_mutex);
802
803 oldflags = fi->i_flags;
804
805 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
806 if (!capable(CAP_LINUX_IMMUTABLE)) {
807 mutex_unlock(&inode->i_mutex);
808 ret = -EPERM;
809 goto out;
810 }
811 }
812
813 flags = flags & FS_FL_USER_MODIFIABLE;
814 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
815 fi->i_flags = flags;
816 mutex_unlock(&inode->i_mutex);
817
818 f2fs_set_inode_flags(inode);
819 inode->i_ctime = CURRENT_TIME;
820 mark_inode_dirty(inode);
821out:
Al Virobdaec332013-03-20 09:33:23 -0400822 mnt_drop_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900823 return ret;
824 }
825 default:
826 return -ENOTTY;
827 }
828}
829
Namjae Jeone9750822013-02-04 23:41:41 +0900830#ifdef CONFIG_COMPAT
831long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
832{
833 switch (cmd) {
834 case F2FS_IOC32_GETFLAGS:
835 cmd = F2FS_IOC_GETFLAGS;
836 break;
837 case F2FS_IOC32_SETFLAGS:
838 cmd = F2FS_IOC_SETFLAGS;
839 break;
840 default:
841 return -ENOIOCTLCMD;
842 }
843 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
844}
845#endif
846
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900847const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +0800848 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400849 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -0400850 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400851 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400852 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900853 .open = generic_file_open,
854 .mmap = f2fs_file_mmap,
855 .fsync = f2fs_sync_file,
856 .fallocate = f2fs_fallocate,
857 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900858#ifdef CONFIG_COMPAT
859 .compat_ioctl = f2fs_compat_ioctl,
860#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900861 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400862 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900863};