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>
22 #include <linux/pagevec.h>
29 #include <trace/events/f2fs.h>
31 static int f2fs_vm_page_mkwrite(struct vm_area_struct
*vma
,
34 struct page
*page
= vmf
->page
;
35 struct inode
*inode
= file_inode(vma
->vm_file
);
36 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
37 struct dnode_of_data dn
;
42 sb_start_pagefault(inode
->i_sb
);
44 /* block allocation */
46 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
47 err
= f2fs_reserve_block(&dn
, page
->index
);
52 file_update_time(vma
->vm_file
);
54 if (unlikely(page
->mapping
!= inode
->i_mapping
||
55 page_offset(page
) > i_size_read(inode
) ||
56 !PageUptodate(page
))) {
63 * check to see if the page is mapped already (no holes)
65 if (PageMappedToDisk(page
))
68 /* page is wholly or partially inside EOF */
69 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > i_size_read(inode
)) {
71 offset
= i_size_read(inode
) & ~PAGE_CACHE_MASK
;
72 zero_user_segment(page
, offset
, PAGE_CACHE_SIZE
);
75 SetPageUptodate(page
);
77 trace_f2fs_vm_page_mkwrite(page
, DATA
);
80 f2fs_wait_on_page_writeback(page
, DATA
);
82 sb_end_pagefault(inode
->i_sb
);
83 return block_page_mkwrite_return(err
);
86 static const struct vm_operations_struct f2fs_file_vm_ops
= {
87 .fault
= filemap_fault
,
88 .map_pages
= filemap_map_pages
,
89 .page_mkwrite
= f2fs_vm_page_mkwrite
,
90 .remap_pages
= generic_file_remap_pages
,
93 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
95 struct dentry
*dentry
;
98 dentry
= d_find_any_alias(inode
);
103 if (update_dent_inode(inode
, &dentry
->d_name
)) {
108 *pino
= parent_ino(dentry
);
113 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
115 struct inode
*inode
= file
->f_mapping
->host
;
116 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
117 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
119 bool need_cp
= false;
120 struct writeback_control wbc
= {
121 .sync_mode
= WB_SYNC_ALL
,
122 .nr_to_write
= LONG_MAX
,
126 if (unlikely(f2fs_readonly(inode
->i_sb
)))
129 trace_f2fs_sync_file_enter(inode
);
130 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
132 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
136 /* guarantee free sections for fsync */
137 f2fs_balance_fs(sbi
);
139 down_read(&fi
->i_sem
);
142 * Both of fdatasync() and fsync() are able to be recovered from
145 if (!S_ISREG(inode
->i_mode
) || inode
->i_nlink
!= 1)
147 else if (file_wrong_pino(inode
))
149 else if (!space_for_roll_forward(sbi
))
151 else if (!is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
153 else if (F2FS_I(inode
)->xattr_ver
== cur_cp_version(F2FS_CKPT(sbi
)))
161 /* all the dirty node pages should be flushed for POR */
162 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
164 down_write(&fi
->i_sem
);
165 F2FS_I(inode
)->xattr_ver
= 0;
166 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
);
170 up_write(&fi
->i_sem
);
171 mark_inode_dirty_sync(inode
);
172 ret
= f2fs_write_inode(inode
, NULL
);
176 up_write(&fi
->i_sem
);
179 /* if there is no written node page, write its inode page */
180 while (!sync_node_pages(sbi
, inode
->i_ino
, &wbc
)) {
181 if (fsync_mark_done(sbi
, inode
->i_ino
))
183 mark_inode_dirty_sync(inode
);
184 ret
= f2fs_write_inode(inode
, NULL
);
188 ret
= wait_on_node_pages_writeback(sbi
, inode
->i_ino
);
191 ret
= f2fs_issue_flush(F2FS_SB(inode
->i_sb
));
194 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
198 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
199 pgoff_t pgofs
, int whence
)
204 if (whence
!= SEEK_DATA
)
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
);
215 static bool __found_offset(block_t blkaddr
, pgoff_t dirty
, pgoff_t pgofs
,
220 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
221 (blkaddr
!= NEW_ADDR
&& blkaddr
!= NULL_ADDR
))
225 if (blkaddr
== NULL_ADDR
)
232 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
234 struct inode
*inode
= file
->f_mapping
->host
;
235 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
236 struct dnode_of_data dn
;
237 pgoff_t pgofs
, end_offset
, dirty
;
238 loff_t data_ofs
= offset
;
242 mutex_lock(&inode
->i_mutex
);
244 isize
= i_size_read(inode
);
248 /* handle inline data case */
249 if (f2fs_has_inline_data(inode
)) {
250 if (whence
== SEEK_HOLE
)
255 pgofs
= (pgoff_t
)(offset
>> PAGE_CACHE_SHIFT
);
257 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
259 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
) {
264 } else if (err
== -ENOENT
) {
265 /* direct node is not exist */
266 if (whence
== SEEK_DATA
) {
267 pgofs
= PGOFS_OF_NEXT_DNODE(pgofs
,
275 end_offset
= IS_INODE(dn
.node_page
) ?
276 ADDRS_PER_INODE(F2FS_I(inode
)) : ADDRS_PER_BLOCK
;
278 /* find data/hole in dnode block */
279 for (; dn
.ofs_in_node
< end_offset
;
280 dn
.ofs_in_node
++, pgofs
++,
281 data_ofs
= pgofs
<< PAGE_CACHE_SHIFT
) {
283 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
285 if (__found_offset(blkaddr
, dirty
, pgofs
, whence
)) {
293 if (whence
== SEEK_DATA
)
296 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
298 mutex_unlock(&inode
->i_mutex
);
299 return vfs_setpos(file
, data_ofs
, maxbytes
);
301 mutex_unlock(&inode
->i_mutex
);
305 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
307 struct inode
*inode
= file
->f_mapping
->host
;
308 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
314 return generic_file_llseek_size(file
, offset
, whence
,
315 maxbytes
, i_size_read(inode
));
318 return f2fs_seek_block(file
, offset
, whence
);
324 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
327 vma
->vm_ops
= &f2fs_file_vm_ops
;
331 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
333 int nr_free
= 0, ofs
= dn
->ofs_in_node
;
334 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
335 struct f2fs_node
*raw_node
;
338 raw_node
= F2FS_NODE(dn
->node_page
);
339 addr
= blkaddr_in_node(raw_node
) + ofs
;
341 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
342 block_t blkaddr
= le32_to_cpu(*addr
);
343 if (blkaddr
== NULL_ADDR
)
346 update_extent_cache(NULL_ADDR
, dn
);
347 invalidate_blocks(sbi
, blkaddr
);
351 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
352 set_page_dirty(dn
->node_page
);
355 dn
->ofs_in_node
= ofs
;
357 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
358 dn
->ofs_in_node
, nr_free
);
362 void truncate_data_blocks(struct dnode_of_data
*dn
)
364 truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK
);
367 static void truncate_partial_data_page(struct inode
*inode
, u64 from
)
369 unsigned offset
= from
& (PAGE_CACHE_SIZE
- 1);
372 if (f2fs_has_inline_data(inode
))
373 return truncate_inline_data(inode
, from
);
378 page
= find_data_page(inode
, from
>> PAGE_CACHE_SHIFT
, false);
383 if (unlikely(page
->mapping
!= inode
->i_mapping
)) {
384 f2fs_put_page(page
, 1);
387 f2fs_wait_on_page_writeback(page
, DATA
);
388 zero_user(page
, offset
, PAGE_CACHE_SIZE
- offset
);
389 set_page_dirty(page
);
390 f2fs_put_page(page
, 1);
393 int truncate_blocks(struct inode
*inode
, u64 from
)
395 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
396 unsigned int blocksize
= inode
->i_sb
->s_blocksize
;
397 struct dnode_of_data dn
;
399 int count
= 0, err
= 0;
401 trace_f2fs_truncate_blocks_enter(inode
, from
);
403 if (f2fs_has_inline_data(inode
))
406 free_from
= (pgoff_t
)
407 ((from
+ blocksize
- 1) >> (sbi
->log_blocksize
));
411 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
412 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE
);
417 trace_f2fs_truncate_blocks_exit(inode
, err
);
421 count
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
423 count
-= dn
.ofs_in_node
;
424 f2fs_bug_on(count
< 0);
426 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
427 truncate_data_blocks_range(&dn
, count
);
433 err
= truncate_inode_blocks(inode
, free_from
);
436 /* lastly zero out the first data page */
437 truncate_partial_data_page(inode
, from
);
439 trace_f2fs_truncate_blocks_exit(inode
, err
);
443 void f2fs_truncate(struct inode
*inode
)
445 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
446 S_ISLNK(inode
->i_mode
)))
449 trace_f2fs_truncate(inode
);
451 if (!truncate_blocks(inode
, i_size_read(inode
))) {
452 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
453 mark_inode_dirty(inode
);
457 int f2fs_getattr(struct vfsmount
*mnt
,
458 struct dentry
*dentry
, struct kstat
*stat
)
460 struct inode
*inode
= dentry
->d_inode
;
461 generic_fillattr(inode
, stat
);
466 #ifdef CONFIG_F2FS_FS_POSIX_ACL
467 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
469 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
470 unsigned int ia_valid
= attr
->ia_valid
;
472 if (ia_valid
& ATTR_UID
)
473 inode
->i_uid
= attr
->ia_uid
;
474 if (ia_valid
& ATTR_GID
)
475 inode
->i_gid
= attr
->ia_gid
;
476 if (ia_valid
& ATTR_ATIME
)
477 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
478 inode
->i_sb
->s_time_gran
);
479 if (ia_valid
& ATTR_MTIME
)
480 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
481 inode
->i_sb
->s_time_gran
);
482 if (ia_valid
& ATTR_CTIME
)
483 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
484 inode
->i_sb
->s_time_gran
);
485 if (ia_valid
& ATTR_MODE
) {
486 umode_t mode
= attr
->ia_mode
;
488 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
490 set_acl_inode(fi
, mode
);
494 #define __setattr_copy setattr_copy
497 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
499 struct inode
*inode
= dentry
->d_inode
;
500 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
503 err
= inode_change_ok(inode
, attr
);
507 if ((attr
->ia_valid
& ATTR_SIZE
) &&
508 attr
->ia_size
!= i_size_read(inode
)) {
509 err
= f2fs_convert_inline_data(inode
, attr
->ia_size
);
513 truncate_setsize(inode
, attr
->ia_size
);
514 f2fs_truncate(inode
);
515 f2fs_balance_fs(F2FS_SB(inode
->i_sb
));
518 __setattr_copy(inode
, attr
);
520 if (attr
->ia_valid
& ATTR_MODE
) {
521 err
= posix_acl_chmod(inode
, get_inode_mode(inode
));
522 if (err
|| is_inode_flag_set(fi
, FI_ACL_MODE
)) {
523 inode
->i_mode
= fi
->i_acl_mode
;
524 clear_inode_flag(fi
, FI_ACL_MODE
);
528 mark_inode_dirty(inode
);
532 const struct inode_operations f2fs_file_inode_operations
= {
533 .getattr
= f2fs_getattr
,
534 .setattr
= f2fs_setattr
,
535 .get_acl
= f2fs_get_acl
,
536 .set_acl
= f2fs_set_acl
,
537 #ifdef CONFIG_F2FS_FS_XATTR
538 .setxattr
= generic_setxattr
,
539 .getxattr
= generic_getxattr
,
540 .listxattr
= f2fs_listxattr
,
541 .removexattr
= generic_removexattr
,
543 .fiemap
= f2fs_fiemap
,
546 static void fill_zero(struct inode
*inode
, pgoff_t index
,
547 loff_t start
, loff_t len
)
549 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
555 f2fs_balance_fs(sbi
);
558 page
= get_new_data_page(inode
, NULL
, index
, false);
562 f2fs_wait_on_page_writeback(page
, DATA
);
563 zero_user(page
, start
, len
);
564 set_page_dirty(page
);
565 f2fs_put_page(page
, 1);
569 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
574 for (index
= pg_start
; index
< pg_end
; index
++) {
575 struct dnode_of_data dn
;
577 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
578 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
585 if (dn
.data_blkaddr
!= NULL_ADDR
)
586 truncate_data_blocks_range(&dn
, 1);
592 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
594 pgoff_t pg_start
, pg_end
;
595 loff_t off_start
, off_end
;
598 ret
= f2fs_convert_inline_data(inode
, MAX_INLINE_DATA
+ 1);
602 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
603 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
605 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
606 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
608 if (pg_start
== pg_end
) {
609 fill_zero(inode
, pg_start
, off_start
,
610 off_end
- off_start
);
613 fill_zero(inode
, pg_start
++, off_start
,
614 PAGE_CACHE_SIZE
- off_start
);
616 fill_zero(inode
, pg_end
, 0, off_end
);
618 if (pg_start
< pg_end
) {
619 struct address_space
*mapping
= inode
->i_mapping
;
620 loff_t blk_start
, blk_end
;
621 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
623 f2fs_balance_fs(sbi
);
625 blk_start
= pg_start
<< PAGE_CACHE_SHIFT
;
626 blk_end
= pg_end
<< PAGE_CACHE_SHIFT
;
627 truncate_inode_pages_range(mapping
, blk_start
,
631 ret
= truncate_hole(inode
, pg_start
, pg_end
);
639 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
640 loff_t len
, int mode
)
642 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
643 pgoff_t index
, pg_start
, pg_end
;
644 loff_t new_size
= i_size_read(inode
);
645 loff_t off_start
, off_end
;
648 ret
= inode_newsize_ok(inode
, (len
+ offset
));
652 ret
= f2fs_convert_inline_data(inode
, offset
+ len
);
656 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
657 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
659 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
660 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
664 for (index
= pg_start
; index
<= pg_end
; index
++) {
665 struct dnode_of_data dn
;
667 if (index
== pg_end
&& !off_end
)
670 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
671 ret
= f2fs_reserve_block(&dn
, index
);
675 if (pg_start
== pg_end
)
676 new_size
= offset
+ len
;
677 else if (index
== pg_start
&& off_start
)
678 new_size
= (index
+ 1) << PAGE_CACHE_SHIFT
;
679 else if (index
== pg_end
)
680 new_size
= (index
<< PAGE_CACHE_SHIFT
) + off_end
;
682 new_size
+= PAGE_CACHE_SIZE
;
685 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
686 i_size_read(inode
) < new_size
) {
687 i_size_write(inode
, new_size
);
688 mark_inode_dirty(inode
);
689 update_inode_page(inode
);
696 static long f2fs_fallocate(struct file
*file
, int mode
,
697 loff_t offset
, loff_t len
)
699 struct inode
*inode
= file_inode(file
);
702 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
705 mutex_lock(&inode
->i_mutex
);
707 if (mode
& FALLOC_FL_PUNCH_HOLE
)
708 ret
= punch_hole(inode
, offset
, len
);
710 ret
= expand_inode_data(inode
, offset
, len
, mode
);
713 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
714 mark_inode_dirty(inode
);
717 mutex_unlock(&inode
->i_mutex
);
719 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
723 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
724 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
726 static inline __u32
f2fs_mask_flags(umode_t mode
, __u32 flags
)
730 else if (S_ISREG(mode
))
731 return flags
& F2FS_REG_FLMASK
;
733 return flags
& F2FS_OTHER_FLMASK
;
736 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
738 struct inode
*inode
= file_inode(filp
);
739 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
744 case F2FS_IOC_GETFLAGS
:
745 flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
746 return put_user(flags
, (int __user
*) arg
);
747 case F2FS_IOC_SETFLAGS
:
749 unsigned int oldflags
;
751 ret
= mnt_want_write_file(filp
);
755 if (!inode_owner_or_capable(inode
)) {
760 if (get_user(flags
, (int __user
*) arg
)) {
765 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
767 mutex_lock(&inode
->i_mutex
);
769 oldflags
= fi
->i_flags
;
771 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
772 if (!capable(CAP_LINUX_IMMUTABLE
)) {
773 mutex_unlock(&inode
->i_mutex
);
779 flags
= flags
& FS_FL_USER_MODIFIABLE
;
780 flags
|= oldflags
& ~FS_FL_USER_MODIFIABLE
;
782 mutex_unlock(&inode
->i_mutex
);
784 f2fs_set_inode_flags(inode
);
785 inode
->i_ctime
= CURRENT_TIME
;
786 mark_inode_dirty(inode
);
788 mnt_drop_write_file(filp
);
797 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
800 case F2FS_IOC32_GETFLAGS
:
801 cmd
= F2FS_IOC_GETFLAGS
;
803 case F2FS_IOC32_SETFLAGS
:
804 cmd
= F2FS_IOC_SETFLAGS
;
809 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
813 const struct file_operations f2fs_file_operations
= {
814 .llseek
= f2fs_llseek
,
815 .read
= new_sync_read
,
816 .write
= new_sync_write
,
817 .read_iter
= generic_file_read_iter
,
818 .write_iter
= generic_file_write_iter
,
819 .open
= generic_file_open
,
820 .mmap
= f2fs_file_mmap
,
821 .fsync
= f2fs_sync_file
,
822 .fallocate
= f2fs_fallocate
,
823 .unlocked_ioctl
= f2fs_ioctl
,
825 .compat_ioctl
= f2fs_compat_ioctl
,
827 .splice_read
= generic_file_splice_read
,
828 .splice_write
= iter_file_splice_write
,