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
);
37 struct dnode_of_data dn
;
42 sb_start_pagefault(inode
->i_sb
);
44 /* block allocation */
45 ilock
= mutex_lock_op(sbi
);
46 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
47 err
= get_dnode_of_data(&dn
, page
->index
, ALLOC_NODE
);
49 mutex_unlock_op(sbi
, ilock
);
53 old_blk_addr
= dn
.data_blkaddr
;
55 if (old_blk_addr
== NULL_ADDR
) {
56 err
= reserve_new_block(&dn
);
59 mutex_unlock_op(sbi
, ilock
);
64 mutex_unlock_op(sbi
, ilock
);
66 file_update_time(vma
->vm_file
);
68 if (page
->mapping
!= inode
->i_mapping
||
69 page_offset(page
) > i_size_read(inode
) ||
70 !PageUptodate(page
)) {
77 * check to see if the page is mapped already (no holes)
79 if (PageMappedToDisk(page
))
82 /* page is wholly or partially inside EOF */
83 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > i_size_read(inode
)) {
85 offset
= i_size_read(inode
) & ~PAGE_CACHE_MASK
;
86 zero_user_segment(page
, offset
, PAGE_CACHE_SIZE
);
89 SetPageUptodate(page
);
93 wait_on_page_writeback(page
);
95 sb_end_pagefault(inode
->i_sb
);
96 return block_page_mkwrite_return(err
);
99 static const struct vm_operations_struct f2fs_file_vm_ops
= {
100 .fault
= filemap_fault
,
101 .page_mkwrite
= f2fs_vm_page_mkwrite
,
102 .remap_pages
= generic_file_remap_pages
,
105 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
107 struct dentry
*dentry
;
109 inode
= igrab(inode
);
110 dentry
= d_find_any_alias(inode
);
115 inode
= igrab(dentry
->d_parent
->d_inode
);
118 *pino
= inode
->i_ino
;
123 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
125 struct inode
*inode
= file
->f_mapping
->host
;
126 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
128 bool need_cp
= false;
129 struct writeback_control wbc
= {
130 .sync_mode
= WB_SYNC_ALL
,
131 .nr_to_write
= LONG_MAX
,
135 if (f2fs_readonly(inode
->i_sb
))
138 trace_f2fs_sync_file_enter(inode
);
139 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
141 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
145 /* guarantee free sections for fsync */
146 f2fs_balance_fs(sbi
);
148 mutex_lock(&inode
->i_mutex
);
150 if (datasync
&& !(inode
->i_state
& I_DIRTY_DATASYNC
))
153 if (!S_ISREG(inode
->i_mode
) || inode
->i_nlink
!= 1)
155 else if (file_wrong_pino(inode
))
157 else if (!space_for_roll_forward(sbi
))
159 else if (!is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
165 /* all the dirty node pages should be flushed for POR */
166 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
167 if (file_wrong_pino(inode
) && inode
->i_nlink
== 1 &&
168 get_parent_ino(inode
, &pino
)) {
169 F2FS_I(inode
)->i_pino
= pino
;
170 file_got_pino(inode
);
171 mark_inode_dirty_sync(inode
);
172 ret
= f2fs_write_inode(inode
, NULL
);
177 /* if there is no written node page, write its inode page */
178 while (!sync_node_pages(sbi
, inode
->i_ino
, &wbc
)) {
179 mark_inode_dirty_sync(inode
);
180 ret
= f2fs_write_inode(inode
, NULL
);
184 filemap_fdatawait_range(sbi
->node_inode
->i_mapping
,
186 ret
= blkdev_issue_flush(inode
->i_sb
->s_bdev
, GFP_KERNEL
, NULL
);
189 mutex_unlock(&inode
->i_mutex
);
190 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
194 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
197 vma
->vm_ops
= &f2fs_file_vm_ops
;
201 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
203 int nr_free
= 0, ofs
= dn
->ofs_in_node
;
204 struct f2fs_sb_info
*sbi
= F2FS_SB(dn
->inode
->i_sb
);
205 struct f2fs_node
*raw_node
;
208 raw_node
= page_address(dn
->node_page
);
209 addr
= blkaddr_in_node(raw_node
) + ofs
;
211 for ( ; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
212 block_t blkaddr
= le32_to_cpu(*addr
);
213 if (blkaddr
== NULL_ADDR
)
216 update_extent_cache(NULL_ADDR
, dn
);
217 invalidate_blocks(sbi
, blkaddr
);
221 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
222 set_page_dirty(dn
->node_page
);
225 dn
->ofs_in_node
= ofs
;
227 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
228 dn
->ofs_in_node
, nr_free
);
232 void truncate_data_blocks(struct dnode_of_data
*dn
)
234 truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK
);
237 static void truncate_partial_data_page(struct inode
*inode
, u64 from
)
239 unsigned offset
= from
& (PAGE_CACHE_SIZE
- 1);
245 page
= find_data_page(inode
, from
>> PAGE_CACHE_SHIFT
, false);
250 if (page
->mapping
!= inode
->i_mapping
) {
251 f2fs_put_page(page
, 1);
254 wait_on_page_writeback(page
);
255 zero_user(page
, offset
, PAGE_CACHE_SIZE
- offset
);
256 set_page_dirty(page
);
257 f2fs_put_page(page
, 1);
260 static int truncate_blocks(struct inode
*inode
, u64 from
)
262 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
263 unsigned int blocksize
= inode
->i_sb
->s_blocksize
;
264 struct dnode_of_data dn
;
266 int count
= 0, ilock
= -1;
269 trace_f2fs_truncate_blocks_enter(inode
, from
);
271 free_from
= (pgoff_t
)
272 ((from
+ blocksize
- 1) >> (sbi
->log_blocksize
));
274 ilock
= mutex_lock_op(sbi
);
275 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
276 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE
);
280 mutex_unlock_op(sbi
, ilock
);
281 trace_f2fs_truncate_blocks_exit(inode
, err
);
285 if (IS_INODE(dn
.node_page
))
286 count
= ADDRS_PER_INODE
;
288 count
= ADDRS_PER_BLOCK
;
290 count
-= dn
.ofs_in_node
;
293 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
294 truncate_data_blocks_range(&dn
, count
);
300 err
= truncate_inode_blocks(inode
, free_from
);
301 mutex_unlock_op(sbi
, ilock
);
303 /* lastly zero out the first data page */
304 truncate_partial_data_page(inode
, from
);
306 trace_f2fs_truncate_blocks_exit(inode
, err
);
310 void f2fs_truncate(struct inode
*inode
)
312 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
313 S_ISLNK(inode
->i_mode
)))
316 trace_f2fs_truncate(inode
);
318 if (!truncate_blocks(inode
, i_size_read(inode
))) {
319 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
320 mark_inode_dirty(inode
);
324 int f2fs_getattr(struct vfsmount
*mnt
,
325 struct dentry
*dentry
, struct kstat
*stat
)
327 struct inode
*inode
= dentry
->d_inode
;
328 generic_fillattr(inode
, stat
);
333 #ifdef CONFIG_F2FS_FS_POSIX_ACL
334 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
336 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
337 unsigned int ia_valid
= attr
->ia_valid
;
339 if (ia_valid
& ATTR_UID
)
340 inode
->i_uid
= attr
->ia_uid
;
341 if (ia_valid
& ATTR_GID
)
342 inode
->i_gid
= attr
->ia_gid
;
343 if (ia_valid
& ATTR_ATIME
)
344 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
345 inode
->i_sb
->s_time_gran
);
346 if (ia_valid
& ATTR_MTIME
)
347 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
348 inode
->i_sb
->s_time_gran
);
349 if (ia_valid
& ATTR_CTIME
)
350 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
351 inode
->i_sb
->s_time_gran
);
352 if (ia_valid
& ATTR_MODE
) {
353 umode_t mode
= attr
->ia_mode
;
355 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
357 set_acl_inode(fi
, mode
);
361 #define __setattr_copy setattr_copy
364 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
366 struct inode
*inode
= dentry
->d_inode
;
367 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
370 err
= inode_change_ok(inode
, attr
);
374 if ((attr
->ia_valid
& ATTR_SIZE
) &&
375 attr
->ia_size
!= i_size_read(inode
)) {
376 truncate_setsize(inode
, attr
->ia_size
);
377 f2fs_truncate(inode
);
378 f2fs_balance_fs(F2FS_SB(inode
->i_sb
));
381 __setattr_copy(inode
, attr
);
383 if (attr
->ia_valid
& ATTR_MODE
) {
384 err
= f2fs_acl_chmod(inode
);
385 if (err
|| is_inode_flag_set(fi
, FI_ACL_MODE
)) {
386 inode
->i_mode
= fi
->i_acl_mode
;
387 clear_inode_flag(fi
, FI_ACL_MODE
);
391 mark_inode_dirty(inode
);
395 const struct inode_operations f2fs_file_inode_operations
= {
396 .getattr
= f2fs_getattr
,
397 .setattr
= f2fs_setattr
,
398 .get_acl
= f2fs_get_acl
,
399 #ifdef CONFIG_F2FS_FS_XATTR
400 .setxattr
= generic_setxattr
,
401 .getxattr
= generic_getxattr
,
402 .listxattr
= f2fs_listxattr
,
403 .removexattr
= generic_removexattr
,
407 static void fill_zero(struct inode
*inode
, pgoff_t index
,
408 loff_t start
, loff_t len
)
410 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
417 f2fs_balance_fs(sbi
);
419 ilock
= mutex_lock_op(sbi
);
420 page
= get_new_data_page(inode
, NULL
, index
, false);
421 mutex_unlock_op(sbi
, ilock
);
424 wait_on_page_writeback(page
);
425 zero_user(page
, start
, len
);
426 set_page_dirty(page
);
427 f2fs_put_page(page
, 1);
431 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
436 for (index
= pg_start
; index
< pg_end
; index
++) {
437 struct dnode_of_data dn
;
439 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
440 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
447 if (dn
.data_blkaddr
!= NULL_ADDR
)
448 truncate_data_blocks_range(&dn
, 1);
454 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
, int mode
)
456 pgoff_t pg_start
, pg_end
;
457 loff_t off_start
, off_end
;
460 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
461 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
463 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
464 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
466 if (pg_start
== pg_end
) {
467 fill_zero(inode
, pg_start
, off_start
,
468 off_end
- off_start
);
471 fill_zero(inode
, pg_start
++, off_start
,
472 PAGE_CACHE_SIZE
- off_start
);
474 fill_zero(inode
, pg_end
, 0, off_end
);
476 if (pg_start
< pg_end
) {
477 struct address_space
*mapping
= inode
->i_mapping
;
478 loff_t blk_start
, blk_end
;
479 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
482 f2fs_balance_fs(sbi
);
484 blk_start
= pg_start
<< PAGE_CACHE_SHIFT
;
485 blk_end
= pg_end
<< PAGE_CACHE_SHIFT
;
486 truncate_inode_pages_range(mapping
, blk_start
,
489 ilock
= mutex_lock_op(sbi
);
490 ret
= truncate_hole(inode
, pg_start
, pg_end
);
491 mutex_unlock_op(sbi
, ilock
);
495 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
496 i_size_read(inode
) <= (offset
+ len
)) {
497 i_size_write(inode
, offset
);
498 mark_inode_dirty(inode
);
504 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
505 loff_t len
, int mode
)
507 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
508 pgoff_t index
, pg_start
, pg_end
;
509 loff_t new_size
= i_size_read(inode
);
510 loff_t off_start
, off_end
;
513 ret
= inode_newsize_ok(inode
, (len
+ offset
));
517 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
518 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
520 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
521 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
523 for (index
= pg_start
; index
<= pg_end
; index
++) {
524 struct dnode_of_data dn
;
527 ilock
= mutex_lock_op(sbi
);
528 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
529 ret
= get_dnode_of_data(&dn
, index
, ALLOC_NODE
);
531 mutex_unlock_op(sbi
, ilock
);
535 if (dn
.data_blkaddr
== NULL_ADDR
) {
536 ret
= reserve_new_block(&dn
);
539 mutex_unlock_op(sbi
, ilock
);
544 mutex_unlock_op(sbi
, ilock
);
546 if (pg_start
== pg_end
)
547 new_size
= offset
+ len
;
548 else if (index
== pg_start
&& off_start
)
549 new_size
= (index
+ 1) << PAGE_CACHE_SHIFT
;
550 else if (index
== pg_end
)
551 new_size
= (index
<< PAGE_CACHE_SHIFT
) + off_end
;
553 new_size
+= PAGE_CACHE_SIZE
;
556 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
557 i_size_read(inode
) < new_size
) {
558 i_size_write(inode
, new_size
);
559 mark_inode_dirty(inode
);
565 static long f2fs_fallocate(struct file
*file
, int mode
,
566 loff_t offset
, loff_t len
)
568 struct inode
*inode
= file_inode(file
);
571 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
574 if (mode
& FALLOC_FL_PUNCH_HOLE
)
575 ret
= punch_hole(inode
, offset
, len
, mode
);
577 ret
= expand_inode_data(inode
, offset
, len
, mode
);
580 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
581 mark_inode_dirty(inode
);
583 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
587 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
588 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
590 static inline __u32
f2fs_mask_flags(umode_t mode
, __u32 flags
)
594 else if (S_ISREG(mode
))
595 return flags
& F2FS_REG_FLMASK
;
597 return flags
& F2FS_OTHER_FLMASK
;
600 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
602 struct inode
*inode
= file_inode(filp
);
603 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
608 case F2FS_IOC_GETFLAGS
:
609 flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
610 return put_user(flags
, (int __user
*) arg
);
611 case F2FS_IOC_SETFLAGS
:
613 unsigned int oldflags
;
615 ret
= mnt_want_write_file(filp
);
619 if (!inode_owner_or_capable(inode
)) {
624 if (get_user(flags
, (int __user
*) arg
)) {
629 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
631 mutex_lock(&inode
->i_mutex
);
633 oldflags
= fi
->i_flags
;
635 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
636 if (!capable(CAP_LINUX_IMMUTABLE
)) {
637 mutex_unlock(&inode
->i_mutex
);
643 flags
= flags
& FS_FL_USER_MODIFIABLE
;
644 flags
|= oldflags
& ~FS_FL_USER_MODIFIABLE
;
646 mutex_unlock(&inode
->i_mutex
);
648 f2fs_set_inode_flags(inode
);
649 inode
->i_ctime
= CURRENT_TIME
;
650 mark_inode_dirty(inode
);
652 mnt_drop_write_file(filp
);
661 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
664 case F2FS_IOC32_GETFLAGS
:
665 cmd
= F2FS_IOC_GETFLAGS
;
667 case F2FS_IOC32_SETFLAGS
:
668 cmd
= F2FS_IOC_SETFLAGS
;
673 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
677 const struct file_operations f2fs_file_operations
= {
678 .llseek
= generic_file_llseek
,
679 .read
= do_sync_read
,
680 .write
= do_sync_write
,
681 .aio_read
= generic_file_aio_read
,
682 .aio_write
= generic_file_aio_write
,
683 .open
= generic_file_open
,
684 .mmap
= f2fs_file_mmap
,
685 .fsync
= f2fs_sync_file
,
686 .fallocate
= f2fs_fallocate
,
687 .unlocked_ioctl
= f2fs_ioctl
,
689 .compat_ioctl
= f2fs_compat_ioctl
,
691 .splice_read
= generic_file_splice_read
,
692 .splice_write
= generic_file_splice_write
,