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>
23 #include <linux/random.h>
31 #include <trace/events/f2fs.h>
33 static int f2fs_vm_page_mkwrite(struct vm_area_struct
*vma
,
36 struct page
*page
= vmf
->page
;
37 struct inode
*inode
= file_inode(vma
->vm_file
);
38 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
39 struct dnode_of_data dn
;
44 sb_start_pagefault(inode
->i_sb
);
46 f2fs_bug_on(sbi
, f2fs_has_inline_data(inode
));
48 /* block allocation */
50 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
51 err
= f2fs_reserve_block(&dn
, page
->index
);
59 file_update_time(vma
->vm_file
);
61 if (unlikely(page
->mapping
!= inode
->i_mapping
||
62 page_offset(page
) > i_size_read(inode
) ||
63 !PageUptodate(page
))) {
70 * check to see if the page is mapped already (no holes)
72 if (PageMappedToDisk(page
))
75 /* page is wholly or partially inside EOF */
76 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > i_size_read(inode
)) {
78 offset
= i_size_read(inode
) & ~PAGE_CACHE_MASK
;
79 zero_user_segment(page
, offset
, PAGE_CACHE_SIZE
);
82 SetPageUptodate(page
);
84 trace_f2fs_vm_page_mkwrite(page
, DATA
);
87 f2fs_wait_on_page_writeback(page
, DATA
);
89 sb_end_pagefault(inode
->i_sb
);
90 return block_page_mkwrite_return(err
);
93 static const struct vm_operations_struct f2fs_file_vm_ops
= {
94 .fault
= filemap_fault
,
95 .map_pages
= filemap_map_pages
,
96 .page_mkwrite
= f2fs_vm_page_mkwrite
,
99 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
101 struct dentry
*dentry
;
103 inode
= igrab(inode
);
104 dentry
= d_find_any_alias(inode
);
109 if (update_dent_inode(inode
, inode
, &dentry
->d_name
)) {
114 *pino
= parent_ino(dentry
);
119 static inline bool need_do_checkpoint(struct inode
*inode
)
121 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
122 bool need_cp
= false;
124 if (!S_ISREG(inode
->i_mode
) || inode
->i_nlink
!= 1)
126 else if (file_enc_name(inode
) && need_dentry_mark(sbi
, inode
->i_ino
))
128 else if (file_wrong_pino(inode
))
130 else if (!space_for_roll_forward(sbi
))
132 else if (!is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
134 else if (F2FS_I(inode
)->xattr_ver
== cur_cp_version(F2FS_CKPT(sbi
)))
136 else if (test_opt(sbi
, FASTBOOT
))
138 else if (sbi
->active_logs
== 2)
144 static bool need_inode_page_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
146 struct page
*i
= find_get_page(NODE_MAPPING(sbi
), ino
);
148 /* But we need to avoid that there are some inode updates */
149 if ((i
&& PageDirty(i
)) || need_inode_block_update(sbi
, ino
))
155 static void try_to_fix_pino(struct inode
*inode
)
157 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
160 down_write(&fi
->i_sem
);
162 if (file_wrong_pino(inode
) && inode
->i_nlink
== 1 &&
163 get_parent_ino(inode
, &pino
)) {
165 file_got_pino(inode
);
166 up_write(&fi
->i_sem
);
168 mark_inode_dirty_sync(inode
);
169 f2fs_write_inode(inode
, NULL
);
171 up_write(&fi
->i_sem
);
175 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
177 struct inode
*inode
= file
->f_mapping
->host
;
178 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
179 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
180 nid_t ino
= inode
->i_ino
;
182 bool need_cp
= false;
183 struct writeback_control wbc
= {
184 .sync_mode
= WB_SYNC_ALL
,
185 .nr_to_write
= LONG_MAX
,
189 if (unlikely(f2fs_readonly(inode
->i_sb
)))
192 trace_f2fs_sync_file_enter(inode
);
194 /* if fdatasync is triggered, let's do in-place-update */
195 if (get_dirty_pages(inode
) <= SM_I(sbi
)->min_fsync_blocks
)
196 set_inode_flag(fi
, FI_NEED_IPU
);
197 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
198 clear_inode_flag(fi
, FI_NEED_IPU
);
201 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
205 /* if the inode is dirty, let's recover all the time */
206 if (!datasync
&& is_inode_flag_set(fi
, FI_DIRTY_INODE
)) {
207 update_inode_page(inode
);
212 * if there is no written data, don't waste time to write recovery info.
214 if (!is_inode_flag_set(fi
, FI_APPEND_WRITE
) &&
215 !exist_written_data(sbi
, ino
, APPEND_INO
)) {
217 /* it may call write_inode just prior to fsync */
218 if (need_inode_page_update(sbi
, ino
))
221 if (is_inode_flag_set(fi
, FI_UPDATE_WRITE
) ||
222 exist_written_data(sbi
, ino
, UPDATE_INO
))
227 /* guarantee free sections for fsync */
228 f2fs_balance_fs(sbi
);
231 * Both of fdatasync() and fsync() are able to be recovered from
234 down_read(&fi
->i_sem
);
235 need_cp
= need_do_checkpoint(inode
);
239 /* all the dirty node pages should be flushed for POR */
240 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
243 * We've secured consistency through sync_fs. Following pino
244 * will be used only for fsynced inodes after checkpoint.
246 try_to_fix_pino(inode
);
247 clear_inode_flag(fi
, FI_APPEND_WRITE
);
248 clear_inode_flag(fi
, FI_UPDATE_WRITE
);
252 sync_node_pages(sbi
, ino
, &wbc
);
254 /* if cp_error was enabled, we should avoid infinite loop */
255 if (unlikely(f2fs_cp_error(sbi
)))
258 if (need_inode_block_update(sbi
, ino
)) {
259 mark_inode_dirty_sync(inode
);
260 f2fs_write_inode(inode
, NULL
);
264 ret
= wait_on_node_pages_writeback(sbi
, ino
);
268 /* once recovery info is written, don't need to tack this */
269 remove_dirty_inode(sbi
, ino
, APPEND_INO
);
270 clear_inode_flag(fi
, FI_APPEND_WRITE
);
272 remove_dirty_inode(sbi
, ino
, UPDATE_INO
);
273 clear_inode_flag(fi
, FI_UPDATE_WRITE
);
274 ret
= f2fs_issue_flush(sbi
);
276 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
277 f2fs_trace_ios(NULL
, 1);
281 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
282 pgoff_t pgofs
, int whence
)
287 if (whence
!= SEEK_DATA
)
290 /* find first dirty page index */
291 pagevec_init(&pvec
, 0);
292 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &pgofs
,
293 PAGECACHE_TAG_DIRTY
, 1);
294 pgofs
= nr_pages
? pvec
.pages
[0]->index
: LONG_MAX
;
295 pagevec_release(&pvec
);
299 static bool __found_offset(block_t blkaddr
, pgoff_t dirty
, pgoff_t pgofs
,
304 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
305 (blkaddr
!= NEW_ADDR
&& blkaddr
!= NULL_ADDR
))
309 if (blkaddr
== NULL_ADDR
)
316 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
318 struct inode
*inode
= file
->f_mapping
->host
;
319 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
320 struct dnode_of_data dn
;
321 pgoff_t pgofs
, end_offset
, dirty
;
322 loff_t data_ofs
= offset
;
326 mutex_lock(&inode
->i_mutex
);
328 isize
= i_size_read(inode
);
332 /* handle inline data case */
333 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
334 if (whence
== SEEK_HOLE
)
339 pgofs
= (pgoff_t
)(offset
>> PAGE_CACHE_SHIFT
);
341 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
343 for (; data_ofs
< isize
; data_ofs
= pgofs
<< PAGE_CACHE_SHIFT
) {
344 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
345 err
= get_dnode_of_data(&dn
, pgofs
, LOOKUP_NODE_RA
);
346 if (err
&& err
!= -ENOENT
) {
348 } else if (err
== -ENOENT
) {
349 /* direct node does not exists */
350 if (whence
== SEEK_DATA
) {
351 pgofs
= PGOFS_OF_NEXT_DNODE(pgofs
,
359 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
361 /* find data/hole in dnode block */
362 for (; dn
.ofs_in_node
< end_offset
;
363 dn
.ofs_in_node
++, pgofs
++,
364 data_ofs
= (loff_t
)pgofs
<< PAGE_CACHE_SHIFT
) {
366 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
368 if (__found_offset(blkaddr
, dirty
, pgofs
, whence
)) {
376 if (whence
== SEEK_DATA
)
379 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
381 mutex_unlock(&inode
->i_mutex
);
382 return vfs_setpos(file
, data_ofs
, maxbytes
);
384 mutex_unlock(&inode
->i_mutex
);
388 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
390 struct inode
*inode
= file
->f_mapping
->host
;
391 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
397 return generic_file_llseek_size(file
, offset
, whence
,
398 maxbytes
, i_size_read(inode
));
403 return f2fs_seek_block(file
, offset
, whence
);
409 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
411 struct inode
*inode
= file_inode(file
);
413 if (f2fs_encrypted_inode(inode
)) {
414 int err
= f2fs_get_encryption_info(inode
);
419 /* we don't need to use inline_data strictly */
420 if (f2fs_has_inline_data(inode
)) {
421 int err
= f2fs_convert_inline_inode(inode
);
427 vma
->vm_ops
= &f2fs_file_vm_ops
;
431 static int f2fs_file_open(struct inode
*inode
, struct file
*filp
)
433 int ret
= generic_file_open(inode
, filp
);
435 if (!ret
&& f2fs_encrypted_inode(inode
)) {
436 ret
= f2fs_get_encryption_info(inode
);
443 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
445 int nr_free
= 0, ofs
= dn
->ofs_in_node
;
446 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
447 struct f2fs_node
*raw_node
;
450 raw_node
= F2FS_NODE(dn
->node_page
);
451 addr
= blkaddr_in_node(raw_node
) + ofs
;
453 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
454 block_t blkaddr
= le32_to_cpu(*addr
);
455 if (blkaddr
== NULL_ADDR
)
458 dn
->data_blkaddr
= NULL_ADDR
;
459 set_data_blkaddr(dn
);
460 f2fs_update_extent_cache(dn
);
461 invalidate_blocks(sbi
, blkaddr
);
462 if (dn
->ofs_in_node
== 0 && IS_INODE(dn
->node_page
))
463 clear_inode_flag(F2FS_I(dn
->inode
),
464 FI_FIRST_BLOCK_WRITTEN
);
468 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
469 set_page_dirty(dn
->node_page
);
472 dn
->ofs_in_node
= ofs
;
474 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
475 dn
->ofs_in_node
, nr_free
);
479 void truncate_data_blocks(struct dnode_of_data
*dn
)
481 truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK
);
484 static int truncate_partial_data_page(struct inode
*inode
, u64 from
,
487 unsigned offset
= from
& (PAGE_CACHE_SIZE
- 1);
488 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
489 struct address_space
*mapping
= inode
->i_mapping
;
492 if (!offset
&& !cache_only
)
496 page
= grab_cache_page(mapping
, index
);
497 if (page
&& PageUptodate(page
))
499 f2fs_put_page(page
, 1);
503 page
= get_lock_data_page(inode
, index
);
507 f2fs_wait_on_page_writeback(page
, DATA
);
508 zero_user(page
, offset
, PAGE_CACHE_SIZE
- offset
);
509 if (!cache_only
|| !f2fs_encrypted_inode(inode
) || !S_ISREG(inode
->i_mode
))
510 set_page_dirty(page
);
511 f2fs_put_page(page
, 1);
515 int truncate_blocks(struct inode
*inode
, u64 from
, bool lock
)
517 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
518 unsigned int blocksize
= inode
->i_sb
->s_blocksize
;
519 struct dnode_of_data dn
;
521 int count
= 0, err
= 0;
523 bool truncate_page
= false;
525 trace_f2fs_truncate_blocks_enter(inode
, from
);
527 free_from
= (pgoff_t
)F2FS_BYTES_TO_BLK(from
+ blocksize
- 1);
532 ipage
= get_node_page(sbi
, inode
->i_ino
);
534 err
= PTR_ERR(ipage
);
538 if (f2fs_has_inline_data(inode
)) {
539 if (truncate_inline_inode(ipage
, from
))
540 set_page_dirty(ipage
);
541 f2fs_put_page(ipage
, 1);
542 truncate_page
= true;
546 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
547 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE
);
554 count
= ADDRS_PER_PAGE(dn
.node_page
, F2FS_I(inode
));
556 count
-= dn
.ofs_in_node
;
557 f2fs_bug_on(sbi
, count
< 0);
559 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
560 truncate_data_blocks_range(&dn
, count
);
566 err
= truncate_inode_blocks(inode
, free_from
);
571 /* lastly zero out the first data page */
573 err
= truncate_partial_data_page(inode
, from
, truncate_page
);
575 trace_f2fs_truncate_blocks_exit(inode
, err
);
579 void f2fs_truncate(struct inode
*inode
)
581 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
582 S_ISLNK(inode
->i_mode
)))
585 trace_f2fs_truncate(inode
);
587 /* we should check inline_data size */
588 if (f2fs_has_inline_data(inode
) && !f2fs_may_inline_data(inode
)) {
589 if (f2fs_convert_inline_inode(inode
))
593 if (!truncate_blocks(inode
, i_size_read(inode
), true)) {
594 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
595 mark_inode_dirty(inode
);
599 int f2fs_getattr(struct vfsmount
*mnt
,
600 struct dentry
*dentry
, struct kstat
*stat
)
602 struct inode
*inode
= d_inode(dentry
);
603 generic_fillattr(inode
, stat
);
608 #ifdef CONFIG_F2FS_FS_POSIX_ACL
609 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
611 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
612 unsigned int ia_valid
= attr
->ia_valid
;
614 if (ia_valid
& ATTR_UID
)
615 inode
->i_uid
= attr
->ia_uid
;
616 if (ia_valid
& ATTR_GID
)
617 inode
->i_gid
= attr
->ia_gid
;
618 if (ia_valid
& ATTR_ATIME
)
619 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
620 inode
->i_sb
->s_time_gran
);
621 if (ia_valid
& ATTR_MTIME
)
622 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
623 inode
->i_sb
->s_time_gran
);
624 if (ia_valid
& ATTR_CTIME
)
625 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
626 inode
->i_sb
->s_time_gran
);
627 if (ia_valid
& ATTR_MODE
) {
628 umode_t mode
= attr
->ia_mode
;
630 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
632 set_acl_inode(fi
, mode
);
636 #define __setattr_copy setattr_copy
639 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
641 struct inode
*inode
= d_inode(dentry
);
642 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
645 err
= inode_change_ok(inode
, attr
);
649 if (attr
->ia_valid
& ATTR_SIZE
) {
650 if (f2fs_encrypted_inode(inode
) &&
651 f2fs_get_encryption_info(inode
))
654 if (attr
->ia_size
<= i_size_read(inode
)) {
655 truncate_setsize(inode
, attr
->ia_size
);
656 f2fs_truncate(inode
);
657 f2fs_balance_fs(F2FS_I_SB(inode
));
660 * do not trim all blocks after i_size if target size is
661 * larger than i_size.
663 truncate_setsize(inode
, attr
->ia_size
);
667 __setattr_copy(inode
, attr
);
669 if (attr
->ia_valid
& ATTR_MODE
) {
670 err
= posix_acl_chmod(inode
, get_inode_mode(inode
));
671 if (err
|| is_inode_flag_set(fi
, FI_ACL_MODE
)) {
672 inode
->i_mode
= fi
->i_acl_mode
;
673 clear_inode_flag(fi
, FI_ACL_MODE
);
677 mark_inode_dirty(inode
);
681 const struct inode_operations f2fs_file_inode_operations
= {
682 .getattr
= f2fs_getattr
,
683 .setattr
= f2fs_setattr
,
684 .get_acl
= f2fs_get_acl
,
685 .set_acl
= f2fs_set_acl
,
686 #ifdef CONFIG_F2FS_FS_XATTR
687 .setxattr
= generic_setxattr
,
688 .getxattr
= generic_getxattr
,
689 .listxattr
= f2fs_listxattr
,
690 .removexattr
= generic_removexattr
,
692 .fiemap
= f2fs_fiemap
,
695 static void fill_zero(struct inode
*inode
, pgoff_t index
,
696 loff_t start
, loff_t len
)
698 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
704 f2fs_balance_fs(sbi
);
707 page
= get_new_data_page(inode
, NULL
, index
, false);
711 f2fs_wait_on_page_writeback(page
, DATA
);
712 zero_user(page
, start
, len
);
713 set_page_dirty(page
);
714 f2fs_put_page(page
, 1);
718 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
723 for (index
= pg_start
; index
< pg_end
; index
++) {
724 struct dnode_of_data dn
;
726 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
727 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
734 if (dn
.data_blkaddr
!= NULL_ADDR
)
735 truncate_data_blocks_range(&dn
, 1);
741 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
743 pgoff_t pg_start
, pg_end
;
744 loff_t off_start
, off_end
;
747 if (!S_ISREG(inode
->i_mode
))
750 if (f2fs_has_inline_data(inode
)) {
751 ret
= f2fs_convert_inline_inode(inode
);
756 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
757 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
759 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
760 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
762 if (pg_start
== pg_end
) {
763 fill_zero(inode
, pg_start
, off_start
,
764 off_end
- off_start
);
767 fill_zero(inode
, pg_start
++, off_start
,
768 PAGE_CACHE_SIZE
- off_start
);
770 fill_zero(inode
, pg_end
, 0, off_end
);
772 if (pg_start
< pg_end
) {
773 struct address_space
*mapping
= inode
->i_mapping
;
774 loff_t blk_start
, blk_end
;
775 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
777 f2fs_balance_fs(sbi
);
779 blk_start
= pg_start
<< PAGE_CACHE_SHIFT
;
780 blk_end
= pg_end
<< PAGE_CACHE_SHIFT
;
781 truncate_inode_pages_range(mapping
, blk_start
,
785 ret
= truncate_hole(inode
, pg_start
, pg_end
);
793 static int f2fs_do_collapse(struct inode
*inode
, pgoff_t start
, pgoff_t end
)
795 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
796 struct dnode_of_data dn
;
797 pgoff_t nrpages
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
802 for (; end
< nrpages
; start
++, end
++) {
803 block_t new_addr
, old_addr
;
805 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
806 ret
= get_dnode_of_data(&dn
, end
, LOOKUP_NODE_RA
);
807 if (ret
&& ret
!= -ENOENT
) {
809 } else if (ret
== -ENOENT
) {
810 new_addr
= NULL_ADDR
;
812 new_addr
= dn
.data_blkaddr
;
813 truncate_data_blocks_range(&dn
, 1);
817 if (new_addr
== NULL_ADDR
) {
818 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
819 ret
= get_dnode_of_data(&dn
, start
, LOOKUP_NODE_RA
);
820 if (ret
&& ret
!= -ENOENT
)
822 else if (ret
== -ENOENT
)
825 if (dn
.data_blkaddr
== NULL_ADDR
) {
829 truncate_data_blocks_range(&dn
, 1);
836 ipage
= get_node_page(sbi
, inode
->i_ino
);
838 ret
= PTR_ERR(ipage
);
842 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
843 ret
= f2fs_reserve_block(&dn
, start
);
847 old_addr
= dn
.data_blkaddr
;
848 if (old_addr
!= NEW_ADDR
&& new_addr
== NEW_ADDR
) {
849 dn
.data_blkaddr
= NULL_ADDR
;
850 f2fs_update_extent_cache(&dn
);
851 invalidate_blocks(sbi
, old_addr
);
853 dn
.data_blkaddr
= new_addr
;
854 set_data_blkaddr(&dn
);
855 } else if (new_addr
!= NEW_ADDR
) {
858 get_node_info(sbi
, dn
.nid
, &ni
);
859 f2fs_replace_block(sbi
, &dn
, old_addr
, new_addr
,
872 static int f2fs_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
874 pgoff_t pg_start
, pg_end
;
878 if (!S_ISREG(inode
->i_mode
))
881 if (offset
+ len
>= i_size_read(inode
))
884 /* collapse range should be aligned to block size of f2fs. */
885 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
888 pg_start
= offset
>> PAGE_CACHE_SHIFT
;
889 pg_end
= (offset
+ len
) >> PAGE_CACHE_SHIFT
;
891 /* write out all dirty pages from offset */
892 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
896 truncate_pagecache(inode
, offset
);
898 ret
= f2fs_do_collapse(inode
, pg_start
, pg_end
);
902 new_size
= i_size_read(inode
) - len
;
904 ret
= truncate_blocks(inode
, new_size
, true);
906 i_size_write(inode
, new_size
);
911 static int f2fs_zero_range(struct inode
*inode
, loff_t offset
, loff_t len
,
914 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
915 struct address_space
*mapping
= inode
->i_mapping
;
916 pgoff_t index
, pg_start
, pg_end
;
917 loff_t new_size
= i_size_read(inode
);
918 loff_t off_start
, off_end
;
921 if (!S_ISREG(inode
->i_mode
))
924 ret
= inode_newsize_ok(inode
, (len
+ offset
));
928 f2fs_balance_fs(sbi
);
930 if (f2fs_has_inline_data(inode
)) {
931 ret
= f2fs_convert_inline_inode(inode
);
936 ret
= filemap_write_and_wait_range(mapping
, offset
, offset
+ len
- 1);
940 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
942 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
943 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
945 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
946 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
948 if (pg_start
== pg_end
) {
949 fill_zero(inode
, pg_start
, off_start
, off_end
- off_start
);
950 if (offset
+ len
> new_size
)
951 new_size
= offset
+ len
;
952 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
955 fill_zero(inode
, pg_start
++, off_start
,
956 PAGE_CACHE_SIZE
- off_start
);
957 new_size
= max_t(loff_t
, new_size
,
958 pg_start
<< PAGE_CACHE_SHIFT
);
961 for (index
= pg_start
; index
< pg_end
; index
++) {
962 struct dnode_of_data dn
;
967 ipage
= get_node_page(sbi
, inode
->i_ino
);
969 ret
= PTR_ERR(ipage
);
974 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
975 ret
= f2fs_reserve_block(&dn
, index
);
981 if (dn
.data_blkaddr
!= NEW_ADDR
) {
982 invalidate_blocks(sbi
, dn
.data_blkaddr
);
984 dn
.data_blkaddr
= NEW_ADDR
;
985 set_data_blkaddr(&dn
);
987 dn
.data_blkaddr
= NULL_ADDR
;
988 f2fs_update_extent_cache(&dn
);
993 new_size
= max_t(loff_t
, new_size
,
994 (index
+ 1) << PAGE_CACHE_SHIFT
);
998 fill_zero(inode
, pg_end
, 0, off_end
);
999 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1004 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && i_size_read(inode
) < new_size
) {
1005 i_size_write(inode
, new_size
);
1006 mark_inode_dirty(inode
);
1007 update_inode_page(inode
);
1013 static int f2fs_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1015 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1016 pgoff_t pg_start
, pg_end
, delta
, nrpages
, idx
;
1020 if (!S_ISREG(inode
->i_mode
))
1023 new_size
= i_size_read(inode
) + len
;
1024 if (new_size
> inode
->i_sb
->s_maxbytes
)
1027 if (offset
>= i_size_read(inode
))
1030 /* insert range should be aligned to block size of f2fs. */
1031 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1034 f2fs_balance_fs(sbi
);
1036 ret
= truncate_blocks(inode
, i_size_read(inode
), true);
1040 /* write out all dirty pages from offset */
1041 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1045 truncate_pagecache(inode
, offset
);
1047 pg_start
= offset
>> PAGE_CACHE_SHIFT
;
1048 pg_end
= (offset
+ len
) >> PAGE_CACHE_SHIFT
;
1049 delta
= pg_end
- pg_start
;
1050 nrpages
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1052 for (idx
= nrpages
- 1; idx
>= pg_start
&& idx
!= -1; idx
--) {
1053 struct dnode_of_data dn
;
1055 block_t new_addr
, old_addr
;
1059 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1060 ret
= get_dnode_of_data(&dn
, idx
, LOOKUP_NODE_RA
);
1061 if (ret
&& ret
!= -ENOENT
) {
1063 } else if (ret
== -ENOENT
) {
1065 } else if (dn
.data_blkaddr
== NULL_ADDR
) {
1066 f2fs_put_dnode(&dn
);
1069 new_addr
= dn
.data_blkaddr
;
1070 truncate_data_blocks_range(&dn
, 1);
1071 f2fs_put_dnode(&dn
);
1074 ipage
= get_node_page(sbi
, inode
->i_ino
);
1075 if (IS_ERR(ipage
)) {
1076 ret
= PTR_ERR(ipage
);
1080 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
1081 ret
= f2fs_reserve_block(&dn
, idx
+ delta
);
1085 old_addr
= dn
.data_blkaddr
;
1086 f2fs_bug_on(sbi
, old_addr
!= NEW_ADDR
);
1088 if (new_addr
!= NEW_ADDR
) {
1089 struct node_info ni
;
1091 get_node_info(sbi
, dn
.nid
, &ni
);
1092 f2fs_replace_block(sbi
, &dn
, old_addr
, new_addr
,
1095 f2fs_put_dnode(&dn
);
1097 f2fs_unlock_op(sbi
);
1100 i_size_write(inode
, new_size
);
1103 f2fs_unlock_op(sbi
);
1107 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
1108 loff_t len
, int mode
)
1110 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1111 pgoff_t index
, pg_start
, pg_end
;
1112 loff_t new_size
= i_size_read(inode
);
1113 loff_t off_start
, off_end
;
1116 f2fs_balance_fs(sbi
);
1118 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1122 if (f2fs_has_inline_data(inode
)) {
1123 ret
= f2fs_convert_inline_inode(inode
);
1128 pg_start
= ((unsigned long long) offset
) >> PAGE_CACHE_SHIFT
;
1129 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_CACHE_SHIFT
;
1131 off_start
= offset
& (PAGE_CACHE_SIZE
- 1);
1132 off_end
= (offset
+ len
) & (PAGE_CACHE_SIZE
- 1);
1136 for (index
= pg_start
; index
<= pg_end
; index
++) {
1137 struct dnode_of_data dn
;
1139 if (index
== pg_end
&& !off_end
)
1142 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1143 ret
= f2fs_reserve_block(&dn
, index
);
1147 if (pg_start
== pg_end
)
1148 new_size
= offset
+ len
;
1149 else if (index
== pg_start
&& off_start
)
1150 new_size
= (index
+ 1) << PAGE_CACHE_SHIFT
;
1151 else if (index
== pg_end
)
1152 new_size
= (index
<< PAGE_CACHE_SHIFT
) + off_end
;
1154 new_size
+= PAGE_CACHE_SIZE
;
1157 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
1158 i_size_read(inode
) < new_size
) {
1159 i_size_write(inode
, new_size
);
1160 mark_inode_dirty(inode
);
1161 update_inode_page(inode
);
1163 f2fs_unlock_op(sbi
);
1168 static long f2fs_fallocate(struct file
*file
, int mode
,
1169 loff_t offset
, loff_t len
)
1171 struct inode
*inode
= file_inode(file
);
1174 if (f2fs_encrypted_inode(inode
) &&
1175 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
)))
1178 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
1179 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
1180 FALLOC_FL_INSERT_RANGE
))
1183 mutex_lock(&inode
->i_mutex
);
1185 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
1186 if (offset
>= inode
->i_size
)
1189 ret
= punch_hole(inode
, offset
, len
);
1190 } else if (mode
& FALLOC_FL_COLLAPSE_RANGE
) {
1191 ret
= f2fs_collapse_range(inode
, offset
, len
);
1192 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
1193 ret
= f2fs_zero_range(inode
, offset
, len
, mode
);
1194 } else if (mode
& FALLOC_FL_INSERT_RANGE
) {
1195 ret
= f2fs_insert_range(inode
, offset
, len
);
1197 ret
= expand_inode_data(inode
, offset
, len
, mode
);
1201 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1202 mark_inode_dirty(inode
);
1206 mutex_unlock(&inode
->i_mutex
);
1208 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
1212 static int f2fs_release_file(struct inode
*inode
, struct file
*filp
)
1214 /* some remained atomic pages should discarded */
1215 if (f2fs_is_atomic_file(inode
))
1216 commit_inmem_pages(inode
, true);
1217 if (f2fs_is_volatile_file(inode
)) {
1218 set_inode_flag(F2FS_I(inode
), FI_DROP_CACHE
);
1219 filemap_fdatawrite(inode
->i_mapping
);
1220 clear_inode_flag(F2FS_I(inode
), FI_DROP_CACHE
);
1225 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1226 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1228 static inline __u32
f2fs_mask_flags(umode_t mode
, __u32 flags
)
1232 else if (S_ISREG(mode
))
1233 return flags
& F2FS_REG_FLMASK
;
1235 return flags
& F2FS_OTHER_FLMASK
;
1238 static int f2fs_ioc_getflags(struct file
*filp
, unsigned long arg
)
1240 struct inode
*inode
= file_inode(filp
);
1241 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1242 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
1243 return put_user(flags
, (int __user
*)arg
);
1246 static int f2fs_ioc_setflags(struct file
*filp
, unsigned long arg
)
1248 struct inode
*inode
= file_inode(filp
);
1249 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1250 unsigned int flags
= fi
->i_flags
& FS_FL_USER_VISIBLE
;
1251 unsigned int oldflags
;
1254 ret
= mnt_want_write_file(filp
);
1258 if (!inode_owner_or_capable(inode
)) {
1263 if (get_user(flags
, (int __user
*)arg
)) {
1268 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
1270 mutex_lock(&inode
->i_mutex
);
1272 oldflags
= fi
->i_flags
;
1274 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
1275 if (!capable(CAP_LINUX_IMMUTABLE
)) {
1276 mutex_unlock(&inode
->i_mutex
);
1282 flags
= flags
& FS_FL_USER_MODIFIABLE
;
1283 flags
|= oldflags
& ~FS_FL_USER_MODIFIABLE
;
1284 fi
->i_flags
= flags
;
1285 mutex_unlock(&inode
->i_mutex
);
1287 f2fs_set_inode_flags(inode
);
1288 inode
->i_ctime
= CURRENT_TIME
;
1289 mark_inode_dirty(inode
);
1291 mnt_drop_write_file(filp
);
1295 static int f2fs_ioc_getversion(struct file
*filp
, unsigned long arg
)
1297 struct inode
*inode
= file_inode(filp
);
1299 return put_user(inode
->i_generation
, (int __user
*)arg
);
1302 static int f2fs_ioc_start_atomic_write(struct file
*filp
)
1304 struct inode
*inode
= file_inode(filp
);
1306 if (!inode_owner_or_capable(inode
))
1309 f2fs_balance_fs(F2FS_I_SB(inode
));
1311 if (f2fs_is_atomic_file(inode
))
1314 set_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
1316 return f2fs_convert_inline_inode(inode
);
1319 static int f2fs_ioc_commit_atomic_write(struct file
*filp
)
1321 struct inode
*inode
= file_inode(filp
);
1324 if (!inode_owner_or_capable(inode
))
1327 if (f2fs_is_volatile_file(inode
))
1330 ret
= mnt_want_write_file(filp
);
1334 if (f2fs_is_atomic_file(inode
))
1335 commit_inmem_pages(inode
, false);
1337 ret
= f2fs_sync_file(filp
, 0, LONG_MAX
, 0);
1338 mnt_drop_write_file(filp
);
1339 clear_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
1343 static int f2fs_ioc_start_volatile_write(struct file
*filp
)
1345 struct inode
*inode
= file_inode(filp
);
1347 if (!inode_owner_or_capable(inode
))
1350 if (f2fs_is_volatile_file(inode
))
1353 set_inode_flag(F2FS_I(inode
), FI_VOLATILE_FILE
);
1355 return f2fs_convert_inline_inode(inode
);
1358 static int f2fs_ioc_release_volatile_write(struct file
*filp
)
1360 struct inode
*inode
= file_inode(filp
);
1362 if (!inode_owner_or_capable(inode
))
1365 if (!f2fs_is_volatile_file(inode
))
1368 if (!f2fs_is_first_block_written(inode
))
1369 return truncate_partial_data_page(inode
, 0, true);
1371 punch_hole(inode
, 0, F2FS_BLKSIZE
);
1375 static int f2fs_ioc_abort_volatile_write(struct file
*filp
)
1377 struct inode
*inode
= file_inode(filp
);
1380 if (!inode_owner_or_capable(inode
))
1383 ret
= mnt_want_write_file(filp
);
1387 f2fs_balance_fs(F2FS_I_SB(inode
));
1389 if (f2fs_is_atomic_file(inode
)) {
1390 commit_inmem_pages(inode
, false);
1391 clear_inode_flag(F2FS_I(inode
), FI_ATOMIC_FILE
);
1394 if (f2fs_is_volatile_file(inode
))
1395 clear_inode_flag(F2FS_I(inode
), FI_VOLATILE_FILE
);
1397 mnt_drop_write_file(filp
);
1401 static int f2fs_ioc_shutdown(struct file
*filp
, unsigned long arg
)
1403 struct inode
*inode
= file_inode(filp
);
1404 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1405 struct super_block
*sb
= sbi
->sb
;
1408 if (!capable(CAP_SYS_ADMIN
))
1411 if (get_user(in
, (__u32 __user
*)arg
))
1415 case F2FS_GOING_DOWN_FULLSYNC
:
1416 sb
= freeze_bdev(sb
->s_bdev
);
1417 if (sb
&& !IS_ERR(sb
)) {
1418 f2fs_stop_checkpoint(sbi
);
1419 thaw_bdev(sb
->s_bdev
, sb
);
1422 case F2FS_GOING_DOWN_METASYNC
:
1423 /* do checkpoint only */
1424 f2fs_sync_fs(sb
, 1);
1425 f2fs_stop_checkpoint(sbi
);
1427 case F2FS_GOING_DOWN_NOSYNC
:
1428 f2fs_stop_checkpoint(sbi
);
1436 static int f2fs_ioc_fitrim(struct file
*filp
, unsigned long arg
)
1438 struct inode
*inode
= file_inode(filp
);
1439 struct super_block
*sb
= inode
->i_sb
;
1440 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
1441 struct fstrim_range range
;
1444 if (!capable(CAP_SYS_ADMIN
))
1447 if (!blk_queue_discard(q
))
1450 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
1454 range
.minlen
= max((unsigned int)range
.minlen
,
1455 q
->limits
.discard_granularity
);
1456 ret
= f2fs_trim_fs(F2FS_SB(sb
), &range
);
1460 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
1466 static bool uuid_is_nonzero(__u8 u
[16])
1470 for (i
= 0; i
< 16; i
++)
1476 static int f2fs_ioc_set_encryption_policy(struct file
*filp
, unsigned long arg
)
1478 #ifdef CONFIG_F2FS_FS_ENCRYPTION
1479 struct f2fs_encryption_policy policy
;
1480 struct inode
*inode
= file_inode(filp
);
1482 if (copy_from_user(&policy
, (struct f2fs_encryption_policy __user
*)arg
,
1486 return f2fs_process_policy(&policy
, inode
);
1492 static int f2fs_ioc_get_encryption_policy(struct file
*filp
, unsigned long arg
)
1494 #ifdef CONFIG_F2FS_FS_ENCRYPTION
1495 struct f2fs_encryption_policy policy
;
1496 struct inode
*inode
= file_inode(filp
);
1499 err
= f2fs_get_policy(inode
, &policy
);
1503 if (copy_to_user((struct f2fs_encryption_policy __user
*)arg
, &policy
,
1512 static int f2fs_ioc_get_encryption_pwsalt(struct file
*filp
, unsigned long arg
)
1514 struct inode
*inode
= file_inode(filp
);
1515 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1518 if (!f2fs_sb_has_crypto(inode
->i_sb
))
1521 if (uuid_is_nonzero(sbi
->raw_super
->encrypt_pw_salt
))
1524 err
= mnt_want_write_file(filp
);
1528 /* update superblock with uuid */
1529 generate_random_uuid(sbi
->raw_super
->encrypt_pw_salt
);
1531 err
= f2fs_commit_super(sbi
, false);
1533 mnt_drop_write_file(filp
);
1536 memset(sbi
->raw_super
->encrypt_pw_salt
, 0, 16);
1540 if (copy_to_user((__u8 __user
*)arg
, sbi
->raw_super
->encrypt_pw_salt
,
1546 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1549 case F2FS_IOC_GETFLAGS
:
1550 return f2fs_ioc_getflags(filp
, arg
);
1551 case F2FS_IOC_SETFLAGS
:
1552 return f2fs_ioc_setflags(filp
, arg
);
1553 case F2FS_IOC_GETVERSION
:
1554 return f2fs_ioc_getversion(filp
, arg
);
1555 case F2FS_IOC_START_ATOMIC_WRITE
:
1556 return f2fs_ioc_start_atomic_write(filp
);
1557 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
1558 return f2fs_ioc_commit_atomic_write(filp
);
1559 case F2FS_IOC_START_VOLATILE_WRITE
:
1560 return f2fs_ioc_start_volatile_write(filp
);
1561 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
1562 return f2fs_ioc_release_volatile_write(filp
);
1563 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
1564 return f2fs_ioc_abort_volatile_write(filp
);
1565 case F2FS_IOC_SHUTDOWN
:
1566 return f2fs_ioc_shutdown(filp
, arg
);
1568 return f2fs_ioc_fitrim(filp
, arg
);
1569 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
1570 return f2fs_ioc_set_encryption_policy(filp
, arg
);
1571 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
1572 return f2fs_ioc_get_encryption_policy(filp
, arg
);
1573 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
1574 return f2fs_ioc_get_encryption_pwsalt(filp
, arg
);
1580 static ssize_t
f2fs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1582 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1584 if (f2fs_encrypted_inode(inode
) &&
1585 !f2fs_has_encryption_key(inode
) &&
1586 f2fs_get_encryption_info(inode
))
1589 return generic_file_write_iter(iocb
, from
);
1592 #ifdef CONFIG_COMPAT
1593 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1596 case F2FS_IOC32_GETFLAGS
:
1597 cmd
= F2FS_IOC_GETFLAGS
;
1599 case F2FS_IOC32_SETFLAGS
:
1600 cmd
= F2FS_IOC_SETFLAGS
;
1603 return -ENOIOCTLCMD
;
1605 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
1609 const struct file_operations f2fs_file_operations
= {
1610 .llseek
= f2fs_llseek
,
1611 .read_iter
= generic_file_read_iter
,
1612 .write_iter
= f2fs_file_write_iter
,
1613 .open
= f2fs_file_open
,
1614 .release
= f2fs_release_file
,
1615 .mmap
= f2fs_file_mmap
,
1616 .fsync
= f2fs_sync_file
,
1617 .fallocate
= f2fs_fallocate
,
1618 .unlocked_ioctl
= f2fs_ioctl
,
1619 #ifdef CONFIG_COMPAT
1620 .compat_ioctl
= f2fs_compat_ioctl
,
1622 .splice_read
= generic_file_splice_read
,
1623 .splice_write
= iter_file_splice_write
,