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/uio.h>
24 #include <linux/uuid.h>
25 #include <linux/file.h>
34 #include <trace/events/f2fs.h>
36 static int f2fs_filemap_fault(struct vm_fault
*vmf
)
38 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
41 down_read(&F2FS_I(inode
)->i_mmap_sem
);
42 err
= filemap_fault(vmf
);
43 up_read(&F2FS_I(inode
)->i_mmap_sem
);
48 static int f2fs_vm_page_mkwrite(struct vm_fault
*vmf
)
50 struct page
*page
= vmf
->page
;
51 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
52 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
53 struct dnode_of_data dn
;
56 sb_start_pagefault(inode
->i_sb
);
58 f2fs_bug_on(sbi
, f2fs_has_inline_data(inode
));
60 /* block allocation */
62 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
63 err
= f2fs_reserve_block(&dn
, page
->index
);
71 f2fs_balance_fs(sbi
, dn
.node_changed
);
73 file_update_time(vmf
->vma
->vm_file
);
74 down_read(&F2FS_I(inode
)->i_mmap_sem
);
76 if (unlikely(page
->mapping
!= inode
->i_mapping
||
77 page_offset(page
) > i_size_read(inode
) ||
78 !PageUptodate(page
))) {
85 * check to see if the page is mapped already (no holes)
87 if (PageMappedToDisk(page
))
90 /* page is wholly or partially inside EOF */
91 if (((loff_t
)(page
->index
+ 1) << PAGE_SHIFT
) >
94 offset
= i_size_read(inode
) & ~PAGE_MASK
;
95 zero_user_segment(page
, offset
, PAGE_SIZE
);
98 if (!PageUptodate(page
))
99 SetPageUptodate(page
);
101 f2fs_update_iostat(sbi
, APP_MAPPED_IO
, F2FS_BLKSIZE
);
103 trace_f2fs_vm_page_mkwrite(page
, DATA
);
106 f2fs_wait_on_page_writeback(page
, DATA
, false);
108 /* wait for GCed encrypted page writeback */
109 if (f2fs_encrypted_file(inode
))
110 f2fs_wait_on_block_writeback(sbi
, dn
.data_blkaddr
);
113 up_read(&F2FS_I(inode
)->i_mmap_sem
);
115 sb_end_pagefault(inode
->i_sb
);
116 f2fs_update_time(sbi
, REQ_TIME
);
117 return block_page_mkwrite_return(err
);
120 static const struct vm_operations_struct f2fs_file_vm_ops
= {
121 .fault
= f2fs_filemap_fault
,
122 .map_pages
= filemap_map_pages
,
123 .page_mkwrite
= f2fs_vm_page_mkwrite
,
126 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
128 struct dentry
*dentry
;
130 inode
= igrab(inode
);
131 dentry
= d_find_any_alias(inode
);
136 *pino
= parent_ino(dentry
);
141 static inline bool need_do_checkpoint(struct inode
*inode
)
143 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
144 bool need_cp
= false;
146 if (!S_ISREG(inode
->i_mode
) || inode
->i_nlink
!= 1)
148 else if (is_sbi_flag_set(sbi
, SBI_NEED_CP
))
150 else if (file_wrong_pino(inode
))
152 else if (!space_for_roll_forward(sbi
))
154 else if (!is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
156 else if (test_opt(sbi
, FASTBOOT
))
158 else if (sbi
->active_logs
== 2)
164 static bool need_inode_page_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
166 struct page
*i
= find_get_page(NODE_MAPPING(sbi
), ino
);
168 /* But we need to avoid that there are some inode updates */
169 if ((i
&& PageDirty(i
)) || need_inode_block_update(sbi
, ino
))
175 static void try_to_fix_pino(struct inode
*inode
)
177 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
180 down_write(&fi
->i_sem
);
181 if (file_wrong_pino(inode
) && inode
->i_nlink
== 1 &&
182 get_parent_ino(inode
, &pino
)) {
183 f2fs_i_pino_write(inode
, pino
);
184 file_got_pino(inode
);
186 up_write(&fi
->i_sem
);
189 static int f2fs_do_sync_file(struct file
*file
, loff_t start
, loff_t end
,
190 int datasync
, bool atomic
)
192 struct inode
*inode
= file
->f_mapping
->host
;
193 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
194 nid_t ino
= inode
->i_ino
;
196 bool need_cp
= false;
197 struct writeback_control wbc
= {
198 .sync_mode
= WB_SYNC_ALL
,
199 .nr_to_write
= LONG_MAX
,
203 if (unlikely(f2fs_readonly(inode
->i_sb
)))
206 trace_f2fs_sync_file_enter(inode
);
208 if (S_ISDIR(inode
->i_mode
))
211 /* if fdatasync is triggered, let's do in-place-update */
212 if (datasync
|| get_dirty_pages(inode
) <= SM_I(sbi
)->min_fsync_blocks
)
213 set_inode_flag(inode
, FI_NEED_IPU
);
214 ret
= file_write_and_wait_range(file
, start
, end
);
215 clear_inode_flag(inode
, FI_NEED_IPU
);
218 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
222 /* if the inode is dirty, let's recover all the time */
223 if (!f2fs_skip_inode_update(inode
, datasync
)) {
224 f2fs_write_inode(inode
, NULL
);
229 * if there is no written data, don't waste time to write recovery info.
231 if (!is_inode_flag_set(inode
, FI_APPEND_WRITE
) &&
232 !exist_written_data(sbi
, ino
, APPEND_INO
)) {
234 /* it may call write_inode just prior to fsync */
235 if (need_inode_page_update(sbi
, ino
))
238 if (is_inode_flag_set(inode
, FI_UPDATE_WRITE
) ||
239 exist_written_data(sbi
, ino
, UPDATE_INO
))
245 * Both of fdatasync() and fsync() are able to be recovered from
248 down_read(&F2FS_I(inode
)->i_sem
);
249 need_cp
= need_do_checkpoint(inode
);
250 up_read(&F2FS_I(inode
)->i_sem
);
253 /* all the dirty node pages should be flushed for POR */
254 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
257 * We've secured consistency through sync_fs. Following pino
258 * will be used only for fsynced inodes after checkpoint.
260 try_to_fix_pino(inode
);
261 clear_inode_flag(inode
, FI_APPEND_WRITE
);
262 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
266 ret
= fsync_node_pages(sbi
, inode
, &wbc
, atomic
);
270 /* if cp_error was enabled, we should avoid infinite loop */
271 if (unlikely(f2fs_cp_error(sbi
))) {
276 if (need_inode_block_update(sbi
, ino
)) {
277 f2fs_mark_inode_dirty_sync(inode
, true);
278 f2fs_write_inode(inode
, NULL
);
283 * If it's atomic_write, it's just fine to keep write ordering. So
284 * here we don't need to wait for node write completion, since we use
285 * node chain which serializes node blocks. If one of node writes are
286 * reordered, we can see simply broken chain, resulting in stopping
287 * roll-forward recovery. It means we'll recover all or none node blocks
291 ret
= wait_on_node_pages_writeback(sbi
, ino
);
296 /* once recovery info is written, don't need to tack this */
297 remove_ino_entry(sbi
, ino
, APPEND_INO
);
298 clear_inode_flag(inode
, FI_APPEND_WRITE
);
300 remove_ino_entry(sbi
, ino
, UPDATE_INO
);
301 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
303 ret
= f2fs_issue_flush(sbi
);
304 f2fs_update_time(sbi
, REQ_TIME
);
306 trace_f2fs_sync_file_exit(inode
, need_cp
, datasync
, ret
);
307 f2fs_trace_ios(NULL
, 1);
311 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
313 return f2fs_do_sync_file(file
, start
, end
, datasync
, false);
316 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
317 pgoff_t pgofs
, int whence
)
322 if (whence
!= SEEK_DATA
)
325 /* find first dirty page index */
326 pagevec_init(&pvec
, 0);
327 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &pgofs
,
328 PAGECACHE_TAG_DIRTY
, 1);
329 pgofs
= nr_pages
? pvec
.pages
[0]->index
: ULONG_MAX
;
330 pagevec_release(&pvec
);
334 static bool __found_offset(struct f2fs_sb_info
*sbi
, block_t blkaddr
,
335 pgoff_t dirty
, pgoff_t pgofs
, int whence
)
339 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
340 is_valid_data_blkaddr(sbi
, blkaddr
))
344 if (blkaddr
== NULL_ADDR
)
351 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
353 struct inode
*inode
= file
->f_mapping
->host
;
354 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
355 struct dnode_of_data dn
;
356 pgoff_t pgofs
, end_offset
, dirty
;
357 loff_t data_ofs
= offset
;
363 isize
= i_size_read(inode
);
367 /* handle inline data case */
368 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
369 if (whence
== SEEK_HOLE
)
374 pgofs
= (pgoff_t
)(offset
>> PAGE_SHIFT
);
376 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
378 for (; data_ofs
< isize
; data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
379 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
380 err
= get_dnode_of_data(&dn
, pgofs
, LOOKUP_NODE
);
381 if (err
&& err
!= -ENOENT
) {
383 } else if (err
== -ENOENT
) {
384 /* direct node does not exists */
385 if (whence
== SEEK_DATA
) {
386 pgofs
= get_next_page_offset(&dn
, pgofs
);
393 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
395 /* find data/hole in dnode block */
396 for (; dn
.ofs_in_node
< end_offset
;
397 dn
.ofs_in_node
++, pgofs
++,
398 data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
400 blkaddr
= datablock_addr(dn
.inode
,
401 dn
.node_page
, dn
.ofs_in_node
);
403 if (__is_valid_data_blkaddr(blkaddr
) &&
404 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode
),
405 blkaddr
, DATA_GENERIC
)) {
410 if (__found_offset(F2FS_I_SB(inode
), blkaddr
, dirty
,
419 if (whence
== SEEK_DATA
)
422 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
425 return vfs_setpos(file
, data_ofs
, maxbytes
);
431 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
433 struct inode
*inode
= file
->f_mapping
->host
;
434 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
440 return generic_file_llseek_size(file
, offset
, whence
,
441 maxbytes
, i_size_read(inode
));
446 return f2fs_seek_block(file
, offset
, whence
);
452 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
454 struct inode
*inode
= file_inode(file
);
457 /* we don't need to use inline_data strictly */
458 err
= f2fs_convert_inline_inode(inode
);
463 vma
->vm_ops
= &f2fs_file_vm_ops
;
467 static int f2fs_file_open(struct inode
*inode
, struct file
*filp
)
471 if (f2fs_encrypted_inode(inode
)) {
472 int ret
= fscrypt_get_encryption_info(inode
);
475 if (!fscrypt_has_encryption_key(inode
))
478 dir
= dget_parent(file_dentry(filp
));
479 if (f2fs_encrypted_inode(d_inode(dir
)) &&
480 !fscrypt_has_permitted_context(d_inode(dir
), inode
)) {
485 return dquot_file_open(inode
, filp
);
488 int truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
490 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
491 struct f2fs_node
*raw_node
;
492 int nr_free
= 0, ofs
= dn
->ofs_in_node
, len
= count
;
496 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
497 base
= get_extra_isize(dn
->inode
);
499 raw_node
= F2FS_NODE(dn
->node_page
);
500 addr
= blkaddr_in_node(raw_node
) + base
+ ofs
;
502 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
503 block_t blkaddr
= le32_to_cpu(*addr
);
504 if (blkaddr
== NULL_ADDR
)
507 dn
->data_blkaddr
= NULL_ADDR
;
508 set_data_blkaddr(dn
);
510 if (__is_valid_data_blkaddr(blkaddr
) &&
511 !f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
))
514 invalidate_blocks(sbi
, blkaddr
);
515 if (dn
->ofs_in_node
== 0 && IS_INODE(dn
->node_page
))
516 clear_inode_flag(dn
->inode
, FI_FIRST_BLOCK_WRITTEN
);
523 * once we invalidate valid blkaddr in range [ofs, ofs + count],
524 * we will invalidate all blkaddr in the whole range.
526 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
),
528 f2fs_update_extent_cache_range(dn
, fofs
, 0, len
);
529 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
531 dn
->ofs_in_node
= ofs
;
533 f2fs_update_time(sbi
, REQ_TIME
);
534 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
535 dn
->ofs_in_node
, nr_free
);
539 void truncate_data_blocks(struct dnode_of_data
*dn
)
541 truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK
);
544 static int truncate_partial_data_page(struct inode
*inode
, u64 from
,
547 unsigned offset
= from
& (PAGE_SIZE
- 1);
548 pgoff_t index
= from
>> PAGE_SHIFT
;
549 struct address_space
*mapping
= inode
->i_mapping
;
552 if (!offset
&& !cache_only
)
556 page
= find_lock_page(mapping
, index
);
557 if (page
&& PageUptodate(page
))
559 f2fs_put_page(page
, 1);
563 page
= get_lock_data_page(inode
, index
, true);
565 return PTR_ERR(page
) == -ENOENT
? 0 : PTR_ERR(page
);
567 f2fs_wait_on_page_writeback(page
, DATA
, true);
568 zero_user(page
, offset
, PAGE_SIZE
- offset
);
570 /* An encrypted inode should have a key and truncate the last page. */
571 f2fs_bug_on(F2FS_I_SB(inode
), cache_only
&& f2fs_encrypted_inode(inode
));
573 set_page_dirty(page
);
574 f2fs_put_page(page
, 1);
578 int truncate_blocks(struct inode
*inode
, u64 from
, bool lock
)
580 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
581 unsigned int blocksize
= inode
->i_sb
->s_blocksize
;
582 struct dnode_of_data dn
;
584 int count
= 0, err
= 0;
586 bool truncate_page
= false;
588 trace_f2fs_truncate_blocks_enter(inode
, from
);
590 free_from
= (pgoff_t
)F2FS_BYTES_TO_BLK(from
+ blocksize
- 1);
592 if (free_from
>= sbi
->max_file_blocks
)
598 ipage
= get_node_page(sbi
, inode
->i_ino
);
600 err
= PTR_ERR(ipage
);
604 if (f2fs_has_inline_data(inode
)) {
605 truncate_inline_inode(inode
, ipage
, from
);
606 f2fs_put_page(ipage
, 1);
607 truncate_page
= true;
611 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
612 err
= get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE_RA
);
619 count
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
621 count
-= dn
.ofs_in_node
;
622 f2fs_bug_on(sbi
, count
< 0);
624 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
625 truncate_data_blocks_range(&dn
, count
);
631 err
= truncate_inode_blocks(inode
, free_from
);
636 /* lastly zero out the first data page */
638 err
= truncate_partial_data_page(inode
, from
, truncate_page
);
640 trace_f2fs_truncate_blocks_exit(inode
, err
);
644 int f2fs_truncate(struct inode
*inode
)
648 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
649 S_ISLNK(inode
->i_mode
)))
652 trace_f2fs_truncate(inode
);
654 #ifdef CONFIG_F2FS_FAULT_INJECTION
655 if (time_to_inject(F2FS_I_SB(inode
), FAULT_TRUNCATE
)) {
656 f2fs_show_injection_info(FAULT_TRUNCATE
);
660 /* we should check inline_data size */
661 if (!f2fs_may_inline_data(inode
)) {
662 err
= f2fs_convert_inline_inode(inode
);
667 err
= truncate_blocks(inode
, i_size_read(inode
), true);
671 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
672 f2fs_mark_inode_dirty_sync(inode
, false);
676 int f2fs_getattr(const struct path
*path
, struct kstat
*stat
,
677 u32 request_mask
, unsigned int query_flags
)
679 struct inode
*inode
= d_inode(path
->dentry
);
680 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
683 flags
= fi
->i_flags
& (FS_FL_USER_VISIBLE
| FS_PROJINHERIT_FL
);
684 if (flags
& FS_APPEND_FL
)
685 stat
->attributes
|= STATX_ATTR_APPEND
;
686 if (flags
& FS_COMPR_FL
)
687 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
688 if (f2fs_encrypted_inode(inode
))
689 stat
->attributes
|= STATX_ATTR_ENCRYPTED
;
690 if (flags
& FS_IMMUTABLE_FL
)
691 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
692 if (flags
& FS_NODUMP_FL
)
693 stat
->attributes
|= STATX_ATTR_NODUMP
;
695 stat
->attributes_mask
|= (STATX_ATTR_APPEND
|
696 STATX_ATTR_COMPRESSED
|
697 STATX_ATTR_ENCRYPTED
|
698 STATX_ATTR_IMMUTABLE
|
701 generic_fillattr(inode
, stat
);
703 /* we need to show initial sectors used for inline_data/dentries */
704 if ((S_ISREG(inode
->i_mode
) && f2fs_has_inline_data(inode
)) ||
705 f2fs_has_inline_dentry(inode
))
706 stat
->blocks
+= (stat
->size
+ 511) >> 9;
711 #ifdef CONFIG_F2FS_FS_POSIX_ACL
712 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
714 unsigned int ia_valid
= attr
->ia_valid
;
716 if (ia_valid
& ATTR_UID
)
717 inode
->i_uid
= attr
->ia_uid
;
718 if (ia_valid
& ATTR_GID
)
719 inode
->i_gid
= attr
->ia_gid
;
720 if (ia_valid
& ATTR_ATIME
)
721 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
722 inode
->i_sb
->s_time_gran
);
723 if (ia_valid
& ATTR_MTIME
)
724 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
725 inode
->i_sb
->s_time_gran
);
726 if (ia_valid
& ATTR_CTIME
)
727 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
728 inode
->i_sb
->s_time_gran
);
729 if (ia_valid
& ATTR_MODE
) {
730 umode_t mode
= attr
->ia_mode
;
732 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
734 set_acl_inode(inode
, mode
);
738 #define __setattr_copy setattr_copy
741 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
743 struct inode
*inode
= d_inode(dentry
);
745 bool size_changed
= false;
747 err
= setattr_prepare(dentry
, attr
);
751 if (is_quota_modification(inode
, attr
)) {
752 err
= dquot_initialize(inode
);
756 if ((attr
->ia_valid
& ATTR_UID
&&
757 !uid_eq(attr
->ia_uid
, inode
->i_uid
)) ||
758 (attr
->ia_valid
& ATTR_GID
&&
759 !gid_eq(attr
->ia_gid
, inode
->i_gid
))) {
760 err
= dquot_transfer(inode
, attr
);
765 if (attr
->ia_valid
& ATTR_SIZE
) {
766 if (f2fs_encrypted_inode(inode
)) {
767 err
= fscrypt_get_encryption_info(inode
);
770 if (!fscrypt_has_encryption_key(inode
))
774 if (attr
->ia_size
<= i_size_read(inode
)) {
775 down_write(&F2FS_I(inode
)->i_mmap_sem
);
776 truncate_setsize(inode
, attr
->ia_size
);
777 err
= f2fs_truncate(inode
);
778 up_write(&F2FS_I(inode
)->i_mmap_sem
);
783 * do not trim all blocks after i_size if target size is
784 * larger than i_size.
786 down_write(&F2FS_I(inode
)->i_mmap_sem
);
787 truncate_setsize(inode
, attr
->ia_size
);
788 up_write(&F2FS_I(inode
)->i_mmap_sem
);
790 /* should convert inline inode here */
791 if (!f2fs_may_inline_data(inode
)) {
792 err
= f2fs_convert_inline_inode(inode
);
796 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
802 __setattr_copy(inode
, attr
);
804 if (attr
->ia_valid
& ATTR_MODE
) {
805 err
= posix_acl_chmod(inode
, get_inode_mode(inode
));
806 if (err
|| is_inode_flag_set(inode
, FI_ACL_MODE
)) {
807 inode
->i_mode
= F2FS_I(inode
)->i_acl_mode
;
808 clear_inode_flag(inode
, FI_ACL_MODE
);
812 /* file size may changed here */
813 f2fs_mark_inode_dirty_sync(inode
, size_changed
);
815 /* inode change will produce dirty node pages flushed by checkpoint */
816 f2fs_balance_fs(F2FS_I_SB(inode
), true);
821 const struct inode_operations f2fs_file_inode_operations
= {
822 .getattr
= f2fs_getattr
,
823 .setattr
= f2fs_setattr
,
824 .get_acl
= f2fs_get_acl
,
825 .set_acl
= f2fs_set_acl
,
826 #ifdef CONFIG_F2FS_FS_XATTR
827 .listxattr
= f2fs_listxattr
,
829 .fiemap
= f2fs_fiemap
,
832 static int fill_zero(struct inode
*inode
, pgoff_t index
,
833 loff_t start
, loff_t len
)
835 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
841 f2fs_balance_fs(sbi
, true);
844 page
= get_new_data_page(inode
, NULL
, index
, false);
848 return PTR_ERR(page
);
850 f2fs_wait_on_page_writeback(page
, DATA
, true);
851 zero_user(page
, start
, len
);
852 set_page_dirty(page
);
853 f2fs_put_page(page
, 1);
857 int truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
861 while (pg_start
< pg_end
) {
862 struct dnode_of_data dn
;
863 pgoff_t end_offset
, count
;
865 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
866 err
= get_dnode_of_data(&dn
, pg_start
, LOOKUP_NODE
);
868 if (err
== -ENOENT
) {
875 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
876 count
= min(end_offset
- dn
.ofs_in_node
, pg_end
- pg_start
);
878 f2fs_bug_on(F2FS_I_SB(inode
), count
== 0 || count
> end_offset
);
880 truncate_data_blocks_range(&dn
, count
);
888 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
890 pgoff_t pg_start
, pg_end
;
891 loff_t off_start
, off_end
;
894 ret
= f2fs_convert_inline_inode(inode
);
898 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
899 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
901 off_start
= offset
& (PAGE_SIZE
- 1);
902 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
904 if (pg_start
== pg_end
) {
905 ret
= fill_zero(inode
, pg_start
, off_start
,
906 off_end
- off_start
);
911 ret
= fill_zero(inode
, pg_start
++, off_start
,
912 PAGE_SIZE
- off_start
);
917 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
922 if (pg_start
< pg_end
) {
923 struct address_space
*mapping
= inode
->i_mapping
;
924 loff_t blk_start
, blk_end
;
925 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
927 f2fs_balance_fs(sbi
, true);
929 blk_start
= (loff_t
)pg_start
<< PAGE_SHIFT
;
930 blk_end
= (loff_t
)pg_end
<< PAGE_SHIFT
;
931 down_write(&F2FS_I(inode
)->i_mmap_sem
);
932 truncate_inode_pages_range(mapping
, blk_start
,
936 ret
= truncate_hole(inode
, pg_start
, pg_end
);
938 up_write(&F2FS_I(inode
)->i_mmap_sem
);
945 static int __read_out_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
946 int *do_replace
, pgoff_t off
, pgoff_t len
)
948 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
949 struct dnode_of_data dn
;
953 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
954 ret
= get_dnode_of_data(&dn
, off
, LOOKUP_NODE_RA
);
955 if (ret
&& ret
!= -ENOENT
) {
957 } else if (ret
== -ENOENT
) {
958 if (dn
.max_level
== 0)
960 done
= min((pgoff_t
)ADDRS_PER_BLOCK
- dn
.ofs_in_node
, len
);
966 done
= min((pgoff_t
)ADDRS_PER_PAGE(dn
.node_page
, inode
) -
967 dn
.ofs_in_node
, len
);
968 for (i
= 0; i
< done
; i
++, blkaddr
++, do_replace
++, dn
.ofs_in_node
++) {
969 *blkaddr
= datablock_addr(dn
.inode
,
970 dn
.node_page
, dn
.ofs_in_node
);
971 if (!is_checkpointed_data(sbi
, *blkaddr
)) {
973 if (test_opt(sbi
, LFS
)) {
978 /* do not invalidate this block address */
979 f2fs_update_data_blkaddr(&dn
, NULL_ADDR
);
992 static int __roll_back_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
993 int *do_replace
, pgoff_t off
, int len
)
995 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
996 struct dnode_of_data dn
;
999 for (i
= 0; i
< len
; i
++, do_replace
++, blkaddr
++) {
1000 if (*do_replace
== 0)
1003 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1004 ret
= get_dnode_of_data(&dn
, off
+ i
, LOOKUP_NODE_RA
);
1006 dec_valid_block_count(sbi
, inode
, 1);
1007 invalidate_blocks(sbi
, *blkaddr
);
1009 f2fs_update_data_blkaddr(&dn
, *blkaddr
);
1011 f2fs_put_dnode(&dn
);
1016 static int __clone_blkaddrs(struct inode
*src_inode
, struct inode
*dst_inode
,
1017 block_t
*blkaddr
, int *do_replace
,
1018 pgoff_t src
, pgoff_t dst
, pgoff_t len
, bool full
)
1020 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src_inode
);
1025 if (blkaddr
[i
] == NULL_ADDR
&& !full
) {
1030 if (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
) {
1031 struct dnode_of_data dn
;
1032 struct node_info ni
;
1036 set_new_dnode(&dn
, dst_inode
, NULL
, NULL
, 0);
1037 ret
= get_dnode_of_data(&dn
, dst
+ i
, ALLOC_NODE
);
1041 get_node_info(sbi
, dn
.nid
, &ni
);
1042 ilen
= min((pgoff_t
)
1043 ADDRS_PER_PAGE(dn
.node_page
, dst_inode
) -
1044 dn
.ofs_in_node
, len
- i
);
1046 dn
.data_blkaddr
= datablock_addr(dn
.inode
,
1047 dn
.node_page
, dn
.ofs_in_node
);
1048 truncate_data_blocks_range(&dn
, 1);
1050 if (do_replace
[i
]) {
1051 f2fs_i_blocks_write(src_inode
,
1053 f2fs_i_blocks_write(dst_inode
,
1055 f2fs_replace_block(sbi
, &dn
, dn
.data_blkaddr
,
1056 blkaddr
[i
], ni
.version
, true, false);
1062 new_size
= (dst
+ i
) << PAGE_SHIFT
;
1063 if (dst_inode
->i_size
< new_size
)
1064 f2fs_i_size_write(dst_inode
, new_size
);
1065 } while (--ilen
&& (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
));
1067 f2fs_put_dnode(&dn
);
1069 struct page
*psrc
, *pdst
;
1071 psrc
= get_lock_data_page(src_inode
, src
+ i
, true);
1073 return PTR_ERR(psrc
);
1074 pdst
= get_new_data_page(dst_inode
, NULL
, dst
+ i
,
1077 f2fs_put_page(psrc
, 1);
1078 return PTR_ERR(pdst
);
1080 f2fs_copy_page(psrc
, pdst
);
1081 set_page_dirty(pdst
);
1082 f2fs_put_page(pdst
, 1);
1083 f2fs_put_page(psrc
, 1);
1085 ret
= truncate_hole(src_inode
, src
+ i
, src
+ i
+ 1);
1094 static int __exchange_data_block(struct inode
*src_inode
,
1095 struct inode
*dst_inode
, pgoff_t src
, pgoff_t dst
,
1096 pgoff_t len
, bool full
)
1098 block_t
*src_blkaddr
;
1104 olen
= min((pgoff_t
)4 * ADDRS_PER_BLOCK
, len
);
1106 src_blkaddr
= kvzalloc(sizeof(block_t
) * olen
, GFP_KERNEL
);
1110 do_replace
= kvzalloc(sizeof(int) * olen
, GFP_KERNEL
);
1112 kvfree(src_blkaddr
);
1116 ret
= __read_out_blkaddrs(src_inode
, src_blkaddr
,
1117 do_replace
, src
, olen
);
1121 ret
= __clone_blkaddrs(src_inode
, dst_inode
, src_blkaddr
,
1122 do_replace
, src
, dst
, olen
, full
);
1130 kvfree(src_blkaddr
);
1136 __roll_back_blkaddrs(src_inode
, src_blkaddr
, do_replace
, src
, len
);
1137 kvfree(src_blkaddr
);
1142 static int f2fs_do_collapse(struct inode
*inode
, pgoff_t start
, pgoff_t end
)
1144 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1145 pgoff_t nrpages
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1148 f2fs_balance_fs(sbi
, true);
1151 f2fs_drop_extent_tree(inode
);
1153 ret
= __exchange_data_block(inode
, inode
, end
, start
, nrpages
- end
, true);
1154 f2fs_unlock_op(sbi
);
1158 static int f2fs_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1160 pgoff_t pg_start
, pg_end
;
1164 if (offset
+ len
>= i_size_read(inode
))
1167 /* collapse range should be aligned to block size of f2fs. */
1168 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1171 ret
= f2fs_convert_inline_inode(inode
);
1175 pg_start
= offset
>> PAGE_SHIFT
;
1176 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1178 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1179 /* write out all dirty pages from offset */
1180 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1184 truncate_pagecache(inode
, offset
);
1186 ret
= f2fs_do_collapse(inode
, pg_start
, pg_end
);
1190 /* write out all moved pages, if possible */
1191 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1192 truncate_pagecache(inode
, offset
);
1194 new_size
= i_size_read(inode
) - len
;
1195 truncate_pagecache(inode
, new_size
);
1197 ret
= truncate_blocks(inode
, new_size
, true);
1199 f2fs_i_size_write(inode
, new_size
);
1202 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1206 static int f2fs_do_zero_range(struct dnode_of_data
*dn
, pgoff_t start
,
1209 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1210 pgoff_t index
= start
;
1211 unsigned int ofs_in_node
= dn
->ofs_in_node
;
1215 for (; index
< end
; index
++, dn
->ofs_in_node
++) {
1216 if (datablock_addr(dn
->inode
, dn
->node_page
,
1217 dn
->ofs_in_node
) == NULL_ADDR
)
1221 dn
->ofs_in_node
= ofs_in_node
;
1222 ret
= reserve_new_blocks(dn
, count
);
1226 dn
->ofs_in_node
= ofs_in_node
;
1227 for (index
= start
; index
< end
; index
++, dn
->ofs_in_node
++) {
1228 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
1229 dn
->node_page
, dn
->ofs_in_node
);
1231 * reserve_new_blocks will not guarantee entire block
1234 if (dn
->data_blkaddr
== NULL_ADDR
) {
1238 if (dn
->data_blkaddr
!= NEW_ADDR
) {
1239 invalidate_blocks(sbi
, dn
->data_blkaddr
);
1240 dn
->data_blkaddr
= NEW_ADDR
;
1241 set_data_blkaddr(dn
);
1245 f2fs_update_extent_cache_range(dn
, start
, 0, index
- start
);
1250 static int f2fs_zero_range(struct inode
*inode
, loff_t offset
, loff_t len
,
1253 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1254 struct address_space
*mapping
= inode
->i_mapping
;
1255 pgoff_t index
, pg_start
, pg_end
;
1256 loff_t new_size
= i_size_read(inode
);
1257 loff_t off_start
, off_end
;
1260 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1264 ret
= f2fs_convert_inline_inode(inode
);
1268 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1269 ret
= filemap_write_and_wait_range(mapping
, offset
, offset
+ len
- 1);
1273 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
1275 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
1276 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
1278 off_start
= offset
& (PAGE_SIZE
- 1);
1279 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1281 if (pg_start
== pg_end
) {
1282 ret
= fill_zero(inode
, pg_start
, off_start
,
1283 off_end
- off_start
);
1287 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1290 ret
= fill_zero(inode
, pg_start
++, off_start
,
1291 PAGE_SIZE
- off_start
);
1295 new_size
= max_t(loff_t
, new_size
,
1296 (loff_t
)pg_start
<< PAGE_SHIFT
);
1299 for (index
= pg_start
; index
< pg_end
;) {
1300 struct dnode_of_data dn
;
1301 unsigned int end_offset
;
1306 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1307 ret
= get_dnode_of_data(&dn
, index
, ALLOC_NODE
);
1309 f2fs_unlock_op(sbi
);
1313 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1314 end
= min(pg_end
, end_offset
- dn
.ofs_in_node
+ index
);
1316 ret
= f2fs_do_zero_range(&dn
, index
, end
);
1317 f2fs_put_dnode(&dn
);
1318 f2fs_unlock_op(sbi
);
1320 f2fs_balance_fs(sbi
, dn
.node_changed
);
1326 new_size
= max_t(loff_t
, new_size
,
1327 (loff_t
)index
<< PAGE_SHIFT
);
1331 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
1335 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1340 if (new_size
> i_size_read(inode
)) {
1341 if (mode
& FALLOC_FL_KEEP_SIZE
)
1342 file_set_keep_isize(inode
);
1344 f2fs_i_size_write(inode
, new_size
);
1347 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1352 static int f2fs_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1354 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1355 pgoff_t nr
, pg_start
, pg_end
, delta
, idx
;
1359 new_size
= i_size_read(inode
) + len
;
1360 ret
= inode_newsize_ok(inode
, new_size
);
1364 if (offset
>= i_size_read(inode
))
1367 /* insert range should be aligned to block size of f2fs. */
1368 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1371 ret
= f2fs_convert_inline_inode(inode
);
1375 f2fs_balance_fs(sbi
, true);
1377 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1378 ret
= truncate_blocks(inode
, i_size_read(inode
), true);
1382 /* write out all dirty pages from offset */
1383 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1387 truncate_pagecache(inode
, offset
);
1389 pg_start
= offset
>> PAGE_SHIFT
;
1390 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1391 delta
= pg_end
- pg_start
;
1392 idx
= (i_size_read(inode
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
1394 while (!ret
&& idx
> pg_start
) {
1395 nr
= idx
- pg_start
;
1401 f2fs_drop_extent_tree(inode
);
1403 ret
= __exchange_data_block(inode
, inode
, idx
,
1404 idx
+ delta
, nr
, false);
1405 f2fs_unlock_op(sbi
);
1408 /* write out all moved pages, if possible */
1409 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1410 truncate_pagecache(inode
, offset
);
1413 f2fs_i_size_write(inode
, new_size
);
1415 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1419 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
1420 loff_t len
, int mode
)
1422 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1423 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
};
1425 loff_t new_size
= i_size_read(inode
);
1429 err
= inode_newsize_ok(inode
, (len
+ offset
));
1433 err
= f2fs_convert_inline_inode(inode
);
1437 f2fs_balance_fs(sbi
, true);
1439 pg_end
= ((unsigned long long)offset
+ len
) >> PAGE_SHIFT
;
1440 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1442 map
.m_lblk
= ((unsigned long long)offset
) >> PAGE_SHIFT
;
1443 map
.m_len
= pg_end
- map
.m_lblk
;
1447 err
= f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_AIO
);
1454 last_off
= map
.m_lblk
+ map
.m_len
- 1;
1456 /* update new size to the failed position */
1457 new_size
= (last_off
== pg_end
) ? offset
+ len
:
1458 (loff_t
)(last_off
+ 1) << PAGE_SHIFT
;
1460 new_size
= ((loff_t
)pg_end
<< PAGE_SHIFT
) + off_end
;
1463 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && i_size_read(inode
) < new_size
)
1464 f2fs_i_size_write(inode
, new_size
);
1469 static long f2fs_fallocate(struct file
*file
, int mode
,
1470 loff_t offset
, loff_t len
)
1472 struct inode
*inode
= file_inode(file
);
1475 /* f2fs only support ->fallocate for regular file */
1476 if (!S_ISREG(inode
->i_mode
))
1479 if (f2fs_encrypted_inode(inode
) &&
1480 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
)))
1483 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
1484 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
1485 FALLOC_FL_INSERT_RANGE
))
1490 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
1491 if (offset
>= inode
->i_size
)
1494 ret
= punch_hole(inode
, offset
, len
);
1495 } else if (mode
& FALLOC_FL_COLLAPSE_RANGE
) {
1496 ret
= f2fs_collapse_range(inode
, offset
, len
);
1497 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
1498 ret
= f2fs_zero_range(inode
, offset
, len
, mode
);
1499 } else if (mode
& FALLOC_FL_INSERT_RANGE
) {
1500 ret
= f2fs_insert_range(inode
, offset
, len
);
1502 ret
= expand_inode_data(inode
, offset
, len
, mode
);
1506 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
1507 f2fs_mark_inode_dirty_sync(inode
, false);
1508 if (mode
& FALLOC_FL_KEEP_SIZE
)
1509 file_set_keep_isize(inode
);
1510 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1514 inode_unlock(inode
);
1516 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
1520 static int f2fs_release_file(struct inode
*inode
, struct file
*filp
)
1523 * f2fs_relase_file is called at every close calls. So we should
1524 * not drop any inmemory pages by close called by other process.
1526 if (!(filp
->f_mode
& FMODE_WRITE
) ||
1527 atomic_read(&inode
->i_writecount
) != 1)
1530 /* some remained atomic pages should discarded */
1531 if (f2fs_is_atomic_file(inode
))
1532 drop_inmem_pages(inode
);
1533 if (f2fs_is_volatile_file(inode
)) {
1534 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1535 stat_dec_volatile_write(inode
);
1536 set_inode_flag(inode
, FI_DROP_CACHE
);
1537 filemap_fdatawrite(inode
->i_mapping
);
1538 clear_inode_flag(inode
, FI_DROP_CACHE
);
1543 static int f2fs_file_flush(struct file
*file
, fl_owner_t id
)
1545 struct inode
*inode
= file_inode(file
);
1548 * If the process doing a transaction is crashed, we should do
1549 * roll-back. Otherwise, other reader/write can see corrupted database
1550 * until all the writers close its file. Since this should be done
1551 * before dropping file lock, it needs to do in ->flush.
1553 if (f2fs_is_atomic_file(inode
) &&
1554 F2FS_I(inode
)->inmem_task
== current
)
1555 drop_inmem_pages(inode
);
1559 static int f2fs_ioc_getflags(struct file
*filp
, unsigned long arg
)
1561 struct inode
*inode
= file_inode(filp
);
1562 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1563 unsigned int flags
= fi
->i_flags
&
1564 (FS_FL_USER_VISIBLE
| FS_PROJINHERIT_FL
);
1565 return put_user(flags
, (int __user
*)arg
);
1568 static int __f2fs_ioc_setflags(struct inode
*inode
, unsigned int flags
)
1570 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1571 unsigned int oldflags
;
1573 /* Is it quota file? Do not allow user to mess with it */
1574 if (IS_NOQUOTA(inode
))
1577 flags
= f2fs_mask_flags(inode
->i_mode
, flags
);
1579 oldflags
= fi
->i_flags
;
1581 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
))
1582 if (!capable(CAP_LINUX_IMMUTABLE
))
1585 flags
= flags
& (FS_FL_USER_MODIFIABLE
| FS_PROJINHERIT_FL
);
1586 flags
|= oldflags
& ~(FS_FL_USER_MODIFIABLE
| FS_PROJINHERIT_FL
);
1587 fi
->i_flags
= flags
;
1589 if (fi
->i_flags
& FS_PROJINHERIT_FL
)
1590 set_inode_flag(inode
, FI_PROJ_INHERIT
);
1592 clear_inode_flag(inode
, FI_PROJ_INHERIT
);
1594 inode
->i_ctime
= current_time(inode
);
1595 f2fs_set_inode_flags(inode
);
1596 f2fs_mark_inode_dirty_sync(inode
, false);
1600 static int f2fs_ioc_setflags(struct file
*filp
, unsigned long arg
)
1602 struct inode
*inode
= file_inode(filp
);
1606 if (!inode_owner_or_capable(inode
))
1609 if (get_user(flags
, (int __user
*)arg
))
1612 ret
= mnt_want_write_file(filp
);
1618 ret
= __f2fs_ioc_setflags(inode
, flags
);
1620 inode_unlock(inode
);
1621 mnt_drop_write_file(filp
);
1625 static int f2fs_ioc_getversion(struct file
*filp
, unsigned long arg
)
1627 struct inode
*inode
= file_inode(filp
);
1629 return put_user(inode
->i_generation
, (int __user
*)arg
);
1632 static int f2fs_ioc_start_atomic_write(struct file
*filp
)
1634 struct inode
*inode
= file_inode(filp
);
1637 if (!inode_owner_or_capable(inode
))
1640 if (!S_ISREG(inode
->i_mode
))
1643 ret
= mnt_want_write_file(filp
);
1649 down_write(&F2FS_I(inode
)->dio_rwsem
[WRITE
]);
1651 if (f2fs_is_atomic_file(inode
))
1654 ret
= f2fs_convert_inline_inode(inode
);
1658 set_inode_flag(inode
, FI_ATOMIC_FILE
);
1659 set_inode_flag(inode
, FI_HOT_DATA
);
1660 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1662 if (!get_dirty_pages(inode
))
1665 f2fs_msg(F2FS_I_SB(inode
)->sb
, KERN_WARNING
,
1666 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1667 inode
->i_ino
, get_dirty_pages(inode
));
1668 ret
= filemap_write_and_wait_range(inode
->i_mapping
, 0, LLONG_MAX
);
1670 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
1671 clear_inode_flag(inode
, FI_HOT_DATA
);
1676 F2FS_I(inode
)->inmem_task
= current
;
1677 stat_inc_atomic_write(inode
);
1678 stat_update_max_atomic_write(inode
);
1680 up_write(&F2FS_I(inode
)->dio_rwsem
[WRITE
]);
1681 inode_unlock(inode
);
1682 mnt_drop_write_file(filp
);
1686 static int f2fs_ioc_commit_atomic_write(struct file
*filp
)
1688 struct inode
*inode
= file_inode(filp
);
1691 if (!inode_owner_or_capable(inode
))
1694 ret
= mnt_want_write_file(filp
);
1700 if (f2fs_is_volatile_file(inode
))
1703 if (f2fs_is_atomic_file(inode
)) {
1704 ret
= commit_inmem_pages(inode
);
1708 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1710 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
1711 clear_inode_flag(inode
, FI_HOT_DATA
);
1712 stat_dec_atomic_write(inode
);
1715 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 1, false);
1718 inode_unlock(inode
);
1719 mnt_drop_write_file(filp
);
1723 static int f2fs_ioc_start_volatile_write(struct file
*filp
)
1725 struct inode
*inode
= file_inode(filp
);
1728 if (!inode_owner_or_capable(inode
))
1731 if (!S_ISREG(inode
->i_mode
))
1734 ret
= mnt_want_write_file(filp
);
1740 if (f2fs_is_volatile_file(inode
))
1743 ret
= f2fs_convert_inline_inode(inode
);
1747 stat_inc_volatile_write(inode
);
1748 stat_update_max_volatile_write(inode
);
1750 set_inode_flag(inode
, FI_VOLATILE_FILE
);
1751 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1753 inode_unlock(inode
);
1754 mnt_drop_write_file(filp
);
1758 static int f2fs_ioc_release_volatile_write(struct file
*filp
)
1760 struct inode
*inode
= file_inode(filp
);
1763 if (!inode_owner_or_capable(inode
))
1766 ret
= mnt_want_write_file(filp
);
1772 if (!f2fs_is_volatile_file(inode
))
1775 if (!f2fs_is_first_block_written(inode
)) {
1776 ret
= truncate_partial_data_page(inode
, 0, true);
1780 ret
= punch_hole(inode
, 0, F2FS_BLKSIZE
);
1782 inode_unlock(inode
);
1783 mnt_drop_write_file(filp
);
1787 static int f2fs_ioc_abort_volatile_write(struct file
*filp
)
1789 struct inode
*inode
= file_inode(filp
);
1792 if (!inode_owner_or_capable(inode
))
1795 ret
= mnt_want_write_file(filp
);
1801 if (f2fs_is_atomic_file(inode
))
1802 drop_inmem_pages(inode
);
1803 if (f2fs_is_volatile_file(inode
)) {
1804 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1805 stat_dec_volatile_write(inode
);
1806 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1809 inode_unlock(inode
);
1811 mnt_drop_write_file(filp
);
1812 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1816 static int f2fs_ioc_shutdown(struct file
*filp
, unsigned long arg
)
1818 struct inode
*inode
= file_inode(filp
);
1819 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1820 struct super_block
*sb
= sbi
->sb
;
1824 if (!capable(CAP_SYS_ADMIN
))
1827 if (get_user(in
, (__u32 __user
*)arg
))
1830 if (in
!= F2FS_GOING_DOWN_FULLSYNC
) {
1831 ret
= mnt_want_write_file(filp
);
1837 case F2FS_GOING_DOWN_FULLSYNC
:
1838 sb
= freeze_bdev(sb
->s_bdev
);
1839 if (sb
&& !IS_ERR(sb
)) {
1840 f2fs_stop_checkpoint(sbi
, false);
1841 thaw_bdev(sb
->s_bdev
, sb
);
1844 case F2FS_GOING_DOWN_METASYNC
:
1845 /* do checkpoint only */
1846 f2fs_sync_fs(sb
, 1);
1847 f2fs_stop_checkpoint(sbi
, false);
1849 case F2FS_GOING_DOWN_NOSYNC
:
1850 f2fs_stop_checkpoint(sbi
, false);
1852 case F2FS_GOING_DOWN_METAFLUSH
:
1853 sync_meta_pages(sbi
, META
, LONG_MAX
, FS_META_IO
);
1854 f2fs_stop_checkpoint(sbi
, false);
1860 f2fs_update_time(sbi
, REQ_TIME
);
1862 if (in
!= F2FS_GOING_DOWN_FULLSYNC
)
1863 mnt_drop_write_file(filp
);
1867 static int f2fs_ioc_fitrim(struct file
*filp
, unsigned long arg
)
1869 struct inode
*inode
= file_inode(filp
);
1870 struct super_block
*sb
= inode
->i_sb
;
1871 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
1872 struct fstrim_range range
;
1875 if (!capable(CAP_SYS_ADMIN
))
1878 if (!blk_queue_discard(q
))
1881 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
1885 ret
= mnt_want_write_file(filp
);
1889 range
.minlen
= max((unsigned int)range
.minlen
,
1890 q
->limits
.discard_granularity
);
1891 ret
= f2fs_trim_fs(F2FS_SB(sb
), &range
);
1892 mnt_drop_write_file(filp
);
1896 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
1899 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1903 static bool uuid_is_nonzero(__u8 u
[16])
1907 for (i
= 0; i
< 16; i
++)
1913 static int f2fs_ioc_set_encryption_policy(struct file
*filp
, unsigned long arg
)
1915 struct inode
*inode
= file_inode(filp
);
1917 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1919 return fscrypt_ioctl_set_policy(filp
, (const void __user
*)arg
);
1922 static int f2fs_ioc_get_encryption_policy(struct file
*filp
, unsigned long arg
)
1924 return fscrypt_ioctl_get_policy(filp
, (void __user
*)arg
);
1927 static int f2fs_ioc_get_encryption_pwsalt(struct file
*filp
, unsigned long arg
)
1929 struct inode
*inode
= file_inode(filp
);
1930 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1933 if (!f2fs_sb_has_crypto(inode
->i_sb
))
1936 if (uuid_is_nonzero(sbi
->raw_super
->encrypt_pw_salt
))
1939 err
= mnt_want_write_file(filp
);
1943 /* update superblock with uuid */
1944 generate_random_uuid(sbi
->raw_super
->encrypt_pw_salt
);
1946 err
= f2fs_commit_super(sbi
, false);
1949 memset(sbi
->raw_super
->encrypt_pw_salt
, 0, 16);
1950 mnt_drop_write_file(filp
);
1953 mnt_drop_write_file(filp
);
1955 if (copy_to_user((__u8 __user
*)arg
, sbi
->raw_super
->encrypt_pw_salt
,
1961 static int f2fs_ioc_gc(struct file
*filp
, unsigned long arg
)
1963 struct inode
*inode
= file_inode(filp
);
1964 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1968 if (!capable(CAP_SYS_ADMIN
))
1971 if (get_user(sync
, (__u32 __user
*)arg
))
1974 if (f2fs_readonly(sbi
->sb
))
1977 ret
= mnt_want_write_file(filp
);
1982 if (!mutex_trylock(&sbi
->gc_mutex
)) {
1987 mutex_lock(&sbi
->gc_mutex
);
1990 ret
= f2fs_gc(sbi
, sync
, true, NULL_SEGNO
);
1992 mnt_drop_write_file(filp
);
1996 static int f2fs_ioc_gc_range(struct file
*filp
, unsigned long arg
)
1998 struct inode
*inode
= file_inode(filp
);
1999 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2000 struct f2fs_gc_range range
;
2004 if (!capable(CAP_SYS_ADMIN
))
2007 if (copy_from_user(&range
, (struct f2fs_gc_range __user
*)arg
,
2011 if (f2fs_readonly(sbi
->sb
))
2014 ret
= mnt_want_write_file(filp
);
2018 end
= range
.start
+ range
.len
;
2019 if (range
.start
< MAIN_BLKADDR(sbi
) || end
>= MAX_BLKADDR(sbi
))
2023 if (!mutex_trylock(&sbi
->gc_mutex
)) {
2028 mutex_lock(&sbi
->gc_mutex
);
2031 ret
= f2fs_gc(sbi
, range
.sync
, true, GET_SEGNO(sbi
, range
.start
));
2032 range
.start
+= sbi
->blocks_per_seg
;
2033 if (range
.start
<= end
)
2036 mnt_drop_write_file(filp
);
2040 static int f2fs_ioc_write_checkpoint(struct file
*filp
, unsigned long arg
)
2042 struct inode
*inode
= file_inode(filp
);
2043 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2046 if (!capable(CAP_SYS_ADMIN
))
2049 if (f2fs_readonly(sbi
->sb
))
2052 ret
= mnt_want_write_file(filp
);
2056 ret
= f2fs_sync_fs(sbi
->sb
, 1);
2058 mnt_drop_write_file(filp
);
2062 static int f2fs_defragment_range(struct f2fs_sb_info
*sbi
,
2064 struct f2fs_defragment
*range
)
2066 struct inode
*inode
= file_inode(filp
);
2067 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
};
2068 struct extent_info ei
= {0,0,0};
2069 pgoff_t pg_start
, pg_end
;
2070 unsigned int blk_per_seg
= sbi
->blocks_per_seg
;
2071 unsigned int total
= 0, sec_num
;
2072 block_t blk_end
= 0;
2073 bool fragmented
= false;
2076 /* if in-place-update policy is enabled, don't waste time here */
2077 if (need_inplace_update_policy(inode
, NULL
))
2080 pg_start
= range
->start
>> PAGE_SHIFT
;
2081 pg_end
= (range
->start
+ range
->len
) >> PAGE_SHIFT
;
2083 f2fs_balance_fs(sbi
, true);
2087 /* writeback all dirty pages in the range */
2088 err
= filemap_write_and_wait_range(inode
->i_mapping
, range
->start
,
2089 range
->start
+ range
->len
- 1);
2094 * lookup mapping info in extent cache, skip defragmenting if physical
2095 * block addresses are continuous.
2097 if (f2fs_lookup_extent_cache(inode
, pg_start
, &ei
)) {
2098 if (ei
.fofs
+ ei
.len
>= pg_end
)
2102 map
.m_lblk
= pg_start
;
2105 * lookup mapping info in dnode page cache, skip defragmenting if all
2106 * physical block addresses are continuous even if there are hole(s)
2107 * in logical blocks.
2109 while (map
.m_lblk
< pg_end
) {
2110 map
.m_len
= pg_end
- map
.m_lblk
;
2111 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
2115 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
2120 if (blk_end
&& blk_end
!= map
.m_pblk
) {
2124 blk_end
= map
.m_pblk
+ map
.m_len
;
2126 map
.m_lblk
+= map
.m_len
;
2132 map
.m_lblk
= pg_start
;
2133 map
.m_len
= pg_end
- pg_start
;
2135 sec_num
= (map
.m_len
+ BLKS_PER_SEC(sbi
) - 1) / BLKS_PER_SEC(sbi
);
2138 * make sure there are enough free section for LFS allocation, this can
2139 * avoid defragment running in SSR mode when free section are allocated
2142 if (has_not_enough_free_secs(sbi
, 0, sec_num
)) {
2147 while (map
.m_lblk
< pg_end
) {
2152 map
.m_len
= pg_end
- map
.m_lblk
;
2153 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
2157 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
2162 set_inode_flag(inode
, FI_DO_DEFRAG
);
2165 while (idx
< map
.m_lblk
+ map
.m_len
&& cnt
< blk_per_seg
) {
2168 page
= get_lock_data_page(inode
, idx
, true);
2170 err
= PTR_ERR(page
);
2174 set_page_dirty(page
);
2175 f2fs_put_page(page
, 1);
2184 if (idx
< pg_end
&& cnt
< blk_per_seg
)
2187 clear_inode_flag(inode
, FI_DO_DEFRAG
);
2189 err
= filemap_fdatawrite(inode
->i_mapping
);
2194 clear_inode_flag(inode
, FI_DO_DEFRAG
);
2196 inode_unlock(inode
);
2198 range
->len
= (u64
)total
<< PAGE_SHIFT
;
2202 static int f2fs_ioc_defragment(struct file
*filp
, unsigned long arg
)
2204 struct inode
*inode
= file_inode(filp
);
2205 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2206 struct f2fs_defragment range
;
2209 if (!capable(CAP_SYS_ADMIN
))
2212 if (!S_ISREG(inode
->i_mode
) || f2fs_is_atomic_file(inode
))
2215 if (f2fs_readonly(sbi
->sb
))
2218 if (copy_from_user(&range
, (struct f2fs_defragment __user
*)arg
,
2222 /* verify alignment of offset & size */
2223 if (range
.start
& (F2FS_BLKSIZE
- 1) || range
.len
& (F2FS_BLKSIZE
- 1))
2226 if (unlikely((range
.start
+ range
.len
) >> PAGE_SHIFT
>
2227 sbi
->max_file_blocks
))
2230 err
= mnt_want_write_file(filp
);
2234 err
= f2fs_defragment_range(sbi
, filp
, &range
);
2235 mnt_drop_write_file(filp
);
2237 f2fs_update_time(sbi
, REQ_TIME
);
2241 if (copy_to_user((struct f2fs_defragment __user
*)arg
, &range
,
2248 static int f2fs_move_file_range(struct file
*file_in
, loff_t pos_in
,
2249 struct file
*file_out
, loff_t pos_out
, size_t len
)
2251 struct inode
*src
= file_inode(file_in
);
2252 struct inode
*dst
= file_inode(file_out
);
2253 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src
);
2254 size_t olen
= len
, dst_max_i_size
= 0;
2258 if (file_in
->f_path
.mnt
!= file_out
->f_path
.mnt
||
2259 src
->i_sb
!= dst
->i_sb
)
2262 if (unlikely(f2fs_readonly(src
->i_sb
)))
2265 if (!S_ISREG(src
->i_mode
) || !S_ISREG(dst
->i_mode
))
2268 if (f2fs_encrypted_inode(src
) || f2fs_encrypted_inode(dst
))
2272 if (pos_in
== pos_out
)
2274 if (pos_out
> pos_in
&& pos_out
< pos_in
+ len
)
2280 if (!inode_trylock(dst
)) {
2287 if (pos_in
+ len
> src
->i_size
|| pos_in
+ len
< pos_in
)
2290 olen
= len
= src
->i_size
- pos_in
;
2291 if (pos_in
+ len
== src
->i_size
)
2292 len
= ALIGN(src
->i_size
, F2FS_BLKSIZE
) - pos_in
;
2298 dst_osize
= dst
->i_size
;
2299 if (pos_out
+ olen
> dst
->i_size
)
2300 dst_max_i_size
= pos_out
+ olen
;
2302 /* verify the end result is block aligned */
2303 if (!IS_ALIGNED(pos_in
, F2FS_BLKSIZE
) ||
2304 !IS_ALIGNED(pos_in
+ len
, F2FS_BLKSIZE
) ||
2305 !IS_ALIGNED(pos_out
, F2FS_BLKSIZE
))
2308 ret
= f2fs_convert_inline_inode(src
);
2312 ret
= f2fs_convert_inline_inode(dst
);
2316 /* write out all dirty pages from offset */
2317 ret
= filemap_write_and_wait_range(src
->i_mapping
,
2318 pos_in
, pos_in
+ len
);
2322 ret
= filemap_write_and_wait_range(dst
->i_mapping
,
2323 pos_out
, pos_out
+ len
);
2327 f2fs_balance_fs(sbi
, true);
2329 ret
= __exchange_data_block(src
, dst
, pos_in
>> F2FS_BLKSIZE_BITS
,
2330 pos_out
>> F2FS_BLKSIZE_BITS
,
2331 len
>> F2FS_BLKSIZE_BITS
, false);
2335 f2fs_i_size_write(dst
, dst_max_i_size
);
2336 else if (dst_osize
!= dst
->i_size
)
2337 f2fs_i_size_write(dst
, dst_osize
);
2339 f2fs_unlock_op(sbi
);
2348 static int f2fs_ioc_move_range(struct file
*filp
, unsigned long arg
)
2350 struct f2fs_move_range range
;
2354 if (!(filp
->f_mode
& FMODE_READ
) ||
2355 !(filp
->f_mode
& FMODE_WRITE
))
2358 if (copy_from_user(&range
, (struct f2fs_move_range __user
*)arg
,
2362 dst
= fdget(range
.dst_fd
);
2366 if (!(dst
.file
->f_mode
& FMODE_WRITE
)) {
2371 err
= mnt_want_write_file(filp
);
2375 err
= f2fs_move_file_range(filp
, range
.pos_in
, dst
.file
,
2376 range
.pos_out
, range
.len
);
2378 mnt_drop_write_file(filp
);
2382 if (copy_to_user((struct f2fs_move_range __user
*)arg
,
2383 &range
, sizeof(range
)))
2390 static int f2fs_ioc_flush_device(struct file
*filp
, unsigned long arg
)
2392 struct inode
*inode
= file_inode(filp
);
2393 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2394 struct sit_info
*sm
= SIT_I(sbi
);
2395 unsigned int start_segno
= 0, end_segno
= 0;
2396 unsigned int dev_start_segno
= 0, dev_end_segno
= 0;
2397 struct f2fs_flush_device range
;
2400 if (!capable(CAP_SYS_ADMIN
))
2403 if (f2fs_readonly(sbi
->sb
))
2406 if (copy_from_user(&range
, (struct f2fs_flush_device __user
*)arg
,
2410 if (sbi
->s_ndevs
<= 1 || sbi
->s_ndevs
- 1 <= range
.dev_num
||
2411 sbi
->segs_per_sec
!= 1) {
2412 f2fs_msg(sbi
->sb
, KERN_WARNING
,
2413 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2414 range
.dev_num
, sbi
->s_ndevs
,
2419 ret
= mnt_want_write_file(filp
);
2423 if (range
.dev_num
!= 0)
2424 dev_start_segno
= GET_SEGNO(sbi
, FDEV(range
.dev_num
).start_blk
);
2425 dev_end_segno
= GET_SEGNO(sbi
, FDEV(range
.dev_num
).end_blk
);
2427 start_segno
= sm
->last_victim
[FLUSH_DEVICE
];
2428 if (start_segno
< dev_start_segno
|| start_segno
>= dev_end_segno
)
2429 start_segno
= dev_start_segno
;
2430 end_segno
= min(start_segno
+ range
.segments
, dev_end_segno
);
2432 while (start_segno
< end_segno
) {
2433 if (!mutex_trylock(&sbi
->gc_mutex
)) {
2437 sm
->last_victim
[GC_CB
] = end_segno
+ 1;
2438 sm
->last_victim
[GC_GREEDY
] = end_segno
+ 1;
2439 sm
->last_victim
[ALLOC_NEXT
] = end_segno
+ 1;
2440 ret
= f2fs_gc(sbi
, true, true, start_segno
);
2448 mnt_drop_write_file(filp
);
2452 static int f2fs_ioc_get_features(struct file
*filp
, unsigned long arg
)
2454 struct inode
*inode
= file_inode(filp
);
2455 u32 sb_feature
= le32_to_cpu(F2FS_I_SB(inode
)->raw_super
->feature
);
2457 /* Must validate to set it with SQLite behavior in Android. */
2458 sb_feature
|= F2FS_FEATURE_ATOMIC_WRITE
;
2460 return put_user(sb_feature
, (u32 __user
*)arg
);
2464 static int f2fs_ioc_setproject(struct file
*filp
, __u32 projid
)
2466 struct inode
*inode
= file_inode(filp
);
2467 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
2468 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2469 struct super_block
*sb
= sbi
->sb
;
2470 struct dquot
*transfer_to
[MAXQUOTAS
] = {};
2475 if (!f2fs_sb_has_project_quota(sb
)) {
2476 if (projid
!= F2FS_DEF_PROJID
)
2482 if (!f2fs_has_extra_attr(inode
))
2485 kprojid
= make_kprojid(&init_user_ns
, (projid_t
)projid
);
2487 if (projid_eq(kprojid
, F2FS_I(inode
)->i_projid
))
2490 err
= mnt_want_write_file(filp
);
2497 /* Is it quota file? Do not allow user to mess with it */
2498 if (IS_NOQUOTA(inode
))
2501 ipage
= get_node_page(sbi
, inode
->i_ino
);
2502 if (IS_ERR(ipage
)) {
2503 err
= PTR_ERR(ipage
);
2507 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage
), fi
->i_extra_isize
,
2510 f2fs_put_page(ipage
, 1);
2513 f2fs_put_page(ipage
, 1);
2515 err
= dquot_initialize(inode
);
2519 transfer_to
[PRJQUOTA
] = dqget(sb
, make_kqid_projid(kprojid
));
2520 if (!IS_ERR(transfer_to
[PRJQUOTA
])) {
2521 err
= __dquot_transfer(inode
, transfer_to
);
2522 dqput(transfer_to
[PRJQUOTA
]);
2527 F2FS_I(inode
)->i_projid
= kprojid
;
2528 inode
->i_ctime
= current_time(inode
);
2530 f2fs_mark_inode_dirty_sync(inode
, true);
2532 inode_unlock(inode
);
2533 mnt_drop_write_file(filp
);
2537 static int f2fs_ioc_setproject(struct file
*filp
, __u32 projid
)
2539 if (projid
!= F2FS_DEF_PROJID
)
2545 /* Transfer internal flags to xflags */
2546 static inline __u32
f2fs_iflags_to_xflags(unsigned long iflags
)
2550 if (iflags
& FS_SYNC_FL
)
2551 xflags
|= FS_XFLAG_SYNC
;
2552 if (iflags
& FS_IMMUTABLE_FL
)
2553 xflags
|= FS_XFLAG_IMMUTABLE
;
2554 if (iflags
& FS_APPEND_FL
)
2555 xflags
|= FS_XFLAG_APPEND
;
2556 if (iflags
& FS_NODUMP_FL
)
2557 xflags
|= FS_XFLAG_NODUMP
;
2558 if (iflags
& FS_NOATIME_FL
)
2559 xflags
|= FS_XFLAG_NOATIME
;
2560 if (iflags
& FS_PROJINHERIT_FL
)
2561 xflags
|= FS_XFLAG_PROJINHERIT
;
2565 #define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2566 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2567 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2569 /* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
2570 #define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \
2577 /* Transfer xflags flags to internal */
2578 static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags
)
2580 unsigned long iflags
= 0;
2582 if (xflags
& FS_XFLAG_SYNC
)
2583 iflags
|= FS_SYNC_FL
;
2584 if (xflags
& FS_XFLAG_IMMUTABLE
)
2585 iflags
|= FS_IMMUTABLE_FL
;
2586 if (xflags
& FS_XFLAG_APPEND
)
2587 iflags
|= FS_APPEND_FL
;
2588 if (xflags
& FS_XFLAG_NODUMP
)
2589 iflags
|= FS_NODUMP_FL
;
2590 if (xflags
& FS_XFLAG_NOATIME
)
2591 iflags
|= FS_NOATIME_FL
;
2592 if (xflags
& FS_XFLAG_PROJINHERIT
)
2593 iflags
|= FS_PROJINHERIT_FL
;
2598 static int f2fs_ioc_fsgetxattr(struct file
*filp
, unsigned long arg
)
2600 struct inode
*inode
= file_inode(filp
);
2601 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
2604 memset(&fa
, 0, sizeof(struct fsxattr
));
2605 fa
.fsx_xflags
= f2fs_iflags_to_xflags(fi
->i_flags
&
2606 (FS_FL_USER_VISIBLE
| FS_PROJINHERIT_FL
));
2608 if (f2fs_sb_has_project_quota(inode
->i_sb
))
2609 fa
.fsx_projid
= (__u32
)from_kprojid(&init_user_ns
,
2612 if (copy_to_user((struct fsxattr __user
*)arg
, &fa
, sizeof(fa
)))
2617 static int f2fs_ioc_fssetxattr(struct file
*filp
, unsigned long arg
)
2619 struct inode
*inode
= file_inode(filp
);
2620 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
2625 if (copy_from_user(&fa
, (struct fsxattr __user
*)arg
, sizeof(fa
)))
2628 /* Make sure caller has proper permission */
2629 if (!inode_owner_or_capable(inode
))
2632 if (fa
.fsx_xflags
& ~F2FS_SUPPORTED_FS_XFLAGS
)
2635 flags
= f2fs_xflags_to_iflags(fa
.fsx_xflags
);
2636 if (f2fs_mask_flags(inode
->i_mode
, flags
) != flags
)
2639 err
= mnt_want_write_file(filp
);
2644 flags
= (fi
->i_flags
& ~F2FS_FL_XFLAG_VISIBLE
) |
2645 (flags
& F2FS_FL_XFLAG_VISIBLE
);
2646 err
= __f2fs_ioc_setflags(inode
, flags
);
2647 inode_unlock(inode
);
2648 mnt_drop_write_file(filp
);
2652 err
= f2fs_ioc_setproject(filp
, fa
.fsx_projid
);
2659 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2662 case F2FS_IOC_GETFLAGS
:
2663 return f2fs_ioc_getflags(filp
, arg
);
2664 case F2FS_IOC_SETFLAGS
:
2665 return f2fs_ioc_setflags(filp
, arg
);
2666 case F2FS_IOC_GETVERSION
:
2667 return f2fs_ioc_getversion(filp
, arg
);
2668 case F2FS_IOC_START_ATOMIC_WRITE
:
2669 return f2fs_ioc_start_atomic_write(filp
);
2670 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
2671 return f2fs_ioc_commit_atomic_write(filp
);
2672 case F2FS_IOC_START_VOLATILE_WRITE
:
2673 return f2fs_ioc_start_volatile_write(filp
);
2674 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
2675 return f2fs_ioc_release_volatile_write(filp
);
2676 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
2677 return f2fs_ioc_abort_volatile_write(filp
);
2678 case F2FS_IOC_SHUTDOWN
:
2679 return f2fs_ioc_shutdown(filp
, arg
);
2681 return f2fs_ioc_fitrim(filp
, arg
);
2682 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
2683 return f2fs_ioc_set_encryption_policy(filp
, arg
);
2684 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
2685 return f2fs_ioc_get_encryption_policy(filp
, arg
);
2686 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
2687 return f2fs_ioc_get_encryption_pwsalt(filp
, arg
);
2688 case F2FS_IOC_GARBAGE_COLLECT
:
2689 return f2fs_ioc_gc(filp
, arg
);
2690 case F2FS_IOC_GARBAGE_COLLECT_RANGE
:
2691 return f2fs_ioc_gc_range(filp
, arg
);
2692 case F2FS_IOC_WRITE_CHECKPOINT
:
2693 return f2fs_ioc_write_checkpoint(filp
, arg
);
2694 case F2FS_IOC_DEFRAGMENT
:
2695 return f2fs_ioc_defragment(filp
, arg
);
2696 case F2FS_IOC_MOVE_RANGE
:
2697 return f2fs_ioc_move_range(filp
, arg
);
2698 case F2FS_IOC_FLUSH_DEVICE
:
2699 return f2fs_ioc_flush_device(filp
, arg
);
2700 case F2FS_IOC_GET_FEATURES
:
2701 return f2fs_ioc_get_features(filp
, arg
);
2702 case F2FS_IOC_FSGETXATTR
:
2703 return f2fs_ioc_fsgetxattr(filp
, arg
);
2704 case F2FS_IOC_FSSETXATTR
:
2705 return f2fs_ioc_fssetxattr(filp
, arg
);
2711 static ssize_t
f2fs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
2713 struct file
*file
= iocb
->ki_filp
;
2714 struct inode
*inode
= file_inode(file
);
2715 struct blk_plug plug
;
2719 ret
= generic_write_checks(iocb
, from
);
2721 bool preallocated
= false;
2722 size_t target_size
= 0;
2725 if (iov_iter_fault_in_readable(from
, iov_iter_count(from
)))
2726 set_inode_flag(inode
, FI_NO_PREALLOC
);
2728 preallocated
= true;
2729 target_size
= iocb
->ki_pos
+ iov_iter_count(from
);
2731 err
= f2fs_preallocate_blocks(iocb
, from
);
2733 clear_inode_flag(inode
, FI_NO_PREALLOC
);
2734 inode_unlock(inode
);
2737 blk_start_plug(&plug
);
2738 ret
= __generic_file_write_iter(iocb
, from
);
2739 blk_finish_plug(&plug
);
2740 clear_inode_flag(inode
, FI_NO_PREALLOC
);
2742 /* if we couldn't write data, we should deallocate blocks. */
2743 if (preallocated
&& i_size_read(inode
) < target_size
)
2744 f2fs_truncate(inode
);
2747 f2fs_update_iostat(F2FS_I_SB(inode
), APP_WRITE_IO
, ret
);
2749 inode_unlock(inode
);
2752 ret
= generic_write_sync(iocb
, ret
);
2756 #ifdef CONFIG_COMPAT
2757 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2760 case F2FS_IOC32_GETFLAGS
:
2761 cmd
= F2FS_IOC_GETFLAGS
;
2763 case F2FS_IOC32_SETFLAGS
:
2764 cmd
= F2FS_IOC_SETFLAGS
;
2766 case F2FS_IOC32_GETVERSION
:
2767 cmd
= F2FS_IOC_GETVERSION
;
2769 case F2FS_IOC_START_ATOMIC_WRITE
:
2770 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
2771 case F2FS_IOC_START_VOLATILE_WRITE
:
2772 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
2773 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
2774 case F2FS_IOC_SHUTDOWN
:
2775 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
2776 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
2777 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
2778 case F2FS_IOC_GARBAGE_COLLECT
:
2779 case F2FS_IOC_GARBAGE_COLLECT_RANGE
:
2780 case F2FS_IOC_WRITE_CHECKPOINT
:
2781 case F2FS_IOC_DEFRAGMENT
:
2782 case F2FS_IOC_MOVE_RANGE
:
2783 case F2FS_IOC_FLUSH_DEVICE
:
2784 case F2FS_IOC_GET_FEATURES
:
2785 case F2FS_IOC_FSGETXATTR
:
2786 case F2FS_IOC_FSSETXATTR
:
2789 return -ENOIOCTLCMD
;
2791 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
2795 const struct file_operations f2fs_file_operations
= {
2796 .llseek
= f2fs_llseek
,
2797 .read_iter
= generic_file_read_iter
,
2798 .write_iter
= f2fs_file_write_iter
,
2799 .open
= f2fs_file_open
,
2800 .release
= f2fs_release_file
,
2801 .mmap
= f2fs_file_mmap
,
2802 .flush
= f2fs_file_flush
,
2803 .fsync
= f2fs_sync_file
,
2804 .fallocate
= f2fs_fallocate
,
2805 .unlocked_ioctl
= f2fs_ioctl
,
2806 #ifdef CONFIG_COMPAT
2807 .compat_ioctl
= f2fs_compat_ioctl
,
2809 .splice_read
= generic_file_splice_read
,
2810 .splice_write
= iter_file_splice_write
,