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.
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>
28 #include <trace/events/f2fs.h>
30 static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
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;
41 sb_start_pagefault(inode->i_sb);
43 /* block allocation */
45 set_new_dnode(&dn, inode, NULL, NULL, 0);
46 err = f2fs_reserve_block(&dn, page->index);
51 file_update_time(vma->vm_file);
53 if (unlikely(page->mapping != inode->i_mapping ||
54 page_offset(page) > i_size_read(inode) ||
55 !PageUptodate(page))) {
62 * check to see if the page is mapped already (no holes)
64 if (PageMappedToDisk(page))
67 /* page is wholly or partially inside EOF */
68 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
70 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
71 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
74 SetPageUptodate(page);
76 trace_f2fs_vm_page_mkwrite(page, DATA);
79 wait_on_page_writeback(page);
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;
96 dentry = d_find_any_alias(inode);
101 if (update_dent_inode(inode, &dentry->d_name)) {
106 *pino = parent_ino(dentry);
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);
116 bool need_cp = false;
117 struct writeback_control wbc = {
118 .sync_mode = WB_SYNC_ALL,
119 .nr_to_write = LONG_MAX,
123 if (unlikely(f2fs_readonly(inode->i_sb)))
126 trace_f2fs_sync_file_enter(inode);
127 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
129 trace_f2fs_sync_file_exit(inode, need_cp, datasync, 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
142 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
144 else if (file_wrong_pino(inode))
146 else if (!space_for_roll_forward(sbi))
148 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
150 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
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);
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);
177 ret = wait_on_node_pages_writeback(sbi, inode->i_ino);
180 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
183 mutex_unlock(&inode->i_mutex);
184 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
188 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
191 vma->vm_ops = &f2fs_file_vm_ops;
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;
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)
210 update_extent_cache(NULL_ADDR, dn);
211 invalidate_blocks(sbi, blkaddr);
215 dec_valid_block_count(sbi, dn->inode, nr_free);
216 set_page_dirty(dn->node_page);
219 dn->ofs_in_node = ofs;
221 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
222 dn->ofs_in_node, 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);
239 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
244 if (unlikely(page->mapping != inode->i_mapping)) {
245 f2fs_put_page(page, 1);
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 static 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;
260 int count = 0, err = 0;
262 trace_f2fs_truncate_blocks_enter(inode, from);
264 free_from = (pgoff_t)
265 ((from + blocksize - 1) >> (sbi->log_blocksize));
269 if (f2fs_has_inline_data(inode))
272 set_new_dnode(&dn, inode, NULL, NULL, 0);
273 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
278 trace_f2fs_truncate_blocks_exit(inode, err);
282 if (IS_INODE(dn.node_page))
283 count = ADDRS_PER_INODE(F2FS_I(inode));
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);
297 err = truncate_inode_blocks(inode, free_from);
301 /* lastly zero out the first data page */
302 truncate_partial_data_page(inode, from);
304 trace_f2fs_truncate_blocks_exit(inode, err);
308 void f2fs_truncate(struct inode *inode)
310 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
311 S_ISLNK(inode->i_mode)))
314 trace_f2fs_truncate(inode);
316 if (!truncate_blocks(inode, i_size_read(inode))) {
317 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
318 mark_inode_dirty(inode);
322 int f2fs_getattr(struct vfsmount *mnt,
323 struct dentry *dentry, struct kstat *stat)
325 struct inode *inode = dentry->d_inode;
326 generic_fillattr(inode, stat);
331 #ifdef CONFIG_F2FS_FS_POSIX_ACL
332 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
334 struct f2fs_inode_info *fi = F2FS_I(inode);
335 unsigned int ia_valid = attr->ia_valid;
337 if (ia_valid & ATTR_UID)
338 inode->i_uid = attr->ia_uid;
339 if (ia_valid & ATTR_GID)
340 inode->i_gid = attr->ia_gid;
341 if (ia_valid & ATTR_ATIME)
342 inode->i_atime = timespec_trunc(attr->ia_atime,
343 inode->i_sb->s_time_gran);
344 if (ia_valid & ATTR_MTIME)
345 inode->i_mtime = timespec_trunc(attr->ia_mtime,
346 inode->i_sb->s_time_gran);
347 if (ia_valid & ATTR_CTIME)
348 inode->i_ctime = timespec_trunc(attr->ia_ctime,
349 inode->i_sb->s_time_gran);
350 if (ia_valid & ATTR_MODE) {
351 umode_t mode = attr->ia_mode;
353 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
355 set_acl_inode(fi, mode);
359 #define __setattr_copy setattr_copy
362 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
364 struct inode *inode = dentry->d_inode;
365 struct f2fs_inode_info *fi = F2FS_I(inode);
368 err = inode_change_ok(inode, attr);
372 if ((attr->ia_valid & ATTR_SIZE) &&
373 attr->ia_size != i_size_read(inode)) {
374 if (f2fs_has_inline_data(inode) &&
375 (attr->ia_size > MAX_INLINE_DATA)) {
376 unsigned flags = AOP_FLAG_NOFS;
377 err = f2fs_convert_inline_data(inode, NULL, flags);
382 truncate_setsize(inode, attr->ia_size);
383 if (!f2fs_has_inline_data(inode))
384 f2fs_truncate(inode);
385 f2fs_balance_fs(F2FS_SB(inode->i_sb));
388 __setattr_copy(inode, attr);
390 if (attr->ia_valid & ATTR_MODE) {
391 err = f2fs_acl_chmod(inode);
392 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
393 inode->i_mode = fi->i_acl_mode;
394 clear_inode_flag(fi, FI_ACL_MODE);
398 mark_inode_dirty(inode);
402 const struct inode_operations f2fs_file_inode_operations = {
403 .getattr = f2fs_getattr,
404 .setattr = f2fs_setattr,
405 .get_acl = f2fs_get_acl,
406 #ifdef CONFIG_F2FS_FS_XATTR
407 .setxattr = generic_setxattr,
408 .getxattr = generic_getxattr,
409 .listxattr = f2fs_listxattr,
410 .removexattr = generic_removexattr,
414 static void fill_zero(struct inode *inode, pgoff_t index,
415 loff_t start, loff_t len)
417 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
423 f2fs_balance_fs(sbi);
426 page = get_new_data_page(inode, NULL, index, false);
430 wait_on_page_writeback(page);
431 zero_user(page, start, len);
432 set_page_dirty(page);
433 f2fs_put_page(page, 1);
437 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
442 for (index = pg_start; index < pg_end; index++) {
443 struct dnode_of_data dn;
445 set_new_dnode(&dn, inode, NULL, NULL, 0);
446 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
453 if (dn.data_blkaddr != NULL_ADDR)
454 truncate_data_blocks_range(&dn, 1);
460 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
462 pgoff_t pg_start, pg_end;
463 loff_t off_start, off_end;
466 if (f2fs_has_inline_data(inode)) {
468 unsigned flags = AOP_FLAG_NOFS;
469 page = grab_cache_page_write_begin(inode->i_mapping, 0, flags);
471 return PTR_ERR(page);
472 if (offset + len > MAX_INLINE_DATA) {
473 ret = f2fs_convert_inline_data(inode, page, flags);
474 f2fs_put_page(page, 1);
478 zero_user_segment(page, offset, offset + len);
479 SetPageUptodate(page);
480 set_page_dirty(page);
481 f2fs_put_page(page, 1);
486 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
487 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
489 off_start = offset & (PAGE_CACHE_SIZE - 1);
490 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
492 if (pg_start == pg_end) {
493 fill_zero(inode, pg_start, off_start,
494 off_end - off_start);
497 fill_zero(inode, pg_start++, off_start,
498 PAGE_CACHE_SIZE - off_start);
500 fill_zero(inode, pg_end, 0, off_end);
502 if (pg_start < pg_end) {
503 struct address_space *mapping = inode->i_mapping;
504 loff_t blk_start, blk_end;
505 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
507 f2fs_balance_fs(sbi);
509 blk_start = pg_start << PAGE_CACHE_SHIFT;
510 blk_end = pg_end << PAGE_CACHE_SHIFT;
511 truncate_inode_pages_range(mapping, blk_start,
515 ret = truncate_hole(inode, pg_start, pg_end);
523 static int expand_inode_data(struct inode *inode, loff_t offset,
524 loff_t len, int mode)
526 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
527 pgoff_t index, pg_start, pg_end;
528 loff_t new_size = i_size_read(inode);
529 loff_t off_start, off_end;
532 if (f2fs_has_inline_data(inode) && (offset + len > MAX_INLINE_DATA)) {
533 ret = f2fs_convert_inline_data(inode, NULL, AOP_FLAG_NOFS);
538 ret = inode_newsize_ok(inode, (len + offset));
542 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
543 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
545 off_start = offset & (PAGE_CACHE_SIZE - 1);
546 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
548 for (index = pg_start; index <= pg_end; index++) {
549 struct dnode_of_data dn;
552 set_new_dnode(&dn, inode, NULL, NULL, 0);
553 ret = f2fs_reserve_block(&dn, index);
558 if (pg_start == pg_end)
559 new_size = offset + len;
560 else if (index == pg_start && off_start)
561 new_size = (index + 1) << PAGE_CACHE_SHIFT;
562 else if (index == pg_end)
563 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
565 new_size += PAGE_CACHE_SIZE;
568 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
569 i_size_read(inode) < new_size) {
570 i_size_write(inode, new_size);
571 mark_inode_dirty(inode);
577 static long f2fs_fallocate(struct file *file, int mode,
578 loff_t offset, loff_t len)
580 struct inode *inode = file_inode(file);
583 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
586 if (mode & FALLOC_FL_PUNCH_HOLE)
587 ret = punch_hole(inode, offset, len);
589 ret = expand_inode_data(inode, offset, len, mode);
592 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
593 mark_inode_dirty(inode);
595 trace_f2fs_fallocate(inode, mode, offset, len, ret);
599 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
600 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
602 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
606 else if (S_ISREG(mode))
607 return flags & F2FS_REG_FLMASK;
609 return flags & F2FS_OTHER_FLMASK;
612 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
614 struct inode *inode = file_inode(filp);
615 struct f2fs_inode_info *fi = F2FS_I(inode);
620 case F2FS_IOC_GETFLAGS:
621 flags = fi->i_flags & FS_FL_USER_VISIBLE;
622 return put_user(flags, (int __user *) arg);
623 case F2FS_IOC_SETFLAGS:
625 unsigned int oldflags;
627 ret = mnt_want_write_file(filp);
631 if (!inode_owner_or_capable(inode)) {
636 if (get_user(flags, (int __user *) arg)) {
641 flags = f2fs_mask_flags(inode->i_mode, flags);
643 mutex_lock(&inode->i_mutex);
645 oldflags = fi->i_flags;
647 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
648 if (!capable(CAP_LINUX_IMMUTABLE)) {
649 mutex_unlock(&inode->i_mutex);
655 flags = flags & FS_FL_USER_MODIFIABLE;
656 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
658 mutex_unlock(&inode->i_mutex);
660 f2fs_set_inode_flags(inode);
661 inode->i_ctime = CURRENT_TIME;
662 mark_inode_dirty(inode);
664 mnt_drop_write_file(filp);
673 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
676 case F2FS_IOC32_GETFLAGS:
677 cmd = F2FS_IOC_GETFLAGS;
679 case F2FS_IOC32_SETFLAGS:
680 cmd = F2FS_IOC_SETFLAGS;
685 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
689 const struct file_operations f2fs_file_operations = {
690 .llseek = generic_file_llseek,
691 .read = do_sync_read,
692 .write = do_sync_write,
693 .aio_read = generic_file_aio_read,
694 .aio_write = generic_file_aio_write,
695 .open = generic_file_open,
696 .mmap = f2fs_file_mmap,
697 .fsync = f2fs_sync_file,
698 .fallocate = f2fs_fallocate,
699 .unlocked_ioctl = f2fs_ioctl,
701 .compat_ioctl = f2fs_compat_ioctl,
703 .splice_read = generic_file_splice_read,
704 .splice_write = generic_file_splice_write,