Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / fs / f2fs / file.c
blob0dfcef53a6ed830e442f8c174ea166b0d1b2eabd
1 /*
2 * fs/f2fs/file.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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/blkdev.h>
17 #include <linux/falloc.h>
18 #include <linux/types.h>
19 #include <linux/compat.h>
20 #include <linux/uaccess.h>
21 #include <linux/mount.h>
23 #include "f2fs.h"
24 #include "node.h"
25 #include "segment.h"
26 #include "xattr.h"
27 #include "acl.h"
28 #include <trace/events/f2fs.h>
30 static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
31 struct vm_fault *vmf)
33 struct page *page = vmf->page;
34 struct inode *inode = file_inode(vma->vm_file);
35 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
36 struct dnode_of_data dn;
37 int err;
39 f2fs_balance_fs(sbi);
41 sb_start_pagefault(inode->i_sb);
43 /* block allocation */
44 f2fs_lock_op(sbi);
45 set_new_dnode(&dn, inode, NULL, NULL, 0);
46 err = f2fs_reserve_block(&dn, page->index);
47 f2fs_unlock_op(sbi);
48 if (err)
49 goto out;
51 file_update_time(vma->vm_file);
52 lock_page(page);
53 if (unlikely(page->mapping != inode->i_mapping ||
54 page_offset(page) > i_size_read(inode) ||
55 !PageUptodate(page))) {
56 unlock_page(page);
57 err = -EFAULT;
58 goto out;
62 * check to see if the page is mapped already (no holes)
64 if (PageMappedToDisk(page))
65 goto mapped;
67 /* page is wholly or partially inside EOF */
68 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
69 unsigned offset;
70 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
71 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
73 set_page_dirty(page);
74 SetPageUptodate(page);
76 trace_f2fs_vm_page_mkwrite(page, DATA);
77 mapped:
78 /* fill the page */
79 wait_on_page_writeback(page);
80 out:
81 sb_end_pagefault(inode->i_sb);
82 return block_page_mkwrite_return(err);
85 static const struct vm_operations_struct f2fs_file_vm_ops = {
86 .fault = filemap_fault,
87 .page_mkwrite = f2fs_vm_page_mkwrite,
88 .remap_pages = generic_file_remap_pages,
91 static int get_parent_ino(struct inode *inode, nid_t *pino)
93 struct dentry *dentry;
95 inode = igrab(inode);
96 dentry = d_find_any_alias(inode);
97 iput(inode);
98 if (!dentry)
99 return 0;
101 if (update_dent_inode(inode, &dentry->d_name)) {
102 dput(dentry);
103 return 0;
106 *pino = parent_ino(dentry);
107 dput(dentry);
108 return 1;
111 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
113 struct inode *inode = file->f_mapping->host;
114 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
115 int ret = 0;
116 bool need_cp = false;
117 struct writeback_control wbc = {
118 .sync_mode = WB_SYNC_NONE,
119 .nr_to_write = LONG_MAX,
120 .for_reclaim = 0,
123 if (unlikely(f2fs_readonly(inode->i_sb)))
124 return 0;
126 trace_f2fs_sync_file_enter(inode);
127 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
128 if (ret) {
129 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
130 return ret;
133 /* guarantee free sections for fsync */
134 f2fs_balance_fs(sbi);
136 mutex_lock(&inode->i_mutex);
139 * Both of fdatasync() and fsync() are able to be recovered from
140 * sudden-power-off.
142 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
143 need_cp = true;
144 else if (file_wrong_pino(inode))
145 need_cp = true;
146 else if (!space_for_roll_forward(sbi))
147 need_cp = true;
148 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
149 need_cp = true;
150 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
151 need_cp = true;
153 if (need_cp) {
154 nid_t pino;
156 F2FS_I(inode)->xattr_ver = 0;
158 /* all the dirty node pages should be flushed for POR */
159 ret = f2fs_sync_fs(inode->i_sb, 1);
160 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
161 get_parent_ino(inode, &pino)) {
162 F2FS_I(inode)->i_pino = pino;
163 file_got_pino(inode);
164 mark_inode_dirty_sync(inode);
165 ret = f2fs_write_inode(inode, NULL);
166 if (ret)
167 goto out;
169 } else {
170 /* if there is no written node page, write its inode page */
171 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
172 mark_inode_dirty_sync(inode);
173 ret = f2fs_write_inode(inode, NULL);
174 if (ret)
175 goto out;
177 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
178 if (ret)
179 goto out;
180 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
182 out:
183 mutex_unlock(&inode->i_mutex);
184 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
185 return ret;
188 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
190 file_accessed(file);
191 vma->vm_ops = &f2fs_file_vm_ops;
192 return 0;
195 int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
197 int nr_free = 0, ofs = dn->ofs_in_node;
198 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
199 struct f2fs_node *raw_node;
200 __le32 *addr;
202 raw_node = F2FS_NODE(dn->node_page);
203 addr = blkaddr_in_node(raw_node) + ofs;
205 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
206 block_t blkaddr = le32_to_cpu(*addr);
207 if (blkaddr == NULL_ADDR)
208 continue;
210 update_extent_cache(NULL_ADDR, dn);
211 invalidate_blocks(sbi, blkaddr);
212 nr_free++;
214 if (nr_free) {
215 dec_valid_block_count(sbi, dn->inode, nr_free);
216 set_page_dirty(dn->node_page);
217 sync_inode_page(dn);
219 dn->ofs_in_node = ofs;
221 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
222 dn->ofs_in_node, nr_free);
223 return nr_free;
226 void truncate_data_blocks(struct dnode_of_data *dn)
228 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
231 static void truncate_partial_data_page(struct inode *inode, u64 from)
233 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
234 struct page *page;
236 if (!offset)
237 return;
239 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
240 if (IS_ERR(page))
241 return;
243 lock_page(page);
244 if (unlikely(page->mapping != inode->i_mapping)) {
245 f2fs_put_page(page, 1);
246 return;
248 wait_on_page_writeback(page);
249 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
250 set_page_dirty(page);
251 f2fs_put_page(page, 1);
254 int truncate_blocks(struct inode *inode, u64 from)
256 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
257 unsigned int blocksize = inode->i_sb->s_blocksize;
258 struct dnode_of_data dn;
259 pgoff_t free_from;
260 int count = 0, err = 0;
262 trace_f2fs_truncate_blocks_enter(inode, from);
264 if (f2fs_has_inline_data(inode))
265 goto done;
267 free_from = (pgoff_t)
268 ((from + blocksize - 1) >> (sbi->log_blocksize));
270 f2fs_lock_op(sbi);
272 set_new_dnode(&dn, inode, NULL, NULL, 0);
273 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
274 if (err) {
275 if (err == -ENOENT)
276 goto free_next;
277 f2fs_unlock_op(sbi);
278 trace_f2fs_truncate_blocks_exit(inode, err);
279 return err;
282 if (IS_INODE(dn.node_page))
283 count = ADDRS_PER_INODE(F2FS_I(inode));
284 else
285 count = ADDRS_PER_BLOCK;
287 count -= dn.ofs_in_node;
288 f2fs_bug_on(count < 0);
290 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
291 truncate_data_blocks_range(&dn, count);
292 free_from += count;
295 f2fs_put_dnode(&dn);
296 free_next:
297 err = truncate_inode_blocks(inode, free_from);
298 f2fs_unlock_op(sbi);
299 done:
300 /* lastly zero out the first data page */
301 truncate_partial_data_page(inode, from);
303 trace_f2fs_truncate_blocks_exit(inode, err);
304 return err;
307 void f2fs_truncate(struct inode *inode)
309 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
310 S_ISLNK(inode->i_mode)))
311 return;
313 trace_f2fs_truncate(inode);
315 if (!truncate_blocks(inode, i_size_read(inode))) {
316 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
317 mark_inode_dirty(inode);
321 int f2fs_getattr(struct vfsmount *mnt,
322 struct dentry *dentry, struct kstat *stat)
324 struct inode *inode = dentry->d_inode;
325 generic_fillattr(inode, stat);
326 stat->blocks <<= 3;
327 return 0;
330 #ifdef CONFIG_F2FS_FS_POSIX_ACL
331 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
333 struct f2fs_inode_info *fi = F2FS_I(inode);
334 unsigned int ia_valid = attr->ia_valid;
336 if (ia_valid & ATTR_UID)
337 inode->i_uid = attr->ia_uid;
338 if (ia_valid & ATTR_GID)
339 inode->i_gid = attr->ia_gid;
340 if (ia_valid & ATTR_ATIME)
341 inode->i_atime = timespec_trunc(attr->ia_atime,
342 inode->i_sb->s_time_gran);
343 if (ia_valid & ATTR_MTIME)
344 inode->i_mtime = timespec_trunc(attr->ia_mtime,
345 inode->i_sb->s_time_gran);
346 if (ia_valid & ATTR_CTIME)
347 inode->i_ctime = timespec_trunc(attr->ia_ctime,
348 inode->i_sb->s_time_gran);
349 if (ia_valid & ATTR_MODE) {
350 umode_t mode = attr->ia_mode;
352 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
353 mode &= ~S_ISGID;
354 set_acl_inode(fi, mode);
357 #else
358 #define __setattr_copy setattr_copy
359 #endif
361 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
363 struct inode *inode = dentry->d_inode;
364 struct f2fs_inode_info *fi = F2FS_I(inode);
365 int err;
367 err = inode_change_ok(inode, attr);
368 if (err)
369 return err;
371 if ((attr->ia_valid & ATTR_SIZE) &&
372 attr->ia_size != i_size_read(inode)) {
373 err = f2fs_convert_inline_data(inode, attr->ia_size);
374 if (err)
375 return err;
377 truncate_setsize(inode, attr->ia_size);
378 f2fs_truncate(inode);
379 f2fs_balance_fs(F2FS_SB(inode->i_sb));
382 __setattr_copy(inode, attr);
384 if (attr->ia_valid & ATTR_MODE) {
385 err = posix_acl_chmod(inode, get_inode_mode(inode));
386 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
387 inode->i_mode = fi->i_acl_mode;
388 clear_inode_flag(fi, FI_ACL_MODE);
392 mark_inode_dirty(inode);
393 return err;
396 const struct inode_operations f2fs_file_inode_operations = {
397 .getattr = f2fs_getattr,
398 .setattr = f2fs_setattr,
399 .get_acl = f2fs_get_acl,
400 .set_acl = f2fs_set_acl,
401 #ifdef CONFIG_F2FS_FS_XATTR
402 .setxattr = generic_setxattr,
403 .getxattr = generic_getxattr,
404 .listxattr = f2fs_listxattr,
405 .removexattr = generic_removexattr,
406 #endif
409 static void fill_zero(struct inode *inode, pgoff_t index,
410 loff_t start, loff_t len)
412 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
413 struct page *page;
415 if (!len)
416 return;
418 f2fs_balance_fs(sbi);
420 f2fs_lock_op(sbi);
421 page = get_new_data_page(inode, NULL, index, false);
422 f2fs_unlock_op(sbi);
424 if (!IS_ERR(page)) {
425 wait_on_page_writeback(page);
426 zero_user(page, start, len);
427 set_page_dirty(page);
428 f2fs_put_page(page, 1);
432 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
434 pgoff_t index;
435 int err;
437 for (index = pg_start; index < pg_end; index++) {
438 struct dnode_of_data dn;
440 set_new_dnode(&dn, inode, NULL, NULL, 0);
441 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
442 if (err) {
443 if (err == -ENOENT)
444 continue;
445 return err;
448 if (dn.data_blkaddr != NULL_ADDR)
449 truncate_data_blocks_range(&dn, 1);
450 f2fs_put_dnode(&dn);
452 return 0;
455 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
457 pgoff_t pg_start, pg_end;
458 loff_t off_start, off_end;
459 int ret = 0;
461 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1);
462 if (ret)
463 return ret;
465 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
466 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
468 off_start = offset & (PAGE_CACHE_SIZE - 1);
469 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
471 if (pg_start == pg_end) {
472 fill_zero(inode, pg_start, off_start,
473 off_end - off_start);
474 } else {
475 if (off_start)
476 fill_zero(inode, pg_start++, off_start,
477 PAGE_CACHE_SIZE - off_start);
478 if (off_end)
479 fill_zero(inode, pg_end, 0, off_end);
481 if (pg_start < pg_end) {
482 struct address_space *mapping = inode->i_mapping;
483 loff_t blk_start, blk_end;
484 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
486 f2fs_balance_fs(sbi);
488 blk_start = pg_start << PAGE_CACHE_SHIFT;
489 blk_end = pg_end << PAGE_CACHE_SHIFT;
490 truncate_inode_pages_range(mapping, blk_start,
491 blk_end - 1);
493 f2fs_lock_op(sbi);
494 ret = truncate_hole(inode, pg_start, pg_end);
495 f2fs_unlock_op(sbi);
499 return ret;
502 static int expand_inode_data(struct inode *inode, loff_t offset,
503 loff_t len, int mode)
505 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
506 pgoff_t index, pg_start, pg_end;
507 loff_t new_size = i_size_read(inode);
508 loff_t off_start, off_end;
509 int ret = 0;
511 ret = inode_newsize_ok(inode, (len + offset));
512 if (ret)
513 return ret;
515 ret = f2fs_convert_inline_data(inode, offset + len);
516 if (ret)
517 return ret;
519 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
520 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
522 off_start = offset & (PAGE_CACHE_SIZE - 1);
523 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
525 for (index = pg_start; index <= pg_end; index++) {
526 struct dnode_of_data dn;
528 f2fs_lock_op(sbi);
529 set_new_dnode(&dn, inode, NULL, NULL, 0);
530 ret = f2fs_reserve_block(&dn, index);
531 f2fs_unlock_op(sbi);
532 if (ret)
533 break;
535 if (pg_start == pg_end)
536 new_size = offset + len;
537 else if (index == pg_start && off_start)
538 new_size = (index + 1) << PAGE_CACHE_SHIFT;
539 else if (index == pg_end)
540 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
541 else
542 new_size += PAGE_CACHE_SIZE;
545 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
546 i_size_read(inode) < new_size) {
547 i_size_write(inode, new_size);
548 mark_inode_dirty(inode);
551 return ret;
554 static long f2fs_fallocate(struct file *file, int mode,
555 loff_t offset, loff_t len)
557 struct inode *inode = file_inode(file);
558 long ret;
560 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
561 return -EOPNOTSUPP;
563 if (mode & FALLOC_FL_PUNCH_HOLE)
564 ret = punch_hole(inode, offset, len);
565 else
566 ret = expand_inode_data(inode, offset, len, mode);
568 if (!ret) {
569 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
570 mark_inode_dirty(inode);
572 trace_f2fs_fallocate(inode, mode, offset, len, ret);
573 return ret;
576 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
577 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
579 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
581 if (S_ISDIR(mode))
582 return flags;
583 else if (S_ISREG(mode))
584 return flags & F2FS_REG_FLMASK;
585 else
586 return flags & F2FS_OTHER_FLMASK;
589 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
591 struct inode *inode = file_inode(filp);
592 struct f2fs_inode_info *fi = F2FS_I(inode);
593 unsigned int flags;
594 int ret;
596 switch (cmd) {
597 case F2FS_IOC_GETFLAGS:
598 flags = fi->i_flags & FS_FL_USER_VISIBLE;
599 return put_user(flags, (int __user *) arg);
600 case F2FS_IOC_SETFLAGS:
602 unsigned int oldflags;
604 ret = mnt_want_write_file(filp);
605 if (ret)
606 return ret;
608 if (!inode_owner_or_capable(inode)) {
609 ret = -EACCES;
610 goto out;
613 if (get_user(flags, (int __user *) arg)) {
614 ret = -EFAULT;
615 goto out;
618 flags = f2fs_mask_flags(inode->i_mode, flags);
620 mutex_lock(&inode->i_mutex);
622 oldflags = fi->i_flags;
624 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
625 if (!capable(CAP_LINUX_IMMUTABLE)) {
626 mutex_unlock(&inode->i_mutex);
627 ret = -EPERM;
628 goto out;
632 flags = flags & FS_FL_USER_MODIFIABLE;
633 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
634 fi->i_flags = flags;
635 mutex_unlock(&inode->i_mutex);
637 f2fs_set_inode_flags(inode);
638 inode->i_ctime = CURRENT_TIME;
639 mark_inode_dirty(inode);
640 out:
641 mnt_drop_write_file(filp);
642 return ret;
644 default:
645 return -ENOTTY;
649 #ifdef CONFIG_COMPAT
650 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
652 switch (cmd) {
653 case F2FS_IOC32_GETFLAGS:
654 cmd = F2FS_IOC_GETFLAGS;
655 break;
656 case F2FS_IOC32_SETFLAGS:
657 cmd = F2FS_IOC_SETFLAGS;
658 break;
659 default:
660 return -ENOIOCTLCMD;
662 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
664 #endif
666 const struct file_operations f2fs_file_operations = {
667 .llseek = generic_file_llseek,
668 .read = do_sync_read,
669 .write = do_sync_write,
670 .aio_read = generic_file_aio_read,
671 .aio_write = generic_file_aio_write,
672 .open = generic_file_open,
673 .mmap = f2fs_file_mmap,
674 .fsync = f2fs_sync_file,
675 .fallocate = f2fs_fallocate,
676 .unlocked_ioctl = f2fs_ioctl,
677 #ifdef CONFIG_COMPAT
678 .compat_ioctl = f2fs_compat_ioctl,
679 #endif
680 .splice_read = generic_file_splice_read,
681 .splice_write = generic_file_splice_write,