1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/buffer_head.h>
12 #include <linux/writeback.h>
13 #include <linux/blkdev.h>
14 #include <linux/falloc.h>
15 #include <linux/types.h>
16 #include <linux/compat.h>
17 #include <linux/uaccess.h>
18 #include <linux/mount.h>
19 #include <linux/pagevec.h>
20 #include <linux/uio.h>
21 #include <linux/uuid.h>
22 #include <linux/file.h>
31 #include <trace/events/f2fs.h>
33 static vm_fault_t
f2fs_filemap_fault(struct vm_fault
*vmf
)
35 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
38 down_read(&F2FS_I(inode
)->i_mmap_sem
);
39 ret
= filemap_fault(vmf
);
40 up_read(&F2FS_I(inode
)->i_mmap_sem
);
42 trace_f2fs_filemap_fault(inode
, vmf
->pgoff
, (unsigned long)ret
);
47 static vm_fault_t
f2fs_vm_page_mkwrite(struct vm_fault
*vmf
)
49 struct page
*page
= vmf
->page
;
50 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
51 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
52 struct dnode_of_data dn
= { .node_changed
= false };
55 if (unlikely(f2fs_cp_error(sbi
))) {
60 sb_start_pagefault(inode
->i_sb
);
62 f2fs_bug_on(sbi
, f2fs_has_inline_data(inode
));
64 file_update_time(vmf
->vma
->vm_file
);
65 down_read(&F2FS_I(inode
)->i_mmap_sem
);
67 if (unlikely(page
->mapping
!= inode
->i_mapping
||
68 page_offset(page
) > i_size_read(inode
) ||
69 !PageUptodate(page
))) {
75 /* block allocation */
76 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, true);
77 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
78 err
= f2fs_get_block(&dn
, page
->index
);
80 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, false);
87 f2fs_wait_on_page_writeback(page
, DATA
, false, true);
89 /* wait for GCed page writeback via META_MAPPING */
90 f2fs_wait_on_block_writeback(inode
, dn
.data_blkaddr
);
93 * check to see if the page is mapped already (no holes)
95 if (PageMappedToDisk(page
))
98 /* page is wholly or partially inside EOF */
99 if (((loff_t
)(page
->index
+ 1) << PAGE_SHIFT
) >
100 i_size_read(inode
)) {
103 offset
= i_size_read(inode
) & ~PAGE_MASK
;
104 zero_user_segment(page
, offset
, PAGE_SIZE
);
106 set_page_dirty(page
);
107 if (!PageUptodate(page
))
108 SetPageUptodate(page
);
110 f2fs_update_iostat(sbi
, APP_MAPPED_IO
, F2FS_BLKSIZE
);
111 f2fs_update_time(sbi
, REQ_TIME
);
113 trace_f2fs_vm_page_mkwrite(page
, DATA
);
115 up_read(&F2FS_I(inode
)->i_mmap_sem
);
117 f2fs_balance_fs(sbi
, dn
.node_changed
);
119 sb_end_pagefault(inode
->i_sb
);
121 return block_page_mkwrite_return(err
);
124 static const struct vm_operations_struct f2fs_file_vm_ops
= {
125 .fault
= f2fs_filemap_fault
,
126 .map_pages
= filemap_map_pages
,
127 .page_mkwrite
= f2fs_vm_page_mkwrite
,
130 static int get_parent_ino(struct inode
*inode
, nid_t
*pino
)
132 struct dentry
*dentry
;
134 inode
= igrab(inode
);
135 dentry
= d_find_any_alias(inode
);
140 *pino
= parent_ino(dentry
);
145 static inline enum cp_reason_type
need_do_checkpoint(struct inode
*inode
)
147 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
148 enum cp_reason_type cp_reason
= CP_NO_NEEDED
;
150 if (!S_ISREG(inode
->i_mode
))
151 cp_reason
= CP_NON_REGULAR
;
152 else if (inode
->i_nlink
!= 1)
153 cp_reason
= CP_HARDLINK
;
154 else if (is_sbi_flag_set(sbi
, SBI_NEED_CP
))
155 cp_reason
= CP_SB_NEED_CP
;
156 else if (file_wrong_pino(inode
))
157 cp_reason
= CP_WRONG_PINO
;
158 else if (!f2fs_space_for_roll_forward(sbi
))
159 cp_reason
= CP_NO_SPC_ROLL
;
160 else if (!f2fs_is_checkpointed_node(sbi
, F2FS_I(inode
)->i_pino
))
161 cp_reason
= CP_NODE_NEED_CP
;
162 else if (test_opt(sbi
, FASTBOOT
))
163 cp_reason
= CP_FASTBOOT_MODE
;
164 else if (F2FS_OPTION(sbi
).active_logs
== 2)
165 cp_reason
= CP_SPEC_LOG_NUM
;
166 else if (F2FS_OPTION(sbi
).fsync_mode
== FSYNC_MODE_STRICT
&&
167 f2fs_need_dentry_mark(sbi
, inode
->i_ino
) &&
168 f2fs_exist_written_data(sbi
, F2FS_I(inode
)->i_pino
,
170 cp_reason
= CP_RECOVER_DIR
;
175 static bool need_inode_page_update(struct f2fs_sb_info
*sbi
, nid_t ino
)
177 struct page
*i
= find_get_page(NODE_MAPPING(sbi
), ino
);
179 /* But we need to avoid that there are some inode updates */
180 if ((i
&& PageDirty(i
)) || f2fs_need_inode_block_update(sbi
, ino
))
186 static void try_to_fix_pino(struct inode
*inode
)
188 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
191 down_write(&fi
->i_sem
);
192 if (file_wrong_pino(inode
) && inode
->i_nlink
== 1 &&
193 get_parent_ino(inode
, &pino
)) {
194 f2fs_i_pino_write(inode
, pino
);
195 file_got_pino(inode
);
197 up_write(&fi
->i_sem
);
200 static int f2fs_do_sync_file(struct file
*file
, loff_t start
, loff_t end
,
201 int datasync
, bool atomic
)
203 struct inode
*inode
= file
->f_mapping
->host
;
204 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
205 nid_t ino
= inode
->i_ino
;
207 enum cp_reason_type cp_reason
= 0;
208 struct writeback_control wbc
= {
209 .sync_mode
= WB_SYNC_ALL
,
210 .nr_to_write
= LONG_MAX
,
213 unsigned int seq_id
= 0;
215 if (unlikely(f2fs_readonly(inode
->i_sb
) ||
216 is_sbi_flag_set(sbi
, SBI_CP_DISABLED
)))
219 trace_f2fs_sync_file_enter(inode
);
221 if (S_ISDIR(inode
->i_mode
))
224 /* if fdatasync is triggered, let's do in-place-update */
225 if (datasync
|| get_dirty_pages(inode
) <= SM_I(sbi
)->min_fsync_blocks
)
226 set_inode_flag(inode
, FI_NEED_IPU
);
227 ret
= file_write_and_wait_range(file
, start
, end
);
228 clear_inode_flag(inode
, FI_NEED_IPU
);
231 trace_f2fs_sync_file_exit(inode
, cp_reason
, datasync
, ret
);
235 /* if the inode is dirty, let's recover all the time */
236 if (!f2fs_skip_inode_update(inode
, datasync
)) {
237 f2fs_write_inode(inode
, NULL
);
242 * if there is no written data, don't waste time to write recovery info.
244 if (!is_inode_flag_set(inode
, FI_APPEND_WRITE
) &&
245 !f2fs_exist_written_data(sbi
, ino
, APPEND_INO
)) {
247 /* it may call write_inode just prior to fsync */
248 if (need_inode_page_update(sbi
, ino
))
251 if (is_inode_flag_set(inode
, FI_UPDATE_WRITE
) ||
252 f2fs_exist_written_data(sbi
, ino
, UPDATE_INO
))
258 * Both of fdatasync() and fsync() are able to be recovered from
261 down_read(&F2FS_I(inode
)->i_sem
);
262 cp_reason
= need_do_checkpoint(inode
);
263 up_read(&F2FS_I(inode
)->i_sem
);
266 /* all the dirty node pages should be flushed for POR */
267 ret
= f2fs_sync_fs(inode
->i_sb
, 1);
270 * We've secured consistency through sync_fs. Following pino
271 * will be used only for fsynced inodes after checkpoint.
273 try_to_fix_pino(inode
);
274 clear_inode_flag(inode
, FI_APPEND_WRITE
);
275 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
279 atomic_inc(&sbi
->wb_sync_req
[NODE
]);
280 ret
= f2fs_fsync_node_pages(sbi
, inode
, &wbc
, atomic
, &seq_id
);
281 atomic_dec(&sbi
->wb_sync_req
[NODE
]);
285 /* if cp_error was enabled, we should avoid infinite loop */
286 if (unlikely(f2fs_cp_error(sbi
))) {
291 if (f2fs_need_inode_block_update(sbi
, ino
)) {
292 f2fs_mark_inode_dirty_sync(inode
, true);
293 f2fs_write_inode(inode
, NULL
);
298 * If it's atomic_write, it's just fine to keep write ordering. So
299 * here we don't need to wait for node write completion, since we use
300 * node chain which serializes node blocks. If one of node writes are
301 * reordered, we can see simply broken chain, resulting in stopping
302 * roll-forward recovery. It means we'll recover all or none node blocks
306 ret
= f2fs_wait_on_node_pages_writeback(sbi
, seq_id
);
311 /* once recovery info is written, don't need to tack this */
312 f2fs_remove_ino_entry(sbi
, ino
, APPEND_INO
);
313 clear_inode_flag(inode
, FI_APPEND_WRITE
);
315 if (!atomic
&& F2FS_OPTION(sbi
).fsync_mode
!= FSYNC_MODE_NOBARRIER
)
316 ret
= f2fs_issue_flush(sbi
, inode
->i_ino
);
318 f2fs_remove_ino_entry(sbi
, ino
, UPDATE_INO
);
319 clear_inode_flag(inode
, FI_UPDATE_WRITE
);
320 f2fs_remove_ino_entry(sbi
, ino
, FLUSH_INO
);
322 f2fs_update_time(sbi
, REQ_TIME
);
324 trace_f2fs_sync_file_exit(inode
, cp_reason
, datasync
, ret
);
325 f2fs_trace_ios(NULL
, 1);
329 int f2fs_sync_file(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
331 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file
)))))
333 return f2fs_do_sync_file(file
, start
, end
, datasync
, false);
336 static pgoff_t
__get_first_dirty_index(struct address_space
*mapping
,
337 pgoff_t pgofs
, int whence
)
342 if (whence
!= SEEK_DATA
)
345 /* find first dirty page index */
346 nr_pages
= find_get_pages_tag(mapping
, &pgofs
, PAGECACHE_TAG_DIRTY
,
355 static bool __found_offset(struct f2fs_sb_info
*sbi
, block_t blkaddr
,
356 pgoff_t dirty
, pgoff_t pgofs
, int whence
)
360 if ((blkaddr
== NEW_ADDR
&& dirty
== pgofs
) ||
361 __is_valid_data_blkaddr(blkaddr
))
365 if (blkaddr
== NULL_ADDR
)
372 static loff_t
f2fs_seek_block(struct file
*file
, loff_t offset
, int whence
)
374 struct inode
*inode
= file
->f_mapping
->host
;
375 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
376 struct dnode_of_data dn
;
377 pgoff_t pgofs
, end_offset
, dirty
;
378 loff_t data_ofs
= offset
;
384 isize
= i_size_read(inode
);
388 /* handle inline data case */
389 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
390 if (whence
== SEEK_HOLE
)
395 pgofs
= (pgoff_t
)(offset
>> PAGE_SHIFT
);
397 dirty
= __get_first_dirty_index(inode
->i_mapping
, pgofs
, whence
);
399 for (; data_ofs
< isize
; data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
400 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
401 err
= f2fs_get_dnode_of_data(&dn
, pgofs
, LOOKUP_NODE
);
402 if (err
&& err
!= -ENOENT
) {
404 } else if (err
== -ENOENT
) {
405 /* direct node does not exists */
406 if (whence
== SEEK_DATA
) {
407 pgofs
= f2fs_get_next_page_offset(&dn
, pgofs
);
414 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
416 /* find data/hole in dnode block */
417 for (; dn
.ofs_in_node
< end_offset
;
418 dn
.ofs_in_node
++, pgofs
++,
419 data_ofs
= (loff_t
)pgofs
<< PAGE_SHIFT
) {
422 blkaddr
= datablock_addr(dn
.inode
,
423 dn
.node_page
, dn
.ofs_in_node
);
425 if (__is_valid_data_blkaddr(blkaddr
) &&
426 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode
),
427 blkaddr
, DATA_GENERIC_ENHANCE
)) {
432 if (__found_offset(F2FS_I_SB(inode
), blkaddr
, dirty
,
441 if (whence
== SEEK_DATA
)
444 if (whence
== SEEK_HOLE
&& data_ofs
> isize
)
447 return vfs_setpos(file
, data_ofs
, maxbytes
);
453 static loff_t
f2fs_llseek(struct file
*file
, loff_t offset
, int whence
)
455 struct inode
*inode
= file
->f_mapping
->host
;
456 loff_t maxbytes
= inode
->i_sb
->s_maxbytes
;
462 return generic_file_llseek_size(file
, offset
, whence
,
463 maxbytes
, i_size_read(inode
));
468 return f2fs_seek_block(file
, offset
, whence
);
474 static int f2fs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
476 struct inode
*inode
= file_inode(file
);
479 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
))))
482 /* we don't need to use inline_data strictly */
483 err
= f2fs_convert_inline_inode(inode
);
488 vma
->vm_ops
= &f2fs_file_vm_ops
;
492 static int f2fs_file_open(struct inode
*inode
, struct file
*filp
)
494 int err
= fscrypt_file_open(inode
, filp
);
499 filp
->f_mode
|= FMODE_NOWAIT
;
501 return dquot_file_open(inode
, filp
);
504 void f2fs_truncate_data_blocks_range(struct dnode_of_data
*dn
, int count
)
506 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
507 struct f2fs_node
*raw_node
;
508 int nr_free
= 0, ofs
= dn
->ofs_in_node
, len
= count
;
512 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
513 base
= get_extra_isize(dn
->inode
);
515 raw_node
= F2FS_NODE(dn
->node_page
);
516 addr
= blkaddr_in_node(raw_node
) + base
+ ofs
;
518 for (; count
> 0; count
--, addr
++, dn
->ofs_in_node
++) {
519 block_t blkaddr
= le32_to_cpu(*addr
);
521 if (blkaddr
== NULL_ADDR
)
524 dn
->data_blkaddr
= NULL_ADDR
;
525 f2fs_set_data_blkaddr(dn
);
527 if (__is_valid_data_blkaddr(blkaddr
) &&
528 !f2fs_is_valid_blkaddr(sbi
, blkaddr
,
529 DATA_GENERIC_ENHANCE
))
532 f2fs_invalidate_blocks(sbi
, blkaddr
);
533 if (dn
->ofs_in_node
== 0 && IS_INODE(dn
->node_page
))
534 clear_inode_flag(dn
->inode
, FI_FIRST_BLOCK_WRITTEN
);
541 * once we invalidate valid blkaddr in range [ofs, ofs + count],
542 * we will invalidate all blkaddr in the whole range.
544 fofs
= f2fs_start_bidx_of_node(ofs_of_node(dn
->node_page
),
546 f2fs_update_extent_cache_range(dn
, fofs
, 0, len
);
547 dec_valid_block_count(sbi
, dn
->inode
, nr_free
);
549 dn
->ofs_in_node
= ofs
;
551 f2fs_update_time(sbi
, REQ_TIME
);
552 trace_f2fs_truncate_data_blocks_range(dn
->inode
, dn
->nid
,
553 dn
->ofs_in_node
, nr_free
);
556 void f2fs_truncate_data_blocks(struct dnode_of_data
*dn
)
558 f2fs_truncate_data_blocks_range(dn
, ADDRS_PER_BLOCK(dn
->inode
));
561 static int truncate_partial_data_page(struct inode
*inode
, u64 from
,
564 loff_t offset
= from
& (PAGE_SIZE
- 1);
565 pgoff_t index
= from
>> PAGE_SHIFT
;
566 struct address_space
*mapping
= inode
->i_mapping
;
569 if (!offset
&& !cache_only
)
573 page
= find_lock_page(mapping
, index
);
574 if (page
&& PageUptodate(page
))
576 f2fs_put_page(page
, 1);
580 page
= f2fs_get_lock_data_page(inode
, index
, true);
582 return PTR_ERR(page
) == -ENOENT
? 0 : PTR_ERR(page
);
584 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
585 zero_user(page
, offset
, PAGE_SIZE
- offset
);
587 /* An encrypted inode should have a key and truncate the last page. */
588 f2fs_bug_on(F2FS_I_SB(inode
), cache_only
&& IS_ENCRYPTED(inode
));
590 set_page_dirty(page
);
591 f2fs_put_page(page
, 1);
595 int f2fs_truncate_blocks(struct inode
*inode
, u64 from
, bool lock
)
597 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
598 struct dnode_of_data dn
;
600 int count
= 0, err
= 0;
602 bool truncate_page
= false;
604 trace_f2fs_truncate_blocks_enter(inode
, from
);
606 free_from
= (pgoff_t
)F2FS_BLK_ALIGN(from
);
608 if (free_from
>= sbi
->max_file_blocks
)
614 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
616 err
= PTR_ERR(ipage
);
620 if (f2fs_has_inline_data(inode
)) {
621 f2fs_truncate_inline_inode(inode
, ipage
, from
);
622 f2fs_put_page(ipage
, 1);
623 truncate_page
= true;
627 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
628 err
= f2fs_get_dnode_of_data(&dn
, free_from
, LOOKUP_NODE_RA
);
635 count
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
637 count
-= dn
.ofs_in_node
;
638 f2fs_bug_on(sbi
, count
< 0);
640 if (dn
.ofs_in_node
|| IS_INODE(dn
.node_page
)) {
641 f2fs_truncate_data_blocks_range(&dn
, count
);
647 err
= f2fs_truncate_inode_blocks(inode
, free_from
);
652 /* lastly zero out the first data page */
654 err
= truncate_partial_data_page(inode
, from
, truncate_page
);
656 trace_f2fs_truncate_blocks_exit(inode
, err
);
660 int f2fs_truncate(struct inode
*inode
)
664 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
))))
667 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
668 S_ISLNK(inode
->i_mode
)))
671 trace_f2fs_truncate(inode
);
673 if (time_to_inject(F2FS_I_SB(inode
), FAULT_TRUNCATE
)) {
674 f2fs_show_injection_info(FAULT_TRUNCATE
);
678 /* we should check inline_data size */
679 if (!f2fs_may_inline_data(inode
)) {
680 err
= f2fs_convert_inline_inode(inode
);
685 err
= f2fs_truncate_blocks(inode
, i_size_read(inode
), true);
689 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
690 f2fs_mark_inode_dirty_sync(inode
, false);
694 int f2fs_getattr(const struct path
*path
, struct kstat
*stat
,
695 u32 request_mask
, unsigned int query_flags
)
697 struct inode
*inode
= d_inode(path
->dentry
);
698 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
699 struct f2fs_inode
*ri
;
702 if (f2fs_has_extra_attr(inode
) &&
703 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode
)) &&
704 F2FS_FITS_IN_INODE(ri
, fi
->i_extra_isize
, i_crtime
)) {
705 stat
->result_mask
|= STATX_BTIME
;
706 stat
->btime
.tv_sec
= fi
->i_crtime
.tv_sec
;
707 stat
->btime
.tv_nsec
= fi
->i_crtime
.tv_nsec
;
711 if (flags
& F2FS_APPEND_FL
)
712 stat
->attributes
|= STATX_ATTR_APPEND
;
713 if (IS_ENCRYPTED(inode
))
714 stat
->attributes
|= STATX_ATTR_ENCRYPTED
;
715 if (flags
& F2FS_IMMUTABLE_FL
)
716 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
717 if (flags
& F2FS_NODUMP_FL
)
718 stat
->attributes
|= STATX_ATTR_NODUMP
;
720 stat
->attributes_mask
|= (STATX_ATTR_APPEND
|
721 STATX_ATTR_ENCRYPTED
|
722 STATX_ATTR_IMMUTABLE
|
725 generic_fillattr(inode
, stat
);
727 /* we need to show initial sectors used for inline_data/dentries */
728 if ((S_ISREG(inode
->i_mode
) && f2fs_has_inline_data(inode
)) ||
729 f2fs_has_inline_dentry(inode
))
730 stat
->blocks
+= (stat
->size
+ 511) >> 9;
735 #ifdef CONFIG_F2FS_FS_POSIX_ACL
736 static void __setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
738 unsigned int ia_valid
= attr
->ia_valid
;
740 if (ia_valid
& ATTR_UID
)
741 inode
->i_uid
= attr
->ia_uid
;
742 if (ia_valid
& ATTR_GID
)
743 inode
->i_gid
= attr
->ia_gid
;
744 if (ia_valid
& ATTR_ATIME
)
745 inode
->i_atime
= timespec64_trunc(attr
->ia_atime
,
746 inode
->i_sb
->s_time_gran
);
747 if (ia_valid
& ATTR_MTIME
)
748 inode
->i_mtime
= timespec64_trunc(attr
->ia_mtime
,
749 inode
->i_sb
->s_time_gran
);
750 if (ia_valid
& ATTR_CTIME
)
751 inode
->i_ctime
= timespec64_trunc(attr
->ia_ctime
,
752 inode
->i_sb
->s_time_gran
);
753 if (ia_valid
& ATTR_MODE
) {
754 umode_t mode
= attr
->ia_mode
;
756 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
758 set_acl_inode(inode
, mode
);
762 #define __setattr_copy setattr_copy
765 int f2fs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
767 struct inode
*inode
= d_inode(dentry
);
770 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
))))
773 err
= setattr_prepare(dentry
, attr
);
777 err
= fscrypt_prepare_setattr(dentry
, attr
);
781 if (is_quota_modification(inode
, attr
)) {
782 err
= dquot_initialize(inode
);
786 if ((attr
->ia_valid
& ATTR_UID
&&
787 !uid_eq(attr
->ia_uid
, inode
->i_uid
)) ||
788 (attr
->ia_valid
& ATTR_GID
&&
789 !gid_eq(attr
->ia_gid
, inode
->i_gid
))) {
790 f2fs_lock_op(F2FS_I_SB(inode
));
791 err
= dquot_transfer(inode
, attr
);
793 set_sbi_flag(F2FS_I_SB(inode
),
794 SBI_QUOTA_NEED_REPAIR
);
795 f2fs_unlock_op(F2FS_I_SB(inode
));
799 * update uid/gid under lock_op(), so that dquot and inode can
800 * be updated atomically.
802 if (attr
->ia_valid
& ATTR_UID
)
803 inode
->i_uid
= attr
->ia_uid
;
804 if (attr
->ia_valid
& ATTR_GID
)
805 inode
->i_gid
= attr
->ia_gid
;
806 f2fs_mark_inode_dirty_sync(inode
, true);
807 f2fs_unlock_op(F2FS_I_SB(inode
));
810 if (attr
->ia_valid
& ATTR_SIZE
) {
811 bool to_smaller
= (attr
->ia_size
<= i_size_read(inode
));
813 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
814 down_write(&F2FS_I(inode
)->i_mmap_sem
);
816 truncate_setsize(inode
, attr
->ia_size
);
819 err
= f2fs_truncate(inode
);
821 * do not trim all blocks after i_size if target size is
822 * larger than i_size.
824 up_write(&F2FS_I(inode
)->i_mmap_sem
);
825 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
831 /* should convert inline inode here */
832 if (!f2fs_may_inline_data(inode
)) {
833 err
= f2fs_convert_inline_inode(inode
);
837 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
840 down_write(&F2FS_I(inode
)->i_sem
);
841 F2FS_I(inode
)->last_disk_size
= i_size_read(inode
);
842 up_write(&F2FS_I(inode
)->i_sem
);
845 __setattr_copy(inode
, attr
);
847 if (attr
->ia_valid
& ATTR_MODE
) {
848 err
= posix_acl_chmod(inode
, f2fs_get_inode_mode(inode
));
849 if (err
|| is_inode_flag_set(inode
, FI_ACL_MODE
)) {
850 inode
->i_mode
= F2FS_I(inode
)->i_acl_mode
;
851 clear_inode_flag(inode
, FI_ACL_MODE
);
855 /* file size may changed here */
856 f2fs_mark_inode_dirty_sync(inode
, true);
858 /* inode change will produce dirty node pages flushed by checkpoint */
859 f2fs_balance_fs(F2FS_I_SB(inode
), true);
864 const struct inode_operations f2fs_file_inode_operations
= {
865 .getattr
= f2fs_getattr
,
866 .setattr
= f2fs_setattr
,
867 .get_acl
= f2fs_get_acl
,
868 .set_acl
= f2fs_set_acl
,
869 #ifdef CONFIG_F2FS_FS_XATTR
870 .listxattr
= f2fs_listxattr
,
872 .fiemap
= f2fs_fiemap
,
875 static int fill_zero(struct inode
*inode
, pgoff_t index
,
876 loff_t start
, loff_t len
)
878 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
884 f2fs_balance_fs(sbi
, true);
887 page
= f2fs_get_new_data_page(inode
, NULL
, index
, false);
891 return PTR_ERR(page
);
893 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
894 zero_user(page
, start
, len
);
895 set_page_dirty(page
);
896 f2fs_put_page(page
, 1);
900 int f2fs_truncate_hole(struct inode
*inode
, pgoff_t pg_start
, pgoff_t pg_end
)
904 while (pg_start
< pg_end
) {
905 struct dnode_of_data dn
;
906 pgoff_t end_offset
, count
;
908 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
909 err
= f2fs_get_dnode_of_data(&dn
, pg_start
, LOOKUP_NODE
);
911 if (err
== -ENOENT
) {
912 pg_start
= f2fs_get_next_page_offset(&dn
,
919 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
920 count
= min(end_offset
- dn
.ofs_in_node
, pg_end
- pg_start
);
922 f2fs_bug_on(F2FS_I_SB(inode
), count
== 0 || count
> end_offset
);
924 f2fs_truncate_data_blocks_range(&dn
, count
);
932 static int punch_hole(struct inode
*inode
, loff_t offset
, loff_t len
)
934 pgoff_t pg_start
, pg_end
;
935 loff_t off_start
, off_end
;
938 ret
= f2fs_convert_inline_inode(inode
);
942 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
943 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
945 off_start
= offset
& (PAGE_SIZE
- 1);
946 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
948 if (pg_start
== pg_end
) {
949 ret
= fill_zero(inode
, pg_start
, off_start
,
950 off_end
- off_start
);
955 ret
= fill_zero(inode
, pg_start
++, off_start
,
956 PAGE_SIZE
- off_start
);
961 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
966 if (pg_start
< pg_end
) {
967 struct address_space
*mapping
= inode
->i_mapping
;
968 loff_t blk_start
, blk_end
;
969 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
971 f2fs_balance_fs(sbi
, true);
973 blk_start
= (loff_t
)pg_start
<< PAGE_SHIFT
;
974 blk_end
= (loff_t
)pg_end
<< PAGE_SHIFT
;
976 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
977 down_write(&F2FS_I(inode
)->i_mmap_sem
);
979 truncate_inode_pages_range(mapping
, blk_start
,
983 ret
= f2fs_truncate_hole(inode
, pg_start
, pg_end
);
986 up_write(&F2FS_I(inode
)->i_mmap_sem
);
987 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
994 static int __read_out_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
995 int *do_replace
, pgoff_t off
, pgoff_t len
)
997 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
998 struct dnode_of_data dn
;
1002 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1003 ret
= f2fs_get_dnode_of_data(&dn
, off
, LOOKUP_NODE_RA
);
1004 if (ret
&& ret
!= -ENOENT
) {
1006 } else if (ret
== -ENOENT
) {
1007 if (dn
.max_level
== 0)
1009 done
= min((pgoff_t
)ADDRS_PER_BLOCK(inode
) - dn
.ofs_in_node
,
1016 done
= min((pgoff_t
)ADDRS_PER_PAGE(dn
.node_page
, inode
) -
1017 dn
.ofs_in_node
, len
);
1018 for (i
= 0; i
< done
; i
++, blkaddr
++, do_replace
++, dn
.ofs_in_node
++) {
1019 *blkaddr
= datablock_addr(dn
.inode
,
1020 dn
.node_page
, dn
.ofs_in_node
);
1022 if (__is_valid_data_blkaddr(*blkaddr
) &&
1023 !f2fs_is_valid_blkaddr(sbi
, *blkaddr
,
1024 DATA_GENERIC_ENHANCE
)) {
1025 f2fs_put_dnode(&dn
);
1026 return -EFSCORRUPTED
;
1029 if (!f2fs_is_checkpointed_data(sbi
, *blkaddr
)) {
1031 if (test_opt(sbi
, LFS
)) {
1032 f2fs_put_dnode(&dn
);
1036 /* do not invalidate this block address */
1037 f2fs_update_data_blkaddr(&dn
, NULL_ADDR
);
1041 f2fs_put_dnode(&dn
);
1050 static int __roll_back_blkaddrs(struct inode
*inode
, block_t
*blkaddr
,
1051 int *do_replace
, pgoff_t off
, int len
)
1053 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1054 struct dnode_of_data dn
;
1057 for (i
= 0; i
< len
; i
++, do_replace
++, blkaddr
++) {
1058 if (*do_replace
== 0)
1061 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1062 ret
= f2fs_get_dnode_of_data(&dn
, off
+ i
, LOOKUP_NODE_RA
);
1064 dec_valid_block_count(sbi
, inode
, 1);
1065 f2fs_invalidate_blocks(sbi
, *blkaddr
);
1067 f2fs_update_data_blkaddr(&dn
, *blkaddr
);
1069 f2fs_put_dnode(&dn
);
1074 static int __clone_blkaddrs(struct inode
*src_inode
, struct inode
*dst_inode
,
1075 block_t
*blkaddr
, int *do_replace
,
1076 pgoff_t src
, pgoff_t dst
, pgoff_t len
, bool full
)
1078 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src_inode
);
1083 if (blkaddr
[i
] == NULL_ADDR
&& !full
) {
1088 if (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
) {
1089 struct dnode_of_data dn
;
1090 struct node_info ni
;
1094 set_new_dnode(&dn
, dst_inode
, NULL
, NULL
, 0);
1095 ret
= f2fs_get_dnode_of_data(&dn
, dst
+ i
, ALLOC_NODE
);
1099 ret
= f2fs_get_node_info(sbi
, dn
.nid
, &ni
);
1101 f2fs_put_dnode(&dn
);
1105 ilen
= min((pgoff_t
)
1106 ADDRS_PER_PAGE(dn
.node_page
, dst_inode
) -
1107 dn
.ofs_in_node
, len
- i
);
1109 dn
.data_blkaddr
= datablock_addr(dn
.inode
,
1110 dn
.node_page
, dn
.ofs_in_node
);
1111 f2fs_truncate_data_blocks_range(&dn
, 1);
1113 if (do_replace
[i
]) {
1114 f2fs_i_blocks_write(src_inode
,
1116 f2fs_i_blocks_write(dst_inode
,
1118 f2fs_replace_block(sbi
, &dn
, dn
.data_blkaddr
,
1119 blkaddr
[i
], ni
.version
, true, false);
1125 new_size
= (dst
+ i
) << PAGE_SHIFT
;
1126 if (dst_inode
->i_size
< new_size
)
1127 f2fs_i_size_write(dst_inode
, new_size
);
1128 } while (--ilen
&& (do_replace
[i
] || blkaddr
[i
] == NULL_ADDR
));
1130 f2fs_put_dnode(&dn
);
1132 struct page
*psrc
, *pdst
;
1134 psrc
= f2fs_get_lock_data_page(src_inode
,
1137 return PTR_ERR(psrc
);
1138 pdst
= f2fs_get_new_data_page(dst_inode
, NULL
, dst
+ i
,
1141 f2fs_put_page(psrc
, 1);
1142 return PTR_ERR(pdst
);
1144 f2fs_copy_page(psrc
, pdst
);
1145 set_page_dirty(pdst
);
1146 f2fs_put_page(pdst
, 1);
1147 f2fs_put_page(psrc
, 1);
1149 ret
= f2fs_truncate_hole(src_inode
,
1150 src
+ i
, src
+ i
+ 1);
1159 static int __exchange_data_block(struct inode
*src_inode
,
1160 struct inode
*dst_inode
, pgoff_t src
, pgoff_t dst
,
1161 pgoff_t len
, bool full
)
1163 block_t
*src_blkaddr
;
1169 olen
= min((pgoff_t
)4 * ADDRS_PER_BLOCK(src_inode
), len
);
1171 src_blkaddr
= f2fs_kvzalloc(F2FS_I_SB(src_inode
),
1172 array_size(olen
, sizeof(block_t
)),
1177 do_replace
= f2fs_kvzalloc(F2FS_I_SB(src_inode
),
1178 array_size(olen
, sizeof(int)),
1181 kvfree(src_blkaddr
);
1185 ret
= __read_out_blkaddrs(src_inode
, src_blkaddr
,
1186 do_replace
, src
, olen
);
1190 ret
= __clone_blkaddrs(src_inode
, dst_inode
, src_blkaddr
,
1191 do_replace
, src
, dst
, olen
, full
);
1199 kvfree(src_blkaddr
);
1205 __roll_back_blkaddrs(src_inode
, src_blkaddr
, do_replace
, src
, olen
);
1206 kvfree(src_blkaddr
);
1211 static int f2fs_do_collapse(struct inode
*inode
, loff_t offset
, loff_t len
)
1213 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1214 pgoff_t nrpages
= DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
);
1215 pgoff_t start
= offset
>> PAGE_SHIFT
;
1216 pgoff_t end
= (offset
+ len
) >> PAGE_SHIFT
;
1219 f2fs_balance_fs(sbi
, true);
1221 /* avoid gc operation during block exchange */
1222 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1223 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1226 f2fs_drop_extent_tree(inode
);
1227 truncate_pagecache(inode
, offset
);
1228 ret
= __exchange_data_block(inode
, inode
, end
, start
, nrpages
- end
, true);
1229 f2fs_unlock_op(sbi
);
1231 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1232 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1236 static int f2fs_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1241 if (offset
+ len
>= i_size_read(inode
))
1244 /* collapse range should be aligned to block size of f2fs. */
1245 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1248 ret
= f2fs_convert_inline_inode(inode
);
1252 /* write out all dirty pages from offset */
1253 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1257 ret
= f2fs_do_collapse(inode
, offset
, len
);
1261 /* write out all moved pages, if possible */
1262 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1263 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1264 truncate_pagecache(inode
, offset
);
1266 new_size
= i_size_read(inode
) - len
;
1267 truncate_pagecache(inode
, new_size
);
1269 ret
= f2fs_truncate_blocks(inode
, new_size
, true);
1270 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1272 f2fs_i_size_write(inode
, new_size
);
1276 static int f2fs_do_zero_range(struct dnode_of_data
*dn
, pgoff_t start
,
1279 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1280 pgoff_t index
= start
;
1281 unsigned int ofs_in_node
= dn
->ofs_in_node
;
1285 for (; index
< end
; index
++, dn
->ofs_in_node
++) {
1286 if (datablock_addr(dn
->inode
, dn
->node_page
,
1287 dn
->ofs_in_node
) == NULL_ADDR
)
1291 dn
->ofs_in_node
= ofs_in_node
;
1292 ret
= f2fs_reserve_new_blocks(dn
, count
);
1296 dn
->ofs_in_node
= ofs_in_node
;
1297 for (index
= start
; index
< end
; index
++, dn
->ofs_in_node
++) {
1298 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
1299 dn
->node_page
, dn
->ofs_in_node
);
1301 * f2fs_reserve_new_blocks will not guarantee entire block
1304 if (dn
->data_blkaddr
== NULL_ADDR
) {
1308 if (dn
->data_blkaddr
!= NEW_ADDR
) {
1309 f2fs_invalidate_blocks(sbi
, dn
->data_blkaddr
);
1310 dn
->data_blkaddr
= NEW_ADDR
;
1311 f2fs_set_data_blkaddr(dn
);
1315 f2fs_update_extent_cache_range(dn
, start
, 0, index
- start
);
1320 static int f2fs_zero_range(struct inode
*inode
, loff_t offset
, loff_t len
,
1323 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1324 struct address_space
*mapping
= inode
->i_mapping
;
1325 pgoff_t index
, pg_start
, pg_end
;
1326 loff_t new_size
= i_size_read(inode
);
1327 loff_t off_start
, off_end
;
1330 ret
= inode_newsize_ok(inode
, (len
+ offset
));
1334 ret
= f2fs_convert_inline_inode(inode
);
1338 ret
= filemap_write_and_wait_range(mapping
, offset
, offset
+ len
- 1);
1342 pg_start
= ((unsigned long long) offset
) >> PAGE_SHIFT
;
1343 pg_end
= ((unsigned long long) offset
+ len
) >> PAGE_SHIFT
;
1345 off_start
= offset
& (PAGE_SIZE
- 1);
1346 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1348 if (pg_start
== pg_end
) {
1349 ret
= fill_zero(inode
, pg_start
, off_start
,
1350 off_end
- off_start
);
1354 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1357 ret
= fill_zero(inode
, pg_start
++, off_start
,
1358 PAGE_SIZE
- off_start
);
1362 new_size
= max_t(loff_t
, new_size
,
1363 (loff_t
)pg_start
<< PAGE_SHIFT
);
1366 for (index
= pg_start
; index
< pg_end
;) {
1367 struct dnode_of_data dn
;
1368 unsigned int end_offset
;
1371 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1372 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1374 truncate_pagecache_range(inode
,
1375 (loff_t
)index
<< PAGE_SHIFT
,
1376 ((loff_t
)pg_end
<< PAGE_SHIFT
) - 1);
1380 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1381 ret
= f2fs_get_dnode_of_data(&dn
, index
, ALLOC_NODE
);
1383 f2fs_unlock_op(sbi
);
1384 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1385 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1389 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1390 end
= min(pg_end
, end_offset
- dn
.ofs_in_node
+ index
);
1392 ret
= f2fs_do_zero_range(&dn
, index
, end
);
1393 f2fs_put_dnode(&dn
);
1395 f2fs_unlock_op(sbi
);
1396 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1397 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1399 f2fs_balance_fs(sbi
, dn
.node_changed
);
1405 new_size
= max_t(loff_t
, new_size
,
1406 (loff_t
)index
<< PAGE_SHIFT
);
1410 ret
= fill_zero(inode
, pg_end
, 0, off_end
);
1414 new_size
= max_t(loff_t
, new_size
, offset
+ len
);
1419 if (new_size
> i_size_read(inode
)) {
1420 if (mode
& FALLOC_FL_KEEP_SIZE
)
1421 file_set_keep_isize(inode
);
1423 f2fs_i_size_write(inode
, new_size
);
1428 static int f2fs_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
1430 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1431 pgoff_t nr
, pg_start
, pg_end
, delta
, idx
;
1435 new_size
= i_size_read(inode
) + len
;
1436 ret
= inode_newsize_ok(inode
, new_size
);
1440 if (offset
>= i_size_read(inode
))
1443 /* insert range should be aligned to block size of f2fs. */
1444 if (offset
& (F2FS_BLKSIZE
- 1) || len
& (F2FS_BLKSIZE
- 1))
1447 ret
= f2fs_convert_inline_inode(inode
);
1451 f2fs_balance_fs(sbi
, true);
1453 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1454 ret
= f2fs_truncate_blocks(inode
, i_size_read(inode
), true);
1455 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1459 /* write out all dirty pages from offset */
1460 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1464 pg_start
= offset
>> PAGE_SHIFT
;
1465 pg_end
= (offset
+ len
) >> PAGE_SHIFT
;
1466 delta
= pg_end
- pg_start
;
1467 idx
= DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
);
1469 /* avoid gc operation during block exchange */
1470 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1471 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1472 truncate_pagecache(inode
, offset
);
1474 while (!ret
&& idx
> pg_start
) {
1475 nr
= idx
- pg_start
;
1481 f2fs_drop_extent_tree(inode
);
1483 ret
= __exchange_data_block(inode
, inode
, idx
,
1484 idx
+ delta
, nr
, false);
1485 f2fs_unlock_op(sbi
);
1487 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1488 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1490 /* write out all moved pages, if possible */
1491 down_write(&F2FS_I(inode
)->i_mmap_sem
);
1492 filemap_write_and_wait_range(inode
->i_mapping
, offset
, LLONG_MAX
);
1493 truncate_pagecache(inode
, offset
);
1494 up_write(&F2FS_I(inode
)->i_mmap_sem
);
1497 f2fs_i_size_write(inode
, new_size
);
1501 static int expand_inode_data(struct inode
*inode
, loff_t offset
,
1502 loff_t len
, int mode
)
1504 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1505 struct f2fs_map_blocks map
= { .m_next_pgofs
= NULL
,
1506 .m_next_extent
= NULL
, .m_seg_type
= NO_CHECK_TYPE
,
1507 .m_may_create
= true };
1509 loff_t new_size
= i_size_read(inode
);
1513 err
= inode_newsize_ok(inode
, (len
+ offset
));
1517 err
= f2fs_convert_inline_inode(inode
);
1521 f2fs_balance_fs(sbi
, true);
1523 pg_end
= ((unsigned long long)offset
+ len
) >> PAGE_SHIFT
;
1524 off_end
= (offset
+ len
) & (PAGE_SIZE
- 1);
1526 map
.m_lblk
= ((unsigned long long)offset
) >> PAGE_SHIFT
;
1527 map
.m_len
= pg_end
- map
.m_lblk
;
1531 if (f2fs_is_pinned_file(inode
))
1532 map
.m_seg_type
= CURSEG_COLD_DATA
;
1534 err
= f2fs_map_blocks(inode
, &map
, 1, (f2fs_is_pinned_file(inode
) ?
1535 F2FS_GET_BLOCK_PRE_DIO
:
1536 F2FS_GET_BLOCK_PRE_AIO
));
1543 last_off
= map
.m_lblk
+ map
.m_len
- 1;
1545 /* update new size to the failed position */
1546 new_size
= (last_off
== pg_end
) ? offset
+ len
:
1547 (loff_t
)(last_off
+ 1) << PAGE_SHIFT
;
1549 new_size
= ((loff_t
)pg_end
<< PAGE_SHIFT
) + off_end
;
1552 if (new_size
> i_size_read(inode
)) {
1553 if (mode
& FALLOC_FL_KEEP_SIZE
)
1554 file_set_keep_isize(inode
);
1556 f2fs_i_size_write(inode
, new_size
);
1562 static long f2fs_fallocate(struct file
*file
, int mode
,
1563 loff_t offset
, loff_t len
)
1565 struct inode
*inode
= file_inode(file
);
1568 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
))))
1571 /* f2fs only support ->fallocate for regular file */
1572 if (!S_ISREG(inode
->i_mode
))
1575 if (IS_ENCRYPTED(inode
) &&
1576 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
)))
1579 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
1580 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
1581 FALLOC_FL_INSERT_RANGE
))
1586 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
1587 if (offset
>= inode
->i_size
)
1590 ret
= punch_hole(inode
, offset
, len
);
1591 } else if (mode
& FALLOC_FL_COLLAPSE_RANGE
) {
1592 ret
= f2fs_collapse_range(inode
, offset
, len
);
1593 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
1594 ret
= f2fs_zero_range(inode
, offset
, len
, mode
);
1595 } else if (mode
& FALLOC_FL_INSERT_RANGE
) {
1596 ret
= f2fs_insert_range(inode
, offset
, len
);
1598 ret
= expand_inode_data(inode
, offset
, len
, mode
);
1602 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
1603 f2fs_mark_inode_dirty_sync(inode
, false);
1604 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1608 inode_unlock(inode
);
1610 trace_f2fs_fallocate(inode
, mode
, offset
, len
, ret
);
1614 static int f2fs_release_file(struct inode
*inode
, struct file
*filp
)
1617 * f2fs_relase_file is called at every close calls. So we should
1618 * not drop any inmemory pages by close called by other process.
1620 if (!(filp
->f_mode
& FMODE_WRITE
) ||
1621 atomic_read(&inode
->i_writecount
) != 1)
1624 /* some remained atomic pages should discarded */
1625 if (f2fs_is_atomic_file(inode
))
1626 f2fs_drop_inmem_pages(inode
);
1627 if (f2fs_is_volatile_file(inode
)) {
1628 set_inode_flag(inode
, FI_DROP_CACHE
);
1629 filemap_fdatawrite(inode
->i_mapping
);
1630 clear_inode_flag(inode
, FI_DROP_CACHE
);
1631 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1632 stat_dec_volatile_write(inode
);
1637 static int f2fs_file_flush(struct file
*file
, fl_owner_t id
)
1639 struct inode
*inode
= file_inode(file
);
1642 * If the process doing a transaction is crashed, we should do
1643 * roll-back. Otherwise, other reader/write can see corrupted database
1644 * until all the writers close its file. Since this should be done
1645 * before dropping file lock, it needs to do in ->flush.
1647 if (f2fs_is_atomic_file(inode
) &&
1648 F2FS_I(inode
)->inmem_task
== current
)
1649 f2fs_drop_inmem_pages(inode
);
1653 static int f2fs_setflags_common(struct inode
*inode
, u32 iflags
, u32 mask
)
1655 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1658 /* Is it quota file? Do not allow user to mess with it */
1659 if (IS_NOQUOTA(inode
))
1662 oldflags
= fi
->i_flags
;
1664 if ((iflags
^ oldflags
) & (F2FS_APPEND_FL
| F2FS_IMMUTABLE_FL
))
1665 if (!capable(CAP_LINUX_IMMUTABLE
))
1668 fi
->i_flags
= iflags
| (oldflags
& ~mask
);
1670 if (fi
->i_flags
& F2FS_PROJINHERIT_FL
)
1671 set_inode_flag(inode
, FI_PROJ_INHERIT
);
1673 clear_inode_flag(inode
, FI_PROJ_INHERIT
);
1675 inode
->i_ctime
= current_time(inode
);
1676 f2fs_set_inode_flags(inode
);
1677 f2fs_mark_inode_dirty_sync(inode
, true);
1681 /* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1684 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1685 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1686 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1687 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1690 static const struct {
1693 } f2fs_fsflags_map
[] = {
1694 { F2FS_SYNC_FL
, FS_SYNC_FL
},
1695 { F2FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
},
1696 { F2FS_APPEND_FL
, FS_APPEND_FL
},
1697 { F2FS_NODUMP_FL
, FS_NODUMP_FL
},
1698 { F2FS_NOATIME_FL
, FS_NOATIME_FL
},
1699 { F2FS_INDEX_FL
, FS_INDEX_FL
},
1700 { F2FS_DIRSYNC_FL
, FS_DIRSYNC_FL
},
1701 { F2FS_PROJINHERIT_FL
, FS_PROJINHERIT_FL
},
1704 #define F2FS_GETTABLE_FS_FL ( \
1712 FS_PROJINHERIT_FL | \
1714 FS_INLINE_DATA_FL | \
1717 #define F2FS_SETTABLE_FS_FL ( \
1726 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1727 static inline u32
f2fs_iflags_to_fsflags(u32 iflags
)
1732 for (i
= 0; i
< ARRAY_SIZE(f2fs_fsflags_map
); i
++)
1733 if (iflags
& f2fs_fsflags_map
[i
].iflag
)
1734 fsflags
|= f2fs_fsflags_map
[i
].fsflag
;
1739 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1740 static inline u32
f2fs_fsflags_to_iflags(u32 fsflags
)
1745 for (i
= 0; i
< ARRAY_SIZE(f2fs_fsflags_map
); i
++)
1746 if (fsflags
& f2fs_fsflags_map
[i
].fsflag
)
1747 iflags
|= f2fs_fsflags_map
[i
].iflag
;
1752 static int f2fs_ioc_getflags(struct file
*filp
, unsigned long arg
)
1754 struct inode
*inode
= file_inode(filp
);
1755 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
1756 u32 fsflags
= f2fs_iflags_to_fsflags(fi
->i_flags
);
1758 if (IS_ENCRYPTED(inode
))
1759 fsflags
|= FS_ENCRYPT_FL
;
1760 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
))
1761 fsflags
|= FS_INLINE_DATA_FL
;
1762 if (is_inode_flag_set(inode
, FI_PIN_FILE
))
1763 fsflags
|= FS_NOCOW_FL
;
1765 fsflags
&= F2FS_GETTABLE_FS_FL
;
1767 return put_user(fsflags
, (int __user
*)arg
);
1770 static int f2fs_ioc_setflags(struct file
*filp
, unsigned long arg
)
1772 struct inode
*inode
= file_inode(filp
);
1777 if (!inode_owner_or_capable(inode
))
1780 if (get_user(fsflags
, (int __user
*)arg
))
1783 if (fsflags
& ~F2FS_GETTABLE_FS_FL
)
1785 fsflags
&= F2FS_SETTABLE_FS_FL
;
1787 iflags
= f2fs_fsflags_to_iflags(fsflags
);
1788 if (f2fs_mask_flags(inode
->i_mode
, iflags
) != iflags
)
1791 ret
= mnt_want_write_file(filp
);
1797 ret
= f2fs_setflags_common(inode
, iflags
,
1798 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL
));
1799 inode_unlock(inode
);
1800 mnt_drop_write_file(filp
);
1804 static int f2fs_ioc_getversion(struct file
*filp
, unsigned long arg
)
1806 struct inode
*inode
= file_inode(filp
);
1808 return put_user(inode
->i_generation
, (int __user
*)arg
);
1811 static int f2fs_ioc_start_atomic_write(struct file
*filp
)
1813 struct inode
*inode
= file_inode(filp
);
1816 if (!inode_owner_or_capable(inode
))
1819 if (!S_ISREG(inode
->i_mode
))
1822 ret
= mnt_want_write_file(filp
);
1828 if (f2fs_is_atomic_file(inode
)) {
1829 if (is_inode_flag_set(inode
, FI_ATOMIC_REVOKE_REQUEST
))
1834 ret
= f2fs_convert_inline_inode(inode
);
1838 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1841 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
1842 * f2fs_is_atomic_file.
1844 if (get_dirty_pages(inode
))
1845 f2fs_warn(F2FS_I_SB(inode
), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1846 inode
->i_ino
, get_dirty_pages(inode
));
1847 ret
= filemap_write_and_wait_range(inode
->i_mapping
, 0, LLONG_MAX
);
1849 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1853 set_inode_flag(inode
, FI_ATOMIC_FILE
);
1854 clear_inode_flag(inode
, FI_ATOMIC_REVOKE_REQUEST
);
1855 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
1857 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1858 F2FS_I(inode
)->inmem_task
= current
;
1859 stat_inc_atomic_write(inode
);
1860 stat_update_max_atomic_write(inode
);
1862 inode_unlock(inode
);
1863 mnt_drop_write_file(filp
);
1867 static int f2fs_ioc_commit_atomic_write(struct file
*filp
)
1869 struct inode
*inode
= file_inode(filp
);
1872 if (!inode_owner_or_capable(inode
))
1875 ret
= mnt_want_write_file(filp
);
1879 f2fs_balance_fs(F2FS_I_SB(inode
), true);
1883 if (f2fs_is_volatile_file(inode
)) {
1888 if (f2fs_is_atomic_file(inode
)) {
1889 ret
= f2fs_commit_inmem_pages(inode
);
1893 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1895 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
1896 F2FS_I(inode
)->i_gc_failures
[GC_FAILURE_ATOMIC
] = 0;
1897 stat_dec_atomic_write(inode
);
1900 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 1, false);
1903 if (is_inode_flag_set(inode
, FI_ATOMIC_REVOKE_REQUEST
)) {
1904 clear_inode_flag(inode
, FI_ATOMIC_REVOKE_REQUEST
);
1907 inode_unlock(inode
);
1908 mnt_drop_write_file(filp
);
1912 static int f2fs_ioc_start_volatile_write(struct file
*filp
)
1914 struct inode
*inode
= file_inode(filp
);
1917 if (!inode_owner_or_capable(inode
))
1920 if (!S_ISREG(inode
->i_mode
))
1923 ret
= mnt_want_write_file(filp
);
1929 if (f2fs_is_volatile_file(inode
))
1932 ret
= f2fs_convert_inline_inode(inode
);
1936 stat_inc_volatile_write(inode
);
1937 stat_update_max_volatile_write(inode
);
1939 set_inode_flag(inode
, FI_VOLATILE_FILE
);
1940 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1942 inode_unlock(inode
);
1943 mnt_drop_write_file(filp
);
1947 static int f2fs_ioc_release_volatile_write(struct file
*filp
)
1949 struct inode
*inode
= file_inode(filp
);
1952 if (!inode_owner_or_capable(inode
))
1955 ret
= mnt_want_write_file(filp
);
1961 if (!f2fs_is_volatile_file(inode
))
1964 if (!f2fs_is_first_block_written(inode
)) {
1965 ret
= truncate_partial_data_page(inode
, 0, true);
1969 ret
= punch_hole(inode
, 0, F2FS_BLKSIZE
);
1971 inode_unlock(inode
);
1972 mnt_drop_write_file(filp
);
1976 static int f2fs_ioc_abort_volatile_write(struct file
*filp
)
1978 struct inode
*inode
= file_inode(filp
);
1981 if (!inode_owner_or_capable(inode
))
1984 ret
= mnt_want_write_file(filp
);
1990 if (f2fs_is_atomic_file(inode
))
1991 f2fs_drop_inmem_pages(inode
);
1992 if (f2fs_is_volatile_file(inode
)) {
1993 clear_inode_flag(inode
, FI_VOLATILE_FILE
);
1994 stat_dec_volatile_write(inode
);
1995 ret
= f2fs_do_sync_file(filp
, 0, LLONG_MAX
, 0, true);
1998 clear_inode_flag(inode
, FI_ATOMIC_REVOKE_REQUEST
);
2000 inode_unlock(inode
);
2002 mnt_drop_write_file(filp
);
2003 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
2007 static int f2fs_ioc_shutdown(struct file
*filp
, unsigned long arg
)
2009 struct inode
*inode
= file_inode(filp
);
2010 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2011 struct super_block
*sb
= sbi
->sb
;
2015 if (!capable(CAP_SYS_ADMIN
))
2018 if (get_user(in
, (__u32 __user
*)arg
))
2021 if (in
!= F2FS_GOING_DOWN_FULLSYNC
) {
2022 ret
= mnt_want_write_file(filp
);
2028 case F2FS_GOING_DOWN_FULLSYNC
:
2029 sb
= freeze_bdev(sb
->s_bdev
);
2035 f2fs_stop_checkpoint(sbi
, false);
2036 set_sbi_flag(sbi
, SBI_IS_SHUTDOWN
);
2037 thaw_bdev(sb
->s_bdev
, sb
);
2040 case F2FS_GOING_DOWN_METASYNC
:
2041 /* do checkpoint only */
2042 ret
= f2fs_sync_fs(sb
, 1);
2045 f2fs_stop_checkpoint(sbi
, false);
2046 set_sbi_flag(sbi
, SBI_IS_SHUTDOWN
);
2048 case F2FS_GOING_DOWN_NOSYNC
:
2049 f2fs_stop_checkpoint(sbi
, false);
2050 set_sbi_flag(sbi
, SBI_IS_SHUTDOWN
);
2052 case F2FS_GOING_DOWN_METAFLUSH
:
2053 f2fs_sync_meta_pages(sbi
, META
, LONG_MAX
, FS_META_IO
);
2054 f2fs_stop_checkpoint(sbi
, false);
2055 set_sbi_flag(sbi
, SBI_IS_SHUTDOWN
);
2057 case F2FS_GOING_DOWN_NEED_FSCK
:
2058 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
2059 set_sbi_flag(sbi
, SBI_CP_DISABLED_QUICK
);
2060 set_sbi_flag(sbi
, SBI_IS_DIRTY
);
2061 /* do checkpoint only */
2062 ret
= f2fs_sync_fs(sb
, 1);
2069 f2fs_stop_gc_thread(sbi
);
2070 f2fs_stop_discard_thread(sbi
);
2072 f2fs_drop_discard_cmd(sbi
);
2073 clear_opt(sbi
, DISCARD
);
2075 f2fs_update_time(sbi
, REQ_TIME
);
2077 if (in
!= F2FS_GOING_DOWN_FULLSYNC
)
2078 mnt_drop_write_file(filp
);
2080 trace_f2fs_shutdown(sbi
, in
, ret
);
2085 static int f2fs_ioc_fitrim(struct file
*filp
, unsigned long arg
)
2087 struct inode
*inode
= file_inode(filp
);
2088 struct super_block
*sb
= inode
->i_sb
;
2089 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
2090 struct fstrim_range range
;
2093 if (!capable(CAP_SYS_ADMIN
))
2096 if (!f2fs_hw_support_discard(F2FS_SB(sb
)))
2099 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
2103 ret
= mnt_want_write_file(filp
);
2107 range
.minlen
= max((unsigned int)range
.minlen
,
2108 q
->limits
.discard_granularity
);
2109 ret
= f2fs_trim_fs(F2FS_SB(sb
), &range
);
2110 mnt_drop_write_file(filp
);
2114 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
2117 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
2121 static bool uuid_is_nonzero(__u8 u
[16])
2125 for (i
= 0; i
< 16; i
++)
2131 static int f2fs_ioc_set_encryption_policy(struct file
*filp
, unsigned long arg
)
2133 struct inode
*inode
= file_inode(filp
);
2135 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode
)))
2138 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
2140 return fscrypt_ioctl_set_policy(filp
, (const void __user
*)arg
);
2143 static int f2fs_ioc_get_encryption_policy(struct file
*filp
, unsigned long arg
)
2145 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp
))))
2147 return fscrypt_ioctl_get_policy(filp
, (void __user
*)arg
);
2150 static int f2fs_ioc_get_encryption_pwsalt(struct file
*filp
, unsigned long arg
)
2152 struct inode
*inode
= file_inode(filp
);
2153 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2156 if (!f2fs_sb_has_encrypt(sbi
))
2159 err
= mnt_want_write_file(filp
);
2163 down_write(&sbi
->sb_lock
);
2165 if (uuid_is_nonzero(sbi
->raw_super
->encrypt_pw_salt
))
2168 /* update superblock with uuid */
2169 generate_random_uuid(sbi
->raw_super
->encrypt_pw_salt
);
2171 err
= f2fs_commit_super(sbi
, false);
2174 memset(sbi
->raw_super
->encrypt_pw_salt
, 0, 16);
2178 if (copy_to_user((__u8 __user
*)arg
, sbi
->raw_super
->encrypt_pw_salt
,
2182 up_write(&sbi
->sb_lock
);
2183 mnt_drop_write_file(filp
);
2187 static int f2fs_ioc_gc(struct file
*filp
, unsigned long arg
)
2189 struct inode
*inode
= file_inode(filp
);
2190 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2194 if (!capable(CAP_SYS_ADMIN
))
2197 if (get_user(sync
, (__u32 __user
*)arg
))
2200 if (f2fs_readonly(sbi
->sb
))
2203 ret
= mnt_want_write_file(filp
);
2208 if (!mutex_trylock(&sbi
->gc_mutex
)) {
2213 mutex_lock(&sbi
->gc_mutex
);
2216 ret
= f2fs_gc(sbi
, sync
, true, NULL_SEGNO
);
2218 mnt_drop_write_file(filp
);
2222 static int f2fs_ioc_gc_range(struct file
*filp
, unsigned long arg
)
2224 struct inode
*inode
= file_inode(filp
);
2225 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2226 struct f2fs_gc_range range
;
2230 if (!capable(CAP_SYS_ADMIN
))
2233 if (copy_from_user(&range
, (struct f2fs_gc_range __user
*)arg
,
2237 if (f2fs_readonly(sbi
->sb
))
2240 end
= range
.start
+ range
.len
;
2241 if (range
.start
< MAIN_BLKADDR(sbi
) || end
>= MAX_BLKADDR(sbi
)) {
2245 ret
= mnt_want_write_file(filp
);
2251 if (!mutex_trylock(&sbi
->gc_mutex
)) {
2256 mutex_lock(&sbi
->gc_mutex
);
2259 ret
= f2fs_gc(sbi
, range
.sync
, true, GET_SEGNO(sbi
, range
.start
));
2260 range
.start
+= BLKS_PER_SEC(sbi
);
2261 if (range
.start
<= end
)
2264 mnt_drop_write_file(filp
);
2268 static int f2fs_ioc_write_checkpoint(struct file
*filp
, unsigned long arg
)
2270 struct inode
*inode
= file_inode(filp
);
2271 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2274 if (!capable(CAP_SYS_ADMIN
))
2277 if (f2fs_readonly(sbi
->sb
))
2280 if (unlikely(is_sbi_flag_set(sbi
, SBI_CP_DISABLED
))) {
2281 f2fs_info(sbi
, "Skipping Checkpoint. Checkpoints currently disabled.");
2285 ret
= mnt_want_write_file(filp
);
2289 ret
= f2fs_sync_fs(sbi
->sb
, 1);
2291 mnt_drop_write_file(filp
);
2295 static int f2fs_defragment_range(struct f2fs_sb_info
*sbi
,
2297 struct f2fs_defragment
*range
)
2299 struct inode
*inode
= file_inode(filp
);
2300 struct f2fs_map_blocks map
= { .m_next_extent
= NULL
,
2301 .m_seg_type
= NO_CHECK_TYPE
,
2302 .m_may_create
= false };
2303 struct extent_info ei
= {0, 0, 0};
2304 pgoff_t pg_start
, pg_end
, next_pgofs
;
2305 unsigned int blk_per_seg
= sbi
->blocks_per_seg
;
2306 unsigned int total
= 0, sec_num
;
2307 block_t blk_end
= 0;
2308 bool fragmented
= false;
2311 /* if in-place-update policy is enabled, don't waste time here */
2312 if (f2fs_should_update_inplace(inode
, NULL
))
2315 pg_start
= range
->start
>> PAGE_SHIFT
;
2316 pg_end
= (range
->start
+ range
->len
) >> PAGE_SHIFT
;
2318 f2fs_balance_fs(sbi
, true);
2322 /* writeback all dirty pages in the range */
2323 err
= filemap_write_and_wait_range(inode
->i_mapping
, range
->start
,
2324 range
->start
+ range
->len
- 1);
2329 * lookup mapping info in extent cache, skip defragmenting if physical
2330 * block addresses are continuous.
2332 if (f2fs_lookup_extent_cache(inode
, pg_start
, &ei
)) {
2333 if (ei
.fofs
+ ei
.len
>= pg_end
)
2337 map
.m_lblk
= pg_start
;
2338 map
.m_next_pgofs
= &next_pgofs
;
2341 * lookup mapping info in dnode page cache, skip defragmenting if all
2342 * physical block addresses are continuous even if there are hole(s)
2343 * in logical blocks.
2345 while (map
.m_lblk
< pg_end
) {
2346 map
.m_len
= pg_end
- map
.m_lblk
;
2347 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
2351 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
2352 map
.m_lblk
= next_pgofs
;
2356 if (blk_end
&& blk_end
!= map
.m_pblk
)
2359 /* record total count of block that we're going to move */
2362 blk_end
= map
.m_pblk
+ map
.m_len
;
2364 map
.m_lblk
+= map
.m_len
;
2370 sec_num
= DIV_ROUND_UP(total
, BLKS_PER_SEC(sbi
));
2373 * make sure there are enough free section for LFS allocation, this can
2374 * avoid defragment running in SSR mode when free section are allocated
2377 if (has_not_enough_free_secs(sbi
, 0, sec_num
)) {
2382 map
.m_lblk
= pg_start
;
2383 map
.m_len
= pg_end
- pg_start
;
2386 while (map
.m_lblk
< pg_end
) {
2391 map
.m_len
= pg_end
- map
.m_lblk
;
2392 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
2396 if (!(map
.m_flags
& F2FS_MAP_FLAGS
)) {
2397 map
.m_lblk
= next_pgofs
;
2401 set_inode_flag(inode
, FI_DO_DEFRAG
);
2404 while (idx
< map
.m_lblk
+ map
.m_len
&& cnt
< blk_per_seg
) {
2407 page
= f2fs_get_lock_data_page(inode
, idx
, true);
2409 err
= PTR_ERR(page
);
2413 set_page_dirty(page
);
2414 f2fs_put_page(page
, 1);
2423 if (idx
< pg_end
&& cnt
< blk_per_seg
)
2426 clear_inode_flag(inode
, FI_DO_DEFRAG
);
2428 err
= filemap_fdatawrite(inode
->i_mapping
);
2433 clear_inode_flag(inode
, FI_DO_DEFRAG
);
2435 inode_unlock(inode
);
2437 range
->len
= (u64
)total
<< PAGE_SHIFT
;
2441 static int f2fs_ioc_defragment(struct file
*filp
, unsigned long arg
)
2443 struct inode
*inode
= file_inode(filp
);
2444 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2445 struct f2fs_defragment range
;
2448 if (!capable(CAP_SYS_ADMIN
))
2451 if (!S_ISREG(inode
->i_mode
) || f2fs_is_atomic_file(inode
))
2454 if (f2fs_readonly(sbi
->sb
))
2457 if (copy_from_user(&range
, (struct f2fs_defragment __user
*)arg
,
2461 /* verify alignment of offset & size */
2462 if (range
.start
& (F2FS_BLKSIZE
- 1) || range
.len
& (F2FS_BLKSIZE
- 1))
2465 if (unlikely((range
.start
+ range
.len
) >> PAGE_SHIFT
>
2466 sbi
->max_file_blocks
))
2469 err
= mnt_want_write_file(filp
);
2473 err
= f2fs_defragment_range(sbi
, filp
, &range
);
2474 mnt_drop_write_file(filp
);
2476 f2fs_update_time(sbi
, REQ_TIME
);
2480 if (copy_to_user((struct f2fs_defragment __user
*)arg
, &range
,
2487 static int f2fs_move_file_range(struct file
*file_in
, loff_t pos_in
,
2488 struct file
*file_out
, loff_t pos_out
, size_t len
)
2490 struct inode
*src
= file_inode(file_in
);
2491 struct inode
*dst
= file_inode(file_out
);
2492 struct f2fs_sb_info
*sbi
= F2FS_I_SB(src
);
2493 size_t olen
= len
, dst_max_i_size
= 0;
2497 if (file_in
->f_path
.mnt
!= file_out
->f_path
.mnt
||
2498 src
->i_sb
!= dst
->i_sb
)
2501 if (unlikely(f2fs_readonly(src
->i_sb
)))
2504 if (!S_ISREG(src
->i_mode
) || !S_ISREG(dst
->i_mode
))
2507 if (IS_ENCRYPTED(src
) || IS_ENCRYPTED(dst
))
2511 if (pos_in
== pos_out
)
2513 if (pos_out
> pos_in
&& pos_out
< pos_in
+ len
)
2520 if (!inode_trylock(dst
))
2525 if (pos_in
+ len
> src
->i_size
|| pos_in
+ len
< pos_in
)
2528 olen
= len
= src
->i_size
- pos_in
;
2529 if (pos_in
+ len
== src
->i_size
)
2530 len
= ALIGN(src
->i_size
, F2FS_BLKSIZE
) - pos_in
;
2536 dst_osize
= dst
->i_size
;
2537 if (pos_out
+ olen
> dst
->i_size
)
2538 dst_max_i_size
= pos_out
+ olen
;
2540 /* verify the end result is block aligned */
2541 if (!IS_ALIGNED(pos_in
, F2FS_BLKSIZE
) ||
2542 !IS_ALIGNED(pos_in
+ len
, F2FS_BLKSIZE
) ||
2543 !IS_ALIGNED(pos_out
, F2FS_BLKSIZE
))
2546 ret
= f2fs_convert_inline_inode(src
);
2550 ret
= f2fs_convert_inline_inode(dst
);
2554 /* write out all dirty pages from offset */
2555 ret
= filemap_write_and_wait_range(src
->i_mapping
,
2556 pos_in
, pos_in
+ len
);
2560 ret
= filemap_write_and_wait_range(dst
->i_mapping
,
2561 pos_out
, pos_out
+ len
);
2565 f2fs_balance_fs(sbi
, true);
2567 down_write(&F2FS_I(src
)->i_gc_rwsem
[WRITE
]);
2570 if (!down_write_trylock(&F2FS_I(dst
)->i_gc_rwsem
[WRITE
]))
2575 ret
= __exchange_data_block(src
, dst
, pos_in
>> F2FS_BLKSIZE_BITS
,
2576 pos_out
>> F2FS_BLKSIZE_BITS
,
2577 len
>> F2FS_BLKSIZE_BITS
, false);
2581 f2fs_i_size_write(dst
, dst_max_i_size
);
2582 else if (dst_osize
!= dst
->i_size
)
2583 f2fs_i_size_write(dst
, dst_osize
);
2585 f2fs_unlock_op(sbi
);
2588 up_write(&F2FS_I(dst
)->i_gc_rwsem
[WRITE
]);
2590 up_write(&F2FS_I(src
)->i_gc_rwsem
[WRITE
]);
2599 static int f2fs_ioc_move_range(struct file
*filp
, unsigned long arg
)
2601 struct f2fs_move_range range
;
2605 if (!(filp
->f_mode
& FMODE_READ
) ||
2606 !(filp
->f_mode
& FMODE_WRITE
))
2609 if (copy_from_user(&range
, (struct f2fs_move_range __user
*)arg
,
2613 dst
= fdget(range
.dst_fd
);
2617 if (!(dst
.file
->f_mode
& FMODE_WRITE
)) {
2622 err
= mnt_want_write_file(filp
);
2626 err
= f2fs_move_file_range(filp
, range
.pos_in
, dst
.file
,
2627 range
.pos_out
, range
.len
);
2629 mnt_drop_write_file(filp
);
2633 if (copy_to_user((struct f2fs_move_range __user
*)arg
,
2634 &range
, sizeof(range
)))
2641 static int f2fs_ioc_flush_device(struct file
*filp
, unsigned long arg
)
2643 struct inode
*inode
= file_inode(filp
);
2644 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2645 struct sit_info
*sm
= SIT_I(sbi
);
2646 unsigned int start_segno
= 0, end_segno
= 0;
2647 unsigned int dev_start_segno
= 0, dev_end_segno
= 0;
2648 struct f2fs_flush_device range
;
2651 if (!capable(CAP_SYS_ADMIN
))
2654 if (f2fs_readonly(sbi
->sb
))
2657 if (unlikely(is_sbi_flag_set(sbi
, SBI_CP_DISABLED
)))
2660 if (copy_from_user(&range
, (struct f2fs_flush_device __user
*)arg
,
2664 if (!f2fs_is_multi_device(sbi
) || sbi
->s_ndevs
- 1 <= range
.dev_num
||
2665 __is_large_section(sbi
)) {
2666 f2fs_warn(sbi
, "Can't flush %u in %d for segs_per_sec %u != 1",
2667 range
.dev_num
, sbi
->s_ndevs
, sbi
->segs_per_sec
);
2671 ret
= mnt_want_write_file(filp
);
2675 if (range
.dev_num
!= 0)
2676 dev_start_segno
= GET_SEGNO(sbi
, FDEV(range
.dev_num
).start_blk
);
2677 dev_end_segno
= GET_SEGNO(sbi
, FDEV(range
.dev_num
).end_blk
);
2679 start_segno
= sm
->last_victim
[FLUSH_DEVICE
];
2680 if (start_segno
< dev_start_segno
|| start_segno
>= dev_end_segno
)
2681 start_segno
= dev_start_segno
;
2682 end_segno
= min(start_segno
+ range
.segments
, dev_end_segno
);
2684 while (start_segno
< end_segno
) {
2685 if (!mutex_trylock(&sbi
->gc_mutex
)) {
2689 sm
->last_victim
[GC_CB
] = end_segno
+ 1;
2690 sm
->last_victim
[GC_GREEDY
] = end_segno
+ 1;
2691 sm
->last_victim
[ALLOC_NEXT
] = end_segno
+ 1;
2692 ret
= f2fs_gc(sbi
, true, true, start_segno
);
2700 mnt_drop_write_file(filp
);
2704 static int f2fs_ioc_get_features(struct file
*filp
, unsigned long arg
)
2706 struct inode
*inode
= file_inode(filp
);
2707 u32 sb_feature
= le32_to_cpu(F2FS_I_SB(inode
)->raw_super
->feature
);
2709 /* Must validate to set it with SQLite behavior in Android. */
2710 sb_feature
|= F2FS_FEATURE_ATOMIC_WRITE
;
2712 return put_user(sb_feature
, (u32 __user
*)arg
);
2716 int f2fs_transfer_project_quota(struct inode
*inode
, kprojid_t kprojid
)
2718 struct dquot
*transfer_to
[MAXQUOTAS
] = {};
2719 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2720 struct super_block
*sb
= sbi
->sb
;
2723 transfer_to
[PRJQUOTA
] = dqget(sb
, make_kqid_projid(kprojid
));
2724 if (!IS_ERR(transfer_to
[PRJQUOTA
])) {
2725 err
= __dquot_transfer(inode
, transfer_to
);
2727 set_sbi_flag(sbi
, SBI_QUOTA_NEED_REPAIR
);
2728 dqput(transfer_to
[PRJQUOTA
]);
2733 static int f2fs_ioc_setproject(struct file
*filp
, __u32 projid
)
2735 struct inode
*inode
= file_inode(filp
);
2736 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
2737 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2742 if (!f2fs_sb_has_project_quota(sbi
)) {
2743 if (projid
!= F2FS_DEF_PROJID
)
2749 if (!f2fs_has_extra_attr(inode
))
2752 kprojid
= make_kprojid(&init_user_ns
, (projid_t
)projid
);
2754 if (projid_eq(kprojid
, F2FS_I(inode
)->i_projid
))
2758 /* Is it quota file? Do not allow user to mess with it */
2759 if (IS_NOQUOTA(inode
))
2762 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
2764 return PTR_ERR(ipage
);
2766 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage
), fi
->i_extra_isize
,
2769 f2fs_put_page(ipage
, 1);
2772 f2fs_put_page(ipage
, 1);
2774 err
= dquot_initialize(inode
);
2779 err
= f2fs_transfer_project_quota(inode
, kprojid
);
2783 F2FS_I(inode
)->i_projid
= kprojid
;
2784 inode
->i_ctime
= current_time(inode
);
2785 f2fs_mark_inode_dirty_sync(inode
, true);
2787 f2fs_unlock_op(sbi
);
2791 int f2fs_transfer_project_quota(struct inode
*inode
, kprojid_t kprojid
)
2796 static int f2fs_ioc_setproject(struct file
*filp
, __u32 projid
)
2798 if (projid
!= F2FS_DEF_PROJID
)
2804 /* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
2807 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
2808 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
2809 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
2812 static const struct {
2815 } f2fs_xflags_map
[] = {
2816 { F2FS_SYNC_FL
, FS_XFLAG_SYNC
},
2817 { F2FS_IMMUTABLE_FL
, FS_XFLAG_IMMUTABLE
},
2818 { F2FS_APPEND_FL
, FS_XFLAG_APPEND
},
2819 { F2FS_NODUMP_FL
, FS_XFLAG_NODUMP
},
2820 { F2FS_NOATIME_FL
, FS_XFLAG_NOATIME
},
2821 { F2FS_PROJINHERIT_FL
, FS_XFLAG_PROJINHERIT
},
2824 #define F2FS_SUPPORTED_XFLAGS ( \
2826 FS_XFLAG_IMMUTABLE | \
2829 FS_XFLAG_NOATIME | \
2830 FS_XFLAG_PROJINHERIT)
2832 /* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
2833 static inline u32
f2fs_iflags_to_xflags(u32 iflags
)
2838 for (i
= 0; i
< ARRAY_SIZE(f2fs_xflags_map
); i
++)
2839 if (iflags
& f2fs_xflags_map
[i
].iflag
)
2840 xflags
|= f2fs_xflags_map
[i
].xflag
;
2845 /* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
2846 static inline u32
f2fs_xflags_to_iflags(u32 xflags
)
2851 for (i
= 0; i
< ARRAY_SIZE(f2fs_xflags_map
); i
++)
2852 if (xflags
& f2fs_xflags_map
[i
].xflag
)
2853 iflags
|= f2fs_xflags_map
[i
].iflag
;
2858 static int f2fs_ioc_fsgetxattr(struct file
*filp
, unsigned long arg
)
2860 struct inode
*inode
= file_inode(filp
);
2861 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
2864 memset(&fa
, 0, sizeof(struct fsxattr
));
2865 fa
.fsx_xflags
= f2fs_iflags_to_xflags(fi
->i_flags
);
2867 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode
)))
2868 fa
.fsx_projid
= (__u32
)from_kprojid(&init_user_ns
,
2871 if (copy_to_user((struct fsxattr __user
*)arg
, &fa
, sizeof(fa
)))
2876 static int f2fs_ioctl_check_project(struct inode
*inode
, struct fsxattr
*fa
)
2879 * Project Quota ID state is only allowed to change from within the init
2880 * namespace. Enforce that restriction only if we are trying to change
2881 * the quota ID state. Everything else is allowed in user namespaces.
2883 if (current_user_ns() == &init_user_ns
)
2886 if (__kprojid_val(F2FS_I(inode
)->i_projid
) != fa
->fsx_projid
)
2889 if (F2FS_I(inode
)->i_flags
& F2FS_PROJINHERIT_FL
) {
2890 if (!(fa
->fsx_xflags
& FS_XFLAG_PROJINHERIT
))
2893 if (fa
->fsx_xflags
& FS_XFLAG_PROJINHERIT
)
2900 static int f2fs_ioc_fssetxattr(struct file
*filp
, unsigned long arg
)
2902 struct inode
*inode
= file_inode(filp
);
2907 if (copy_from_user(&fa
, (struct fsxattr __user
*)arg
, sizeof(fa
)))
2910 /* Make sure caller has proper permission */
2911 if (!inode_owner_or_capable(inode
))
2914 if (fa
.fsx_xflags
& ~F2FS_SUPPORTED_XFLAGS
)
2917 iflags
= f2fs_xflags_to_iflags(fa
.fsx_xflags
);
2918 if (f2fs_mask_flags(inode
->i_mode
, iflags
) != iflags
)
2921 err
= mnt_want_write_file(filp
);
2926 err
= f2fs_ioctl_check_project(inode
, &fa
);
2929 err
= f2fs_setflags_common(inode
, iflags
,
2930 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS
));
2934 err
= f2fs_ioc_setproject(filp
, fa
.fsx_projid
);
2936 inode_unlock(inode
);
2937 mnt_drop_write_file(filp
);
2941 int f2fs_pin_file_control(struct inode
*inode
, bool inc
)
2943 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
2944 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2946 /* Use i_gc_failures for normal file as a risk signal. */
2948 f2fs_i_gc_failures_write(inode
,
2949 fi
->i_gc_failures
[GC_FAILURE_PIN
] + 1);
2951 if (fi
->i_gc_failures
[GC_FAILURE_PIN
] > sbi
->gc_pin_file_threshold
) {
2952 f2fs_warn(sbi
, "%s: Enable GC = ino %lx after %x GC trials",
2953 __func__
, inode
->i_ino
,
2954 fi
->i_gc_failures
[GC_FAILURE_PIN
]);
2955 clear_inode_flag(inode
, FI_PIN_FILE
);
2961 static int f2fs_ioc_set_pin_file(struct file
*filp
, unsigned long arg
)
2963 struct inode
*inode
= file_inode(filp
);
2967 if (get_user(pin
, (__u32 __user
*)arg
))
2970 if (!S_ISREG(inode
->i_mode
))
2973 if (f2fs_readonly(F2FS_I_SB(inode
)->sb
))
2976 ret
= mnt_want_write_file(filp
);
2982 if (f2fs_should_update_outplace(inode
, NULL
)) {
2988 clear_inode_flag(inode
, FI_PIN_FILE
);
2989 f2fs_i_gc_failures_write(inode
, 0);
2993 if (f2fs_pin_file_control(inode
, false)) {
2997 ret
= f2fs_convert_inline_inode(inode
);
3001 set_inode_flag(inode
, FI_PIN_FILE
);
3002 ret
= F2FS_I(inode
)->i_gc_failures
[GC_FAILURE_PIN
];
3004 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
3006 inode_unlock(inode
);
3007 mnt_drop_write_file(filp
);
3011 static int f2fs_ioc_get_pin_file(struct file
*filp
, unsigned long arg
)
3013 struct inode
*inode
= file_inode(filp
);
3016 if (is_inode_flag_set(inode
, FI_PIN_FILE
))
3017 pin
= F2FS_I(inode
)->i_gc_failures
[GC_FAILURE_PIN
];
3018 return put_user(pin
, (u32 __user
*)arg
);
3021 int f2fs_precache_extents(struct inode
*inode
)
3023 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
3024 struct f2fs_map_blocks map
;
3025 pgoff_t m_next_extent
;
3029 if (is_inode_flag_set(inode
, FI_NO_EXTENT
))
3033 map
.m_next_pgofs
= NULL
;
3034 map
.m_next_extent
= &m_next_extent
;
3035 map
.m_seg_type
= NO_CHECK_TYPE
;
3036 map
.m_may_create
= false;
3037 end
= F2FS_I_SB(inode
)->max_file_blocks
;
3039 while (map
.m_lblk
< end
) {
3040 map
.m_len
= end
- map
.m_lblk
;
3042 down_write(&fi
->i_gc_rwsem
[WRITE
]);
3043 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_PRECACHE
);
3044 up_write(&fi
->i_gc_rwsem
[WRITE
]);
3048 map
.m_lblk
= m_next_extent
;
3054 static int f2fs_ioc_precache_extents(struct file
*filp
, unsigned long arg
)
3056 return f2fs_precache_extents(file_inode(filp
));
3059 static int f2fs_ioc_resize_fs(struct file
*filp
, unsigned long arg
)
3061 struct f2fs_sb_info
*sbi
= F2FS_I_SB(file_inode(filp
));
3065 if (!capable(CAP_SYS_ADMIN
))
3068 if (f2fs_readonly(sbi
->sb
))
3071 if (copy_from_user(&block_count
, (void __user
*)arg
,
3072 sizeof(block_count
)))
3075 ret
= f2fs_resize_fs(sbi
, block_count
);
3080 long f2fs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
3082 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp
)))))
3086 case F2FS_IOC_GETFLAGS
:
3087 return f2fs_ioc_getflags(filp
, arg
);
3088 case F2FS_IOC_SETFLAGS
:
3089 return f2fs_ioc_setflags(filp
, arg
);
3090 case F2FS_IOC_GETVERSION
:
3091 return f2fs_ioc_getversion(filp
, arg
);
3092 case F2FS_IOC_START_ATOMIC_WRITE
:
3093 return f2fs_ioc_start_atomic_write(filp
);
3094 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
3095 return f2fs_ioc_commit_atomic_write(filp
);
3096 case F2FS_IOC_START_VOLATILE_WRITE
:
3097 return f2fs_ioc_start_volatile_write(filp
);
3098 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
3099 return f2fs_ioc_release_volatile_write(filp
);
3100 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
3101 return f2fs_ioc_abort_volatile_write(filp
);
3102 case F2FS_IOC_SHUTDOWN
:
3103 return f2fs_ioc_shutdown(filp
, arg
);
3105 return f2fs_ioc_fitrim(filp
, arg
);
3106 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
3107 return f2fs_ioc_set_encryption_policy(filp
, arg
);
3108 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
3109 return f2fs_ioc_get_encryption_policy(filp
, arg
);
3110 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
3111 return f2fs_ioc_get_encryption_pwsalt(filp
, arg
);
3112 case F2FS_IOC_GARBAGE_COLLECT
:
3113 return f2fs_ioc_gc(filp
, arg
);
3114 case F2FS_IOC_GARBAGE_COLLECT_RANGE
:
3115 return f2fs_ioc_gc_range(filp
, arg
);
3116 case F2FS_IOC_WRITE_CHECKPOINT
:
3117 return f2fs_ioc_write_checkpoint(filp
, arg
);
3118 case F2FS_IOC_DEFRAGMENT
:
3119 return f2fs_ioc_defragment(filp
, arg
);
3120 case F2FS_IOC_MOVE_RANGE
:
3121 return f2fs_ioc_move_range(filp
, arg
);
3122 case F2FS_IOC_FLUSH_DEVICE
:
3123 return f2fs_ioc_flush_device(filp
, arg
);
3124 case F2FS_IOC_GET_FEATURES
:
3125 return f2fs_ioc_get_features(filp
, arg
);
3126 case F2FS_IOC_FSGETXATTR
:
3127 return f2fs_ioc_fsgetxattr(filp
, arg
);
3128 case F2FS_IOC_FSSETXATTR
:
3129 return f2fs_ioc_fssetxattr(filp
, arg
);
3130 case F2FS_IOC_GET_PIN_FILE
:
3131 return f2fs_ioc_get_pin_file(filp
, arg
);
3132 case F2FS_IOC_SET_PIN_FILE
:
3133 return f2fs_ioc_set_pin_file(filp
, arg
);
3134 case F2FS_IOC_PRECACHE_EXTENTS
:
3135 return f2fs_ioc_precache_extents(filp
, arg
);
3136 case F2FS_IOC_RESIZE_FS
:
3137 return f2fs_ioc_resize_fs(filp
, arg
);
3143 static ssize_t
f2fs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
3145 struct file
*file
= iocb
->ki_filp
;
3146 struct inode
*inode
= file_inode(file
);
3149 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
)))) {
3154 if ((iocb
->ki_flags
& IOCB_NOWAIT
) && !(iocb
->ki_flags
& IOCB_DIRECT
)) {
3159 if (!inode_trylock(inode
)) {
3160 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
3167 ret
= generic_write_checks(iocb
, from
);
3169 bool preallocated
= false;
3170 size_t target_size
= 0;
3173 if (iov_iter_fault_in_readable(from
, iov_iter_count(from
)))
3174 set_inode_flag(inode
, FI_NO_PREALLOC
);
3176 if ((iocb
->ki_flags
& IOCB_NOWAIT
)) {
3177 if (!f2fs_overwrite_io(inode
, iocb
->ki_pos
,
3178 iov_iter_count(from
)) ||
3179 f2fs_has_inline_data(inode
) ||
3180 f2fs_force_buffered_io(inode
, iocb
, from
)) {
3181 clear_inode_flag(inode
, FI_NO_PREALLOC
);
3182 inode_unlock(inode
);
3187 preallocated
= true;
3188 target_size
= iocb
->ki_pos
+ iov_iter_count(from
);
3190 err
= f2fs_preallocate_blocks(iocb
, from
);
3192 clear_inode_flag(inode
, FI_NO_PREALLOC
);
3193 inode_unlock(inode
);
3198 ret
= __generic_file_write_iter(iocb
, from
);
3199 clear_inode_flag(inode
, FI_NO_PREALLOC
);
3201 /* if we couldn't write data, we should deallocate blocks. */
3202 if (preallocated
&& i_size_read(inode
) < target_size
)
3203 f2fs_truncate(inode
);
3206 f2fs_update_iostat(F2FS_I_SB(inode
), APP_WRITE_IO
, ret
);
3208 inode_unlock(inode
);
3210 trace_f2fs_file_write_iter(inode
, iocb
->ki_pos
,
3211 iov_iter_count(from
), ret
);
3213 ret
= generic_write_sync(iocb
, ret
);
3217 #ifdef CONFIG_COMPAT
3218 long f2fs_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3221 case F2FS_IOC32_GETFLAGS
:
3222 cmd
= F2FS_IOC_GETFLAGS
;
3224 case F2FS_IOC32_SETFLAGS
:
3225 cmd
= F2FS_IOC_SETFLAGS
;
3227 case F2FS_IOC32_GETVERSION
:
3228 cmd
= F2FS_IOC_GETVERSION
;
3230 case F2FS_IOC_START_ATOMIC_WRITE
:
3231 case F2FS_IOC_COMMIT_ATOMIC_WRITE
:
3232 case F2FS_IOC_START_VOLATILE_WRITE
:
3233 case F2FS_IOC_RELEASE_VOLATILE_WRITE
:
3234 case F2FS_IOC_ABORT_VOLATILE_WRITE
:
3235 case F2FS_IOC_SHUTDOWN
:
3236 case F2FS_IOC_SET_ENCRYPTION_POLICY
:
3237 case F2FS_IOC_GET_ENCRYPTION_PWSALT
:
3238 case F2FS_IOC_GET_ENCRYPTION_POLICY
:
3239 case F2FS_IOC_GARBAGE_COLLECT
:
3240 case F2FS_IOC_GARBAGE_COLLECT_RANGE
:
3241 case F2FS_IOC_WRITE_CHECKPOINT
:
3242 case F2FS_IOC_DEFRAGMENT
:
3243 case F2FS_IOC_MOVE_RANGE
:
3244 case F2FS_IOC_FLUSH_DEVICE
:
3245 case F2FS_IOC_GET_FEATURES
:
3246 case F2FS_IOC_FSGETXATTR
:
3247 case F2FS_IOC_FSSETXATTR
:
3248 case F2FS_IOC_GET_PIN_FILE
:
3249 case F2FS_IOC_SET_PIN_FILE
:
3250 case F2FS_IOC_PRECACHE_EXTENTS
:
3251 case F2FS_IOC_RESIZE_FS
:
3254 return -ENOIOCTLCMD
;
3256 return f2fs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
3260 const struct file_operations f2fs_file_operations
= {
3261 .llseek
= f2fs_llseek
,
3262 .read_iter
= generic_file_read_iter
,
3263 .write_iter
= f2fs_file_write_iter
,
3264 .open
= f2fs_file_open
,
3265 .release
= f2fs_release_file
,
3266 .mmap
= f2fs_file_mmap
,
3267 .flush
= f2fs_file_flush
,
3268 .fsync
= f2fs_sync_file
,
3269 .fallocate
= f2fs_fallocate
,
3270 .unlocked_ioctl
= f2fs_ioctl
,
3271 #ifdef CONFIG_COMPAT
3272 .compat_ioctl
= f2fs_compat_ioctl
,
3274 .splice_read
= generic_file_splice_read
,
3275 .splice_write
= iter_file_splice_write
,