blob: 7c652b3e1511f65884ef7be9c8ba0414b1f1bc0c [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 Kimfbfa2cc2012-11-02 17:09:44 +0900130 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900131 if (ret) {
132 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900133 return ret;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900134 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900135
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900136 /* guarantee free sections for fsync */
137 f2fs_balance_fs(sbi);
138
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900139 down_read(&fi->i_sem);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900140
Jaegeuk Kime5d23852013-07-03 10:55:52 +0900141 /*
142 * Both of fdatasync() and fsync() are able to be recovered from
143 * sudden-power-off.
144 */
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900145 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
146 need_cp = true;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900147 else if (file_wrong_pino(inode))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900148 need_cp = true;
Changman Leefacb0202013-01-31 17:16:56 +0900149 else if (!space_for_roll_forward(sbi))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900150 need_cp = true;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900151 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900152 need_cp = true;
Jaegeuk Kimd71b5562013-08-09 15:03:21 +0900153 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900154 need_cp = true;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900155
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900156 up_read(&fi->i_sem);
157
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900158 if (need_cp) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900159 nid_t pino;
160
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900161 /* all the dirty node pages should be flushed for POR */
162 ret = f2fs_sync_fs(inode->i_sb, 1);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900163
164 down_write(&fi->i_sem);
165 F2FS_I(inode)->xattr_ver = 0;
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900166 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
167 get_parent_ino(inode, &pino)) {
168 F2FS_I(inode)->i_pino = pino;
169 file_got_pino(inode);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900170 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900171 mark_inode_dirty_sync(inode);
172 ret = f2fs_write_inode(inode, NULL);
173 if (ret)
174 goto out;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900175 } else {
176 up_write(&fi->i_sem);
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900177 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900178 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900179 /* if there is no written node page, write its inode page */
180 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900181 if (fsync_mark_done(sbi, inode->i_ino))
182 goto out;
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900183 mark_inode_dirty_sync(inode);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900184 ret = f2fs_write_inode(inode, NULL);
185 if (ret)
186 goto out;
187 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +0900188 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
189 if (ret)
190 goto out;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900191 ret = f2fs_issue_flush(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900192 }
193out:
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900194 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900195 return ret;
196}
197
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900198static pgoff_t __get_first_dirty_index(struct address_space *mapping,
199 pgoff_t pgofs, int whence)
200{
201 struct pagevec pvec;
202 int nr_pages;
203
204 if (whence != SEEK_DATA)
205 return 0;
206
207 /* find first dirty page index */
208 pagevec_init(&pvec, 0);
209 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, PAGECACHE_TAG_DIRTY, 1);
210 pgofs = nr_pages ? pvec.pages[0]->index: LONG_MAX;
211 pagevec_release(&pvec);
212 return pgofs;
213}
214
215static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
216 int whence)
217{
218 switch (whence) {
219 case SEEK_DATA:
220 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
221 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
222 return true;
223 break;
224 case SEEK_HOLE:
225 if (blkaddr == NULL_ADDR)
226 return true;
227 break;
228 }
229 return false;
230}
231
Chao Yu267378d2014-04-23 14:10:24 +0800232static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
233{
234 struct inode *inode = file->f_mapping->host;
235 loff_t maxbytes = inode->i_sb->s_maxbytes;
236 struct dnode_of_data dn;
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900237 pgoff_t pgofs, end_offset, dirty;
238 loff_t data_ofs = offset;
239 loff_t isize;
Chao Yu267378d2014-04-23 14:10:24 +0800240 int err = 0;
241
242 mutex_lock(&inode->i_mutex);
243
244 isize = i_size_read(inode);
245 if (offset >= isize)
246 goto fail;
247
248 /* handle inline data case */
249 if (f2fs_has_inline_data(inode)) {
250 if (whence == SEEK_HOLE)
251 data_ofs = isize;
252 goto found;
253 }
254
255 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
256
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900257 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
258
Chao Yu267378d2014-04-23 14:10:24 +0800259 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
260 set_new_dnode(&dn, inode, NULL, NULL, 0);
261 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
262 if (err && err != -ENOENT) {
263 goto fail;
264 } else if (err == -ENOENT) {
265 /* direct node is not exist */
266 if (whence == SEEK_DATA) {
267 pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
268 F2FS_I(inode));
269 continue;
270 } else {
271 goto found;
272 }
273 }
274
Chao Yu6b2920a2014-07-07 11:21:59 +0800275 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Chao Yu267378d2014-04-23 14:10:24 +0800276
277 /* find data/hole in dnode block */
278 for (; dn.ofs_in_node < end_offset;
279 dn.ofs_in_node++, pgofs++,
280 data_ofs = pgofs << PAGE_CACHE_SHIFT) {
281 block_t blkaddr;
282 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
283
Jaegeuk Kim7f7670f2014-04-28 18:12:36 +0900284 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
Chao Yu267378d2014-04-23 14:10:24 +0800285 f2fs_put_dnode(&dn);
286 goto found;
287 }
288 }
289 f2fs_put_dnode(&dn);
290 }
291
292 if (whence == SEEK_DATA)
293 goto fail;
Chao Yu267378d2014-04-23 14:10:24 +0800294found:
Jaegeuk Kimfe369bc2014-04-28 17:02:48 +0900295 if (whence == SEEK_HOLE && data_ofs > isize)
296 data_ofs = isize;
Chao Yu267378d2014-04-23 14:10:24 +0800297 mutex_unlock(&inode->i_mutex);
298 return vfs_setpos(file, data_ofs, maxbytes);
299fail:
300 mutex_unlock(&inode->i_mutex);
301 return -ENXIO;
302}
303
304static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
305{
306 struct inode *inode = file->f_mapping->host;
307 loff_t maxbytes = inode->i_sb->s_maxbytes;
308
309 switch (whence) {
310 case SEEK_SET:
311 case SEEK_CUR:
312 case SEEK_END:
313 return generic_file_llseek_size(file, offset, whence,
314 maxbytes, i_size_read(inode));
315 case SEEK_DATA:
316 case SEEK_HOLE:
317 return f2fs_seek_block(file, offset, whence);
318 }
319
320 return -EINVAL;
321}
322
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900323static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
324{
325 file_accessed(file);
326 vma->vm_ops = &f2fs_file_vm_ops;
327 return 0;
328}
329
Jaegeuk Kimb292dcab2013-05-22 08:02:02 +0900330int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900331{
332 int nr_free = 0, ofs = dn->ofs_in_node;
333 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
334 struct f2fs_node *raw_node;
335 __le32 *addr;
336
Gu Zheng45590712013-07-15 17:57:38 +0800337 raw_node = F2FS_NODE(dn->node_page);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900338 addr = blkaddr_in_node(raw_node) + ofs;
339
Chris Fries6c311ec2014-01-17 14:44:39 -0600340 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900341 block_t blkaddr = le32_to_cpu(*addr);
342 if (blkaddr == NULL_ADDR)
343 continue;
344
345 update_extent_cache(NULL_ADDR, dn);
346 invalidate_blocks(sbi, blkaddr);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900347 nr_free++;
348 }
349 if (nr_free) {
Namjae Jeond7cc9502013-06-08 21:25:40 +0900350 dec_valid_block_count(sbi, dn->inode, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900351 set_page_dirty(dn->node_page);
352 sync_inode_page(dn);
353 }
354 dn->ofs_in_node = ofs;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900355
356 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
357 dn->ofs_in_node, nr_free);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900358 return nr_free;
359}
360
361void truncate_data_blocks(struct dnode_of_data *dn)
362{
363 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
364}
365
366static void truncate_partial_data_page(struct inode *inode, u64 from)
367{
368 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
369 struct page *page;
370
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800371 if (f2fs_has_inline_data(inode))
372 return truncate_inline_data(inode, from);
373
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900374 if (!offset)
375 return;
376
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900377 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900378 if (IS_ERR(page))
379 return;
380
381 lock_page(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800382 if (unlikely(!PageUptodate(page) ||
383 page->mapping != inode->i_mapping))
384 goto out;
385
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900386 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900387 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
388 set_page_dirty(page);
Chao Yuca0a81b2014-06-12 13:31:50 +0800389
390out:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900391 f2fs_put_page(page, 1);
392}
393
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900394int truncate_blocks(struct inode *inode, u64 from)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900395{
396 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
397 unsigned int blocksize = inode->i_sb->s_blocksize;
398 struct dnode_of_data dn;
399 pgoff_t free_from;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800400 int count = 0, err = 0;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900401
Namjae Jeon51dd6242013-04-20 01:28:52 +0900402 trace_f2fs_truncate_blocks_enter(inode, from);
403
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900404 if (f2fs_has_inline_data(inode))
405 goto done;
406
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900407 free_from = (pgoff_t)
408 ((from + blocksize - 1) >> (sbi->log_blocksize));
409
Gu Zhenge4795562013-09-27 18:08:30 +0800410 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800411
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900412 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900413 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900414 if (err) {
415 if (err == -ENOENT)
416 goto free_next;
Gu Zhenge4795562013-09-27 18:08:30 +0800417 f2fs_unlock_op(sbi);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900418 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900419 return err;
420 }
421
Chao Yu6403eb12014-04-26 19:59:52 +0800422 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900423
424 count -= dn.ofs_in_node;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900425 f2fs_bug_on(count < 0);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900426
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900427 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
428 truncate_data_blocks_range(&dn, count);
429 free_from += count;
430 }
431
432 f2fs_put_dnode(&dn);
433free_next:
434 err = truncate_inode_blocks(inode, free_from);
Gu Zhenge4795562013-09-27 18:08:30 +0800435 f2fs_unlock_op(sbi);
Jaegeuk Kimf185ff92013-12-27 11:01:54 +0900436done:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900437 /* lastly zero out the first data page */
438 truncate_partial_data_page(inode, from);
439
Namjae Jeon51dd6242013-04-20 01:28:52 +0900440 trace_f2fs_truncate_blocks_exit(inode, err);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900441 return err;
442}
443
444void f2fs_truncate(struct inode *inode)
445{
446 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
447 S_ISLNK(inode->i_mode)))
448 return;
449
Namjae Jeon51dd6242013-04-20 01:28:52 +0900450 trace_f2fs_truncate(inode);
451
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900452 if (!truncate_blocks(inode, i_size_read(inode))) {
453 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
454 mark_inode_dirty(inode);
455 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900456}
457
Jaegeuk Kim2d4d9fb52013-06-07 16:33:07 +0900458int f2fs_getattr(struct vfsmount *mnt,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900459 struct dentry *dentry, struct kstat *stat)
460{
461 struct inode *inode = dentry->d_inode;
462 generic_fillattr(inode, stat);
463 stat->blocks <<= 3;
464 return 0;
465}
466
467#ifdef CONFIG_F2FS_FS_POSIX_ACL
468static void __setattr_copy(struct inode *inode, const struct iattr *attr)
469{
470 struct f2fs_inode_info *fi = F2FS_I(inode);
471 unsigned int ia_valid = attr->ia_valid;
472
473 if (ia_valid & ATTR_UID)
474 inode->i_uid = attr->ia_uid;
475 if (ia_valid & ATTR_GID)
476 inode->i_gid = attr->ia_gid;
477 if (ia_valid & ATTR_ATIME)
478 inode->i_atime = timespec_trunc(attr->ia_atime,
479 inode->i_sb->s_time_gran);
480 if (ia_valid & ATTR_MTIME)
481 inode->i_mtime = timespec_trunc(attr->ia_mtime,
482 inode->i_sb->s_time_gran);
483 if (ia_valid & ATTR_CTIME)
484 inode->i_ctime = timespec_trunc(attr->ia_ctime,
485 inode->i_sb->s_time_gran);
486 if (ia_valid & ATTR_MODE) {
487 umode_t mode = attr->ia_mode;
488
489 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
490 mode &= ~S_ISGID;
491 set_acl_inode(fi, mode);
492 }
493}
494#else
495#define __setattr_copy setattr_copy
496#endif
497
498int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
499{
500 struct inode *inode = dentry->d_inode;
501 struct f2fs_inode_info *fi = F2FS_I(inode);
502 int err;
503
504 err = inode_change_ok(inode, attr);
505 if (err)
506 return err;
507
508 if ((attr->ia_valid & ATTR_SIZE) &&
509 attr->ia_size != i_size_read(inode)) {
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900510 err = f2fs_convert_inline_data(inode, attr->ia_size);
511 if (err)
512 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800513
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900514 truncate_setsize(inode, attr->ia_size);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900515 f2fs_truncate(inode);
Jaegeuk Kimd4686d562013-01-31 15:36:04 +0900516 f2fs_balance_fs(F2FS_SB(inode->i_sb));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900517 }
518
519 __setattr_copy(inode, attr);
520
521 if (attr->ia_valid & ATTR_MODE) {
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800522 err = posix_acl_chmod(inode, get_inode_mode(inode));
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900523 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
524 inode->i_mode = fi->i_acl_mode;
525 clear_inode_flag(fi, FI_ACL_MODE);
526 }
527 }
528
529 mark_inode_dirty(inode);
530 return err;
531}
532
533const struct inode_operations f2fs_file_inode_operations = {
534 .getattr = f2fs_getattr,
535 .setattr = f2fs_setattr,
536 .get_acl = f2fs_get_acl,
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800537 .set_acl = f2fs_set_acl,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900538#ifdef CONFIG_F2FS_FS_XATTR
539 .setxattr = generic_setxattr,
540 .getxattr = generic_getxattr,
541 .listxattr = f2fs_listxattr,
542 .removexattr = generic_removexattr,
543#endif
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900544 .fiemap = f2fs_fiemap,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900545};
546
547static void fill_zero(struct inode *inode, pgoff_t index,
548 loff_t start, loff_t len)
549{
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900550 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900551 struct page *page;
552
553 if (!len)
554 return;
555
Jaegeuk Kimbd43df02013-01-25 18:33:41 +0900556 f2fs_balance_fs(sbi);
557
Gu Zhenge4795562013-09-27 18:08:30 +0800558 f2fs_lock_op(sbi);
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900559 page = get_new_data_page(inode, NULL, index, false);
Gu Zhenge4795562013-09-27 18:08:30 +0800560 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900561
562 if (!IS_ERR(page)) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900563 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900564 zero_user(page, start, len);
565 set_page_dirty(page);
566 f2fs_put_page(page, 1);
567 }
568}
569
570int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
571{
572 pgoff_t index;
573 int err;
574
575 for (index = pg_start; index < pg_end; index++) {
576 struct dnode_of_data dn;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900577
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900578 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900579 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900580 if (err) {
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900581 if (err == -ENOENT)
582 continue;
583 return err;
584 }
585
586 if (dn.data_blkaddr != NULL_ADDR)
587 truncate_data_blocks_range(&dn, 1);
588 f2fs_put_dnode(&dn);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900589 }
590 return 0;
591}
592
Chao Yua66c7b22013-11-22 16:52:50 +0800593static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900594{
595 pgoff_t pg_start, pg_end;
596 loff_t off_start, off_end;
597 int ret = 0;
598
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900599 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1);
Jaegeuk Kim8230a0a2013-12-27 11:13:21 +0900600 if (ret)
601 return ret;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800602
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900603 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
604 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
605
606 off_start = offset & (PAGE_CACHE_SIZE - 1);
607 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
608
609 if (pg_start == pg_end) {
610 fill_zero(inode, pg_start, off_start,
611 off_end - off_start);
612 } else {
613 if (off_start)
614 fill_zero(inode, pg_start++, off_start,
615 PAGE_CACHE_SIZE - off_start);
616 if (off_end)
617 fill_zero(inode, pg_end, 0, off_end);
618
619 if (pg_start < pg_end) {
620 struct address_space *mapping = inode->i_mapping;
621 loff_t blk_start, blk_end;
Jason Hrycay1127a3d2013-04-08 20:16:44 -0500622 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
623
624 f2fs_balance_fs(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900625
626 blk_start = pg_start << PAGE_CACHE_SHIFT;
627 blk_end = pg_end << PAGE_CACHE_SHIFT;
628 truncate_inode_pages_range(mapping, blk_start,
629 blk_end - 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900630
Gu Zhenge4795562013-09-27 18:08:30 +0800631 f2fs_lock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900632 ret = truncate_hole(inode, pg_start, pg_end);
Gu Zhenge4795562013-09-27 18:08:30 +0800633 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900634 }
635 }
636
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900637 return ret;
638}
639
640static int expand_inode_data(struct inode *inode, loff_t offset,
641 loff_t len, int mode)
642{
643 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
644 pgoff_t index, pg_start, pg_end;
645 loff_t new_size = i_size_read(inode);
646 loff_t off_start, off_end;
647 int ret = 0;
648
649 ret = inode_newsize_ok(inode, (len + offset));
650 if (ret)
651 return ret;
652
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900653 ret = f2fs_convert_inline_data(inode, offset + len);
654 if (ret)
655 return ret;
656
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900657 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
658 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
659
660 off_start = offset & (PAGE_CACHE_SIZE - 1);
661 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
662
Jaegeuk Kimead43272014-06-13 13:05:55 +0900663 f2fs_lock_op(sbi);
664
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900665 for (index = pg_start; index <= pg_end; index++) {
666 struct dnode_of_data dn;
667
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900668 if (index == pg_end && !off_end)
669 goto noalloc;
670
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900671 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800672 ret = f2fs_reserve_block(&dn, index);
Huajun Lib6009652013-11-10 23:13:18 +0800673 if (ret)
674 break;
Jaegeuk Kim98397ff2014-06-13 13:07:31 +0900675noalloc:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900676 if (pg_start == pg_end)
677 new_size = offset + len;
678 else if (index == pg_start && off_start)
679 new_size = (index + 1) << PAGE_CACHE_SHIFT;
680 else if (index == pg_end)
681 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
682 else
683 new_size += PAGE_CACHE_SIZE;
684 }
685
686 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
687 i_size_read(inode) < new_size) {
688 i_size_write(inode, new_size);
689 mark_inode_dirty(inode);
Jaegeuk Kimead43272014-06-13 13:05:55 +0900690 update_inode_page(inode);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900691 }
Jaegeuk Kimead43272014-06-13 13:05:55 +0900692 f2fs_unlock_op(sbi);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900693
694 return ret;
695}
696
697static long f2fs_fallocate(struct file *file, int mode,
698 loff_t offset, loff_t len)
699{
Al Viro6131ffa2013-02-27 16:59:05 -0500700 struct inode *inode = file_inode(file);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900701 long ret;
702
703 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
704 return -EOPNOTSUPP;
705
Chao Yu3375f692014-01-28 10:29:26 +0800706 mutex_lock(&inode->i_mutex);
707
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900708 if (mode & FALLOC_FL_PUNCH_HOLE)
Chao Yua66c7b22013-11-22 16:52:50 +0800709 ret = punch_hole(inode, offset, len);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900710 else
711 ret = expand_inode_data(inode, offset, len, mode);
712
Namjae Jeon3af60a42012-12-30 14:52:37 +0900713 if (!ret) {
714 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
715 mark_inode_dirty(inode);
716 }
Chao Yu3375f692014-01-28 10:29:26 +0800717
718 mutex_unlock(&inode->i_mutex);
719
Namjae Jeonc01e2852013-04-23 17:00:52 +0900720 trace_f2fs_fallocate(inode, mode, offset, len, ret);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900721 return ret;
722}
723
724#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
725#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
726
727static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
728{
729 if (S_ISDIR(mode))
730 return flags;
731 else if (S_ISREG(mode))
732 return flags & F2FS_REG_FLMASK;
733 else
734 return flags & F2FS_OTHER_FLMASK;
735}
736
737long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
738{
Al Viro6131ffa2013-02-27 16:59:05 -0500739 struct inode *inode = file_inode(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900740 struct f2fs_inode_info *fi = F2FS_I(inode);
741 unsigned int flags;
742 int ret;
743
744 switch (cmd) {
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900745 case F2FS_IOC_GETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900746 flags = fi->i_flags & FS_FL_USER_VISIBLE;
747 return put_user(flags, (int __user *) arg);
Namjae Jeon6a3e8ef2013-06-08 21:25:28 +0900748 case F2FS_IOC_SETFLAGS:
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900749 {
750 unsigned int oldflags;
751
Al Virobdaec332013-03-20 09:33:23 -0400752 ret = mnt_want_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900753 if (ret)
754 return ret;
755
756 if (!inode_owner_or_capable(inode)) {
757 ret = -EACCES;
758 goto out;
759 }
760
761 if (get_user(flags, (int __user *) arg)) {
762 ret = -EFAULT;
763 goto out;
764 }
765
766 flags = f2fs_mask_flags(inode->i_mode, flags);
767
768 mutex_lock(&inode->i_mutex);
769
770 oldflags = fi->i_flags;
771
772 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
773 if (!capable(CAP_LINUX_IMMUTABLE)) {
774 mutex_unlock(&inode->i_mutex);
775 ret = -EPERM;
776 goto out;
777 }
778 }
779
780 flags = flags & FS_FL_USER_MODIFIABLE;
781 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
782 fi->i_flags = flags;
783 mutex_unlock(&inode->i_mutex);
784
785 f2fs_set_inode_flags(inode);
786 inode->i_ctime = CURRENT_TIME;
787 mark_inode_dirty(inode);
788out:
Al Virobdaec332013-03-20 09:33:23 -0400789 mnt_drop_write_file(filp);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900790 return ret;
791 }
792 default:
793 return -ENOTTY;
794 }
795}
796
Namjae Jeone9750822013-02-04 23:41:41 +0900797#ifdef CONFIG_COMPAT
798long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
799{
800 switch (cmd) {
801 case F2FS_IOC32_GETFLAGS:
802 cmd = F2FS_IOC_GETFLAGS;
803 break;
804 case F2FS_IOC32_SETFLAGS:
805 cmd = F2FS_IOC_SETFLAGS;
806 break;
807 default:
808 return -ENOIOCTLCMD;
809 }
810 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
811}
812#endif
813
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900814const struct file_operations f2fs_file_operations = {
Chao Yu267378d2014-04-23 14:10:24 +0800815 .llseek = f2fs_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400816 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -0400817 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400818 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400819 .write_iter = generic_file_write_iter,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900820 .open = generic_file_open,
821 .mmap = f2fs_file_mmap,
822 .fsync = f2fs_sync_file,
823 .fallocate = f2fs_fallocate,
824 .unlocked_ioctl = f2fs_ioctl,
Namjae Jeone9750822013-02-04 23:41:41 +0900825#ifdef CONFIG_COMPAT
826 .compat_ioctl = f2fs_compat_ioctl,
827#endif
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900828 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400829 .splice_write = iter_file_splice_write,
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900830};