blob: 819de7f39f265a2700bc10c4321cc0d2aa3feed4 [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>
16#include <linux/falloc.h>
17#include <linux/types.h>
18#include <linux/uaccess.h>
19#include <linux/mount.h>
20
21#include "f2fs.h"
22#include "node.h"
23#include "segment.h"
24#include "xattr.h"
25#include "acl.h"
26
27static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
28 struct vm_fault *vmf)
29{
30 struct page *page = vmf->page;
31 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
32 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090033 block_t old_blk_addr;
34 struct dnode_of_data dn;
35 int err;
36
37 f2fs_balance_fs(sbi);
38
39 sb_start_pagefault(inode->i_sb);
40
41 mutex_lock_op(sbi, DATA_NEW);
42
43 /* block allocation */
44 set_new_dnode(&dn, inode, NULL, NULL, 0);
45 err = get_dnode_of_data(&dn, page->index, 0);
46 if (err) {
47 mutex_unlock_op(sbi, DATA_NEW);
48 goto out;
49 }
50
51 old_blk_addr = dn.data_blkaddr;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +090052
53 if (old_blk_addr == NULL_ADDR) {
54 err = reserve_new_block(&dn);
55 if (err) {
56 f2fs_put_dnode(&dn);
57 mutex_unlock_op(sbi, DATA_NEW);
58 goto out;
59 }
60 }
61 f2fs_put_dnode(&dn);
62
63 mutex_unlock_op(sbi, DATA_NEW);
64
65 lock_page(page);
66 if (page->mapping != inode->i_mapping ||
67 page_offset(page) >= i_size_read(inode) ||
68 !PageUptodate(page)) {
69 unlock_page(page);
70 err = -EFAULT;
71 goto out;
72 }
73
74 /*
75 * check to see if the page is mapped already (no holes)
76 */
77 if (PageMappedToDisk(page))
78 goto out;
79
80 /* fill the page */
81 wait_on_page_writeback(page);
82
83 /* page is wholly or partially inside EOF */
84 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
85 unsigned offset;
86 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
87 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
88 }
89 set_page_dirty(page);
90 SetPageUptodate(page);
91
92 file_update_time(vma->vm_file);
93out:
94 sb_end_pagefault(inode->i_sb);
95 return block_page_mkwrite_return(err);
96}
97
98static const struct vm_operations_struct f2fs_file_vm_ops = {
99 .fault = filemap_fault,
100 .page_mkwrite = f2fs_vm_page_mkwrite,
101};
102
103static int need_to_sync_dir(struct f2fs_sb_info *sbi, struct inode *inode)
104{
105 struct dentry *dentry;
106 nid_t pino;
107
108 inode = igrab(inode);
109 dentry = d_find_any_alias(inode);
110 if (!dentry) {
111 iput(inode);
112 return 0;
113 }
114 pino = dentry->d_parent->d_inode->i_ino;
115 dput(dentry);
116 iput(inode);
117 return !is_checkpointed_node(sbi, pino);
118}
119
120int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
121{
122 struct inode *inode = file->f_mapping->host;
123 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
124 unsigned long long cur_version;
125 int ret = 0;
126 bool need_cp = false;
127 struct writeback_control wbc = {
128 .sync_mode = WB_SYNC_ALL,
129 .nr_to_write = LONG_MAX,
130 .for_reclaim = 0,
131 };
132
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900133 if (inode->i_sb->s_flags & MS_RDONLY)
134 return 0;
135
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900136 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
137 if (ret)
138 return ret;
139
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900140 /* guarantee free sections for fsync */
141 f2fs_balance_fs(sbi);
142
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900143 mutex_lock(&inode->i_mutex);
144
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900145 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
146 goto out;
147
148 mutex_lock(&sbi->cp_mutex);
149 cur_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver);
150 mutex_unlock(&sbi->cp_mutex);
151
152 if (F2FS_I(inode)->data_version != cur_version &&
153 !(inode->i_state & I_DIRTY))
154 goto out;
155 F2FS_I(inode)->data_version--;
156
157 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
158 need_cp = true;
159 if (is_inode_flag_set(F2FS_I(inode), FI_NEED_CP))
160 need_cp = true;
161 if (!space_for_roll_forward(sbi))
162 need_cp = true;
163 if (need_to_sync_dir(sbi, inode))
164 need_cp = true;
165
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900166 if (need_cp) {
167 /* all the dirty node pages should be flushed for POR */
168 ret = f2fs_sync_fs(inode->i_sb, 1);
169 clear_inode_flag(F2FS_I(inode), FI_NEED_CP);
170 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900171 /* if there is no written node page, write its inode page */
172 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
173 ret = f2fs_write_inode(inode, NULL);
174 if (ret)
175 goto out;
176 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900177 filemap_fdatawait_range(sbi->node_inode->i_mapping,
178 0, LONG_MAX);
179 }
180out:
181 mutex_unlock(&inode->i_mutex);
182 return ret;
183}
184
185static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
186{
187 file_accessed(file);
188 vma->vm_ops = &f2fs_file_vm_ops;
189 return 0;
190}
191
192static int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
193{
194 int nr_free = 0, ofs = dn->ofs_in_node;
195 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
196 struct f2fs_node *raw_node;
197 __le32 *addr;
198
199 raw_node = page_address(dn->node_page);
200 addr = blkaddr_in_node(raw_node) + ofs;
201
202 for ( ; count > 0; count--, addr++, dn->ofs_in_node++) {
203 block_t blkaddr = le32_to_cpu(*addr);
204 if (blkaddr == NULL_ADDR)
205 continue;
206
207 update_extent_cache(NULL_ADDR, dn);
208 invalidate_blocks(sbi, blkaddr);
209 dec_valid_block_count(sbi, dn->inode, 1);
210 nr_free++;
211 }
212 if (nr_free) {
213 set_page_dirty(dn->node_page);
214 sync_inode_page(dn);
215 }
216 dn->ofs_in_node = ofs;
217 return nr_free;
218}
219
220void truncate_data_blocks(struct dnode_of_data *dn)
221{
222 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
223}
224
225static void truncate_partial_data_page(struct inode *inode, u64 from)
226{
227 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
228 struct page *page;
229
230 if (!offset)
231 return;
232
233 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT);
234 if (IS_ERR(page))
235 return;
236
237 lock_page(page);
238 wait_on_page_writeback(page);
239 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
240 set_page_dirty(page);
241 f2fs_put_page(page, 1);
242}
243
244static int truncate_blocks(struct inode *inode, u64 from)
245{
246 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
247 unsigned int blocksize = inode->i_sb->s_blocksize;
248 struct dnode_of_data dn;
249 pgoff_t free_from;
250 int count = 0;
251 int err;
252
253 free_from = (pgoff_t)
254 ((from + blocksize - 1) >> (sbi->log_blocksize));
255
256 mutex_lock_op(sbi, DATA_TRUNC);
257
258 set_new_dnode(&dn, inode, NULL, NULL, 0);
259 err = get_dnode_of_data(&dn, free_from, RDONLY_NODE);
260 if (err) {
261 if (err == -ENOENT)
262 goto free_next;
263 mutex_unlock_op(sbi, DATA_TRUNC);
264 return err;
265 }
266
267 if (IS_INODE(dn.node_page))
268 count = ADDRS_PER_INODE;
269 else
270 count = ADDRS_PER_BLOCK;
271
272 count -= dn.ofs_in_node;
273 BUG_ON(count < 0);
274 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
275 truncate_data_blocks_range(&dn, count);
276 free_from += count;
277 }
278
279 f2fs_put_dnode(&dn);
280free_next:
281 err = truncate_inode_blocks(inode, free_from);
282 mutex_unlock_op(sbi, DATA_TRUNC);
283
284 /* lastly zero out the first data page */
285 truncate_partial_data_page(inode, from);
286
287 return err;
288}
289
290void f2fs_truncate(struct inode *inode)
291{
292 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
293 S_ISLNK(inode->i_mode)))
294 return;
295
296 if (!truncate_blocks(inode, i_size_read(inode))) {
297 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
298 mark_inode_dirty(inode);
299 }
300
301 f2fs_balance_fs(F2FS_SB(inode->i_sb));
302}
303
304static int f2fs_getattr(struct vfsmount *mnt,
305 struct dentry *dentry, struct kstat *stat)
306{
307 struct inode *inode = dentry->d_inode;
308 generic_fillattr(inode, stat);
309 stat->blocks <<= 3;
310 return 0;
311}
312
313#ifdef CONFIG_F2FS_FS_POSIX_ACL
314static void __setattr_copy(struct inode *inode, const struct iattr *attr)
315{
316 struct f2fs_inode_info *fi = F2FS_I(inode);
317 unsigned int ia_valid = attr->ia_valid;
318
319 if (ia_valid & ATTR_UID)
320 inode->i_uid = attr->ia_uid;
321 if (ia_valid & ATTR_GID)
322 inode->i_gid = attr->ia_gid;
323 if (ia_valid & ATTR_ATIME)
324 inode->i_atime = timespec_trunc(attr->ia_atime,
325 inode->i_sb->s_time_gran);
326 if (ia_valid & ATTR_MTIME)
327 inode->i_mtime = timespec_trunc(attr->ia_mtime,
328 inode->i_sb->s_time_gran);
329 if (ia_valid & ATTR_CTIME)
330 inode->i_ctime = timespec_trunc(attr->ia_ctime,
331 inode->i_sb->s_time_gran);
332 if (ia_valid & ATTR_MODE) {
333 umode_t mode = attr->ia_mode;
334
335 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
336 mode &= ~S_ISGID;
337 set_acl_inode(fi, mode);
338 }
339}
340#else
341#define __setattr_copy setattr_copy
342#endif
343
344int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
345{
346 struct inode *inode = dentry->d_inode;
347 struct f2fs_inode_info *fi = F2FS_I(inode);
348 int err;
349
350 err = inode_change_ok(inode, attr);
351 if (err)
352 return err;
353
354 if ((attr->ia_valid & ATTR_SIZE) &&
355 attr->ia_size != i_size_read(inode)) {
356 truncate_setsize(inode, attr->ia_size);
357 f2fs_truncate(inode);
358 }
359
360 __setattr_copy(inode, attr);
361
362 if (attr->ia_valid & ATTR_MODE) {
363 err = f2fs_acl_chmod(inode);
364 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
365 inode->i_mode = fi->i_acl_mode;
366 clear_inode_flag(fi, FI_ACL_MODE);
367 }
368 }
369
370 mark_inode_dirty(inode);
371 return err;
372}
373
374const struct inode_operations f2fs_file_inode_operations = {
375 .getattr = f2fs_getattr,
376 .setattr = f2fs_setattr,
377 .get_acl = f2fs_get_acl,
378#ifdef CONFIG_F2FS_FS_XATTR
379 .setxattr = generic_setxattr,
380 .getxattr = generic_getxattr,
381 .listxattr = f2fs_listxattr,
382 .removexattr = generic_removexattr,
383#endif
384};
385
386static void fill_zero(struct inode *inode, pgoff_t index,
387 loff_t start, loff_t len)
388{
389 struct page *page;
390
391 if (!len)
392 return;
393
394 page = get_new_data_page(inode, index, false);
395
396 if (!IS_ERR(page)) {
397 wait_on_page_writeback(page);
398 zero_user(page, start, len);
399 set_page_dirty(page);
400 f2fs_put_page(page, 1);
401 }
402}
403
404int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
405{
406 pgoff_t index;
407 int err;
408
409 for (index = pg_start; index < pg_end; index++) {
410 struct dnode_of_data dn;
411 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
412
Jaegeuk Kim9eaeba72013-01-11 14:09:38 +0900413 f2fs_balance_fs(sbi);
414
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900415 mutex_lock_op(sbi, DATA_TRUNC);
416 set_new_dnode(&dn, inode, NULL, NULL, 0);
417 err = get_dnode_of_data(&dn, index, RDONLY_NODE);
418 if (err) {
419 mutex_unlock_op(sbi, DATA_TRUNC);
420 if (err == -ENOENT)
421 continue;
422 return err;
423 }
424
425 if (dn.data_blkaddr != NULL_ADDR)
426 truncate_data_blocks_range(&dn, 1);
427 f2fs_put_dnode(&dn);
428 mutex_unlock_op(sbi, DATA_TRUNC);
429 }
430 return 0;
431}
432
433static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode)
434{
435 pgoff_t pg_start, pg_end;
436 loff_t off_start, off_end;
437 int ret = 0;
438
439 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
440 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
441
442 off_start = offset & (PAGE_CACHE_SIZE - 1);
443 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
444
445 if (pg_start == pg_end) {
446 fill_zero(inode, pg_start, off_start,
447 off_end - off_start);
448 } else {
449 if (off_start)
450 fill_zero(inode, pg_start++, off_start,
451 PAGE_CACHE_SIZE - off_start);
452 if (off_end)
453 fill_zero(inode, pg_end, 0, off_end);
454
455 if (pg_start < pg_end) {
456 struct address_space *mapping = inode->i_mapping;
457 loff_t blk_start, blk_end;
458
459 blk_start = pg_start << PAGE_CACHE_SHIFT;
460 blk_end = pg_end << PAGE_CACHE_SHIFT;
461 truncate_inode_pages_range(mapping, blk_start,
462 blk_end - 1);
463 ret = truncate_hole(inode, pg_start, pg_end);
464 }
465 }
466
467 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
468 i_size_read(inode) <= (offset + len)) {
469 i_size_write(inode, offset);
470 mark_inode_dirty(inode);
471 }
472
473 return ret;
474}
475
476static int expand_inode_data(struct inode *inode, loff_t offset,
477 loff_t len, int mode)
478{
479 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
480 pgoff_t index, pg_start, pg_end;
481 loff_t new_size = i_size_read(inode);
482 loff_t off_start, off_end;
483 int ret = 0;
484
485 ret = inode_newsize_ok(inode, (len + offset));
486 if (ret)
487 return ret;
488
489 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
490 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
491
492 off_start = offset & (PAGE_CACHE_SIZE - 1);
493 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
494
495 for (index = pg_start; index <= pg_end; index++) {
496 struct dnode_of_data dn;
497
498 mutex_lock_op(sbi, DATA_NEW);
499
500 set_new_dnode(&dn, inode, NULL, NULL, 0);
501 ret = get_dnode_of_data(&dn, index, 0);
502 if (ret) {
503 mutex_unlock_op(sbi, DATA_NEW);
504 break;
505 }
506
507 if (dn.data_blkaddr == NULL_ADDR) {
508 ret = reserve_new_block(&dn);
509 if (ret) {
510 f2fs_put_dnode(&dn);
511 mutex_unlock_op(sbi, DATA_NEW);
512 break;
513 }
514 }
515 f2fs_put_dnode(&dn);
516
517 mutex_unlock_op(sbi, DATA_NEW);
518
519 if (pg_start == pg_end)
520 new_size = offset + len;
521 else if (index == pg_start && off_start)
522 new_size = (index + 1) << PAGE_CACHE_SHIFT;
523 else if (index == pg_end)
524 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
525 else
526 new_size += PAGE_CACHE_SIZE;
527 }
528
529 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
530 i_size_read(inode) < new_size) {
531 i_size_write(inode, new_size);
532 mark_inode_dirty(inode);
533 }
534
535 return ret;
536}
537
538static long f2fs_fallocate(struct file *file, int mode,
539 loff_t offset, loff_t len)
540{
541 struct inode *inode = file->f_path.dentry->d_inode;
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900542 long ret;
543
544 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
545 return -EOPNOTSUPP;
546
547 if (mode & FALLOC_FL_PUNCH_HOLE)
548 ret = punch_hole(inode, offset, len, mode);
549 else
550 ret = expand_inode_data(inode, offset, len, mode);
551
Namjae Jeon3af60a42012-12-30 14:52:37 +0900552 if (!ret) {
553 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
554 mark_inode_dirty(inode);
555 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900556 return ret;
557}
558
559#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
560#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
561
562static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
563{
564 if (S_ISDIR(mode))
565 return flags;
566 else if (S_ISREG(mode))
567 return flags & F2FS_REG_FLMASK;
568 else
569 return flags & F2FS_OTHER_FLMASK;
570}
571
572long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
573{
574 struct inode *inode = filp->f_dentry->d_inode;
575 struct f2fs_inode_info *fi = F2FS_I(inode);
576 unsigned int flags;
577 int ret;
578
579 switch (cmd) {
580 case FS_IOC_GETFLAGS:
581 flags = fi->i_flags & FS_FL_USER_VISIBLE;
582 return put_user(flags, (int __user *) arg);
583 case FS_IOC_SETFLAGS:
584 {
585 unsigned int oldflags;
586
587 ret = mnt_want_write(filp->f_path.mnt);
588 if (ret)
589 return ret;
590
591 if (!inode_owner_or_capable(inode)) {
592 ret = -EACCES;
593 goto out;
594 }
595
596 if (get_user(flags, (int __user *) arg)) {
597 ret = -EFAULT;
598 goto out;
599 }
600
601 flags = f2fs_mask_flags(inode->i_mode, flags);
602
603 mutex_lock(&inode->i_mutex);
604
605 oldflags = fi->i_flags;
606
607 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
608 if (!capable(CAP_LINUX_IMMUTABLE)) {
609 mutex_unlock(&inode->i_mutex);
610 ret = -EPERM;
611 goto out;
612 }
613 }
614
615 flags = flags & FS_FL_USER_MODIFIABLE;
616 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
617 fi->i_flags = flags;
618 mutex_unlock(&inode->i_mutex);
619
620 f2fs_set_inode_flags(inode);
621 inode->i_ctime = CURRENT_TIME;
622 mark_inode_dirty(inode);
623out:
624 mnt_drop_write(filp->f_path.mnt);
625 return ret;
626 }
627 default:
628 return -ENOTTY;
629 }
630}
631
632const struct file_operations f2fs_file_operations = {
633 .llseek = generic_file_llseek,
634 .read = do_sync_read,
635 .write = do_sync_write,
636 .aio_read = generic_file_aio_read,
637 .aio_write = generic_file_aio_write,
638 .open = generic_file_open,
639 .mmap = f2fs_file_mmap,
640 .fsync = f2fs_sync_file,
641 .fallocate = f2fs_fallocate,
642 .unlocked_ioctl = f2fs_ioctl,
643 .splice_read = generic_file_splice_read,
644 .splice_write = generic_file_splice_write,
645};