blob: 9f0ea3d3251be88c88f5e385e6e7ac7aa81251ac [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 Kim40813632014-09-02 15:31:18 -070036 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
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
Chao Yu9d1589e2014-08-20 18:37:35 +0800118static inline bool need_do_checkpoint(struct inode *inode)
119{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700120 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu9d1589e2014-08-20 18:37:35 +0800121 bool need_cp = false;
122
123 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
124 need_cp = true;
125 else if (file_wrong_pino(inode))
126 need_cp = true;
127 else if (!space_for_roll_forward(sbi))
128 need_cp = true;
129 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
130 need_cp = true;
131 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
132 need_cp = true;
133
134 return need_cp;
135}
136
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900137int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
138{
139 struct inode *inode = file->f_mapping->host;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900140 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700141 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900142 int ret = 0;
143 bool need_cp = false;
144 struct writeback_control wbc = {
Jaegeuk Kimc81bf1c2014-03-03 11:28:40 +0900145 .sync_mode = WB_SYNC_ALL,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900146 .nr_to_write = LONG_MAX,
147 .for_reclaim = 0,
148 };
149
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900150 if (unlikely(f2fs_readonly(inode->i_sb)))
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900151 return 0;
152
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900153 trace_f2fs_sync_file_enter(inode);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700154
155 /* if fdatasync is triggered, let's do in-place-update */
156 if (datasync)
157 set_inode_flag(fi, FI_NEED_IPU);
158
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900159 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Jaegeuk Kimea1aa122014-07-24 19:11:43 -0700160 if (datasync)
161 clear_inode_flag(fi, FI_NEED_IPU);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900162 if (ret) {
163 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900164 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900165 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900166
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700167 /*
168 * if there is no written data, don't waste time to write recovery info.
169 */
170 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
171 !exist_written_data(sbi, inode->i_ino, APPEND_INO)) {
172 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
173 exist_written_data(sbi, inode->i_ino, UPDATE_INO))
174 goto flush_out;
175 goto out;
176 }
177
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900178 /* guarantee free sections for fsync */
179 f2fs_balance_fs(sbi);
180
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900181 /*
182 * Both of fdatasync() and fsync() are able to be recovered from
183 * sudden-power-off.
184 */
Chao Yu9d1589e2014-08-20 18:37:35 +0800185 down_read(&fi->i_sem);
186 need_cp = need_do_checkpoint(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900187 up_read(&fi->i_sem);
188
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900189 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900190 nid_t pino;
191
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900192 /* all the dirty node pages should be flushed for POR */
193 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900194
195 down_write(&fi->i_sem);
196 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900197 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
198 get_parent_ino(inode, &pino)) {
199 F2FS_I(inode)->i_pino = pino;
200 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900201 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900202 mark_inode_dirty_sync(inode);
203 ret = f2fs_write_inode(inode, NULL);
204 if (ret)
205 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900206 } else {
207 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900208 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900209 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900210 /* if there is no written node page, write its inode page */
211 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900212 if (fsync_mark_done(sbi, inode->i_ino))
213 goto out;
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900214 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900215 ret = f2fs_write_inode(inode, NULL);
216 if (ret)
217 goto out;
218 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900219 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
220 if (ret)
221 goto out;
Jaegeuk Kim6d99ba412014-07-24 19:08:02 -0700222
223 /* once recovery info is written, don't need to tack this */
224 remove_dirty_inode(sbi, inode->i_ino, APPEND_INO);
225 clear_inode_flag(fi, FI_APPEND_WRITE);
226flush_out:
227 remove_dirty_inode(sbi, inode->i_ino, UPDATE_INO);
228 clear_inode_flag(fi, FI_UPDATE_WRITE);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700229 ret = f2fs_issue_flush(F2FS_I_SB(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900230 }
231out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900232 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900233 return ret;
234}
235
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900236static pgoff_t __get_first_dirty_index(struct address_space *mapping,
237 pgoff_t pgofs, int whence)
238{
239 struct pagevec pvec;
240 int nr_pages;
241
242 if (whence != SEEK_DATA)
243 return 0;
244
245 /* find first dirty page index */
246 pagevec_init(&pvec, 0);
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -0700247 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
248 PAGECACHE_TAG_DIRTY, 1);
249 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900250 pagevec_release(&pvec);
251 return pgofs;
252}
253
254static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
255 int whence)
256{
257 switch (whence) {
258 case SEEK_DATA:
259 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
260 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
261 return true;
262 break;
263 case SEEK_HOLE:
264 if (blkaddr == NULL_ADDR)
265 return true;
266 break;
267 }
268 return false;
269}
270
Chao Yu267378d2014-04-23 14:10:24 +0800271static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
272{
273 struct inode *inode = file->f_mapping->host;
274 loff_t maxbytes = inode->i_sb->s_maxbytes;
275 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900276 pgoff_t pgofs, end_offset, dirty;
277 loff_t data_ofs = offset;
278 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800279 int err = 0;
280
281 mutex_lock(&inode->i_mutex);
282
283 isize = i_size_read(inode);
284 if (offset >= isize)
285 goto fail;
286
287 /* handle inline data case */
288 if (f2fs_has_inline_data(inode)) {
289 if (whence == SEEK_HOLE)
290 data_ofs = isize;
291 goto found;
292 }
293
294 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
295
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900296 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
297
Chao Yu267378d2014-04-23 14:10:24 +0800298 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
299 set_new_dnode(&dn, inode, NULL, NULL, 0);
300 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
301 if (err && err != -ENOENT) {
302 goto fail;
303 } else if (err == -ENOENT) {
arter97e1c42042014-08-06 23:22:50 +0900304 /* direct node does not exists */
Chao Yu267378d2014-04-23 14:10:24 +0800305 if (whence == SEEK_DATA) {
306 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
307 F2FS_I(inode));
308 continue;
309 } else {
310 goto found;
311 }
312 }
313
Chao Yu6b2920a2014-07-07 11:21:59 +0800314 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800315
316 /* find data/hole in dnode block */
317 for (; dn.ofs_in_node < end_offset;
318 dn.ofs_in_node++, pgofs++,
319 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
320 block_t blkaddr;
321 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
322
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900323 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800324 f2fs_put_dnode(&dn);
325 goto found;
326 }
327 }
328 f2fs_put_dnode(&dn);
329 }
330
331 if (whence == SEEK_DATA)
332 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800333found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900334 if (whence == SEEK_HOLE && data_ofs > isize)
335 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800336 mutex_unlock(&inode->i_mutex);
337 return vfs_setpos(file, data_ofs, maxbytes);
338fail:
339 mutex_unlock(&inode->i_mutex);
340 return -ENXIO;
341}
342
343static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
344{
345 struct inode *inode = file->f_mapping->host;
346 loff_t maxbytes = inode->i_sb->s_maxbytes;
347
348 switch (whence) {
349 case SEEK_SET:
350 case SEEK_CUR:
351 case SEEK_END:
352 return generic_file_llseek_size(file, offset, whence,
353 maxbytes, i_size_read(inode));
354 case SEEK_DATA:
355 case SEEK_HOLE:
356 return f2fs_seek_block(file, offset, whence);
357 }
358
359 return -EINVAL;
360}
361
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900362static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
363{
364 file_accessed(file);
365 vma->vm_ops = &f2fs_file_vm_ops;
366 return 0;
367}
368
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900369int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900370{
371 int nr_free = 0, ofs = dn->ofs_in_node;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700372 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900373 struct f2fs_node *raw_node;
374 __le32 *addr;
375
Gu Zheng45590712013-07-15 17:57:38 +0800376 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900377 addr = blkaddr_in_node(raw_node) + ofs;
378
Chris Fries6c311ec2014-01-17 14:44:39 -0600379 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900380 block_t blkaddr = le32_to_cpu(*addr);
381 if (blkaddr == NULL_ADDR)
382 continue;
383
384 update_extent_cache(NULL_ADDR, dn);
385 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900386 nr_free++;
387 }
388 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900389 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900390 set_page_dirty(dn->node_page);
391 sync_inode_page(dn);
392 }
393 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900394
395 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
396 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900397 return nr_free;
398}
399
400void truncate_data_blocks(struct dnode_of_data *dn)
401{
402 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
403}
404
405static void truncate_partial_data_page(struct inode *inode, u64 from)
406{
407 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
408 struct page *page;
409
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800410 if (f2fs_has_inline_data(inode))
411 return truncate_inline_data(inode, from);
412
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900413 if (!offset)
414 return;
415
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900416 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900417 if (IS_ERR(page))
418 return;
419
420 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800421 if (unlikely(!PageUptodate(page) ||
422 page->mapping != inode->i_mapping))
423 goto out;
424
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900425 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900426 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
427 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800428
429out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900430 f2fs_put_page(page, 1);
431}
432
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700433int truncate_blocks(struct inode *inode, u64 from, bool lock)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900434{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700435 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900436 unsigned int blocksize = inode->i_sb->s_blocksize;
437 struct dnode_of_data dn;
438 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800439 int count = 0, err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900440
Namjae Jeon51dd6242013-04-20 01:28:52 +0900441 trace_f2fs_truncate_blocks_enter(inode, from);
442
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900443 if (f2fs_has_inline_data(inode))
444 goto done;
445
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900446 free_from = (pgoff_t)
447 ((from + blocksize - 1) >> (sbi->log_blocksize));
448
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700449 if (lock)
450 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800451
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900452 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900453 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900454 if (err) {
455 if (err == -ENOENT)
456 goto free_next;
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700457 if (lock)
458 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900459 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900460 return err;
461 }
462
Chao Yu6403eb12014-04-26 19:59:52 +0800463 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900464
465 count -= dn.ofs_in_node;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700466 f2fs_bug_on(sbi, count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900467
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900468 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
469 truncate_data_blocks_range(&dn, count);
470 free_from += count;
471 }
472
473 f2fs_put_dnode(&dn);
474free_next:
475 err = truncate_inode_blocks(inode, free_from);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700476 if (lock)
477 f2fs_unlock_op(sbi);
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900478done:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900479 /* lastly zero out the first data page */
480 truncate_partial_data_page(inode, from);
481
Namjae Jeon51dd6242013-04-20 01:28:52 +0900482 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900483 return err;
484}
485
486void f2fs_truncate(struct inode *inode)
487{
488 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
489 S_ISLNK(inode->i_mode)))
490 return;
491
Namjae Jeon51dd6242013-04-20 01:28:52 +0900492 trace_f2fs_truncate(inode);
493
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700494 if (!truncate_blocks(inode, i_size_read(inode), true)) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900495 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
496 mark_inode_dirty(inode);
497 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900498}
499
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900500int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900501 struct dentry *dentry, struct kstat *stat)
502{
503 struct inode *inode = dentry->d_inode;
504 generic_fillattr(inode, stat);
505 stat->blocks <<= 3;
506 return 0;
507}
508
509#ifdef CONFIG_F2FS_FS_POSIX_ACL
510static void __setattr_copy(struct inode *inode, const struct iattr *attr)
511{
512 struct f2fs_inode_info *fi = F2FS_I(inode);
513 unsigned int ia_valid = attr->ia_valid;
514
515 if (ia_valid & ATTR_UID)
516 inode->i_uid = attr->ia_uid;
517 if (ia_valid & ATTR_GID)
518 inode->i_gid = attr->ia_gid;
519 if (ia_valid & ATTR_ATIME)
520 inode->i_atime = timespec_trunc(attr->ia_atime,
521 inode->i_sb->s_time_gran);
522 if (ia_valid & ATTR_MTIME)
523 inode->i_mtime = timespec_trunc(attr->ia_mtime,
524 inode->i_sb->s_time_gran);
525 if (ia_valid & ATTR_CTIME)
526 inode->i_ctime = timespec_trunc(attr->ia_ctime,
527 inode->i_sb->s_time_gran);
528 if (ia_valid & ATTR_MODE) {
529 umode_t mode = attr->ia_mode;
530
531 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
532 mode &= ~S_ISGID;
533 set_acl_inode(fi, mode);
534 }
535}
536#else
537#define __setattr_copy setattr_copy
538#endif
539
540int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
541{
542 struct inode *inode = dentry->d_inode;
543 struct f2fs_inode_info *fi = F2FS_I(inode);
544 int err;
545
546 err = inode_change_ok(inode, attr);
547 if (err)
548 return err;
549
550 if ((attr->ia_valid & ATTR_SIZE) &&
551 attr->ia_size != i_size_read(inode)) {
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700552 err = f2fs_convert_inline_data(inode, attr->ia_size, NULL);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900553 if (err)
554 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800555
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900556 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900557 f2fs_truncate(inode);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700558 f2fs_balance_fs(F2FS_I_SB(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900559 }
560
561 __setattr_copy(inode, attr);
562
563 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800564 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900565 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
566 inode->i_mode = fi->i_acl_mode;
567 clear_inode_flag(fi, FI_ACL_MODE);
568 }
569 }
570
571 mark_inode_dirty(inode);
572 return err;
573}
574
575const struct inode_operations f2fs_file_inode_operations = {
576 .getattr = f2fs_getattr,
577 .setattr = f2fs_setattr,
578 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800579 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900580#ifdef CONFIG_F2FS_FS_XATTR
581 .setxattr = generic_setxattr,
582 .getxattr = generic_getxattr,
583 .listxattr = f2fs_listxattr,
584 .removexattr = generic_removexattr,
585#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900586 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900587};
588
589static void fill_zero(struct inode *inode, pgoff_t index,
590 loff_t start, loff_t len)
591{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700592 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900593 struct page *page;
594
595 if (!len)
596 return;
597
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900598 f2fs_balance_fs(sbi);
599
Gu Zhenge4795562013-09-27 18:08:30 +0800600 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900601 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800602 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900603
604 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900605 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900606 zero_user(page, start, len);
607 set_page_dirty(page);
608 f2fs_put_page(page, 1);
609 }
610}
611
612int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
613{
614 pgoff_t index;
615 int err;
616
617 for (index = pg_start; index < pg_end; index++) {
618 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900619
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900620 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900621 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900622 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900623 if (err == -ENOENT)
624 continue;
625 return err;
626 }
627
628 if (dn.data_blkaddr != NULL_ADDR)
629 truncate_data_blocks_range(&dn, 1);
630 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900631 }
632 return 0;
633}
634
Chao Yua66c7b22013-11-22 16:52:50 +0800635static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900636{
637 pgoff_t pg_start, pg_end;
638 loff_t off_start, off_end;
639 int ret = 0;
640
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700641 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, NULL);
Jaegeuk Kim8230a0a2013-12-27 11:13:21 +0900642 if (ret)
643 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800644
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900645 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
646 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
647
648 off_start = offset & (PAGE_CACHE_SIZE - 1);
649 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
650
651 if (pg_start == pg_end) {
652 fill_zero(inode, pg_start, off_start,
653 off_end - off_start);
654 } else {
655 if (off_start)
656 fill_zero(inode, pg_start++, off_start,
657 PAGE_CACHE_SIZE - off_start);
658 if (off_end)
659 fill_zero(inode, pg_end, 0, off_end);
660
661 if (pg_start < pg_end) {
662 struct address_space *mapping = inode->i_mapping;
663 loff_t blk_start, blk_end;
Jaegeuk Kim40813632014-09-02 15:31:18 -0700664 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500665
666 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900667
668 blk_start = pg_start << PAGE_CACHE_SHIFT;
669 blk_end = pg_end << PAGE_CACHE_SHIFT;
670 truncate_inode_pages_range(mapping, blk_start,
671 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900672
Gu Zhenge4795562013-09-27 18:08:30 +0800673 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900674 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800675 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900676 }
677 }
678
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900679 return ret;
680}
681
682static int expand_inode_data(struct inode *inode, loff_t offset,
683 loff_t len, int mode)
684{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700685 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900686 pgoff_t index, pg_start, pg_end;
687 loff_t new_size = i_size_read(inode);
688 loff_t off_start, off_end;
689 int ret = 0;
690
Chao Yu497a0932014-08-04 10:11:17 +0800691 f2fs_balance_fs(sbi);
692
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900693 ret = inode_newsize_ok(inode, (len + offset));
694 if (ret)
695 return ret;
696
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700697 ret = f2fs_convert_inline_data(inode, offset + len, NULL);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900698 if (ret)
699 return ret;
700
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900701 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
702 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
703
704 off_start = offset & (PAGE_CACHE_SIZE - 1);
705 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
706
Jaegeuk Kimead43272014-06-13 13:05:55 +0900707 f2fs_lock_op(sbi);
708
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900709 for (index = pg_start; index <= pg_end; index++) {
710 struct dnode_of_data dn;
711
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900712 if (index == pg_end && !off_end)
713 goto noalloc;
714
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900715 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800716 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800717 if (ret)
718 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900719noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900720 if (pg_start == pg_end)
721 new_size = offset + len;
722 else if (index == pg_start && off_start)
723 new_size = (index + 1) << PAGE_CACHE_SHIFT;
724 else if (index == pg_end)
725 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
726 else
727 new_size += PAGE_CACHE_SIZE;
728 }
729
730 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
731 i_size_read(inode) < new_size) {
732 i_size_write(inode, new_size);
733 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900734 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900735 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900736 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900737
738 return ret;
739}
740
741static long f2fs_fallocate(struct file *file, int mode,
742 loff_t offset, loff_t len)
743{
Al Viro6131ffa2013-02-27 16:59:05 -0500744 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900745 long ret;
746
747 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
748 return -EOPNOTSUPP;
749
Chao Yu3375f692014-01-28 10:29:26 +0800750 mutex_lock(&inode->i_mutex);
751
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900752 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800753 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900754 else
755 ret = expand_inode_data(inode, offset, len, mode);
756
Namjae Jeon3af60a42012-12-30 14:52:37 +0900757 if (!ret) {
758 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
759 mark_inode_dirty(inode);
760 }
Chao Yu3375f692014-01-28 10:29:26 +0800761
762 mutex_unlock(&inode->i_mutex);
763
Namjae Jeonc01e2852013-04-23 17:00:52 +0900764 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900765 return ret;
766}
767
768#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
769#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
770
771static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
772{
773 if (S_ISDIR(mode))
774 return flags;
775 else if (S_ISREG(mode))
776 return flags & F2FS_REG_FLMASK;
777 else
778 return flags & F2FS_OTHER_FLMASK;
779}
780
781long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
782{
Al Viro6131ffa2013-02-27 16:59:05 -0500783 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900784 struct f2fs_inode_info *fi = F2FS_I(inode);
785 unsigned int flags;
786 int ret;
787
788 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900789 case F2FS_IOC_GETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900790 flags = fi->i_flags & FS_FL_USER_VISIBLE;
791 return put_user(flags, (int __user *) arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900792 case F2FS_IOC_SETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900793 {
794 unsigned int oldflags;
795
Al Virobdaec332013-03-20 09:33:23 -0400796 ret = mnt_want_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900797 if (ret)
798 return ret;
799
800 if (!inode_owner_or_capable(inode)) {
801 ret = -EACCES;
802 goto out;
803 }
804
805 if (get_user(flags, (int __user *) arg)) {
806 ret = -EFAULT;
807 goto out;
808 }
809
810 flags = f2fs_mask_flags(inode->i_mode, flags);
811
812 mutex_lock(&inode->i_mutex);
813
814 oldflags = fi->i_flags;
815
816 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
817 if (!capable(CAP_LINUX_IMMUTABLE)) {
818 mutex_unlock(&inode->i_mutex);
819 ret = -EPERM;
820 goto out;
821 }
822 }
823
824 flags = flags & FS_FL_USER_MODIFIABLE;
825 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
826 fi->i_flags = flags;
827 mutex_unlock(&inode->i_mutex);
828
829 f2fs_set_inode_flags(inode);
830 inode->i_ctime = CURRENT_TIME;
831 mark_inode_dirty(inode);
832out:
Al Virobdaec332013-03-20 09:33:23 -0400833 mnt_drop_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900834 return ret;
835 }
836 default:
837 return -ENOTTY;
838 }
839}
840
Namjae Jeone9750822013-02-04 23:41:41 +0900841#ifdef CONFIG_COMPAT
842long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
843{
844 switch (cmd) {
845 case F2FS_IOC32_GETFLAGS:
846 cmd = F2FS_IOC_GETFLAGS;
847 break;
848 case F2FS_IOC32_SETFLAGS:
849 cmd = F2FS_IOC_SETFLAGS;
850 break;
851 default:
852 return -ENOIOCTLCMD;
853 }
854 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
855}
856#endif
857
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900858const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +0800859 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400860 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -0400861 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400862 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400863 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900864 .open = generic_file_open,
865 .mmap = f2fs_file_mmap,
866 .fsync = f2fs_sync_file,
867 .fallocate = f2fs_fallocate,
868 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900869#ifdef CONFIG_COMPAT
870 .compat_ioctl = f2fs_compat_ioctl,
871#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900872 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400873 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900874};