blob: 2a91a7d1760cae25b2e90c7668b92c7ce6a74931 [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 Kimfbfa2cc2012-11-02 17:09:44 +090044 /* block allocation */
Gu Zhenge4795562013-09-27 18:08:30 +080045 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090046 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +080047 err = f2fs_reserve_block(&dn, page->index);
Gu Zhenge4795562013-09-27 18:08:30 +080048 f2fs_unlock_op(sbi);
Huajun Lib6009652013-11-10 23:13:18 +080049 if (err)
50 goto out;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090051
Namjae Jeon9851e6e2013-04-28 09:04:18 +090052 file_update_time(vma->vm_file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090053 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090054 if (unlikely(page->mapping != inode->i_mapping ||
Namjae Jeon9851e6e2013-04-28 09:04:18 +090055 page_offset(page) > i_size_read(inode) ||
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090056 !PageUptodate(page))) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090057 unlock_page(page);
58 err = -EFAULT;
59 goto out;
60 }
61
62 /*
63 * check to see if the page is mapped already (no holes)
64 */
65 if (PageMappedToDisk(page))
Namjae Jeon9851e6e2013-04-28 09:04:18 +090066 goto mapped;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090067
68 /* page is wholly or partially inside EOF */
69 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
70 unsigned offset;
71 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
72 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
73 }
74 set_page_dirty(page);
75 SetPageUptodate(page);
76
Jaegeuk Kime943a102013-10-25 14:26:31 +090077 trace_f2fs_vm_page_mkwrite(page, DATA);
Namjae Jeon9851e6e2013-04-28 09:04:18 +090078mapped:
79 /* fill the page */
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +090080 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090081out:
82 sb_end_pagefault(inode->i_sb);
83 return block_page_mkwrite_return(err);
84}
85
86static const struct vm_operations_struct f2fs_file_vm_ops = {
Jaegeuk Kim692bb552013-01-17 18:37:41 +090087 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070088 .map_pages = filemap_map_pages,
Jaegeuk Kim692bb552013-01-17 18:37:41 +090089 .page_mkwrite = f2fs_vm_page_mkwrite,
90 .remap_pages = generic_file_remap_pages,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090091};
92
Jaegeuk Kim354a3392013-06-14 08:52:35 +090093static int get_parent_ino(struct inode *inode, nid_t *pino)
94{
95 struct dentry *dentry;
96
97 inode = igrab(inode);
98 dentry = d_find_any_alias(inode);
99 iput(inode);
100 if (!dentry)
101 return 0;
102
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900103 if (update_dent_inode(inode, &dentry->d_name)) {
104 dput(dentry);
105 return 0;
106 }
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900107
Jaegeuk Kimf0947e52013-07-22 22:12:56 +0900108 *pino = parent_ino(dentry);
109 dput(dentry);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900110 return 1;
111}
112
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900113int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
114{
115 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900116 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900117 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900118 int ret = 0;
119 bool need_cp = false;
120 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900121 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900122 .nr_to_write = LONG_MAX,
123 .for_reclaim = 0,
124 };
125
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900126 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900127 return 0;
128
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900129 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700130
131 /* if fdatasync is triggered, let's do in-place-update */
132 if (datasync)
133 set_inode_flag(fi, FI_NEED_IPU);
134
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900135 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700136 if (datasync)
137 clear_inode_flag(fi, FI_NEED_IPU);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900138 if (ret) {
139 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900140 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900141 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900142
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700143 /*
144 * if there is no written data, don't waste time to write recovery info.
145 */
146 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
147 !exist_written_data(sbi, inode->i_ino, APPEND_INO)) {
148 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
149 exist_written_data(sbi, inode->i_ino, UPDATE_INO))
150 goto flush_out;
151 goto out;
152 }
153
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900154 /* guarantee free sections for fsync */
155 f2fs_balance_fs(sbi);
156
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900157 down_read(&fi->i_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900158
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900159 /*
160 * Both of fdatasync() and fsync() are able to be recovered from
161 * sudden-power-off.
162 */
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900163 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
164 need_cp = true;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900165 else if (file_wrong_pino(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900166 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900167 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900168 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900169 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900170 need_cp = true;
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900171 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900172 need_cp = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900173
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900174 up_read(&fi->i_sem);
175
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900176 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900177 nid_t pino;
178
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900179 /* all the dirty node pages should be flushed for POR */
180 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900181
182 down_write(&fi->i_sem);
183 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900184 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
185 get_parent_ino(inode, &pino)) {
186 F2FS_I(inode)->i_pino = pino;
187 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900188 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900189 mark_inode_dirty_sync(inode);
190 ret = f2fs_write_inode(inode, NULL);
191 if (ret)
192 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900193 } else {
194 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900195 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900196 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900197 /* if there is no written node page, write its inode page */
198 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900199 if (fsync_mark_done(sbi, inode->i_ino))
200 goto out;
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900201 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900202 ret = f2fs_write_inode(inode, NULL);
203 if (ret)
204 goto out;
205 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900206 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
207 if (ret)
208 goto out;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700209
210 /* once recovery info is written, don't need to tack this */
211 remove_dirty_inode(sbi, inode->i_ino, APPEND_INO);
212 clear_inode_flag(fi, FI_APPEND_WRITE);
213flush_out:
214 remove_dirty_inode(sbi, inode->i_ino, UPDATE_INO);
215 clear_inode_flag(fi, FI_UPDATE_WRITE);
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900216 ret = f2fs_issue_flush(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900217 }
218out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900219 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900220 return ret;
221}
222
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900223static pgoff_t __get_first_dirty_index(struct address_space *mapping,
224 pgoff_t pgofs, int whence)
225{
226 struct pagevec pvec;
227 int nr_pages;
228
229 if (whence != SEEK_DATA)
230 return 0;
231
232 /* find first dirty page index */
233 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700234 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
235 PAGECACHE_TAG_DIRTY, 1);
236 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900237 pagevec_release(&pvec);
238 return pgofs;
239}
240
241static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
242 int whence)
243{
244 switch (whence) {
245 case SEEK_DATA:
246 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
247 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
248 return true;
249 break;
250 case SEEK_HOLE:
251 if (blkaddr == NULL_ADDR)
252 return true;
253 break;
254 }
255 return false;
256}
257
Chao Yu267378d2014-04-23 14:10:24 +0800258static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
259{
260 struct inode *inode = file->f_mapping->host;
261 loff_t maxbytes = inode->i_sb->s_maxbytes;
262 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900263 pgoff_t pgofs, end_offset, dirty;
264 loff_t data_ofs = offset;
265 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800266 int err = 0;
267
268 mutex_lock(&inode->i_mutex);
269
270 isize = i_size_read(inode);
271 if (offset >= isize)
272 goto fail;
273
274 /* handle inline data case */
275 if (f2fs_has_inline_data(inode)) {
276 if (whence == SEEK_HOLE)
277 data_ofs = isize;
278 goto found;
279 }
280
281 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
282
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900283 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
284
Chao Yu267378d2014-04-23 14:10:24 +0800285 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
286 set_new_dnode(&dn, inode, NULL, NULL, 0);
287 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
288 if (err && err != -ENOENT) {
289 goto fail;
290 } else if (err == -ENOENT) {
291 /* direct node is not exist */
292 if (whence == SEEK_DATA) {
293 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
294 F2FS_I(inode));
295 continue;
296 } else {
297 goto found;
298 }
299 }
300
Chao Yu6b2920a2014-07-07 11:21:59 +0800301 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800302
303 /* find data/hole in dnode block */
304 for (; dn.ofs_in_node < end_offset;
305 dn.ofs_in_node++, pgofs++,
306 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
307 block_t blkaddr;
308 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
309
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900310 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800311 f2fs_put_dnode(&dn);
312 goto found;
313 }
314 }
315 f2fs_put_dnode(&dn);
316 }
317
318 if (whence == SEEK_DATA)
319 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800320found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900321 if (whence == SEEK_HOLE && data_ofs > isize)
322 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800323 mutex_unlock(&inode->i_mutex);
324 return vfs_setpos(file, data_ofs, maxbytes);
325fail:
326 mutex_unlock(&inode->i_mutex);
327 return -ENXIO;
328}
329
330static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
331{
332 struct inode *inode = file->f_mapping->host;
333 loff_t maxbytes = inode->i_sb->s_maxbytes;
334
335 switch (whence) {
336 case SEEK_SET:
337 case SEEK_CUR:
338 case SEEK_END:
339 return generic_file_llseek_size(file, offset, whence,
340 maxbytes, i_size_read(inode));
341 case SEEK_DATA:
342 case SEEK_HOLE:
343 return f2fs_seek_block(file, offset, whence);
344 }
345
346 return -EINVAL;
347}
348
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900349static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
350{
351 file_accessed(file);
352 vma->vm_ops = &f2fs_file_vm_ops;
353 return 0;
354}
355
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900356int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900357{
358 int nr_free = 0, ofs = dn->ofs_in_node;
359 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
360 struct f2fs_node *raw_node;
361 __le32 *addr;
362
Gu Zheng45590712013-07-15 17:57:38 +0800363 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900364 addr = blkaddr_in_node(raw_node) + ofs;
365
Chris Fries6c311ec2014-01-17 14:44:39 -0600366 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900367 block_t blkaddr = le32_to_cpu(*addr);
368 if (blkaddr == NULL_ADDR)
369 continue;
370
371 update_extent_cache(NULL_ADDR, dn);
372 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900373 nr_free++;
374 }
375 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900376 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900377 set_page_dirty(dn->node_page);
378 sync_inode_page(dn);
379 }
380 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900381
382 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
383 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900384 return nr_free;
385}
386
387void truncate_data_blocks(struct dnode_of_data *dn)
388{
389 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
390}
391
392static void truncate_partial_data_page(struct inode *inode, u64 from)
393{
394 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
395 struct page *page;
396
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800397 if (f2fs_has_inline_data(inode))
398 return truncate_inline_data(inode, from);
399
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900400 if (!offset)
401 return;
402
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900403 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900404 if (IS_ERR(page))
405 return;
406
407 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800408 if (unlikely(!PageUptodate(page) ||
409 page->mapping != inode->i_mapping))
410 goto out;
411
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900412 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900413 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
414 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800415
416out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900417 f2fs_put_page(page, 1);
418}
419
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900420int truncate_blocks(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900421{
422 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
423 unsigned int blocksize = inode->i_sb->s_blocksize;
424 struct dnode_of_data dn;
425 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800426 int count = 0, err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900427
Namjae Jeon51dd6242013-04-20 01:28:52 +0900428 trace_f2fs_truncate_blocks_enter(inode, from);
429
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900430 if (f2fs_has_inline_data(inode))
431 goto done;
432
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900433 free_from = (pgoff_t)
434 ((from + blocksize - 1) >> (sbi->log_blocksize));
435
Gu Zhenge4795562013-09-27 18:08:30 +0800436 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800437
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900438 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900439 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900440 if (err) {
441 if (err == -ENOENT)
442 goto free_next;
Gu Zhenge4795562013-09-27 18:08:30 +0800443 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900444 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900445 return err;
446 }
447
Chao Yu6403eb12014-04-26 19:59:52 +0800448 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900449
450 count -= dn.ofs_in_node;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900451 f2fs_bug_on(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900452
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900453 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
454 truncate_data_blocks_range(&dn, count);
455 free_from += count;
456 }
457
458 f2fs_put_dnode(&dn);
459free_next:
460 err = truncate_inode_blocks(inode, free_from);
Gu Zhenge4795562013-09-27 18:08:30 +0800461 f2fs_unlock_op(sbi);
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900462done:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900463 /* lastly zero out the first data page */
464 truncate_partial_data_page(inode, from);
465
Namjae Jeon51dd6242013-04-20 01:28:52 +0900466 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900467 return err;
468}
469
470void f2fs_truncate(struct inode *inode)
471{
472 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
473 S_ISLNK(inode->i_mode)))
474 return;
475
Namjae Jeon51dd6242013-04-20 01:28:52 +0900476 trace_f2fs_truncate(inode);
477
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900478 if (!truncate_blocks(inode, i_size_read(inode))) {
479 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
480 mark_inode_dirty(inode);
481 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900482}
483
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900484int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900485 struct dentry *dentry, struct kstat *stat)
486{
487 struct inode *inode = dentry->d_inode;
488 generic_fillattr(inode, stat);
489 stat->blocks <<= 3;
490 return 0;
491}
492
493#ifdef CONFIG_F2FS_FS_POSIX_ACL
494static void __setattr_copy(struct inode *inode, const struct iattr *attr)
495{
496 struct f2fs_inode_info *fi = F2FS_I(inode);
497 unsigned int ia_valid = attr->ia_valid;
498
499 if (ia_valid & ATTR_UID)
500 inode->i_uid = attr->ia_uid;
501 if (ia_valid & ATTR_GID)
502 inode->i_gid = attr->ia_gid;
503 if (ia_valid & ATTR_ATIME)
504 inode->i_atime = timespec_trunc(attr->ia_atime,
505 inode->i_sb->s_time_gran);
506 if (ia_valid & ATTR_MTIME)
507 inode->i_mtime = timespec_trunc(attr->ia_mtime,
508 inode->i_sb->s_time_gran);
509 if (ia_valid & ATTR_CTIME)
510 inode->i_ctime = timespec_trunc(attr->ia_ctime,
511 inode->i_sb->s_time_gran);
512 if (ia_valid & ATTR_MODE) {
513 umode_t mode = attr->ia_mode;
514
515 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
516 mode &= ~S_ISGID;
517 set_acl_inode(fi, mode);
518 }
519}
520#else
521#define __setattr_copy setattr_copy
522#endif
523
524int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
525{
526 struct inode *inode = dentry->d_inode;
527 struct f2fs_inode_info *fi = F2FS_I(inode);
528 int err;
529
530 err = inode_change_ok(inode, attr);
531 if (err)
532 return err;
533
534 if ((attr->ia_valid & ATTR_SIZE) &&
535 attr->ia_size != i_size_read(inode)) {
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900536 err = f2fs_convert_inline_data(inode, attr->ia_size);
537 if (err)
538 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800539
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900540 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900541 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900542 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900543 }
544
545 __setattr_copy(inode, attr);
546
547 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800548 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900549 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
550 inode->i_mode = fi->i_acl_mode;
551 clear_inode_flag(fi, FI_ACL_MODE);
552 }
553 }
554
555 mark_inode_dirty(inode);
556 return err;
557}
558
559const struct inode_operations f2fs_file_inode_operations = {
560 .getattr = f2fs_getattr,
561 .setattr = f2fs_setattr,
562 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800563 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900564#ifdef CONFIG_F2FS_FS_XATTR
565 .setxattr = generic_setxattr,
566 .getxattr = generic_getxattr,
567 .listxattr = f2fs_listxattr,
568 .removexattr = generic_removexattr,
569#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900570 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900571};
572
573static void fill_zero(struct inode *inode, pgoff_t index,
574 loff_t start, loff_t len)
575{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900576 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900577 struct page *page;
578
579 if (!len)
580 return;
581
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900582 f2fs_balance_fs(sbi);
583
Gu Zhenge4795562013-09-27 18:08:30 +0800584 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900585 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800586 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900587
588 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900589 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900590 zero_user(page, start, len);
591 set_page_dirty(page);
592 f2fs_put_page(page, 1);
593 }
594}
595
596int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
597{
598 pgoff_t index;
599 int err;
600
601 for (index = pg_start; index < pg_end; index++) {
602 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900603
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900604 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900605 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900606 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900607 if (err == -ENOENT)
608 continue;
609 return err;
610 }
611
612 if (dn.data_blkaddr != NULL_ADDR)
613 truncate_data_blocks_range(&dn, 1);
614 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900615 }
616 return 0;
617}
618
Chao Yua66c7b22013-11-22 16:52:50 +0800619static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620{
621 pgoff_t pg_start, pg_end;
622 loff_t off_start, off_end;
623 int ret = 0;
624
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900625 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1);
Jaegeuk Kim8230a0a2013-12-27 11:13:21 +0900626 if (ret)
627 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800628
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900629 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
630 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
631
632 off_start = offset & (PAGE_CACHE_SIZE - 1);
633 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
634
635 if (pg_start == pg_end) {
636 fill_zero(inode, pg_start, off_start,
637 off_end - off_start);
638 } else {
639 if (off_start)
640 fill_zero(inode, pg_start++, off_start,
641 PAGE_CACHE_SIZE - off_start);
642 if (off_end)
643 fill_zero(inode, pg_end, 0, off_end);
644
645 if (pg_start < pg_end) {
646 struct address_space *mapping = inode->i_mapping;
647 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500648 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
649
650 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900651
652 blk_start = pg_start << PAGE_CACHE_SHIFT;
653 blk_end = pg_end << PAGE_CACHE_SHIFT;
654 truncate_inode_pages_range(mapping, blk_start,
655 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900656
Gu Zhenge4795562013-09-27 18:08:30 +0800657 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900658 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800659 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900660 }
661 }
662
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900663 return ret;
664}
665
666static int expand_inode_data(struct inode *inode, loff_t offset,
667 loff_t len, int mode)
668{
669 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
670 pgoff_t index, pg_start, pg_end;
671 loff_t new_size = i_size_read(inode);
672 loff_t off_start, off_end;
673 int ret = 0;
674
675 ret = inode_newsize_ok(inode, (len + offset));
676 if (ret)
677 return ret;
678
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900679 ret = f2fs_convert_inline_data(inode, offset + len);
680 if (ret)
681 return ret;
682
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900683 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
684 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
685
686 off_start = offset & (PAGE_CACHE_SIZE - 1);
687 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
688
Jaegeuk Kimead43272014-06-13 13:05:55 +0900689 f2fs_lock_op(sbi);
690
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900691 for (index = pg_start; index <= pg_end; index++) {
692 struct dnode_of_data dn;
693
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900694 if (index == pg_end && !off_end)
695 goto noalloc;
696
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900697 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800698 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800699 if (ret)
700 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900701noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900702 if (pg_start == pg_end)
703 new_size = offset + len;
704 else if (index == pg_start && off_start)
705 new_size = (index + 1) << PAGE_CACHE_SHIFT;
706 else if (index == pg_end)
707 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
708 else
709 new_size += PAGE_CACHE_SIZE;
710 }
711
712 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
713 i_size_read(inode) < new_size) {
714 i_size_write(inode, new_size);
715 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900716 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900717 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900718 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900719
720 return ret;
721}
722
723static long f2fs_fallocate(struct file *file, int mode,
724 loff_t offset, loff_t len)
725{
Al Viro6131ffa2013-02-27 16:59:05 -0500726 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900727 long ret;
728
729 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
730 return -EOPNOTSUPP;
731
Chao Yu3375f692014-01-28 10:29:26 +0800732 mutex_lock(&inode->i_mutex);
733
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900734 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800735 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900736 else
737 ret = expand_inode_data(inode, offset, len, mode);
738
Namjae Jeon3af60a42012-12-30 14:52:37 +0900739 if (!ret) {
740 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
741 mark_inode_dirty(inode);
742 }
Chao Yu3375f692014-01-28 10:29:26 +0800743
744 mutex_unlock(&inode->i_mutex);
745
Namjae Jeonc01e2852013-04-23 17:00:52 +0900746 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900747 return ret;
748}
749
750#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
751#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
752
753static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
754{
755 if (S_ISDIR(mode))
756 return flags;
757 else if (S_ISREG(mode))
758 return flags & F2FS_REG_FLMASK;
759 else
760 return flags & F2FS_OTHER_FLMASK;
761}
762
763long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
764{
Al Viro6131ffa2013-02-27 16:59:05 -0500765 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900766 struct f2fs_inode_info *fi = F2FS_I(inode);
767 unsigned int flags;
768 int ret;
769
770 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900771 case F2FS_IOC_GETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900772 flags = fi->i_flags & FS_FL_USER_VISIBLE;
773 return put_user(flags, (int __user *) arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900774 case F2FS_IOC_SETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900775 {
776 unsigned int oldflags;
777
Al Virobdaec332013-03-20 09:33:23 -0400778 ret = mnt_want_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900779 if (ret)
780 return ret;
781
782 if (!inode_owner_or_capable(inode)) {
783 ret = -EACCES;
784 goto out;
785 }
786
787 if (get_user(flags, (int __user *) arg)) {
788 ret = -EFAULT;
789 goto out;
790 }
791
792 flags = f2fs_mask_flags(inode->i_mode, flags);
793
794 mutex_lock(&inode->i_mutex);
795
796 oldflags = fi->i_flags;
797
798 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
799 if (!capable(CAP_LINUX_IMMUTABLE)) {
800 mutex_unlock(&inode->i_mutex);
801 ret = -EPERM;
802 goto out;
803 }
804 }
805
806 flags = flags & FS_FL_USER_MODIFIABLE;
807 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
808 fi->i_flags = flags;
809 mutex_unlock(&inode->i_mutex);
810
811 f2fs_set_inode_flags(inode);
812 inode->i_ctime = CURRENT_TIME;
813 mark_inode_dirty(inode);
814out:
Al Virobdaec332013-03-20 09:33:23 -0400815 mnt_drop_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900816 return ret;
817 }
818 default:
819 return -ENOTTY;
820 }
821}
822
Namjae Jeone9750822013-02-04 23:41:41 +0900823#ifdef CONFIG_COMPAT
824long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
825{
826 switch (cmd) {
827 case F2FS_IOC32_GETFLAGS:
828 cmd = F2FS_IOC_GETFLAGS;
829 break;
830 case F2FS_IOC32_SETFLAGS:
831 cmd = F2FS_IOC_SETFLAGS;
832 break;
833 default:
834 return -ENOIOCTLCMD;
835 }
836 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
837}
838#endif
839
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900840const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +0800841 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400842 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -0400843 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400844 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400845 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900846 .open = generic_file_open,
847 .mmap = f2fs_file_mmap,
848 .fsync = f2fs_sync_file,
849 .fallocate = f2fs_fallocate,
850 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900851#ifdef CONFIG_COMPAT
852 .compat_ioctl = f2fs_compat_ioctl,
853#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900854 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400855 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900856};